]> git.localhorst.tv Git - blank.git/blob - tst/rand/GaloisLFSRTest.cpp
test for random [sic] utility methods
[blank.git] / tst / rand / GaloisLFSRTest.cpp
1 #include "GaloisLFSRTest.hpp"
2
3 #include "rand/GaloisLFSR.hpp"
4
5 #include <algorithm>
6 #include <sstream>
7
8 CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::GaloisLFSRTest);
9
10 using namespace std;
11
12
13 namespace blank {
14 namespace test {
15
16 void GaloisLFSRTest::setUp() {
17
18 }
19
20 void GaloisLFSRTest::tearDown() {
21
22 }
23
24 void GaloisLFSRTest::testFloatNorm() {
25         GaloisLFSR random(4);
26         for (int i = 0; i < 64; ++i) {
27                 float value = random.SNorm();
28                 AssertBetween(
29                         "random signed normal float",
30                         -1.0f, 1.0f, value);
31         }
32         for (int i = 0; i < 64; ++i) {
33                 float value = random.UNorm();
34                 AssertBetween(
35                         "random unsigned normal float",
36                         0.0f, 1.0f, value);
37         }
38 }
39
40 void GaloisLFSRTest::testFromContainer() {
41         GaloisLFSR random(5);
42         const vector<int> container({ 1, 2, 3, 4, 5 });
43         for (int i = 0; i < 64; ++i) {
44                 int element = random.From(container);
45                 AssertContains(
46                         "random element from container",
47                         container, element);
48         }
49 }
50
51 void GaloisLFSRTest::AssertBetween(
52         string message,
53         float minimum,
54         float maximum,
55         float actual
56 ) {
57         stringstream msg;
58         msg << message << ": " << actual << " not in ["
59                 << minimum << ',' << maximum << ']';
60         CPPUNIT_ASSERT_MESSAGE(
61                 msg.str(),
62                 minimum <= actual && actual <= maximum);
63
64 }
65
66 void GaloisLFSRTest::AssertContains(
67         string message,
68         const vector<int> &container,
69         int element
70 ) {
71         stringstream msg;
72         msg << message << ": " << element << " not in { ";
73         for (int i : container) {
74                 msg << i << ' ';
75         }
76         msg << '}';
77         CPPUNIT_ASSERT_MESSAGE(
78                 msg.str(),
79                 find(container.begin(), container.end(), element) != container.end());
80
81 }
82
83 }
84 }