From 0899be21c33b84766a36f940019c6ba5cc0ef902 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sun, 10 Oct 2021 09:36:35 +0200 Subject: [PATCH] Add support for seed (set/random) for rand --- tests/check-generic.hpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tests/check-generic.hpp b/tests/check-generic.hpp index c777ea13..d970d277 100644 --- a/tests/check-generic.hpp +++ b/tests/check-generic.hpp @@ -23,6 +23,7 @@ #include #include #include +#include /* This test will perform random generic operations on the M*LIB container and compares it to the same operation on the @@ -415,6 +416,27 @@ void test(int select_test, int index, assert(s0 == s1); } +static void init_rand(void) +{ + const char *seed = getenv("CHECK_RANDOMIZE"); + if (seed == NULL) { + // No check randomize requested. + // Initialize with a common value + srand(0x45856525U); + } else { + unsigned int seed_value = strtoul (seed, NULL, 10); + if (seed_value > 1) { + // Seed has been set by the user. Use it. + printf("Using seed = %u\n", seed_value); + } else { + time_t tv; + time (&tv); + seed_value = tv; + printf("Generating seed = %u\n", seed_value); + } + srand(seed_value); + } +} int main(int argc, const char *argv[]) { @@ -425,6 +447,7 @@ int main(int argc, const char *argv[]) CONT_TYPE c0; CONT_CLASS c1; + init_rand(); M_CALL_INIT(CONT_OPL, c0); M_CALL_INIT(M_GET_OPLIST CONT_OPL, b0); TYPE_SET_INT(b0, 0);