Skip to content

Commit

Permalink
[test] Avoid seeding random number generators with time. NFC (emscrip…
Browse files Browse the repository at this point in the history
…ten-core#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.
  • Loading branch information
sbc100 authored Dec 5, 2024
1 parent 58d4441 commit 3dff04c
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 84 deletions.
1 change: 0 additions & 1 deletion test/benchmark/benchmark_utf16.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions test/benchmark/benchmark_utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions test/code_size/random_printf_wasm.json
Original file line number Diff line number Diff line change
@@ -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
}
8 changes: 4 additions & 4 deletions test/code_size/random_printf_wasm2js.json
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 0 additions & 1 deletion test/core/test_emmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
42 changes: 42 additions & 0 deletions test/core/test_rand.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <assert.h>

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;
}
21 changes: 21 additions & 0 deletions test/core/test_rand.out
Original file line number Diff line number Diff line change
@@ -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!
1 change: 0 additions & 1 deletion test/fetch/test_fetch_idb_store.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion test/fetch/test_fetch_idb_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/hello_random_printf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <emscripten.h>

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",
Expand Down
1 change: 0 additions & 1 deletion test/malloc_bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion test/pthread/test_pthread_barrier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
63 changes: 1 addition & 62 deletions test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5155,68 +5155,7 @@ def test_random(self):
self.do_run(src, '956867869')

def test_rand(self):
src = r'''#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
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')
Expand Down
2 changes: 0 additions & 2 deletions test/test_memset_alignment.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ void test_copysize(int copySize) {
}

int main() {
srand(time(NULL));

for (int copySize = 0; copySize < 128; ++copySize) {
test_copysize(copySize);
}
Expand Down
2 changes: 0 additions & 2 deletions test/webaudio/audioworklet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3dff04c

Please sign in to comment.