]> git.localhorst.tv Git - blank.git/commitdiff
test for random [sic] utility methods
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 10 Nov 2016 13:02:58 +0000 (14:02 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 10 Nov 2016 13:02:58 +0000 (14:02 +0100)
src/rand/GaloisLFSR.hpp
tst/rand/GaloisLFSRTest.cpp [new file with mode: 0644]
tst/rand/GaloisLFSRTest.hpp [new file with mode: 0644]

index 81652d85f54a64cd628c22a95d3493b6de6476cd..001173919c1214a6b0931e788f2130c6bdac312f 100644 (file)
@@ -63,10 +63,12 @@ public:
 
        template<class Container>
        typename Container::reference From(Container &c) {
+               assert(c.size() > 0);
                return c[Next<typename Container::size_type>() % c.size()];
        }
        template<class Container>
        typename Container::const_reference From(const Container &c) {
+               assert(c.size() > 0);
                return c[Next<typename Container::size_type>() % c.size()];
        }
 
diff --git a/tst/rand/GaloisLFSRTest.cpp b/tst/rand/GaloisLFSRTest.cpp
new file mode 100644 (file)
index 0000000..05c4f4e
--- /dev/null
@@ -0,0 +1,84 @@
+#include "GaloisLFSRTest.hpp"
+
+#include "rand/GaloisLFSR.hpp"
+
+#include <algorithm>
+#include <sstream>
+
+CPPUNIT_TEST_SUITE_REGISTRATION(blank::test::GaloisLFSRTest);
+
+using namespace std;
+
+
+namespace blank {
+namespace test {
+
+void GaloisLFSRTest::setUp() {
+
+}
+
+void GaloisLFSRTest::tearDown() {
+
+}
+
+void GaloisLFSRTest::testFloatNorm() {
+       GaloisLFSR random(4);
+       for (int i = 0; i < 64; ++i) {
+               float value = random.SNorm();
+               AssertBetween(
+                       "random signed normal float",
+                       -1.0f, 1.0f, value);
+       }
+       for (int i = 0; i < 64; ++i) {
+               float value = random.UNorm();
+               AssertBetween(
+                       "random unsigned normal float",
+                       0.0f, 1.0f, value);
+       }
+}
+
+void GaloisLFSRTest::testFromContainer() {
+       GaloisLFSR random(5);
+       const vector<int> container({ 1, 2, 3, 4, 5 });
+       for (int i = 0; i < 64; ++i) {
+               int element = random.From(container);
+               AssertContains(
+                       "random element from container",
+                       container, element);
+       }
+}
+
+void GaloisLFSRTest::AssertBetween(
+       string message,
+       float minimum,
+       float maximum,
+       float actual
+) {
+       stringstream msg;
+       msg << message << ": " << actual << " not in ["
+               << minimum << ',' << maximum << ']';
+       CPPUNIT_ASSERT_MESSAGE(
+               msg.str(),
+               minimum <= actual && actual <= maximum);
+
+}
+
+void GaloisLFSRTest::AssertContains(
+       string message,
+       const vector<int> &container,
+       int element
+) {
+       stringstream msg;
+       msg << message << ": " << element << " not in { ";
+       for (int i : container) {
+               msg << i << ' ';
+       }
+       msg << '}';
+       CPPUNIT_ASSERT_MESSAGE(
+               msg.str(),
+               find(container.begin(), container.end(), element) != container.end());
+
+}
+
+}
+}
diff --git a/tst/rand/GaloisLFSRTest.hpp b/tst/rand/GaloisLFSRTest.hpp
new file mode 100644 (file)
index 0000000..3669ab4
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef BLANK_TEST_RAND_GALOISLFSRTEST_HPP
+#define BLANK_TEST_RAND_GALOISLFSRTEST_HPP
+
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <string>
+#include <vector>
+
+
+namespace blank {
+
+namespace test {
+
+class GaloisLFSRTest
+: public CppUnit::TestFixture {
+
+CPPUNIT_TEST_SUITE(GaloisLFSRTest);
+
+CPPUNIT_TEST(testFloatNorm);
+CPPUNIT_TEST(testFromContainer);
+
+CPPUNIT_TEST_SUITE_END();
+
+public:
+       void setUp();
+       void tearDown();
+
+       void testFloatNorm();
+       void testFromContainer();
+
+       /// check if value is in range [minimum,maximum]
+       static void AssertBetween(
+               std::string message,
+               float minimum,
+               float maximum,
+               float actual);
+
+       static void AssertContains(
+               std::string message,
+               const std::vector<int> &container,
+               int element);
+
+};
+
+}
+}
+
+#endif