diff --git a/src/Makefile.test.include b/src/Makefile.test.include --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -65,6 +65,7 @@ test/jsonutil.cpp \ test/jsonutil.h \ test/key_tests.cpp \ + test/lcg_tests.cpp \ test/limitedmap_tests.cpp \ test/main_tests.cpp \ test/mempool_tests.cpp \ diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -82,6 +82,7 @@ inv_tests.cpp jsonutil.cpp key_tests.cpp + lcg_tests.cpp limitedmap_tests.cpp main_tests.cpp mempool_tests.cpp diff --git a/src/test/lcg.h b/src/test/lcg.h new file mode 100644 --- /dev/null +++ b/src/test/lcg.h @@ -0,0 +1,23 @@ +// Copyright (c) 2019 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_TEST_LCH_H +#define BITCOIN_TEST_LCH_H + +#include + +// Simple 64-bit linear congruential generator by Donald Knuth. +class LCG64 { + uint64_t state; + +public: + uint64_t next() { + uint64_t oldstate = state; + state = oldstate * 6364136223846793005 + 1442695040888963407; + return oldstate; + } + LCG64(uint64_t initialstate = 0) : state(initialstate) {} +}; + +#endif diff --git a/src/test/lcg_tests.cpp b/src/test/lcg_tests.cpp new file mode 100644 --- /dev/null +++ b/src/test/lcg_tests.cpp @@ -0,0 +1,22 @@ +// Copyright (c) 2019 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "test/lcg.h" + +#include + +BOOST_AUTO_TEST_CASE(lcg_tests) { + + LCG64 lcg; + // We want that the first iteration is 0 which is a helpful special + // case. + BOOST_REQUIRE_EQUAL(lcg.next(), 0); + for (int i = 0; i < 99; i++) { + lcg.next(); + } + // Make sure the LCG is producing expected value after many iterations. + // This ensures mul and add overflows are acting as expected on this + // architecture. + BOOST_REQUIRE_EQUAL(lcg.next(), 0xf306b780aa88c194); +}