Skip to content

Commit

Permalink
Initial implementation of sidestake unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescowens committed Jan 23, 2024
1 parent aa3beae commit d2a5b5e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Makefile.test.include
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
85 changes: 85 additions & 0 deletions src/test/gridcoin/sidestake_tests.cpp
Original file line number Diff line number Diff line change
@@ -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 <boost/test/unit_test.hpp>

#include <util.h>
#include <gridcoin/sidestake.h>

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()

0 comments on commit d2a5b5e

Please sign in to comment.