Skip to content

Commit 620c8d4

Browse files
committed
[test] Avoid seeding random number generators with time. NFC
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 f5956e1 commit 620c8d4

33 files changed

+91
-102
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",

0 commit comments

Comments
 (0)