133 lines
3.1 KiB
C++
133 lines
3.1 KiB
C++
#include <J3ML/Algorithm/RNG.h>
|
|
#include <jtest/jtest.hpp>
|
|
using J3ML::Algorithm::RNG;
|
|
|
|
int RNGTests()
|
|
{
|
|
TEST("RNG::IntFast", []{
|
|
RNG rng;
|
|
u32 prev = rng.IntFast();
|
|
|
|
for (int i = 0; i < 1000; ++i)
|
|
{
|
|
u32 next = rng.IntFast();
|
|
jtest::check(next != prev);
|
|
prev = next;
|
|
}
|
|
});
|
|
|
|
TEST("RNG::Int", []{
|
|
RNG rng;
|
|
assert(rng.lastNumber != 0 || rng.increment != 0);
|
|
bool allEqual = true;
|
|
for (int i = 0; i < 1000; ++i)
|
|
{
|
|
int prev = rng.Int();
|
|
int next = rng.Int();
|
|
jtest::check(prev != 0 || next != 0);
|
|
if (prev != next)
|
|
allEqual = false;
|
|
}
|
|
jtest::check(!allEqual);
|
|
|
|
});
|
|
|
|
TEST("Rng::Int_A_B", []{
|
|
RNG rng;
|
|
for (int i = 0; i < 1000; ++i)
|
|
{
|
|
int a = rng.Int();
|
|
int b = rng.Int();
|
|
if (b < a)
|
|
Swap(a, b);
|
|
int val = rng.Int(a, b);
|
|
jtest::check( a <= val);
|
|
jtest::check(val <= b);
|
|
}
|
|
});
|
|
|
|
TEST("Rng::Float", []{
|
|
RNG rng;
|
|
bool allEqual = true;
|
|
for (int i = 0; i < 1000; ++i)
|
|
{
|
|
float f = rng.Float();
|
|
float f2 = rng.Float();
|
|
jtest::check(f < 1.f);
|
|
jtest::check(f >= 0.f);
|
|
jtest::check(f != 0.f || f2 != 0.f);
|
|
|
|
if (f != f2)
|
|
allEqual = false;
|
|
}
|
|
jtest::check(!allEqual);
|
|
});
|
|
|
|
TEST("Rng::Float01Incl", []{
|
|
RNG rng;
|
|
bool allEqual = true;
|
|
for (int i = 0; i < 1000; ++i)
|
|
{
|
|
float f = rng.Float01Incl();
|
|
float f2 = rng.Float01Incl();
|
|
|
|
jtest::check(f <= 1.f);
|
|
jtest::check(f >= 0.f);
|
|
jtest::check(f != 0.f || f2 != 0.f);
|
|
if (f != f2)
|
|
allEqual = false;
|
|
}
|
|
jtest::check(!allEqual);
|
|
});
|
|
|
|
TEST("Rng::FloatNeg1_1", []{
|
|
RNG rng;
|
|
bool allEqual = true;
|
|
for (int i = 0; i < 1000; ++i)
|
|
{
|
|
float f = rng.FloatNeg1_1();
|
|
float f2 = rng.FloatNeg1_1();
|
|
jtest::check(f < 1.f);
|
|
jtest::check(f > -1.f);
|
|
jtest::check(f != 0.f || f2 != 0.f);
|
|
if (f != f2)
|
|
allEqual = false;
|
|
}
|
|
jtest::check(!allEqual);
|
|
});
|
|
|
|
TEST("Rng, Float_A_B", []{
|
|
RNG rng;
|
|
for (int i = 0; i < 1000; ++i)
|
|
{
|
|
float a = rng.Float();
|
|
float b = rng.Float();
|
|
if (a == b)
|
|
continue;
|
|
if (b < a)
|
|
Swap(a, b);
|
|
|
|
float f = rng.Float(a, b);
|
|
jtest::check(a <= f);
|
|
jtest::check(f < b);
|
|
}
|
|
});
|
|
|
|
TEST("Rng::Float_A_B_Incl", []{
|
|
RNG rng;
|
|
for (int i = 0; i < 1000; ++i)
|
|
{
|
|
float a = rng.Float();
|
|
float b = rng.Float();
|
|
if (b > a)
|
|
Swap(a, b);
|
|
|
|
float f = rng.FloatIncl(a, b);
|
|
jtest::check(a <= f);
|
|
jtest::check(f <= b);
|
|
}
|
|
});
|
|
|
|
return 0;
|
|
}
|