16#include <boost/test/unit_test.hpp>
112 for (
int i = 0; i < 100000; i++) {
118 for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
125 for (
int i = 0; i < 100000; i++) {
128 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
131 for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
132 uint64_t j = std::numeric_limits<uint64_t>::max();
134 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
162 std::vector<char>::size_type i, j;
172 BOOST_CHECK_MESSAGE((i-1) == j,
"decoded:" << j <<
" expected:" << (i-1));
174 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
180 std::ios_base::failure expectedException(
"non-canonical ReadCompactSize()");
186 return strcmp(expectedException.what(), ex.what()) == 0;
191 std::vector<uint8_t> vec1{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
192 std::vector<bool> vec2{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
194 BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
200 std::array<uint8_t, 32> array1{1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1};
203 std::array<uint8_t, 32> array2;
213 std::vector<char>::size_type n;
216 ss << std::span{
"\xfd\x00\x00"}.first(3);
220 ss << std::span{
"\xfd\xfc\x00"}.first(3);
224 ss << std::span{
"\xfd\xfd\x00"}.first(3);
229 ss << std::span{
"\xfe\x00\x00\x00\x00"}.first(5);
233 ss << std::span{
"\xfe\xff\xff\x00\x00"}.first(5);
237 ss << std::span{
"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.first(9);
241 ss << std::span{
"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.first(9);
247 const std::string_view sv{
"hello, world"};
257 const std::vector<int> v = {1,2,3,4,-5,-6,-7,-8,-9,-10,10000,20000,-30000};
259 auto check = [&]<
size_t N>() {
265 BOOST_CHECK_LE(r.size(), N);
267 }
catch (
const std::ios_base::failure&) {
268 BOOST_CHECK_GT(v.size(), N);
271 check.operator()<0>();
272 check.operator()<10>();
273 check.operator()<12>();
274 check.operator()<13>();
275 check.operator()<14>();
276 check.operator()<100>();
283 std::string stringval(
"testing");
284 const uint8_t charstrval[16]{
"testing charstr"};
302 ss2 << intval << boolval << stringval << charstrval <<
TX_WITH_WITNESS(txval);
307 const std::string in{
"ab"};
308 ds << std::span{in} << std::byte{
'c'};
309 std::array<std::byte, 2> out;
311 ds >> std::span{out} >> out_3;
337 template <
typename Stream>
340 if (s.template GetParams<BaseFormat>().m_base_format ==
BaseFormat::RAW) {
347 template <
typename Stream>
350 if (s.template GetParams<BaseFormat>().m_base_format ==
BaseFormat::RAW) {
353 std::string hex{
"aa"};
354 s >> std::span{hex}.first(hex.size());
383 if (ser_action.ForRead()) {
386 SER_READ(obj, obj.m_derived_data = str);
406 template <
typename Stream>
409 const uint8_t param = s.template GetParams<OtherParam>().param;
413 template <
typename Stream>
416 const uint8_t param = s.template GetParams<OtherParam>().param;
432 const OtherParam other_param_used{.param = 0x10};
433 const OtherParam other_param_ignored{.param = 0x11};
434 const OtherParam other_param_override{.param = 0x12};
437 ParamsStream pstream{stream,
RAW, other_param_used, other_param_ignored};
440 pstream << base1 << check << other_param_override(check);
444 pstream >> base2 >> check >> other_param_override(check);
490 std::vector<Base> v{
Base{0x0F},
Base{0xFF}};
497 v[0].m_base_data = 0;
498 v[1].m_base_data = 0;
509 v[0].m_base_data = 0;
510 v[1].m_base_data = 0;
(Un)serialize a number as raw byte or 2 hexadecimal chars.
void Unserialize(Stream &s)
void Serialize(Stream &s) const
CSerializeMethodsTestSingle()=default
SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const uint8_t *charstrvalin, const CTransactionRef &txvalin)
CSerializeMethodsTestSingle()=default
SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)
bool operator==(const CSerializeMethodsTestSingle &rhs) const
Double ended buffer combining vector and stream-like interfaces.
vector_type::size_type size_type
std::string m_derived_data
SERIALIZE_METHODS(Derived, obj)
A writer stream (for serialization) that computes a 256-bit hash.
void Unserialize(Stream &s) const
void Serialize(Stream &s) const
Wrapper that overrides the GetParams() function of a stream.
auto & GetStream()
Get reference to underlying stream.
UncopyableStream & operator=(const UncopyableStream &)=delete
UncopyableStream(const UncopyableStream &)=delete
UncopyableStream(UncopyableStream &&) noexcept=default
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
BOOST_CHECK_EXCEPTION(req.LoadBody(reader), std::runtime_error, HasReason{"Cannot parse Content-Length value"})
BOOST_AUTO_TEST_CASE(ipc_tests)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
constexpr TransactionSerParams TX_WITH_WITNESS
std::shared_ptr< const CTransaction > CTransactionRef
#define VARINT_MODE(obj, mode)
#define SER_PARAMS(type)
Formatter methods can retrieve parameters attached to a stream using the SER_PARAMS(type) macro as lo...
constexpr uint64_t MAX_SIZE
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size...
#define LIMITED_VECTOR(obj, n)
Out & AsBase(In &x)
Convert any argument to a reference to X, maintaining constness.
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
#define SER_READ(obj, code)
#define SER_PARAMS_OPFUNC
Helper macro for SerParams structs.
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
uint64_t GetSerializeSize(const T &t)
constexpr DerivedAndBaseFormat HEX_UPPER
constexpr DerivedAndBaseFormat RAW_LOWER
static bool isCanonicalException(const std::ios_base::failure &ex)
auto MakeByteSpan(const V &v) noexcept
A mutable version of CTransaction.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
std::optional< std::vector< Byte > > TryParseHex(std::string_view str)
Parse the hex string into bytes (uint8_t or std::byte).
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.