1
0
forked from 0ad/0ad

Removes rand function usage from tests to avoid non-uniform distributions.

This was SVN commit r26012.
This commit is contained in:
Vladislav Belov 2021-11-25 17:57:59 +00:00
parent 6efa293fd1
commit 809e3ed0bd
5 changed files with 60 additions and 33 deletions

View File

@ -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 <random>
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<u8> 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;

View File

@ -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 <random>
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<u8> 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];

View File

@ -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 <random>
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<size_t> distributionProbability(0, 9);
std::uniform_int_distribution<int> distributionValue(0);
RingBuf<int, N> buf;
std::deque<int> 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);

View File

@ -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 <cstdlib>
#include <cmath>
#include "maths/Matrix3D.h"
#include "maths/Quaternion.h"
#include <cmath>
#include <cstdlib>
#include <random>
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<float> 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<float> 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<float> 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<float> 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<float> 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;

View File

@ -32,6 +32,7 @@
#include "simulation2/Simulation2.h"
#include <fstream>
#include <random>
class TestCmpPathfinder : public CxxTest::TestSuite
{
@ -177,13 +178,15 @@ public:
double t = timer_Time();
srand(1234);
std::mt19937 engine(42);
std::uniform_int_distribution<int> distribution511(0, 511);
std::uniform_int_distribution<int> 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<ICmpObstructionManager> cmpObstructionMan(sim2, SYSTEM_ENTITY);
CmpPtr<ICmpPathfinder> cmpPathfinder(sim2, SYSTEM_ENTITY);
srand(0);
std::mt19937 engine(42);
std::uniform_real_distribution<float> 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);
}