<commit_before>// Catch
#include <catch.hpp>
#include <catchExtension.hpp>

// C++ standard library
#include <random>

// Mantella
#include <mantella>

TEST_CASE("Rng") {
  SECTION("Getting a working generator.") {
    arma::Col<double>::fixed<1000> values;
    for (arma::uword n = 0; n < values.n_elem; ++n) {
      values.at(n) = std::uniform_real_distribution<double>(0, 1)(mant::Rng::getGenerator());
    }

    arma::Col<arma::uword> histogram = arma::hist(values, arma::linspace<arma::Col<double>>(0.05, 0.95, 10));
    CHECK(0.25 > static_cast<double>(histogram.max() - histogram.min()) / static_cast<double>(values.n_elem));
  }

  SECTION("Can specify a seed.") {
    arma::uword seed = 12345;
    mant::Rng::setSeed(seed);

    SECTION("Works with the C++ standard library.") {
      arma::Col<double>::fixed<10> expectedValues;
      for (arma::uword n = 0; n < expectedValues.n_elem; ++n) {
        expectedValues.at(n) = std::uniform_real_distribution<double>(0, 1)(mant::Rng::getGenerator());
      }

      mant::Rng::setSeed(seed);
      arma::Col<double>::fixed<10> actualValues;
      for (arma::uword n = 0; n < actualValues.n_elem; ++n) {
        actualValues.at(n) = std::uniform_real_distribution<double>(0, 1)(mant::Rng::getGenerator());
      }

      COMPARE(actualValues, expectedValues);
    }

    SECTION("Works with Armadillo.") {
      arma::Col<double>::fixed<10> expectedValues = arma::randu<arma::Col<double>>(10);
      mant::Rng::setSeed(seed);
      arma::Col<double>::fixed<10> actualValues = arma::randu<arma::Col<double>>(10);

      COMPARE(actualValues, expectedValues);
    }

    // Returns last seed
    CHECK(mant::Rng::getSeed() == seed);
  }

  SECTION("Can use a random seed.") {
    mant::Rng::setRandomSeed();

    SECTION("Works with the C++ standard library.") {
      arma::Col<double>::fixed<1000> values;
      for (arma::uword n = 0; n < values.n_elem; ++n) {
        values.at(n) = std::uniform_real_distribution<double>(0, 1)(mant::Rng::getGenerator());
      }

      arma::Col<arma::uword> histogram = arma::hist(values, arma::linspace<arma::Col<double>>(0.05, 0.95, 10));
      CHECK(0.25 > static_cast<double>(histogram.max() - histogram.min()) / static_cast<double>(values.n_elem));
    }

    SECTION("Works with Armadillo.") {
      arma::Col<double>::fixed<1000> values = arma::randu<arma::Col<double>>(1000);

      arma::Col<arma::uword> histogram = arma::hist(values, arma::linspace<arma::Col<double>>(0.05, 0.95, 10));
      CHECK(0.25 > static_cast<double>(histogram.max() - histogram.min()) / static_cast<double>(values.n_elem));
    }
  }
}<commit_msg>Improved tests for the RNG<commit_after>// Catch
#include <catch.hpp>
#include <catchExtension.hpp>

// C++ standard library
#include <random>

// Mantella
#include <mantella>

// This test is intently avoiding fuzzy testing, as we are challenging the random generator itself
TEST_CASE("Rng") {
  SECTION("::getGenerator") {
    SECTION("Is a valid (working) generator for C++ standard library random functions") {
      arma::Col<double>::fixed<10000> randomValues;
      for (arma::uword n = 0; n < randomValues.n_elem; ++n) {
        randomValues.at(n) = std::uniform_real_distribution<double>(0, 1)(mant::Rng::getGenerator());
      }

      const arma::Col<arma::uword>& histogram = arma::hist(randomValues, 10);
      CAPTURE(histogram);
      
      // Assumes that all values are drawn from [0, 1]
      CHECK(std::abs(static_cast<double>(histogram.max() - histogram.min()) < 0.05 * randomValues.n_elem));
    }
  }
    
  SECTION("::setSeed") {
    const arma::arma_rng::seed_type seed = 12345;
    CAPTURE(seed);
    mant::Rng::setSeed(seed);
    
    SECTION("Resetting the seed generates the same random sequence") {
      SECTION("Works with C++ standard library random functions") {
      arma::Col<double>::fixed<10> expectedRandomValues;
        for (arma::uword n = 0; n < expectedRandomValues.n_elem; ++n) {
          expectedRandomValues.at(n) = std::uniform_real_distribution<double>(0, 1)(mant::Rng::getGenerator());
        }
        CAPTURE(expectedRandomValues);

        mant::Rng::setSeed(seed);
        
        arma::Col<double>::fixed<10> actualRandomValues;
        for (arma::uword n = 0; n < actualRandomValues.n_elem; ++n) {
          actualRandomValues.at(n) = std::uniform_real_distribution<double>(0, 1)(mant::Rng::getGenerator());
        }
        CAPTURE(actualRandomValues);

        COMPARE(actualRandomValues, expectedRandomValues);
      }
      
      SECTION("Works with Armadillo C++") {
        const arma::Col<double>::fixed<10> expectedRandomValues = arma::randu<arma::Col<double>>(10);
        CAPTURE(expectedRandomValues);
        
        mant::Rng::setSeed(seed);
        
        const arma::Col<double>::fixed<10> actualRandomValues = arma::randu<arma::Col<double>>(10);
        CAPTURE(actualRandomValues);
        
        COMPARE(actualRandomValues, expectedRandomValues);
      }
    }
  }

  SECTION("::getSeed") {
    SECTION("Returns the current seed") {
      const arma::arma_rng::seed_type seed = 12345;
      CAPTURE(seed);
      mant::Rng::setSeed(seed);
    
      // Generate some random values
      arma::Col<double>::fixed<100> randomValues;
      for (arma::uword n = 0; n < randomValues.n_elem; ++n) {
        std::uniform_real_distribution<double>(0, 1)(mant::Rng::getGenerator());
      }
      CAPTURE(randomValues);

      CHECK(mant::Rng::getSeed() == seed);
    }
  }
  
  SECTION("::setRandomSeed") {
    SECTION("The values of a random sequences are uniform distributed between different random seeds.") {
      arma::Mat<double>::fixed<10000, 10> randomValues;
      for (arma::uword n = 0; n < randomValues.n_cols; ++n) {
        mant::Rng::setRandomSeed();
        CAPTURE(mant::Rng::getSeed());
        
        randomValues.col(n) = arma::randu<arma::Col<double>>(randomValues.n_rows);
      }

      const arma::Mat<arma::uword>& histogram = arma::hist(randomValues, 10);
      CAPTURE(histogram);
      
      // Assumes that all values are drawn from [0, 1]
      for (arma::uword n = 0; n < randomValues.n_cols; ++n) {
        CHECK(std::abs(static_cast<double>(histogram.col(n).max() - histogram.col(n).min()) < 0.05 * randomValues.n_rows));
      }
    }
  }
}
{'fraction_non_alphanumeric': 0.17051424932335615, 'fraction_numerical': 0.02276707530647986, 'mean_word_length': 3.636162361623616, 'pattern_counts': {'":': 4, '<': 90, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6165966', 'n_tokens_mistral': 2273, 'n_tokens_neox': 2070, 'n_words': 377}