/** * \file RandomCoverage.cpp * \brief Coverage test for %RandomLib * * Compile/link with, e.g.,\n * g++ -I../include -O2 -funroll-loops * -o RandomCoverage RandomCoverage.cpp ../src/Random.cpp\n * ./RandomCoverage * * This executes nearly all of the public functions in %RandomLib. This is * important, since it allows the compiler to check the code which appears in * header files. It also shows how templated functions can be invoked. * * Copyright (c) Charles Karney (2006-2012) and licensed * under the MIT/X11 License. For more information, see * http://randomlib.sourceforge.net/ **********************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define repeat for (int i = 0; i < 1000; ++i) void coverage32() { typedef RandomLib::SRandom32 Random; { // Setting and examing the seed + seed management std::vector v(Random::SeedVector()); unsigned long w = Random::SeedWord(); std::string s(Random::VectorToString(v)); { Random r(v); } { Random r(v.begin(), v.end()); } int a[] = {1, 2, 3, 4}; { Random r(a, a + 4); } { Random r(w); } { Random r(s); } { Random r; } Random r(0); r.Reseed(v); r.Reseed(v.begin(), v.end()); r.Reseed(w); r.Reseed(s); r.Reseed(); v = r.Seed(); s = r.SeedString(); r.Reseed(Random::VectorToString(v)); r.Reseed(Random::StringToVector(s)); } Random r; { // Functions returning random integers repeat r(); repeat r.Ran(); repeat r.Ran32(); repeat r.Ran64(); repeat r(52); repeat r.Integer(); repeat r.Integer(); repeat r.Integer(); repeat r.Integer<3>(); repeat r.Integer(); repeat r.Integer(); repeat r.Integer(52); repeat r.IntegerC(51); repeat r.IntegerC(1,52); repeat r.Integer(6u); repeat r.IntegerC(5u); repeat r.IntegerC(1u,6u); repeat r(); repeat r(52u); } { // Functions returning random reals repeat { r.Fixed (); r.Fixed (); r.Fixed (); } repeat { r.FixedU(); r.FixedU(); r.FixedU(); } repeat { r.FixedN(); r.FixedN(); r.FixedN(); } repeat { r.FixedW(); r.FixedW(); r.FixedW(); } repeat { r.FixedS(); r.FixedS(); r.FixedS(); } repeat { r.FixedO(); r.FixedO(); r.FixedO(); } repeat { r.Float (); r.Float (); r.Float (); } repeat { r.FloatU(); r.FloatU(); r.FloatU(); } repeat { r.FloatN(); r.FloatN(); r.FloatN(); } repeat { r.FloatW(); r.FloatW(); r.FloatW(); } repeat { r.Real(); r.Real(); } } { // Functions returning other random results repeat r.Boolean(); repeat r.Prob(0.5f); repeat r.Prob(2.3, 7.0); repeat r.Prob(23, 70); repeat r.Bits< 5>(); repeat r.Bits<64>(); } { // Normal distribution RandomLib::NormalDistribution nf; RandomLib::NormalDistribution<> nd; repeat nf(r); repeat nd(r, 1.0, 2.0); } { // Exponention distribution RandomLib::ExponentialDistribution ef; RandomLib::ExponentialDistribution<> ed; repeat ef(r); repeat ed(r, 2.0); } { // Discrete probabilities unsigned w[] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 }; std::vector wi(13); std::vector wf(13); for (int i = 0; i < 13; ++i) { wi[i] = int(w[i]); wf[i] = float(w[i]); } { RandomLib::RandomSelect sel; } { RandomLib::RandomSelect sel(w, w + 13); } { RandomLib::RandomSelect sel(wi.begin(), wi.end()); } { RandomLib::RandomSelect sel(wi); } { RandomLib::RandomSelect<> sel; } { RandomLib::RandomSelect<> sel(w, w + 13); } { RandomLib::RandomSelect<> sel(wi); } { RandomLib::RandomSelect<> sel(wf.begin(), wf.end()); } { RandomLib::RandomSelect<> sel(wf); } { RandomLib::RandomSelect sel; sel.Init(w, w + 13); sel.Init(wi.begin(), wi.end()); sel.Init(wi); repeat sel(r); sel.TotalWeight(); sel.MaxWeight(); sel.Weight(3); sel.Choices(); } { RandomLib::RandomSelect<> sel; sel.Init(w, w + 13); sel.Init(wi.begin(), wi.end()); sel.Init(wi); sel.Init(wf); repeat sel(r); sel.TotalWeight(); sel.MaxWeight(); sel.Weight(3); sel.Choices(); } // Other distributions { RandomLib::LeadingZeros lz; repeat lz(r); } { RandomLib::ExponentialProb ep; repeat ep(r, 1.5f); } { // Infinite precision random numbers { RandomLib::RandomNumber<1> n; n.Init(); n.Digit(r, 10); n.RawDigit(4); n.AddInteger(-2); n.Negate(); n.Sign(); n.Floor(); n.Ceiling(); n.Size(); n.Range(); n.Fraction(r); n.Value(r); RandomLib::RandomNumber<1> p; n.LessThan(r, p); std::ostringstream os; os << n; } { RandomLib::RandomNumber<32> n; n.Init(); n.Digit(r, 10); n.RawDigit(4); n.AddInteger(-2); n.Negate(); n.Sign(); n.Floor(); n.Ceiling(); n.Size(); n.Range(); n.Fraction(r); n.Value(r); RandomLib::RandomNumber<32> p; n.LessThan(r, p); std::ostringstream os; os << n; } { if (RandomLib::UniformInteger::Check(37, 7)) { RandomLib::UniformInteger u(r,37); u.Min(); u.Max(); u.Entropy(); u.Add(3); u.LessThan(r, 4, 1); u.LessThanEqual(r, 4, 1); u.GreaterThan(r, 60, 7); u.GreaterThanEqual(r, 60, 7); u.Negate(); std::ostringstream os; os << u; os << " " << u(r); } } { RandomLib::UniformInteger u(r,37); u.Min(); u.Max(); u.Entropy(); u.Add(3); u.LessThan(r, 4, 1); u.LessThanEqual(r, 4, 1); u.GreaterThan(r, 60, 7); u.GreaterThanEqual(r, 60, 7); u.Negate(); std::ostringstream os; os << u; os << " " << u(r); } { RandomLib::UniformInteger u(r,37); u.Min(); u.Max(); u.Entropy(); u.Add(3); u.LessThan(r, 4, 1); u.LessThanEqual(r, 4, 1); u.GreaterThan(r, 60, 7); u.GreaterThanEqual(r, 60, 7); u.Negate(); std::ostringstream os; os << u; os << " " << u(r); } { RandomLib::UniformInteger u(r,37); u.Min(); u.Max(); u.Entropy(); u.Add(3); u.LessThan(r, 4, 1); u.LessThanEqual(r, 4, 1); u.GreaterThan(r, 60, 7); u.GreaterThanEqual(r, 60, 7); u.Negate(); std::ostringstream os; os << u; os << " " << u(r); } // Exact distributions { RandomLib::ExactExponential< 1> ed; repeat ed(r).Value(r); } { RandomLib::ExactExponential<32> ed; repeat ed(r).Value(r); } { RandomLib::ExactNormal< 1> ed; repeat ed(r).Value(r); } { RandomLib::ExactNormal<32> ed; repeat ed(r).Value(r); } { RandomLib::ExactPower< 1> pd; repeat pd(r,2).Value(r); } { RandomLib::ExactPower<32> pd; repeat pd(r,3).Value(r); } { RandomLib::DiscreteNormal pd(7); repeat pd(r); } { RandomLib::DiscreteNormal pd(7,1,1,2); repeat pd(r); } { RandomLib::DiscreteNormal pd(7,1,1,2); repeat pd(r); } { RandomLib::DiscreteNormal pd(7); repeat pd(r); } { RandomLib::DiscreteNormalAlt pd(7); repeat pd(r)(r); } { RandomLib::DiscreteNormalAlt pd(7); repeat pd(r)(r); } { RandomLib::DiscreteNormalAlt pd(7); repeat pd(r)(r); } { RandomLib::DiscreteNormalAlt pd(7); repeat pd(r)(r); } { RandomLib::InversePiProb pd; repeat pd(r); } { RandomLib::InverseEProb pd; repeat pd(r); } } } { // Setting position in sequence r.Count(); r.StepCount(1000); r.Reset(); r.SetCount(10000); r.SetStride(10,1); r.SetStride(5); r.GetStride(); r.SetStride(); } { // Other Random s(r); s = Random::Global; void(s == r); void(s != s); r.swap(s); std::swap(r, s); } } void coverage64() { typedef RandomLib::SRandom64 Random; { // Setting and examing the seed + seed management std::vector v(Random::SeedVector()); unsigned long w = Random::SeedWord(); std::string s(Random::VectorToString(v)); { Random r(v); } { Random r(v.begin(), v.end()); } int a[] = {1, 2, 3, 4}; { Random r(a, a + 4); } { Random r(w); } { Random r(s); } { Random r; } Random r(0); r.Reseed(v); r.Reseed(v.begin(), v.end()); r.Reseed(w); r.Reseed(s); r.Reseed(); v = r.Seed(); s = r.SeedString(); r.Reseed(Random::VectorToString(v)); r.Reseed(Random::StringToVector(s)); } Random r; { // Functions returning random integers repeat r(); repeat r.Ran(); repeat r.Ran32(); repeat r.Ran64(); repeat r(52); repeat r.Integer(); repeat r.Integer(); repeat r.Integer(); repeat r.Integer<3>(); repeat r.Integer(); repeat r.Integer(); repeat r.Integer(52); repeat r.IntegerC(51); repeat r.IntegerC(1,52); repeat r.Integer(6u); repeat r.IntegerC(5u); repeat r.IntegerC(1u,6u); repeat r(); repeat r(52u); } { // Functions returning random reals repeat { r.Fixed (); r.Fixed (); r.Fixed (); } repeat { r.FixedU(); r.FixedU(); r.FixedU(); } repeat { r.FixedN(); r.FixedN(); r.FixedN(); } repeat { r.FixedW(); r.FixedW(); r.FixedW(); } repeat { r.FixedS(); r.FixedS(); r.FixedS(); } repeat { r.FixedO(); r.FixedO(); r.FixedO(); } repeat { r.Float (); r.Float (); r.Float (); } repeat { r.FloatU(); r.FloatU(); r.FloatU(); } repeat { r.FloatN(); r.FloatN(); r.FloatN(); } repeat { r.FloatW(); r.FloatW(); r.FloatW(); } repeat { r.Real(); r.Real(); } } { // Functions returning other random results repeat r.Boolean(); repeat r.Prob(0.5f); repeat r.Prob(2.3, 7.0); repeat r.Prob(23, 70); repeat r.Bits< 5>(); repeat r.Bits<64>(); } { // Normal distribution RandomLib::NormalDistribution nf; RandomLib::NormalDistribution<> nd; repeat nf(r); repeat nd(r, 1.0, 2.0); } { // Exponention distribution RandomLib::ExponentialDistribution ef; RandomLib::ExponentialDistribution<> ed; repeat ef(r); repeat ed(r, 2.0); } { // Discrete probabilities unsigned w[] = { 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 }; std::vector wi(13); std::vector wf(13); for (int i = 0; i < 13; ++i) { wi[i] = int(w[i]); wf[i] = float(w[i]); } { RandomLib::RandomSelect sel; } { RandomLib::RandomSelect sel(w, w + 13); } { RandomLib::RandomSelect sel(wi.begin(), wi.end()); } { RandomLib::RandomSelect sel(wi); } { RandomLib::RandomSelect<> sel; } { RandomLib::RandomSelect<> sel(w, w + 13); } { RandomLib::RandomSelect<> sel(wi); } { RandomLib::RandomSelect<> sel(wf.begin(), wf.end()); } { RandomLib::RandomSelect<> sel(wf); } { RandomLib::RandomSelect sel; sel.Init(w, w + 13); sel.Init(wi.begin(), wi.end()); sel.Init(wi); repeat sel(r); sel.TotalWeight(); sel.MaxWeight(); sel.Weight(3); sel.Choices(); } { RandomLib::RandomSelect<> sel; sel.Init(w, w + 13); sel.Init(wi.begin(), wi.end()); sel.Init(wi); sel.Init(wf); repeat sel(r); sel.TotalWeight(); sel.MaxWeight(); sel.Weight(3); sel.Choices(); } // Other distributions { RandomLib::LeadingZeros lz; repeat lz(r); } { RandomLib::ExponentialProb ep; repeat ep(r, 1.5f); } { // Infinite precision random numbers { RandomLib::RandomNumber<1> n; n.Init(); n.Digit(r, 10); n.RawDigit(4); n.AddInteger(-2); n.Negate(); n.Sign(); n.Floor(); n.Ceiling(); n.Size(); n.Range(); n.Fraction(r); n.Value(r); RandomLib::RandomNumber<1> p; n.LessThan(r, p); std::ostringstream os; os << n; } { RandomLib::RandomNumber<32> n; n.Init(); n.Digit(r, 10); n.RawDigit(4); n.AddInteger(-2); n.Negate(); n.Sign(); n.Floor(); n.Ceiling(); n.Size(); n.Range(); n.Fraction(r); n.Value(r); RandomLib::RandomNumber<32> p; n.LessThan(r, p); std::ostringstream os; os << n; } { if (RandomLib::UniformInteger::Check(37, 7)) { RandomLib::UniformInteger u(r,37); u.Min(); u.Max(); u.Entropy(); u.Add(3); u.LessThan(r, 4, 1); u.LessThanEqual(r, 4, 1); u.GreaterThan(r, 60, 7); u.GreaterThanEqual(r, 60, 7); u.Negate(); std::ostringstream os; os << u; os << " " << u(r); } } { RandomLib::UniformInteger u(r,37); u.Min(); u.Max(); u.Entropy(); u.Add(3); u.LessThan(r, 4, 1); u.LessThanEqual(r, 4, 1); u.GreaterThan(r, 60, 7); u.GreaterThanEqual(r, 60, 7); u.Negate(); std::ostringstream os; os << u; os << " " << u(r); } { RandomLib::UniformInteger u(r,37); u.Min(); u.Max(); u.Entropy(); u.Add(3); u.LessThan(r, 4, 1); u.LessThanEqual(r, 4, 1); u.GreaterThan(r, 60, 7); u.GreaterThanEqual(r, 60, 7); u.Negate(); std::ostringstream os; os << u; os << " " << u(r); } { RandomLib::UniformInteger u(r,37); u.Min(); u.Max(); u.Entropy(); u.Add(3); u.LessThan(r, 4, 1); u.LessThanEqual(r, 4, 1); u.GreaterThan(r, 60, 7); u.GreaterThanEqual(r, 60, 7); u.Negate(); std::ostringstream os; os << u; os << " " << u(r); } // Exact distributions { RandomLib::ExactExponential< 1> ed; repeat ed(r).Value(r); } { RandomLib::ExactExponential<32> ed; repeat ed(r).Value(r); } { RandomLib::ExactNormal< 1> ed; repeat ed(r).Value(r); } { RandomLib::ExactNormal<32> ed; repeat ed(r).Value(r); } { RandomLib::ExactPower< 1> pd; repeat pd(r,2).Value(r); } { RandomLib::ExactPower<32> pd; repeat pd(r,3).Value(r); } { RandomLib::DiscreteNormal pd(7); repeat pd(r); } { RandomLib::DiscreteNormal pd(7,1,1,2); repeat pd(r); } { RandomLib::DiscreteNormal pd(7,1,1,2); repeat pd(r); } { RandomLib::DiscreteNormal pd(7); repeat pd(r); } { RandomLib::DiscreteNormalAlt pd(7); repeat pd(r)(r); } { RandomLib::DiscreteNormalAlt pd(7); repeat pd(r)(r); } { RandomLib::DiscreteNormalAlt pd(7); repeat pd(r)(r); } { RandomLib::DiscreteNormalAlt pd(7); repeat pd(r)(r); } { RandomLib::InversePiProb pd; repeat pd(r); } { RandomLib::InverseEProb pd; repeat pd(r); } } } { // Setting position in sequence r.Count(); r.StepCount(1000); r.Reset(); r.SetCount(10000); r.SetStride(10,1); r.SetStride(5); r.GetStride(); r.SetStride(); } { // Other Random s(r); s = Random::Global; void(s == r); void(s != s); r.swap(s); std::swap(r, s); } } int main(int, char**) { std::cout << "RandomLib Coverage Test\n"; coverage32(); coverage64(); int retval = 0; { using namespace RandomLib; { RandomEngine,MixerMT0 > s("0x123,0x234,0x345,0x456"); s.SetCount(999); bool pass = s() == 3460025646U; std::cout << "Check " << s.Name() << (pass ? " passed\n" : " FAILED\n"); if (!pass) retval = 1; } { RandomEngine,MixerMT0 > s("0x12345,0,0x23456,0,0x34567,0,0x45678,0"); s.SetCount(999); bool pass = s() == 994412663058993407ULL; std::cout << "Check " << s.Name() << (pass ? " passed\n" : " FAILED\n"); if (!pass) retval = 1; } { SRandomGenerator32 s("0x1234,0x5678,0x9abc,0xdef0"); s.SetCount(999); bool pass = s() == 788493625U; std::cout << "Check " << s.Name() << (pass ? " passed\n" : " FAILED\n"); if (!pass) retval = 1; } { SRandomGenerator64 s("5,4,3,2,1"); s.SetCount(999); bool pass = s() == 13356980519185762498ULL; std::cout << "Check " << s.Name() << (pass ? " passed\n" : " FAILED\n"); if (!pass) retval = 1; } } return retval; }