From 3dff04cd868b95659989c36681378832fa9bc9b7 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Thu, 5 Dec 2024 13:34:43 -0800 Subject: [PATCH] [test] Avoid seeding random number generators with time. NFC (#23050) Using the current time to seed the random number generator in tests will make them non-determinisitic. Simply using the default seed or seeding with a fixed value seem fine all of these test cases. --- test/benchmark/benchmark_utf16.cpp | 1 - test/benchmark/benchmark_utf8.c | 3 -- test/code_size/random_printf_wasm.json | 8 +-- test/code_size/random_printf_wasm2js.json | 8 +-- test/core/test_emmalloc.c | 1 - test/core/test_rand.c | 42 +++++++++++++++ test/core/test_rand.out | 21 ++++++++ test/fetch/test_fetch_idb_store.c | 1 - test/fetch/test_fetch_idb_store.cpp | 1 - test/hello_random_printf.c | 2 +- test/malloc_bench.c | 1 - test/pthread/test_pthread_barrier.cpp | 1 - test/test_core.py | 63 +---------------------- test/test_memset_alignment.c | 2 - test/webaudio/audioworklet.c | 2 - 15 files changed, 73 insertions(+), 84 deletions(-) create mode 100644 test/core/test_rand.c create mode 100644 test/core/test_rand.out diff --git a/test/benchmark/benchmark_utf16.cpp b/test/benchmark/benchmark_utf16.cpp index ad17747a7964a..8d33d6129782e 100644 --- a/test/benchmark/benchmark_utf16.cpp +++ b/test/benchmark/benchmark_utf16.cpp @@ -54,7 +54,6 @@ unsigned short *randomString(int len) { } int main() { - srand(time(NULL)); double t = 0; double t2 = emscripten_get_now(); for(int i = 0; i < 10; ++i) { diff --git a/test/benchmark/benchmark_utf8.c b/test/benchmark/benchmark_utf8.c index 38a93b25902db..378a59e5ae673 100644 --- a/test/benchmark/benchmark_utf8.c +++ b/test/benchmark/benchmark_utf8.c @@ -54,9 +54,6 @@ char *randomString(int len) { } int main() { - time_t seed = time(NULL); - printf("Random seed: %lld\n", seed); - srand(seed); double t = 0; double t2 = emscripten_get_now(); for (int i = 0; i < 100000; ++i) { diff --git a/test/code_size/random_printf_wasm.json b/test/code_size/random_printf_wasm.json index 2a9f84036e46f..8548e41737392 100644 --- a/test/code_size/random_printf_wasm.json +++ b/test/code_size/random_printf_wasm.json @@ -1,6 +1,6 @@ { - "a.html": 12686, - "a.html.gz": 6930, - "total": 12686, - "total_gz": 6930 + "a.html": 12597, + "a.html.gz": 6882, + "total": 12597, + "total_gz": 6882 } diff --git a/test/code_size/random_printf_wasm2js.json b/test/code_size/random_printf_wasm2js.json index 7fa7980b7d20b..b3fe0c465524e 100644 --- a/test/code_size/random_printf_wasm2js.json +++ b/test/code_size/random_printf_wasm2js.json @@ -1,6 +1,6 @@ { - "a.html": 17266, - "a.html.gz": 7515, - "total": 17266, - "total_gz": 7515 + "a.html": 17195, + "a.html.gz": 7478, + "total": 17195, + "total_gz": 7478 } diff --git a/test/core/test_emmalloc.c b/test/core/test_emmalloc.c index 6af556beec5d1..58bf5aa1bb140 100644 --- a/test/core/test_emmalloc.c +++ b/test/core/test_emmalloc.c @@ -178,7 +178,6 @@ void randoms() { for (int i = 0; i < BINS; i++) { bins[i] = NULL; } - srandom(1337101); for (int i = 0; i < RANDOM_ITERS; i++) { unsigned int r = random(); int alloc = r & 1; diff --git a/test/core/test_rand.c b/test/core/test_rand.c new file mode 100644 index 0000000000000..cd5fccf5c47e6 --- /dev/null +++ b/test/core/test_rand.c @@ -0,0 +1,42 @@ +#include +#include +#include +#include + +int main() { + // We need RAND_MAX to be a bitmask (power of 2 minus 1). This assertion will + // error if RAND_MAX ever changes, so we don't miss that. + assert(RAND_MAX == 0x7fffffff); + + srand(0xdeadbeef); + for (int i = 0; i < 10; ++i) { + printf("%d\n", rand()); + } + + unsigned int seed = 0xdeadbeef; + for (int i = 0; i < 10; ++i) { + printf("%d\n", rand_r(&seed)); + } + + bool haveEvenAndOdd = true; + for (int i = 1; i <= 30; ++i) { + int mask = 1 << i; + if (mask > RAND_MAX) break; + bool haveEven = false; + bool haveOdd = false; + for (int j = 0; j < 1000 && (!haveEven || !haveOdd); ++j) { + if ((rand() & mask) == 0) { + haveEven = true; + } else { + haveOdd = true; + } + } + haveEvenAndOdd = haveEvenAndOdd && haveEven && haveOdd; + } + + if (haveEvenAndOdd) { + printf("Have even and odd!\n"); + } + + return 0; +} diff --git a/test/core/test_rand.out b/test/core/test_rand.out new file mode 100644 index 0000000000000..0cd26bd9c6bae --- /dev/null +++ b/test/core/test_rand.out @@ -0,0 +1,21 @@ +490242850 +2074599277 +1480056542 +1912638067 +931112055 +2110392489 +2053422194 +1614832492 +216117595 +174823244 +760368382 +602359081 +1121118963 +1291018924 +1608306807 +352705809 +958258461 +1182561381 +114276303 +1481323674 +Have even and odd! diff --git a/test/fetch/test_fetch_idb_store.c b/test/fetch/test_fetch_idb_store.c index 2ab526f5ee827..62f38e29bc9e8 100644 --- a/test/fetch/test_fetch_idb_store.c +++ b/test/fetch/test_fetch_idb_store.c @@ -38,7 +38,6 @@ int main() { // Create data uint8_t *data = (uint8_t*)malloc(10240); - srand(time(NULL)); for(int i = 0; i < 10240; ++i) data[i] = (uint8_t)rand(); persistFileToIndexedDB("outputfile.dat", data, 10240); diff --git a/test/fetch/test_fetch_idb_store.cpp b/test/fetch/test_fetch_idb_store.cpp index baa4ff22c5da2..46ff2f5f499de 100644 --- a/test/fetch/test_fetch_idb_store.cpp +++ b/test/fetch/test_fetch_idb_store.cpp @@ -21,7 +21,6 @@ int main() strcpy(attr.requestMethod, "EM_IDB_STORE"); attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_PERSIST_FILE; uint8_t *data = (uint8_t*)malloc(TEST_SIZE); - srand(time(NULL)); for(int i = 0; i < TEST_SIZE; ++i) data[i] = (uint8_t)rand() | 0x40; attr.requestData = (char *)data; diff --git a/test/hello_random_printf.c b/test/hello_random_printf.c index dbefa3333c356..87166bd13dc12 100644 --- a/test/hello_random_printf.c +++ b/test/hello_random_printf.c @@ -4,7 +4,7 @@ #include int main() { - srand(time(NULL)); + srand(0); printf("hello: a random string: %s, an integer: %d, a float: %f. Time now: %f\n", emscripten_random() > 0.5 ? "test" : "test2", diff --git a/test/malloc_bench.c b/test/malloc_bench.c index 91c536aeb7f21..078df4e47eedf 100644 --- a/test/malloc_bench.c +++ b/test/malloc_bench.c @@ -29,7 +29,6 @@ const bool USE_MEMORY = true; const bool USE_SHIFTS = false; void randoms() { - srandom(1); size_t before = (size_t)sbrk(0); double sum_sbrk = 0; size_t max_sbrk = before; diff --git a/test/pthread/test_pthread_barrier.cpp b/test/pthread/test_pthread_barrier.cpp index cb397a90a9eba..76201e7da1d07 100644 --- a/test/pthread/test_pthread_barrier.cpp +++ b/test/pthread/test_pthread_barrier.cpp @@ -57,7 +57,6 @@ int main(int argc, char **argv) // Create the matrix and compute the expected result. int expectedTotalSum = 0; - srand(time(NULL)); for(int i = 0; i < N; ++i) for(int j = 0; j < N; ++j) { diff --git a/test/test_core.py b/test/test_core.py index 521edb501411a..2163d969cffc1 100644 --- a/test/test_core.py +++ b/test/test_core.py @@ -5155,68 +5155,7 @@ def test_random(self): self.do_run(src, '956867869') def test_rand(self): - src = r'''#include -#include -#include -int main() -{ - // we need RAND_MAX to be a bitmask (power of 2 minus 1). this assertions guarantees - // if RAND_MAX changes the test failure will focus attention on that issue here. - assert(RAND_MAX == 0x7fffffff); - - srand(0xdeadbeef); - for(int i = 0; i < 10; ++i) - printf("%d\n", rand()); - - unsigned int seed = 0xdeadbeef; - for(int i = 0; i < 10; ++i) - printf("%d\n", rand_r(&seed)); - - bool haveEvenAndOdd = true; - for(int i = 1; i <= 30; ++i) - { - int mask = 1 << i; - if (mask > RAND_MAX) break; - bool haveEven = false; - bool haveOdd = false; - for(int j = 0; j < 1000 && (!haveEven || !haveOdd); ++j) - { - if ((rand() & mask) == 0) - haveEven = true; - else - haveOdd = true; - } - haveEvenAndOdd = haveEvenAndOdd && haveEven && haveOdd; - } - if (haveEvenAndOdd) - printf("Have even and odd!\n"); - - return 0; -} -''' - expected = '''490242850 -2074599277 -1480056542 -1912638067 -931112055 -2110392489 -2053422194 -1614832492 -216117595 -174823244 -760368382 -602359081 -1121118963 -1291018924 -1608306807 -352705809 -958258461 -1182561381 -114276303 -1481323674 -Have even and odd! -''' - self.do_run(src, expected) + self.do_core_test('test_rand.c') def test_strtod(self): self.do_core_test('test_strtod.c') diff --git a/test/test_memset_alignment.c b/test/test_memset_alignment.c index 8402f7661786b..c116a89a41a06 100644 --- a/test/test_memset_alignment.c +++ b/test/test_memset_alignment.c @@ -58,8 +58,6 @@ void test_copysize(int copySize) { } int main() { - srand(time(NULL)); - for (int copySize = 0; copySize < 128; ++copySize) { test_copysize(copySize); } diff --git a/test/webaudio/audioworklet.c b/test/webaudio/audioworklet.c index 4169475ff62b8..9b4bdd8a3dd60 100644 --- a/test/webaudio/audioworklet.c +++ b/test/webaudio/audioworklet.c @@ -124,8 +124,6 @@ void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, bool s uint8_t wasmAudioWorkletStack[4096]; int main() { - srand(time(NULL)); - assert(!emscripten_current_thread_is_audio_worklet()); // Create an audio context