Developer Documentation

unittests_randomNumberGenerator.cc
1
2 #include <gtest/gtest.h>
3 #include <OpenMesh/Core/Utils/RandomNumberGenerator.hh>
4
5 namespace {
6
7 class RandomNumberGenerator : public testing::Test {
8
9 protected:
10
11 // This function is called before each test is run
12 virtual void SetUp() {
13
14 // Do some initial stuff with the member data here...
15 }
16
17 // This function is called after all tests are through
18 virtual void TearDown() {
19
20 // Do some final stuff with the member data here...
21 }
22
23 // Member already defined in OpenMeshBase
24 //Mesh mesh_;
25 };
26
27 /*
28 * ====================================================================
29 * Define tests below
30 * ====================================================================
31 */
32
33 TEST_F(RandomNumberGenerator, RandomNumberGeneratorMaxTestHighres) {
34
35
36 OpenMesh::RandomNumberGenerator rng(100000000);
37
38 unsigned int lowerZero = 0;
39 unsigned int above1 = 0;
40
41 double average = 0.0;
42
43 for ( unsigned int i = 0 ; i < 100000000 ; ++i) {
44 double randomNumber = rng.getRand();
45 if ( randomNumber < 0.0 )
46 lowerZero++;
47
48 if ( randomNumber > 1.0 )
49 above1++;
50
51 average += randomNumber;
52 }
53
54 average /= 100000000.0;
55
56 EXPECT_EQ(0u, lowerZero ) << "Below zero!";
57 EXPECT_EQ(0u, above1 ) << "Above zero!";
58
59 EXPECT_TRUE( (average - 0.5) < 0.01 ) << "Expected value not 0.5";
60 }
61
62 TEST_F(RandomNumberGenerator, RandomNumberGeneratorMaxTestLowres) {
63
64 OpenMesh::RandomNumberGenerator rng(1000);
65
66 unsigned int lowerZero = 0;
67 unsigned int above1 = 0;
68
69 double average = 0.0;
70
71 for ( unsigned int i = 0 ; i < 10000000 ; ++i) {
72
73 double randomNumber = rng.getRand();
74 if ( randomNumber < 0.0 )
75 lowerZero++;
76
77 if ( randomNumber > 1.0 ) {
78 above1++;
79 }
80
81 average += randomNumber;
82 }
83
84 average /= 10000000.0;
85
86 EXPECT_EQ(0u, lowerZero ) << "Below zero!";
87 EXPECT_EQ(0u, above1 ) << "Above zero!";
88
89 EXPECT_TRUE( (average - 0.5) < 0.01 ) << "Expected value not 0.5";
90 }
91
92 }
OpenMesh::RandomNumberGenerator
Definition: RandomNumberGenerator.hh:77