Line data Source code
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 : 17 : UniValue 18 : read_json(const std::string& jsondata); 19 : 20 : // #define PRINT_JSON 1 21 : 22 : template<typename T> 23 288 : void expect_deser_same(const T& expected) 24 : { 25 288 : CDataStream ss1(SER_NETWORK, PROTOCOL_VERSION); 26 288 : ss1 << expected; 27 : 28 288 : auto serialized_size = ss1.size(); 29 : 30 576 : CDataStream ss1Copy = ss1; 31 560 : T object; 32 288 : ss1Copy >> object; 33 : 34 576 : CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION); 35 288 : ss2 << object; 36 : 37 288 : BOOST_ASSERT(serialized_size == ss2.size()); 38 576 : BOOST_CHECK(memcmp(&*ss1.begin(), &*ss2.begin(), serialized_size) == 0); 39 288 : } 40 : 41 : template<typename T, typename U> 42 288 : void expect_test_vector(T& v, const U& expected) 43 : { 44 288 : expect_deser_same(expected); 45 : 46 288 : CDataStream ss1(SER_NETWORK, PROTOCOL_VERSION); 47 288 : ss1 << expected; 48 : 49 : #ifdef PRINT_JSON 50 : std::cout << "\t\"" ; 51 : std::cout << HexStr(ss1.begin(), ss1.end()) << "\",\n"; 52 : #else 53 576 : std::string raw = v.get_str(); 54 864 : CDataStream ss2(ParseHex(raw), SER_NETWORK, PROTOCOL_VERSION); 55 : 56 288 : BOOST_CHECK_EQUAL(ss1.size(), ss2.size()); 57 576 : BOOST_CHECK(memcmp(&*ss1.begin(), &*ss2.begin(), ss1.size()) == 0); 58 : #endif 59 288 : } 60 : 61 : #endif // PIVX_TEST_LIBRUST_JSON_TEST_VECTORS_H