From cdb20d48488e0d94210c39feafdddf36093d05c0 Mon Sep 17 00:00:00 2001 From: Enzo Evers Date: Sat, 30 Nov 2024 14:29:14 +0100 Subject: [PATCH] test: enable unity test fixture and update tests --- .gitignore | 2 + .vscode/settings.json | 17 +- .../MultiTextFileZip_store.zip | Bin 0 -> 588 bytes .../Files/MultiTextFileZip_store/Readme.md | 1 + .../TextFileOne_store.txt | 2 + .../TextFileOne_store.unzipped.txt | 0 .../TextFileTwo_store.txt | 2 + .../TextFileTwo_store.unzipped.txt | 0 .../ThirdTextFile_store.txt | 3 + .../ThirdTextFile_store.unzipped.txt | 0 .../SmallBasicTextFileZip_store/Readme.md | 1 + .../SmallBasicTextFile_store.txt | 4 + .../SmallBasicTextFile_store.unzipped.txt | 0 .../SmallBasicTextFile_store.zip | Bin 0 -> 337 bytes CoDeLib/RaiiString/src/RaiiString.c | 32 +++- CoDeLib/Test/CMakeLists.txt | 24 ++- CoDeLib/Test/Utility/FileUtils.c | 2 +- CoDeLib/Test/src/TestDeflateInflateZlib.c | 38 ++-- CoDeLib/Test/src/TestDeflateInflateZlib.h | 1 + CoDeLib/Test/src/TestRaiiString.c | 175 ++++++++++++++++++ CoDeLib/Test/src/TestUnzipMinizip.c | 88 +++++++++ CoDeLib/Test/src/TestUnzipMinizip.h | 1 + CoDeLib/Test/src/main.c | 29 +++ .../include/CoDeLib/RaiiString/RaiiString.h | 8 +- Scripts/RunTest.py | 30 ++- 25 files changed, 416 insertions(+), 44 deletions(-) create mode 100644 Benchmark/Files/MultiTextFileZip_store/MultiTextFileZip_store.zip create mode 100644 Benchmark/Files/MultiTextFileZip_store/Readme.md create mode 100644 Benchmark/Files/MultiTextFileZip_store/TextFileOne_store.txt create mode 100644 Benchmark/Files/MultiTextFileZip_store/TextFileOne_store.unzipped.txt create mode 100644 Benchmark/Files/MultiTextFileZip_store/TextFileTwo_store.txt create mode 100644 Benchmark/Files/MultiTextFileZip_store/TextFileTwo_store.unzipped.txt create mode 100644 Benchmark/Files/MultiTextFileZip_store/ThirdTextFile_store.txt create mode 100644 Benchmark/Files/MultiTextFileZip_store/ThirdTextFile_store.unzipped.txt create mode 100644 Benchmark/Files/SmallBasicTextFileZip_store/Readme.md create mode 100644 Benchmark/Files/SmallBasicTextFileZip_store/SmallBasicTextFile_store.txt create mode 100644 Benchmark/Files/SmallBasicTextFileZip_store/SmallBasicTextFile_store.unzipped.txt create mode 100644 Benchmark/Files/SmallBasicTextFileZip_store/SmallBasicTextFile_store.zip create mode 100644 CoDeLib/Test/src/TestDeflateInflateZlib.h create mode 100644 CoDeLib/Test/src/TestRaiiString.c create mode 100644 CoDeLib/Test/src/TestUnzipMinizip.c create mode 100644 CoDeLib/Test/src/TestUnzipMinizip.h create mode 100644 CoDeLib/Test/src/main.c diff --git a/.gitignore b/.gitignore index 1e544a8b..d0670fc6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,8 @@ main.exe Benchmark*.txt* Benchmark/Files/*.compressed.* Benchmark/Files/*.decompressed.* +Benchmark/Files/*.zipped.* +Benchmark/Files/*.unzipped.* # Test generated files Test*.txt* diff --git a/.vscode/settings.json b/.vscode/settings.json index 06da219b..ff785e3f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,6 +22,21 @@ "fileutils.h": "c", "zip_minizip.h": "c", "unzip.h": "c", - "unzip_minizip.h": "c" + "unzip_minizip.h": "c", + "testzipunzipminizip.h": "c", + "unity_fixture.h": "c", + "testdeflateinflatezlib.h": "c", + "stdbool.h": "c", + "cstdlib": "c", + "rope": "c", + "*.def": "c", + "string.h": "c", + "testunzipminizip.h": "c", + "unity_fixture_internals.h": "c", + "unity_memory.h": "c", + "string_view": "c", + "regex": "c", + "*.inc": "c", + "bitset": "c" }, } \ No newline at end of file diff --git a/Benchmark/Files/MultiTextFileZip_store/MultiTextFileZip_store.zip b/Benchmark/Files/MultiTextFileZip_store/MultiTextFileZip_store.zip new file mode 100644 index 0000000000000000000000000000000000000000..6f96d621f5825976024e93c5bd5f74616513e286 GIT binary patch literal 588 zcmWIWW@h1H00Fary2y&e94A>I8-zuHI3%^A#4R%?)juyazPKd6C{?edq9i0EvsfXs zn3t<0BUK?Sv#7X4p#-Q7$jnLA3xJ#F0W|Njcfx!zpgACnY+gutKEk}<)a3lU6tH%n zu?i(X<9WG|42l67BsPufp&`%^5Echwpc{%(V6H7xV5Kanoh{TH6IH{c&@(Ku$STZycSvtDO5uapi6rTid!`RngW@V#mAA+%G zE9%B)e?H&ece=6|46`Jl{H{K)_gQn6f8a#8-78~O%2Qq8i8o|0=Mz_&JjhkR=oxk) zJkCBWutxA$JKKOEi^1;@ln|-a3`f^Bew=}zl2X>x^_5yhIrSw%OKA^EMu~XTiL;Ee zEp~7qtTBhRvz|QJ7TgZ_=wz;Cv^Dy?EYg=n@@%PhSt|hI`)tLm&!@>WdHi17{Ou?0 S8~#mMj9!D2%F$wPG4lucm{}$O literal 0 HcmV?d00001 diff --git a/CoDeLib/RaiiString/src/RaiiString.c b/CoDeLib/RaiiString/src/RaiiString.c index f5a6492c..89636471 100644 --- a/CoDeLib/RaiiString/src/RaiiString.c +++ b/CoDeLib/RaiiString/src/RaiiString.c @@ -1,21 +1,45 @@ #include #include #include +#include -RaiiString RaiiStringCreate(size_t length) { - RaiiString newRaiistring = {NULL, length}; - newRaiistring.pString = (char *)calloc(length, sizeof(char)); +RaiiString RaiiStringCreate(size_t lengthWithTermination) { + RaiiString newRaiistring = {NULL, 0}; + + if (lengthWithTermination == 0 || + lengthWithTermination >= MAX_CSTRING_INCLUDING_TERMINATION_LENGTH) { + return newRaiistring; + } + + newRaiistring.lengthWithTermination = lengthWithTermination; + newRaiistring.pString = (char *)calloc(lengthWithTermination, sizeof(char)); if (newRaiistring.pString == NULL) { - newRaiistring.length = 0; + newRaiistring.lengthWithTermination = 0; printf("Failed to allocate memory for RaiiString\n"); exit(1); } return newRaiistring; } +RaiiString RaiiStringCreateFromCString(const char *pCString) { + const size_t length = + strnlen(pCString, MAX_CSTRING_INCLUDING_TERMINATION_LENGTH); + RaiiString newRaiistring = RaiiStringCreate(length + 1); + + if (newRaiistring.pString == NULL) { + return newRaiistring; + } + + strncpy(newRaiistring.pString, pCString, + newRaiistring.lengthWithTermination - 1); + newRaiistring.pString[newRaiistring.lengthWithTermination - 1] = '\0'; + return newRaiistring; +} + void RaiiStringClean(RaiiString *pThis) { if (pThis->pString != NULL) { free(pThis->pString); pThis->pString = NULL; } + pThis->lengthWithTermination = 0; } diff --git a/CoDeLib/Test/CMakeLists.txt b/CoDeLib/Test/CMakeLists.txt index 6bba10f3..a44f5a64 100644 --- a/CoDeLib/Test/CMakeLists.txt +++ b/CoDeLib/Test/CMakeLists.txt @@ -1,22 +1,34 @@ add_executable(CoDeLib_Test - src/TestDeflateInflateZlib.c) + src/main.c + src/TestDeflateInflateZlib.c + src/TestUnzipMinizip.c + src/TestRaiiString.c +) target_include_directories(CoDeLib_Test PUBLIC $ ) -target_link_libraries(CoDeLib_Test PRIVATE Deflate_zlib) -target_link_libraries(CoDeLib_Test PRIVATE Inflate_zlib) -target_link_libraries(CoDeLib_Test PRIVATE RaiiString) - FetchContent_Declare( Unity GIT_REPOSITORY https://github.com/ThrowTheSwitch/Unity.git GIT_TAG 860062d51b2e8a75d150337b63ca2a472840d13c # v2.6.0 EXCLUDE_FROM_ALL ) +# Solution to use `CACHE INTERNAL` found here: https://discourse.cmake.org/t/what-is-the-correct-way-to-set-options-of-a-project-before-fetch-content/268/4 +# If cache is not used, you will get `Policy CMP0077 is not set: option() honors normal variables.` +set(UNITY_EXTENSION_FIXTURE ON CACHE INTERNAL "") FetchContent_MakeAvailable(Unity) -target_link_libraries(CoDeLib_Test PRIVATE unity) add_subdirectory(Utility) target_link_libraries(CoDeLib_Test PUBLIC Utility) + +target_link_libraries(CoDeLib_Test PRIVATE Deflate_zlib) +target_link_libraries(CoDeLib_Test PRIVATE Inflate_zlib) +target_link_libraries(CoDeLib_Test PRIVATE Zip_minizip) +target_link_libraries(CoDeLib_Test PRIVATE UnZip_minizip) +target_link_libraries(CoDeLib_Test PRIVATE RaiiString) + +target_link_libraries(CoDeLib_Test PRIVATE unity) + + diff --git a/CoDeLib/Test/Utility/FileUtils.c b/CoDeLib/Test/Utility/FileUtils.c index cac85768..092f33b3 100644 --- a/CoDeLib/Test/Utility/FileUtils.c +++ b/CoDeLib/Test/Utility/FileUtils.c @@ -10,7 +10,7 @@ void CreateFullPathToFile(const RaiiString *pFullPath, assert(pBasePath != NULL); assert(pFileName != NULL); assert(pFullPath->pString != NULL); - assert(pFullPath->length >= maxFullPathStringSize); + assert(pFullPath->lengthWithTermination >= maxFullPathStringSize); const int charsWritten = snprintf(pFullPath->pString, maxFullPathStringSize, "%s%s", pBasePath, pFileName); diff --git a/CoDeLib/Test/src/TestDeflateInflateZlib.c b/CoDeLib/Test/src/TestDeflateInflateZlib.c index d2e6c8e7..fe902e59 100644 --- a/CoDeLib/Test/src/TestDeflateInflateZlib.c +++ b/CoDeLib/Test/src/TestDeflateInflateZlib.c @@ -1,4 +1,4 @@ -#include "unity.h" +#include "unity_fixture.h" #include #include #include @@ -7,17 +7,23 @@ #include #include -char *g_pFullPathToBenchmarkTestFiles = NULL; +static char *g_pFullPathToBenchmarkTestFiles = NULL; -void setUp(void) { - // set stuff up here +void SetupTestDeflateInflateZlib(char *pFullPathToBenchmarkTestFiles) { + g_pFullPathToBenchmarkTestFiles = pFullPathToBenchmarkTestFiles; } -void tearDown(void) { - // clean stuff up here +TEST_GROUP(TestDeflateInflateZlib); + +TEST_SETUP(TestDeflateInflateZlib) { + // Nothing +} + +TEST_TEAR_DOWN(TestDeflateInflateZlib) { + // Nothing } -void test_InflateZlibWorkWithDeflateZlib(void) { +TEST(TestDeflateInflateZlib, test_InflateZlibWorkWithDeflateZlib) { FILE *pInFile = NULL; FILE *pOutCompressedFile = NULL; FILE *pOutDecompressedFile = NULL; @@ -41,22 +47,12 @@ void test_InflateZlibWorkWithDeflateZlib(void) { TEST_ASSERT_GREATER_THAN(0, GetFileSizeInBytes(pOutDecompressedFile)); TEST_ASSERT_EQUAL(INFLATE_SUCCESS, statusInflate); - FilesAreEqual(pInFile, pOutDecompressedFile); + TEST_ASSERT(FilesAreEqual(pInFile, pOutDecompressedFile)); fclose(pInFile); fclose(pOutCompressedFile); fclose(pOutDecompressedFile); } -int main(int argc, char **argv) { - // TODO: use getopt(...) - if (argc == 1) { - printf("No arguments provided\n"); - return 1; - } else { - g_pFullPathToBenchmarkTestFiles = argv[1]; - } - - UNITY_BEGIN(); - RUN_TEST(test_InflateZlibWorkWithDeflateZlib); - return UNITY_END(); -} \ No newline at end of file +TEST_GROUP_RUNNER(TestDeflateInflateZlib) { + RUN_TEST_CASE(TestDeflateInflateZlib, test_InflateZlibWorkWithDeflateZlib); +} diff --git a/CoDeLib/Test/src/TestDeflateInflateZlib.h b/CoDeLib/Test/src/TestDeflateInflateZlib.h new file mode 100644 index 00000000..c087fb01 --- /dev/null +++ b/CoDeLib/Test/src/TestDeflateInflateZlib.h @@ -0,0 +1 @@ +void SetupTestDeflateInflateZlib(char *pFullPathToBenchmarkTestFiles); \ No newline at end of file diff --git a/CoDeLib/Test/src/TestRaiiString.c b/CoDeLib/Test/src/TestRaiiString.c new file mode 100644 index 00000000..dc717949 --- /dev/null +++ b/CoDeLib/Test/src/TestRaiiString.c @@ -0,0 +1,175 @@ +#include "unity_fixture.h" +#include +#include +#include + +TEST_GROUP(TestRaiiString); + +static RaiiString raiiString; + +TEST_SETUP(TestRaiiString) {} + +TEST_TEAR_DOWN(TestRaiiString) { RaiiStringClean(&raiiString); } + +//============================== +// RaiiStringCreate() +//============================== + +TEST(TestRaiiString, test_RaiiStringCreate_SetsCorrectsLengthInReturnedObject) { + const size_t length = 10; + raiiString = RaiiStringCreate(length); + TEST_ASSERT_EQUAL(length, raiiString.lengthWithTermination); +} + +TEST(TestRaiiString, test_RaiiStringCreate_SetsNonNullPointerInReturnedObject) { + const size_t length = 10; + raiiString = RaiiStringCreate(length); + TEST_ASSERT_NOT_NULL(raiiString.pString); +} + +TEST(TestRaiiString, + test_RaiiStringCreate_SetsNullptrAndLengthZeroIfGivenLengthIsZero) { + const size_t length = 0; + raiiString = RaiiStringCreate(length); + TEST_ASSERT_NULL(raiiString.pString); + TEST_ASSERT_EQUAL(0, raiiString.lengthWithTermination); +} + +TEST(TestRaiiString, + test_RaiiStringCreate_SetsZeroLengthAndNullptrIfLengthIsMaxLength) { + const size_t length = MAX_CSTRING_INCLUDING_TERMINATION_LENGTH; + raiiString = RaiiStringCreate(length); + TEST_ASSERT_NULL(raiiString.pString); + TEST_ASSERT_EQUAL(0, raiiString.lengthWithTermination); +} + +TEST( + TestRaiiString, + test_RaiiStringCreate_SetsZeroLengthAndNullptrIfLengthIsGreaterThanMaxLength) { + const size_t length = MAX_CSTRING_INCLUDING_TERMINATION_LENGTH + 1; + raiiString = RaiiStringCreate(length); + TEST_ASSERT_NULL(raiiString.pString); + TEST_ASSERT_EQUAL(0, raiiString.lengthWithTermination); +} + +//============================== +// RaiiStringCreateFromCString() +//============================== + +TEST( + TestRaiiString, + test_RaiiStringCreateFromCString_SetsLengthOfOneAndNonNullptrIfProvidedEmptyString) { + const char *pCString = ""; + raiiString = RaiiStringCreateFromCString(pCString); + TEST_ASSERT_NOT_NULL(raiiString.pString); + TEST_ASSERT_EQUAL(1, raiiString.lengthWithTermination); +} + +TEST( + TestRaiiString, + test_RaiiStringCreateFromCString_SetsExecptedLengthAndNonNullptrIfProvidedNormalString) { + const char *pCString = "Hello, world!"; + raiiString = RaiiStringCreateFromCString(pCString); + TEST_ASSERT_NOT_NULL(raiiString.pString); + TEST_ASSERT_EQUAL(14, raiiString.lengthWithTermination); +} + +TEST(TestRaiiString, test_RaiiStringCreateFromCString_SetsExecptedString) { + const char *pCString = "Hello, world!"; + raiiString = RaiiStringCreateFromCString(pCString); + TEST_ASSERT_EQUAL_STRING(pCString, raiiString.pString); +} + +TEST( + TestRaiiString, + test_RaiiStringCreateFromCString_DoesNotUseProvidedPointerInReturnedObject) { + const char *pCString = ""; + raiiString = RaiiStringCreateFromCString(pCString); + TEST_ASSERT_NOT_EQUAL(pCString, raiiString.pString); +} + +TEST( + TestRaiiString, + test_RaiiStringCreateFromCString_SetsZeroLengthAndNullptrIfLengthIsMaxLength) { + const size_t lengthWithNullTermination = + MAX_CSTRING_INCLUDING_TERMINATION_LENGTH + 1; + char *pCString = (char *)malloc(lengthWithNullTermination * sizeof(char)); + assert(pCString != NULL && "Failed to allocate memory for pCString"); + memset(pCString, 'a', lengthWithNullTermination - 1); + pCString[MAX_CSTRING_INCLUDING_TERMINATION_LENGTH] = '\0'; + + raiiString = RaiiStringCreateFromCString(pCString); + TEST_ASSERT_NULL(raiiString.pString); + TEST_ASSERT_EQUAL(0, raiiString.lengthWithTermination); +} + +TEST( + TestRaiiString, + test_RaiiStringCreateFromCString_SetsZeroLengthAndNullptrIfLengthIsGreaterThanMaxLength) { + const size_t lengthWithNullTermination = + MAX_CSTRING_INCLUDING_TERMINATION_LENGTH + 2; + char *pCString = (char *)malloc(lengthWithNullTermination * sizeof(char)); + assert(pCString != NULL && "Failed to allocate memory for pCString"); + memset(pCString, 'a', lengthWithNullTermination - 1); + pCString[lengthWithNullTermination - 1] = '\0'; + + raiiString = RaiiStringCreateFromCString(pCString); + TEST_ASSERT_NULL(raiiString.pString); + TEST_ASSERT_EQUAL(0, raiiString.lengthWithTermination); +} + +//============================== +// RaiiStringClean() +//============================== + +TEST(TestRaiiString, test_RaiiStringClean_SetsNullptrInObject) { + raiiString = RaiiStringCreate(10); + RaiiStringClean(&raiiString); + TEST_ASSERT_NULL(raiiString.pString); +} + +TEST(TestRaiiString, test_RaiiStringClean_SetsLengthZeroInObject) { + raiiString = RaiiStringCreate(10); + RaiiStringClean(&raiiString); + TEST_ASSERT_EQUAL(0, raiiString.lengthWithTermination); +} + +TEST_GROUP_RUNNER(TestRaiiString) { + // RaiiStringCreate() + RUN_TEST_CASE(TestRaiiString, + test_RaiiStringCreate_SetsCorrectsLengthInReturnedObject); + RUN_TEST_CASE(TestRaiiString, + test_RaiiStringCreate_SetsNonNullPointerInReturnedObject); + RUN_TEST_CASE( + TestRaiiString, + test_RaiiStringCreate_SetsNullptrAndLengthZeroIfGivenLengthIsZero); + RUN_TEST_CASE( + TestRaiiString, + test_RaiiStringCreate_SetsZeroLengthAndNullptrIfLengthIsMaxLength); + RUN_TEST_CASE( + TestRaiiString, + test_RaiiStringCreate_SetsZeroLengthAndNullptrIfLengthIsGreaterThanMaxLength); + + // RaiiStringCreateFromCString() + RUN_TEST_CASE( + TestRaiiString, + test_RaiiStringCreateFromCString_SetsLengthOfOneAndNonNullptrIfProvidedEmptyString); + RUN_TEST_CASE( + TestRaiiString, + test_RaiiStringCreateFromCString_SetsExecptedLengthAndNonNullptrIfProvidedNormalString); + RUN_TEST_CASE(TestRaiiString, + test_RaiiStringCreateFromCString_SetsExecptedString); + RUN_TEST_CASE( + TestRaiiString, + test_RaiiStringCreateFromCString_DoesNotUseProvidedPointerInReturnedObject); + RUN_TEST_CASE( + TestRaiiString, + test_RaiiStringCreateFromCString_SetsZeroLengthAndNullptrIfLengthIsMaxLength); + RUN_TEST_CASE( + TestRaiiString, + test_RaiiStringCreateFromCString_SetsZeroLengthAndNullptrIfLengthIsGreaterThanMaxLength); + + // RaiiStringClean() + RUN_TEST_CASE(TestRaiiString, test_RaiiStringClean_SetsNullptrInObject); + RUN_TEST_CASE(TestRaiiString, test_RaiiStringClean_SetsLengthZeroInObject); +} diff --git a/CoDeLib/Test/src/TestUnzipMinizip.c b/CoDeLib/Test/src/TestUnzipMinizip.c new file mode 100644 index 00000000..c8416270 --- /dev/null +++ b/CoDeLib/Test/src/TestUnzipMinizip.c @@ -0,0 +1,88 @@ +#include "unity_fixture.h" +#include +#include +#include +#include +#include +#include + +static char *g_pFullPathToBenchmarkTestFiles = NULL; + +void SetupTestUnzipMinizip(char *pFullPathToBenchmarkTestFiles) { + g_pFullPathToBenchmarkTestFiles = pFullPathToBenchmarkTestFiles; +} + +TEST_GROUP(TestUnZipMinizip); + +TEST_SETUP(TestUnZipMinizip) { + // Nothing +} + +TEST_TEAR_DOWN(TestUnZipMinizip) { + // Nothing +} + +TEST(TestUnZipMinizip, test_UnZipMinizip_UnZipsSingleFileCorrectly) { + TEST_IGNORE_MESSAGE("In progress"); + + FILE *pInFile = NULL; + FILE *pUnZippedFile = NULL; + FILE *pReferenceUnZippedFile = NULL; + + OpenFile(&pInFile, g_pFullPathToBenchmarkTestFiles, + "/SmallBasicTextFileZip_store/SmallBasicTextFile_store.zip", "r"); + OpenFile( + &pUnZippedFile, g_pFullPathToBenchmarkTestFiles, + "/SmallBasicTextFileZip_store/SmallBasicTextFile_store.unzipped.txt", + "w+"); + OpenFile(&pReferenceUnZippedFile, g_pFullPathToBenchmarkTestFiles, + "/SmallBasicTextFileZip_store/SmallBasicTextFile_store.txt", "r"); + + TEST_ASSERT(FilesAreEqual(pUnZippedFile, pReferenceUnZippedFile)); + fclose(pInFile); + fclose(pUnZippedFile); + fclose(pReferenceUnZippedFile); +} + +TEST(TestUnZipMinizip, test_UnZipMinizip_UnZipsMultipleFilesCorrectly) { + TEST_IGNORE_MESSAGE("In progress"); + + FILE *pInFile = NULL; + FILE *pUnZippedFiles[] = {NULL, NULL, NULL}; + FILE *pReferenceUnZippedFiles[] = {NULL, NULL, NULL}; + const size_t fileCount = 3; + + OpenFile(&pInFile, g_pFullPathToBenchmarkTestFiles, + "/MultiTextFileZip_store/MultiTextFileZip_store.zip", "r"); + // Output files + OpenFile(&pUnZippedFiles[0], g_pFullPathToBenchmarkTestFiles, + "/MultiTextFileZip_store/TextFileOne_store.unzipped.txt", "w+"); + OpenFile(&pUnZippedFiles[1], g_pFullPathToBenchmarkTestFiles, + "/MultiTextFileZip_store/TextFileTwo_store.unzipped.txt", "w+"); + OpenFile(&pUnZippedFiles[2], g_pFullPathToBenchmarkTestFiles, + "/MultiTextFileZip_store/ThirdTextFile_store.unzipped.txt", "w+"); + // Reference files + OpenFile(&pReferenceUnZippedFiles[0], g_pFullPathToBenchmarkTestFiles, + "/MultiTextFileZip_store/TextFileOne_store.txt", "r"); + OpenFile(&pReferenceUnZippedFiles[1], g_pFullPathToBenchmarkTestFiles, + "/MultiTextFileZip_store/TextFileTwo_store.txt", "r"); + OpenFile(&pReferenceUnZippedFiles[2], g_pFullPathToBenchmarkTestFiles, + "/MultiTextFileZip_store/ThirdTextFile_store.txt", "r"); + + for (size_t i = 0; i < fileCount; ++i) { + TEST_ASSERT( + FilesAreEqual(pUnZippedFiles[i], pReferenceUnZippedFiles[i])); + } + fclose(pInFile); + for (size_t i = 0; i < fileCount; ++i) { + fclose(pUnZippedFiles[i]); + fclose(pReferenceUnZippedFiles[i]); + } +} + +TEST_GROUP_RUNNER(TestUnZipMinizip) { + RUN_TEST_CASE(TestUnZipMinizip, + test_UnZipMinizip_UnZipsSingleFileCorrectly); + RUN_TEST_CASE(TestUnZipMinizip, + test_UnZipMinizip_UnZipsMultipleFilesCorrectly); +} diff --git a/CoDeLib/Test/src/TestUnzipMinizip.h b/CoDeLib/Test/src/TestUnzipMinizip.h new file mode 100644 index 00000000..90e9085b --- /dev/null +++ b/CoDeLib/Test/src/TestUnzipMinizip.h @@ -0,0 +1 @@ +void SetupTestUnzipMinizip(char *pFullPathToBenchmarkTestFiles); \ No newline at end of file diff --git a/CoDeLib/Test/src/main.c b/CoDeLib/Test/src/main.c new file mode 100644 index 00000000..77357592 --- /dev/null +++ b/CoDeLib/Test/src/main.c @@ -0,0 +1,29 @@ +#include "unity_fixture.h" + +#include "TestDeflateInflateZlib.h" +#include "TestUnzipMinizip.h" +#include + +static void RunAllTests(void) { + RUN_TEST_GROUP(TestRaiiString); + RUN_TEST_GROUP(TestDeflateInflateZlib); + RUN_TEST_GROUP(TestUnZipMinizip); +} + +int main(int argc, const char **argv) { + RaiiString fullPathToBenchmarkTestFiles + __attribute__((cleanup(RaiiStringClean))); + + // TODO: use getopt(...) + if (argc == 1) { + printf("No arguments provided\n"); + return 1; + } else { + fullPathToBenchmarkTestFiles = RaiiStringCreateFromCString(argv[1]); + } + + SetupTestDeflateInflateZlib(fullPathToBenchmarkTestFiles.pString); + SetupTestUnzipMinizip(fullPathToBenchmarkTestFiles.pString); + + return UnityMain(argc, argv, RunAllTests); +} \ No newline at end of file diff --git a/CoDeLib/include/CoDeLib/RaiiString/RaiiString.h b/CoDeLib/include/CoDeLib/RaiiString/RaiiString.h index ec348338..d738362d 100644 --- a/CoDeLib/include/CoDeLib/RaiiString/RaiiString.h +++ b/CoDeLib/include/CoDeLib/RaiiString/RaiiString.h @@ -11,8 +11,14 @@ typedef struct { char *pString; - size_t length; + + // Length of the string (including the null terminator) + size_t lengthWithTermination; } RaiiString; +// Maximum length of a C string (including the null terminator) +static const size_t MAX_CSTRING_INCLUDING_TERMINATION_LENGTH = 1024; + RaiiString RaiiStringCreate(size_t length); +RaiiString RaiiStringCreateFromCString(const char *pCString); void RaiiStringClean(RaiiString *pThis); \ No newline at end of file diff --git a/Scripts/RunTest.py b/Scripts/RunTest.py index c6ceee04..f9e96b11 100644 --- a/Scripts/RunTest.py +++ b/Scripts/RunTest.py @@ -80,8 +80,9 @@ def RunTests( buildEnv: EnvironmentConfig.EnvironmentConfiguration, buildConfig: EnvironmentConfig.BuildConfig, testList: List[str], -) -> List[str]: +) -> tuple[bool, List[str]]: RawTestResultsFileNames = [] + success = True BuildTypeString = EnvironmentConfig.BuildConfig.ToCMakeBuildType(buildConfig) targetPlatformString = EnvironmentConfig.Platform.PlatformToOsName( @@ -118,18 +119,19 @@ def RunTests( TestResultsFileRaw = open(RawTestResultsFileNames[-1], "w") - subprocess.run( + result = subprocess.run( TestCommand, shell=True, - check=True, + check=False, stdout=TestResultsFileRaw, ) - PrependTestHeaderInRawResultsFile(testHeader, RawTestResultsFileNames[-1]) - TestResultsFileRaw.close() - return RawTestResultsFileNames + if result.returncode != 0: + success = False + + return (success, RawTestResultsFileNames) # ===================================================================================== @@ -147,13 +149,21 @@ def RunTests( RepositoryRootPath, targetPlatform ) -RawResultFiles = RunTests(BuildEnv, EnvironmentConfig.BuildConfig.DEBUG, TestList) -AppendTestResultToTotalResultFile(TestResultsFileName, RawResultFiles) +[successDebug, RawResultFilesDebug] = RunTests( + BuildEnv, EnvironmentConfig.BuildConfig.DEBUG, TestList +) +AppendTestResultToTotalResultFile(TestResultsFileName, RawResultFilesDebug) -RawResultFiles = RunTests(BuildEnv, EnvironmentConfig.BuildConfig.RELEASE, TestList) -AppendTestResultToTotalResultFile(TestResultsFileName, RawResultFiles) +[successRelease, RawResultFilesRelease] = RunTests( + BuildEnv, EnvironmentConfig.BuildConfig.RELEASE, TestList +) +AppendTestResultToTotalResultFile(TestResultsFileName, RawResultFilesRelease) # Print the content of BenchmarkResultFileName to the console print("\n") with open(TestResultsFileName, "r") as file: print(file.read()) + +if not successDebug or not successRelease: + print("Test failed") + exit(1)