diff --git a/source/lib/file/archive/disabled_tests/test_codec_zlib.h b/source/lib/file/archive/disabled_tests/test_codec_zlib.h index 68f02fe80b..3f4c3533e3 100644 --- a/source/lib/file/archive/disabled_tests/test_codec_zlib.h +++ b/source/lib/file/archive/disabled_tests/test_codec_zlib.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -25,6 +25,8 @@ #include "lib/self_test.h" #include "lib/res/file/archive/codec_zlib.h" +#include + class TestCodecZLib : public CxxTest::TestSuite { public: @@ -35,10 +37,12 @@ public: // generate random input udata // (limit values to 0..7 so that the udata will actually be compressible) + std::mt19937 engine(42); + std::uniform_int_distribution distribution(0x00, 0x07); const size_t usize = 10000; u8 udata[usize]; for(size_t i = 0; i < usize; i++) - udata[i] = rand() & 0x07; + udata[i] = distribution(engine); // compress u8* cdata; size_t csize; diff --git a/source/lib/file/archive/disabled_tests/test_compression.h b/source/lib/file/archive/disabled_tests/test_compression.h index dee5f60732..1158020e9d 100644 --- a/source/lib/file/archive/disabled_tests/test_compression.h +++ b/source/lib/file/archive/disabled_tests/test_compression.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -25,6 +25,8 @@ #include "lib/self_test.h" #include "lib/res/file/archive/compression.h" +#include + class TestCompression : public CxxTest::TestSuite { public: @@ -32,10 +34,12 @@ public: { // generate random input data // (limit values to 0..7 so that the data will actually be compressible) + std::mt19937 engine(42); + std::uniform_int_distribution distribution(0x00, 0x07); const size_t data_size = 10000; u8 data[data_size]; for(size_t i = 0; i < data_size; i++) - data[i] = rand() & 0x07; + data[i] = distribution(engine); u8* cdata; size_t csize; u8 udata[data_size]; diff --git a/source/lib/tests/test_adts.h b/source/lib/tests/test_adts.h index 84807e51eb..c6a05530e0 100644 --- a/source/lib/tests/test_adts.h +++ b/source/lib/tests/test_adts.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the @@ -23,7 +23,8 @@ #include "lib/self_test.h" #include "lib/adts/ring_buf.h" -#include "lib/rand.h" + +#include class TestRingbuf : public CxxTest::TestSuite { @@ -61,16 +62,19 @@ public: void test_randomized_insert_remove() { - srand(1); + std::mt19937 engine(42); + std::uniform_int_distribution distributionProbability(0, 9); + std::uniform_int_distribution distributionValue(0); + RingBuf buf; std::deque deq; for(size_t rep = 0; rep < 1000; rep++) { - size_t rnd_op = rand(0, 10); + const size_t rnd_op = distributionProbability(engine); // 70% - insert if(rnd_op >= 3) { - int item = rand(); + int item = distributionValue(engine); buf.push_back(item); deq.push_back(item); diff --git a/source/maths/tests/test_Matrix3d.h b/source/maths/tests/test_Matrix3d.h index 57bab8ee33..4e449ff50a 100644 --- a/source/maths/tests/test_Matrix3d.h +++ b/source/maths/tests/test_Matrix3d.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2011 Wildfire Games. +/* Copyright (C) 2021 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -17,23 +17,32 @@ #include "lib/self_test.h" -#include -#include #include "maths/Matrix3D.h" #include "maths/Quaternion.h" +#include +#include +#include + class TestMatrix : public CxxTest::TestSuite { + std::mt19937 m_Engine; + public: + void setUp() + { + m_Engine = std::mt19937(42); + } + void test_inverse() { CMatrix3D m; - srand(0); + std::uniform_real_distribution distribution01(0.0f, std::nextafter(1.0f, 2.0f)); for (int i = 0; i < 4; ++i) { for (int j = 0; j < 16; ++j) { - m._data[j] = -1.0f + 2.0f*(rand()/(float)RAND_MAX); + m._data[j] = -1.0f + 2.0f * distribution01(m_Engine); } CMatrix3D n; m.GetInverse(n); @@ -52,14 +61,14 @@ public: void test_quats() { - srand(0); + std::uniform_real_distribution distribution01(0.0f, std::nextafter(1.0f, 2.0f)); for (int i = 0; i < 4; ++i) { CQuaternion q; q.FromEulerAngles( - -6.28f + 12.56f*(rand()/(float)RAND_MAX), - -6.28f + 12.56f*(rand()/(float)RAND_MAX), - -6.28f + 12.56f*(rand()/(float)RAND_MAX) + -6.28f + 12.56f * distribution01(m_Engine), + -6.28f + 12.56f * distribution01(m_Engine), + -6.28f + 12.56f * distribution01(m_Engine) ); CMatrix3D m; q.ToMatrix(m); @@ -83,11 +92,11 @@ public: void test_rotate() { - CMatrix3D m; - srand(0); + std::uniform_real_distribution distribution01(0.0f, std::nextafter(1.0f, 2.0f)); + CMatrix3D m; for (int j = 0; j < 16; ++j) - m._data[j] = -1.0f + 2.0f*(rand()/(float)RAND_MAX); + m._data[j] = -1.0f + 2.0f * distribution01(m_Engine); CMatrix3D r, a, b; @@ -124,8 +133,9 @@ public: void test_getRotation() { + std::uniform_real_distribution distribution01(0.0f, std::nextafter(1.0f, 2.0f)); + CMatrix3D m; - srand(0); m.SetZero(); TS_ASSERT_EQUALS(m.GetYRotation(), 0.f); @@ -135,7 +145,7 @@ public: for (int j = 0; j < 16; ++j) { - float a = 2*M_PI*rand()/(float)RAND_MAX - M_PI; + float a = 2 * M_PI * distribution01(m_Engine) - M_PI; m.SetYRotation(a); TS_ASSERT_DELTA(m.GetYRotation(), a, 0.001f); } @@ -143,11 +153,12 @@ public: void test_scale() { + std::uniform_real_distribution distribution01(0.0f, std::nextafter(1.0f, 2.0f)); + CMatrix3D m; - srand(0); for (int j = 0; j < 16; ++j) - m._data[j] = -1.0f + 2.0f*(rand()/(float)RAND_MAX); + m._data[j] = -1.0f + 2.0f * distribution01(m_Engine); CMatrix3D s, a, b; diff --git a/source/simulation2/components/tests/test_Pathfinder.h b/source/simulation2/components/tests/test_Pathfinder.h index 85e8f4e890..4ee7a5f17b 100644 --- a/source/simulation2/components/tests/test_Pathfinder.h +++ b/source/simulation2/components/tests/test_Pathfinder.h @@ -32,6 +32,7 @@ #include "simulation2/Simulation2.h" #include +#include class TestCmpPathfinder : public CxxTest::TestSuite { @@ -177,13 +178,15 @@ public: double t = timer_Time(); - srand(1234); + std::mt19937 engine(42); + std::uniform_int_distribution distribution511(0, 511); + std::uniform_int_distribution distribution63(0, 63); for (size_t j = 0; j < 1024*2; ++j) { - entity_pos_t x0 = entity_pos_t::FromInt(rand() % 512); - entity_pos_t z0 = entity_pos_t::FromInt(rand() % 512); - entity_pos_t x1 = x0 + entity_pos_t::FromInt(rand() % 64); - entity_pos_t z1 = z0 + entity_pos_t::FromInt(rand() % 64); + entity_pos_t x0 = entity_pos_t::FromInt(distribution511(engine)); + entity_pos_t z0 = entity_pos_t::FromInt(distribution511(engine)); + entity_pos_t x1 = x0 + entity_pos_t::FromInt(distribution63(engine)); + entity_pos_t z1 = z0 + entity_pos_t::FromInt(distribution63(engine)); PathGoal goal = { PathGoal::POINT, x1, z1 }; WaypointPath path; @@ -208,11 +211,12 @@ public: CmpPtr cmpObstructionMan(sim2, SYSTEM_ENTITY); CmpPtr cmpPathfinder(sim2, SYSTEM_ENTITY); - srand(0); + std::mt19937 engine(42); + std::uniform_real_distribution distribution01(0.0f, std::nextafter(1.0f, 2.0f)); for (size_t i = 0; i < 200; ++i) { - fixed x = fixed::FromFloat(1.5f*range.ToFloat() * rand()/(float)RAND_MAX); - fixed z = fixed::FromFloat(1.5f*range.ToFloat() * rand()/(float)RAND_MAX); + fixed x = fixed::FromFloat(1.5f*range.ToFloat() * distribution01(engine)); + fixed z = fixed::FromFloat(1.5f*range.ToFloat() * distribution01(engine)); // printf("# %f %f\n", x.ToFloat(), z.ToFloat()); cmpObstructionMan->AddUnitShape(INVALID_ENTITY, x, z, fixed::FromInt(2), 0, INVALID_ENTITY); }