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 --- 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 ++- 8 files changed, 62 insertions(+), 31 deletions(-) (limited to 'source/test/unittests') 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