Skip to content

Commit 8e7df0e

Browse files
committed
tests: test another seed for rand()
1 parent 484e190 commit 8e7df0e

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

tests/stdlib.c

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -158,28 +158,41 @@ static int _mktemp(char const * progname)
158158

159159

160160
/* rand */
161+
static int _rand_test(unsigned int seed, const uint16_t * expected, unsigned int cnt);
162+
161163
static int _rand(char const * progname)
162164
{
163165
int ret = 0;
164166
const uint16_t expected1[10] = { 19533, 24984, 3136, 4047, 27914, 25471,
165167
17373, 7887, 7782, 20541 };
166-
uint16_t obtained[10];
167-
unsigned int i;
168+
const uint16_t expected0[10] = { 30440, 896, 24647, 22198, 11946, 311,
169+
28109, 32562, 13591, 7962 };
168170

169171
printf("%s: Testing rand()\n", progname);
170-
srand(1);
171-
for(i = 0; i < sizeof(expected1) / sizeof(*expected1); i++)
172+
if(_rand_test(1, expected1, sizeof(expected1) / sizeof(*expected1)) != 0
173+
|| _rand_test(0, expected0,
174+
sizeof(expected0) / sizeof(*expected0)) != 0)
172175
{
173-
obtained[i] = rand();
174-
printf("%d: %u (%u)\n", i, obtained[i], expected1[i]);
176+
errno = ERANGE;
177+
ret = _error(progname, "srand", 1);
178+
}
179+
return ret;
180+
}
181+
182+
static int _rand_test(unsigned int seed, const uint16_t * expected, unsigned int cnt)
183+
{
184+
int ret = 0;
185+
unsigned int i;
186+
uint16_t obtained;
187+
188+
srand(seed);
189+
for(i = 0; i < cnt; i++)
190+
{
191+
obtained = rand();
192+
printf("%u:%u: %u (%u)\n", seed, i, obtained, expected[i]);
193+
if(obtained != expected[i])
194+
ret = 1;
175195
}
176-
for(i = 0; i < sizeof(expected1) / sizeof(*expected1); i++)
177-
if(obtained[i] != expected1[i])
178-
{
179-
errno = ERANGE;
180-
ret = _error(progname, "srand", 1);
181-
break;
182-
}
183196
return ret;
184197
}
185198

0 commit comments

Comments
 (0)