Skip to content

Commit 3dff04c

Browse files
authored
[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.
1 parent 58d4441 commit 3dff04c

15 files changed

+73
-84
lines changed

test/benchmark/benchmark_utf16.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ unsigned short *randomString(int len) {
5454
}
5555

5656
int main() {
57-
srand(time(NULL));
5857
double t = 0;
5958
double t2 = emscripten_get_now();
6059
for(int i = 0; i < 10; ++i) {

test/benchmark/benchmark_utf8.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ char *randomString(int len) {
5454
}
5555

5656
int main() {
57-
time_t seed = time(NULL);
58-
printf("Random seed: %lld\n", seed);
59-
srand(seed);
6057
double t = 0;
6158
double t2 = emscripten_get_now();
6259
for (int i = 0; i < 100000; ++i) {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"a.html": 12686,
3-
"a.html.gz": 6930,
4-
"total": 12686,
5-
"total_gz": 6930
2+
"a.html": 12597,
3+
"a.html.gz": 6882,
4+
"total": 12597,
5+
"total_gz": 6882
66
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"a.html": 17266,
3-
"a.html.gz": 7515,
4-
"total": 17266,
5-
"total_gz": 7515
2+
"a.html": 17195,
3+
"a.html.gz": 7478,
4+
"total": 17195,
5+
"total_gz": 7478
66
}

test/core/test_emmalloc.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ void randoms() {
178178
for (int i = 0; i < BINS; i++) {
179179
bins[i] = NULL;
180180
}
181-
srandom(1337101);
182181
for (int i = 0; i < RANDOM_ITERS; i++) {
183182
unsigned int r = random();
184183
int alloc = r & 1;

test/core/test_rand.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stdlib.h>
2+
#include <stdbool.h>
3+
#include <stdio.h>
4+
#include <assert.h>
5+
6+
int main() {
7+
// We need RAND_MAX to be a bitmask (power of 2 minus 1). This assertion will
8+
// error if RAND_MAX ever changes, so we don't miss that.
9+
assert(RAND_MAX == 0x7fffffff);
10+
11+
srand(0xdeadbeef);
12+
for (int i = 0; i < 10; ++i) {
13+
printf("%d\n", rand());
14+
}
15+
16+
unsigned int seed = 0xdeadbeef;
17+
for (int i = 0; i < 10; ++i) {
18+
printf("%d\n", rand_r(&seed));
19+
}
20+
21+
bool haveEvenAndOdd = true;
22+
for (int i = 1; i <= 30; ++i) {
23+
int mask = 1 << i;
24+
if (mask > RAND_MAX) break;
25+
bool haveEven = false;
26+
bool haveOdd = false;
27+
for (int j = 0; j < 1000 && (!haveEven || !haveOdd); ++j) {
28+
if ((rand() & mask) == 0) {
29+
haveEven = true;
30+
} else {
31+
haveOdd = true;
32+
}
33+
}
34+
haveEvenAndOdd = haveEvenAndOdd && haveEven && haveOdd;
35+
}
36+
37+
if (haveEvenAndOdd) {
38+
printf("Have even and odd!\n");
39+
}
40+
41+
return 0;
42+
}

test/core/test_rand.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
490242850
2+
2074599277
3+
1480056542
4+
1912638067
5+
931112055
6+
2110392489
7+
2053422194
8+
1614832492
9+
216117595
10+
174823244
11+
760368382
12+
602359081
13+
1121118963
14+
1291018924
15+
1608306807
16+
352705809
17+
958258461
18+
1182561381
19+
114276303
20+
1481323674
21+
Have even and odd!

test/fetch/test_fetch_idb_store.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ int main()
3838
{
3939
// Create data
4040
uint8_t *data = (uint8_t*)malloc(10240);
41-
srand(time(NULL));
4241
for(int i = 0; i < 10240; ++i) data[i] = (uint8_t)rand();
4342

4443
persistFileToIndexedDB("outputfile.dat", data, 10240);

test/fetch/test_fetch_idb_store.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ int main()
2121
strcpy(attr.requestMethod, "EM_IDB_STORE");
2222
attr.attributes = EMSCRIPTEN_FETCH_REPLACE | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_PERSIST_FILE;
2323
uint8_t *data = (uint8_t*)malloc(TEST_SIZE);
24-
srand(time(NULL));
2524
for(int i = 0; i < TEST_SIZE; ++i)
2625
data[i] = (uint8_t)rand() | 0x40;
2726
attr.requestData = (char *)data;

test/hello_random_printf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <emscripten.h>
55

66
int main() {
7-
srand(time(NULL));
7+
srand(0);
88

99
printf("hello: a random string: %s, an integer: %d, a float: %f. Time now: %f\n",
1010
emscripten_random() > 0.5 ? "test" : "test2",

test/malloc_bench.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const bool USE_MEMORY = true;
2929
const bool USE_SHIFTS = false;
3030

3131
void randoms() {
32-
srandom(1);
3332
size_t before = (size_t)sbrk(0);
3433
double sum_sbrk = 0;
3534
size_t max_sbrk = before;

test/pthread/test_pthread_barrier.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ int main(int argc, char **argv)
5757

5858
// Create the matrix and compute the expected result.
5959
int expectedTotalSum = 0;
60-
srand(time(NULL));
6160
for(int i = 0; i < N; ++i)
6261
for(int j = 0; j < N; ++j)
6362
{

test/test_core.py

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -5155,68 +5155,7 @@ def test_random(self):
51555155
self.do_run(src, '956867869')
51565156

51575157
def test_rand(self):
5158-
src = r'''#include <stdlib.h>
5159-
#include <stdio.h>
5160-
#include <assert.h>
5161-
int main()
5162-
{
5163-
// we need RAND_MAX to be a bitmask (power of 2 minus 1). this assertions guarantees
5164-
// if RAND_MAX changes the test failure will focus attention on that issue here.
5165-
assert(RAND_MAX == 0x7fffffff);
5166-
5167-
srand(0xdeadbeef);
5168-
for(int i = 0; i < 10; ++i)
5169-
printf("%d\n", rand());
5170-
5171-
unsigned int seed = 0xdeadbeef;
5172-
for(int i = 0; i < 10; ++i)
5173-
printf("%d\n", rand_r(&seed));
5174-
5175-
bool haveEvenAndOdd = true;
5176-
for(int i = 1; i <= 30; ++i)
5177-
{
5178-
int mask = 1 << i;
5179-
if (mask > RAND_MAX) break;
5180-
bool haveEven = false;
5181-
bool haveOdd = false;
5182-
for(int j = 0; j < 1000 && (!haveEven || !haveOdd); ++j)
5183-
{
5184-
if ((rand() & mask) == 0)
5185-
haveEven = true;
5186-
else
5187-
haveOdd = true;
5188-
}
5189-
haveEvenAndOdd = haveEvenAndOdd && haveEven && haveOdd;
5190-
}
5191-
if (haveEvenAndOdd)
5192-
printf("Have even and odd!\n");
5193-
5194-
return 0;
5195-
}
5196-
'''
5197-
expected = '''490242850
5198-
2074599277
5199-
1480056542
5200-
1912638067
5201-
931112055
5202-
2110392489
5203-
2053422194
5204-
1614832492
5205-
216117595
5206-
174823244
5207-
760368382
5208-
602359081
5209-
1121118963
5210-
1291018924
5211-
1608306807
5212-
352705809
5213-
958258461
5214-
1182561381
5215-
114276303
5216-
1481323674
5217-
Have even and odd!
5218-
'''
5219-
self.do_run(src, expected)
5158+
self.do_core_test('test_rand.c')
52205159

52215160
def test_strtod(self):
52225161
self.do_core_test('test_strtod.c')

test/test_memset_alignment.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ void test_copysize(int copySize) {
5858
}
5959

6060
int main() {
61-
srand(time(NULL));
62-
6361
for (int copySize = 0; copySize < 128; ++copySize) {
6462
test_copysize(copySize);
6563
}

test/webaudio/audioworklet.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ void WebAudioWorkletThreadInitialized(EMSCRIPTEN_WEBAUDIO_T audioContext, bool s
124124
uint8_t wasmAudioWorkletStack[4096];
125125

126126
int main() {
127-
srand(time(NULL));
128-
129127
assert(!emscripten_current_thread_is_audio_worklet());
130128

131129
// Create an audio context

0 commit comments

Comments
 (0)