Skip to content

Commit 6ecc931

Browse files
committed
Do not call srand before each rand
It should only be called once, to initialize the seed. Otherwise, all values returned by rand() will be the same.
1 parent 9bf7a0d commit 6ecc931

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

test/malloc_compliance_tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ using umf_test::test;
3232
// Helpers
3333
//------------------------------------------------------------------------
3434

35-
static inline size_t rand_alloc_size(int max) {
36-
srand(SRAND_INIT_VALUE);
37-
return rand() % max;
38-
}
35+
static inline size_t rand_alloc_size(int max) { return rand() % max; }
3936

4037
static inline void free_memory(umf_memory_pool_handle_t hPool,
4138
void *ptr[ITERATIONS]) {
@@ -50,6 +47,7 @@ static inline void free_memory(umf_memory_pool_handle_t hPool,
5047

5148
// ISO/IEC 9899:TC3 7.20.3.3
5249
void malloc_compliance_test(umf_memory_pool_handle_t hPool) {
50+
srand(SRAND_INIT_VALUE);
5351
void *alloc_ptr[ITERATIONS];
5452
size_t alloc_ptr_size[ITERATIONS];
5553

@@ -80,6 +78,7 @@ void malloc_compliance_test(umf_memory_pool_handle_t hPool) {
8078

8179
// ISO/IEC 9899:TC3 7.20.3.1
8280
void calloc_compliance_test(umf_memory_pool_handle_t hPool) {
81+
srand(SRAND_INIT_VALUE);
8382
void *alloc_ptr[ITERATIONS];
8483
size_t alloc_size;
8584

@@ -98,6 +97,7 @@ void calloc_compliance_test(umf_memory_pool_handle_t hPool) {
9897

9998
// ISO/IEC 9899:TC3 7.20.3.4
10099
void realloc_compliance_test(umf_memory_pool_handle_t hPool) {
100+
srand(SRAND_INIT_VALUE);
101101
// If ptr is a null pointer, the realloc function behaves
102102
// like the malloc function for the specified size
103103
void *ret = umfPoolRealloc(hPool, NULL, 10);

0 commit comments

Comments
 (0)