14 #include <boost/test/unit_test.hpp>
28 CSerializeMethodsTestSingle(
int intvalin,
bool boolvalin, std::string stringvalin,
const uint8_t* charstrvalin,
const CTransactionRef& txvalin) : intval(intvalin), boolval(boolvalin), stringval(
std::move(stringvalin)), txval(txvalin)
30 memcpy(charstrval, charstrvalin,
sizeof(charstrval));
44 return intval == rhs.
intval &&
98 for (
int i = 0; i < 100000; i++) {
104 for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
111 for (
int i = 0; i < 100000; i++) {
114 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
117 for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
118 uint64_t j = std::numeric_limits<uint64_t>::max();
120 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
148 std::vector<char>::size_type i, j;
158 BOOST_CHECK_MESSAGE((i-1) == j,
"decoded:" << j <<
" expected:" << (i-1));
160 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
166 std::ios_base::failure expectedException(
"non-canonical ReadCompactSize()");
172 return strcmp(expectedException.what(), ex.what()) == 0;
177 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};
178 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};
180 BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
186 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};
189 std::array<uint8_t, 32> array2;
199 std::vector<char>::size_type n;
215 ss <<
Span{
"\xfe\x00\x00\x00\x00"}.
first(5);
219 ss <<
Span{
"\xfe\xff\xff\x00\x00"}.
first(5);
223 ss <<
Span{
"\xff\x00\x00\x00\x00\x00\x00\x00\x00"}.
first(9);
227 ss <<
Span{
"\xff\xff\xff\xff\x01\x00\x00\x00\x00"}.
first(9);
235 std::string stringval(
"testing");
236 const uint8_t charstrval[16]{
"testing charstr"};
254 ss2 << intval << boolval << stringval << charstrval <<
TX_WITH_WITNESS(txval);
259 const std::string in{
"ab"};
260 ds <<
Span{in} << std::byte{
'c'};
261 std::array<std::byte, 2>
out;
289 template <
typename Stream>
299 template <
typename Stream>
305 std::string hex{
"aa"};
307 m_base_data = TryParseHex<uint8_t>(hex).value().at(0);
332 READWRITE(fmt.m_base_format(AsBase<Base>(obj)));
334 if (ser_action.ForRead()) {
337 SER_READ(obj, obj.m_derived_data = str);
371 std::vector<Base> v{
Base{0x0F},
Base{0xFF}};
378 v[0].m_base_data = 0;
379 v[1].m_base_data = 0;
390 v[0].m_base_data = 0;
391 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
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_PARAMS(Derived, obj, DerivedAndBaseFormat, fmt)
A writer stream (for serialization) that computes a 256-bit hash.
A Span is an object that can refer to a contiguous sequence of objects.
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static constexpr TransactionSerParams TX_WITH_WITNESS
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
size_t GetSerializeSize(const T &t)
#define VARINT_MODE(obj, mode)
static constexpr uint64_t MAX_SIZE
The maximum size of a serialized object in bytes or number of elements (for eg vectors) when the size...
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.
constexpr DerivedAndBaseFormat HEX_UPPER
constexpr DerivedAndBaseFormat RAW_LOWER
static bool isCanonicalException(const std::ios_base::failure &ex)
BOOST_AUTO_TEST_CASE(sizes)
A mutable version of CTransaction.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string ToUpper(std::string_view str)
Returns the uppercase equivalent of the given string.
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.