14 #include <boost/test/unit_test.hpp>
24 std::vector<unsigned char> serialized_representation(ss.
begin(), ss.
end());
29 ss >> thing_deserialized;
46 CSerializeMethodsTestSingle(
int intvalin,
bool boolvalin, std::string stringvalin,
const char* charstrvalin,
CTransaction txvalin) : intval(intvalin), boolval(boolvalin), stringval(
std::move(stringvalin)), txval(MakeTransactionRef(txvalin))
48 memcpy(charstrval, charstrvalin,
sizeof(charstrval));
62 return intval == rhs.
intval && \
77 READWRITE(obj.intval, obj.boolval, obj.stringval, obj.charstrval, obj.txval);
163 for (
int i = 0; i < 1000; i++) {
169 for (
int i = 0; i < 1000; i++) {
172 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
180 for (
int i = 0; i < 1000; i++) {
186 for (
int i = 0; i < 1000; i++) {
189 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
195 check_ser_rep<Optional<unsigned char>>(0xff, {0x01, 0xff});
196 check_ser_rep<Optional<unsigned char>>(boost::none, {0x00});
197 check_ser_rep<Optional<std::string>>(std::string(
"Test"), {0x01, 0x04,
'T',
'e',
's',
't'});
206 for (
int i = 0; i < 100000; i++) {
212 for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
219 for (
int i = 0; i < 100000; i++) {
222 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
225 for (uint64_t i = 0; i < 100000000000ULL; i += 999999937) {
228 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
256 std::vector<char>::size_type i, j;
258 for (i = 1; i <= MAX_SIZE; i *= 2)
263 for (i = 1; i <= MAX_SIZE; i *= 2)
266 BOOST_CHECK_MESSAGE((i-1) == j,
"decoded:" << j <<
" expected:" << (i-1));
268 BOOST_CHECK_MESSAGE(i == j,
"decoded:" << j <<
" expected:" << i);
272 static bool isCanonicalException(
const std::ios_base::failure& ex)
274 std::ios_base::failure expectedException(
"non-canonical ReadCompactSize()");
280 return strcmp(expectedException.what(), ex.what()) == 0;
285 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};
286 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};
288 BOOST_CHECK(vec1 == std::vector<uint8_t>(vec2.begin(), vec2.end()));
297 std::vector<char>::size_type n;
300 ss.
write(
"\xfd\x00\x00", 3);
301 BOOST_CHECK_EXCEPTION(
ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
304 ss.
write(
"\xfd\xfc\x00", 3);
305 BOOST_CHECK_EXCEPTION(
ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
308 ss.
write(
"\xfd\xfd\x00", 3);
313 ss.
write(
"\xfe\x00\x00\x00\x00", 5);
314 BOOST_CHECK_EXCEPTION(
ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
317 ss.
write(
"\xfe\xff\xff\x00\x00", 5);
318 BOOST_CHECK_EXCEPTION(
ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
321 ss.
write(
"\xff\x00\x00\x00\x00\x00\x00\x00\x00", 9);
322 BOOST_CHECK_EXCEPTION(
ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
325 ss.
write(
"\xff\xff\xff\xff\x01\x00\x00\x00\x00", 9);
326 BOOST_CHECK_EXCEPTION(
ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
335 ss.
write(
"\x00\x01\x02\xff", 4);
381 std::string stringval(
"testing");
382 const char charstrval[16] =
"testing charstr";
void write(const char *pch, size_t nSize)
const_iterator end() const
void GetAndClear(CSerializeData &data)
iterator insert(iterator it, const char x=char())
const_iterator begin() const
vector_type::size_type size_type
iterator erase(iterator it)
SERIALIZE_METHODS(CSerializeMethodsTestMany, obj)
CSerializeMethodsTestSingle()=default
SERIALIZE_METHODS(CSerializeMethodsTestSingle, obj)
bool operator==(const CSerializeMethodsTestSingle &rhs)
CSerializeMethodsTestSingle(int intvalin, bool boolvalin, std::string stringvalin, const char *charstrvalin, CTransaction txvalin)
The basic transaction that is broadcasted on the network and contained in blocks.
BOOST_AUTO_TEST_SUITE_END()
void * memcpy(void *a, const void *b, size_t c)
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
#define T(expected, seed, data)
#define BOOST_FIXTURE_TEST_SUITE(a, b)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
#define VARINT_MODE(obj, mode)
float ser_uint32_to_float(uint32_t y)
unsigned int GetSerializeSize(const std::array< T, N > &item)
array
uint32_t ser_float_to_uint32(float x)
double ser_uint64_to_double(uint64_t y)
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
uint64_t ser_double_to_uint64(double x)
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
BOOST_AUTO_TEST_CASE(sizes)
void check_ser_rep(T thing, std::vector< unsigned char > expected)
A mutable version of CTransaction.
std::shared_ptr< const CTransaction > CTransactionRef
uint256 uint256S(const char *str)
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::vector< char, zero_after_free_allocator< char > > CSerializeData