From 7e6b36bc2d332a3cb66b83116a393ac4728236d5 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov Date: Thu, 29 Dec 2022 07:51:16 +0100 Subject: Improve C89/90 compat --- CMakeLists.txt | 115 ++++++++++++++++------- config.cmake.in | 2 +- gen_cmake.py | 6 +- source/kit/CMakeLists.txt | 2 +- source/kit/array_ref.c | 6 +- source/kit/async_function.h | 27 +++--- source/kit/bigint.h | 87 ++++++++++------- source/kit/dynamic_array.h | 16 ++-- source/kit/file.c | 19 ++-- source/kit/mersenne_twister_64.c | 4 +- source/kit/move_back.h | 3 +- source/kit/secure_random.c | 5 +- source/kit/thread.posix.c | 10 ++ source/kit/time.c | 16 +++- source/kit/time.h | 4 + source/kit_test/CMakeLists.txt | 2 +- source/kit_test/test.c | 30 +++--- source/test/programs/CMakeLists.txt | 8 +- source/test/programs/too_many_assertions.c | 3 +- source/test/programs/too_many_tests.c | 3 +- source/test/unittests/CMakeLists.txt | 2 +- source/test/unittests/async_function.test.c | 9 +- source/test/unittests/atomic.test.c | 51 ++++++---- source/test/unittests/bigint.test.c | 3 +- source/test/unittests/condition_variable.test.c | 3 +- source/test/unittests/mersenne_twister_64.test.c | 9 +- source/test/unittests/mutex.test.c | 10 +- source/test/unittests/secure_random.test.c | 6 +- 28 files changed, 304 insertions(+), 157 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index be0471c..f24fe6f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,26 +11,24 @@ option(KIT_ENABLE_TESTING "Enable testing" ON) option(KIT_DISABLE_SYSTEM_MALLOC "Disable system memory allocator" OFF) option(KIT_DISABLE_SYSTEM_THREADS "Disable system threads" OFF) -set(KIT_PROJECT kit) -set(KIT_LIBRARY kit) -set(KIT_TEST_LIBRARY kit_test) -set(KIT_TEST_SUITE kit_test_suite) -set(KIT_TARGETS kit-targets) +if(KIT_ENABLE_TESTING) + set(CMAKE_C_STANDARD 90) + set(CMAKE_C_EXTENSIONS OFF) +endif() project( - ${KIT_PROJECT} + kit VERSION 0.1.1 DESCRIPTION "A collection of C libraries" LANGUAGES C) -add_library(${KIT_LIBRARY} STATIC) -add_library(${KIT_PROJECT}::${KIT_LIBRARY} ALIAS ${KIT_LIBRARY}) +add_library(kit STATIC) +add_library(kit::kit ALIAS kit) target_include_directories( - ${KIT_LIBRARY} INTERFACE + kit INTERFACE $) -target_compile_features(${KIT_LIBRARY} PUBLIC c_std_11) if(WIN32) - target_link_libraries(${KIT_LIBRARY} PUBLIC Shlwapi) + target_link_libraries(kit PUBLIC Shlwapi) endif() include(CheckCSourceRuns) @@ -40,12 +38,24 @@ check_c_source_runs( #include int main() { struct timespec t; + (void) t.tv_sec; + (void) t.tv_nsec; return 0; } " KIT_HAVE_STRUCT_TIMESPEC ) +check_c_source_runs( + " + #include + int main() { + return 0; + } + " + KIT_HAVE_BITS_TYPES_STRUCT_TIMESPEC_H +) + check_c_source_runs( " #include @@ -58,6 +68,31 @@ check_c_source_runs( KIT_HAVE_TIMESPEC_GET ) +check_c_source_runs( + " + #include + int main() { + struct timespec t; + clock_gettime(CLOCK_REALTIME, &t) + return 0; + } + " + KIT_HAVE_CLOCK_GETTIME +) + +check_c_source_runs( + " + #include + int main() { + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + return 0; + } + " + KIT_HAVE_PTHREAD_MUTEXATTR_SETTYPE +) + check_c_source_runs( " #include @@ -99,57 +134,67 @@ if(NOT KIT_HAVE_MALLOC) endif() if(NOT KIT_HAVE_STRUCT_TIMESPEC) - target_compile_definitions(${KIT_LIBRARY} PUBLIC KIT_NEED_STRUCT_TIMESPEC) + target_compile_definitions(kit PUBLIC KIT_NEED_STRUCT_TIMESPEC) +endif() + +if(KIT_HAVE_BITS_TYPES_STRUCT_TIMESPEC_H) + target_compile_definitions(kit PUBLIC KIT_HAVE_BITS_TYPES_STRUCT_TIMESPEC_H) endif() if(NOT KIT_HAVE_TIMESPEC_GET) - target_compile_definitions(${KIT_LIBRARY} PUBLIC KIT_NEED_TIMESPEC_GET) + target_compile_definitions(kit PUBLIC KIT_NEED_TIMESPEC_GET) +endif() + +if(KIT_HAVE_CLOCK_GETTIME) + target_compile_definitions(kit PUBLIC KIT_HAVE_CLOCK_GETTIME) +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_LIBRARY} PUBLIC KIT_DISABLE_SYSTEM_MALLOC) + target_compile_definitions(kit PUBLIC KIT_DISABLE_SYSTEM_MALLOC) endif() if(KIT_DISABLE_SYSTEM_THREADS) - target_compile_definitions(${KIT_LIBRARY} PUBLIC KIT_DISABLE_SYSTEM_THREADS) + target_compile_definitions(kit PUBLIC KIT_DISABLE_SYSTEM_THREADS) endif() if(NOT KIT_DISABLE_SYSTEM_THREADS) find_package(Threads REQUIRED) - target_link_libraries(${KIT_LIBRARY} PUBLIC Threads::Threads) + target_link_libraries(kit PUBLIC Threads::Threads) endif() -add_library(${KIT_TEST_LIBRARY} STATIC) -add_library(${KIT_PROJECT}::${KIT_TEST_LIBRARY} ALIAS ${KIT_TEST_LIBRARY}) +add_library(kit_test STATIC) +add_library(kit::kit_test ALIAS kit_test) target_include_directories( - ${KIT_TEST_LIBRARY} INTERFACE + kit_test INTERFACE $) -target_compile_features(${KIT_TEST_LIBRARY} PUBLIC c_std_11) enable_testing() if(KIT_ENABLE_TESTING) - add_executable(${KIT_TEST_SUITE}) - add_executable(${KIT_PROJECT}::${KIT_TEST_SUITE} ALIAS ${KIT_TEST_SUITE}) - target_compile_features(${KIT_TEST_SUITE} PRIVATE c_std_11) - target_link_libraries(${KIT_TEST_SUITE} PRIVATE ${KIT_LIBRARY} ${KIT_TEST_LIBRARY} Threads::Threads) + 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 Threads::Threads) if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang") target_compile_options( - ${KIT_LIBRARY} PUBLIC + kit PUBLIC -fsanitize=undefined,address --coverage -O0 -g) target_link_options( - ${KIT_LIBRARY} PUBLIC + kit PUBLIC -fsanitize=undefined,address --coverage) endif() add_test( - NAME ${KIT_LIBRARY}_unittests - COMMAND ${KIT_TEST_SUITE} --no-carriage-return) + NAME kit_unittests + COMMAND kit_test_suite --no-carriage-return) set_tests_properties( - ${KIT_LIBRARY}_unittests + kit_unittests PROPERTIES TIMEOUT "120") endif() @@ -176,21 +221,21 @@ unset(path_) unset(dir_) install( - TARGETS ${KIT_LIBRARY} ${KIT_TEST_LIBRARY} - EXPORT ${KIT_TARGETS} + 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 + 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" + EXPORT kit-targets + FILE "${CMAKE_CURRENT_BINARY_DIR}/cmake/kit-targets.cmake" NAMESPACE ${PROJECT_NAME}::) include(CMakePackageConfigHelpers) diff --git a/config.cmake.in b/config.cmake.in index 2638feb..d2f3c05 100644 --- a/config.cmake.in +++ b/config.cmake.in @@ -5,4 +5,4 @@ if(NOT @KIT_DISABLE_SYSTEM_THREADS@) find_dependency(Threads) endif() -include("${CMAKE_CURRENT_LIST_DIR}/@KIT_TARGETS@.cmake") +include("${CMAKE_CURRENT_LIST_DIR}/kit-targets.cmake") diff --git a/gen_cmake.py b/gen_cmake.py index ec3a176..bded58a 100755 --- a/gen_cmake.py +++ b/gen_cmake.py @@ -81,9 +81,9 @@ def gen_cmake(folder: str, target_name: str): write_subdirs(folder, target_name) def main(): - gen_cmake(os.path.join('source', 'kit'), '${KIT_LIBRARY}') - gen_cmake(os.path.join('source', 'kit_test'), '${KIT_TEST_LIBRARY}') - gen_cmake(os.path.join('source', 'test', 'unittests'), '${KIT_TEST_SUITE}') + gen_cmake(os.path.join('source', 'kit'), 'kit') + gen_cmake(os.path.join('source', 'kit_test'), 'kit_test') + gen_cmake(os.path.join('source', 'test', 'unittests'), 'kit_test_suite') if __name__ == '__main__': main() diff --git a/source/kit/CMakeLists.txt b/source/kit/CMakeLists.txt index e12077a..bd4d5ea 100644 --- a/source/kit/CMakeLists.txt +++ b/source/kit/CMakeLists.txt @@ -1,5 +1,5 @@ target_sources( - ${KIT_LIBRARY} + kit PRIVATE input_buffer.c bigint.c status.c time.c secure_random.c thread.posix.c atomic.win32.c condition_variable.c thread.win32.c diff --git a/source/kit/array_ref.c b/source/kit/array_ref.c index f2a0988..dcc4ff5 100644 --- a/source/kit/array_ref.c +++ b/source/kit/array_ref.c @@ -6,11 +6,12 @@ int kit_ar_equal_bytes(ptrdiff_t left_element_size, ptrdiff_t left_size, void const *left_data, ptrdiff_t right_element_size, ptrdiff_t right_size, void const *right_data) { + ptrdiff_t i; if (left_element_size != right_element_size) return 0; if (left_size != right_size) return 0; - for (ptrdiff_t i = 0; i < left_size; i++) + for (i = 0; i < left_size; i++) if (memcmp((char const *) left_data + i * left_element_size, (char const *) right_data + i * left_element_size, left_element_size) != 0) @@ -23,11 +24,12 @@ int kit_ar_compare(ptrdiff_t left_element_size, ptrdiff_t left_size, ptrdiff_t right_element_size, ptrdiff_t right_size, void const *right_data, kit_ar_compare_fn compare) { + ptrdiff_t i; if (left_element_size < right_element_size) return -1; if (left_element_size > right_element_size) return 1; - for (ptrdiff_t i = 0; i < left_size && i < right_size; i++) { + for (i = 0; i < left_size && i < right_size; i++) { int const c = compare( (char const *) left_data + i * left_element_size, (char const *) right_data + i * left_element_size); diff --git a/source/kit/async_function.h b/source/kit/async_function.h index cf2de81..73e3fee 100644 --- a/source/kit/async_function.h +++ b/source/kit/async_function.h @@ -178,22 +178,24 @@ typedef struct { #define KIT_AF_RESUME(promise_) \ (promise_)._state_machine(&(promise_), kit_af_request_resume) -#define KIT_AF_RESUME_N(promises_, size_) \ - do { \ - for (int kit_af_index_ = 0; kit_af_index_ < (size_); \ - kit_af_index_++) \ - KIT_AF_RESUME((promises_)[kit_af_index_]); \ +#define KIT_AF_RESUME_N(promises_, size_) \ + do { \ + int kit_af_index_; \ + for (kit_af_index_ = 0; kit_af_index_ < (size_); \ + kit_af_index_++) \ + KIT_AF_RESUME((promises_)[kit_af_index_]); \ } while (0) #define KIT_AF_JOIN(promise_) \ ((promise_)._state_machine(&(promise_), kit_af_request_join), \ (promise_).return_value) -#define KIT_AF_JOIN_N(promises_, size_) \ - do { \ - for (int kit_af_index_ = 0; kit_af_index_ < (size_); \ - kit_af_index_++) \ - KIT_AF_JOIN((promises_)[kit_af_index_]); \ +#define KIT_AF_JOIN_N(promises_, size_) \ + do { \ + int kit_af_index_; \ + for (kit_af_index_ = 0; kit_af_index_ < (size_); \ + kit_af_index_++) \ + KIT_AF_JOIN((promises_)[kit_af_index_]); \ } while (0) #define KIT_AF_RESUME_AND_JOIN(promise_) \ @@ -223,8 +225,9 @@ typedef struct { #define KIT_AF_FINISHED_N(return_, promises_, size_) \ do { \ + int kit_af_index_; \ (return_) = 1; \ - for (int kit_af_index_ = 0; kit_af_index_ < (size_); \ + for (kit_af_index_ = 0; kit_af_index_ < (size_); \ kit_af_index_++) \ if (!KIT_AF_FINISHED((promises_)[kit_af_index_])) { \ (return_) = 0; \ @@ -238,10 +241,10 @@ typedef struct { #define KIT_AF_AWAIT_N(promises_, size_) \ do { \ + int kit_af_done_; \ case KIT_AF_LINE_(): \ self->_index = KIT_AF_LINE_(); \ KIT_AF_RESUME_AND_JOIN_N((promises_), (size_)); \ - int kit_af_done_; \ KIT_AF_FINISHED_N(kit_af_done_, (promises_), (size_)); \ if (!kit_af_done_) \ return; \ diff --git a/source/kit/bigint.h b/source/kit/bigint.h index 7a00cba..b302c22 100644 --- a/source/kit/bigint.h +++ b/source/kit/bigint.h @@ -13,6 +13,7 @@ extern "C" { # define KIT_BIGINT_SIZE 64 #endif +#if __STDC_VERSION__ >= 199901L static_assert(sizeof(uint8_t) == 1, "uint8_t size should be 1 byte"); static_assert(sizeof(uint32_t) == 4, "uint32_t size should be 4 bytes"); @@ -20,6 +21,7 @@ static_assert(sizeof(uint64_t) == 8, "uint64_t size should be 8 bytes"); static_assert(KIT_BIGINT_SIZE > 0 && (KIT_BIGINT_SIZE % 8) == 0, "Invalid big integer size"); +#endif typedef struct { uint32_t v[KIT_BIGINT_SIZE / 4]; @@ -66,7 +68,8 @@ static kit_bigint_t kit_bi_int64(int64_t const x) { } static int kit_bi_is_zero(kit_bigint_t const x) { - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) + ptrdiff_t i; + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) if (x.v[i] != 0) return 0; return 1; @@ -83,7 +86,8 @@ static int kit_bi_equal(kit_bigint_t const x, kit_bigint_t const y) { static int kit_bi_compare(kit_bigint_t const x, kit_bigint_t const y) { - for (ptrdiff_t i = KIT_BIGINT_SIZE / 4 - 1; i >= 0; i--) + ptrdiff_t i; + for (i = KIT_BIGINT_SIZE / 4 - 1; i >= 0; i--) if (x.v[i] < y.v[i]) return -1; else if (x.v[i] > y.v[i]) @@ -140,9 +144,9 @@ static ptrdiff_t kit_bi_significant_bit_count(kit_bigint_t const x) { static kit_bigint_t kit_bi_and(kit_bigint_t const x, kit_bigint_t const y) { kit_bigint_t z; + ptrdiff_t i; - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) - z.v[i] = x.v[i] & y.v[i]; + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) z.v[i] = x.v[i] & y.v[i]; return z; } @@ -150,9 +154,9 @@ static kit_bigint_t kit_bi_and(kit_bigint_t const x, static kit_bigint_t kit_bi_or(kit_bigint_t const x, kit_bigint_t const y) { kit_bigint_t z; + ptrdiff_t i; - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) - z.v[i] = x.v[i] | y.v[i]; + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) z.v[i] = x.v[i] | y.v[i]; return z; } @@ -160,9 +164,9 @@ static kit_bigint_t kit_bi_or(kit_bigint_t const x, static kit_bigint_t kit_bi_xor(kit_bigint_t const x, kit_bigint_t const y) { kit_bigint_t z; + ptrdiff_t i; - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) - z.v[i] = x.v[i] ^ y.v[i]; + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) z.v[i] = x.v[i] ^ y.v[i]; return z; } @@ -174,8 +178,9 @@ static kit_bigint_t kit_bi_shl_uint(kit_bigint_t const x, ptrdiff_t const words = (ptrdiff_t) (y / 32); ptrdiff_t const bits = (ptrdiff_t) (y % 32); + ptrdiff_t i; - for (ptrdiff_t i = words; i < KIT_BIGINT_SIZE / 4; i++) { + for (i = words; i < KIT_BIGINT_SIZE / 4; i++) { z.v[i] |= x.v[i - words] << bits; if (bits != 0 && i + 1 < KIT_BIGINT_SIZE / 4) z.v[i + 1] = x.v[i - words] >> (32 - bits); @@ -191,8 +196,9 @@ static kit_bigint_t kit_bi_shr_uint(kit_bigint_t const x, ptrdiff_t const words = (ptrdiff_t) (y / 32); ptrdiff_t const bits = (ptrdiff_t) (y % 32); + ptrdiff_t i; - for (ptrdiff_t i = KIT_BIGINT_SIZE / 4 - words - 1; i >= 0; i--) { + for (i = KIT_BIGINT_SIZE / 4 - words - 1; i >= 0; i--) { z.v[i] |= x.v[i + words] >> bits; if (bits != 0 && i > 0) z.v[i - 1] = x.v[i + words] << (32 - bits); @@ -212,8 +218,9 @@ static kit_bit_t kit_bi_carry(uint32_t const x, uint32_t const y, static kit_bigint_t kit_bi_inc(kit_bigint_t const x) { kit_bigint_t z; kit_bit_t carry = 1; + ptrdiff_t i; - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) { + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) { z.v[i] = x.v[i] + carry; carry = kit_bi_carry(x.v[i], 0, carry); } @@ -226,8 +233,9 @@ static kit_bigint_t kit_bi_inc(kit_bigint_t const x) { static kit_bigint_t kit_bi_dec(kit_bigint_t const x) { kit_bigint_t z; kit_bit_t carry = 0; + ptrdiff_t i; - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) { + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) { z.v[i] = x.v[i] + 0xffffffff + carry; carry = kit_bi_carry(x.v[i], 0xffffffff, carry); } @@ -241,8 +249,9 @@ static kit_bigint_t kit_bi_add(kit_bigint_t const x, kit_bigint_t const y) { kit_bigint_t z; kit_bit_t carry = 0; + ptrdiff_t i; - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) { + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) { z.v[i] = x.v[i] + y.v[i] + carry; carry = kit_bi_carry(x.v[i], y.v[i], carry); } @@ -255,8 +264,9 @@ static kit_bigint_t kit_bi_add(kit_bigint_t const x, static kit_bigint_t kit_bi_neg(kit_bigint_t const x) { kit_bigint_t y; kit_bit_t carry = 1; + ptrdiff_t i; - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) { + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) { y.v[i] = (x.v[i] ^ 0xffffffff) + carry; carry = kit_bi_carry(x.v[i] ^ 0xffffffff, 0, carry); } @@ -270,8 +280,9 @@ static kit_bigint_t kit_bi_sub(kit_bigint_t const x, kit_bigint_t const y) { kit_bigint_t z; kit_bit_t carry = 1; + ptrdiff_t i; - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) { + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) { z.v[i] = x.v[i] + (y.v[i] ^ 0xffffffff) + carry; carry = kit_bi_carry(x.v[i], (y.v[i] ^ 0xffffffff), carry); } @@ -282,17 +293,18 @@ static kit_bigint_t kit_bi_sub(kit_bigint_t const x, static kit_bigint_t kit_bi_mul_uint32(kit_bigint_t const x, uint32_t const y) { kit_bigint_t z; + ptrdiff_t i, k; + memset(&z, 0, sizeof z); if (y != 0) - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) { + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) { if (x.v[i] == 0) continue; uint64_t carry = ((uint64_t) x.v[i]) * ((uint64_t) y); - for (ptrdiff_t k = i; k < KIT_BIGINT_SIZE / 4 && carry != 0; - k++) { + for (k = i; k < KIT_BIGINT_SIZE / 4 && carry != 0; k++) { uint64_t const sum = ((uint64_t) z.v[k]) + carry; z.v[k] = ((uint32_t) (sum & 0xffffffffull)); carry = sum >> 32; @@ -307,20 +319,21 @@ static kit_bigint_t kit_bi_mul_uint32(kit_bigint_t const x, static kit_bigint_t kit_bi_mul(kit_bigint_t const x, kit_bigint_t const y) { kit_bigint_t z; + ptrdiff_t i, j, k; + memset(&z, 0, sizeof z); - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) { + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) { if (x.v[i] == 0) continue; - for (ptrdiff_t j = 0; i + j < KIT_BIGINT_SIZE / 4; j++) { + for (j = 0; i + j < KIT_BIGINT_SIZE / 4; j++) { if (y.v[j] == 0) continue; uint64_t carry = ((uint64_t) x.v[i]) * ((uint64_t) y.v[j]); - for (ptrdiff_t k = i + j; k < KIT_BIGINT_SIZE / 4 && carry != 0; - k++) { + for (k = i + j; k < KIT_BIGINT_SIZE / 4 && carry != 0; k++) { uint64_t const sum = ((uint64_t) z.v[k]) + carry; z.v[k] = ((uint32_t) (sum & 0xffffffffull)); carry = sum >> 32; @@ -400,9 +413,11 @@ static kit_bi_division_t kit_bi_div(kit_bigint_t const x, static void kit_bi_serialize(kit_bigint_t const in, uint8_t *const out) { + ptrdiff_t i; + assert(out != NULL); - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE / 4; i++) { + for (i = 0; i < KIT_BIGINT_SIZE / 4; i++) { out[i * 4] = (uint8_t) (in.v[i] & 0xff); out[i * 4 + 1] = (uint8_t) ((in.v[i] >> 8) & 0xff); out[i * 4 + 2] = (uint8_t) ((in.v[i] >> 16) & 0xff); @@ -411,12 +426,14 @@ static void kit_bi_serialize(kit_bigint_t const in, } static kit_bigint_t kit_bi_deserialize(uint8_t const *const in) { + ptrdiff_t i; + kit_bigint_t out; + assert(in != NULL); - kit_bigint_t out; memset(&out, 0, sizeof out); - for (ptrdiff_t i = 0; i < KIT_BIGINT_SIZE; i++) + for (i = 0; i < KIT_BIGINT_SIZE; i++) out.v[i / 4] |= ((uint32_t) in[i]) << (8 * (i % 4)); return out; @@ -429,10 +446,11 @@ static uint8_t kit_bin_digit(char const hex) { static kit_bigint_t kit_bi_from_bin(kit_str_t const bin) { kit_bigint_t z; + ptrdiff_t i; + memset(&z, 0, sizeof z); - for (ptrdiff_t i = 0; i < bin.size && i / 8 < KIT_BIGINT_SIZE; - i++) { + for (i = 0; i < bin.size && i / 8 < KIT_BIGINT_SIZE; i++) { uint8_t const digit = kit_bin_digit(bin.values[bin.size - i - 1]); z.v[i / 32] |= digit << (i % 32); } @@ -448,8 +466,9 @@ static uint8_t kit_dec_digit(char const c) { static kit_bigint_t kit_bi_from_dec(kit_str_t const dec) { kit_bigint_t z = kit_bi_uint32(0); kit_bigint_t factor = kit_bi_uint32(1); + ptrdiff_t i; - for (ptrdiff_t i = 0; i < dec.size; i++) { + for (i = 0; i < dec.size; i++) { uint32_t const digit = kit_dec_digit( dec.values[dec.size - i - 1]); z = kit_bi_add(z, kit_bi_mul_uint32(factor, digit)); @@ -475,10 +494,11 @@ static uint8_t kit_hex_digit(char const hex) { static kit_bigint_t kit_bi_from_hex(kit_str_t const hex) { kit_bigint_t z; + ptrdiff_t i; + memset(&z, 0, sizeof z); - for (ptrdiff_t i = 0; i < hex.size && i / 2 < KIT_BIGINT_SIZE; - i++) { + for (i = 0; i < hex.size && i / 2 < KIT_BIGINT_SIZE; i++) { uint8_t const digit = kit_hex_digit(hex.values[hex.size - i - 1]); z.v[i / 8] |= digit << (4 * (i % 8)); } @@ -508,9 +528,11 @@ static uint8_t kit_base32_digit(char const c) { static kit_bigint_t kit_bi_from_base32(kit_str_t const base32) { kit_bigint_t z; + ptrdiff_t i; + memset(&z, 0, sizeof z); - for (ptrdiff_t i = 0; i < base32.size; i++) { + for (i = 0; i < base32.size; i++) { z = kit_bi_shl_uint(z, 5 * i); z.v[0] |= kit_base32_digit(base32.values[i]); } @@ -546,8 +568,9 @@ static uint8_t kit_base58_digit(char const c) { static kit_bigint_t kit_bi_from_base58(kit_str_t const base58) { kit_bigint_t z = kit_bi_uint32(0); kit_bigint_t factor = kit_bi_uint32(1); + ptrdiff_t i; - for (ptrdiff_t i = 0; i < base58.size; i++) { + for (i = 0; i < base58.size; i++) { uint32_t const digit = kit_base58_digit( base58.values[base58.size - i - 1]); z = kit_bi_add(z, kit_bi_mul_uint32(factor, digit)); diff --git a/source/kit/dynamic_array.h b/source/kit/dynamic_array.h index 62695f4..b571c75 100644 --- a/source/kit/dynamic_array.h +++ b/source/kit/dynamic_array.h @@ -71,12 +71,13 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size, */ #define KIT_DA_INSERT(array_, index_, value_) \ do { \ + ptrdiff_t kit_i_; \ ptrdiff_t const kit_index_back_ = (array_).size; \ ptrdiff_t const kit_indert_n_ = (index_); \ KIT_DA_RESIZE((array_), kit_index_back_ + 1); \ if (kit_index_back_ + 1 == (array_).size) { \ - for (ptrdiff_t kit_i_ = kit_index_back_; \ - kit_i_ > kit_indert_n_; kit_i_--) \ + for (kit_i_ = kit_index_back_; kit_i_ > kit_indert_n_; \ + kit_i_--) \ (array_).values[kit_i_] = (array_).values[kit_i_ - 1]; \ (array_).values[kit_indert_n_] = (value_); \ } \ @@ -84,11 +85,12 @@ void kit_da_resize(kit_da_void_t *array, ptrdiff_t element_size, /* Erase a value from dynamic array. */ -#define KIT_DA_ERASE(array_, index_) \ - do { \ - for (ptrdiff_t i_ = (index_) + 1; i_ < (array_).size; i_++) \ - (array_).values[i_ - 1] = (array_).values[i_]; \ - KIT_DA_RESIZE((array_), (array_).size - 1); \ +#define KIT_DA_ERASE(array_, index_) \ + do { \ + ptrdiff_t i_; \ + for (i_ = (index_) + 1; i_ < (array_).size; i_++) \ + (array_).values[i_ - 1] = (array_).values[i_]; \ + KIT_DA_RESIZE((array_), (array_).size - 1); \ } while (0) typedef KIT_DA(char) kit_string_t; diff --git a/source/kit/file.c b/source/kit/file.c index 4f609e6..f4000b7 100644 --- a/source/kit/file.c +++ b/source/kit/file.c @@ -25,6 +25,7 @@ static int is_delim(char const c) { kit_string_t kit_path_norm(kit_str_t const path, kit_allocator_t const alloc) { str_t const parent = SZ(".."); + ptrdiff_t i, i1, j; string_t norm; DA_INIT(norm, path.size, alloc); @@ -35,7 +36,7 @@ kit_string_t kit_path_norm(kit_str_t const path, memcpy(norm.values, path.values, path.size); - for (ptrdiff_t i1 = 0, i = 0; i < path.size; i++) { + for (i1 = 0, i = 0; i < path.size; i++) { if (!is_delim(path.values[i])) continue; @@ -45,7 +46,7 @@ kit_string_t kit_path_norm(kit_str_t const path, int have_parent = 0; ptrdiff_t i0 = 0; - for (ptrdiff_t j = 0; j < i1; j++) { + for (j = 0; j < i1; j++) { if (norm.values[j] != '\0') have_parent = 1; if (is_delim(norm.values[j])) @@ -65,7 +66,7 @@ kit_string_t kit_path_norm(kit_str_t const path, ptrdiff_t size = 0; - for (ptrdiff_t i = 0; i < norm.size; i++) { + for (i = 0; i < norm.size; i++) { if (norm.values[i] != '\0') { if (is_delim(norm.values[i])) norm.values[size] = KIT_PATH_DELIM; @@ -234,7 +235,9 @@ kit_status_t kit_file_create_folder(kit_str_t const path) { } kit_status_t kit_file_create_folder_recursive(kit_str_t const path) { - for (ptrdiff_t i = 0;; i++) { + ptrdiff_t i; + + for (i = 0;; i++) { str_t const part = kit_path_take(path, i); int const type = kit_path_type(part); if (type == KIT_PATH_FILE) @@ -271,7 +274,8 @@ kit_status_t kit_file_remove_folder(kit_str_t const path) { kit_status_t kit_file_remove_recursive(kit_str_t const path, kit_allocator_t const alloc) { - int type = kit_path_type(path); + int type = kit_path_type(path); + ptrdiff_t i; switch (type) { case KIT_PATH_FILE: return kit_file_remove(path); @@ -282,7 +286,7 @@ kit_status_t kit_file_remove_recursive(kit_str_t const path, kit_path_list_destroy(list); return list.status; } - for (ptrdiff_t i = 0; i < list.files.size; i++) { + for (i = 0; i < list.files.size; i++) { str_t const s = { .size = list.files.values[i].size, .values = list.files.values[i].values }; kit_file_remove_recursive(s, alloc); @@ -434,7 +438,8 @@ kit_path_list_t kit_file_enum_folder(kit_str_t const path, } void kit_path_list_destroy(kit_path_list_t list) { - for (ptrdiff_t i = 0; i < list.files.size; i++) + ptrdiff_t i; + for (i = 0; i < list.files.size; i++) DA_DESTROY(list.files.values[i]); DA_DESTROY(list.files); } diff --git a/source/kit/mersenne_twister_64.c b/source/kit/mersenne_twister_64.c index 926a886..c40806e 100644 --- a/source/kit/mersenne_twister_64.c +++ b/source/kit/mersenne_twister_64.c @@ -8,8 +8,8 @@ void kit_mt64_init_array(kit_mt64_state_t *const state, ptrdiff_t const size, uint64_t const *const seed) { - for (ptrdiff_t i = 0; i < size && i < KIT_MT64_N; i++) - state->mt[i] = seed[i]; + ptrdiff_t i; + for (i = 0; i < size && i < KIT_MT64_N; i++) state->mt[i] = seed[i]; for (state->index = size; state->index < KIT_MT64_N; state->index++) state->mt[state->index] = (6364136223846793005ull * (state->mt[state->index - 1] ^ diff --git a/source/kit/move_back.h b/source/kit/move_back.h index 4bcc7ba..6eee4cf 100644 --- a/source/kit/move_back.h +++ b/source/kit/move_back.h @@ -9,9 +9,10 @@ extern "C" { #define KIT_MOVE_BACK_INL(new_size, array, ...) \ do { \ + ptrdiff_t index_; \ ptrdiff_t end_ = (array).size; \ unsigned char temp_[sizeof *(array).values]; \ - for (ptrdiff_t index_ = 0; index_ < end_;) { \ + for (index_ = 0; index_ < end_;) { \ if (__VA_ARGS__) { \ end_--; \ if (index_ != end_) { \ diff --git a/source/kit/secure_random.c b/source/kit/secure_random.c index 2f1c796..3112167 100644 --- a/source/kit/secure_random.c +++ b/source/kit/secure_random.c @@ -53,8 +53,9 @@ static void secure_random_fallback(ptrdiff_t const size, static uint64_t n = 0; static uint64_t time_sec = 0; static uint64_t time_nsec = 0; - struct timespec t; + ptrdiff_t i; + timespec_get(&t, TIME_UTC); kit_mt64_state_t state; @@ -80,7 +81,7 @@ static void secure_random_fallback(ptrdiff_t const size, time_sec = (uint64_t) t.tv_sec; time_nsec = (uint64_t) t.tv_nsec; - for (ptrdiff_t i = 0; i < size; i++) + for (i = 0; i < size; i++) ((uint8_t *) data)[i] = (uint8_t) (kit_mt64_generate(&state) >> 56); diff --git a/source/kit/thread.posix.c b/source/kit/thread.posix.c index 442d7f8..4d21abb 100644 --- a/source/kit/thread.posix.c +++ b/source/kit/thread.posix.c @@ -14,6 +14,10 @@ # include "mutex.h" # include "thread.h" +# ifndef PTHREAD_STACK_MIN +# define PTHREAD_STACK_MIN 16384 +# endif + /* Configuration macro: @@ -122,7 +126,9 @@ __attribute__((weak)) int pthread_mutexattr_destroy( # endif int mtx_init(mtx_t *mtx, int type) { +# ifdef KIT_HAVE_PTHREAD_MUTEXATTR_SETTYPE pthread_mutexattr_t attr; +# endif assert(mtx != NULL); if (type != mtx_plain && type != mtx_timed && type != (mtx_plain | mtx_recursive) && @@ -134,11 +140,15 @@ int mtx_init(mtx_t *mtx, int type) { return thrd_success; } +# ifdef KIT_HAVE_PTHREAD_MUTEXATTR_SETTYPE pthread_mutexattr_init(&attr); pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(mtx, &attr); pthread_mutexattr_destroy(&attr); return thrd_success; +# else + return thrd_error; +# endif } int mtx_lock(mtx_t *mtx) { diff --git a/source/kit/time.c b/source/kit/time.c index 108434f..ccd3835 100644 --- a/source/kit/time.c +++ b/source/kit/time.c @@ -35,13 +35,27 @@ int timespec_get(struct timespec *ts, int base) { # undef _TIMESPEC_IMPL_TICKS_PER_SECONDS } +# elif defined(KIT_HAVE_CLOCK_GETTIME) +int timespec_get(struct timespec *ts, int base) { + if (ts == NULL) + return 0; + if (base == TIME_UTC) { + clock_gettime(CLOCK_REALTIME, ts); + return base; + } + return 0; +} # else +# include int timespec_get(struct timespec *ts, int base) { if (ts == NULL) return 0; if (base == TIME_UTC) { - clock_gettime(CLOCK_REALTIME, ts); + struct timeval tv; + gettimeofday(&tv, NULL); + ts->tv_sec = tv.tv_sec; + ts->tv_nsec = tv.tv_usec * 1000; return base; } return 0; diff --git a/source/kit/time.h b/source/kit/time.h index 9445912..cf54d47 100644 --- a/source/kit/time.h +++ b/source/kit/time.h @@ -12,10 +12,14 @@ extern "C" { #endif #ifdef KIT_NEED_STRUCT_TIMESPEC +# ifdef KIT_HAVE_BITS_TYPES_STRUCT_TIMESPEC_H +# include +# else struct timespec { time_t tv_sec; /* Seconds - >= 0 */ long tv_nsec; /* Nanoseconds - [0, 999999999] */ }; +# endif #endif #ifdef KIT_NEED_TIMESPEC_GET diff --git a/source/kit_test/CMakeLists.txt b/source/kit_test/CMakeLists.txt index 25b9d81..e590437 100644 --- a/source/kit_test/CMakeLists.txt +++ b/source/kit_test/CMakeLists.txt @@ -1,5 +1,5 @@ target_sources( - ${KIT_TEST_LIBRARY} + kit_test PRIVATE test.c PUBLIC diff --git a/source/kit_test/test.c b/source/kit_test/test.c index 1704343..e60413a 100644 --- a/source/kit_test/test.c +++ b/source/kit_test/test.c @@ -68,7 +68,9 @@ static void handle_signal(int signum) { } static void setup_signals() { - for (int i = 0; i < sizeof signums / sizeof *signums; i++) { + int i; + + for (i = 0; i < sizeof signums / sizeof *signums; i++) { #if defined(_WIN32) && !(defined __CYGWIN__) signal(signums[i], handle_signal); #else @@ -94,8 +96,6 @@ static int run_test(volatile int i) { } int kit_run_tests(int argc, char **argv) { - setup_signals(); - int success_count = 0; int fail_assertion_count = 0; int total_assertion_count = 0; @@ -105,9 +105,13 @@ int kit_run_tests(int argc, char **argv) { int line_width = 20; int carriage_return = 1; + int i, j; + char const *specific_test = NULL; - for (int i = 0; i < argc; i++) + setup_signals(); + + for (i = 0; i < argc; i++) if (strcmp("--no-term-color", argv[i]) == 0) no_color = 1; else if (strcmp("--no-carriage-return", argv[i]) == 0) @@ -132,7 +136,7 @@ int kit_run_tests(int argc, char **argv) { ptrdiff_t file_root = -1; int tests_total = 0; - for (int i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT; + for (i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT; i++) { if (specific_test != NULL && strstr(kit_tests_list.tests[i].test_name, specific_test) == @@ -147,8 +151,8 @@ int kit_run_tests(int argc, char **argv) { if (tests_total > 0) { char const *const s = kit_tests_list.tests[0].test_file; - for (int j = 1; - j < kit_tests_list.size && j < KIT_TESTS_SIZE_LIMIT; j++) { + for (j = 1; j < kit_tests_list.size && j < KIT_TESTS_SIZE_LIMIT; + j++) { if (specific_test != NULL && strstr(kit_tests_list.tests[j].test_name, specific_test) == NULL) @@ -165,13 +169,13 @@ int kit_run_tests(int argc, char **argv) { } if (file_root == -1) { - for (int i = 0; s[i] != '\0'; i++) + for (i = 0; s[i] != '\0'; i++) if (s[i] == '/' || s[i] == '\\') file_root = i + 1; } } - for (int i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT; + for (i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT; i++) { if (specific_test != NULL && strstr(kit_tests_list.tests[i].test_name, specific_test) == @@ -202,7 +206,7 @@ int kit_run_tests(int argc, char **argv) { int duration = (int) (ns_to_ms(end.tv_nsec - begin.tv_nsec) + sec_to_ms(end.tv_sec - begin.tv_sec)); - for (int j = 0; j < kit_tests_list.tests[i].assertions; j++) + for (j = 0; j < kit_tests_list.tests[i].assertions; j++) if (kit_tests_list.tests[i].status[j] == 0) { fail_assertion_count++; test_status = 0; @@ -249,8 +253,8 @@ int kit_run_tests(int argc, char **argv) { no_color || print_color(light); if (status != 0) { - for (int i = 0; - i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT; i++) { + for (i = 0; i < kit_tests_list.size && i < KIT_TESTS_SIZE_LIMIT; + i++) { if (specific_test != NULL && strstr(kit_tests_list.tests[i].test_name, specific_test) == NULL) @@ -300,7 +304,7 @@ int kit_run_tests(int argc, char **argv) { no_color || print_color(light); quiet || printf("\"!.\n"); } else - for (int j = 0; j < kit_tests_list.tests[i].assertions; j++) + for (j = 0; j < kit_tests_list.tests[i].assertions; j++) if (!kit_tests_list.tests[i].status[j]) { no_color || print_color(light); quiet || printf("Assertion on line "); diff --git a/source/test/programs/CMakeLists.txt b/source/test/programs/CMakeLists.txt index bdaf89e..81332cf 100644 --- a/source/test/programs/CMakeLists.txt +++ b/source/test/programs/CMakeLists.txt @@ -6,7 +6,7 @@ project( if(KIT_ENABLE_TESTING) add_executable(too_many_tests too_many_tests.c) - target_link_libraries(too_many_tests ${KIT_TEST_LIBRARY}) + target_link_libraries(too_many_tests kit_test) add_test( NAME too_many_tests_test COMMAND too_many_tests --quiet) @@ -16,7 +16,7 @@ if(KIT_ENABLE_TESTING) TIMEOUT "15") add_executable(too_many_assertions too_many_assertions.c) - target_link_libraries(too_many_assertions ${KIT_TEST_LIBRARY}) + target_link_libraries(too_many_assertions kit_test) add_test( NAME too_many_assertions_test COMMAND too_many_assertions --quiet) @@ -26,7 +26,7 @@ if(KIT_ENABLE_TESTING) TIMEOUT "15") add_executable(cpp_example cpp_example.cpp) - target_link_libraries(cpp_example ${KIT_TEST_LIBRARY}) + target_link_libraries(cpp_example kit_test) add_test( NAME cpp_example_test COMMAND cpp_example --quiet) @@ -37,7 +37,7 @@ if(KIT_ENABLE_TESTING) if(NOT MSVC) add_executable(signals signals.cpp) - target_link_libraries(signals ${KIT_TEST_LIBRARY}) + target_link_libraries(signals kit_test) add_test( NAME signals_test COMMAND signals --no-carriage-return) diff --git a/source/test/programs/too_many_assertions.c b/source/test/programs/too_many_assertions.c index 846970f..06b440b 100644 --- a/source/test/programs/too_many_assertions.c +++ b/source/test/programs/too_many_assertions.c @@ -1,7 +1,8 @@ #include "../../kit_test/test.h" TEST("foo") { - for (int i = 0; i <= KIT_TEST_ASSERTIONS_LIMIT; i++) REQUIRE(1); + int i; + for (i = 0; i <= KIT_TEST_ASSERTIONS_LIMIT; i++) REQUIRE(1); } int main(int argc, char **argv) { diff --git a/source/test/programs/too_many_tests.c b/source/test/programs/too_many_tests.c index bc7b543..ac1cbf7 100644 --- a/source/test/programs/too_many_tests.c +++ b/source/test/programs/too_many_tests.c @@ -3,7 +3,8 @@ void bar(int index, kit_test_report_fn report) { } int main(int argc, char **argv) { - for (int i = 0; i <= KIT_TESTS_SIZE_LIMIT; i++) + int i; + for (i = 0; i <= KIT_TESTS_SIZE_LIMIT; i++) test_register("foo", __FILE__, bar); if (run_tests(argc, argv) != 1) diff --git a/source/test/unittests/CMakeLists.txt b/source/test/unittests/CMakeLists.txt index b2ef480..2ebc195 100644 --- a/source/test/unittests/CMakeLists.txt +++ b/source/test/unittests/CMakeLists.txt @@ -1,5 +1,5 @@ target_sources( - ${KIT_TEST_SUITE} + 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 thread.test.c diff --git a/source/test/unittests/async_function.test.c b/source/test/unittests/async_function.test.c index 6e1bc69..320f323 100644 --- a/source/test/unittests/async_function.test.c +++ b/source/test/unittests/async_function.test.c @@ -48,7 +48,8 @@ STATIC_CORO(int, test_nest_generator, AF_TYPE(test_gen) promise;) { CORO_END STATIC_CORO(int, test_join_multiple, AF_TYPE(test_bar) promises[3];) { - for (int i = 0; i < 3; i++) + int i; + for (i = 0; i < 3; i++) AF_INIT(af promises[i], test_bar, .return_value = 0); AF_RESUME_AND_JOIN_ALL(af promises); AF_RETURN(af promises[0].return_value + @@ -59,7 +60,8 @@ CORO_END STATIC_CORO(int, test_await_multiple, AF_TYPE(test_bar) promises[3];) { - for (int i = 0; i < 3; i++) + int i; + for (i = 0; i < 3; i++) AF_INIT(af promises[i], test_bar, .return_value = 0); AF_AWAIT_ALL(af promises); AF_RETURN(af promises[0].return_value + @@ -131,8 +133,9 @@ TEST("coroutine suspend") { } TEST("coroutine generator") { + int i; AF_CREATE(promise, test_gen, .min = 10, .max = 15); - for (int i = 0; i <= 5; i++) + for (i = 0; i <= 5; i++) REQUIRE(AF_RESUME_AND_JOIN(promise) == 10 + i); } diff --git a/source/test/unittests/atomic.test.c b/source/test/unittests/atomic.test.c index b8cf0ac..e7cf1fa 100644 --- a/source/test/unittests/atomic.test.c +++ b/source/test/unittests/atomic.test.c @@ -29,9 +29,10 @@ TEST("atomic fetch add") { enum { THREAD_COUNT = 20, TICK_COUNT = 10000 }; static int test_8_(void *p) { + ptrdiff_t i; ATOMIC(int8_t) *x = (ATOMIC(int8_t) *) p; - for (ptrdiff_t i = 0; i < TICK_COUNT; i++) { + for (i = 0; i < TICK_COUNT; i++) { atomic_fetch_add_explicit(x, 20, memory_order_relaxed); thrd_yield(); atomic_fetch_add_explicit(x, 22, memory_order_relaxed); @@ -39,6 +40,8 @@ static int test_8_(void *p) { atomic_fetch_add_explicit(x, -42, memory_order_relaxed); thrd_yield(); } + + return 0; } TEST("atomic types") { @@ -81,6 +84,8 @@ TEST("atomic types") { } TEST("atomic byte concurrency") { + ptrdiff_t i; + ATOMIC(int8_t) foo; ATOMIC(int8_t) bar; @@ -88,20 +93,21 @@ TEST("atomic byte concurrency") { atomic_store_explicit(&bar, 43, memory_order_relaxed); thrd_t threads[THREAD_COUNT]; - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) + for (i = 0; i < THREAD_COUNT; i++) thrd_create(threads + i, test_8_, (void *) ((i % 2) ? &foo : &bar)); - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) - thrd_join(threads[i], NULL); + for (i = 0; i < THREAD_COUNT; i++) thrd_join(threads[i], NULL); REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42); REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43); } static int test_16_(void *p) { + ptrdiff_t i; + ATOMIC(int16_t) *x = (ATOMIC(int16_t) *) p; - for (ptrdiff_t i = 0; i < TICK_COUNT; i++) { + for (i = 0; i < TICK_COUNT; i++) { atomic_fetch_add_explicit(x, 2020, memory_order_relaxed); thrd_yield(); atomic_fetch_add_explicit(x, 2222, memory_order_relaxed); @@ -109,9 +115,13 @@ static int test_16_(void *p) { atomic_fetch_add_explicit(x, -4242, memory_order_relaxed); thrd_yield(); } + + return 0; } TEST("atomic int16 concurrency") { + ptrdiff_t i; + ATOMIC(int16_t) foo; ATOMIC(int16_t) bar; @@ -119,20 +129,21 @@ TEST("atomic int16 concurrency") { atomic_store_explicit(&bar, 43, memory_order_relaxed); thrd_t threads[THREAD_COUNT]; - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) + for (i = 0; i < THREAD_COUNT; i++) thrd_create(threads + i, test_16_, (void *) ((i % 2) ? &foo : &bar)); - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) - thrd_join(threads[i], NULL); + for (i = 0; i < THREAD_COUNT; i++) thrd_join(threads[i], NULL); REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42); REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43); } static int test_32_(void *p) { + ptrdiff_t i; + ATOMIC(int32_t) *x = (ATOMIC(int32_t) *) p; - for (ptrdiff_t i = 0; i < TICK_COUNT; i++) { + for (i = 0; i < TICK_COUNT; i++) { atomic_fetch_add_explicit(x, 202020, memory_order_relaxed); thrd_yield(); atomic_fetch_add_explicit(x, 222222, memory_order_relaxed); @@ -140,9 +151,13 @@ static int test_32_(void *p) { atomic_fetch_add_explicit(x, -424242, memory_order_relaxed); thrd_yield(); } + + return 0; } TEST("atomic int32 concurrency") { + ptrdiff_t i; + ATOMIC(int32_t) foo; ATOMIC(int32_t) bar; @@ -150,20 +165,21 @@ TEST("atomic int32 concurrency") { atomic_store_explicit(&bar, 43, memory_order_relaxed); thrd_t threads[THREAD_COUNT]; - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) + for (i = 0; i < THREAD_COUNT; i++) thrd_create(threads + i, test_32_, (void *) ((i % 2) ? &foo : &bar)); - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) - thrd_join(threads[i], NULL); + for (i = 0; i < THREAD_COUNT; i++) thrd_join(threads[i], NULL); REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42); REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43); } static int test_64_(void *p) { + ptrdiff_t i; + ATOMIC(int64_t) *x = (ATOMIC(int64_t) *) p; - for (ptrdiff_t i = 0; i < TICK_COUNT; i++) { + for (i = 0; i < TICK_COUNT; i++) { atomic_fetch_add_explicit(x, 20202020202020ll, memory_order_relaxed); thrd_yield(); @@ -174,9 +190,13 @@ static int test_64_(void *p) { memory_order_relaxed); thrd_yield(); } + + return 0; } TEST("atomic int64 concurrency") { + ptrdiff_t i; + ATOMIC(int64_t) foo; ATOMIC(int64_t) bar; @@ -184,11 +204,10 @@ TEST("atomic int64 concurrency") { atomic_store_explicit(&bar, 43, memory_order_relaxed); thrd_t threads[THREAD_COUNT]; - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) + for (i = 0; i < THREAD_COUNT; i++) thrd_create(threads + i, test_64_, (void *) ((i % 2) ? &foo : &bar)); - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) - thrd_join(threads[i], NULL); + for (i = 0; i < THREAD_COUNT; i++) thrd_join(threads[i], NULL); REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42); REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43); diff --git a/source/test/unittests/bigint.test.c b/source/test/unittests/bigint.test.c index 048bcca..260b0ec 100644 --- a/source/test/unittests/bigint.test.c +++ b/source/test/unittests/bigint.test.c @@ -4,7 +4,9 @@ #define KIT_TEST_FILE bigint #include "../../kit_test/test.h" +#if __STDC_VERSION__ >= 199901L static_assert(sizeof(bigint_t) == 256, "KIT_BIGINT_SIZE check"); +#endif TEST("bigint bin hex") { REQUIRE(bi_equal(HEX("10"), BIN("10000"))); @@ -77,7 +79,6 @@ TEST("bigint div") { REQUIRE(bi_equal(bi_div(bi_int32(3), bi_int32(-4)).remainder, bi_int32(3))); - REQUIRE( bi_equal(bi_div(HEX("100"), HEX("10")).quotient, HEX("10"))); diff --git a/source/test/unittests/condition_variable.test.c b/source/test/unittests/condition_variable.test.c index c5b0468..2dba7ca 100644 --- a/source/test/unittests/condition_variable.test.c +++ b/source/test/unittests/condition_variable.test.c @@ -37,9 +37,10 @@ static int test_run(void *p) { } TEST("condition variable") { + int i; int ok = 1; - for (int i = 0; i < 10; i++) { + for (i = 0; i < 10; i++) { test_data_t data; data.in = 0; diff --git a/source/test/unittests/mersenne_twister_64.test.c b/source/test/unittests/mersenne_twister_64.test.c index 80d1c2c..e523154 100644 --- a/source/test/unittests/mersenne_twister_64.test.c +++ b/source/test/unittests/mersenne_twister_64.test.c @@ -7,7 +7,8 @@ enum { SIZE = 1000 }; TEST("mt64 same seeds") { - uint64_t seed; + ptrdiff_t i; + uint64_t seed; secure_random(sizeof seed, &seed); mt64_state_t foo, bar; @@ -15,19 +16,21 @@ TEST("mt64 same seeds") { mt64_init(&bar, seed); int ok = 1; - for (ptrdiff_t i = 0; i < SIZE; i++) + for (i = 0; i < SIZE; i++) ok = ok && mt64_generate(&foo) == mt64_generate(&bar); REQUIRE(ok); } TEST("mt64 different seeds") { + ptrdiff_t i; + mt64_state_t foo, bar; mt64_init(&foo, 42); mt64_init(&bar, 4242424242); ptrdiff_t difference_count = 0; - for (ptrdiff_t i = 0; i < SIZE; i++) + for (i = 0; i < SIZE; i++) if (mt64_generate(&foo) != mt64_generate(&bar)) difference_count++; diff --git a/source/test/unittests/mutex.test.c b/source/test/unittests/mutex.test.c index e597e59..1bd2b3d 100644 --- a/source/test/unittests/mutex.test.c +++ b/source/test/unittests/mutex.test.c @@ -12,8 +12,9 @@ typedef struct { } test_data_t; static int test_run(void *data) { + int i; test_data_t *x = (test_data_t *) data; - for (int i = 0; i < TICK_COUNT; i++) { + for (i = 0; i < TICK_COUNT; i++) { mtx_lock(&x->lock); x->value += i; @@ -30,15 +31,16 @@ static int test_run(void *data) { } TEST("mutex lock") { + ptrdiff_t i; + test_data_t data; thrd_t pool[THREAD_COUNT]; data.value = 42; REQUIRE(mtx_init(&data.lock, mtx_plain) == thrd_success); - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) + for (i = 0; i < THREAD_COUNT; i++) thrd_create(pool + i, test_run, &data); - for (ptrdiff_t i = 0; i < THREAD_COUNT; i++) - thrd_join(pool[i], NULL); + for (i = 0; i < THREAD_COUNT; i++) thrd_join(pool[i], NULL); mtx_destroy(&data.lock); REQUIRE(data.value == 42); diff --git a/source/test/unittests/secure_random.test.c b/source/test/unittests/secure_random.test.c index ec0a7b2..bde68a5 100644 --- a/source/test/unittests/secure_random.test.c +++ b/source/test/unittests/secure_random.test.c @@ -5,6 +5,8 @@ #include "../../kit_test/test.h" TEST("secure random") { + int i, j; + int v[20]; memset(v, 0, sizeof v); @@ -13,8 +15,8 @@ TEST("secure random") { int repeats = 0; - for (int i = 1; i < sizeof v / sizeof *v; i++) - for (int j = 0; j < i; j++) + for (i = 1; i < sizeof v / sizeof *v; i++) + for (j = 0; j < i; j++) if (v[i] == v[j]) repeats++; -- cgit v1.2.3