Skip to content
Bitcoin Roots

v31.1-roots.1 · Commit 11: [PATCH 11/17] fix(test): bound policy oracles and retain CI diagnostics

src/test/util/roots_policy_oracles.h

Before: /dev/null · After: src/test/util/roots_policy_oracles.h

File metadata
new file mode 100644
index 0000000000000000000000000000000000000000..b0d8efc5e6ddc8ffaae961bc9eb4af8630c660ca

Download original patch · Release notes

Diff rows 1–47 of 47. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. The two line-number columns refer to before and after.

src/test/util/roots_policy_oracles.h ADDED
@@ -0,0 +1,47 @@
1
+ // Copyright (c) 2026 The Bitcoin Roots 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
+ #ifndef BITCOIN_TEST_UTIL_ROOTS_POLICY_ORACLES_H
6
+ #define BITCOIN_TEST_UTIL_ROOTS_POLICY_ORACLES_H
7
+
8
+ #include <cstdint>
9
+ #include <limits>
10
+ #include <string_view>
11
+
12
+ namespace roots_test {
13
+ // Convert a signed reference result without asking multiprecision to convert a
14
+ // negative magnitude to a signed native integer. Handle INT64_MIN separately:
15
+ // its positive magnitude is not representable as int64_t.
16
+ template <typename Wide>
17
+ int64_t BoundedSignedReference(const Wide& value)
18
+ {
19
+ constexpr auto minimum{std::numeric_limits<int64_t>::min()};
20
+ constexpr auto maximum{std::numeric_limits<int64_t>::max()};
21
+ if (value <= minimum) return minimum;
22
+ if (value >= maximum) return maximum;
23
+ if (value < 0) return -static_cast<int64_t>(-value);
24
+ return static_cast<int64_t>(value);
25
+ }
26
+
27
+ // Decimal accumulation with an explicit bound is independent of ToIntegral.
28
+ inline bool ValidInteger(std::string_view value, uint64_t maximum)
29
+ {
30
+ bool negative{false};
31
+ if (!value.empty() && (value.front() == '+' || value.front() == '-')) {
32
+ negative = value.front() == '-';
33
+ value.remove_prefix(1);
34
+ }
35
+ if (value.empty()) return false;
36
+ uint64_t number{0};
37
+ for (const char digit : value) {
38
+ if (digit < '0' || digit > '9') return false;
39
+ const auto numeric_digit{static_cast<uint64_t>(digit - '0')};
40
+ if (number > maximum / 10 || (number == maximum / 10 && numeric_digit > maximum % 10)) return false;
41
+ number = number * 10 + numeric_digit;
42
+ }
43
+ return !negative || number == 0;
44
+ }
45
+ } // namespace roots_test
46
+
47
+ #endif // BITCOIN_TEST_UTIL_ROOTS_POLICY_ORACLES_H