Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ Checks: >
-readability-identifier-length,
-bugprone-easily-swappable-parameters,
-readability-magic-numbers,
-cppcoreguidelines-avoid-magic-numbers
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-readability-function-cognitive-complexity
WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none
Expand Down
44 changes: 24 additions & 20 deletions tests/test_LinearCongruentialGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,36 @@
#include <limits>
#include <cmath>

static constexpr uint64_t minval = std::numeric_limits<uint64_t>::min();
static constexpr uint64_t maxval = std::numeric_limits<uint64_t>::max();


// Test that the lcg constructors do not fail
TEST(TestLinearCongruentialGenerator, BlankConstructor) {
EXPECT_NO_THROW(LinearCongruentialGenerator());
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator());
}

TEST(TestLinearCongruentialGenerator, SeedConstructor) {
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::min()));
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::min() + 1));
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::max() / 10));
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::max() / 2));
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::max() - 1));
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::max()));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(minval));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(minval + 1));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(maxval / 10));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(maxval / 2));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(maxval - 1));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(maxval));
}

TEST(TestLinearCongruentialGenerator, CustomConstructor) {
EXPECT_NO_THROW(LinearCongruentialGenerator(0x7FFFFFFF, 1103515245, 12345, 0x7FFFFFFF));
EXPECT_NO_THROW(LinearCongruentialGenerator(60, 3453983, 897, 0xFFFFFFFF));
EXPECT_NO_THROW(LinearCongruentialGenerator(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF));
EXPECT_NO_THROW(LinearCongruentialGenerator(1, std::numeric_limits<uint64_t>::max() / 2, -50, 0xFFF));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(0x7FFFFFFF, 1103515245, 12345, 0x7FFFFFFF));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(60, 3453983, 897, 0xFFFFFFFF));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(1, maxval / 2, 50, 0xFFF));
}

TEST(TestLinearCongruentialGenerator, SeedAndCustomConstructor) {
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::min(), 0x7FFFFFFF, 1103515245, 12345, 0x7FFFFFFF));
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::max(), 60, 3453983, 897, 0xFFFFFFFF));
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::max() / 2, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF));
EXPECT_NO_THROW(LinearCongruentialGenerator(std::numeric_limits<uint64_t>::max() - 1, 1, std::numeric_limits<uint64_t>::max() / 2, 58678575, 0xFFFFFF));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(minval, 0x7FFFFFFF, 1103515245, 12345, 0x7FFFFFFF));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(maxval, 60, 3453983, 897, 0xFFFFFFFF));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(maxval / 2, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF));
EXPECT_NO_THROW(auto lcg = LinearCongruentialGenerator(maxval - 1, 1, maxval / 2, 58678575, 0xFFFFFF));
}

TEST(TestLinearCongruentialGenerator, GenerateUnitNormalRandomValueHasCorrectRange) {
Expand Down Expand Up @@ -66,8 +70,8 @@ TEST(TestLinearCongruentialGenerator, GenerateUnitNormalRandomValueEquidistribut

TEST(TestLinearCongruentialGenerator, GenerateFloatingPointRandomValueInRange){
LinearCongruentialGenerator lcg = LinearCongruentialGenerator();
const float min = -25.678f;
const float max = 3242.342f;
const float min = -25.678F;
const float max = 3242.342F;
const int num_iterations = 1000;
for (int i = 0; i < num_iterations; ++i){
double value = lcg.generateFloatingPointRandomValue(min, max);
Expand All @@ -78,8 +82,8 @@ TEST(TestLinearCongruentialGenerator, GenerateFloatingPointRandomValueInRange){

TEST(TestLinearCongruentialGenerator, GenerateFloatingPointRandomValueEquidistributed){
LinearCongruentialGenerator lcg = LinearCongruentialGenerator();
const double min = -300.0;
const double max = 700.0;
const float min = -300.0F;
const float max = 700.0F;
const int num_iterations = 10000;
double sum = 0.0;
double variance_sum = 0.0;
Expand All @@ -93,7 +97,7 @@ TEST(TestLinearCongruentialGenerator, GenerateFloatingPointRandomValueEquidistri
double variance = variance_sum / num_iterations;
double standard_deviation = std::sqrt(variance);
double three_sigma = 3 * standard_deviation;
ASSERT_NEAR(expected_value, average, three_sigma);;
ASSERT_NEAR(expected_value, average, three_sigma);

}

Expand Down
30 changes: 17 additions & 13 deletions tests/test_MersenneTwister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@
#include <limits>
#include <cmath>

static constexpr uint64_t minval = std::numeric_limits<uint64_t>::min();
static constexpr uint64_t maxval = std::numeric_limits<uint64_t>::max();


// Test that the mt constructors do not fail
TEST(TestMersenneTwister, BlankConstructor) {
EXPECT_NO_THROW(MersenneTwister());
EXPECT_NO_THROW(auto mt = MersenneTwister());
}

TEST(TestMersenneTwister, SeedConstructor) {
EXPECT_NO_THROW(MersenneTwister(std::numeric_limits<uint64_t>::min()));
EXPECT_NO_THROW(MersenneTwister(std::numeric_limits<uint64_t>::min() + 1));
EXPECT_NO_THROW(MersenneTwister(std::numeric_limits<uint64_t>::max() / 10));
EXPECT_NO_THROW(MersenneTwister(std::numeric_limits<uint64_t>::max() / 2));
EXPECT_NO_THROW(MersenneTwister(std::numeric_limits<uint64_t>::max() - 1));
EXPECT_NO_THROW(MersenneTwister(std::numeric_limits<uint64_t>::max()));
EXPECT_NO_THROW(auto mt = MersenneTwister(minval));
EXPECT_NO_THROW(auto mt = MersenneTwister(minval + 1));
EXPECT_NO_THROW(auto mt = MersenneTwister(maxval / 10));
EXPECT_NO_THROW(auto mt = MersenneTwister(maxval / 2));
EXPECT_NO_THROW(auto mt = MersenneTwister(maxval - 1));
EXPECT_NO_THROW(auto mt = MersenneTwister(maxval));
}

TEST(TestMersenneTwister, GenerateUnitNormalRandomValueHasCorrectRange) {
Expand Down Expand Up @@ -52,8 +56,8 @@ TEST(TestMersenneTwister, GenerateUnitNormalRandomValueEquidistributed) {

TEST(TestMersenneTwister, GenerateFloatingPointRandomValueInRange){
MersenneTwister mt = MersenneTwister();
const float min = -25.678f;
const float max = 3242.342f;
const float min = -25.678F;
const float max = 3242.342F;
const int num_iterations = 1000;
for (int i = 0; i < num_iterations; ++i){
double value = mt.generateFloatingPointRandomValue(min, max);
Expand All @@ -64,11 +68,11 @@ TEST(TestMersenneTwister, GenerateFloatingPointRandomValueInRange){

TEST(TestMersenneTwister, GenerateFloatingPointRandomValueEquidistributed){
MersenneTwister mt = MersenneTwister();
const double min = -300.0;
const double max = 700.0;
const float min = -300.0F;
const float max = 700.0F;
const int num_iterations = 10000;
double sum = 0.0;
double variance_sum = 0.0;
double sum = 0.0F;
double variance_sum = 0.0F;
double expected_value = (min + max) / 2.0;
for (int i = 0; i < num_iterations; ++i){
double value = mt.generateFloatingPointRandomValue(min, max);
Expand Down
41 changes: 30 additions & 11 deletions tests/test_ProgramRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,43 @@
constexpr const char* highest_i32_plus_one = "2147483648";
constexpr const char* lowest_i32_minus_one = "-2147483649";
constexpr const char* highest_u32_plus_one = "4294967296";
constexpr const char* highest_float_plus_more = "3.402823467e+38";
constexpr const char* lowest_float_minus_more = "-3.402823467e+38";

struct ArgvBuilder {
std::vector<std::string> args;
std::vector<char*> argv;
char **argv;

ArgvBuilder(std::initializer_list<std::string> init) : args(init) {
std::string program_name = "randomizer";
argv.push_back(const_cast<char*>(program_name.c_str()));
for (auto& s : args) {
argv.push_back(const_cast<char*>(s.c_str()));
std::string program_name = "this-should-not-matter";
args.emplace(args.begin(), program_name);
size_t null_terminator_arg_element = 1;
size_t all_args_size = args.size() + null_terminator_arg_element;
argv = new char*[all_args_size]; // NOLINT (cppcoreguidelines-owning-memory)

for (size_t i = 0; i < args.size(); ++i){
std::string this_arg = args[i];
size_t null_terminated_size = this_arg.size() + 1;
argv[i] = new char[null_terminated_size]; // NOLINT (cppcoreguidelines-owning-memory)
strncpy(argv[i], this_arg.c_str(), null_terminated_size);
argv[i][this_arg.size()] = '\0';
}
argv.push_back(nullptr); // argv must be null-terminated

argv[args.size()] = nullptr;
}

ArgvBuilder(const ArgvBuilder& other) = delete;
ArgvBuilder(ArgvBuilder&& other) = delete;
ArgvBuilder& operator=(const ArgvBuilder& other) = delete;
ArgvBuilder& operator=(const ArgvBuilder&& other) = delete;

~ArgvBuilder() {
for (size_t i = 0; i < args.size(); i++){
delete[] argv[i]; // NOLINT (cppcoreguidelines-owning-memory)
}
delete[] argv;
}

int argc() const { return static_cast<int>(argv.size() - 1); }
char** argv_ptr() { return argv.data(); }
int argc() const { return static_cast<int>(args.size()); }
char** argv_ptr() const { return argv; }
};

TEST(TestProgramRunner, NoArgs){
Expand Down Expand Up @@ -145,7 +164,7 @@ TEST(TestProgramRunner, ExplicitAlgorithmMersenne){
}

TEST(TestProgramRunner, ExplicitAlgorithmInvalid){
ArgvBuilder builder({"--algorithm", "invalid-algorithm", "--min", "0", "-M" "10", "--count", "5", "-t", "int"});
ArgvBuilder builder({"--algorithm", "invalid-algorithm", "--min", "0", "-M", "10", "--count", "5", "-t", "int"});
ProgramRunner program_runner = ProgramRunner(builder.argc(), builder.argv_ptr());
ProgramRunner::ProgramStatus status = program_runner.run();
ASSERT_TRUE(status.stderr_message.has_value());
Expand Down
42 changes: 23 additions & 19 deletions tests/test_XORShift.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,34 @@
#include <limits>
#include <cmath>

static constexpr uint64_t minval = std::numeric_limits<uint64_t>::min();
static constexpr uint64_t maxval = std::numeric_limits<uint64_t>::max();


// Test that the lcg constructors do not fail
TEST(TestXORShift, BlankConstructor) {
EXPECT_NO_THROW(XORShift());
EXPECT_NO_THROW(auto xsh = XORShift());
}

TEST(TestXORShift, SeedConstructor) {
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::min()));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::min() + 1));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::min() + 5));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::max() / 5));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::max() / 2));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::max() - 1));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::max()));
EXPECT_NO_THROW(auto xsh = XORShift(minval));
EXPECT_NO_THROW(auto xsh = XORShift(minval + 1));
EXPECT_NO_THROW(auto xsh = XORShift(minval + 5));
EXPECT_NO_THROW(auto xsh = XORShift(maxval / 5));
EXPECT_NO_THROW(auto xsh = XORShift(maxval / 2));
EXPECT_NO_THROW(auto xsh = XORShift(maxval - 1));
EXPECT_NO_THROW(auto xsh = XORShift(maxval));
}


TEST(TestXORShift, SeedAndConstantsConstructor) {
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::min(), 1, 2, 3));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::min() + 1, 79, 55555, std::numeric_limits<uint64_t>::max()));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::min() + 5, 4545, 9876567, 9876786));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::max() / 5, 1432, 6876, 3));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::max() / 2, 42, 42, 42));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::max() - 1, 2345678987, 9876543, 123422));
EXPECT_NO_THROW(XORShift(std::numeric_limits<uint64_t>::max(), 345679875, 2, 0));
EXPECT_NO_THROW(auto xsh = XORShift(minval, 1, 2, 3));
EXPECT_NO_THROW(auto xsh = XORShift(minval + 1, 79, 55555, maxval));
EXPECT_NO_THROW(auto xsh = XORShift(minval + 5, 4545, 9876567, 9876786));
EXPECT_NO_THROW(auto xsh = XORShift(maxval / 5, 1432, 6876, 3));
EXPECT_NO_THROW(auto xsh = XORShift(maxval / 2, 42, 42, 42));
EXPECT_NO_THROW(auto xsh = XORShift(maxval - 1, 2345678987, 9876543, 123422));
EXPECT_NO_THROW(auto xsh = XORShift(maxval, 345679875, 2, 0));
}


Expand Down Expand Up @@ -65,8 +69,8 @@ TEST(TestXORShift, GenerateUnitNormalRandomValueEquidistributed) {

TEST(TestXORShift, GenerateFloatingPointRandomValueInRange){
XORShift xor_shift = XORShift();
const float min = -25.678f;
const float max = 3242.342f;
const float min = -25.678F;
const float max = 3242.342F;
const int num_iterations = 1000;
for (int i = 0; i < num_iterations; ++i){
double value = xor_shift.generateFloatingPointRandomValue(min, max);
Expand All @@ -77,8 +81,8 @@ TEST(TestXORShift, GenerateFloatingPointRandomValueInRange){

TEST(TestXORShift, GenerateFloatingPointRandomValueEquidistributed){
XORShift xor_shift = XORShift();
const double min = -300.0;
const double max = 700.0;
const float min = -300.0F;
const float max = 700.0F;
const int num_iterations = 10000;
double sum = 0.0;
double variance_sum = 0.0;
Expand Down