Add some catch2 tests

This commit is contained in:
Martin 2023-11-19 20:45:17 +01:00
parent 697f010d70
commit 05ab4dc05c
5 changed files with 24072 additions and 1 deletions

View File

@ -10,7 +10,7 @@ if(CMAKE_MINOR_VERSION LESS 12)
project(miniz) project(miniz)
# see issue https://gitlab.kitware.com/cmake/cmake/merge_requests/1799 # see issue https://gitlab.kitware.com/cmake/cmake/merge_requests/1799
else() else()
project(miniz C) project(miniz)
set(CMAKE_C_STANDARD 90) set(CMAKE_C_STANDARD 90)
set(CMAKE_VERBOSE_MAKEFILE ON) set(CMAKE_VERBOSE_MAKEFILE ON)
# set(CMAKE_C_VISIBILITY_PRESET hidden) # set(CMAKE_C_VISIBILITY_PRESET hidden)
@ -42,6 +42,7 @@ option(BUILD_FUZZERS "Build fuzz targets" OFF)
option(AMALGAMATE_SOURCES "Amalgamate sources into miniz.h/c" OFF) option(AMALGAMATE_SOURCES "Amalgamate sources into miniz.h/c" OFF)
option(BUILD_HEADER_ONLY "Build a header-only version" OFF) option(BUILD_HEADER_ONLY "Build a header-only version" OFF)
option(BUILD_SHARED_LIBS "Build shared library instead of static" OFF) option(BUILD_SHARED_LIBS "Build shared library instead of static" OFF)
option(BUILD_TESTS "Build tests" ${MINIZ_STANDALONE_PROJECT})
option(INSTALL_PROJECT "Install project" ${MINIZ_STANDALONE_PROJECT}) option(INSTALL_PROJECT "Install project" ${MINIZ_STANDALONE_PROJECT})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
@ -296,6 +297,20 @@ if(BUILD_FUZZERS)
target_link_libraries(zip_fuzzer miniz) target_link_libraries(zip_fuzzer miniz)
endif() endif()
if(BUILD_TESTS)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
add_executable(catch_tests tests/main.cpp
tests/catch_amalgamated.cpp)
target_link_libraries(catch_tests miniz)
enable_testing()
add_test(NAME catch_tests
COMMAND $<TARGET_FILE:catch_tests>)
endif()
set(INCLUDE_INSTALL_DIR "include") set(INCLUDE_INSTALL_DIR "include")
if(INSTALL_PROJECT) if(INSTALL_PROJECT)

17
CMakePresets.json Normal file
View File

@ -0,0 +1,17 @@
{
"version": 1,
"cmakeMinimumRequired": {
"major": 3,
"minor": 5,
"patch": 0
},
"configurePresets": [
{
"name": "gcc",
"displayName": "GCC",
"description": "Default build options for GCC",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build"
}
]
}

10795
tests/catch_amalgamated.cpp Normal file

File diff suppressed because it is too large Load Diff

13143
tests/catch_amalgamated.hpp Normal file

File diff suppressed because it is too large Load Diff

101
tests/main.cpp Normal file
View File

@ -0,0 +1,101 @@
#include "catch_amalgamated.hpp"
#include "../miniz.h"
#include <assert.h>
#include <string>
#ifdef _WIN32
#define unlink _unlink
#endif
bool create_test_zip()
{
unlink("test.zip");
mz_zip_archive zip_archive = {};
auto b = mz_zip_writer_init_file(&zip_archive, "test.zip", 0);
if (!b)
return false;
b = mz_zip_writer_add_mem(&zip_archive, "test.txt", "foo", 3, MZ_DEFAULT_COMPRESSION);
if (!b)
return false;
b = mz_zip_writer_finalize_archive(&zip_archive);
if (!b)
return false;
b = mz_zip_writer_end(&zip_archive);
if (!b)
return false;
return true;
}
TEST_CASE("Zip writer tests")
{
auto b = create_test_zip();
REQUIRE(b);
SECTION("Test test.txt content correct")
{
mz_zip_archive zip_archive = {};
auto b = mz_zip_reader_init_file(&zip_archive, "test.zip", 0);
REQUIRE(b);
size_t content_size;
auto content = mz_zip_reader_extract_file_to_heap(&zip_archive, "test.txt", &content_size, 0);
std::string_view content_view(reinterpret_cast<char *>(content), content_size);
REQUIRE(content_view == "foo");
REQUIRE(content_view.size() == 3);
free(content);
}
}
TEST_CASE("Tinfl / tdefl tests")
{
SECTION("simple_test1")
{
size_t cmp_len = 0;
const char *p = "This is a test.This is a test.This is a test.1234567This is a test.This is a test.123456";
size_t uncomp_len = strlen(p);
void *pComp_data = tdefl_compress_mem_to_heap(p, uncomp_len, &cmp_len, TDEFL_WRITE_ZLIB_HEADER);
REQUIRE(pComp_data);
size_t decomp_len = 0;
void *pDecomp_data = tinfl_decompress_mem_to_heap(pComp_data, cmp_len, &decomp_len, TINFL_FLAG_PARSE_ZLIB_HEADER);
REQUIRE(pDecomp_data);
REQUIRE(decomp_len == uncomp_len);
REQUIRE(memcmp(pDecomp_data, p, uncomp_len) == 0);
free(pComp_data);
free(pDecomp_data);
}
SECTION("simple_test2")
{
uint8_t cmp_buf[1024], decomp_buf[1024];
uLong cmp_len = sizeof(cmp_buf);
const char *p = "This is a test.This is a test.This is a test.1234567This is a test.This is a test.123456";
uLong uncomp_len = (uLong)strlen(p);
int status = compress(cmp_buf, &cmp_len, (const uint8_t *)p, uncomp_len);
REQUIRE(status == Z_OK);
REQUIRE(cmp_len <= compressBound(uncomp_len));
uLong decomp_len = sizeof(decomp_buf);
status = uncompress(decomp_buf, &decomp_len, cmp_buf, cmp_len);
;
REQUIRE(status == Z_OK);
REQUIRE(decomp_len == uncomp_len);
REQUIRE(memcmp(decomp_buf, p, uncomp_len) == 0);
}
}