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()];
}
--- /dev/null
+#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());
+
+}
+
+}
+}
--- /dev/null
+#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