Browse Source

Added BENCH/TEST_PRNG, replacing other ad-hoc sources of randomness

When you add a function to every benchmark suite, you know if should
probably be provided by the benchmark runner itself. That being said,
randomness in tests/benchmarks is a bit tricky because it needs to be
strictly controlled and reproducible.

No global state is used, allowing tests/benches to maintain multiple
randomness stream which can be useful for checking results during a run.

There's an argument for having global prng state in that the prng could
be preserved across power-loss, but I have yet to see a use for this,
and it would add a significant requirement to any future test/bench runner.
Christopher Haster 3 years ago
parent
commit
b0382fa891

+ 10 - 24
benches/bench_dir.toml

@@ -1,17 +1,3 @@
-
-
-# deterministic prng
-code = '''
-static uint32_t xorshift32(uint32_t *state) {
-    uint32_t x = *state;
-    x ^= x << 13;
-    x ^= x >> 17;
-    x ^= x << 5;
-    *state = x;
-    return x;
-}
-'''
-
 [cases.bench_dir_open]
 # 0 = in-order
 # 1 = reversed-order
@@ -37,7 +23,7 @@ code = '''
         uint32_t file_prng = i;
         for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
             for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
-                buffer[k] = xorshift32(&file_prng);
+                buffer[k] = BENCH_PRNG(&file_prng);
             }
             lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
         }
@@ -52,7 +38,7 @@ code = '''
         lfs_off_t i_
             = (ORDER == 0) ? i
             : (ORDER == 1) ? (N-1-i)
-            : xorshift32(&prng) % N;
+            : BENCH_PRNG(&prng) % N;
         sprintf(name, "file%08x", i_);
         lfs_file_t file;
         lfs_file_open(&lfs, &file, name, LFS_O_RDONLY) => 0;
@@ -61,7 +47,7 @@ code = '''
         for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
             lfs_file_read(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
             for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
-                assert(buffer[k] == xorshift32(&file_prng));
+                assert(buffer[k] == BENCH_PRNG(&file_prng));
             }
         }
 
@@ -93,7 +79,7 @@ code = '''
         lfs_off_t i_
             = (ORDER == 0) ? i
             : (ORDER == 1) ? (N-1-i)
-            : xorshift32(&prng) % N;
+            : BENCH_PRNG(&prng) % N;
         sprintf(name, "file%08x", i_);
         lfs_file_t file;
         lfs_file_open(&lfs, &file, name,
@@ -102,7 +88,7 @@ code = '''
         uint32_t file_prng = i_;
         for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
             for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
-                buffer[k] = xorshift32(&file_prng);
+                buffer[k] = BENCH_PRNG(&file_prng);
             }
             lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
         }
@@ -139,7 +125,7 @@ code = '''
         uint32_t file_prng = i;
         for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
             for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
-                buffer[k] = xorshift32(&file_prng);
+                buffer[k] = BENCH_PRNG(&file_prng);
             }
             lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
         }
@@ -154,7 +140,7 @@ code = '''
         lfs_off_t i_
             = (ORDER == 0) ? i
             : (ORDER == 1) ? (N-1-i)
-            : xorshift32(&prng) % N;
+            : BENCH_PRNG(&prng) % N;
         sprintf(name, "file%08x", i_);
         int err = lfs_remove(&lfs, name);
         assert(!err || err == LFS_ERR_NOENT);
@@ -185,7 +171,7 @@ code = '''
         uint32_t file_prng = i;
         for (lfs_size_t j = 0; j < FILE_SIZE; j += CHUNK_SIZE) {
             for (lfs_size_t k = 0; k < CHUNK_SIZE; k++) {
-                buffer[k] = xorshift32(&file_prng);
+                buffer[k] = BENCH_PRNG(&file_prng);
             }
             lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
         }
@@ -235,7 +221,7 @@ code = '''
         lfs_off_t i_
             = (ORDER == 0) ? i
             : (ORDER == 1) ? (N-1-i)
-            : xorshift32(&prng) % N;
+            : BENCH_PRNG(&prng) % N;
         printf("hm %d\n", i);
         sprintf(name, "dir%08x", i_);
         int err = lfs_mkdir(&lfs, name);
@@ -271,7 +257,7 @@ code = '''
         lfs_off_t i_
             = (ORDER == 0) ? i
             : (ORDER == 1) ? (N-1-i)
-            : xorshift32(&prng) % N;
+            : BENCH_PRNG(&prng) % N;
         sprintf(name, "dir%08x", i_);
         int err = lfs_remove(&lfs, name);
         assert(!err || err == LFS_ERR_NOENT);

+ 5 - 19
benches/bench_file.toml

@@ -1,17 +1,3 @@
-
-
-# deterministic prng
-code = '''
-static uint32_t xorshift32(uint32_t *state) {
-    uint32_t x = *state;
-    x ^= x << 13;
-    x ^= x >> 17;
-    x ^= x << 5;
-    *state = x;
-    return x;
-}
-'''
-
 [cases.bench_file_read]
 # 0 = in-order
 # 1 = reversed-order
@@ -33,7 +19,7 @@ code = '''
     for (lfs_size_t i = 0; i < chunks; i++) {
         uint32_t chunk_prng = i;
         for (lfs_size_t j = 0; j < CHUNK_SIZE; j++) {
-            buffer[j] = xorshift32(&chunk_prng);
+            buffer[j] = BENCH_PRNG(&chunk_prng);
         }
 
         lfs_file_write(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
@@ -50,14 +36,14 @@ code = '''
         lfs_off_t i_
             = (ORDER == 0) ? i
             : (ORDER == 1) ? (chunks-1-i)
-            : xorshift32(&prng) % chunks;
+            : BENCH_PRNG(&prng) % chunks;
         lfs_file_seek(&lfs, &file, i_*CHUNK_SIZE, LFS_SEEK_SET)
                 => i_*CHUNK_SIZE;
         lfs_file_read(&lfs, &file, buffer, CHUNK_SIZE) => CHUNK_SIZE;
 
         uint32_t chunk_prng = i_;
         for (lfs_size_t j = 0; j < CHUNK_SIZE; j++) {
-            assert(buffer[j] == xorshift32(&chunk_prng));
+            assert(buffer[j] == BENCH_PRNG(&chunk_prng));
         }
     }
 
@@ -91,10 +77,10 @@ code = '''
         lfs_off_t i_
             = (ORDER == 0) ? i
             : (ORDER == 1) ? (chunks-1-i)
-            : xorshift32(&prng) % chunks;
+            : BENCH_PRNG(&prng) % chunks;
         uint32_t chunk_prng = i_;
         for (lfs_size_t j = 0; j < CHUNK_SIZE; j++) {
-            buffer[j] = xorshift32(&chunk_prng);
+            buffer[j] = BENCH_PRNG(&chunk_prng);
         }
 
         lfs_file_seek(&lfs, &file, i_*CHUNK_SIZE, LFS_SEEK_SET)

+ 13 - 0
runners/bench_runner.c

@@ -529,6 +529,19 @@ void bench_trace(const char *fmt, ...) {
 }
 
 
+// bench prng
+uint32_t bench_prng(uint32_t *state) {
+    // A simple xorshift32 generator, easily reproducible. Keep in mind
+    // determinism is much more important than actual randomness here.
+    uint32_t x = *state;
+    x ^= x << 13;
+    x ^= x >> 17;
+    x ^= x << 5;
+    *state = x;
+    return x;
+}
+
+
 // bench recording state
 static struct lfs_config *bench_cfg = NULL;
 static lfs_emubd_io_t bench_last_readed = 0;

+ 6 - 0
runners/bench_runner.h

@@ -74,6 +74,12 @@ struct bench_suite {
 };
 
 
+// deterministic prng for pseudo-randomness in benches
+uint32_t bench_prng(uint32_t *state);
+
+#define BENCH_PRNG(state) bench_prng(state)
+
+
 // access generated bench defines
 intmax_t bench_define(size_t define);
 

+ 13 - 0
runners/test_runner.c

@@ -545,6 +545,19 @@ void test_trace(const char *fmt, ...) {
 }
 
 
+// test prng
+uint32_t test_prng(uint32_t *state) {
+    // A simple xorshift32 generator, easily reproducible. Keep in mind
+    // determinism is much more important than actual randomness here.
+    uint32_t x = *state;
+    x ^= x << 13;
+    x ^= x >> 17;
+    x ^= x << 5;
+    *state = x;
+    return x;
+}
+
+
 // encode our permutation into a reusable id
 static void perm_printid(
         const struct test_suite *suite,

+ 6 - 0
runners/test_runner.h

@@ -67,6 +67,12 @@ struct test_suite {
 };
 
 
+// deterministic prng for pseudo-randomness in testes
+uint32_t test_prng(uint32_t *state);
+
+#define TEST_PRNG(state) test_prng(state)
+
+
 // access generated test defines
 intmax_t test_define(size_t define);
 

+ 30 - 30
tests/test_exhaustion.toml

@@ -25,15 +25,15 @@ code = '''
             // chose name, roughly random seed, and random 2^n size
             char path[1024];
             sprintf(path, "roadrunner/test%d", i);
-            srand(cycle * i);
-            lfs_size_t size = 1 << ((rand() % 10)+2);
+            uint32_t prng = cycle * i;
+            lfs_size_t size = 1 << ((TEST_PRNG(&prng) % 10)+2);
 
             lfs_file_t file;
             lfs_file_open(&lfs, &file, path,
                     LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
 
             for (lfs_size_t j = 0; j < size; j++) {
-                char c = 'a' + (rand() % 26);
+                char c = 'a' + (TEST_PRNG(&prng) % 26);
                 lfs_ssize_t res = lfs_file_write(&lfs, &file, &c, 1);
                 assert(res == 1 || res == LFS_ERR_NOSPC);
                 if (res == LFS_ERR_NOSPC) {
@@ -56,13 +56,13 @@ code = '''
             // check for errors
             char path[1024];
             sprintf(path, "roadrunner/test%d", i);
-            srand(cycle * i);
-            lfs_size_t size = 1 << ((rand() % 10)+2);
+            uint32_t prng = cycle * i;
+            lfs_size_t size = 1 << ((TEST_PRNG(&prng) % 10)+2);
 
             lfs_file_t file;
             lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
             for (lfs_size_t j = 0; j < size; j++) {
-                char c = 'a' + (rand() % 26);
+                char c = 'a' + (TEST_PRNG(&prng) % 26);
                 char r;
                 lfs_file_read(&lfs, &file, &r, 1) => 1;
                 assert(r == c);
@@ -115,15 +115,15 @@ code = '''
             // chose name, roughly random seed, and random 2^n size
             char path[1024];
             sprintf(path, "test%d", i);
-            srand(cycle * i);
-            lfs_size_t size = 1 << ((rand() % 10)+2);
+            uint32_t prng = cycle * i;
+            lfs_size_t size = 1 << ((TEST_PRNG(&prng) % 10)+2);
 
             lfs_file_t file;
             lfs_file_open(&lfs, &file, path,
                     LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
 
             for (lfs_size_t j = 0; j < size; j++) {
-                char c = 'a' + (rand() % 26);
+                char c = 'a' + (TEST_PRNG(&prng) % 26);
                 lfs_ssize_t res = lfs_file_write(&lfs, &file, &c, 1);
                 assert(res == 1 || res == LFS_ERR_NOSPC);
                 if (res == LFS_ERR_NOSPC) {
@@ -146,13 +146,13 @@ code = '''
             // check for errors
             char path[1024];
             sprintf(path, "test%d", i);
-            srand(cycle * i);
-            lfs_size_t size = 1 << ((rand() % 10)+2);
+            uint32_t prng = cycle * i;
+            lfs_size_t size = 1 << ((TEST_PRNG(&prng) % 10)+2);
 
             lfs_file_t file;
             lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
             for (lfs_size_t j = 0; j < size; j++) {
-                char c = 'a' + (rand() % 26);
+                char c = 'a' + (TEST_PRNG(&prng) % 26);
                 char r;
                 lfs_file_read(&lfs, &file, &r, 1) => 1;
                 assert(r == c);
@@ -214,15 +214,15 @@ code = '''
                 // chose name, roughly random seed, and random 2^n size
                 char path[1024];
                 sprintf(path, "roadrunner/test%d", i);
-                srand(cycle * i);
-                lfs_size_t size = 1 << ((rand() % 10)+2);
+                uint32_t prng = cycle * i;
+                lfs_size_t size = 1 << ((TEST_PRNG(&prng) % 10)+2);
 
                 lfs_file_t file;
                 lfs_file_open(&lfs, &file, path,
                         LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
 
                 for (lfs_size_t j = 0; j < size; j++) {
-                    char c = 'a' + (rand() % 26);
+                    char c = 'a' + (TEST_PRNG(&prng) % 26);
                     lfs_ssize_t res = lfs_file_write(&lfs, &file, &c, 1);
                     assert(res == 1 || res == LFS_ERR_NOSPC);
                     if (res == LFS_ERR_NOSPC) {
@@ -245,13 +245,13 @@ code = '''
                 // check for errors
                 char path[1024];
                 sprintf(path, "roadrunner/test%d", i);
-                srand(cycle * i);
-                lfs_size_t size = 1 << ((rand() % 10)+2);
+                uint32_t prng = cycle * i;
+                lfs_size_t size = 1 << ((TEST_PRNG(&prng) % 10)+2);
 
                 lfs_file_t file;
                 lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
                 for (lfs_size_t j = 0; j < size; j++) {
-                    char c = 'a' + (rand() % 26);
+                    char c = 'a' + (TEST_PRNG(&prng) % 26);
                     char r;
                     lfs_file_read(&lfs, &file, &r, 1) => 1;
                     assert(r == c);
@@ -311,15 +311,15 @@ code = '''
                 // chose name, roughly random seed, and random 2^n size
                 char path[1024];
                 sprintf(path, "test%d", i);
-                srand(cycle * i);
-                lfs_size_t size = 1 << ((rand() % 10)+2);
+                uint32_t prng = cycle * i;
+                lfs_size_t size = 1 << ((TEST_PRNG(&prng) % 10)+2);
 
                 lfs_file_t file;
                 lfs_file_open(&lfs, &file, path,
                         LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
 
                 for (lfs_size_t j = 0; j < size; j++) {
-                    char c = 'a' + (rand() % 26);
+                    char c = 'a' + (TEST_PRNG(&prng) % 26);
                     lfs_ssize_t res = lfs_file_write(&lfs, &file, &c, 1);
                     assert(res == 1 || res == LFS_ERR_NOSPC);
                     if (res == LFS_ERR_NOSPC) {
@@ -342,13 +342,13 @@ code = '''
                 // check for errors
                 char path[1024];
                 sprintf(path, "test%d", i);
-                srand(cycle * i);
-                lfs_size_t size = 1 << ((rand() % 10)+2);
+                uint32_t prng = cycle * i;
+                lfs_size_t size = 1 << ((TEST_PRNG(&prng) % 10)+2);
 
                 lfs_file_t file;
                 lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
                 for (lfs_size_t j = 0; j < size; j++) {
-                    char c = 'a' + (rand() % 26);
+                    char c = 'a' + (TEST_PRNG(&prng) % 26);
                     char r;
                     lfs_file_read(&lfs, &file, &r, 1) => 1;
                     assert(r == c);
@@ -404,15 +404,15 @@ code = '''
             // chose name, roughly random seed, and random 2^n size
             char path[1024];
             sprintf(path, "roadrunner/test%d", i);
-            srand(cycle * i);
-            lfs_size_t size = 1 << 4; //((rand() % 10)+2);
+            uint32_t prng = cycle * i;
+            lfs_size_t size = 1 << 4; //((TEST_PRNG(&prng) % 10)+2);
 
             lfs_file_t file;
             lfs_file_open(&lfs, &file, path,
                     LFS_O_WRONLY | LFS_O_CREAT | LFS_O_TRUNC) => 0;
 
             for (lfs_size_t j = 0; j < size; j++) {
-                char c = 'a' + (rand() % 26);
+                char c = 'a' + (TEST_PRNG(&prng) % 26);
                 lfs_ssize_t res = lfs_file_write(&lfs, &file, &c, 1);
                 assert(res == 1 || res == LFS_ERR_NOSPC);
                 if (res == LFS_ERR_NOSPC) {
@@ -435,13 +435,13 @@ code = '''
             // check for errors
             char path[1024];
             sprintf(path, "roadrunner/test%d", i);
-            srand(cycle * i);
-            lfs_size_t size = 1 << 4; //((rand() % 10)+2);
+            uint32_t prng = cycle * i;
+            lfs_size_t size = 1 << 4; //((TEST_PRNG(&prng) % 10)+2);
 
             lfs_file_t file;
             lfs_file_open(&lfs, &file, path, LFS_O_RDONLY) => 0;
             for (lfs_size_t j = 0; j < size; j++) {
-                char c = 'a' + (rand() % 26);
+                char c = 'a' + (TEST_PRNG(&prng) % 26);
                 char r;
                 lfs_file_read(&lfs, &file, &r, 1) => 1;
                 assert(r == c);

+ 44 - 44
tests/test_files.toml

@@ -34,12 +34,12 @@ code = '''
     lfs_file_t file;
     lfs_file_open(&lfs, &file, "avacado",
             LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
-    srand(1);
+    uint32_t prng = 1;
     uint8_t buffer[1024];
     for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
         for (lfs_size_t b = 0; b < chunk; b++) {
-            buffer[b] = rand() & 0xff;
+            buffer[b] = TEST_PRNG(&prng) & 0xff;
         }
         lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
     }
@@ -50,12 +50,12 @@ code = '''
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
     lfs_file_size(&lfs, &file) => SIZE;
-    srand(1);
+    prng = 1;
     for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
     lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
@@ -77,11 +77,11 @@ code = '''
     uint8_t buffer[1024];
     lfs_file_open(&lfs, &file, "avacado",
             LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
-    srand(1);
+    uint32_t prng = 1;
     for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
         for (lfs_size_t b = 0; b < chunk; b++) {
-            buffer[b] = rand() & 0xff;
+            buffer[b] = TEST_PRNG(&prng) & 0xff;
         }
         lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
     }
@@ -92,12 +92,12 @@ code = '''
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
     lfs_file_size(&lfs, &file) => SIZE1;
-    srand(1);
+    prng = 1;
     for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
     lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
@@ -107,11 +107,11 @@ code = '''
     // rewrite
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_WRONLY) => 0;
-    srand(2);
+    prng = 2;
     for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
         for (lfs_size_t b = 0; b < chunk; b++) {
-            buffer[b] = rand() & 0xff;
+            buffer[b] = TEST_PRNG(&prng) & 0xff;
         }
         lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
     }
@@ -122,24 +122,24 @@ code = '''
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
     lfs_file_size(&lfs, &file) => lfs_max(SIZE1, SIZE2);
-    srand(2);
+    prng = 2;
     for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
     if (SIZE1 > SIZE2) {
-        srand(1);
+        prng = 1;
         for (lfs_size_t b = 0; b < SIZE2; b++) {
-            rand();
+            TEST_PRNG(&prng);
         }
         for (lfs_size_t i = SIZE2; i < SIZE1; i += CHUNKSIZE) {
             lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
             lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
             for (lfs_size_t b = 0; b < chunk; b++) {
-                assert(buffer[b] == (rand() & 0xff));
+                assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
             }
         }
     }
@@ -162,11 +162,11 @@ code = '''
     uint8_t buffer[1024];
     lfs_file_open(&lfs, &file, "avacado",
             LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
-    srand(1);
+    uint32_t prng = 1;
     for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
         for (lfs_size_t b = 0; b < chunk; b++) {
-            buffer[b] = rand() & 0xff;
+            buffer[b] = TEST_PRNG(&prng) & 0xff;
         }
         lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
     }
@@ -177,12 +177,12 @@ code = '''
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
     lfs_file_size(&lfs, &file) => SIZE1;
-    srand(1);
+    prng = 1;
     for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
     lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
@@ -192,11 +192,11 @@ code = '''
     // append
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_WRONLY | LFS_O_APPEND) => 0;
-    srand(2);
+    prng = 2;
     for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
         for (lfs_size_t b = 0; b < chunk; b++) {
-            buffer[b] = rand() & 0xff;
+            buffer[b] = TEST_PRNG(&prng) & 0xff;
         }
         lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
     }
@@ -207,20 +207,20 @@ code = '''
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
     lfs_file_size(&lfs, &file) => SIZE1 + SIZE2;
-    srand(1);
+    prng = 1;
     for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
-    srand(2);
+    prng = 2;
     for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
     lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
@@ -242,11 +242,11 @@ code = '''
     uint8_t buffer[1024];
     lfs_file_open(&lfs, &file, "avacado",
             LFS_O_WRONLY | LFS_O_CREAT | LFS_O_EXCL) => 0;
-    srand(1);
+    uint32_t prng = 1;
     for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
         for (lfs_size_t b = 0; b < chunk; b++) {
-            buffer[b] = rand() & 0xff;
+            buffer[b] = TEST_PRNG(&prng) & 0xff;
         }
         lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
     }
@@ -257,12 +257,12 @@ code = '''
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
     lfs_file_size(&lfs, &file) => SIZE1;
-    srand(1);
+    prng = 1;
     for (lfs_size_t i = 0; i < SIZE1; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE1-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
     lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
@@ -272,11 +272,11 @@ code = '''
     // truncate
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_WRONLY | LFS_O_TRUNC) => 0;
-    srand(2);
+    prng = 2;
     for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
         for (lfs_size_t b = 0; b < chunk; b++) {
-            buffer[b] = rand() & 0xff;
+            buffer[b] = TEST_PRNG(&prng) & 0xff;
         }
         lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
     }
@@ -287,12 +287,12 @@ code = '''
     lfs_mount(&lfs, cfg) => 0;
     lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
     lfs_file_size(&lfs, &file) => SIZE2;
-    srand(2);
+    prng = 2;
     for (lfs_size_t i = 0; i < SIZE2; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE2-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
     lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
@@ -325,11 +325,11 @@ code = '''
 
     // write
     lfs_file_open(&lfs, &file, "avacado", LFS_O_WRONLY | LFS_O_CREAT) => 0;
-    srand(1);
+    uint32_t prng = 1;
     for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
         for (lfs_size_t b = 0; b < chunk; b++) {
-            buffer[b] = rand() & 0xff;
+            buffer[b] = TEST_PRNG(&prng) & 0xff;
         }
         lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
     }
@@ -338,12 +338,12 @@ code = '''
     // read
     lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
     lfs_file_size(&lfs, &file) => SIZE;
-    srand(1);
+    prng = 1;
     for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
     lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;
@@ -377,12 +377,12 @@ code = '''
         // with syncs we could be any size, but it at least must be valid data
         lfs_size_t size = lfs_file_size(&lfs, &file);
         assert(size <= SIZE);
-        srand(1);
+        uint32_t prng = 1;
         for (lfs_size_t i = 0; i < size; i += CHUNKSIZE) {
             lfs_size_t chunk = lfs_min(CHUNKSIZE, size-i);
             lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
             for (lfs_size_t b = 0; b < chunk; b++) {
-                assert(buffer[b] == (rand() & 0xff));
+                assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
             }
         }
         lfs_file_close(&lfs, &file) => 0;
@@ -393,15 +393,15 @@ code = '''
         LFS_O_WRONLY | LFS_O_CREAT | MODE) => 0;
     lfs_size_t size = lfs_file_size(&lfs, &file);
     assert(size <= SIZE);
-    srand(1);
+    uint32_t prng = 1;
     lfs_size_t skip = (MODE == LFS_O_APPEND) ? size : 0;
     for (lfs_size_t b = 0; b < skip; b++) {
-        rand();
+        TEST_PRNG(&prng);
     }
     for (lfs_size_t i = skip; i < SIZE; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
         for (lfs_size_t b = 0; b < chunk; b++) {
-            buffer[b] = rand() & 0xff;
+            buffer[b] = TEST_PRNG(&prng) & 0xff;
         }
         lfs_file_write(&lfs, &file, buffer, chunk) => chunk;
         lfs_file_sync(&lfs, &file) => 0;
@@ -411,12 +411,12 @@ code = '''
     // read
     lfs_file_open(&lfs, &file, "avacado", LFS_O_RDONLY) => 0;
     lfs_file_size(&lfs, &file) => SIZE;
-    srand(1);
+    prng = 1;
     for (lfs_size_t i = 0; i < SIZE; i += CHUNKSIZE) {
         lfs_size_t chunk = lfs_min(CHUNKSIZE, SIZE-i);
         lfs_file_read(&lfs, &file, buffer, chunk) => chunk;
         for (lfs_size_t b = 0; b < chunk; b++) {
-            assert(buffer[b] == (rand() & 0xff));
+            assert(buffer[b] == (TEST_PRNG(&prng) & 0xff));
         }
     }
     lfs_file_read(&lfs, &file, buffer, CHUNKSIZE) => 0;

+ 2 - 2
tests/test_orphans.toml

@@ -77,13 +77,13 @@ code = '''
         lfs_mount(&lfs, cfg) => 0;
     }
 
-    srand(1);
+    uint32_t prng = 1;
     const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
     for (unsigned i = 0; i < CYCLES; i++) {
         // create random path
         char full_path[256];
         for (unsigned d = 0; d < DEPTH; d++) {
-            sprintf(&full_path[2*d], "/%c", alpha[rand() % FILES]);
+            sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
         }
 
         // if it does not exist, we create it, else we destroy

+ 5 - 5
tests/test_relocations.toml

@@ -186,13 +186,13 @@ code = '''
         lfs_mount(&lfs, cfg) => 0;
     }
 
-    srand(1);
+    uint32_t prng = 1;
     const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
     for (unsigned i = 0; i < CYCLES; i++) {
         // create random path
         char full_path[256];
         for (unsigned d = 0; d < DEPTH; d++) {
-            sprintf(&full_path[2*d], "/%c", alpha[rand() % FILES]);
+            sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
         }
 
         // if it does not exist, we create it, else we destroy
@@ -255,13 +255,13 @@ code = '''
         lfs_mount(&lfs, cfg) => 0;
     }
 
-    srand(1);
+    uint32_t prng = 1;
     const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
     for (unsigned i = 0; i < CYCLES; i++) {
         // create random path
         char full_path[256];
         for (unsigned d = 0; d < DEPTH; d++) {
-            sprintf(&full_path[2*d], "/%c", alpha[rand() % FILES]);
+            sprintf(&full_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
         }
 
         // if it does not exist, we create it, else we destroy
@@ -293,7 +293,7 @@ code = '''
             // create new random path
             char new_path[256];
             for (unsigned d = 0; d < DEPTH; d++) {
-                sprintf(&new_path[2*d], "/%c", alpha[rand() % FILES]);
+                sprintf(&new_path[2*d], "/%c", alpha[TEST_PRNG(&prng) % FILES]);
             }
 
             // if new path does not exist, rename, otherwise destroy