Skip to content

Commit

Permalink
test: Use deterministic RNG for tests (#2694)
Browse files Browse the repository at this point in the history
apparently otherwise the random engine can be different on different platforms which I observed in #2625 with macos

at the same time I hardcoded the type for the `std::uniform_int_distribution`. apparently this can also make a difference for the random value
  • Loading branch information
andiwand authored Nov 20, 2023
1 parent eae867a commit 1d8d3ec
Show file tree
Hide file tree
Showing 35 changed files with 511 additions and 375 deletions.
1 change: 0 additions & 1 deletion Core/include/Acts/Surfaces/TrapezoidBounds.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace Acts {
/// @image html TrapezoidBounds.gif
///
/// @todo can be speed optimized by calculating kappa/delta and caching it

class TrapezoidBounds : public PlanarBounds {
public:
enum BoundValues {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ struct Uniform {
auto bin = binningData.search(value);
auto lower = binningData.boundaries()[bin];
auto higher = binningData.boundaries()[bin + 1];
std::uniform_real_distribution<> dist{0., 1.};
std::uniform_real_distribution<double> dist{0., 1.};
double svalue = lower + (higher - lower) * dist(rnd);
return std::pair{svalue, (higher - lower) / std::sqrt(12.)};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "ActsFatras/EventData/Barcode.hpp"
#include "ActsFatras/EventData/Particle.hpp"

#include <cstdint>
#include <limits>
#include <random>
#include <utility>
Expand All @@ -37,7 +38,7 @@ ActsExamples::ParametricParticleGenerator::ParametricParticleGenerator(

ActsExamples::SimParticleContainer
ActsExamples::ParametricParticleGenerator::operator()(RandomEngine& rng) {
using UniformIndex = std::uniform_int_distribution<unsigned int>;
using UniformIndex = std::uniform_int_distribution<std::uint8_t>;
using UniformReal = std::uniform_real_distribution<double>;

// choose between particle/anti-particle if requested
Expand Down
2 changes: 1 addition & 1 deletion Tests/Benchmarks/CovarianceTransportBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int main(int argc, char* argv[]) {
VectorHelpers::theta(direction), qop, time;

std::minstd_rand rng;
std::uniform_real_distribution<> uniform(0.5, 0.95);
std::uniform_real_distribution<double> uniform(0.5, 0.95);

unsigned int sillyCounter = 0;

Expand Down
6 changes: 3 additions & 3 deletions Tests/Benchmarks/SolenoidFieldBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ int main(int argc, char* argv[]) {
Acts::MagneticFieldContext mctx{};

std::minstd_rand rng;
std::uniform_real_distribution<> zDist(1.5 * (-L / 2.), 1.5 * L / 2.);
std::uniform_real_distribution<> rDist(0, R * 1.5);
std::uniform_real_distribution<> phiDist(-M_PI, M_PI);
std::uniform_real_distribution<double> zDist(1.5 * (-L / 2.), 1.5 * L / 2.);
std::uniform_real_distribution<double> rDist(0, R * 1.5);
std::uniform_real_distribution<double> phiDist(-M_PI, M_PI);
auto genPos = [&]() -> Acts::Vector3 {
const double z = zDist(rng), r = rDist(rng), phi = phiDist(rng);
return {r * std::cos(phi), r * std::sin(phi), z};
Expand Down
11 changes: 6 additions & 5 deletions Tests/Benchmarks/SurfaceIntersectionBenchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "Acts/Tests/CommonHelpers/BenchmarkTools.hpp"

#include <cmath>
#include <random>

namespace bdata = boost::unit_test::data;
namespace tt = boost::test_tools;
Expand Down Expand Up @@ -89,12 +90,12 @@ MicroBenchmarkResult intersectionTest(const surface_t& surface, double phi,

BOOST_DATA_TEST_CASE(
benchmark_surface_intersections,
bdata::random(
(bdata::seed = 21,
bdata::distribution = std::uniform_real_distribution<>(-M_PI, M_PI))) ^
bdata::random((bdata::seed = 22,
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 21,
bdata::distribution =
std::uniform_real_distribution<double>(-M_PI, M_PI))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 22,
bdata::distribution =
std::uniform_real_distribution<>(-0.3, 0.3))) ^
std::uniform_real_distribution<double>(-0.3, 0.3))) ^
bdata::xrange(ntests),
phi, theta, index) {
(void)index;
Expand Down
14 changes: 8 additions & 6 deletions Tests/IntegrationTests/InterpolatedSolenoidBFieldTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,17 @@ StreamWrapper valid(std::ofstream("magfield_lookup.csv"));
const int ntests = 10000;
BOOST_DATA_TEST_CASE(
solenoid_interpolated_bfield_comparison,
bdata::random((bdata::seed = 1, bdata::engine = std::mt19937(),
bdata::distribution = std::uniform_real_distribution<>(
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 1,
bdata::distribution = std::uniform_real_distribution<double>(
1.5 * (-L / 2.), 1.5 * L / 2.))) ^
bdata::random((bdata::seed = 2, bdata::engine = std::mt19937(),
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
bdata::distribution =
std::uniform_real_distribution<>(0, R * 1.5))) ^
bdata::random((bdata::seed = 3, bdata::engine = std::mt19937(),
std::uniform_real_distribution<double>(0,
R * 1.5))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
bdata::distribution =
std::uniform_real_distribution<>(-M_PI, M_PI))) ^
std::uniform_real_distribution<double>(-M_PI,
M_PI))) ^
bdata::xrange(ntests),
z, r, phi, index) {
(void)index;
Expand Down
9 changes: 5 additions & 4 deletions Tests/UnitTests/Core/Clusterization/ClusterizationTests1D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ BOOST_AUTO_TEST_CASE(Grid_1D_rand) {

while (ntries-- > 0) {
std::mt19937_64 rnd(startSeed++);
std::uniform_int_distribution<std::size_t> distr_size(minsize, maxsize);
std::uniform_int_distribution<std::size_t> distr_space(minspace, maxspace);
std::uniform_int_distribution<std::uint32_t> distr_size(minsize, maxsize);
std::uniform_int_distribution<std::uint32_t> distr_space(minspace,
maxspace);

int col = 0;

Expand All @@ -98,8 +99,8 @@ BOOST_AUTO_TEST_CASE(Grid_1D_rand) {
for (std::size_t i = 0; i < nclusters; i++) {
Cluster cl;
col += distr_space(rnd);
std::size_t size = distr_size(rnd);
for (std::size_t j = 0; j < size; j++) {
std::uint32_t size = distr_size(rnd);
for (std::uint32_t j = 0; j < size; j++) {
Cell cell(col++);
cells.push_back(cell);
clusterAddCell(cl, cell);
Expand Down
8 changes: 4 additions & 4 deletions Tests/UnitTests/Core/Clusterization/ClusterizationTests2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ std::vector<Rectangle> segment(int x0, int y0, int x1, int y1, RNG& rng) {
return {{x0, y0, x1, y1}};
}

int x_ = std::uniform_int_distribution(xmin, xmax)(rng);
int y_ = std::uniform_int_distribution(ymin, ymax)(rng);
int x_ = std::uniform_int_distribution<std::int32_t>(xmin, xmax)(rng);
int y_ = std::uniform_int_distribution<std::int32_t>(ymin, ymax)(rng);

if (splitx && !splity) {
return concat({segment(x0, y0, x_, y1, rng), segment(x_, y0, x1, y1, rng)});
Expand Down Expand Up @@ -188,8 +188,8 @@ Cluster2D gencluster(int x0, int y0, int x1, int y1, RNG& rng,
int y0_ = y0 + 1;
int y1_ = y1 - 1;

int x = std::uniform_int_distribution(x0_, x1_)(rng);
int y = std::uniform_int_distribution(y0_, y1_)(rng);
int x = std::uniform_int_distribution<std::int32_t>(x0_, x1_)(rng);
int y = std::uniform_int_distribution<std::int32_t>(y0_, y1_)(rng);

std::vector<Cell2D> cells = {Cell2D(x, y)};
genclusterw(x, y, x0_, y0_, x1_, y1_, cells, rng, startp, decayp);
Expand Down
24 changes: 13 additions & 11 deletions Tests/UnitTests/Core/Digitization/PlanarModuleStepperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ GeometryContext tgContext = GeometryContext();
/// guaranteed to be in on the readout/counter plane
BOOST_DATA_TEST_CASE(
readout_counter_test,
bdata::random((bdata::seed = 0,
bdata::distribution = std::uniform_real_distribution<>(
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 0,
bdata::distribution = std::uniform_real_distribution<double>(
-halfX + sguardX, halfX - sguardX))) ^
bdata::random((bdata::seed = 1,
bdata::distribution = std::uniform_real_distribution<>(
-halfX + sguardX, halfX - sguardX))) ^
bdata::random((bdata::seed = 2,
bdata::random(
(bdata::engine = std::mt19937(), bdata::seed = 1,
bdata::distribution = std::uniform_real_distribution<double>(
-halfX + sguardX, halfX - sguardX))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
bdata::distribution =
std::uniform_real_distribution<>(-halfY, halfY))) ^
bdata::random((bdata::seed = 3,
bdata::distribution = std::uniform_int_distribution<>(
-static_cast<int>(halfY),
static_cast<int>(halfY)))) ^
std::uniform_real_distribution<double>(-halfY,
halfY))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
bdata::distribution =
std::uniform_real_distribution<double>(-halfY,
halfY))) ^
bdata::xrange(ntests),
entryX, entryY, exitX, exitY, index) {
// avoid warning with void
Expand Down
23 changes: 12 additions & 11 deletions Tests/UnitTests/Core/Geometry/BVHDataTestCase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ auto tg = grid.trackingGeometry;

BOOST_DATA_TEST_CASE(
bvhnavigation_test,
bdata::random((bdata::seed = 7, bdata::engine = std::mt19937(),
bdata::distribution = std::uniform_real_distribution<>(-5,
5))) ^
bdata::random((bdata::seed = 2, bdata::engine = std::mt19937(),
bdata::random(
(bdata::engine = std::mt19937(), bdata::seed = 7,
bdata::distribution = std::uniform_real_distribution<double>(-5, 5))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
bdata::distribution =
std::uniform_real_distribution<>(-M_PI, M_PI))) ^
bdata::random((bdata::seed = 3, bdata::engine = std::mt19937(),
std::uniform_real_distribution<double>(-M_PI,
M_PI))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
bdata::distribution =
std::uniform_real_distribution<>(-100, 100))) ^
bdata::random((bdata::seed = 4, bdata::engine = std::mt19937(),
std::uniform_real_distribution<double>(-100, 100))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 4,
bdata::distribution =
std::uniform_real_distribution<>(-100, 100))) ^
bdata::random((bdata::seed = 5, bdata::engine = std::mt19937(),
std::uniform_real_distribution<double>(-100, 100))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 5,
bdata::distribution =
std::uniform_real_distribution<>(-100, 100))) ^
std::uniform_real_distribution<double>(-100, 100))) ^
bdata::xrange(NTESTS),
eta, phi, x, y, z, index) {
using namespace Acts::UnitLiterals;
Expand Down
30 changes: 24 additions & 6 deletions Tests/UnitTests/Core/Geometry/CylinderVolumeBoundsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,30 @@ BOOST_AUTO_TEST_CASE(CylinderVolumeBoundsAccess) {
}

/// Unit test for testing the orientedSurfaces() function
BOOST_DATA_TEST_CASE(CylinderVolumeBoundsOrientedSurfaces,
bdata::random(-M_PI, M_PI) ^ bdata::random(-M_PI, M_PI) ^
bdata::random(-M_PI, M_PI) ^ bdata::random(-10., 10.) ^
bdata::random(-10., 10.) ^ bdata::random(-10., 10.) ^
bdata::xrange(100),
alpha, beta, gamma, posX, posY, posZ, index) {
BOOST_DATA_TEST_CASE(
CylinderVolumeBoundsOrientedSurfaces,
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 1,
bdata::distribution =
std::uniform_real_distribution<double>(-M_PI, M_PI))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
bdata::distribution =
std::uniform_real_distribution<double>(-M_PI,
M_PI))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
bdata::distribution =
std::uniform_real_distribution<double>(-M_PI,
M_PI))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 4,
bdata::distribution =
std::uniform_real_distribution<double>(-10., 10.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 5,
bdata::distribution =
std::uniform_real_distribution<double>(-10., 10.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 6,
bdata::distribution =
std::uniform_real_distribution<double>(-10., 10.))) ^
bdata::xrange(100),
alpha, beta, gamma, posX, posY, posZ, index) {
(void)index;

// Create a test context
Expand Down
87 changes: 67 additions & 20 deletions Tests/UnitTests/Core/Geometry/CylinderVolumeBuilderTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,28 @@ namespace Acts {
namespace Test {

/// Unit test for testing the wraps() function of the CylinderVolumeBuilder
BOOST_DATA_TEST_CASE(CylinderVolumeBuilder_wraps,
bdata::random(-11., -15.) ^ bdata::random(11., 15.) ^
bdata::random(-10., 10.) ^ bdata::random(0., 4.) ^
bdata::random(11., 15.) ^ bdata::random(10., 15.) ^
bdata::xrange(100),
left, right, central, inner, outer, length, index) {
BOOST_DATA_TEST_CASE(
CylinderVolumeBuilder_wraps,
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 1,
bdata::distribution =
std::uniform_real_distribution<double>(-11., -15.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
bdata::distribution =
std::uniform_real_distribution<double>(11., 15.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
bdata::distribution =
std::uniform_real_distribution<double>(-10., 10.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 4,
bdata::distribution =
std::uniform_real_distribution<double>(0., 4.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 5,
bdata::distribution =
std::uniform_real_distribution<double>(11., 15.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 6,
bdata::distribution =
std::uniform_real_distribution<double>(10., 15.))) ^
bdata::xrange(100),
left, right, central, inner, outer, length, index) {
(void)index;
// inner volume
VolumeConfig innerConfig;
Expand Down Expand Up @@ -97,12 +113,28 @@ BOOST_DATA_TEST_CASE(CylinderVolumeBuilder_wraps,

/// Unit test for testing the contains(), containsInR() and containsInZ()
/// function of the CylinderVolumeBuilder
BOOST_DATA_TEST_CASE(CylinderVolumeBuilder_containes,
bdata::random(-11., -15.) ^ bdata::random(11., 15.) ^
bdata::random(-10., 10.) ^ bdata::random(0., 4.) ^
bdata::random(10., 15.) ^ bdata::random(10., 15.) ^
bdata::xrange(100),
left, right, central, inner, outer, length, index) {
BOOST_DATA_TEST_CASE(
CylinderVolumeBuilder_containes,
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 1,
bdata::distribution =
std::uniform_real_distribution<double>(-11., -15.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
bdata::distribution =
std::uniform_real_distribution<double>(11., 15.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
bdata::distribution =
std::uniform_real_distribution<double>(-10., 10.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 4,
bdata::distribution =
std::uniform_real_distribution<double>(0., 4.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 5,
bdata::distribution =
std::uniform_real_distribution<double>(10., 15.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 6,
bdata::distribution =
std::uniform_real_distribution<double>(10., 15.))) ^
bdata::xrange(100),
left, right, central, inner, outer, length, index) {
(void)index;
// inner volume
VolumeConfig innerConfig;
Expand Down Expand Up @@ -184,10 +216,16 @@ BOOST_DATA_TEST_CASE(CylinderVolumeBuilder_containes,

/// Unit test for testing the coverlapsInR()
/// function of the CylinderVolumeBuilder
BOOST_DATA_TEST_CASE(CylinderVolumeBuilder_overlapsInR,
bdata::random(0., 4.) ^ bdata::random(11., 15.) ^
bdata::xrange(100),
inner, outer, index) {
BOOST_DATA_TEST_CASE(
CylinderVolumeBuilder_overlapsInR,
bdata::random((
bdata::engine = std::mt19937(), bdata::seed = 1,
bdata::distribution = std::uniform_real_distribution<double>(0., 4.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
bdata::distribution =
std::uniform_real_distribution<double>(11., 15.))) ^
bdata::xrange(100),
inner, outer, index) {
(void)index;
// reference volume
VolumeConfig Config0;
Expand Down Expand Up @@ -246,10 +284,19 @@ BOOST_DATA_TEST_CASE(CylinderVolumeBuilder_overlapsInR,

/// Unit test for testing the coverlapsInZ()
/// function of the CylinderVolumeBuilder
BOOST_DATA_TEST_CASE(CylinderVolumeBuilder_overlapsInZ,
bdata::random(-11., -15.) ^ bdata::random(11., 15.) ^
bdata::random(0., 4.) ^ bdata::xrange(100),
left, right, inner, index) {
BOOST_DATA_TEST_CASE(
CylinderVolumeBuilder_overlapsInZ,
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 1,
bdata::distribution =
std::uniform_real_distribution<double>(-11., -15.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 2,
bdata::distribution =
std::uniform_real_distribution<double>(11., 15.))) ^
bdata::random((bdata::engine = std::mt19937(), bdata::seed = 3,
bdata::distribution =
std::uniform_real_distribution<double>(0., 4.))) ^
bdata::xrange(100),
left, right, inner, index) {
(void)index;
// inner volume
VolumeConfig Config0;
Expand Down
Loading

0 comments on commit 1d8d3ec

Please sign in to comment.