Skip to content

Commit

Permalink
Avoid rand() in the legacy tr1 test suite (#4921)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanTLavavej committed Aug 28, 2024
1 parent 1acbeae commit 91e4255
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
18 changes: 11 additions & 7 deletions tests/tr1/tests/algorithm/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <algorithm>
#include <ctype.h>
#include <functional>
#include <random>
#include <string.h>

// FUNCTION OBJECTS
Expand Down Expand Up @@ -294,15 +295,13 @@ void test_copy(char* first, char* last, char* dest) { // test copying template f
CHECK_STR(array2, "aaxx");
}

CSTD size_t frand(CSTD size_t nmax) { // return random value in [0, nmax)
return CSTD rand() % nmax;
}

struct rand_gen { // uniform random number generator
STD mt19937 mt;

typedef CSTD size_t result_type;

result_type operator()() { // get random value
return CSTD rand() & 0xfffff;
return mt() & 0xfffff;
}

static result_type(min)() { // get minimum value
Expand Down Expand Up @@ -429,8 +428,13 @@ void test_mutate(char* first, char* last, char* dest) { // test mutating templat
CHECK_STR(array, "ebgf");

STD random_shuffle(first, last);
CSTD size_t (*prand)(CSTD size_t) = &frand;
STD random_shuffle(first, last, prand);

STD mt19937 mt;
auto rng_func = [&mt](CSTD size_t nmax) { // return random value in [0, nmax)
STD uniform_int_distribution<CSTD size_t> dist{0, nmax - 1};
return dist(mt);
};
STD random_shuffle(first, last, rng_func);

rand_gen urng;
STD shuffle(first, last, urng);
Expand Down
6 changes: 4 additions & 2 deletions tests/tr1/tests/memory2/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
#include "tdefs.h"
#include <memory>
#include <mutex>
#include <stdlib.h>
#include <random>
#include <thread>
#include <vector>

using STD shared_ptr;
using STD weak_ptr;

using STD lock_guard;
using STD mt19937;
using STD mutex;
using STD thread;
using STD vector;
Expand All @@ -27,14 +28,15 @@ static shared_ptr<int> sp;
static mutex start_mtx;

static void ctors() { // construct a gazillion shared and weak pointers
mt19937 mt;

{ // wait for access
lock_guard<mutex> lock(start_mtx);
}

for (int i = 0; i < NSETS; ++i) {
for (int j = 0; j < NREPS; ++j) {
if (rand() % 2 != 0) {
if (mt() % 2 != 0) {
shared_ptr<int> sp0(sp);
} else {
weak_ptr<int> wp0(sp);
Expand Down

0 comments on commit 91e4255

Please sign in to comment.