X-Git-Url: https://git.localhorst.tv/?a=blobdiff_plain;f=tst%2Fmath%2FGaloisLFSRTest.cpp;fp=tst%2Fmath%2FGaloisLFSRTest.cpp;h=958ac765fe9d0710a69653e3585dd79ec2e907e7;hb=8f6530c75730f901efd6708e4fde7e68a178adf1;hp=0000000000000000000000000000000000000000;hpb=c51b3bbef5c9b8ab52f55d46f08c5992fe574f70;p=blobs.git diff --git a/tst/math/GaloisLFSRTest.cpp b/tst/math/GaloisLFSRTest.cpp new file mode 100644 index 0000000..958ac76 --- /dev/null +++ b/tst/math/GaloisLFSRTest.cpp @@ -0,0 +1,86 @@ +#include "GaloisLFSRTest.hpp" + +#include "math/GaloisLFSR.hpp" + +#include +#include + +CPPUNIT_TEST_SUITE_REGISTRATION(blobs::math::test::GaloisLFSRTest); + +using namespace std; + + +namespace blobs { +namespace math { +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()); + +} + +} +} +}