diff --git a/test/helpers/random_helpers.cc b/test/helpers/random_helpers.cc index b60519db..d79475c7 100644 --- a/test/helpers/random_helpers.cc +++ b/test/helpers/random_helpers.cc @@ -1,22 +1,40 @@ #include #include -#include +#include +#include using std::string; using std::vector; +static std::default_random_engine engine; + +unsigned get_time_as_seed() { + return time(nullptr); +} + +void random_reseed(unsigned seed) { + engine.seed(seed); +} + +unsigned random_unsigned() { + return std::uniform_int_distribution()(engine); +} + +unsigned random_unsigned(unsigned max) { + return std::uniform_int_distribution(0, max - 1)(engine); +} + static string random_string(char min, char max) { string result; - size_t length = random() % 12; + size_t length = random_unsigned(12); for (size_t i = 0; i < length; i++) { - char inserted_char = min + (random() % (max - min)); - result += inserted_char; + result += (min + random_unsigned(max - min)); } return result; } static string random_char(string characters) { - size_t index = random() % characters.size(); + size_t index = random_unsigned(characters.size()); return string() + characters[index]; } @@ -24,7 +42,7 @@ string random_words(size_t count) { string result; bool just_inserted_word = false; for (size_t i = 0; i < count; i++) { - if (random() % 10 < 6) { + if (random_unsigned(10) < 6) { result += random_char("!(){}[]<>+-="); } else { if (just_inserted_word) @@ -37,5 +55,5 @@ string random_words(size_t count) { } string select_random(const vector &list) { - return list[random() % list.size()]; + return list[random_unsigned(list.size())]; } diff --git a/test/helpers/random_helpers.h b/test/helpers/random_helpers.h index 0c2cc290..b66c4aee 100644 --- a/test/helpers/random_helpers.h +++ b/test/helpers/random_helpers.h @@ -4,6 +4,10 @@ #include #include +unsigned get_time_as_seed(); +void random_reseed(unsigned); +unsigned random_unsigned(); +unsigned random_unsigned(unsigned max); std::string random_words(size_t count); std::string select_random(const std::vector &); diff --git a/test/integration/real_grammars.cc b/test/integration/real_grammars.cc index f494dcdc..a972aa27 100644 --- a/test/integration/real_grammars.cc +++ b/test/integration/real_grammars.cc @@ -75,9 +75,9 @@ for (auto &language_name : test_languages) { set> insertions; for (size_t i = 0; i < 60; i++) { - size_t edit_position = random() % utf8_char_count(entry.input); - size_t deletion_size = random() % (utf8_char_count(entry.input) - edit_position); - string inserted_text = random_words(random() % 4 + 1); + size_t edit_position = random_unsigned(utf8_char_count(entry.input)); + size_t deletion_size = random_unsigned(utf8_char_count(entry.input) - edit_position); + string inserted_text = random_words(random_unsigned(4) + 1); if (insertions.insert({edit_position, inserted_text}).second) { string description = "\"" + inserted_text + "\" at " + to_string(edit_position); diff --git a/test/tests.cc b/test/tests.cc index 2b7c35ba..cb9d6595 100644 --- a/test/tests.cc +++ b/test/tests.cc @@ -1,4 +1,5 @@ #include "test_helper.h" +#include "helpers/random_helpers.h" int main(int argc, char *argv[]) { int seed; @@ -6,11 +7,11 @@ int main(int argc, char *argv[]) { if (seed_env) { seed = atoi(seed_env); } else { - seed = time(nullptr); + seed = get_time_as_seed(); } printf("Random seed: %d\n", seed); - srandom(seed); + random_reseed(seed); return bandit::run(argc, argv); }