Implements tests for randomness distributions.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
3e81784f6f
commit
5e71c59f77
|
@ -1,2 +1,3 @@
|
|||
/cmake-build-debug/
|
||||
/cmake-build-release/
|
||||
/.idea/
|
|
@ -1,6 +1,7 @@
|
|||
#ifdef TESTS_BUILD
|
||||
#include "../extern/catch.hpp"
|
||||
#include "../src/Core/Random.hpp"
|
||||
#include "../src/Core/Exceptions/CreatureException.hpp"
|
||||
|
||||
TEST_CASE( "Random ints", "[Utilities]" ) {
|
||||
auto rand = CreatureLib::Core::Random(10);
|
||||
|
@ -55,4 +56,62 @@ TEST_CASE( "Random ints with upper and bottom", "[Utilities]" ) {
|
|||
CHECK(rand.Get(10, 30) == 21);
|
||||
}
|
||||
|
||||
TEST_CASE( "Random distribution (max 0, min 1)", "[Utilities]" ) {
|
||||
auto rand = CreatureLib::Core::Random(10);
|
||||
const int size = 100000;
|
||||
int arr[size];
|
||||
for (size_t i = 0; i < size; i++){
|
||||
arr[i] = rand.Get(0, 1);
|
||||
}
|
||||
for (size_t i = 0; i < size; i++){
|
||||
if (arr[i] != 0)
|
||||
throw CreatureException("We expected a value of 0 here, but got a " + std::to_string(arr[i]));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE( "Random distribution (max 0, min 2)", "[Utilities]" ) {
|
||||
auto rand = CreatureLib::Core::Random(10);
|
||||
const int size = 100000;
|
||||
int arr[size];
|
||||
for (size_t i = 0; i < size; i++){
|
||||
arr[i] = rand.Get(0, 2);
|
||||
}
|
||||
auto numZeros = 0;
|
||||
auto numOnes = 0;
|
||||
for (size_t i = 0; i < size; i++){
|
||||
if (arr[i] != 0 && arr[i] != 1)
|
||||
throw CreatureException("We expected a value of 0 or 1 here, but got a " + std::to_string(arr[i]));
|
||||
if (arr[i] == 0) numZeros++;
|
||||
else numOnes++;
|
||||
}
|
||||
auto div = static_cast<float>(numZeros) / static_cast<float>(numOnes);
|
||||
INFO("Distribution: " << numZeros << "/" << numOnes);
|
||||
CHECK(Approx(div).margin(0.01) == 1);
|
||||
}
|
||||
|
||||
TEST_CASE( "Random distribution (max 0, min 3)", "[Utilities]" ) {
|
||||
auto rand = CreatureLib::Core::Random(10);
|
||||
const int size = 100000;
|
||||
int arr[size];
|
||||
for (size_t i = 0; i < size; i++){
|
||||
arr[i] = rand.Get(0, 3);
|
||||
}
|
||||
auto numZeros = 0;
|
||||
auto numOnes = 0;
|
||||
auto numTwos = 0;
|
||||
for (size_t i = 0; i < size; i++){
|
||||
if (arr[i] != 0 && arr[i] != 1 && arr[i] != 2)
|
||||
throw CreatureException("We expected a value between 0 and 2 here, but got a " + std::to_string(arr[i]));
|
||||
if (arr[i] == 0) numZeros++;
|
||||
else if (arr[i] == 1) numOnes++;
|
||||
else numTwos++;
|
||||
}
|
||||
INFO("Distribution: " << numZeros << "/" << numOnes << "/" << numTwos);
|
||||
CHECK(Approx(static_cast<float>(numZeros) / static_cast<float>(numOnes)).margin(0.01) == 1);
|
||||
CHECK(Approx(static_cast<float>(numZeros) / static_cast<float>(numTwos)).margin(0.01) == 1);
|
||||
CHECK(Approx(static_cast<float>(numOnes) / static_cast<float>(numTwos)).margin(0.01) == 1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue