1 #include <cppunit/CompilerOutputter.h> 2 #include <cppunit/TestCaller.h> 3 #include <cppunit/TestFixture.h> 4 #include <cppunit/extensions/TestFactoryRegistry.h> 5 #include <cppunit/ui/text/TestRunner.h> 6 7 #include <stdexcept> 8 9 #include "rng.hh" 10 11 using namespace yapet::pwgen; 12 13 constexpr std::uint8_t HI{5}; 14 15 class RngTest : public CppUnit::TestFixture { 16 public: suite()17 static CppUnit::TestSuite *suite() { 18 CppUnit::TestSuite *suiteOfTests = 19 new CppUnit::TestSuite("Random Number Generator Test"); 20 21 suiteOfTests->addTest(new CppUnit::TestCaller<RngTest>{ 22 "Rng should return random byte", &RngTest::rng}); 23 24 suiteOfTests->addTest(new CppUnit::TestCaller<RngTest>{ 25 "Rng should copy properly", &RngTest::rngCopy}); 26 27 suiteOfTests->addTest(new CppUnit::TestCaller<RngTest>{ 28 "Rng should move properly", &RngTest::rngMove}); 29 30 return suiteOfTests; 31 } 32 rng()33 void rng() { 34 yapet::pwgen::Rng rng{HI}; 35 for (int i = 0; i < 100; i++) { 36 CPPUNIT_ASSERT(rng.getNextInt() <= HI); 37 } 38 } 39 rngCopy()40 void rngCopy() { 41 yapet::pwgen::Rng rng1{HI}; 42 43 yapet::pwgen::Rng copy{rng1}; 44 45 for (int i = 0; i < 10; i++) copy.getNextInt(); 46 47 yapet::pwgen::Rng copy2 = rng1; 48 copy2.getNextInt(); 49 50 for (int i = 0; i < 10; i++) rng1.getNextInt(); 51 } 52 rngMove()53 void rngMove() { 54 yapet::pwgen::Rng rng1{HI}; 55 56 yapet::pwgen::Rng moved{std::move(rng1)}; 57 58 for (int i = 0; i < 10; i++) moved.getNextInt(); 59 CPPUNIT_ASSERT_THROW(rng1.getNextInt(), std::runtime_error); 60 61 yapet::pwgen::Rng moved2 = std::move(moved); 62 for (int i = 0; i < 10; i++) moved2.getNextInt(); 63 CPPUNIT_ASSERT_THROW(moved.getNextInt(), std::runtime_error); 64 } 65 }; 66 main()67int main() { 68 CppUnit::TextUi::TestRunner runner; 69 runner.addTest(RngTest::suite()); 70 return runner.run() ? 0 : 1; 71 }