Added json parser tests

This commit is contained in:
SChernykh 2022-06-20 10:50:30 +02:00
parent 56c239a368
commit acf37ff10f
3 changed files with 64 additions and 1 deletions

View file

@ -104,7 +104,7 @@ struct parse_wrapper<T, difficulty_type>
if (!from_hex(s[i], d)) { if (!from_hex(s[i], d)) {
return false; return false;
} }
out_value.hi = (out_value.hi << 4) || (out_value.lo >> 60); out_value.hi = (out_value.hi << 4) | (out_value.lo >> 60);
out_value.lo = (out_value.lo << 4) | d; out_value.lo = (out_value.lo << 4) | d;
} }

View file

@ -16,6 +16,8 @@
*/ */
#include "common.h" #include "common.h"
#include "json_parsers.h"
#include <rapidjson/document.h>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <random> #include <random>
#include <sstream> #include <sstream>
@ -146,6 +148,33 @@ TEST(difficulty_type, input_output)
test_value(std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::max(), "340282366920938463463374607431768211455"); test_value(std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::max(), "340282366920938463463374607431768211455");
} }
TEST(difficulty_type, json_parser)
{
auto test_value = [](uint64_t lo, uint64_t hi, const char* s) {
difficulty_type diff{ lo, hi };
std::stringstream ss;
ss << "{\"diff\":\"" << s << "\"}";
using namespace rapidjson;
Document doc;
doc.Parse(ss.str().c_str());
difficulty_type diff2;
parseValue(doc, "diff", diff2);
ASSERT_EQ(diff2, diff);
};
test_value(0, 0, "0x0");
test_value(1, 0, "0x1");
test_value(0x123456789abcdefull, 0, "0x123456789abcdef");
test_value(0x123456789abcdefull, 0, "0x123456789ABCDEF");
test_value(std::numeric_limits<uint64_t>::max(), 0, "0xffffffffffffffff");
test_value(0, 1, "0x10000000000000000");
test_value(1, 1, "0x10000000000000001");
test_value(0x1122334455667788ull, 0x99aabbccddeeff00ull, "0x99aabbccddeeff001122334455667788");
test_value(std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::max(), "0xffffffffffffffffffffffffffffffff");
}
TEST(difficulty_type, check_pow) TEST(difficulty_type, check_pow)
{ {
hash h; hash h;

View file

@ -16,6 +16,8 @@
*/ */
#include "common.h" #include "common.h"
#include "json_parsers.h"
#include <rapidjson/document.h>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <random> #include <random>
#include <sstream> #include <sstream>
@ -90,4 +92,36 @@ TEST(hash, input_output)
check(h, "fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0"); check(h, "fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0");
} }
TEST(hash, json_parser)
{
auto check = [](const hash& h, const char* s) {
std::stringstream ss;
ss << "{\"hash\":\"" << s << "\"}";
using namespace rapidjson;
Document doc;
doc.Parse(ss.str().c_str());
hash h2;
parseValue(doc, "hash", h2);
ASSERT_EQ(h2, h);
};
hash h;
check(h, "0000000000000000000000000000000000000000000000000000000000000000");
memset(h.h, -1, HASH_SIZE);
check(h, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
for (uint8_t i = 0; i < HASH_SIZE; ++i) {
h.h[i] = i;
}
check(h, "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f");
for (uint8_t i = 0; i < HASH_SIZE; ++i) {
h.h[i] = 0xff - i;
}
check(h, "fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0efeeedecebeae9e8e7e6e5e4e3e2e1e0");
}
} }