More clean testing, with a conversion to C++!

This commit is contained in:
archshift 2014-11-21 21:26:24 -08:00
parent 1bfde024ab
commit ad861177cc
10 changed files with 117 additions and 79 deletions

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include <3ds/gfx.h> #include <3ds.h>
void drawFrames(); void drawFrames();
void print(gfxScreen_t screen, const char* format, ...); void print(gfxScreen_t screen, const char* format, ...);

15
source/test.cpp Normal file
View File

@ -0,0 +1,15 @@
#include "test.h"
#include <3ds.h>
#include "output.h"
void Test(std::string group, std::string name, std::function<bool (void)> test)
{
print(GFX_TOP, "%s: %s - %s\n", group.c_str(), name.c_str(), test() ? "SUCCESS" : "FAILURE");
}
void TestResult(std::string group, std::string name, std::function<int (void)> test)
{
print(GFX_TOP, "%s: %s - %s\n", group.c_str(), name.c_str(), test() == 0 ? "SUCCESS" : "FAILURE");
}

7
source/test.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <functional>
void Test(std::string group, std::string name, std::function<bool (void)> test);
void TestResult(std::string group, std::string name, std::function<int (void)> test);

View File

@ -1,78 +0,0 @@
#include "test_fs.h"
#include <3ds.h>
#include "output.h"
static void FS_TestSdmc()
{
FS_archive sdmcArchive = (FS_archive) { 0x00000009, { PATH_EMPTY, 1, (u8*) "" } };
// Open SDMC
unsigned int openArchiveResult = FSUSER_OpenArchive(NULL, &sdmcArchive);
print(GFX_TOP, "SDMC: Opening SDMC Archive - [%u]\n", openArchiveResult);
Handle dirHandle;
FS_path dirPath = FS_makePath(PATH_CHAR, "/new_dir");
// Create Directory
unsigned int createDirResult = FSUSER_CreateDirectory(NULL, sdmcArchive, dirPath);
print(GFX_TOP, "SDMC: Creating Directory '%s' - [%u]\n", dirPath.data, createDirResult);
// Open Directory
unsigned int openDirResult = FSUSER_OpenDirectory(NULL, &dirHandle, sdmcArchive, dirPath);
print(GFX_TOP, "SDMC: Opening Directory '%s' - [%u]\n", dirPath.data, openDirResult);
Handle fileHandle;
FS_path filePath = FS_makePath(PATH_CHAR, "/new_dir/new_file.txt");
// Open File
unsigned int openFileResult = FSUSER_OpenFile(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_WRITE | FS_OPEN_CREATE, 0);
print(GFX_TOP, "SDMC: Opening File '%s' - [%u]\n", filePath.data, openFileResult);
// Write File
u32 bytesWritten;
const char* stringWritten = "A string\n";
unsigned int writeFileResult = FSFILE_Write(fileHandle, &bytesWritten, 0, stringWritten, strlen(stringWritten), FS_WRITE_FLUSH);
print(GFX_TOP, "SDMC: Writing Data to File .. - [%u]\n", writeFileResult);
// Check File Size
u64 fileSize;
unsigned int getFileSizeResult = FSFILE_GetSize(fileHandle, &fileSize);
print(GFX_TOP, "SDMC: Getting Size of File .. - [%u]\n", getFileSizeResult);
// Verify File Size
print(GFX_TOP, "SDMC: Verifying Size with Written Bytes - %s\n", fileSize == bytesWritten ? "SUCCESS" : "FAILURE");
// Close File
unsigned int closeFileResult = FSFILE_Close(fileHandle);
print(GFX_TOP, "SDMC: Closing File .. - [%u]\n", closeFileResult);
// Close Directory
unsigned int closeDirResult = FSDIR_Close(dirHandle);
print(GFX_TOP, "SDMC: Closing Directory .. - [%u]\n", closeDirResult);
// Delete File
unsigned int deleteFileResult = FSUSER_DeleteFile(NULL, sdmcArchive, filePath);
print(GFX_TOP, "SDMC: Deleting File .. - [%u]\n", deleteFileResult);
// Delete Directory
unsigned int deleteDirResult = FSUSER_DeleteDirectory(NULL, sdmcArchive, dirPath);
print(GFX_TOP, "SDMC: Deleting Directory .. - [%u]\n", deleteDirResult);
// Close SDMC
unsigned int closeArchiveResult = FSUSER_CloseArchive(NULL, &sdmcArchive);
print(GFX_TOP, "SDMC: Closing SDMC Archive - [%u]\n", closeArchiveResult);
}
void FS_TestAll()
{
unsigned int initResult = fsInit();
print(GFX_TOP, "fsInit - [%u]\n", initResult);
FS_TestSdmc();
unsigned int exitResult = fsExit();
print(GFX_TOP, "fsExit - [%u]", exitResult);
}

94
source/test_fs.cpp Normal file
View File

@ -0,0 +1,94 @@
#include "test_fs.h"
#include <3ds.h>
#include "test.h"
static void FS_TestSdmc()
{
FS_archive sdmcArchive = (FS_archive) { 0x00000009, { PATH_EMPTY, 1, (u8*) "" } };
FS_path dirPath = FS_makePath(PATH_CHAR, "/new_dir");
Handle dirHandle;
FS_path filePath = FS_makePath(PATH_CHAR, "/new_dir/new_file.txt");
Handle fileHandle;
u64 fileSize;
u32 bytesWritten;
// Open SDMC
TestResult("SDMC", "Opening archive", [&]{
return FSUSER_OpenArchive(NULL, &sdmcArchive);
});
// Create Directory
TestResult("SDMC", "Creating directory", [&]{
return FSUSER_CreateDirectory(NULL, sdmcArchive, dirPath);
});
// Open Directory
TestResult("SDMC", "Opening directory handle", [&]{
return FSUSER_OpenDirectory(NULL, &dirHandle, sdmcArchive, dirPath);
});
// Open File
TestResult("SDMC", "Opening file handle", [&]{
return FSUSER_OpenFile(NULL, &fileHandle, sdmcArchive, filePath, FS_OPEN_WRITE | FS_OPEN_CREATE, 0);
});
// Write File
TestResult("SDMC", "Writing data to file", [&]{
const char* stringWritten = "A string\n";
return FSFILE_Write(fileHandle, &bytesWritten, 0, stringWritten, strlen(stringWritten), FS_WRITE_FLUSH);
});
// Check File Size
TestResult("SDMC", "Getting size of file", [&]{
return FSFILE_GetSize(fileHandle, &fileSize);
});
// Verify File Size
Test("SDMC", "Verifying size with written bytes", [&]{
return fileSize == bytesWritten;
});
// Close File
TestResult("SDMC", "Closing file handle", [&]{
return FSFILE_Close(fileHandle);
});
// Close Directory
TestResult("SDMC", "Closing directory handle", [&]{
return FSDIR_Close(dirHandle);
});
// Delete File
TestResult("SDMC", "Deleting file", [&]{
return FSUSER_DeleteFile(NULL, sdmcArchive, filePath);
});
// Delete Directory
TestResult("SDMC", "Deleting directory", [&]{
return FSUSER_DeleteDirectory(NULL, sdmcArchive, dirPath);
});
// Close SDMC
TestResult("SDMC", "Closing archive", [&]{
return FSUSER_CloseArchive(NULL, &sdmcArchive);
});
}
void FS_TestAll()
{
TestResult("FS", "Initializing service", [&]{
return fsInit();
});
FS_TestSdmc();
TestResult("FS", "Exiting service", [&]{
return fsExit();
});
}