stdlib: implement rand_r() using XorShiftRng

This commit is contained in:
Alex Lyon
2019-04-25 19:35:32 -07:00
parent 6074616857
commit 13a10ce7af
3 changed files with 55 additions and 3 deletions
+36
View File
@@ -31,4 +31,40 @@ int main(void) {
puts("srand(1) doesn't work");
exit(EXIT_FAILURE);
}
// Ensure rand_r() fails with NULL input
if (rand_r(NULL) != EINVAL) {
puts("rand_r(NULL) doesn't return EINVAL");
exit(EXIT_FAILURE);
}
// Ensure rand_r() produces unique values
int seed = 259;
int rand_r_seed259_1 = rand_r((unsigned *)&seed);
printf("%d\n", rand_r_seed259_1);
int rand_r_seed259_2 = rand_r((unsigned *)&seed);
printf("%d\n", rand_r_seed259_2);
if (rand_r_seed259_1 == rand_r_seed259_2) {
puts("rand_r() fails to produce unique values");
exit(EXIT_FAILURE);
}
// Ensure rand_r() returns reproducible values
seed = 259;
int rand_r_seed259_1_2 = rand_r((unsigned *)&seed);
printf("%d\n", rand_r_seed259_1_2);
int rand_r_seed259_2_2 = rand_r((unsigned *)&seed);
printf("%d\n", rand_r_seed259_2_2);
if (rand_r_seed259_1 != rand_r_seed259_1_2
|| rand_r_seed259_2 != rand_r_seed259_2_2)
{
puts("rand_r() is not reproducible");
exit(EXIT_FAILURE);
}
return 0;
}