diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | CMakeLists.txt | 223 | ||||
-rw-r--r-- | TODO | 11 | ||||
-rw-r--r-- | build.c | 247 | ||||
-rw-r--r-- | build_and_test.sh | 11 | ||||
-rw-r--r-- | config.cmake.in | 8 | ||||
-rw-r--r-- | gen_cmake.c | 200 | ||||
-rw-r--r-- | source/CMakeLists.txt | 6 | ||||
-rw-r--r-- | source/kit/CMakeLists.txt | 27 | ||||
-rw-r--r-- | source/kit/_static.c | 14 | ||||
-rw-r--r-- | source/kit/allocator.h | 4 | ||||
-rw-r--r-- | source/kit/array_ref.h | 18 | ||||
-rw-r--r-- | source/kit/async_function.h | 4 | ||||
-rw-r--r-- | source/kit/atomic.h | 8 | ||||
-rw-r--r-- | source/kit/bigint.h | 1 | ||||
-rw-r--r-- | source/kit/input_buffer.c | 10 | ||||
-rw-r--r-- | source/kit/input_stream.c | 10 | ||||
-rw-r--r-- | source/kit/mersenne_twister_64.h | 4 | ||||
-rw-r--r-- | source/kit/move_back.h | 4 | ||||
-rw-r--r-- | source/kit/mutex.h | 5 | ||||
-rw-r--r-- | source/kit/secure_random.h | 5 | ||||
-rw-r--r-- | source/kit/sha256.h | 4 | ||||
-rw-r--r-- | source/kit/sockets.h | 4 | ||||
-rw-r--r-- | source/kit/status.h | 2 | ||||
-rw-r--r-- | source/kit/string_ref.h | 1 | ||||
-rw-r--r-- | source/kit/thread.h | 5 | ||||
-rw-r--r-- | source/kit/types.h | 17 | ||||
-rw-r--r-- | source/kit_test/CMakeLists.txt | 7 | ||||
-rw-r--r-- | source/kit_test/_static.c | 3 | ||||
-rw-r--r-- | source/kit_test/bench.c | 39 | ||||
-rw-r--r-- | source/kit_test/bench.h | 1 | ||||
-rw-r--r-- | source/kit_test/shared.inl.h | 38 | ||||
-rw-r--r-- | source/kit_test/test.c | 38 | ||||
-rw-r--r-- | source/kit_test/test.h | 4 | ||||
-rw-r--r-- | source/test/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/test/programs/CMakeLists.txt | 49 | ||||
-rw-r--r-- | source/test/unittests/CMakeLists.txt | 8 | ||||
-rw-r--r-- | source/tests/_static.c | 55 | ||||
-rw-r--r-- | source/tests/array_ref.test.c (renamed from source/test/unittests/array_ref.test.c) | 4 | ||||
-rw-r--r-- | source/tests/async_function.test.c (renamed from source/test/unittests/async_function.test.c) | 4 | ||||
-rw-r--r-- | source/tests/atomic.test.c (renamed from source/test/unittests/atomic.test.c) | 6 | ||||
-rw-r--r-- | source/tests/bench.test.c (renamed from source/test/unittests/foo.bench.c) | 4 | ||||
-rw-r--r-- | source/tests/bigint.test.c (renamed from source/test/unittests/bigint.test.c) | 4 | ||||
-rw-r--r-- | source/tests/condition_variable.test.c (renamed from source/test/unittests/condition_variable.test.c) | 16 | ||||
-rw-r--r-- | source/tests/cpp.cpp (renamed from source/test/programs/cpp_example.cpp) | 2 | ||||
-rw-r--r-- | source/tests/dynamic_array.test.c (renamed from source/test/unittests/dynamic_array.test.c) | 4 | ||||
-rw-r--r-- | source/tests/file.test.c (renamed from source/test/unittests/file.test.c) | 6 | ||||
-rw-r--r-- | source/tests/input_buffer.test.c (renamed from source/test/unittests/input_buffer.test.c) | 4 | ||||
-rw-r--r-- | source/tests/input_stream.test.c (renamed from source/test/unittests/input_stream.test.c) | 4 | ||||
-rw-r--r-- | source/tests/lower_bound.test.c (renamed from source/test/unittests/lower_bound.test.c) | 6 | ||||
-rw-r--r-- | source/tests/main.test.c (renamed from source/test/unittests/main.test.c) | 4 | ||||
-rw-r--r-- | source/tests/mersenne_twister_64.test.c (renamed from source/test/unittests/mersenne_twister_64.test.c) | 6 | ||||
-rw-r--r-- | source/tests/move_back.test.c (renamed from source/test/unittests/move_back.test.c) | 4 | ||||
-rw-r--r-- | source/tests/mutex.test.c (renamed from source/test/unittests/mutex.test.c) | 20 | ||||
-rw-r--r-- | source/tests/secure_random.test.c (renamed from source/test/unittests/secure_random.test.c) | 4 | ||||
-rw-r--r-- | source/tests/sha256.test.c (renamed from source/test/unittests/sha256.test.c) | 6 | ||||
-rw-r--r-- | source/tests/signals.cpp (renamed from source/test/programs/signals.cpp) | 2 | ||||
-rw-r--r-- | source/tests/string_ref.test.c (renamed from source/test/unittests/string_ref.test.c) | 4 | ||||
-rw-r--r-- | source/tests/test_duration.test.c (renamed from source/test/unittests/test_duration.test.c) | 5 | ||||
-rw-r--r-- | source/tests/thread.test.c (renamed from source/test/unittests/thread.test.c) | 4 | ||||
-rw-r--r-- | source/tests/too_many_assertions.c (renamed from source/test/programs/too_many_assertions.c) | 2 | ||||
-rw-r--r-- | source/tests/too_many_tests.c (renamed from source/test/programs/too_many_tests.c) | 2 |
62 files changed, 285 insertions, 941 deletions
@@ -1,5 +1,3 @@ /*build*/ *.swp gen_inl -gen_cmake -build_c diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index b199e4d..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,223 +0,0 @@ -cmake_minimum_required(VERSION 3.16) - -set(CMAKE_EXPORT_COMPILE_COMMANDS ON CACHE BOOL "" FORCE) - -if(NOT DEFINED CMAKE_BUILD_PARALLEL_LEVEL) - set(CMAKE_BUILD_PARALLEL_LEVEL 4 CACHE STRING "" FORCE) -endif() - -option(KIT_DISABLE_SYSTEM_MALLOC "Disable system memory allocator" OFF) -option(KIT_DISABLE_SYSTEM_THREADS "Disable system threads" OFF) -option(KIT_ENABLE_CUSTOM_ALLOC_DISPATCH "Enable custom allocator dispatch" OFF) -option(KIT_ENABLE_CUSTOM_ASYNC_FUNCTION_DISPATCH "Enable custom async function dispatch" OFF) -option(KIT_ENABLE_TESTING "Enable testing" ON) - -project( - kit - VERSION 0.1.1 - DESCRIPTION "A collection of C libraries" - LANGUAGES C) - -add_library(kit STATIC) -add_library(kit::kit ALIAS kit) -target_include_directories( - kit INTERFACE - $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/source>) -if(WIN32) - target_link_libraries(kit PUBLIC Shlwapi) -endif() - -if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") - target_compile_options(kit PUBLIC -pedantic -Wall -Werror) -endif() - -include(CheckCSourceRuns) - -check_c_source_runs( - " - __attribute__((weak)) int foo(); - int main() { return 0; } - " - KIT_HAVE_FUNC_ATTRIBUTE_WEAK -) - -set(CMAKE_REQUIRED_LIBRARIES_BACK_ ${CMAKE_REQUIRED_LIBRARIES}) -set(CMAKE_REQUIRED_LIBRARIES "pthread") -check_c_source_runs( - " - #define _GNU_SOURCE - #include <pthread.h> - int main() { - pthread_mutexattr_t attr; - pthread_mutexattr_init(&attr); - pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); - return 0; - } - " - KIT_HAVE_PTHREAD_MUTEXATTR_SETTYPE -) -set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_BACK_}) -unset(CMAKE_REQUIRED_LIBRARIES_BACK_) - -check_c_source_runs( - " - #include <pthread.h> - int main() { - return 0; - } - " - KIT_HAVE_PTHREAD -) - -check_c_source_runs( - " - #include <Windows.h> - int main() { - return 0; - } - " - KIT_HAVE_WINDOWS -) - -check_c_source_runs( - " - #include <stdlib.h> - int main() { - void *p = malloc(10); - free(p); - return 0; - } - " - KIT_HAVE_MALLOC -) - -if(NOT KIT_HAVE_PTHREAD AND NOT KIT_HAVE_WINDOWS) - set(KIT_DISABLE_SYSTEM_THREADS ON) -endif() - -if(NOT KIT_HAVE_MALLOC) - set(KIT_DISABLE_SYSTEM_MALLOC ON) -endif() - -if(KIT_HAVE_FUNC_ATTRIBUTE_WEAK) - target_compile_definitions(kit PUBLIC KIT_HAVE_FUNC_ATTRIBUTE_WEAK) -endif() - -if(KIT_HAVE_PTHREAD_MUTEXATTR_SETTYPE) - target_compile_definitions(kit PUBLIC KIT_HAVE_PTHREAD_MUTEXATTR_SETTYPE) -endif() - -if(KIT_DISABLE_SYSTEM_MALLOC) - target_compile_definitions(kit PUBLIC KIT_DISABLE_SYSTEM_MALLOC) -endif() - -if(KIT_DISABLE_SYSTEM_THREADS) - target_compile_definitions(kit PUBLIC KIT_DISABLE_SYSTEM_THREADS) -endif() - -if(KIT_ENABLE_CUSTOM_ALLOC_DISPATCH) - target_compile_definitions(kit PUBLIC KIT_ENABLE_CUSTOM_ALLOC_DISPATCH) -endif() - -if(KIT_ENABLE_CUSTOM_ASYNC_FUNCTION_DISPATCH) - target_compile_definitions(kit PUBLIC KIT_ENABLE_CUSTOM_ASYNC_FUNCTION_DISPATCH) -endif() - -if(NOT KIT_DISABLE_SYSTEM_THREADS) - find_package(Threads REQUIRED) - target_link_libraries(kit PUBLIC Threads::Threads) -endif() - -add_library(kit_test STATIC) -add_library(kit::kit_test ALIAS kit_test) -target_include_directories( - kit_test INTERFACE - $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/source>) - -enable_testing() - -if(KIT_ENABLE_TESTING) - add_executable(kit_test_suite) - add_executable(kit::kit_test_suite ALIAS kit_test_suite) - target_link_libraries(kit_test_suite PRIVATE kit kit_test) - - if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") - target_compile_options( - kit PUBLIC - -fsanitize=undefined,address - --coverage -O0 -g) - target_link_options( - kit PUBLIC - -fsanitize=undefined,address - --coverage) - endif() - - add_test( - NAME kit_unittests - COMMAND kit_test_suite --no-carriage-return) - set_tests_properties( - kit_unittests - PROPERTIES - TIMEOUT "120") -endif() - -add_subdirectory(source) - -include(GNUInstallDirs) - -file( - GLOB_RECURSE headers_ - RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/source/kit - ${CMAKE_CURRENT_SOURCE_DIR}/source/kit/*.h) - -foreach(path_ ${headers_}) - get_filename_component(dir_ "${path_}" DIRECTORY) - - install( - FILES ${CMAKE_CURRENT_SOURCE_DIR}/source/kit/${path_} - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/kit/${dir_}) -endforeach() - -unset(headers_) -unset(path_) -unset(dir_) - -install( - TARGETS kit kit_test - EXPORT kit-targets - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) - -install( - EXPORT kit-targets - FILE kit-targets.cmake - NAMESPACE ${PROJECT_NAME}:: - DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}") - -export( - EXPORT kit-targets - FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/kit-targets.cmake" - NAMESPACE ${PROJECT_NAME}::) - -include(CMakePackageConfigHelpers) - -string(TOLOWER ${PROJECT_NAME} project_lower_) - -configure_package_config_file( - ${CMAKE_CURRENT_SOURCE_DIR}/config.cmake.in - "${CMAKE_CURRENT_BINARY_DIR}/${project_lower_}-config.cmake" - INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) - -write_basic_package_version_file( - "${CMAKE_CURRENT_BINARY_DIR}/${project_lower_}-config-version.cmake" - VERSION "${PROJECT_VERSION}" - COMPATIBILITY AnyNewerVersion) - -install( - FILES - "${CMAKE_CURRENT_BINARY_DIR}/${project_lower_}-config.cmake" - "${CMAKE_CURRENT_BINARY_DIR}/${project_lower_}-config-version.cmake" - DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}) - -unset(project_lower_) @@ -1,11 +1,10 @@ To-Do +- Code + - Remove "const" - Cryptography - hmac_sha256 https://github.com/h5p9sl/hmac_sha256 - ecdh https://github.com/kokke/tiny-ECDH-c -- Build system - - build.c - - Remove CMake scripts - Allocators - Memory pool - Memory stack @@ -14,6 +13,10 @@ To-Do - Terminal - File and memory mapping - Inter-process transfer - - SDL boilerplate + - Graphics boilerplate + - X11 (Linux) + - Win32 (Windows) + - Cocoa (macOS) + - Emscripten (WebAssembly) - Add sockets tests diff --git a/build.c b/build.c deleted file mode 100644 index f9c0f95..0000000 --- a/build.c +++ /dev/null @@ -1,247 +0,0 @@ -#if 0 -gcc -fsanitize=address,undefined,leak build.c -o build_c && ./build_c -exit -#endif - -/********************************************************************* - * * - * KIT Library * - * * - *********************************************************************/ - -// Just paste the Kit library code here. - -#include "include/kit.inl.h" - -/********************************************************************* - * * - * BUILD_C * - * * - * Experimental pure C build system. * - * Work in progress. * - * * - *********************************************************************/ - -#include <stdio.h> - -#define DL PATH_DELIM - -enum { MAX_STR = 4096, MAX_DEPENDENCIES = 100, MAX_UNITS = 400 }; - -typedef DA(ptrdiff_t) index_array_t; - -typedef enum { UNIT_NONE, UNIT_HEADER, UNIT_SOURCE } unit_type_t; - -typedef struct { - string_t path; - unit_type_t type; - index_array_t dependencies; -} unit_t; - -typedef DA(unit_t) unit_array_t; - -unit_array_t units; - -kit_allocator_t ALLOC; - -int is_header(str_t file) { - ptrdiff_t const n = file.size; - return n > 2 && file.values[n - 2] == '.' && - file.values[n - 1] == 'h'; -} - -int is_source(str_t file) { - ptrdiff_t const n = file.size; - return n > 2 && file.values[n - 2] == '.' && - file.values[n - 1] == 'c'; -} - -unit_type_t unit_type(str_t file) { - if (is_header(file)) - return UNIT_HEADER; - if (is_source(file)) - return UNIT_SOURCE; - return UNIT_NONE; -} - -void enum_sources(str_t folder) { - path_list_t const list = file_enum_folder(folder, ALLOC); - - for (ptrdiff_t i = 0; i < list.files.size; i++) { - str_t item = WRAP_STR(list.files.values[i]); - string_t file = path_join(folder, item, ALLOC); - path_type_t type = path_type(WRAP_STR(file)); - unit_type_t utype = unit_type(WRAP_STR(file)); - - switch (type) { - case PATH_FILE: - if (utype != UNIT_NONE) { - unit_t unit = { .path = file, .type = utype }; - DA_INIT(unit.dependencies, 0, ALLOC); - DA_APPEND(units, unit); - } else - DA_DESTROY(file); - break; - - case PATH_FOLDER: enum_sources(WRAP_STR(file)); - default: DA_DESTROY(file); - } - } - - path_list_destroy(list); -} - -void skip_comment(FILE *f) { - char c = fgetc(f); - if (c != '/' && c != '*') - return; - if (c == '/') - while (c != '\n') { - c = fgetc(f); - if (c == '\\') - fgetc(f); - } - else - while (c != '*') { - c = fgetc(f); - if (c == '*' && fgetc(f) == '/') - break; - } -} - -typedef struct { - int ok; - int is_local; - string_t path; -} include_t; - -include_t parse_include(FILE *f) { - include_t result = { .ok = 0 }; - char c = ' '; - while (c == ' ') c = fgetc(f); - if (c != '#') - return result; - c = ' '; - while (c == ' ') c = fgetc(f); - if (c != 'i') - return result; - char s[] = "nclude"; - for (int i = 0; i + 1 < sizeof s; i++) - if (fgetc(f) != s[i]) - return result; - c = ' '; - while (c == ' ') c = fgetc(f); - if (c != '"' && c != '<') - return result; - if (c == '"') - result.is_local = 1; - else - result.is_local = 0; - DA_INIT(result.path, 1, ALLOC); - for (;;) { - char z = fgetc(f); - if (c == '"' && z == '"') - break; - if (c == '<' && z == '>') - break; - if (z == '\n' || z == '\r' || z == '\0') { - DA_DESTROY(result.path); - return result; - } - result.path.values[result.path.size - 1] = z; - DA_RESIZE(result.path, result.path.size + 1); - } - DA_RESIZE(result.path, result.path.size - 1); - if (result.path.size == 0) { - DA_DESTROY(result.path); - return result; - } - c = ' '; - while (c == ' ') c = fgetc(f); - if (c != '\n' && c != '\r') { - DA_DESTROY(result.path); - return result; - } - result.ok = 1; - return result; -} - -str_t base_path(str_t path) { - ptrdiff_t n = 0; - while (path_index(path, n).size != 0) n++; - return path_take(path, n - 2); -} - -void parse_unit(unit_t *u) { - FILE *f = fopen(BS(u->path), "rt"); - if (f == NULL) { - printf("Can't read %s\n", BS(u->path)); - return; - } - - str_t base = base_path(WRAP_STR(u->path)); - - while (!feof(f)) { - char c = fgetc(f); - switch (c) { - case '/': skip_comment(f); break; - case '\n': { - include_t inc = parse_include(f); - if (inc.ok) { - if (inc.is_local) { - string_t temp = path_join(base, WRAP_STR(inc.path), - ALLOC); - string_t inc_path = path_norm(WRAP_STR(temp), ALLOC); - for (ptrdiff_t i = 0; i < units.size; i++) - if (AR_EQUAL(units.values[i].path, inc_path)) { - ptrdiff_t k = u->dependencies.size; - DA_RESIZE(u->dependencies, k + 1); - u->dependencies.values[k] = i; - break; - } - DA_DESTROY(temp); - DA_DESTROY(inc_path); - } - DA_DESTROY(inc.path); - } - } break; - default:; - } - } - - fclose(f); -} - -void parse_units(void) { - for (ptrdiff_t i = 0; i < units.size; i++) - parse_unit(units.values + i); -} - -int main(int argc, char **argv) { - ALLOC = kit_alloc_default(); - DA_INIT(units, 0, ALLOC); - - enum_sources(SZ("." DL "src")); - enum_sources(SZ("." DL "source")); - parse_units(); - - for (ptrdiff_t i = 0; i < units.size; i++) { - unit_t *u = units.values + i; - if (u->type == UNIT_HEADER) - printf("[ Header ] "); - else - printf("[ Source ] "); - printf("%s\n", BS(u->path)); - for (ptrdiff_t j = 0; j < u->dependencies.size; j++) - printf(" -> %s\n", - BS(units.values[u->dependencies.values[j]].path)); - } - - for (ptrdiff_t i = 0; i < units.size; i++) { - DA_DESTROY(units.values[i].path); - DA_DESTROY(units.values[i].dependencies); - } - DA_DESTROY(units); - - return 0; -} diff --git a/build_and_test.sh b/build_and_test.sh new file mode 100644 index 0000000..e78ef2d --- /dev/null +++ b/build_and_test.sh @@ -0,0 +1,11 @@ +if [ ! -d "build" ]; then + mkdir build +fi + +gcc -c -o "build/kit.o" "source/kit/_static.c" +ar rcs "build/libkit.a" "build/kit.o" + +gcc -c -o "build/kit_test.o" "source/kit_test/_static.c" +ar rcs "build/libkit_test.a" "build/kit_test.o" + +gcc -o "build/kit_test_suite" "source/tests/_static.c" -Lbuild -lkit -lkit_test diff --git a/config.cmake.in b/config.cmake.in deleted file mode 100644 index d2f3c05..0000000 --- a/config.cmake.in +++ /dev/null @@ -1,8 +0,0 @@ -@PACKAGE_INIT@ - -if(NOT @KIT_DISABLE_SYSTEM_THREADS@) - include(CMakeFindDependencyMacro) - find_dependency(Threads) -endif() - -include("${CMAKE_CURRENT_LIST_DIR}/kit-targets.cmake") diff --git a/gen_cmake.c b/gen_cmake.c deleted file mode 100644 index cfb4f24..0000000 --- a/gen_cmake.c +++ /dev/null @@ -1,200 +0,0 @@ -#if 0 -gcc -fsanitize=address,undefined,leak gen_cmake.c -o gen_cmake && ./gen_cmake -exit -#endif - -#include "include/kit.inl.h" - -#include <stdio.h> - -kit_allocator_t ALLOC; - -typedef struct { - kit_status_t status; - int have_cmakelists; -} gen_result_t; - -int is_header(str_t const file) { - return file.size > 2 && file.values[file.size - 2] == '.' && - file.values[file.size - 1] == 'h'; -} - -int is_source(str_t const file) { - return file.size > 2 && file.values[file.size - 2] == '.' && - file.values[file.size - 1] == 'c'; -} - -gen_result_t gen_cmakelists_for(str_t const target, - str_t const folder) { - gen_result_t result = { KIT_OK, 0 }; - - path_list_t const list = file_enum_folder(folder, ALLOC); - - if (list.status != KIT_OK) { - printf("Error: file_enum_folder failed with code %d.\n", - list.status); - result.status = list.status; - return result; - } - - int have_headers = 0; - int have_sources = 0; - int have_folders = 0; - - FILE *out = NULL; - - string_t cmakelists_path = path_join(folder, SZ("CMakeLists.txt"), - ALLOC); - - DA_APPEND(cmakelists_path, '\0'); - - if (cmakelists_path.size == 0 || - cmakelists_path.values[cmakelists_path.size - 1] != '\0') { - result.status = KIT_ERROR_BAD_ALLOC; - result.have_cmakelists = 0; - DA_DESTROY(cmakelists_path); - path_list_destroy(list); - return result; - } - -#define OPEN_CMAKELISTS \ - do { \ - if (out == NULL) { \ - out = fopen(cmakelists_path.values, "wt"); \ - if (out == NULL) { \ - result.status = 101; \ - result.have_cmakelists = 0; \ - DA_DESTROY(cmakelists_path); \ - path_list_destroy(list); \ - return result; \ - } \ - } \ - } while (0) - -#define CHECK_TYPE(type_) \ - string_t const full_path_ = path_join(folder, WRAP_STR(item), \ - ALLOC); \ - int check_ = path_type(WRAP_STR(full_path_)) == (type_); \ - DA_DESTROY(full_path_); \ - if (!check_) \ - continue - - for (ptrdiff_t i = 0; i < list.files.size; i++) { - str_t const item = WRAP_STR(list.files.values[i]); - - CHECK_TYPE(PATH_FILE); - - if (!is_header(item)) - continue; - - OPEN_CMAKELISTS; - - if (!have_headers) - fprintf(out, "target_sources(\n %*s\n PUBLIC", - (int) target.size, BS(target)); - - fprintf( - out, - "\n $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/%*s>", - (int) item.size, BS(item)); - - have_headers = 1; - } - - ptrdiff_t const LINE_LIMIT = 80; - ptrdiff_t line_length = LINE_LIMIT; - - for (ptrdiff_t i = 0; i < list.files.size; i++) { - str_t const item = WRAP_STR(list.files.values[i]); - - CHECK_TYPE(PATH_FILE); - - if (!is_source(item)) - continue; - - OPEN_CMAKELISTS; - - if (!have_headers && !have_sources) - fprintf(out, "target_sources(\n %*s\n PRIVATE", - (int) target.size, BS(target)); - else if (!have_sources) - fprintf(out, "\n PRIVATE"); - - if (line_length + item.size > LINE_LIMIT) { - fprintf(out, "\n "); - line_length = 5; - } - - fprintf(out, " %*s", (int) item.size, BS(item)); - line_length += 1 + item.size; - - have_sources = 1; - } - - if (have_headers || have_sources) - fprintf(out, ")\n"); - - for (ptrdiff_t i = 0; i < list.files.size; i++) { - str_t const item = WRAP_STR(list.files.values[i]); - - CHECK_TYPE(PATH_FOLDER); - - string_t const subfolder = path_join(folder, item, ALLOC); - gen_result_t const r = gen_cmakelists_for(target, - WRAP_STR(subfolder)); - DA_DESTROY(subfolder); - if (r.status != KIT_OK) { - result.status = r.status; - break; - } - - if (!r.have_cmakelists) - continue; - - OPEN_CMAKELISTS; - - if (have_headers || have_sources) - fprintf(out, "\n"); - - fprintf(out, "add_subdirectory(%*s)\n", (int) item.size, - BS(item)); - - have_folders = 1; - } - - if (out != NULL) - fclose(out); - - DA_DESTROY(cmakelists_path); - path_list_destroy(list); - - if (result.status == KIT_OK) - result.have_cmakelists = have_headers || have_sources || - have_folders; - - if (!result.have_cmakelists && out != NULL) - file_remove(WRAP_STR(cmakelists_path)); - - return result; -} - -int main(int argc, char **argv) { - ALLOC = kit_alloc_default(); - - str_t const targets[] = { SZ("kit"), SZ("kit_test"), - SZ("kit_test_suite") }; - - str_t const folders[] = { - SZ("." PATH_DELIM "source" PATH_DELIM "kit"), - SZ("." PATH_DELIM "source" PATH_DELIM "kit_test"), - SZ("." PATH_DELIM "source" PATH_DELIM "test/unittests") - }; - - int ok = 1; - - for (ptrdiff_t i = 0; i < sizeof folders / sizeof *folders; i++) - ok = ok && - gen_cmakelists_for(targets[i], folders[i]).status == KIT_OK; - - return ok ? 0 : 1; -} diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt deleted file mode 100644 index 1444b6a..0000000 --- a/source/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -add_subdirectory(kit) -add_subdirectory(kit_test) - -if(KIT_ENABLE_TESTING) - add_subdirectory(test) -endif() diff --git a/source/kit/CMakeLists.txt b/source/kit/CMakeLists.txt deleted file mode 100644 index 2e89133..0000000 --- a/source/kit/CMakeLists.txt +++ /dev/null @@ -1,27 +0,0 @@ -target_sources( - kit - PUBLIC - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/mutex.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/status.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/atomic.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/allocator.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/string_ref.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/thread.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/bigint.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/dynamic_array.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/condition_variable.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/async_function.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/move_back.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/secure_random.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/input_stream.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/thread_defs.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/input_buffer.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/lower_bound.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/sha256.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/file.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/array_ref.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/mersenne_twister_64.h> - PRIVATE - input_buffer.c secure_random.c sha256.c thread.posix.c atomic.win32.c - condition_variable.c thread.win32.c input_stream.c file.c allocator.c - array_ref.c dynamic_array.c mutex.c mersenne_twister_64.c) diff --git a/source/kit/_static.c b/source/kit/_static.c new file mode 100644 index 0000000..f098ab1 --- /dev/null +++ b/source/kit/_static.c @@ -0,0 +1,14 @@ +#include "allocator.c" +#include "array_ref.c" +#include "atomic.win32.c" +#include "condition_variable.c" +#include "dynamic_array.c" +#include "file.c" +#include "input_buffer.c" +#include "input_stream.c" +#include "mersenne_twister_64.c" +#include "mutex.c" +#include "secure_random.c" +#include "sha256.c" +#include "thread.posix.c" +#include "thread.win32.c" diff --git a/source/kit/allocator.h b/source/kit/allocator.h index 7b73b20..f6dc62d 100644 --- a/source/kit/allocator.h +++ b/source/kit/allocator.h @@ -1,6 +1,10 @@ #ifndef KIT_ALLOCATOR_H #define KIT_ALLOCATOR_H +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + #include <stddef.h> #ifdef __cplusplus diff --git a/source/kit/array_ref.h b/source/kit/array_ref.h index 39f1e8b..c893b49 100644 --- a/source/kit/array_ref.h +++ b/source/kit/array_ref.h @@ -1,6 +1,10 @@ #ifndef KIT_ARRAY_REF_H #define KIT_ARRAY_REF_H +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + #include <stddef.h> #include <stdint.h> @@ -21,15 +25,15 @@ int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, void const *right_data, kit_ar_compare_fn compare); #define KIT_AR_MUT(type_) \ - struct { \ - ptrdiff_t size; \ - type_ *values; \ + struct { \ + ptrdiff_t size; \ + type_ *values; \ } -#define KIT_AR(type_) \ - struct { \ - ptrdiff_t size; \ - type_ const *values; \ +#define KIT_AR(type_) \ + struct { \ + ptrdiff_t size; \ + type_ const *values; \ } #define KIT_AR_MUT_WRAP(name_, element_type_, array_) \ diff --git a/source/kit/async_function.h b/source/kit/async_function.h index 45e1089..f7a04e7 100644 --- a/source/kit/async_function.h +++ b/source/kit/async_function.h @@ -1,6 +1,10 @@ #ifndef KIT_ASYNC_FUNCTION_H #define KIT_ASYNC_FUNCTION_H +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + #include <stddef.h> #include <string.h> diff --git a/source/kit/atomic.h b/source/kit/atomic.h index 23962a2..4d56def 100644 --- a/source/kit/atomic.h +++ b/source/kit/atomic.h @@ -1,8 +1,8 @@ #ifndef KIT_ATOMIC_H #define KIT_ATOMIC_H -#ifdef __cplusplus -extern "C" { +#ifndef _GNU_SOURCE +# define _GNU_SOURCE #endif #include <stdint.h> @@ -16,6 +16,10 @@ extern "C" { # define KIT_ATOMIC(type_) type_ volatile +# ifdef __cplusplus +extern "C" { +# endif + enum { memory_order_relaxed, memory_order_consume, diff --git a/source/kit/bigint.h b/source/kit/bigint.h index b302c22..c948b57 100644 --- a/source/kit/bigint.h +++ b/source/kit/bigint.h @@ -2,6 +2,7 @@ #define KIT_BIGINT_H #include "string_ref.h" + #include <assert.h> #include <string.h> diff --git a/source/kit/input_buffer.c b/source/kit/input_buffer.c index 1ea9e94..c59031f 100644 --- a/source/kit/input_buffer.c +++ b/source/kit/input_buffer.c @@ -67,16 +67,12 @@ static void buf_adjust(void *p, ptrdiff_t size) { } } -static ptrdiff_t min(ptrdiff_t a, ptrdiff_t b) { - if (a < b) - return a; - return b; -} - static ptrdiff_t buf_read(void *p, ptrdiff_t offset, kit_out_str_t destination) { internal_buffer_t *buf = (internal_buffer_t *) p; - ptrdiff_t n = min(destination.size, buf->data.size - offset); + ptrdiff_t n = destination.size < buf->data.size - offset + ? destination.size + : buf->data.size - offset; memcpy(destination.values, buf->data.values + offset, n); return n; } diff --git a/source/kit/input_stream.c b/source/kit/input_stream.c index df31c1d..7d8476b 100644 --- a/source/kit/input_stream.c +++ b/source/kit/input_stream.c @@ -20,17 +20,13 @@ static int check_type(void *state, ptrdiff_t type) { return basic != NULL && basic->type == type; } -static ptrdiff_t min(ptrdiff_t a, ptrdiff_t b) { - if (a < b) - return a; - return b; -} - static ptrdiff_t read_str(void *state, kit_out_str_t destination) { if (!check_type(state, input_stream_str)) return 0; kit_is_state_str_t *str = (kit_is_state_str_t *) state; - ptrdiff_t size = min(destination.size, str->string.size); + ptrdiff_t size = destination.size < str->string.size + ? destination.size + : str->string.size; memcpy(destination.values, str->string.values, size); str->string.values += size; str->string.size -= size; diff --git a/source/kit/mersenne_twister_64.h b/source/kit/mersenne_twister_64.h index f351194..548aea4 100644 --- a/source/kit/mersenne_twister_64.h +++ b/source/kit/mersenne_twister_64.h @@ -1,6 +1,10 @@ #ifndef KIT_MERSENNE_TWISTER_64_H #define KIT_MERSENNE_TWISTER_64_H +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + #include <stddef.h> #include <stdint.h> diff --git a/source/kit/move_back.h b/source/kit/move_back.h index 6eee4cf..0927321 100644 --- a/source/kit/move_back.h +++ b/source/kit/move_back.h @@ -1,6 +1,10 @@ #ifndef KIT_MOVE_BACK_H #define KIT_MOVE_BACK_H +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + #include <string.h> #ifdef __cplusplus diff --git a/source/kit/mutex.h b/source/kit/mutex.h index 7816301..919d92a 100644 --- a/source/kit/mutex.h +++ b/source/kit/mutex.h @@ -2,9 +2,12 @@ #define KIT_MUTEX_H #ifndef KIT_DISABLE_SYSTEM_THREADS +# ifndef _GNU_SOURCE +# define _GNU_SOURCE +# endif + # include "thread_defs.h" -# define _GNU_SOURCE # include <time.h> # if !defined(_WIN32) || defined(__CYGWIN__) diff --git a/source/kit/secure_random.h b/source/kit/secure_random.h index e7a80c2..21399b7 100644 --- a/source/kit/secure_random.h +++ b/source/kit/secure_random.h @@ -1,9 +1,12 @@ #ifndef KIT_SECURE_RANDOM_H #define KIT_SECURE_RANDOM_H +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + #include "status.h" -#define _GNU_SOURCE #include <stddef.h> #include <stdint.h> diff --git a/source/kit/sha256.h b/source/kit/sha256.h index 806bff3..ce74ed9 100644 --- a/source/kit/sha256.h +++ b/source/kit/sha256.h @@ -1,6 +1,10 @@ #ifndef KIT_SHA256_H #define KIT_SHA256_H +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + #include <stddef.h> #include <stdint.h> diff --git a/source/kit/sockets.h b/source/kit/sockets.h index 8afcee6..87e37e6 100644 --- a/source/kit/sockets.h +++ b/source/kit/sockets.h @@ -1,6 +1,10 @@ #ifndef KIT_SOCKETS_H #define KIT_SOCKETS_H +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + #include "status.h" #ifndef KIT_DISABLE_SYSTEM_SOCKETS diff --git a/source/kit/status.h b/source/kit/status.h index 0e23151..e689c5f 100644 --- a/source/kit/status.h +++ b/source/kit/status.h @@ -18,7 +18,7 @@ enum { KIT_ERROR_SOCKET_CONTROL_FAILED }; -typedef int kit_status_t; +typedef signed int kit_status_t; #ifdef __cplusplus } diff --git a/source/kit/string_ref.h b/source/kit/string_ref.h index a5ad5c1..8cf1970 100644 --- a/source/kit/string_ref.h +++ b/source/kit/string_ref.h @@ -2,6 +2,7 @@ #define KIT_STRING_REF_H #include "array_ref.h" + #include <string.h> #ifdef __cplusplus diff --git a/source/kit/thread.h b/source/kit/thread.h index e8fb7e8..92e6a5d 100644 --- a/source/kit/thread.h +++ b/source/kit/thread.h @@ -2,9 +2,12 @@ #define KIT_THREAD_H #ifndef KIT_DISABLE_SYSTEM_THREADS +# ifndef _GNU_SOURCE +# define _GNU_SOURCE +# endif + # include "thread_defs.h" -# define _GNU_SOURCE # include <stddef.h> # include <time.h> diff --git a/source/kit/types.h b/source/kit/types.h new file mode 100644 index 0000000..0251b75 --- /dev/null +++ b/source/kit/types.h @@ -0,0 +1,17 @@ +#ifndef KIT_TYPES_H +#define KIT_TYPES_H + +typedef signed char i8; +typedef signed short i16; +typedef signed int i32; +typedef signed long long i64; + +typedef unsigned char u8; +typedef unsigned short u16; +typedef unsigned int u32; +typedef unsigned long long u64; + +typedef float f32; +typedef double f64; + +#endif diff --git a/source/kit_test/CMakeLists.txt b/source/kit_test/CMakeLists.txt deleted file mode 100644 index 56aa9a4..0000000 --- a/source/kit_test/CMakeLists.txt +++ /dev/null @@ -1,7 +0,0 @@ -target_sources( - kit_test - PUBLIC - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/bench.h> - $<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/test.h> - PRIVATE - test.c bench.c) diff --git a/source/kit_test/_static.c b/source/kit_test/_static.c new file mode 100644 index 0000000..036b397 --- /dev/null +++ b/source/kit_test/_static.c @@ -0,0 +1,3 @@ +#include "test.c" + +#include "bench.c" diff --git a/source/kit_test/bench.c b/source/kit_test/bench.c index 9d439c3..dcf7249 100644 --- a/source/kit_test/bench.c +++ b/source/kit_test/bench.c @@ -1,12 +1,6 @@ #include "bench.h" -#define _GNU_SOURCE -#include <setjmp.h> -#include <signal.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> +#include "shared.inl.h" kit_benchs_list_t kit_benchs_list = { 0 }; @@ -57,17 +51,6 @@ static void bench_end(int i) { kit_benchs_list.v[i].duration_nsec[n] = sec * 1000000000 + nsec; } -enum { white, blue, light, yellow, red, green }; - -static char const *const color_codes[] = { - [white] = "\x1b[38m", [blue] = "\x1b[34m", [light] = "\x1b[37m", - [yellow] = "\x1b[33m", [red] = "\x1b[31m", [green] = "\x1b[32m" -}; - -static int print_color(int c) { - return printf("%s", color_codes[c]); -} - void kit_bench_register(char const *name, char const *file, kit_bench_run_fn fn) { int n = kit_benchs_list.size++; @@ -84,23 +67,11 @@ void kit_bench_register(char const *name, char const *file, static jmp_buf kit_bench_restore_execution; -static int const signums[] = { SIGINT, SIGILL, SIGABRT, - SIGFPE, SIGSEGV, SIGTERM }; - -static char const *const signames[] = { - [SIGINT] = "Interactive attention signal", - [SIGILL] = "Illegal instruction", - [SIGABRT] = "Abnormal termination", - [SIGFPE] = "Erroneous arithmetic operation", - [SIGSEGV] = "Invalid access to storage", - [SIGTERM] = "Termination request" -}; - -static void handle_signal(int signum) { +static void kit_bench_handle_signal(int signum) { longjmp(kit_bench_restore_execution, signum); } -static void setup_signals() { +static void kit_bench_setup_signals() { int i; for (i = 0; i < sizeof signums / sizeof *signums; i++) { @@ -110,7 +81,7 @@ static void setup_signals() { #else struct sigaction action; memset(&action, 0, sizeof action); - action.sa_handler = handle_signal; + action.sa_handler = kit_bench_handle_signal; sigaction(signums[i], &action, NULL); #endif @@ -151,7 +122,7 @@ int kit_run_benchmarks(int argc, char **argv) { char const *specific_bench = NULL; - setup_signals(); + kit_bench_setup_signals(); for (int i = 0; i < argc; i++) if (strcmp("--no-term-color", argv[i]) == 0) diff --git a/source/kit_test/bench.h b/source/kit_test/bench.h index f3d71b8..dd86c17 100644 --- a/source/kit_test/bench.h +++ b/source/kit_test/bench.h @@ -6,6 +6,7 @@ extern "C" { #endif #include "test.h" + #include <stdint.h> #ifndef KIT_TEST_FILE diff --git a/source/kit_test/shared.inl.h b/source/kit_test/shared.inl.h new file mode 100644 index 0000000..e31c9f3 --- /dev/null +++ b/source/kit_test/shared.inl.h @@ -0,0 +1,38 @@ +#ifndef KIT_TEST_SHARED_INL_H +#define KIT_TEST_SHARED_INL_H + +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + +#include <setjmp.h> +#include <signal.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> + +enum { white, blue, light, yellow, red, green }; + +static char const *const color_codes[] = { + [white] = "\x1b[38m", [blue] = "\x1b[34m", [light] = "\x1b[37m", + [yellow] = "\x1b[33m", [red] = "\x1b[31m", [green] = "\x1b[32m" +}; + +static int print_color(int c) { + return printf("%s", color_codes[c]); +} + +static int const signums[] = { SIGINT, SIGILL, SIGABRT, + SIGFPE, SIGSEGV, SIGTERM }; + +static char const *const signames[] = { + [SIGINT] = "Interactive attention signal", + [SIGILL] = "Illegal instruction", + [SIGABRT] = "Abnormal termination", + [SIGFPE] = "Erroneous arithmetic operation", + [SIGSEGV] = "Invalid access to storage", + [SIGTERM] = "Termination request" +}; + +#endif diff --git a/source/kit_test/test.c b/source/kit_test/test.c index d1e048c..034d1e7 100644 --- a/source/kit_test/test.c +++ b/source/kit_test/test.c @@ -1,11 +1,6 @@ #include "test.h" -#define _GNU_SOURCE -#include <setjmp.h> -#include <signal.h> -#include <stdio.h> -#include <string.h> -#include <time.h> +#include "shared.inl.h" kit_tests_list_t kit_tests_list = { 0 }; @@ -30,17 +25,6 @@ static long long sec_to_ms(long long sec) { return 1000 * sec; } -enum { white, blue, light, yellow, red, green }; - -static char const *const color_codes[] = { - [white] = "\x1b[38m", [blue] = "\x1b[34m", [light] = "\x1b[37m", - [yellow] = "\x1b[33m", [red] = "\x1b[31m", [green] = "\x1b[32m" -}; - -static int print_color(int c) { - return printf("%s", color_codes[c]); -} - void kit_test_register(char const *name, char const *file, kit_test_run_fn fn) { int n = kit_tests_list.size++; @@ -54,23 +38,11 @@ void kit_test_register(char const *name, char const *file, static jmp_buf kit_test_restore_execution; -static int const signums[] = { SIGINT, SIGILL, SIGABRT, - SIGFPE, SIGSEGV, SIGTERM }; - -static char const *const signames[] = { - [SIGINT] = "Interactive attention signal", - [SIGILL] = "Illegal instruction", - [SIGABRT] = "Abnormal termination", - [SIGFPE] = "Erroneous arithmetic operation", - [SIGSEGV] = "Invalid access to storage", - [SIGTERM] = "Termination request" -}; - -static void handle_signal(int signum) { +static void kit_test_handle_signal(int signum) { longjmp(kit_test_restore_execution, signum); } -static void setup_signals() { +static void kit_test_setup_signals() { int i; for (i = 0; i < sizeof signums / sizeof *signums; i++) { @@ -80,7 +52,7 @@ static void setup_signals() { #else struct sigaction action; memset(&action, 0, sizeof action); - action.sa_handler = handle_signal; + action.sa_handler = kit_test_handle_signal; sigaction(signums[i], &action, NULL); #endif @@ -113,7 +85,7 @@ int kit_run_tests(int argc, char **argv) { char const *specific_test = NULL; - setup_signals(); + kit_test_setup_signals(); for (i = 0; i < argc; i++) if (strcmp("--no-term-color", argv[i]) == 0) diff --git a/source/kit_test/test.h b/source/kit_test/test.h index 8732366..6584cbe 100644 --- a/source/kit_test/test.h +++ b/source/kit_test/test.h @@ -5,6 +5,10 @@ extern "C" { #endif +#ifndef _GNU_SOURCE +# define _GNU_SOURCE +#endif + #include <stddef.h> #ifndef KIT_TEST_FILE diff --git a/source/test/CMakeLists.txt b/source/test/CMakeLists.txt deleted file mode 100644 index 4435be1..0000000 --- a/source/test/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -add_subdirectory(unittests) -add_subdirectory(programs) diff --git a/source/test/programs/CMakeLists.txt b/source/test/programs/CMakeLists.txt deleted file mode 100644 index 81332cf..0000000 --- a/source/test/programs/CMakeLists.txt +++ /dev/null @@ -1,49 +0,0 @@ -project( - ${KIT_PROJECT}_test_programs - VERSION 0.1.1 - DESCRIPTION "Kit test programs" - LANGUAGES C CXX) - -if(KIT_ENABLE_TESTING) - add_executable(too_many_tests too_many_tests.c) - target_link_libraries(too_many_tests kit_test) - add_test( - NAME too_many_tests_test - COMMAND too_many_tests --quiet) - set_tests_properties( - too_many_tests_test - PROPERTIES - TIMEOUT "15") - - add_executable(too_many_assertions too_many_assertions.c) - target_link_libraries(too_many_assertions kit_test) - add_test( - NAME too_many_assertions_test - COMMAND too_many_assertions --quiet) - set_tests_properties( - too_many_assertions_test - PROPERTIES - TIMEOUT "15") - - add_executable(cpp_example cpp_example.cpp) - target_link_libraries(cpp_example kit_test) - add_test( - NAME cpp_example_test - COMMAND cpp_example --quiet) - set_tests_properties( - cpp_example_test - PROPERTIES - TIMEOUT "15") - - if(NOT MSVC) - add_executable(signals signals.cpp) - target_link_libraries(signals kit_test) - add_test( - NAME signals_test - COMMAND signals --no-carriage-return) - set_tests_properties( - signals_test - PROPERTIES - TIMEOUT "15") - endif() -endif() diff --git a/source/test/unittests/CMakeLists.txt b/source/test/unittests/CMakeLists.txt deleted file mode 100644 index e071ed4..0000000 --- a/source/test/unittests/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -target_sources( - kit_test_suite - PRIVATE - async_function.test.c bigint.test.c mutex.test.c test_duration.test.c - main.test.c string_ref.test.c atomic.test.c foo.bench.c thread.test.c - array_ref.test.c input_stream.test.c sha256.test.c lower_bound.test.c - secure_random.test.c condition_variable.test.c mersenne_twister_64.test.c - input_buffer.test.c move_back.test.c dynamic_array.test.c file.test.c) diff --git a/source/tests/_static.c b/source/tests/_static.c new file mode 100644 index 0000000..4b7543d --- /dev/null +++ b/source/tests/_static.c @@ -0,0 +1,55 @@ +#include "array_ref.test.c" +#undef KIT_TEST_FILE + +#include "async_function.test.c" +#undef KIT_TEST_FILE + +#include "bigint.test.c" +#undef KIT_TEST_FILE + +#include "condition_variable.test.c" +#undef KIT_TEST_FILE + +#include "dynamic_array.test.c" +#undef KIT_TEST_FILE + +#include "file.test.c" +#undef KIT_TEST_FILE + +#include "input_buffer.test.c" +#undef KIT_TEST_FILE + +#include "input_stream.test.c" +#undef KIT_TEST_FILE + +#include "lower_bound.test.c" +#undef KIT_TEST_FILE + +#include "mersenne_twister_64.test.c" +#undef KIT_TEST_FILE + +#include "move_back.test.c" +#undef KIT_TEST_FILE + +#include "mutex.test.c" +#undef KIT_TEST_FILE + +#include "secure_random.test.c" +#undef KIT_TEST_FILE + +#include "sha256.test.c" +#undef KIT_TEST_FILE + +#include "string_ref.test.c" +#undef KIT_TEST_FILE + +#include "test_duration.test.c" +#undef KIT_TEST_FILE + +#include "thread.test.c" +#undef KIT_TEST_FILE + +#include "bench.test.c" +#undef KIT_TEST_FILE + +#include "main.test.c" diff --git a/source/test/unittests/array_ref.test.c b/source/tests/array_ref.test.c index c6083ee..da20aa0 100644 --- a/source/test/unittests/array_ref.test.c +++ b/source/tests/array_ref.test.c @@ -1,7 +1,7 @@ -#include "../../kit/array_ref.h" +#include "../kit/array_ref.h" #define KIT_TEST_FILE array_ref -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("array ref const wrap") { int foo[] = { 1, 2, 3 }; diff --git a/source/test/unittests/async_function.test.c b/source/tests/async_function.test.c index 32aff4e..35ce0ce 100644 --- a/source/test/unittests/async_function.test.c +++ b/source/tests/async_function.test.c @@ -1,7 +1,7 @@ -#include "../../kit/async_function.h" +#include "../kit/async_function.h" #define KIT_TEST_FILE async_function -#include "../../kit_test/test.h" +#include "../kit_test/test.h" AF_STATE(int, test_foo, ); static AF_DECL(test_foo); diff --git a/source/test/unittests/atomic.test.c b/source/tests/atomic.test.c index 7d8b35a..c1831a3 100644 --- a/source/test/unittests/atomic.test.c +++ b/source/tests/atomic.test.c @@ -1,8 +1,8 @@ -#include "../../kit/atomic.h" -#include "../../kit/thread.h" +#include "../kit/atomic.h" +#include "../kit/thread.h" #define KIT_TEST_FILE atomic -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("atomic store and load") { ATOMIC(int) value; diff --git a/source/test/unittests/foo.bench.c b/source/tests/bench.test.c index ac1e0b5..92e216d 100644 --- a/source/test/unittests/foo.bench.c +++ b/source/tests/bench.test.c @@ -1,5 +1,5 @@ -#define KIT_TEST_FILE foo -#include "../../kit_test/bench.h" +#define KIT_TEST_FILE bench +#include "../kit_test/bench.h" struct test_foo_ { double f; diff --git a/source/test/unittests/bigint.test.c b/source/tests/bigint.test.c index 260b0ec..c8ac44f 100644 --- a/source/test/unittests/bigint.test.c +++ b/source/tests/bigint.test.c @@ -1,8 +1,8 @@ #define KIT_BIGINT_SIZE 256 -#include "../../kit/bigint.h" +#include "../kit/bigint.h" #define KIT_TEST_FILE bigint -#include "../../kit_test/test.h" +#include "../kit_test/test.h" #if __STDC_VERSION__ >= 199901L static_assert(sizeof(bigint_t) == 256, "KIT_BIGINT_SIZE check"); diff --git a/source/test/unittests/condition_variable.test.c b/source/tests/condition_variable.test.c index 2dba7ca..dfbe4a6 100644 --- a/source/test/unittests/condition_variable.test.c +++ b/source/tests/condition_variable.test.c @@ -1,8 +1,8 @@ -#include "../../kit/condition_variable.h" -#include "../../kit/thread.h" +#include "../kit/condition_variable.h" +#include "../kit/thread.h" #define KIT_TEST_FILE condition_variable -#include "../../kit_test/test.h" +#include "../kit_test/test.h" typedef struct { mtx_t m; @@ -11,10 +11,10 @@ typedef struct { cnd_t send; cnd_t receive; int value; -} test_data_t; +} cnd_test_data_t; -static int test_run(void *p) { - test_data_t *data = (test_data_t *) p; +static int cnd_test_run(void *p) { + cnd_test_data_t *data = (cnd_test_data_t *) p; mtx_lock(&data->m); data->value = 20; @@ -41,7 +41,7 @@ TEST("condition variable") { int ok = 1; for (i = 0; i < 10; i++) { - test_data_t data; + cnd_test_data_t data; data.in = 0; data.out = 0; @@ -52,7 +52,7 @@ TEST("condition variable") { ok = ok && (cnd_init(&data.receive) == thrd_success); thrd_t t; - ok = ok && (thrd_create(&t, test_run, &data) == thrd_success); + ok = ok && (thrd_create(&t, cnd_test_run, &data) == thrd_success); ok = ok && (mtx_lock(&data.m) == thrd_success); if (data.out == 0) diff --git a/source/test/programs/cpp_example.cpp b/source/tests/cpp.cpp index 523186b..8b762e4 100644 --- a/source/test/programs/cpp_example.cpp +++ b/source/tests/cpp.cpp @@ -1,4 +1,4 @@ -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("foo") { REQUIRE(20 + 22 == 42); diff --git a/source/test/unittests/dynamic_array.test.c b/source/tests/dynamic_array.test.c index fad8d6d..8701e6c 100644 --- a/source/test/unittests/dynamic_array.test.c +++ b/source/tests/dynamic_array.test.c @@ -1,7 +1,7 @@ -#include "../../kit/dynamic_array.h" +#include "../kit/dynamic_array.h" #define KIT_TEST_FILE dynamic_array -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("dynamic array empty") { DA_CREATE(v, char, 0); diff --git a/source/test/unittests/file.test.c b/source/tests/file.test.c index 732f122..e72df83 100644 --- a/source/test/unittests/file.test.c +++ b/source/tests/file.test.c @@ -1,9 +1,9 @@ -#include "../../kit/file.h" -#include "../../kit/string_ref.h" +#include "../kit/file.h" +#include "../kit/string_ref.h" #include <string.h> #define KIT_TEST_FILE file -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("file path cache") { kit_allocator_t alloc = kit_alloc_default(); diff --git a/source/test/unittests/input_buffer.test.c b/source/tests/input_buffer.test.c index c062052..3cd84b6 100644 --- a/source/test/unittests/input_buffer.test.c +++ b/source/tests/input_buffer.test.c @@ -1,7 +1,7 @@ -#include "../../kit/input_buffer.h" +#include "../kit/input_buffer.h" #define KIT_TEST_FILE input_buffer -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("input buffer read once") { str_t text = { .size = 3, .values = "foo" }; diff --git a/source/test/unittests/input_stream.test.c b/source/tests/input_stream.test.c index 7dce043..61c2254 100644 --- a/source/test/unittests/input_stream.test.c +++ b/source/tests/input_stream.test.c @@ -1,7 +1,7 @@ -#include "../../kit/input_stream.h" +#include "../kit/input_stream.h" #define KIT_TEST_FILE input_stream -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("input stream wrap string") { char foo[] = "test"; diff --git a/source/test/unittests/lower_bound.test.c b/source/tests/lower_bound.test.c index 24752a6..3b62325 100644 --- a/source/test/unittests/lower_bound.test.c +++ b/source/tests/lower_bound.test.c @@ -1,8 +1,8 @@ -#include "../../kit/lower_bound.h" -#include "../../kit/array_ref.h" +#include "../kit/lower_bound.h" +#include "../kit/array_ref.h" #define KIT_TEST_FILE lower_bound -#include "../../kit_test/test.h" +#include "../kit_test/test.h" static int kit_less_int(int left, int right) { return left < right; diff --git a/source/test/unittests/main.test.c b/source/tests/main.test.c index 60c2e36..2a41d4b 100644 --- a/source/test/unittests/main.test.c +++ b/source/tests/main.test.c @@ -1,5 +1,5 @@ -#include "../../kit_test/bench.h" -#include "../../kit_test/test.h" +#include "../kit_test/bench.h" +#include "../kit_test/test.h" int main(int argc, char **argv) { int status = run_tests(argc, argv); diff --git a/source/test/unittests/mersenne_twister_64.test.c b/source/tests/mersenne_twister_64.test.c index e523154..6bf5b9c 100644 --- a/source/test/unittests/mersenne_twister_64.test.c +++ b/source/tests/mersenne_twister_64.test.c @@ -1,8 +1,8 @@ -#include "../../kit/mersenne_twister_64.h" -#include "../../kit/secure_random.h" +#include "../kit/mersenne_twister_64.h" +#include "../kit/secure_random.h" #define KIT_TEST_FILE mersenne_twister_64 -#include "../../kit_test/test.h" +#include "../kit_test/test.h" enum { SIZE = 1000 }; diff --git a/source/test/unittests/move_back.test.c b/source/tests/move_back.test.c index 399619d..f08d190 100644 --- a/source/test/unittests/move_back.test.c +++ b/source/tests/move_back.test.c @@ -1,7 +1,7 @@ -#include "../../kit/move_back.h" +#include "../kit/move_back.h" #define KIT_TEST_FILE move_back -#include "../../kit_test/test.h" +#include "../kit_test/test.h" static int is_equal(int const x, int const y) { return x == y; diff --git a/source/test/unittests/mutex.test.c b/source/tests/mutex.test.c index 1bd2b3d..ce26991 100644 --- a/source/test/unittests/mutex.test.c +++ b/source/tests/mutex.test.c @@ -1,19 +1,19 @@ -#include "../../kit/mutex.h" -#include "../../kit/thread.h" +#include "../kit/mutex.h" +#include "../kit/thread.h" #define KIT_TEST_FILE mutex -#include "../../kit_test/test.h" +#include "../kit_test/test.h" enum { SLEEP = 400000000, TICK_COUNT = 200, THREAD_COUNT = 100 }; typedef struct { mtx_t lock; int value; -} test_data_t; +} mtx_test_data_t; -static int test_run(void *data) { - int i; - test_data_t *x = (test_data_t *) data; +static int mtx_test_run(void *data) { + int i; + mtx_test_data_t *x = (mtx_test_data_t *) data; for (i = 0; i < TICK_COUNT; i++) { mtx_lock(&x->lock); @@ -33,13 +33,13 @@ static int test_run(void *data) { TEST("mutex lock") { ptrdiff_t i; - test_data_t data; - thrd_t pool[THREAD_COUNT]; + mtx_test_data_t data; + thrd_t pool[THREAD_COUNT]; data.value = 42; REQUIRE(mtx_init(&data.lock, mtx_plain) == thrd_success); for (i = 0; i < THREAD_COUNT; i++) - thrd_create(pool + i, test_run, &data); + thrd_create(pool + i, mtx_test_run, &data); for (i = 0; i < THREAD_COUNT; i++) thrd_join(pool[i], NULL); mtx_destroy(&data.lock); diff --git a/source/test/unittests/secure_random.test.c b/source/tests/secure_random.test.c index bde68a5..22bdd37 100644 --- a/source/test/unittests/secure_random.test.c +++ b/source/tests/secure_random.test.c @@ -1,8 +1,8 @@ -#include "../../kit/secure_random.h" +#include "../kit/secure_random.h" #include <string.h> #define KIT_TEST_FILE secure_random -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("secure random") { int i, j; diff --git a/source/test/unittests/sha256.test.c b/source/tests/sha256.test.c index e74b286..0f751df 100644 --- a/source/test/unittests/sha256.test.c +++ b/source/tests/sha256.test.c @@ -1,8 +1,8 @@ -#include "../../kit/sha256.h" -#include "../../kit/array_ref.h" +#include "../kit/sha256.h" +#include "../kit/array_ref.h" #define KIT_TEST_FILE sha256_64 -#include "../../kit_test/test.h" +#include "../kit_test/test.h" #include <string.h> diff --git a/source/test/programs/signals.cpp b/source/tests/signals.cpp index e2532c2..0f6d77a 100644 --- a/source/test/programs/signals.cpp +++ b/source/tests/signals.cpp @@ -1,4 +1,4 @@ -#include "../../kit_test/test.h" +#include "../kit_test/test.h" #include <cstdlib> #include <stdexcept> diff --git a/source/test/unittests/string_ref.test.c b/source/tests/string_ref.test.c index 5ec9414..2891f16 100644 --- a/source/test/unittests/string_ref.test.c +++ b/source/tests/string_ref.test.c @@ -1,7 +1,7 @@ -#include "../../kit/string_ref.h" +#include "../kit/string_ref.h" #define KIT_TEST_FILE string_ref -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("static string wrap") { str_t ref = SZ("foo bar"); diff --git a/source/test/unittests/test_duration.test.c b/source/tests/test_duration.test.c index 0912d9f..27384b4 100644 --- a/source/test/unittests/test_duration.test.c +++ b/source/tests/test_duration.test.c @@ -1,7 +1,5 @@ #define KIT_TEST_FILE test_duration -#include "../../kit_test/test.h" - -#include <stdlib.h> +#include "../kit_test/test.h" #if defined(_WIN32) && !defined(__CYGWIN__) __declspec(dllimport) void __stdcall Sleep(unsigned long timeout); @@ -9,6 +7,7 @@ static void kit_sleep(int ms) { Sleep(ms); } #else +# include <stdlib.h> # include <unistd.h> static void kit_sleep(int ms) { usleep(ms * 1000); diff --git a/source/test/unittests/thread.test.c b/source/tests/thread.test.c index 01198c2..50f7c04 100644 --- a/source/test/unittests/thread.test.c +++ b/source/tests/thread.test.c @@ -1,7 +1,7 @@ -#include "../../kit/thread.h" +#include "../kit/thread.h" #define KIT_TEST_FILE thread -#include "../../kit_test/test.h" +#include "../kit_test/test.h" static int test_nothing(void *_) { return 0; diff --git a/source/test/programs/too_many_assertions.c b/source/tests/too_many_assertions.c index 06b440b..662207d 100644 --- a/source/test/programs/too_many_assertions.c +++ b/source/tests/too_many_assertions.c @@ -1,4 +1,4 @@ -#include "../../kit_test/test.h" +#include "../kit_test/test.h" TEST("foo") { int i; diff --git a/source/test/programs/too_many_tests.c b/source/tests/too_many_tests.c index ac1cbf7..d4842e4 100644 --- a/source/test/programs/too_many_tests.c +++ b/source/tests/too_many_tests.c @@ -1,4 +1,4 @@ -#include "../../kit_test/test.h" +#include "../kit_test/test.h" void bar(int index, kit_test_report_fn report) { } |