Skip to content
Snippets Groups Projects

[UnitTest] Make unit tests deterministic with fixed seed

9 files
+ 30
20
Compare changes
  • Side-by-side
  • Inline
Files
9
  • Use the getSeed() method provided by Catch to initialize the
    random number generator instead of using the default random device.
    
    This allows to make the tests deterministic when --rng-seed <seed>
    option is passed.
    Also this allows to report a test failure by specifying the
    seed as reported in the test output, which can then be reproduced
    with --rng-seed <reported_seed>.
@@ -14,13 +14,14 @@
#include <cstdint> // std::uint8_t, std::uint16_t, std::int32_t
#include <numeric> // std::accumulate, std::inner_product
#include <functional> // std::multiplies
#include <random> // std::random_device, std::mt19937,
#include <random> // std::mt19937,
// std::uniform_int_distribution, std::uniform_real_distribution
#include <set>
#include <string>
#include <vector>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators_random.hpp>
#include "aidge/backend/cpu/data/TensorImpl.hpp"
#include "aidge/data/Data.hpp"
@@ -127,7 +128,7 @@ TEST_CASE("[core/data] Tensor(Construction)", "[Tensor][Constructor]") {
constexpr std::uint16_t NBTRIALS = 10;
// Create random number generators
std::random_device rd;
auto rd = Catch::Generators::Detail::getSeed;
std::mt19937 gen(rd());
std::uniform_int_distribution<std::size_t> dimsDist(1, 10);
std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5);
@@ -169,7 +170,7 @@ TEST_CASE("[core/data] Tensor(getter/setter)", "[Tensor][Getter][Setter]") {
constexpr std::uint16_t NBTRIALS = 10;
// Create random number generators
std::random_device rd;
auto rd = Catch::Generators::Detail::getSeed;
std::mt19937 gen(rd());
std::uniform_int_distribution<std::size_t> dimsDist(1, 10);
std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5);
@@ -261,7 +262,7 @@ TEST_CASE("[core/data] Tensor(other)", "[Tensor][extract][zeros][print]") {
constexpr std::uint16_t NBTRIALS = 10;
// Create random number generators
std::random_device rd;
auto rd = Catch::Generators::Detail::getSeed;
std::mt19937 gen(rd());
std::uniform_int_distribution<std::size_t> dimsDist(1, 10);
std::uniform_int_distribution<std::size_t> nbDimsDist(1, 5);
Loading