From: Daniel Karbach Date: Thu, 10 Nov 2016 13:02:58 +0000 (+0100) Subject: test for random [sic] utility methods X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=065bafb23d02edbd99f5bfafe803d8ad75398263;p=blank.git test for random [sic] utility methods --- diff --git a/src/rand/GaloisLFSR.hpp b/src/rand/GaloisLFSR.hpp index 81652d8..0011739 100644 --- a/src/rand/GaloisLFSR.hpp +++ b/src/rand/GaloisLFSR.hpp @@ -63,10 +63,12 @@ public: template typename Container::reference From(Container &c) { + assert(c.size() > 0); return c[Next() % c.size()]; } template typename Container::const_reference From(const Container &c) { + assert(c.size() > 0); return c[Next() % c.size()]; } diff --git a/tst/rand/GaloisLFSRTest.cpp b/tst/rand/GaloisLFSRTest.cpp new file mode 100644 index 0000000..05c4f4e --- /dev/null +++ b/tst/rand/GaloisLFSRTest.cpp @@ -0,0 +1,84 @@ +#include "GaloisLFSRTest.hpp" + +#include "rand/GaloisLFSR.hpp" + +#include +#include + +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 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 &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 index 0000000..3669ab4 --- /dev/null +++ b/tst/rand/GaloisLFSRTest.hpp @@ -0,0 +1,48 @@ +#ifndef BLANK_TEST_RAND_GALOISLFSRTEST_HPP +#define BLANK_TEST_RAND_GALOISLFSRTEST_HPP + +#include + +#include +#include + + +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 &container, + int element); + +}; + +} +} + +#endif