XPlor/tests/360/autotest_cod2_360.cpp

173 lines
5.4 KiB
C++
Raw Permalink Normal View History

2025-04-04 20:40:12 -04:00
#include <QtTest/QtTest>
#include <QDirIterator>
#include <QFileInfo>
#include "autotest_cod.h"
#include "compression.h"
2025-05-03 09:57:47 -04:00
#include "fastfile_factory.h"
2025-04-04 20:40:12 -04:00
class AutoTest_COD2_360 : public AutoTest_COD {
Q_OBJECT
const QString EXPORT_DIR = "./exports/cod2/360";
private slots:
void initTestCase();
2025-04-04 20:40:12 -04:00
void testDecompression_data();
void testDecompression();
2025-04-04 20:40:12 -04:00
void testCompression_data();
void testCompression();
2025-05-03 09:57:47 -04:00
void testFactory_data();
void testFactory();
2025-04-04 20:40:12 -04:00
void cleanupTestCase();
};
void AutoTest_COD2_360::initTestCase() {
// Ensure the exports directory exists.
createDirectory(EXPORT_DIR);
}
void AutoTest_COD2_360::testDecompression_data() {
AutoTest_COD::testDecompression_data();
2025-04-04 20:40:12 -04:00
}
void AutoTest_COD2_360::testDecompression() {
QFETCH(QString, fastFilePath);
2025-04-04 20:40:12 -04:00
const QString testName = "Decompress: " + fastFilePath;
2025-04-23 00:09:35 -04:00
2025-04-04 20:40:12 -04:00
// Open the original .ff file.
QFile testFastFile(fastFilePath);
2025-04-23 00:09:35 -04:00
bool fastFileOpened = testFastFile.open(QIODevice::ReadOnly);
if (!fastFileOpened) {
recordResult(testName, false);
}
QVERIFY2(fastFileOpened
, qPrintable("Failed to open test fastfile: " + fastFilePath));
2025-04-04 20:40:12 -04:00
const QByteArray testFFData = testFastFile.readAll();
testFastFile.close();
// Assume the first 12 bytes are a header; the rest is zlib-compressed zone data.
const QByteArray compressedData = testFFData.mid(20);
const QByteArray testZoneData = Compression::DecompressZLIB(compressedData);
// Verify the decompressed data via its embedded zone size.
2025-09-10 21:58:26 -04:00
XDataStream zoneStream(testZoneData);
zoneStream.setByteOrder(XDataStream::LittleEndian);
2025-04-04 20:40:12 -04:00
quint32 zoneSize;
zoneStream >> zoneSize;
// TODO: Find new way to verify as cod2 doesn't store size in zone file
//QVERIFY2(zoneSize + 44 == testZoneData.size(),
// qPrintable("Decompression validation failed for: " + fastFilePath));
2025-04-04 20:40:12 -04:00
// Write the decompressed zone data to the exports folder with a .zone extension.
QFileInfo fi(fastFilePath);
2025-04-04 20:40:12 -04:00
QString outputFileName = fi.completeBaseName() + ".zone";
QString outputFilePath = QDir(EXPORT_DIR).filePath(outputFileName);
QFile outputFile(outputFilePath);
2025-04-23 00:09:35 -04:00
bool zoneFileOpened = outputFile.open(QIODevice::WriteOnly);
if (!zoneFileOpened) {
recordResult(testName, false);
}
QVERIFY2(zoneFileOpened,
qPrintable("Failed to open output zone file for writing: " + outputFilePath));
2025-04-04 20:40:12 -04:00
outputFile.write(testZoneData);
outputFile.close();
2025-04-23 00:09:35 -04:00
recordResult(testName, true);
2025-04-04 20:40:12 -04:00
}
void AutoTest_COD2_360::testCompression_data() {
AutoTest_COD::testCompression_data();
2025-04-04 20:40:12 -04:00
}
void AutoTest_COD2_360::testCompression() {
QFETCH(QString, zoneFilePath);
2025-04-04 20:40:12 -04:00
const QString testName = "Compress: " + zoneFilePath;
2025-04-23 00:09:35 -04:00
QFile zoneFile(zoneFilePath);
2025-04-23 00:09:35 -04:00
bool zoneFileOpened = zoneFile.open(QIODevice::ReadOnly);
if (!zoneFileOpened) {
recordResult(testName, false);
}
QVERIFY2(zoneFileOpened, qPrintable("Failed to open zone file: " + zoneFilePath));
2025-04-04 20:40:12 -04:00
QByteArray decompressedData = zoneFile.readAll();
zoneFile.close();
QFileInfo fi(zoneFilePath);
2025-04-04 20:40:12 -04:00
QString originalFFPath = QDir(getFastFileDirectory()).filePath(fi.completeBaseName() + ".ff");
QFile originalFile(originalFFPath);
2025-04-23 00:09:35 -04:00
bool origFileOpened = originalFile.open(QIODevice::ReadOnly);
if (!origFileOpened) {
recordResult(testName, false);
}
QVERIFY2(origFileOpened, qPrintable("Failed to open original .ff file: " + originalFFPath));
2025-04-04 20:40:12 -04:00
QByteArray originalFFData = originalFile.readAll();
originalFile.close();
QByteArray header = originalFFData.left(20);
QByteArray recompressedData = header + Compression::CompressZLIB(decompressedData);
2025-04-04 20:40:12 -04:00
QString recompressedFilePath = QDir(EXPORT_DIR).filePath(fi.completeBaseName() + ".ff");
QFile recompressedFile(recompressedFilePath);
2025-04-23 00:09:35 -04:00
bool fastFileOpened = recompressedFile.open(QIODevice::WriteOnly);
if (!fastFileOpened) {
recordResult(testName, false);
}
QVERIFY2(fastFileOpened, qPrintable("Failed to write recompressed file."));
2025-04-04 20:40:12 -04:00
recompressedFile.write(recompressedData);
recompressedFile.close();
2025-04-23 00:09:35 -04:00
bool dataMatches = recompressedData == originalFFData;
if (!dataMatches) {
recordResult(testName, false);
}
2025-04-04 20:40:12 -04:00
QCOMPARE(recompressedData, originalFFData);
2025-04-23 00:09:35 -04:00
recordResult(testName, true);
2025-04-04 20:40:12 -04:00
}
2025-05-03 09:57:47 -04:00
void AutoTest_COD2_360::testFactory_data() {
AutoTest_COD::testFactory_data();
2025-05-03 09:57:47 -04:00
}
void AutoTest_COD2_360::testFactory() {
QFETCH(QString, fastFilePath);
2025-05-03 09:57:47 -04:00
const QString testName = "Factory ingest: " + fastFilePath;
2025-05-03 09:57:47 -04:00
FastFile* fastFile = FastFile::Open(fastFilePath);
const QString game = fastFile->GetGame();
bool correctGame = game == "COD2";
if (!correctGame) {
2025-05-03 09:57:47 -04:00
recordResult(testName, false);
}
QVERIFY2(correctGame
, qPrintable("Factory parsed wrong game [" + game + "] for fastfile: " + fastFilePath));
2025-05-03 09:57:47 -04:00
const QString platform = fastFile->GetPlatform();
bool correctPlatform = platform == "360";
2025-05-03 09:57:47 -04:00
if (!correctPlatform) {
recordResult(testName, false);
}
QVERIFY2(correctPlatform
, qPrintable("Factory parsed wrong platform [" + platform + "] for fastfile: " + fastFilePath));
2025-05-03 09:57:47 -04:00
recordResult(testName, true);
}
2025-04-04 20:40:12 -04:00
void AutoTest_COD2_360::cleanupTestCase() {
// Any cleanup if necessary.
}
// Don't generate a main() function
#include "autotest_cod2_360.moc"