From d2a5b5e9c4d7b9c864167748b18e4ca933149c9a Mon Sep 17 00:00:00 2001 From: "James C. Owens" Date: Tue, 23 Jan 2024 16:11:45 -0500 Subject: [PATCH] Initial implementation of sidestake unit tests --- src/Makefile.test.include | 1 + src/test/CMakeLists.txt | 1 + src/test/gridcoin/sidestake_tests.cpp | 85 +++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) create mode 100644 src/test/gridcoin/sidestake_tests.cpp diff --git a/src/Makefile.test.include b/src/Makefile.test.include index 6c4da11b4e..53d41ea1a5 100755 --- a/src/Makefile.test.include +++ b/src/Makefile.test.include @@ -56,6 +56,7 @@ GRIDCOIN_TESTS =\ test/gridcoin/protocol_tests.cpp \ test/gridcoin/researcher_tests.cpp \ test/gridcoin/scraper_registry_tests.cpp \ + test/gridcoin/sidestake_tests.cpp \ test/gridcoin/superblock_tests.cpp \ test/key_tests.cpp \ test/merkle_tests.cpp \ diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 7bed59e789..ebc70baa79 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -30,6 +30,7 @@ add_executable(test_gridcoin gridcoin/protocol_tests.cpp gridcoin/researcher_tests.cpp gridcoin/scraper_registry_tests.cpp + gridcoin/sidestake_tests.cpp gridcoin/superblock_tests.cpp key_tests.cpp merkle_tests.cpp diff --git a/src/test/gridcoin/sidestake_tests.cpp b/src/test/gridcoin/sidestake_tests.cpp new file mode 100644 index 0000000000..e4f3ef5525 --- /dev/null +++ b/src/test/gridcoin/sidestake_tests.cpp @@ -0,0 +1,85 @@ +// Copyright (c) 2024 The Gridcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or https://opensource.org/licenses/mit-license.php. + +#include + +#include +#include + +BOOST_AUTO_TEST_SUITE(sidestake_tests) + +BOOST_AUTO_TEST_CASE(sidestake_Allocation_Initialization_trivial) +{ + GRC::Allocation allocation; + + BOOST_CHECK_EQUAL(allocation.GetNumerator(), 0); + BOOST_CHECK_EQUAL(allocation.GetDenominator(), 1); + BOOST_CHECK_EQUAL(allocation.IsSimplified(), true); + BOOST_CHECK_EQUAL(allocation.IsZero(), true); + BOOST_CHECK_EQUAL(allocation.IsPositive(), false); + BOOST_CHECK_EQUAL(allocation.IsNonNegative(), true); + BOOST_CHECK_EQUAL(allocation.ToCAmount(), (CAmount) 0); +} + +BOOST_AUTO_TEST_CASE(sidestake_Allocation_Initialization_from_double) +{ + GRC::Allocation allocation((double) 0.0005); + + BOOST_CHECK_EQUAL(allocation.GetNumerator(), 1); + BOOST_CHECK_EQUAL(allocation.GetDenominator(), 2000); + BOOST_CHECK_EQUAL(allocation.IsSimplified(), true); + BOOST_CHECK_EQUAL(allocation.IsZero(), false); + BOOST_CHECK_EQUAL(allocation.IsPositive(), true); + BOOST_CHECK_EQUAL(allocation.IsNonNegative(), true); + BOOST_CHECK_EQUAL(allocation.ToCAmount(), (CAmount) 0); +} + +BOOST_AUTO_TEST_CASE(sidestake_Allocation_Initialization_from_fraction) +{ + GRC::Allocation allocation(Fraction(2500, 10000)); + + BOOST_CHECK_EQUAL(allocation.GetNumerator(), 2500); + BOOST_CHECK_EQUAL(allocation.GetDenominator(), 10000); + BOOST_CHECK_EQUAL(allocation.IsSimplified(), false); + BOOST_CHECK_EQUAL(allocation.IsZero(), false); + BOOST_CHECK_EQUAL(allocation.IsPositive(), true); + BOOST_CHECK_EQUAL(allocation.IsNonNegative(), true); + BOOST_CHECK_EQUAL(allocation.ToCAmount(), (CAmount) 0); + + allocation.Simplify(); + + BOOST_CHECK_EQUAL(allocation.GetNumerator(), 1); + BOOST_CHECK_EQUAL(allocation.GetDenominator(), 4); + BOOST_CHECK_EQUAL(allocation.IsSimplified(), true); +} + +BOOST_AUTO_TEST_CASE(sidestake_Allocation_ToPercent) +{ + GRC::Allocation allocation((double) 0.0005); + + BOOST_CHECK(std::abs(allocation.ToPercent() - (double) 0.05) < 1e-08); +} + +BOOST_AUTO_TEST_CASE(sidestake_Allocation_multiplication) +{ + // Multiplication is a very common operation with Allocations, because + // the general pattern is to multiply the allocation times a CAmount rewards + // to determine the rewards in Halfords (CAmount) to put on the output. + + // Allocations that are initialized from doubles are rounded to the nearest 1/10000. This is the worst case + // therefore, in terms of numerator and denominator. + GRC::Allocation allocation(Fraction(9999, 10000, true)); + + BOOST_CHECK_EQUAL(allocation.GetNumerator(), 9999); + BOOST_CHECK_EQUAL(allocation.GetDenominator(), 10000); + BOOST_CHECK_EQUAL(allocation.IsSimplified(), true); + + CAmount max_accrual = 16384 * COIN; + + allocation *= max_accrual; + + BOOST_CHECK_EQUAL(allocation.ToCAmount(), int64_t {1638236160000}); +} + +BOOST_AUTO_TEST_SUITE_END()