Skip to content

Commit

Permalink
test: enable unity test fixture and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
enzoevers committed Nov 30, 2024
1 parent 152e229 commit cdb20d4
Show file tree
Hide file tree
Showing 25 changed files with 416 additions and 44 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
17 changes: 16 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
}
Binary file not shown.
1 change: 1 addition & 0 deletions Benchmark/Files/MultiTextFileZip_store/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The zip file has been created with 7zip and using deflate compression level `store` to not compress the files.
2 changes: 2 additions & 0 deletions Benchmark/Files/MultiTextFileZip_store/TextFileOne_store.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is
the first text file.
Empty file.
2 changes: 2 additions & 0 deletions Benchmark/Files/MultiTextFileZip_store/TextFileTwo_store.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Second file is this
.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The 3rd file contains

an empty line on line 2.
Empty file.
1 change: 1 addition & 0 deletions Benchmark/Files/SmallBasicTextFileZip_store/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The zip file has been created with 7zip and using deflate compression level `store` to not compress the file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a test file.
On the second line there are eight words
The third line has a tab.
And the last line is a longer piece. Consising of two sentences.
Empty file.
Binary file not shown.
32 changes: 28 additions & 4 deletions CoDeLib/RaiiString/src/RaiiString.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
#include <CoDeLib/RaiiString/RaiiString.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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;
}
24 changes: 18 additions & 6 deletions CoDeLib/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
)

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)


2 changes: 1 addition & 1 deletion CoDeLib/Test/Utility/FileUtils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 17 additions & 21 deletions CoDeLib/Test/src/TestDeflateInflateZlib.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "unity.h"
#include "unity_fixture.h"
#include <CoDeLib/Deflate_zlib/Deflate_zlib.h>
#include <CoDeLib/Inflate_zlib/Inflate_zlib.h>
#include <CoDeLib/RaiiString/RaiiString.h>
Expand All @@ -7,17 +7,23 @@
#include <stdbool.h>
#include <stdio.h>

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;
Expand All @@ -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();
}
TEST_GROUP_RUNNER(TestDeflateInflateZlib) {
RUN_TEST_CASE(TestDeflateInflateZlib, test_InflateZlibWorkWithDeflateZlib);
}
1 change: 1 addition & 0 deletions CoDeLib/Test/src/TestDeflateInflateZlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void SetupTestDeflateInflateZlib(char *pFullPathToBenchmarkTestFiles);
Loading

0 comments on commit cdb20d4

Please sign in to comment.