Use UnitTest-Cpp library for testing

This commit is contained in:
Ilusha 2024-03-15 16:03:33 +03:00 committed by Ilya Shurupov
parent 79d018ffb4
commit b8bcf58a29
53 changed files with 1046 additions and 1383 deletions

View file

@ -12,5 +12,5 @@ target_link_libraries(${PROJECT_NAME} PUBLIC Containers)
enable_testing()
file(GLOB TEST_SOURCES "./tests/*.cpp")
add_executable(${PROJECT_NAME}Tests ${TEST_SOURCES})
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME})
target_link_libraries(${PROJECT_NAME}Tests ${PROJECT_NAME} UnitTest++)
add_test(NAME ${PROJECT_NAME}Tests COMMAND ${PROJECT_NAME}Tests)

View file

@ -1,79 +0,0 @@
#include "Testing.hpp"
#include "Utils.hpp"
#include <iostream>
#include <string>
using namespace tp;
Testing tp::gTesting;
void Testing::startTest(const char* name) {
auto newNode = new (malloc(sizeof(TestingNode))) TestingNode{ {}, {}, name, mCurrent };
mCurrent->mSubTests.pushBack(newNode);
mCurrent = mCurrent->mSubTests.last()->data;
}
void Testing::endTest() {
if (mCurrent->mParent) {
mCurrent = mCurrent->mParent;
}
}
void Testing::addFailedCheck(const FailedCheck& info) {
auto lastRecord = &mCurrent->mFailedChecks.last()->data;
if (lastRecord && lastRecord->failedCheck.file == info.file && lastRecord->failedCheck.line == info.line) {
lastRecord->times++;
return;
}
mCurrent->mFailedChecks.pushBack({ info, 1 });
}
void Testing::reportState() {
mRootTest.updateState();
printf("\n");
mRootTest.report();
}
bool Testing::hasFailed() {
mRootTest.updateState();
return mRootTest.mHasFailed;
}
void Testing::setRootName(const char* name) { mRootTest.mName = name; }
void Testing::TestingNode::updateState() {
for (auto child : mSubTests) {
child->updateState();
mHasFailed = child->mHasFailed;
if (mHasFailed) return;
}
mHasFailed = mFailedChecks.length();
}
void Testing::TestingNode::report(const char* path) const {
if (!mHasFailed) {
return;
}
auto newPath = path ? std::string(path) + "/" + mName : std::string(mName);
for (auto check : mFailedChecks) {
auto failedCheck = &check.data().failedCheck;
auto times = check.data().times;
printf("%s Failed - (%s) %s:%llu x%i\n", newPath.c_str(), failedCheck->expression, failedCheck->file, failedCheck->line, (halni) times);
}
for (const auto& child : mSubTests) {
child->report(newPath.c_str());
}
}
Testing::TestingNode::~TestingNode() {
for (const auto& child : mSubTests) {
child.data()->~TestingNode();
free(child.data());
}
}

View file

@ -3,8 +3,6 @@
#include "ContainersCommon.hpp"
#include "Testing.hpp"
#include <ctime>
#include <random>
@ -19,10 +17,6 @@ static bool initialize(const tp::ModuleManifest*) {
static void deinitialize(const tp::ModuleManifest*) {
deinitializeCallStackCapture();
tp::gTesting.reportState();
if (tp::gTesting.hasFailed()) {
exit(1);
}
}
namespace tp {

View file

@ -1,71 +0,0 @@
#pragma once
#include "List.hpp"
namespace tp {
class Testing {
public:
struct FailedCheck {
const char* expression = nullptr;
const char* file = nullptr;
ualni line = 0;
};
Testing() = default;
void startTest(const char* name);
void endTest();
void addFailedCheck(const FailedCheck& info);
void reportState();
void setRootName(const char* name);
[[nodiscard]] bool hasFailed();
private:
struct TestingNode {
struct FailedCheckRecord {
FailedCheck failedCheck;
ualni times;
};
List<FailedCheckRecord> mFailedChecks;
List<TestingNode*> mSubTests;
const char* mName = "Unnamed";
TestingNode* mParent = nullptr;
bool mHasFailed = false;
void report(const char* path = nullptr) const;
void updateState();
~TestingNode();
};
TestingNode mRootTest;
TestingNode* mCurrent = &mRootTest;
};
extern Testing gTesting;
}
#define TEST_DEF(Name) \
static void Name##FunctorBody(); \
void test##Name() { \
tp::gTesting.startTest(#Name); \
Name##FunctorBody(); \
tp::gTesting.endTest(); \
} \
void Name##FunctorBody()
#define TEST_DEF_STATIC(Name) \
static void Name##FunctorBody(); \
static void test##Name() { \
tp::gTesting.startTest(#Name); \
Name##FunctorBody(); \
tp::gTesting.endTest(); \
} \
void Name##FunctorBody()
#define TEST(expr) \
if (!(expr)) tp::gTesting.addFailedCheck({ #expr, __FILE__, __LINE__ })
#define TEST_ASSERT(expr) \
TEST(expr); \
if (!(expr)) return
#define TEST_EQUAL(l, r) \
if (!((l) == (r))) tp::gTesting.addFailedCheck({ #l " == " #r, __FILE__, __LINE__ })

View file

@ -1,6 +1,7 @@
#include "UnitTest++/UnitTest++.h"
#include "Debugging.hpp"
#include "Testing.hpp"
#include "Utils.hpp"
#include <cstdio>
@ -42,22 +43,23 @@ void root() {
third();
}
TEST_DEF(Debugging) {
root();
SUITE(Utils) {
TEST(CallStackCapture) {
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, nullptr };
tp::ModuleManifest testModule("UtilsTest", nullptr, nullptr, deps);
gCSCapture->logAll();
}
REQUIRE CHECK(testModule.initialize());
int main() {
root();
tp::ModuleManifest* deps[] = { &tp::gModuleUtils, nullptr };
tp::ModuleManifest testModule("UtilsTest", nullptr, nullptr, deps);
gCSCapture->logAll();
if (!testModule.initialize()) {
return 1;
testModule.deinitialize();
}
testDebugging();
TEST(CAllStackCaptureContent) {
CHECK(false);
}
}
testModule.deinitialize();
}
int main() { return UnitTest::RunAllTests(); }