Bitcoin Core 28.99.0
P2P Digital Currency
feefrac_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2024-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <util/feefrac.h>
6#include <random.h>
7
8#include <boost/test/unit_test.hpp>
9
10BOOST_AUTO_TEST_SUITE(feefrac_tests)
11
12BOOST_AUTO_TEST_CASE(feefrac_operators)
13{
14 FeeFrac p1{1000, 100}, p2{500, 300};
15 FeeFrac sum{1500, 400};
16 FeeFrac diff{500, -200};
17 FeeFrac empty{0, 0};
18 [[maybe_unused]] FeeFrac zero_fee{0, 1}; // zero-fee allowed
19
20 BOOST_CHECK(empty == FeeFrac{}); // same as no-args
21
22 BOOST_CHECK(p1 == p1);
23 BOOST_CHECK(p1 + p2 == sum);
24 BOOST_CHECK(p1 - p2 == diff);
25
26 FeeFrac p3{2000, 200};
27 BOOST_CHECK(p1 != p3); // feefracs only equal if both fee and size are same
28 BOOST_CHECK(p2 != p3);
29
30 FeeFrac p4{3000, 300};
31 BOOST_CHECK(p1 == p4-p3);
32 BOOST_CHECK(p1 + p3 == p4);
33
34 // Fee-rate comparison
35 BOOST_CHECK(p1 > p2);
36 BOOST_CHECK(p1 >= p2);
37 BOOST_CHECK(p1 >= p4-p3);
38 BOOST_CHECK(!(p1 >> p3)); // not strictly better
39 BOOST_CHECK(p1 >> p2); // strictly greater feerate
40
41 BOOST_CHECK(p2 < p1);
42 BOOST_CHECK(p2 <= p1);
43 BOOST_CHECK(p1 <= p4-p3);
44 BOOST_CHECK(!(p3 << p1)); // not strictly worse
45 BOOST_CHECK(p2 << p1); // strictly lower feerate
46
47 // "empty" comparisons
48 BOOST_CHECK(!(p1 >> empty)); // << will always result in false
49 BOOST_CHECK(!(p1 << empty));
50 BOOST_CHECK(!(empty >> empty));
51 BOOST_CHECK(!(empty << empty));
52
53 // empty is always bigger than everything else
54 BOOST_CHECK(empty > p1);
55 BOOST_CHECK(empty > p2);
56 BOOST_CHECK(empty > p3);
57 BOOST_CHECK(empty >= p1);
58 BOOST_CHECK(empty >= p2);
59 BOOST_CHECK(empty >= p3);
60
61 // check "max" values for comparison
62 FeeFrac oversized_1{4611686000000, 4000000};
63 FeeFrac oversized_2{184467440000000, 100000};
64
65 BOOST_CHECK(oversized_1 < oversized_2);
66 BOOST_CHECK(oversized_1 <= oversized_2);
67 BOOST_CHECK(oversized_1 << oversized_2);
68 BOOST_CHECK(oversized_1 != oversized_2);
69
70 // Tests paths that use double arithmetic
71 FeeFrac busted{(static_cast<int64_t>(INT32_MAX)) + 1, INT32_MAX};
72 BOOST_CHECK(!(busted < busted));
73
74 FeeFrac max_fee{2100000000000000, INT32_MAX};
75 BOOST_CHECK(!(max_fee < max_fee));
76 BOOST_CHECK(!(max_fee > max_fee));
77 BOOST_CHECK(max_fee <= max_fee);
78 BOOST_CHECK(max_fee >= max_fee);
79
80 FeeFrac max_fee2{1, 1};
81 BOOST_CHECK(max_fee >= max_fee2);
82
83}
84
BOOST_AUTO_TEST_SUITE_END()
volatile double sum
Definition: examples.cpp:10
BOOST_AUTO_TEST_CASE(feefrac_operators)
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Data structure storing a fee and size, ordered by increasing fee/size.
Definition: feefrac.h:39