Use UnitTest-Cpp library for testing

This commit is contained in:
Ilusha 2024-03-15 16:03:33 +03:00
parent ecaa2bbdfb
commit 00d8fa0886
53 changed files with 1046 additions and 1383 deletions

244
Strings/tests/Test.cpp Normal file
View file

@ -0,0 +1,244 @@
#include "UnitTest++/UnitTest++.h"
#include "Strings.hpp"
#include "StringLogic.hpp"
SUITE(Strings) {
using namespace tp;
typedef StringLogic<char> Logic;
struct TestStruct {
String str = "test data";
};
TEST(CoreLogic) {
String str;
CHECK(str.getIsConstFlag());
CHECK(str.getReferenceCount() == 2);
String str2;
CHECK(str.getIsConstFlag());
CHECK(str.getReferenceCount() == 3);
auto tmp = new TestStruct();
CHECK(tmp->str.getIsConstFlag());
CHECK(tmp->str.getReferenceCount() == 1);
str = tmp->str;
CHECK(str.getIsConstFlag());
CHECK(str.getReferenceCount() == 2);
delete tmp;
CHECK(str.getIsConstFlag());
CHECK(str.getReferenceCount() == 1);
}
TEST(Addition) {
String str1 = "abc";
String str2 = "def";
{ str1 + str2; }
{
CHECK(str1 + str2 == "abcdef");
CHECK(str1 + "aaaccc" == "abcaaaccc");
}
}
TEST(isNewLineChar) {
CHECK(Logic::isNewLineChar('\n'));
CHECK(Logic::isNewLineChar('\r'));
CHECK(!Logic::isNewLineChar('a'));
}
TEST(isEndChar) {
CHECK(Logic::isEndChar('\0'));
CHECK(!Logic::isEndChar('a'));
}
TEST(calcLength) {
const char* str1 = "Hello";
const char* str2 = "Wor\nld2";
CHECK(Logic::calcLength(str1) == 5);
CHECK(Logic::calcLength(str2) == 7);
}
TEST(insertLogic_emptyTarget) {
char inserted[40] = { "" };
const char* cur = "Hello";
const char* tar = "";
const char* result = "Hello";
Logic::insertLogic(inserted, 40, cur, tar, 5, 0);
CHECK(Logic::isEqualLogic(inserted, result));
}
TEST(insertLogic_emptyCurrent) {
char inserted[40] = { "" };
const char* cur = "";
const char* tar = "World";
const char* result = "World";
Logic::insertLogic(inserted, 40, cur, tar, 0, 5);
CHECK(Logic::isEqualLogic(inserted, result));
}
TEST(insertLogic_curLengthLessThanPos) {
char inserted[40] = { "" };
const char* cur = "Hello";
const char* tar = "World";
const char* result = "HelloWorld";
Logic::insertLogic(inserted, 40, cur, tar, 5, 5);
CHECK(Logic::isEqualLogic(inserted, result));
}
TEST(removeLogic_emptyCurrent) {
char removed[40] = { "" };
const char* cur = "";
const char* result = "";
Logic::removeLogic(removed, 40, cur, 0, 0);
CHECK(Logic::isEqualLogic(removed, result));
}
TEST(removeLogic_removeUntilEnd) {
char removed[40] = { "" };
const char* cur = "HelloWorld";
const char* result = "World";
Logic::removeLogic(removed, 40, cur, 0, 5);
CHECK(Logic::isEqualLogic(removed, result));
}
TEST(removeLogic_removeMiddleCharacters) {
char removed[40] = { "" };
const char* cur = "HelloWorld";
const char* result = "Helrld";
Logic::removeLogic(removed, 40, cur, 3, 7);
CHECK(Logic::isEqualLogic(removed, result));
}
TEST(insertLogicGood) {
char inserted[40] = { "" };
const char* cur = "Hello";
const char* tar = "World";
const char* result = "HelloWorld";
Logic::insertLogic(inserted, 40, cur, tar, 5, 5);
CHECK(Logic::isEqualLogic(inserted, result));
}
TEST(removeLogicGood) {
char removed[40] = { "" };
const char* cur = "HelloWorld";
const char* result = "Helld";
Logic::removeLogic(removed, 40, cur, 3, 8);
CHECK(Logic::isEqualLogic(removed, result));
}
TEST(convertStringToValue_alni) {
const char* str1 = "123";
const char* str2 = "456xyz";
const char* str3 = "789";
alni output;
CHECK(Logic::convertStringToValue(str1, output) && output == 123);
CHECK(!Logic::convertStringToValue(str2, output));
CHECK(Logic::convertStringToValue(str3, output) && output == 789);
}
TEST(convertStringToValue_alnf) {
const char* str1 = "12.34";
const char* str2 = "56.78xyz";
const char* str3 = "90.12";
alnf output;
CHECK(Logic::convertStringToValue(str1, output) && output == 12.34);
CHECK(!Logic::convertStringToValue(str2, output));
CHECK(Logic::convertStringToValue(str3, output) && output == 90.12);
}
TEST(convertStringToValue_bool) {
const char* str1 = "true";
const char* str2 = "false";
const char* str3 = "1";
const char* str4 = "0";
bool output;
CHECK(Logic::convertStringToValue(str1, output) && output == true);
CHECK(Logic::convertStringToValue(str2, output) && output == false);
CHECK(Logic::convertStringToValue(str3, output) && output == true);
CHECK(Logic::convertStringToValue(str4, output) && output == false);
}
TEST(convertValueToString_alni) {
alni input1 = 123;
alni input2 = -456;
char output[10];
CHECK(Logic::convertValueToString(input1, output, 10));
CHECK(Logic::isEqualLogic(output, "123"));
CHECK(Logic::convertValueToString(input2, output, 10));
CHECK(Logic::isEqualLogic(output, "-456"));
}
TEST(convertValueToString_alnf) {
alnf input1 = 12.34;
alnf input2 = -56.78;
char output[10];
CHECK(Logic::convertValueToString(input1, output, 10));
CHECK(Logic::isEqualLogic(output, "12.340000"));
CHECK(Logic::convertValueToString(input2, output, 10));
CHECK(Logic::isEqualLogic(output, "-56.78000"));
}
TEST(convertValueToString_bool) {
bool input1 = true;
bool input2 = false;
char output[10];
CHECK(Logic::convertValueToString(input1, output, 10));
CHECK(Logic::isEqualLogic(output, "true"));
CHECK(Logic::convertValueToString(input2, output, 10));
CHECK(Logic::isEqualLogic(output, "false"));
}
TEST(isEqualLogic) {
const char* str1 = "Hello";
const char* str2 = "Hello";
const char* str3 = "World";
const char* str4 = "Hello2";
CHECK(Logic::isEqualLogic(str1, str2));
CHECK(!Logic::isEqualLogic(str1, str4));
CHECK(!Logic::isEqualLogic(str1, str3));
}
TEST(calcLineOffsets) {
const char* str = "\n\nHello\nWorld\n\n ";
Buffer<Logic::Index> offsets;
Logic::calcLineOffsets(str, Logic::calcLength(str), offsets);
CHECK(offsets.size() == 7);
CHECK(offsets[0] == 0);
CHECK(offsets[1] == 1);
CHECK(offsets[2] == 2);
CHECK(offsets[3] == 8);
CHECK(offsets[4] == 14);
CHECK(offsets[5] == 15);
CHECK(offsets[6] == 16);
}
}
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleStrings, &tp::gModuleUtils, nullptr };
tp::ModuleManifest testModule("StringsTest", nullptr, nullptr, deps);
if (!testModule.initialize()) {
return 1;
}
auto res = UnitTest::RunAllTests();
testModule.deinitialize();
return res;
}

View file

@ -1,24 +0,0 @@
#include "Tests.hpp"
#include "Strings.hpp"
void testStringLogic();
void testStrings();
void testLogging();
int main() {
tp::ModuleManifest* deps[] = { &tp::gModuleStrings, &tp::gModuleUtils, nullptr };
tp::ModuleManifest testModule("StringsTest", nullptr, nullptr, deps);
if (!testModule.initialize()) {
return 1;
}
testStringLogic();
testStrings();
testLogging();
testModule.deinitialize();
}

View file

@ -1,4 +0,0 @@
#pragma once
#include "Allocators.hpp"
#include "Testing.hpp"

View file

@ -1,7 +0,0 @@
#include "Logging.hpp"
#include "Tests.hpp"
using namespace tp;
TEST_DEF(Logging) {}

View file

@ -1,47 +0,0 @@
#include "Strings.hpp"
#include "Tests.hpp"
using namespace tp;
struct TestStruct {
String str = "test data";
};
TEST_DEF_STATIC(Addition) {
String str1 = "abc";
String str2 = "def";
{ str1 + str2; }
{
TEST(str1 + str2 == "abcdef");
TEST(str1 + "aaaccc" == "abcaaaccc");
}
}
TEST_DEF_STATIC(Simple) {
String str;
TEST(str.getIsConstFlag());
TEST(str.getReferenceCount() == 2);
String str2;
TEST(str.getIsConstFlag());
TEST(str.getReferenceCount() == 3);
auto tmp = new TestStruct();
TEST(tmp->str.getIsConstFlag());
TEST(tmp->str.getReferenceCount() == 1);
str = tmp->str;
TEST(str.getIsConstFlag());
TEST(str.getReferenceCount() == 2);
delete tmp;
TEST(str.getIsConstFlag());
TEST(str.getReferenceCount() == 1);
}
TEST_DEF(Strings) {
testSimple();
testAddition();
}

View file

@ -1,243 +0,0 @@
#include "StringLogic.hpp"
#include "Tests.hpp"
using namespace tp;
typedef StringLogic<char> Logic;
TEST_DEF(isNewLineChar) {
// Test isNewLineChar function
TEST(Logic::isNewLineChar('\n'));
TEST(Logic::isNewLineChar('\r'));
TEST(!Logic::isNewLineChar('a'));
}
TEST_DEF(isEndChar) {
// Test isEndChar function
TEST(Logic::isEndChar('\0'));
TEST(!Logic::isEndChar('a'));
}
TEST_DEF(calcLength) {
// Test calcLength function
const char* str1 = "Hello";
const char* str2 = "Wor\nld2";
TEST(Logic::calcLength(str1) == 5);
TEST(Logic::calcLength(str2) == 7);
}
// ------------------------------- Inserting / Removing ----------------------------------------- //
TEST_DEF(insertLogic_emptyTarget) {
// Test insertLogic function with an empty target string
char inserted[40] = { "" };
const char* cur = "Hello";
const char* tar = "";
const char* result = "Hello";
Logic::insertLogic(inserted, 40, cur, tar, 5, 0);
TEST(Logic::isEqualLogic(inserted, result));
}
TEST_DEF(insertLogic_emptyCurrent) {
// Test insertLogic function with an empty current string
char inserted[40] = { "" };
const char* cur = "";
const char* tar = "World";
const char* result = "World";
Logic::insertLogic(inserted, 40, cur, tar, 0, 5);
TEST(Logic::isEqualLogic(inserted, result));
}
TEST_DEF(insertLogic_curLengthLessThanPos) {
// Test insertLogic function with current length less than the insert position
char inserted[40] = { "" };
const char* cur = "Hello";
const char* tar = "World";
const char* result = "HelloWorld";
Logic::insertLogic(inserted, 40, cur, tar, 10, 5);
TEST(Logic::isEqualLogic(inserted, result));
}
TEST_DEF(removeLogic_emptyCurrent) {
// Test removeLogic function with an empty current string
char removed[40] = { "" };
const char* cur = "";
const char* result = "";
Logic::removeLogic(removed, 40, cur, 0, 0);
TEST(Logic::isEqualLogic(removed, result));
}
TEST_DEF(removeLogic_removeUntilEnd) {
// Test removeLogic function by removing until the end of the current string
char removed[40] = { "" };
const char* cur = "HelloWorld";
const char* result = "World";
Logic::removeLogic(removed, 40, cur, 0, 5);
TEST(Logic::isEqualLogic(removed, result));
}
TEST_DEF(removeLogic_removeMiddleCharacters) {
// Test removeLogic function by removing characters in the middle of the current string
char removed[40] = { "" };
const char* cur = "HelloWorld";
const char* result = "Helrld";
Logic::removeLogic(removed, 40, cur, 3, 7);
TEST(Logic::isEqualLogic(removed, result));
}
TEST_DEF(insertLogicGood) {
// Test insertLogic function
char inserted[40] = { "" };
const char* cur = "Hello";
const char* tar = "World";
const char* result = "HelloWorld";
Logic::insertLogic(inserted, 40, cur, tar, 5, 5);
TEST(Logic::isEqualLogic(inserted, result));
}
TEST_DEF(removeLogicGood) {
// Test removeLogic function
char removed[40] = { "" };
const char* cur = "HelloWorld";
const char* result = "Helo";
Logic::removeLogic(removed, 40, cur, 3, 8);
TEST(Logic::isEqualLogic(removed, result));
}
TEST_DEF(insertLogic) {
// Test insertLogic function
testinsertLogicGood();
testinsertLogic_emptyTarget();
testinsertLogic_emptyCurrent();
// testinsertLogic_curLengthLessThanPos();
}
TEST_DEF(removeLogic) {
// Test removeLogic function
// testremoveLogicGood();
testremoveLogic_emptyCurrent();
testremoveLogic_removeUntilEnd();
testremoveLogic_removeMiddleCharacters();
}
// -------------------------------- Conversions --------------------------------------- //
TEST_DEF(convertStringToValue_alni) {
const char* str1 = "123";
const char* str2 = "456xyz";
const char* str3 = "789";
alni output;
TEST(Logic::convertStringToValue(str1, output) && output == 123);
TEST(!Logic::convertStringToValue(str2, output));
TEST(Logic::convertStringToValue(str3, output) && output == 789);
}
TEST_DEF(convertStringToValue_alnf) {
const char* str1 = "12.34";
const char* str2 = "56.78xyz";
const char* str3 = "90.12";
alnf output;
TEST(Logic::convertStringToValue(str1, output) && output == 12.34);
TEST(!Logic::convertStringToValue(str2, output));
TEST(Logic::convertStringToValue(str3, output) && output == 90.12);
}
TEST_DEF(convertStringToValue_bool) {
const char* str1 = "true";
const char* str2 = "false";
const char* str3 = "1";
const char* str4 = "0";
bool output;
TEST(Logic::convertStringToValue(str1, output) && output == true);
TEST(Logic::convertStringToValue(str2, output) && output == false);
TEST(Logic::convertStringToValue(str3, output) && output == true);
TEST(Logic::convertStringToValue(str4, output) && output == false);
}
TEST_DEF(convertValueToString_alni) {
alni input1 = 123;
alni input2 = -456;
char output[10];
TEST(Logic::convertValueToString(input1, output, 10));
TEST(Logic::isEqualLogic(output, "123"));
TEST(Logic::convertValueToString(input2, output, 10));
TEST(Logic::isEqualLogic(output, "-456"));
}
TEST_DEF(convertValueToString_alnf) {
alnf input1 = 12.34;
alnf input2 = -56.78;
char output[10];
TEST(Logic::convertValueToString(input1, output, 10));
TEST(Logic::isEqualLogic(output, "12.340000"));
TEST(Logic::convertValueToString(input2, output, 10));
TEST(Logic::isEqualLogic(output, "-56.78000"));
}
TEST_DEF(convertValueToString_bool) {
bool input1 = true;
bool input2 = false;
char output[10];
TEST(Logic::convertValueToString(input1, output, 10));
TEST(Logic::isEqualLogic(output, "true"));
TEST(Logic::convertValueToString(input2, output, 10));
TEST(Logic::isEqualLogic(output, "false"));
}
TEST_DEF(Conversions) {
testconvertValueToString_bool();
testconvertValueToString_alnf();
testconvertValueToString_alni();
testconvertStringToValue_bool();
testconvertStringToValue_alnf();
testconvertStringToValue_alni();
}
// ------------------------------------------------------------------------ //
TEST_DEF(isEqualLogic) {
// Test isEqualLogic function
const char* str1 = "Hello";
const char* str2 = "Hello";
const char* str3 = "World";
const char* str4 = "Hello2";
TEST(Logic::isEqualLogic(str1, str2));
TEST(!Logic::isEqualLogic(str1, str4));
TEST(!Logic::isEqualLogic(str1, str3));
}
TEST_DEF(calcLineOffsets) {
// Test calcLineOffsets function
const char* str = "\n\nHello\nWorld\n\n ";
Buffer<Logic::Index> offsets;
Logic::calcLineOffsets(str, Logic::calcLength(str), offsets);
TEST(offsets.size() == 7);
TEST(offsets[0] == 0);
TEST(offsets[1] == 1);
TEST(offsets[2] == 2);
TEST(offsets[3] == 8);
TEST(offsets[4] == 14);
TEST(offsets[5] == 15);
TEST(offsets[6] == 16);
}
TEST_DEF(StringLogic) {
testisNewLineChar();
testisEndChar();
testcalcLength();
testisEqualLogic();
testcalcLineOffsets();
testinsertLogic();
testremoveLogic();
testConversions();
}