Skip to content

Commit

Permalink
Correctly implemented multithreaded tests. (#15)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
tfpf authored Apr 25, 2024
1 parent 2fe1083 commit dd05058
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 35 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
build
/.vscode/
/build
4 changes: 2 additions & 2 deletions benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
benchmarks
benchmarks.exe
/benchmarks
/benchmarks.exe
8 changes: 4 additions & 4 deletions examples/C++/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
thread-safe
thread-safe.exe
thread-unsafe
thread-unsafe.exe
/thread-safe
/thread-safe.exe
/thread-unsafe
/thread-unsafe.exe
8 changes: 4 additions & 4 deletions examples/C/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
thread-safe
thread-safe.exe
thread-unsafe
thread-unsafe.exe
/thread-safe
/thread-safe.exe
/thread-unsafe
/thread-unsafe.exe
8 changes: 4 additions & 4 deletions lib/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
hdrbg.egg-info
*.dll
*.o
*.so
/hdrbg.egg-info/
/*.dll
/*.o
/*.so
4 changes: 2 additions & 2 deletions tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
tests
tests.exe
/tests
/tests.exe
54 changes: 36 additions & 18 deletions tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>

// 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 <threads.h>
#endif

#define WORKERS_SIZE 8
#define CUSTOM_ITERATIONS (1L << 16)
Expand Down Expand Up @@ -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.
*****************************************************************************/
Expand All @@ -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");
}

0 comments on commit dd05058

Please sign in to comment.