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