PIVX Core  5.6.99
P2P Digital Currency
json_test_vectors.h
Go to the documentation of this file.
1 // Copyright (c) 2020 The PIVX 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 #ifndef PIVX_TEST_LIBRUST_JSON_TEST_VECTORS_H
6 #define PIVX_TEST_LIBRUST_JSON_TEST_VECTORS_H
7 
8 #include "utilstrencodings.h"
9 #include "version.h"
10 #include "serialize.h"
11 #include "streams.h"
12 
13 #include <univalue.h>
14 
15 #include <boost/test/unit_test.hpp>
16 
18 read_json(const std::string& jsondata);
19 
20 // #define PRINT_JSON 1
21 
22 template<typename T>
23 void expect_deser_same(const T& expected)
24 {
25  CDataStream ss1(SER_NETWORK, PROTOCOL_VERSION);
26  ss1 << expected;
27 
28  auto serialized_size = ss1.size();
29 
30  CDataStream ss1Copy = ss1;
31  T object;
32  ss1Copy >> object;
33 
34  CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION);
35  ss2 << object;
36 
37  BOOST_ASSERT(serialized_size == ss2.size());
38  BOOST_CHECK(memcmp(&*ss1.begin(), &*ss2.begin(), serialized_size) == 0);
39 }
40 
41 template<typename T, typename U>
42 void expect_test_vector(T& v, const U& expected)
43 {
44  expect_deser_same(expected);
45 
46  CDataStream ss1(SER_NETWORK, PROTOCOL_VERSION);
47  ss1 << expected;
48 
49  #ifdef PRINT_JSON
50  std::cout << "\t\"" ;
51  std::cout << HexStr(ss1.begin(), ss1.end()) << "\",\n";
52  #else
53  std::string raw = v.get_str();
54  CDataStream ss2(ParseHex(raw), SER_NETWORK, PROTOCOL_VERSION);
55 
56  BOOST_CHECK_EQUAL(ss1.size(), ss2.size());
57  BOOST_CHECK(memcmp(&*ss1.begin(), &*ss2.begin(), ss1.size()) == 0);
58  #endif
59 }
60 
61 #endif // PIVX_TEST_LIBRUST_JSON_TEST_VECTORS_H
#define ss1(x)
Definition: bmw.c:140
#define ss2(x)
Definition: bmw.c:142
#define T(expected, seed, data)
void expect_deser_same(const T &expected)
void expect_test_vector(T &v, const U &expected)
UniValue read_json(const std::string &jsondata)
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ SER_NETWORK
Definition: serialize.h:174
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::vector< unsigned char > ParseHex(const char *psz)