stdlib: implement rand_r() using XorShiftRng
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user