ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
random_test.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #define TEST_NAME "util/random"
33 #include "util/testing.h"
34 
35 #include <numeric>
36 
37 #include "util/math.h"
38 #include "util/random.h"
39 
40 using namespace colmap;
41 
42 BOOST_AUTO_TEST_CASE(TestPRNGSeed) {
43  BOOST_CHECK(PRNG == nullptr);
44  SetPRNGSeed();
45  BOOST_CHECK(PRNG != nullptr);
46  SetPRNGSeed(0);
47  BOOST_CHECK(PRNG != nullptr);
48 }
49 
50 BOOST_AUTO_TEST_CASE(TestRepeatability) {
51  SetPRNGSeed(0);
52  std::vector<int> numbers1;
53  for (size_t i = 0; i < 100; ++i) {
54  numbers1.push_back(RandomInteger(0, 10000));
55  }
56  SetPRNGSeed(1);
57  std::vector<int> numbers2;
58  for (size_t i = 0; i < 100; ++i) {
59  numbers2.push_back(RandomInteger(0, 10000));
60  }
61  SetPRNGSeed(0);
62  std::vector<int> numbers3;
63  for (size_t i = 0; i < 100; ++i) {
64  numbers3.push_back(RandomInteger(0, 10000));
65  }
66  BOOST_CHECK_EQUAL_COLLECTIONS(numbers1.begin(), numbers1.end(),
67  numbers3.begin(), numbers3.end());
68  bool all_equal = true;
69  for (size_t i = 0; i < numbers1.size(); ++i) {
70  if (numbers1[i] != numbers2[i]) {
71  all_equal = false;
72  }
73  }
74  BOOST_CHECK(!all_equal);
75 }
76 
77 BOOST_AUTO_TEST_CASE(TestRandomInteger) {
78  SetPRNGSeed();
79  for (size_t i = 0; i < 1000; ++i) {
80  BOOST_CHECK_GE(RandomInteger(-100, 100), -100);
81  BOOST_CHECK_LE(RandomInteger(-100, 100), 100);
82  }
83 }
84 
85 BOOST_AUTO_TEST_CASE(TestRandomReal) {
86  SetPRNGSeed();
87  for (size_t i = 0; i < 1000; ++i) {
88  BOOST_CHECK_GE(RandomReal(-100.0, 100.0), -100.0);
89  BOOST_CHECK_LE(RandomReal(-100.0, 100.0), 100.0);
90  }
91 }
92 
93 BOOST_AUTO_TEST_CASE(TestRandomGaussian) {
94  SetPRNGSeed(0);
95  const double kMean = 1.0;
96  const double kSigma = 1.0;
97  const size_t kNumValues = 100000;
98  std::vector<double> values;
99  for (size_t i = 0; i < kNumValues; ++i) {
100  values.push_back(RandomGaussian(kMean, kSigma));
101  }
102  BOOST_CHECK_LE(std::abs(Mean(values) - kMean), 1e-2);
103  BOOST_CHECK_LE(std::abs(StdDev(values) - kSigma), 1e-2);
104 }
105 
106 BOOST_AUTO_TEST_CASE(TestShuffleNone) {
107  SetPRNGSeed();
108  std::vector<int> numbers(0);
109  Shuffle(0, &numbers);
110  numbers = {1, 2, 3, 4, 5};
111  std::vector<int> shuffled_numbers = numbers;
112  Shuffle(0, &shuffled_numbers);
113  BOOST_CHECK_EQUAL_COLLECTIONS(numbers.begin(), numbers.end(),
114  shuffled_numbers.begin(),
115  shuffled_numbers.end());
116 }
117 
118 BOOST_AUTO_TEST_CASE(TestShuffleAll) {
119  SetPRNGSeed(0);
120  std::vector<int> numbers(1000);
121  std::iota(numbers.begin(), numbers.end(), 0);
122  std::vector<int> shuffled_numbers = numbers;
123  Shuffle(1000, &shuffled_numbers);
124  size_t num_shuffled = 0;
125  for (size_t i = 0; i < numbers.size(); ++i) {
126  if (numbers[i] != shuffled_numbers[i]) {
127  num_shuffled += 1;
128  }
129  }
130  BOOST_CHECK_GT(num_shuffled, 0);
131 }
const double * e
void SetPRNGSeed(unsigned seed)
Definition: random.cc:40
double StdDev(const std::vector< T > &elems)
Definition: math.h:248
thread_local std::unique_ptr< std::mt19937 > PRNG
Definition: random.cc:38
T RandomInteger(const T min, const T max)
Definition: random.h:64
double Mean(const std::vector< T > &elems)
Definition: math.h:227
T RandomGaussian(const T mean, const T stddev)
Definition: random.h:86
T RandomReal(const T min, const T max)
Definition: random.h:75
void Shuffle(const uint32_t num_to_shuffle, std::vector< T > *elems)
Definition: random.h:96
BOOST_AUTO_TEST_CASE(TestPRNGSeed)
Definition: random_test.cc:42