Skip to content

Commit

Permalink
Seed the random number generator before running each test. We use the…
Browse files Browse the repository at this point in the history
… C RNG utilities because this is a C library and most of the test code is written in C.
  • Loading branch information
pavel-kirienko committed Aug 9, 2023
1 parent 02c9a33 commit 118a746
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 13 additions & 0 deletions tests/src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <time.h>

#if !(defined(UDPARD_VERSION_MAJOR) && defined(UDPARD_VERSION_MINOR))
# error "Library version not defined"
Expand Down Expand Up @@ -158,6 +159,18 @@ static inline struct UdpardMemoryDeleter instrumentedAllocatorMakeMemoryDeleter(
return out;
}

static inline void seedRandomNumberGenerator(void)
{
unsigned seed = (unsigned) time(NULL);
const char* const env_var = getenv("RANDOM_SEED");
if (env_var != NULL)
{
seed = (unsigned) atoll(env_var); // Conversion errors are possible but ignored.
}
srand(seed);
(void) fprintf(stderr, "RANDOM_SEED=%u\n", seed);
}

#ifdef __cplusplus
}
#endif
6 changes: 5 additions & 1 deletion tests/src/test_misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/// SPDX-License-Identifier: MIT

#include <udpard.h>
#include "helpers.h"
#include "hexdump.hpp"
#include <unity.h>
#include <iostream>
Expand Down Expand Up @@ -67,7 +68,10 @@ void testGather()
}
} // namespace

void setUp() {}
void setUp()
{
seedRandomNumberGenerator(); // Re-seed the RNG for each test to avoid coupling.
}

void tearDown() {}

Expand Down

0 comments on commit 118a746

Please sign in to comment.