Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ env:
VCVARS_PATH: "C:/Program Files/Microsoft Visual Studio/2022/Enterprise/VC/Auxiliary/Build/vcvars64.bat"

jobs:
build:
name: "Build"
build_and_test:
name: "Build and Test"
runs-on: ${{matrix.os}}
strategy:
fail-fast: false
Expand All @@ -35,22 +35,32 @@ jobs:
with:
linux: |
mkdir build && cd build
cmake -G Ninja ..
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON -DCMAKE_C_FLAGS="-fsanitize=address,leak,undefined -fno-omit-frame-pointer" ..
cmake --build .
macos: |
mkdir build && cd build
cmake -G Ninja ..
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON -DCMAKE_C_FLAGS="-fsanitize=address,undefined -fno-omit-frame-pointer" ..
cmake --build .
windows: |
mkdir build && cd build
call "${{env.VCVARS_PATH}}"
cmake -G Ninja -DCMAKE_TOOLCHAIN_FILE="${{env.TOOLCHAIN_PATH}}" -DVCPKG_TARGET_TRIPLET=x64-windows-static-md ..
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON -DCMAKE_TOOLCHAIN_FILE="${{env.TOOLCHAIN_PATH}}" -DVCPKG_TARGET_TRIPLET=x64-windows-static-md -DCMAKE_C_FLAGS="/fsanitize=address" ..
cmake --build .
windowsShell: cmd

- uses: knicknic/os-specific-run@v1.0.4
name: Run CTest
with:
linux: ctest -j --output-on-failure --test-dir build
macos: ctest -j --output-on-failure --test-dir build
windows: |
call "${{env.VCVARS_PATH}}"
ctest -j --output-on-failure --test-dir build
windowsShell: cmd

integration_test:
name: "Integration Test"
needs: build
needs: build_and_test
runs-on: ${{matrix.os}}
strategy:
fail-fast: false
Expand Down Expand Up @@ -120,3 +130,4 @@ jobs:
archive: false
path: |
hamcore-${{matrix.os}}.se2
retention-days: 30
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ target_sources(libhamcore

find_package(ZLIB REQUIRED)
target_link_libraries(libhamcore PRIVATE ZLIB::ZLIB)

option(BUILD_TESTING "Enable testing" OFF)
if (BUILD_TESTING)
enable_testing()
add_subdirectory(tests)
endif()
64 changes: 64 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
add_library(TestModule)
target_sources(TestModule
PRIVATE
RandomFile.c
Utils.c
)
target_include_directories(TestModule PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/..)
target_link_libraries(TestModule PUBLIC libhamcore ZLIB::ZLIB)

add_executable(TestEncodeDecode)
target_sources(TestEncodeDecode PRIVATE TestEncodeDecode.c)
target_link_libraries(TestEncodeDecode PRIVATE TestModule)

function(EncodeDecodeTest mode num_files)
set(working_dir ${CMAKE_CURRENT_BINARY_DIR}/EncodeDecodeTest-${mode}-${num_files})
file(MAKE_DIRECTORY ${working_dir})

add_test(
NAME TestEncodeDecode-${mode}-${num_files}
COMMAND TestEncodeDecode ${mode} ${num_files}
WORKING_DIRECTORY ${working_dir}
)
endfunction()

EncodeDecodeTest(SMALL 1)
EncodeDecodeTest(SMALL 100)
EncodeDecodeTest(SMALL 1000)
EncodeDecodeTest(MEDIUM 1)
EncodeDecodeTest(MEDIUM 100)
EncodeDecodeTest(MEDIUM 500)
EncodeDecodeTest(LARGE 1)
EncodeDecodeTest(LARGE 100)
EncodeDecodeTest(LARGE 250)
EncodeDecodeTest(MIXED 10)
EncodeDecodeTest(MIXED 250)
EncodeDecodeTest(MIXED 500)

add_executable(TestArchiveFormat)
target_sources(TestArchiveFormat PRIVATE TestArchiveFormat.c)
target_link_libraries(TestArchiveFormat PRIVATE TestModule)

function(ArchiveFormatTest mode num_files)
set(working_dir ${CMAKE_CURRENT_BINARY_DIR}/TestArchiveFormat-${mode}-${num_files})
file(MAKE_DIRECTORY ${working_dir})

add_test(
NAME TestArchiveFormat-${mode}-${num_files}
COMMAND TestArchiveFormat ${mode} ${num_files}
WORKING_DIRECTORY ${working_dir}
)
endfunction()

ArchiveFormatTest(SMALL 1)
ArchiveFormatTest(SMALL 100)
ArchiveFormatTest(SMALL 1000)
ArchiveFormatTest(MEDIUM 1)
ArchiveFormatTest(MEDIUM 100)
ArchiveFormatTest(MEDIUM 500)
ArchiveFormatTest(LARGE 1)
ArchiveFormatTest(LARGE 100)
ArchiveFormatTest(LARGE 250)
ArchiveFormatTest(MIXED 10)
ArchiveFormatTest(MIXED 250)
ArchiveFormatTest(MIXED 500)
114 changes: 114 additions & 0 deletions tests/RandomFile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include "RandomFile.h"
#include "FileSystem.h"

#include <stdint.h>
#include <stdlib.h>
#include <string.h>

static uint32_t Xorshift32(uint32_t *state)
{
uint32_t x = *state;
x ^= x << 13;
x ^= x >> 17;
x ^= x << 5;
*state = x;
return x;
}

static uint8_t *MakeRandomBytes(const size_t size)
{
if (size == 0)
{
return NULL;
}

uint8_t *buffer = malloc(size);
if (!buffer)
{
return NULL;
}

uint32_t state = 12345;

size_t i = 0;
for (; i + sizeof(uint32_t) <= size; i += sizeof(uint32_t))
{
uint32_t random_byte = Xorshift32(&state);
memcpy(buffer + i, &random_byte, sizeof(uint32_t));
}
for (; i < size; i++)
{
buffer[i] = (uint8_t)(Xorshift32(&state) % 256);
}

return buffer;
}

RANDOM_FILE *CreateRandomFile(const char *path, const size_t size)
{
if (!path || size == 0)
{
return NULL;
}

RANDOM_FILE *random_file = malloc(sizeof(RANDOM_FILE));
if (!random_file)
{
return NULL;
}
memset(random_file, 0, sizeof(RANDOM_FILE));

random_file->Size = size;

random_file->Path = malloc(strlen(path) + 1);
if (!random_file->Path)
{
goto FINAL;
}
strcpy(random_file->Path, path);

uint8_t *random_bytes = MakeRandomBytes(size);
if (!random_bytes)
{
goto FINAL;
}

FILE *file = Ham_FileOpen(path, true);
if (!file)
{
free(random_bytes);
goto FINAL;
}

if (! Ham_FileWrite(file, random_bytes, random_file->Size))
{
free(random_bytes);
Ham_FileClose(file);
goto FINAL;
}

free(random_bytes);
Ham_FileClose(file);

return random_file;

FINAL:
DeleteRandomFile(random_file);
return NULL;
}

void DeleteRandomFile(RANDOM_FILE *file)
{
if (!file)
{
return;
}

if (file->Path)
{
remove(file->Path);
free(file->Path);
}

free(file);
}
17 changes: 17 additions & 0 deletions tests/RandomFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef RANDOMFILE_H
#define RANDOMFILE_H

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

typedef struct RANDOM_FILE
{
size_t Size;
char *Path;
} RANDOM_FILE;

RANDOM_FILE *CreateRandomFile(const char *path, const size_t size);
void DeleteRandomFile(RANDOM_FILE *file);

#endif
Loading
Loading