From dd0505867f21b15c997482d5244ee66745c7386c Mon Sep 17 00:00:00 2001 From: Vishal Pankaj Chandratreya <19171016+tfpf@users.noreply.github.com> Date: Thu, 25 Apr 2024 21:46:43 +0530 Subject: [PATCH] Correctly implemented multithreaded tests. (#15) * Correctly implemented multithreaded tests. * I had first attempted this in #2, but removed because my implementation was flawed. * This implementation should do everything correctly. * Updated ignores to be directory-relative. * macOS: use GCC instead of Apple C compiler. * Because the default compiler does not support standard threads, and does not set the macro indicating the same. * Neither does any version of GCC. * Disabled multithreading in macOS tests. * Switched back to default C compiler on macOS. --- .gitignore | 3 ++- benchmarks/.gitignore | 4 +-- examples/C++/.gitignore | 8 +++--- examples/C/.gitignore | 8 +++--- lib/.gitignore | 8 +++--- tests/.gitignore | 4 +-- tests/tests.c | 54 +++++++++++++++++++++++++++-------------- 7 files changed, 54 insertions(+), 35 deletions(-) diff --git a/.gitignore b/.gitignore index 378eac2..af10b74 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -build +/.vscode/ +/build diff --git a/benchmarks/.gitignore b/benchmarks/.gitignore index 5ac565f..19bfb84 100644 --- a/benchmarks/.gitignore +++ b/benchmarks/.gitignore @@ -1,2 +1,2 @@ -benchmarks -benchmarks.exe +/benchmarks +/benchmarks.exe diff --git a/examples/C++/.gitignore b/examples/C++/.gitignore index 795bf9e..4d1cab3 100644 --- a/examples/C++/.gitignore +++ b/examples/C++/.gitignore @@ -1,4 +1,4 @@ -thread-safe -thread-safe.exe -thread-unsafe -thread-unsafe.exe +/thread-safe +/thread-safe.exe +/thread-unsafe +/thread-unsafe.exe diff --git a/examples/C/.gitignore b/examples/C/.gitignore index 795bf9e..4d1cab3 100644 --- a/examples/C/.gitignore +++ b/examples/C/.gitignore @@ -1,4 +1,4 @@ -thread-safe -thread-safe.exe -thread-unsafe -thread-unsafe.exe +/thread-safe +/thread-safe.exe +/thread-unsafe +/thread-unsafe.exe diff --git a/lib/.gitignore b/lib/.gitignore index c4bb5eb..98d7bca 100644 --- a/lib/.gitignore +++ b/lib/.gitignore @@ -1,4 +1,4 @@ -hdrbg.egg-info -*.dll -*.o -*.so +/hdrbg.egg-info/ +/*.dll +/*.o +/*.so diff --git a/tests/.gitignore b/tests/.gitignore index 9ab3da5..a6c7207 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,2 +1,2 @@ -tests -tests.exe +/tests +/tests.exe diff --git a/tests/tests.c b/tests/tests.c index b3df297..dc83095 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -4,7 +4,15 @@ #include #include #include -#include + +// The C compilers available on the macOS runners on GitHub Actions do not +// indicate their lack of support for standard threads with the expected +// preprocessor macro, so disable multithreading on macOS. +#if defined __APPLE__ || defined __STDC_NO_THREADS__ +#define STDC_NO_THREADS +#else +#include +#endif #define WORKERS_SIZE 8 #define CUSTOM_ITERATIONS (1L << 16) @@ -56,19 +64,6 @@ hdrbg_tests_custom(void *hd_) return 0; } -/****************************************************************************** - * Test a particular HDRBG object. - * - * @param hd HDRBG object. - * @param tv Test vectors file. - *****************************************************************************/ -void -tests(struct hdrbg_t *hd, FILE *tv) -{ - hdrbg_tests(hd, tv); - hdrbg_tests_custom(hd); -} - /****************************************************************************** * Main function. *****************************************************************************/ @@ -77,14 +72,37 @@ main(void) { printf("Testing a dynamically-allocated HDRBG object.\n"); FILE *tv = fopen("Hash_DRBG.dat", "rb"); - struct hdrbg_t *hd = hdrbg_init(true); - tests(hd, tv); - hdrbg_zero(hd); + struct hdrbg_t *hds[WORKERS_SIZE]; + for (int i = 0; i < WORKERS_SIZE; ++i) + { + hds[i] = hdrbg_init(true); + hdrbg_tests(hds[i], tv); + rewind(tv); + } +#ifndef STDC_NO_THREADS + thrd_t workers[WORKERS_SIZE]; +#endif + for (int i = 0; i < WORKERS_SIZE; ++i) + { +#ifndef STDC_NO_THREADS + thrd_create(workers + i, hdrbg_tests_custom, hds[i]); +#else + hdrbg_tests_custom(hds[i]); +#endif + } + for (int i = 0; i < WORKERS_SIZE; ++i) + { +#ifndef STDC_NO_THREADS + thrd_join(workers[i], NULL); +#endif + hdrbg_zero(hds[i]); + } printf("All tests passed.\n"); printf("Testing the internal HDRBG object.\n"); rewind(tv); - tests(NULL, tv); + hdrbg_tests(NULL, tv); + hdrbg_tests_custom(NULL); fclose(tv); printf("All tests passed.\n"); }