Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve unit test #117

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions config/stubbed.csv
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ SoundPlayer::InitSoundBuffers
SoundPlayer::PlaySoundByIdx
SoundPlayer::Release
SoundPlayer::PlaySounds
Supervisor::OnUpdate
Supervisor::OnDraw
Supervisor::DeletedCallback
Supervisor::CreateBackBuffer
Supervisor::SetupDInput
Expand Down
109 changes: 73 additions & 36 deletions tests/test_Pbg3Archive.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
#include <direct.h>
#include <fstream>
#include <stdlib.h>
#include <vector>

#include "pbg3/Pbg3Archive.hpp"
#include <munit.h>

static char *file_name_params[] = {"ascii.png", "th06logo.jpg", "text.anm", NULL};

static MunitParameterEnum test_params[] = {
{"file_name", file_name_params},
{NULL, NULL},
};

static MunitResult test_read_raw(const MunitParameter params[], void *user_data)
{
Pbg3Archive archive;
Expand All @@ -23,63 +32,91 @@ static MunitResult test_read_raw(const MunitParameter params[], void *user_data)

static MunitResult test_read_decompress(const MunitParameter params[], void *user_data)
{
Pbg3Archive archive;
munit_assert_int(archive.Load("resources/KOUMAKYO_IN.dat"), !=, 0);

// Read from PBG3
i32 entryIdx = archive.FindEntry("th06logo.jpg");
munit_assert_int(entryIdx, !=, -1);
u8 *entry = archive.ReadDecompressEntry(entryIdx, "th06logo.jpg");
munit_assert_not_null(entry);
u32 entrySize = archive.GetEntrySize(entryIdx);
munit_assert_int(entrySize, !=, 0);

// Write decompressed file to disk.
std::ofstream outfile("resources/th06logo2.jpg.expected", std::ios::out | std::ios::binary);
outfile.write((char *)entry, entrySize);

// Read file on disk
std::ifstream infile("resources/th06logo.jpg", std::ios::binary);
std::vector<char> buffer(std::istreambuf_iterator<char>(infile), std::istreambuf_iterator<char>());
u8 *reference = (u8 *)&buffer[0];
u32 referenceSize = buffer.size();
char *archiveNameRelative = "resources/KOUMAKYO_IN.dat";
char *fileName = params[0].value;
char archiveName[MAX_PATH] = {0};

munit_assert_int(entrySize, ==, referenceSize);
munit_assert_memory_equal(entrySize, entry, reference);
return MUNIT_OK;
}
if (_fullpath(archiveName, archiveNameRelative, MAX_PATH) == NULL)
{
munit_errorf_ex(__FILE__, __LINE__, "Failed to canonicalize %s", archiveNameRelative);
return MUNIT_FAIL;
}

static MunitResult test_read_decompress_anm(const MunitParameter params[], void *user_data)
{
Pbg3Archive archive;
munit_assert_int(archive.Load("resources/KOUMAKYO_IN.dat"), !=, 0);
munit_assert_int(archive.Load(archiveName), !=, 0);

// Read from PBG3
i32 entryIdx = archive.FindEntry("text.anm");
// Read from PBG3.
i32 entryIdx = archive.FindEntry(fileName);
munit_assert_int(entryIdx, !=, -1);
u8 *entry = archive.ReadDecompressEntry(entryIdx, "text.anm");
u8 *entry = archive.ReadDecompressEntry(entryIdx, fileName);
munit_assert_not_null(entry);
u32 entrySize = archive.GetEntrySize(entryIdx);
munit_assert_int(entrySize, !=, 0);

// Write decompressed file to disk.
std::ofstream outfile("resources/text.anm.expected", std::ios::out | std::ios::binary);
// Create a temporary directory to work from.
char tmpNameStr[MAX_PATH] = {0};
tmpnam(tmpNameStr);
// Remove trailing dot.
for (int i = 0; i < MAX_PATH; i++)
{
if (i == 0)
{
if (tmpNameStr[i - 1] == '.')
{
tmpNameStr[i - 1] = '\0';
}
break;
}
}

char tmpPathCStr[MAX_PATH] = {0};
DWORD tmpPathSize = GetTempPathA(MAX_PATH, tmpPathCStr);
munit_assert_int(tmpPathSize, !=, 0);
std::string tmpPath(tmpPathCStr, tmpPathSize);
if (tmpNameStr[0] != '\\')
{
tmpPath += "\\";
}
tmpPath += tmpNameStr;
_mkdir(tmpPath.c_str());

munit_logf_ex(MUNIT_LOG_INFO, __FILE__, __LINE__, "Temporary path: %s", tmpPath.c_str());

// Write decompressed file to disk. Useful for debugging.
std::string actualPath(tmpPath);
actualPath += "/";
actualPath += fileName;
actualPath += ".actual";
std::ofstream outfile(actualPath.c_str(), std::ios::out | std::ios::binary);
outfile.write((char *)entry, entrySize);

// Read file on disk
std::ifstream infile("resources/text.anm", std::ios::binary);
// Extract using reference implementation.
std::string cmd("cd /d ");
cmd += tmpPath;
cmd += " && thdat -x 6 ";
cmd += archiveName;
cmd += " ";
cmd += params[0].value;
munit_logf_ex(MUNIT_LOG_INFO, __FILE__, __LINE__, "Running %s", cmd.c_str());
munit_assert_int(system(cmd.c_str()), !=, -1);

// Read reference extraction
std::string referencePath(tmpPath);
referencePath += "/";
referencePath += params[0].value;
std::ifstream infile(referencePath.c_str(), std::ios::binary);
std::vector<char> buffer(std::istreambuf_iterator<char>(infile), std::istreambuf_iterator<char>());
u8 *reference = (u8 *)&buffer[0];
u32 referenceSize = buffer.size();

// Ensure they are equal.
munit_assert_int(entrySize, ==, referenceSize);
munit_assert_memory_equal(entrySize, entry, reference);
return MUNIT_OK;
}

static MunitTest pbg3archives_test_suite_tests[] = {
{"/read_decompress_anm", test_read_decompress_anm, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
{"/read_decompress", test_read_decompress, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
{"/read_decompress", test_read_decompress, NULL, NULL, MUNIT_TEST_OPTION_NONE, test_params},
{"/read_raw", test_read_raw, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL},
/* Mark the end of the array with an entry where the test
* function is NULL */
Expand Down
Loading