Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly implemented multithreaded tests. #15

Merged
merged 7 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}