Project
Loading...
Searching...
No Matches
test_DataGenerator.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
16
17#define BOOST_TEST_MODULE Utility test
18#define BOOST_TEST_MAIN
19#define BOOST_TEST_DYN_LINK
20#include <boost/test/unit_test.hpp>
21#include "DataGenerator.h"
22#include <iostream>
23#include <iomanip>
24#include <vector>
25
26template <typename DistributionType, typename... Args>
27bool testWithDistribution(Args&&... args)
28{
29 using value_type = typename DistributionType::result_type;
30 o2::test::DataGenerator<value_type, DistributionType> dg(std::forward<Args>(args)...);
31
32 std::vector<int> throws(dg.nbins);
33 const unsigned nRolls = 1000000;
34
35 for (unsigned n = 0; n < nRolls; n++) {
36 value_type v = dg();
37 unsigned bin = v / dg.step - dg.min;
38 BOOST_REQUIRE(bin < dg.nbins);
39 throws[bin]++;
40 }
41
42 int mostAbundantValueBin = 0;
43 int mostAbundantValueCount = 0;
44 auto highestProbability = dg.getProbability(dg.min);
45 highestProbability = 0;
46 for (auto i : dg) {
47 int bin = i / dg.step - dg.min;
48 BOOST_REQUIRE(bin >= 0);
49 if (mostAbundantValueCount < throws[bin]) {
50 mostAbundantValueBin = bin;
51 mostAbundantValueCount = throws[bin];
52 }
53 if (highestProbability < dg.getProbability(i)) {
54 highestProbability = dg.getProbability(i);
55 }
56 std::cout << std::setw(4) << std::right << i << ": " //
57 << std::setw(11) << std::left << dg.getProbability(i) //
58 << " -- " //
59 << throws[bin] //
60 << std::endl; //
61 }
62 std::vector<int> mostProbableValueBins;
63 for (auto i : dg) {
64 int bin = i / dg.step - dg.min;
65 if (dg.getProbability(i) >= highestProbability) {
66 mostProbableValueBins.push_back(bin);
67 }
68 }
69 auto& list = mostProbableValueBins;
70 BOOST_CHECK(std::find(list.begin(), list.end(), mostAbundantValueBin) != list.end());
71
72 return true;
73}
74
75BOOST_AUTO_TEST_CASE(test_DataGenerator)
76{
77 std::cout << "Testing normal distribution" << std::endl;
78 using normal_distribution = o2::test::normal_distribution<double>;
79 testWithDistribution<normal_distribution>(-7.5, 7.5, 1., 0., 1.);
80
81 std::cout << "Testing poisson distribution" << std::endl;
82 using poisson_distribution = o2::test::poisson_distribution<int>;
83 testWithDistribution<poisson_distribution>(0, 15, 1, 3);
84
85 std::cout << "Testing geometric distribution" << std::endl;
86 using geometric_distribution = o2::test::geometric_distribution<int>;
87 testWithDistribution<geometric_distribution>(0, 31, 1, 0.3);
88}
A simple data generator.
int32_t i
A simple data generator.
const result_type step
const size_type nbins
double getProbability(value_type v) const
get theoretical probability of a value
const result_type min
specialization of std::geometric_distribution which implements also the analytic formula.
specialization of std::normal_distribution which implements also the analytic formula.
specialization of std::poisson_distribution which implements also the analytic formula.
GLdouble n
Definition glcorearb.h:1982
const GLdouble * v
Definition glcorearb.h:832
Definition list.h:40
bool testWithDistribution(Args &&... args)
BOOST_AUTO_TEST_CASE(test_DataGenerator)
size_t nRolls
BOOST_CHECK(tree)