Update json from upstream version 3.1.2.

This adds support for ubjson and makes a number of bugfixes.

Binary input and output have switched from strings to uint8_t arrays.
This commit is contained in:
Peter Johnson
2018-05-14 22:53:22 -07:00
parent c274d1790f
commit f8ed48af98
20 changed files with 8915 additions and 6854 deletions

View File

@@ -50,7 +50,7 @@ TEST(CborDiscardedTest, Case)
TEST(CborNullTest, Case)
{
json j = nullptr;
std::string expected = "\xf6";
std::vector<uint8_t> expected = {0xf6};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -61,7 +61,7 @@ TEST(CborNullTest, Case)
TEST(CborBooleanTest, True)
{
json j = true;
std::string expected = "\xf5";
std::vector<uint8_t> expected = {0xf5};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -72,7 +72,7 @@ TEST(CborBooleanTest, True)
TEST(CborBooleanTest, False)
{
json j = false;
std::string expected = "\xf4";
std::vector<uint8_t> expected = {0xf4};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -91,17 +91,17 @@ TEST_P(CborSignedNeg8Test, Case)
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0x3b));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x3b));
uint64_t positive = static_cast<uint64_t>(-1 - GetParam());
expected.push_back(static_cast<char>((positive >> 56) & 0xff));
expected.push_back(static_cast<char>((positive >> 48) & 0xff));
expected.push_back(static_cast<char>((positive >> 40) & 0xff));
expected.push_back(static_cast<char>((positive >> 32) & 0xff));
expected.push_back(static_cast<char>((positive >> 24) & 0xff));
expected.push_back(static_cast<char>((positive >> 16) & 0xff));
expected.push_back(static_cast<char>((positive >> 8) & 0xff));
expected.push_back(static_cast<char>(positive & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 56) & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 48) & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 40) & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 32) & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(positive & 0xff));
// compare result + size
const auto result = json::to_cbor(j);
@@ -153,13 +153,13 @@ TEST_P(CborSignedNeg4Test, Case)
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0x3a));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x3a));
uint32_t positive = static_cast<uint32_t>(static_cast<uint64_t>(-1 - GetParam()) & 0x00000000ffffffff);
expected.push_back(static_cast<char>((positive >> 24) & 0xff));
expected.push_back(static_cast<char>((positive >> 16) & 0xff));
expected.push_back(static_cast<char>((positive >> 8) & 0xff));
expected.push_back(static_cast<char>(positive & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(positive & 0xff));
// compare result + size
const auto result = json::to_cbor(j);
@@ -205,11 +205,11 @@ TEST(CborSignedTest, Neg2)
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0x39));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x39));
uint16_t positive = static_cast<uint16_t>(-1 - i);
expected.push_back(static_cast<char>((positive >> 8) & 0xff));
expected.push_back(static_cast<char>(positive & 0xff));
expected.push_back(static_cast<uint8_t>((positive >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(positive & 0xff));
// compare result + size
const auto result = json::to_cbor(j);
@@ -231,7 +231,7 @@ TEST(CborSignedTest, Neg2)
TEST(CborSignedTest, NegInt16)
{
json j = -9263;
std::string expected = "\x39\x24\x2e";
std::vector<uint8_t> expected = {0x39,0x24,0x2e};
const auto result = json::to_cbor(j);
ASSERT_EQ(result, expected);
@@ -257,9 +257,9 @@ TEST(CborSignedTest, Neg1)
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(0x38);
expected.push_back(static_cast<char>(-1 - i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x38));
expected.push_back(static_cast<uint8_t>(-1 - i));
// compare result + size
const auto result = json::to_cbor(j);
@@ -289,8 +289,8 @@ TEST(CborSignedTest, Neg0)
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0x20 - 1 - static_cast<uint8_t>(i)));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x20 - 1 - static_cast<uint8_t>(i)));
// compare result + size
const auto result = json::to_cbor(j);
@@ -314,14 +314,14 @@ TEST(CborSignedTest, Pos0)
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
j.get_ref<int64_t&>() = static_cast<int64_t>(i);
// check type
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_cbor(j);
@@ -329,7 +329,7 @@ TEST(CborSignedTest, Pos0)
ASSERT_EQ(result.size(), 1u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(i));
EXPECT_EQ(result[0], static_cast<uint8_t>(i));
// roundtrip
EXPECT_EQ(json::from_cbor(result), j);
@@ -345,15 +345,15 @@ TEST(CborSignedTest, Pos1)
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
j.get_ref<int64_t&>() = static_cast<int64_t>(i);
// check type
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0x18));
expected.push_back(static_cast<char>(i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x18));
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_cbor(j);
@@ -362,7 +362,7 @@ TEST(CborSignedTest, Pos1)
// check individual bytes
EXPECT_EQ(result[0], 0x18);
EXPECT_EQ(result[1], static_cast<char>(i));
EXPECT_EQ(result[1], static_cast<uint8_t>(i));
// roundtrip
EXPECT_EQ(json::from_cbor(result), j);
@@ -378,16 +378,16 @@ TEST(CborSignedTest, Pos2)
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
j.get_ref<int64_t&>() = static_cast<int64_t>(i);
// check type
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0x19));
expected.push_back(static_cast<char>((i >> 8) & 0xff));
expected.push_back(static_cast<char>(i & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x19));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
// compare result + size
const auto result = json::to_cbor(j);
@@ -410,19 +410,19 @@ TEST_P(CborSignedPos4Test, Case)
{
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() =
static_cast<json::number_integer_t>(GetParam());
j.get_ref<int64_t&>() =
static_cast<int64_t>(GetParam());
// check type
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
std::vector<uint8_t> expected;
expected.push_back(0x1a);
expected.push_back(static_cast<char>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
// compare result + size
const auto result = json::to_cbor(j);
@@ -456,23 +456,23 @@ TEST_P(CborSignedPos8Test, Case)
{
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() =
static_cast<json::number_integer_t>(GetParam());
j.get_ref<int64_t&>() =
static_cast<int64_t>(GetParam());
// check type
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
std::vector<uint8_t> expected;
expected.push_back(0x1b);
expected.push_back(static_cast<char>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 010) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 010) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
// compare result + size
const auto result = json::to_cbor(j);
@@ -517,10 +517,10 @@ INSTANTIATE_TEST_CASE_P(CborSignedPos8Tests, CborSignedPos8Test,
ASSERT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
std::vector<uint8_t> expected;
expected.push_back(0xd1);
expected.push_back(static_cast<char>((i >> 8) & 0xff));
expected.push_back(static_cast<char>(i & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -552,8 +552,8 @@ TEST(CborUnsignedTest, Pos0)
ASSERT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_cbor(j);
@@ -561,7 +561,7 @@ TEST(CborUnsignedTest, Pos0)
ASSERT_EQ(result.size(), 1u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(i));
EXPECT_EQ(result[0], static_cast<uint8_t>(i));
// roundtrip
EXPECT_EQ(json::from_cbor(result), j);
@@ -582,9 +582,9 @@ TEST(CborUnsignedTest, Pos1)
ASSERT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
std::vector<uint8_t> expected;
expected.push_back(0x18);
expected.push_back(static_cast<char>(i));
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_cbor(j);
@@ -615,10 +615,10 @@ TEST(CborUnsignedTest, Pos2)
ASSERT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
std::vector<uint8_t> expected;
expected.push_back(0x19);
expected.push_back(static_cast<char>((i >> 8) & 0xff));
expected.push_back(static_cast<char>(i & 0xff));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
// compare result + size
const auto result = json::to_cbor(j);
@@ -646,12 +646,12 @@ TEST_P(CborUnsignedPos4Test, Case)
ASSERT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
std::vector<uint8_t> expected;
expected.push_back(0x1a);
expected.push_back(static_cast<char>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
// compare result + size
const auto result = json::to_cbor(j);
@@ -684,16 +684,16 @@ TEST_P(CborUnsignedPos8Test, Case)
ASSERT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
std::vector<uint8_t> expected;
expected.push_back(0x1b);
expected.push_back(static_cast<char>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 010) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 010) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
// compare result + size
const auto result = json::to_cbor(j);
@@ -724,7 +724,7 @@ TEST(CborFloatTest, Number)
{
double v = 3.1415925;
json j = v;
std::string expected = "\xfb\x40\x09\x21\xfb\x3f\xa6\xde\xfc";
std::vector<uint8_t> expected = {0xfb,0x40,0x09,0x21,0xfb,0x3f,0xa6,0xde,0xfc};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -735,16 +735,16 @@ TEST(CborFloatTest, Number)
TEST(CborFloatTest, HalfInfinity)
{
json j = json::from_cbor(wpi::StringRef("\xf9\x7c\x00", 3));
json::number_float_t d = j;
json j = json::from_cbor(std::vector<uint8_t>({0xf9,0x7c,0x00}));
double d = j;
EXPECT_FALSE(std::isfinite(d));
EXPECT_EQ(j.dump(), "null");
}
TEST(CborFloatTest, HalfNaN)
{
json j = json::from_cbor("\xf9\x7c\x01");
json::number_float_t d = j;
json j = json::from_cbor(std::vector<uint8_t>({0xf9,0x7c,0x01}));
double d = j;
EXPECT_TRUE(std::isnan(d));
EXPECT_EQ(j.dump(), "null");
}
@@ -761,9 +761,12 @@ TEST(CborStringTest, String1)
json j = s;
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0x60 + N));
expected.append(s);
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x60 + N));
for (size_t i = 0; i < N; ++i)
{
expected.push_back('x');
}
// compare result + size
const auto result = json::to_cbor(j);
@@ -792,10 +795,13 @@ TEST(CborStringTest, String2)
json j = s;
// create expected byte vector
std::string expected;
expected.push_back(0x78);
expected.push_back(static_cast<char>(N));
expected.append(s);
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0x78));
expected.push_back(static_cast<uint8_t>(N));
for (size_t i = 0; i < N; ++i)
{
expected.push_back('x');
}
// compare result + size
const auto result = json::to_cbor(j);
@@ -818,11 +824,14 @@ TEST_P(CborString3Test, Case)
json j = s;
// create expected byte vector
std::string expected;
std::vector<uint8_t> expected;
expected.push_back(0x79);
expected.push_back(static_cast<char>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
expected.append(s);
expected.push_back(static_cast<uint8_t>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
for (size_t i = 0; i < GetParam(); ++i)
{
expected.push_back('x');
}
// compare result + size
const auto result = json::to_cbor(j);
@@ -856,13 +865,16 @@ TEST_P(CborString5Test, Case)
json j = s;
// create expected byte vector
std::string expected;
std::vector<uint8_t> expected;
expected.push_back(0x7a);
expected.push_back(static_cast<char>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
expected.append(s);
expected.push_back(static_cast<uint8_t>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
for (size_t i = 0; i < GetParam(); ++i)
{
expected.push_back('x');
}
// compare result + size
const auto result = json::to_cbor(j);
@@ -887,7 +899,7 @@ INSTANTIATE_TEST_CASE_P(CborString5Tests, CborString5Test,
TEST(CborArrayTest, Empty)
{
json j = json::array();
std::string expected = "\x80";
std::vector<uint8_t> expected = {0x80};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -899,7 +911,7 @@ TEST(CborArrayTest, Empty)
TEST(CborArrayTest, Null)
{
json j = {nullptr};
std::string expected = "\x81\xf6";
std::vector<uint8_t> expected = {0x81,0xf6};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -911,7 +923,7 @@ TEST(CborArrayTest, Null)
TEST(CborArrayTest, Simple)
{
json j = json::parse("[1,2,3,4,5]");
std::string expected = "\x85\x01\x02\x03\x04\x05";
std::vector<uint8_t> expected = {0x85,0x01,0x02,0x03,0x04,0x05};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -923,7 +935,7 @@ TEST(CborArrayTest, Simple)
TEST(CborArrayTest, NestEmpty)
{
json j = json::parse("[[[[]]]]");
std::string expected = "\x81\x81\x81\x80";
std::vector<uint8_t> expected = {0x81,0x81,0x81,0x80};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -935,7 +947,7 @@ TEST(CborArrayTest, NestEmpty)
TEST(CborArrayTest, UInt16)
{
json j(257, nullptr);
std::string expected(j.size() + 3, static_cast<char>(0xf6)); // all null
std::vector<uint8_t> expected(j.size() + 3, 0xf6); // all null
expected[0] = static_cast<char>(0x99); // array 16 bit
expected[1] = 0x01; // size (0x0101), byte 0
expected[2] = 0x01; // size (0x0101), byte 1
@@ -950,7 +962,7 @@ TEST(CborArrayTest, UInt16)
TEST(CborArrayTest, UInt32)
{
json j(65793, nullptr);
std::string expected(j.size() + 5, static_cast<char>(0xf6)); // all null
std::vector<uint8_t> expected(j.size() + 5, 0xf6); // all null
expected[0] = static_cast<char>(0x9a); // array 32 bit
expected[1] = 0x00; // size (0x00010101), byte 0
expected[2] = 0x01; // size (0x00010101), byte 1
@@ -968,7 +980,7 @@ TEST(CborArrayTest, UInt32)
TEST(CborArrayTest, UInt64)
{
json j(4294967296, nullptr);
std::string expected(j.size() + 9, static_cast<char>(0xf6)); // all null
std::vector<uint8_t> expected(j.size() + 9, 0xf6); // all null
expected[0] = 0x9b; // array 64 bit
expected[1] = 0x00; // size (0x0000000100000000), byte 0
expected[2] = 0x00; // size (0x0000000100000000), byte 1
@@ -989,7 +1001,7 @@ TEST(CborArrayTest, UInt64)
TEST(CborObjectTest, Empty)
{
json j = json::object();
std::string expected = "\xa0";
std::vector<uint8_t> expected = {0xa0};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -1001,7 +1013,7 @@ TEST(CborObjectTest, Empty)
TEST(CborObjectTest, EmptyKey)
{
json j = {{"", nullptr}};
std::string expected = "\xa1\x60\xf6";
std::vector<uint8_t> expected = {0xa1,0x60,0xf6};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -1013,7 +1025,7 @@ TEST(CborObjectTest, EmptyKey)
TEST(CborObjectTest, NestedEmpty)
{
json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}");
std::string expected = "\xa1\x61\x61\xa1\x61\x62\xa1\x61\x63\xa0";
std::vector<uint8_t> expected = {0xa1,0x61,0x61,0xa1,0x61,0x62,0xa1,0x61,0x63,0xa0};
const auto result = json::to_cbor(j);
EXPECT_EQ(result, expected);
@@ -1042,8 +1054,8 @@ TEST(CborObjectTest, UInt8)
// size and the overall size. The rest is then handled in the
// roundtrip check.
ASSERT_EQ(result.size(), 1787u); // 1 type, 1 size, 255*7 content
EXPECT_EQ(result[0], static_cast<char>(0xb8)); // map 8 bit
EXPECT_EQ(result[1], static_cast<char>(0xff)); // size byte (0xff)
EXPECT_EQ(result[0], static_cast<uint8_t>(0xb8)); // map 8 bit
EXPECT_EQ(result[1], static_cast<uint8_t>(0xff)); // size byte (0xff)
// roundtrip
EXPECT_EQ(json::from_cbor(result), j);
}
@@ -1069,7 +1081,7 @@ TEST(CborObjectTest, UInt16)
// size and the overall size. The rest is then handled in the
// roundtrip check.
ASSERT_EQ(result.size(), 1795u); // 1 type, 2 size, 256*7 content
EXPECT_EQ(result[0], static_cast<char>(0xb9)); // map 16 bit
EXPECT_EQ(result[0], static_cast<uint8_t>(0xb9)); // map 16 bit
EXPECT_EQ(result[1], 0x01); // byte 0 of size (0x0100)
EXPECT_EQ(result[2], 0x00); // byte 1 of size (0x0100)
@@ -1098,7 +1110,7 @@ TEST(CborObjectTest, UInt32)
// size and the overall size. The rest is then handled in the
// roundtrip check.
ASSERT_EQ(result.size(), 458757u); // 1 type, 4 size, 65536*7 content
EXPECT_EQ(result[0], static_cast<char>(0xba)); // map 32 bit
EXPECT_EQ(result[0], static_cast<uint8_t>(0xba)); // map 32 bit
EXPECT_EQ(result[1], 0x00); // byte 0 of size (0x00010000)
EXPECT_EQ(result[2], 0x01); // byte 1 of size (0x00010000)
EXPECT_EQ(result[3], 0x00); // byte 2 of size (0x00010000)
@@ -1111,7 +1123,7 @@ TEST(CborObjectTest, UInt32)
// 0x7b (string)
TEST(CborAdditionalDeserializationTest, Case7b)
{
std::string given("\x7b\x00\x00\x00\x00\x00\x00\x00\x01\x61", 10);
std::vector<uint8_t> given{0x7b,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x61};
json j = json::from_cbor(given);
EXPECT_EQ(j, "a");
}
@@ -1119,7 +1131,7 @@ TEST(CborAdditionalDeserializationTest, Case7b)
// 0x9b (array)
TEST(CborAdditionalDeserializationTest, Case9b)
{
std::string given("\x9b\x00\x00\x00\x00\x00\x00\x00\x01\xf4", 10);
std::vector<uint8_t> given{0x9b,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xf4};
json j = json::from_cbor(given);
EXPECT_EQ(j, json::parse("[false]"));
}
@@ -1127,101 +1139,101 @@ TEST(CborAdditionalDeserializationTest, Case9b)
// 0xbb (map)
TEST(CborAdditionalDeserializationTest, Casebb)
{
std::string given("\xbb\x00\x00\x00\x00\x00\x00\x00\x01\x60\xf4", 11);
std::vector<uint8_t> given{0xbb,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x60,0xf4};
json j = json::from_cbor(given);
EXPECT_EQ(j, json::parse("{\"\": false}"));
}
TEST(CborErrorTest, TooShortByteVector)
{
EXPECT_THROW_MSG(json::from_cbor("\x18"), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x18})), json::parse_error,
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor("\x19"), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x19})), json::parse_error,
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x19\x00", 2)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x19,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor("\x1a"), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1a})), json::parse_error,
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1a\x00", 2)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1a,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1a\x00\x00", 3)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1a,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1a\x00\x00\x00", 4)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1a,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 5: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor("\x1b"), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1b})), json::parse_error,
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1b\x00", 2)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1b,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1b\x00\x00", 3)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1b,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1b\x00\x00\x00", 4)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1b,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 5: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1b\x00\x00\x00\x00", 5)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1b,0x00,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 6: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1b\x00\x00\x00\x00\x00", 6)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1b,0x00,0x00,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 7: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1b\x00\x00\x00\x00\x00\x00", 7)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1b,0x00,0x00,0x00,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 8: unexpected end of input");
EXPECT_THROW_MSG(json::from_cbor(wpi::StringRef("\x1b\x00\x00\x00\x00\x00\x00\x00", 8)), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1b,0x00,0x00,0x00,0x00,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 9: unexpected end of input");
}
TEST(CborErrorTest, UnsupportedBytesConcrete)
{
EXPECT_THROW_MSG(json::from_cbor("\x1c"), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0x1c})), json::parse_error,
"[json.exception.parse_error.112] parse error at 1: error reading CBOR; last byte: 0x1c");
EXPECT_THROW_MSG(json::from_cbor("\xf8"), json::parse_error,
EXPECT_THROW_MSG(json::from_cbor(std::vector<uint8_t>({0xf8})), json::parse_error,
"[json.exception.parse_error.112] parse error at 1: error reading CBOR; last byte: 0xf8");
}
class CborUnsupportedBytesTest : public ::testing::TestWithParam<const char*> {
class CborUnsupportedBytesTest : public ::testing::TestWithParam<uint8_t> {
};
TEST_P(CborUnsupportedBytesTest, Case)
{
EXPECT_THROW(json::from_cbor(GetParam()), json::parse_error);
EXPECT_THROW(json::from_cbor(std::vector<uint8_t>({GetParam()})), json::parse_error);
}
static const char* unsupported_bytes_cases[] = {
static const uint8_t unsupported_bytes_cases[] = {
// ?
"\x1c\x1d\x1e\x1f",
0x1c, 0x1d, 0x1e, 0x1f,
// ?
"\x3c\x3d\x3e\x3f",
0x3c, 0x3d, 0x3e, 0x3f,
// byte strings
"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x50\x51\x52\x53\x54\x55\x56\x57",
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
// byte strings
"\x58\x59\x5a\x5b",
0x58, 0x59, 0x5a, 0x5b,
// ?
"\x5c\x5d\x5e",
0x5c, 0x5d, 0x5e,
// byte string
"\x5f",
0x5f,
// ?
"\x7c\x7d\x7e",
0x7c, 0x7d, 0x7e,
// ?
"\x9c\x9d\x9e",
0x9c, 0x9d, 0x9e,
// ?
"\xbc\xbd\xbe",
0xbc, 0xbd, 0xbe,
// date/time
"\xc0\xc1",
0xc0, 0xc1,
// bignum
"\xc2\xc3",
0xc2, 0xc3,
// fraction
"\xc4",
0xc4,
// bigfloat
"\xc5",
0xc5,
// tagged item
"\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4",
0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4,
// expected conversion
"\xd5\xd6\xd7",
0xd5, 0xd6, 0xd7,
// more tagged items
"\xd8\xd9\xda\xdb",
0xd8, 0xd9, 0xda, 0xdb,
// ?
"\xdc\xdd\xde\xdf",
0xdc, 0xdd, 0xde, 0xdf,
// (simple value)
"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3",
0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3,
// undefined
"\xf7",
0xf7,
// simple value
"\xf8",
0xf8,
};
INSTANTIATE_TEST_CASE_P(CborUnsupportedBytesTests, CborUnsupportedBytesTest,
@@ -1543,11 +1555,11 @@ TEST(CborFirstBytesTest, Unsupported)
for (auto i = 0; i < 256; ++i)
{
const auto byte = static_cast<char>(i);
const auto byte = static_cast<uint8_t>(i);
try
{
json::from_cbor(wpi::StringRef(&byte, 1));
json::from_cbor(std::vector<uint8_t>(1, byte));
}
catch (const json::parse_error& e)
{
@@ -1571,7 +1583,7 @@ TEST(CborFirstBytesTest, Unsupported)
namespace internal {
struct CborRoundtripTestParam {
const char* plain;
wpi::StringRef encoded;
std::vector<uint8_t> encoded;
bool test_encode;
};
} // namespace internal
@@ -1589,100 +1601,100 @@ TEST_P(CborRoundtripTest, Case)
}
static const internal::CborRoundtripTestParam rfc7049_appendix_a_numbers[] = {
{"0", wpi::StringRef("\x00", 1), true},
{"1", "\x01", true},
{"10", "\x0a", true},
{"23", "\x17", true},
{"24", "\x18\x18", true},
{"25", "\x18\x19", true},
{"100", "\x18\x64", true},
{"1000", "\x19\x03\xe8", true},
{"1000000", wpi::StringRef("\x1a\x00\x0f\x42\x40", 5), true},
{"1000000000000", wpi::StringRef("\x1b\x00\x00\x00\xe8\xd4\xa5\x10\x00", 9), true},
{"18446744073709551615", "\x1b\xff\xff\xff\xff\xff\xff\xff\xff", true},
{"0", {0x00}, true},
{"1", {0x01}, true},
{"10", {0x0a}, true},
{"23", {0x17}, true},
{"24", {0x18,0x18}, true},
{"25", {0x18,0x19}, true},
{"100", {0x18,0x64}, true},
{"1000", {0x19,0x03,0xe8}, true},
{"1000000", {0x1a,0x00,0x0f,0x42,0x40}, true},
{"1000000000000", {0x1b,0x00,0x00,0x00,0xe8,0xd4,0xa5,0x10,0x00}, true},
{"18446744073709551615", {0x1b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}, true},
// positive bignum is not supported
//{"18446744073709551616", wpi::StringRef("\xc2\x49\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11), true},
//{"-18446744073709551616", "\x3b\xff\xff\xff\xff\xff\xff\xff\xff", true},
//{"18446744073709551616", {0xc2,0x49,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00", 11), true},
//{"-18446744073709551616", {0x3b,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}, true},
// negative bignum is not supported
//{"-18446744073709551617", wpi::StringRef("\xc3\x49\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11), true},
{"-1", "\x20", true},
{"-10", "\x29", true},
{"-100", "\x38\x63", true},
{"-1000", "\x39\x03\xe7", true},
//{"-18446744073709551617", {0xc3,0x49,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, true},
{"-1", {0x20}, true},
{"-10", {0x29}, true},
{"-100", {0x38,0x63}, true},
{"-1000", {0x39,0x03,0xe7}, true},
// half-precision float
{"0.0", wpi::StringRef("\xf9\x00\x00", 3), false},
{"0.0", {0xf9,0x00,0x00}, false},
// half-precision float
{"-0.0", wpi::StringRef("\xf9\x80\x00", 3), false},
{"-0.0", {0xf9,0x80,0x00}, false},
// half-precision float
{"1.0", wpi::StringRef("\xf9\x3c\x00", 3), false},
{"1.1", "\xfb\x3f\xf1\x99\x99\x99\x99\x99\x9a", true},
{"1.0", {0xf9,0x3c,0x00}, false},
{"1.1", {0xfb,0x3f,0xf1,0x99,0x99,0x99,0x99,0x99,0x9a}, true},
// half-precision float
{"1.5", wpi::StringRef("\xf9\x3e\x00", 3), false},
{"1.5", {0xf9,0x3e,0x00}, false},
// half-precision float
{"65504.0", "\xf9\x7b\xff", false},
{"100000.0", wpi::StringRef("\xfa\x47\xc3\x50\x00", 5), false},
{"3.4028234663852886e+38", "\xfa\x7f\x7f\xff\xff", false},
{"1.0e+300", wpi::StringRef("\xfb\x7e\x37\xe4\x3c\x88\x00\x75\x9c", 9), true},
{"65504.0", {0xf9,0x7b,0xff}, false},
{"100000.0", {0xfa,0x47,0xc3,0x50,0x00}, false},
{"3.4028234663852886e+38", {0xfa,0x7f,0x7f,0xff,0xff}, false},
{"1.0e+300", {0xfb,0x7e,0x37,0xe4,0x3c,0x88,0x00,0x75,0x9c}, true},
// half-precision float
{"5.960464477539063e-8", wpi::StringRef("\xf9\x00\x01", 3), false},
{"5.960464477539063e-8", {0xf9,0x00,0x01}, false},
// half-precision float
{"0.00006103515625", wpi::StringRef("\xf9\x04\x00", 3), false},
{"0.00006103515625", {0xf9,0x04,0x00}, false},
// half-precision float
{"-4.0", wpi::StringRef("\xf9\xc4\x00", 3), false},
{"-4.1", "\xfb\xc0\x10\x66\x66\x66\x66\x66\x66", true},
{"-4.0", {0xf9,0xc4,0x00}, false},
{"-4.1", {0xfb,0xc0,0x10,0x66,0x66,0x66,0x66,0x66,0x66}, true},
};
INSTANTIATE_TEST_CASE_P(CborRfc7049AppendixANumberTests, CborRoundtripTest,
::testing::ValuesIn(rfc7049_appendix_a_numbers), );
static const internal::CborRoundtripTestParam rfc7049_appendix_a_simple_values[] = {
{"false", "\xf4", true},
{"true", "\xf5", true},
{"false", {0xf4}, true},
{"true", {0xf5}, true},
};
INSTANTIATE_TEST_CASE_P(CborRfc7049AppendixASimpleValueTests, CborRoundtripTest,
::testing::ValuesIn(rfc7049_appendix_a_simple_values), );
static const internal::CborRoundtripTestParam rfc7049_appendix_a_strings[] = {
{"\"\"", "\x60", true},
{"\"a\"", "\x61\x61", true},
{"\"IETF\"", "\x64\x49\x45\x54\x46", true},
{"\"\\u00fc\"", "\x62\xc3\xbc", true},
{"\"\\u6c34\"", "\x63\xe6\xb0\xb4", true},
{"\"\\ud800\\udd51\"", "\x64\xf0\x90\x85\x91", true},
{"\"\"", {0x60}, true},
{"\"a\"", {0x61,0x61}, true},
{"\"IETF\"", {0x64,0x49,0x45,0x54,0x46}, true},
{"\"\\u00fc\"", {0x62,0xc3,0xbc}, true},
{"\"\\u6c34\"", {0x63,0xe6,0xb0,0xb4}, true},
{"\"\\ud800\\udd51\"", {0x64,0xf0,0x90,0x85,0x91}, true},
// indefinite length strings
{"\"streaming\"", "\x7f\x73\x74\x72\x65\x61\x6d\x69\x6e\x67\xff", false},
{"\"streaming\"", {0x7f,0x65,0x73,0x74,0x72,0x65,0x61,0x64,0x6d,0x69,0x6e,0x67,0xff}, false},
};
INSTANTIATE_TEST_CASE_P(CborRfc7049AppendixAStringTests, CborRoundtripTest,
::testing::ValuesIn(rfc7049_appendix_a_strings), );
static const internal::CborRoundtripTestParam rfc7049_appendix_a_arrays[] = {
{"[]", "\x80", true},
{"[1, 2, 3]", "\x83\x01\x02\x03", true},
{"[1, [2, 3], [4, 5]]", "\x83\x01\x82\x02\x03\x82\x04\x05", true},
{"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]", "\x98\x19\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x18\x18\x19", true},
{"[]", {0x80}, true},
{"[1, 2, 3]", {0x83,0x01,0x02,0x03}, true},
{"[1, [2, 3], [4, 5]]", {0x83,0x01,0x82,0x02,0x03,0x82,0x04,0x05}, true},
{"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]", {0x98,0x19,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x18,0x18,0x19}, true},
// indefinite length arrays
{"[]", "\x9f\xff", false},
{"[1, [2, 3], [4, 5]] ", "\x9f\x01\x82\x02\x03\x9f\x04\x05\xff\xff", false},
{"[1, [2, 3], [4, 5]]", "\x9f\x01\x82\x02\x03\x82\x04\x05\xff", false},
{"[1, [2, 3], [4, 5]]", "\x83\x01\x82\x02\x03\x9f\x04\x05\xff", false},
{"[1, [2, 3], [4, 5]]", "\x83\x01\x9f\x02\x03\xff\x82\x04\x05", false},
{"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]", "\x9f\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x18\x18\x19\xff", false},
{"[]", {0x9f,0xff}, false},
{"[1, [2, 3], [4, 5]] ", {0x9f,0x01,0x82,0x02,0x03,0x9f,0x04,0x05,0xff,0xff}, false},
{"[1, [2, 3], [4, 5]]", {0x9f,0x01,0x82,0x02,0x03,0x82,0x04,0x05,0xff}, false},
{"[1, [2, 3], [4, 5]]", {0x83,0x01,0x82,0x02,0x03,0x9f,0x04,0x05,0xff}, false},
{"[1, [2, 3], [4, 5]]", {0x83,0x01,0x9f,0x02,0x03,0xff,0x82,0x04,0x05}, false},
{"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]", {0x9f,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,0x16,0x17,0x18,0x18,0x18,0x19,0xff}, false},
};
INSTANTIATE_TEST_CASE_P(CborRfc7049AppendixAArrayTests, CborRoundtripTest,
::testing::ValuesIn(rfc7049_appendix_a_arrays), );
static const internal::CborRoundtripTestParam rfc7049_appendix_a_objects[] = {
{"{}", "\xa0", true},
{"{\"a\": 1, \"b\": [2, 3]}", "\xa2\x61\x61\x01\x61\x62\x82\x02\x03", true},
{"[\"a\", {\"b\": \"c\"}]", "\x82\x61\x61\xa1\x61\x62\x61\x63", true},
{"{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\", \"d\": \"D\", \"e\": \"E\"}", "\xa5\x61\x61\x61\x41\x61\x62\x61\x42\x61\x63\x61\x43\x61\x64\x61\x44\x61\x65\x61\x45", true},
{"{}", {0xa0}, true},
{"{\"a\": 1, \"b\": [2, 3]}", {0xa2,0x61,0x61,0x01,0x61,0x62,0x82,0x02,0x03}, true},
{"[\"a\", {\"b\": \"c\"}]", {0x82,0x61,0x61,0xa1,0x61,0x62,0x61,0x63}, true},
{"{\"a\": \"A\", \"b\": \"B\", \"c\": \"C\", \"d\": \"D\", \"e\": \"E\"}", {0xa5,0x61,0x61,0x61,0x41,0x61,0x62,0x61,0x42,0x61,0x63,0x61,0x43,0x61,0x64,0x61,0x44,0x61,0x65,0x61,0x45}, true},
// indefinite length objects
{"{\"a\": 1, \"b\": [2, 3]}", "\xbf\x61\x61\x01\x61\x62\x9f\x02\x03\xff\xff", false},
{"[\"a\", {\"b\": \"c\"}]", "\x82\x61\x61\xbf\x61\x62\x61\x63\xff", false},
{"{\"Fun\": true, \"Amt\": -2}", "\xbf\x63\x46\x75\x6e\xf5\x63\x41\x6d\x74\x21\xff", false},
{"{\"a\": 1, \"b\": [2, 3]}", {0xbf,0x61,0x61,0x01,0x61,0x62,0x9f,0x02,0x03,0xff,0xff}, false},
{"[\"a\", {\"b\": \"c\"}]", {0x82,0x61,0x61,0xbf,0x61,0x62,0x61,0x63,0xff}, false},
{"{\"Fun\": true, \"Amt\": -2}", {0xbf,0x63,0x46,0x75,0x6e,0xf5,0x63,0x41,0x6d,0x74,0x21,0xff}, false},
};
INSTANTIATE_TEST_CASE_P(CborRfc7049AppendixAObjectTests, CborRoundtripTest,

View File

@@ -106,10 +106,10 @@ class JsonConstructObjectImplicitTest : public ::testing::Test {
json j_reference;
};
// std::map<json::string_t, json>
// std::map<std::string, json>
TEST_F(JsonConstructObjectImplicitTest, StdMapStringJson)
{
std::map<json::string_t, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};
std::map<std::string, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};
json j(o);
EXPECT_EQ(j.type(), json::value_t::object);
EXPECT_EQ(j, j_reference);
@@ -151,28 +151,28 @@ TEST_F(JsonConstructObjectImplicitTest, StdMapCharPointerJson)
EXPECT_EQ(j, j_reference);
}
// std::multimap<json::string_t, json>
// std::multimap<std::string, json>
TEST_F(JsonConstructObjectImplicitTest, StdMultiMapStringJson)
{
std::multimap<json::string_t, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};
std::multimap<std::string, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};
json j(o);
EXPECT_EQ(j.type(), json::value_t::object);
EXPECT_EQ(j, j_reference);
}
// std::unordered_map<json::string_t, json>
// std::unordered_map<std::string, json>
TEST_F(JsonConstructObjectImplicitTest, StdUnorderedMapStringJson)
{
std::unordered_map<json::string_t, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};
std::unordered_map<std::string, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};
json j(o);
EXPECT_EQ(j.type(), json::value_t::object);
EXPECT_EQ(j, j_reference);
}
// std::unordered_multimap<json::string_t, json>
// std::unordered_multimap<std::string, json>
TEST_F(JsonConstructObjectImplicitTest, StdUnorderedMultiMapStringJson)
{
std::unordered_multimap<json::string_t, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};
std::unordered_multimap<std::string, json> o {{"a", json(1)}, {"b", json(1u)}, {"c", json(2.2)}, {"d", json(false)}, {"e", json("string")}, {"f", json()}};
json j(o);
EXPECT_EQ(j.type(), json::value_t::object);
EXPECT_EQ(j, j_reference);
@@ -264,14 +264,14 @@ TEST(JsonConstructArrayContainerTest, Case)
TEST(JsonConstructStringExplicitTest, Empty)
{
json::string_t s;
std::string s;
json j(s);
EXPECT_EQ(j.type(), json::value_t::string);
}
TEST(JsonConstructStringExplicitTest, Filled)
{
json::string_t s {"Hello world"};
std::string s {"Hello world"};
json j(s);
EXPECT_EQ(j.type(), json::value_t::string);
}
@@ -281,7 +281,7 @@ class JsonConstructStringTest : public ::testing::Test {
JsonConstructStringTest() : j_reference(s_reference) {}
protected:
json::string_t s_reference {"Hello world"};
std::string s_reference {"Hello world"};
json j_reference;
};
@@ -322,7 +322,7 @@ TEST_F(JsonConstructStringTest, StringLiteral)
TEST(JsonConstructBooleanExplicitTest, Empty)
{
json::boolean_t b{};
bool b{};
json j(b);
EXPECT_EQ(j.type(), json::value_t::boolean);
}
@@ -341,14 +341,14 @@ TEST(JsonConstructBooleanExplicitTest, False)
TEST(JsonConstructIntegerExplicitTest, Uninitialized)
{
json::number_integer_t n{};
int64_t n{};
json j(n);
EXPECT_EQ(j.type(), json::value_t::number_integer);
}
TEST(JsonConstructIntegerExplicitTest, Initialized)
{
json::number_integer_t n(42);
int64_t n(42);
json j(n);
EXPECT_EQ(j.type(), json::value_t::number_integer);
}
@@ -360,9 +360,9 @@ class JsonConstructIntegerTest : public ::testing::Test {
: j_reference(n_reference), j_unsigned_reference(n_unsigned_reference) {}
protected:
json::number_integer_t n_reference = 42;
int64_t n_reference = 42;
json j_reference;
json::number_unsigned_t n_unsigned_reference = 42u;
uint64_t n_unsigned_reference = 42u;
json j_unsigned_reference;
};
@@ -429,9 +429,9 @@ class JsonConstructIntegerLiteralTest : public ::testing::Test {
: j_reference(n_reference), j_unsigned_reference(n_unsigned_reference) {}
protected:
json::number_integer_t n_reference = 42;
int64_t n_reference = 42;
json j_reference;
json::number_unsigned_t n_unsigned_reference = 42u;
uint64_t n_unsigned_reference = 42u;
json j_unsigned_reference;
};
@@ -479,14 +479,14 @@ TEST_F(JsonConstructIntegerLiteralTest, ULL)
TEST(JsonConstructFloatExplicitTest, Uninitialized)
{
json::number_float_t n{};
double n{};
json j(n);
EXPECT_EQ(j.type(), json::value_t::number_float);
}
TEST(JsonConstructFloatExplicitTest, Initialized)
{
json::number_float_t n(42.23);
double n(42.23);
json j(n);
EXPECT_EQ(j.type(), json::value_t::number_float);
}
@@ -494,12 +494,12 @@ TEST(JsonConstructFloatExplicitTest, Initialized)
TEST(JsonConstructFloatExplicitTest, Infinity)
{
// infinity is stored properly, but serialized to null
json::number_float_t n(std::numeric_limits<json::number_float_t>::infinity());
double n(std::numeric_limits<double>::infinity());
json j(n);
EXPECT_EQ(j.type(), json::value_t::number_float);
// check round trip of infinity
json::number_float_t d = j;
double d = j;
EXPECT_EQ(d, n);
// check that inf is serialized to null
@@ -512,7 +512,7 @@ class JsonConstructFloatTest : public ::testing::Test {
JsonConstructFloatTest() : j_reference(n_reference) {}
protected:
json::number_float_t n_reference {42.23};
double n_reference {42.23};
json j_reference;
};
@@ -540,7 +540,7 @@ class JsonConstructFloatLiteralTest : public ::testing::Test {
JsonConstructFloatLiteralTest() : j_reference(n_reference) {}
protected:
json::number_float_t n_reference {42.23};
double n_reference {42.23};
json j_reference;
};
@@ -575,8 +575,7 @@ TEST_F(JsonConstructFloatLiteralTest, L)
TEST(JsonConstructInitializerEmptyTest, Explicit)
{
std::initializer_list<json> l;
json j(l);
json j(json::initializer_list_t{});
EXPECT_EQ(j.type(), json::value_t::object);
}
@@ -725,17 +724,15 @@ TEST(JsonConstructInitializerExplicitTest, ObjectError)
TEST(JsonConstructInitializerPairErrorTest, WrongFieldNumber)
{
json j{{"too", "much"}, {"string", "fields"}};
EXPECT_THROW_MSG((j.get<std::pair<std::string, std::string>>()), json::other_error,
"[json.exception.other_error.502] conversion "
"to std::pair requires the object to have "
"exactly one field, but it has 2");
EXPECT_THROW_MSG((j.get<std::pair<std::string, std::string>>()), json::type_error,
"[json.exception.type_error.304] cannot use at() with object");
}
TEST(JsonConstructInitializerPairErrorTest, WrongJsonType)
{
json j(42);
EXPECT_THROW_MSG((j.get<std::pair<std::string, std::string>>()), json::type_error,
"[json.exception.type_error.302] type must be object, but is number");
"[json.exception.type_error.304] cannot use at() with number");
}
TEST(JsonConstructInitializerTest, EmptyArray)

View File

@@ -1,120 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Modifications Copyright (c) FIRST 2017. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++ (test suite)
| | |__ | | | | | | version 2.1.1
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gtest/gtest.h"
#include "wpi/SmallString.h"
#include "unit-json.h"
#include "json_serializer.h"
using wpi::json;
class JsonTypeNameTest
: public ::testing::TestWithParam<std::pair<json::value_t, const char*>> {};
TEST_P(JsonTypeNameTest, Case)
{
EXPECT_EQ(json(GetParam().first).type_name(), GetParam().second);
}
static const std::pair<json::value_t, const char*> type_name_cases[] = {
{json::value_t::null, "null"},
{json::value_t::object, "object"},
{json::value_t::array, "array"},
{json::value_t::number_integer, "number"},
{json::value_t::number_unsigned, "number"},
{json::value_t::number_float, "number"},
{json::value_t::boolean, "boolean"},
{json::value_t::string, "string"},
{json::value_t::discarded, "discarded"},
};
INSTANTIATE_TEST_CASE_P(JsonTypeNameTests, JsonTypeNameTest,
::testing::ValuesIn(type_name_cases), );
class JsonStringEscapeTest
: public ::testing::TestWithParam<std::pair<const char*, const char*>> {};
TEST_P(JsonStringEscapeTest, Case)
{
wpi::SmallString<32> buf;
wpi::raw_svector_ostream ss(buf);
json::serializer s(ss);
s.dump_escaped(GetParam().first);
EXPECT_EQ(ss.str(), wpi::StringRef(GetParam().second));
}
static const std::pair<const char*, const char*> string_escape_cases[] = {
{"\"", "\\\""},
{"\\", "\\\\"},
{"\b", "\\b"},
{"\f", "\\f"},
{"\n", "\\n"},
{"\r", "\\r"},
{"\t", "\\t"},
{"\x01", "\\u0001"},
{"\x02", "\\u0002"},
{"\x03", "\\u0003"},
{"\x04", "\\u0004"},
{"\x05", "\\u0005"},
{"\x06", "\\u0006"},
{"\x07", "\\u0007"},
{"\x08", "\\b"},
{"\x09", "\\t"},
{"\x0a", "\\n"},
{"\x0b", "\\u000b"},
{"\x0c", "\\f"},
{"\x0d", "\\r"},
{"\x0e", "\\u000e"},
{"\x0f", "\\u000f"},
{"\x10", "\\u0010"},
{"\x11", "\\u0011"},
{"\x12", "\\u0012"},
{"\x13", "\\u0013"},
{"\x14", "\\u0014"},
{"\x15", "\\u0015"},
{"\x16", "\\u0016"},
{"\x17", "\\u0017"},
{"\x18", "\\u0018"},
{"\x19", "\\u0019"},
{"\x1a", "\\u001a"},
{"\x1b", "\\u001b"},
{"\x1c", "\\u001c"},
{"\x1d", "\\u001d"},
{"\x1e", "\\u001e"},
{"\x1f", "\\u001f"},
};
INSTANTIATE_TEST_CASE_P(JsonStringEscapeTests, JsonStringEscapeTest,
::testing::ValuesIn(string_escape_cases), );

View File

@@ -57,10 +57,10 @@ class JsonGetObjectTest : public ::testing::Test {
typedef ::testing::Types<
json::object_t
, std::map<json::string_t, json>
, std::multimap<json::string_t, json>
, std::unordered_map<json::string_t, json>
, std::unordered_multimap<json::string_t, json>
, std::map<std::string, json>
, std::multimap<std::string, json>
, std::unordered_map<std::string, json>
, std::unordered_multimap<std::string, json>
> JsonGetObjectTestTypes;
TYPED_TEST_CASE(JsonGetObjectTest, JsonGetObjectTestTypes);
@@ -195,11 +195,11 @@ class JsonGetStringTest : public ::testing::Test {
JsonGetStringTest() : j(s_reference) {}
protected:
json::string_t s_reference {"Hello world"};
std::string s_reference {"Hello world"};
json j;
};
typedef ::testing::Types<json::string_t, std::string> JsonGetStringTestTypes;
typedef ::testing::Types<std::string, std::string> JsonGetStringTestTypes;
TYPED_TEST_CASE(JsonGetStringTest, JsonGetStringTestTypes);
TYPED_TEST(JsonGetStringTest, Explicit)
@@ -217,19 +217,19 @@ TYPED_TEST(JsonGetStringTest, Implicit)
// exception in case of a non-string type
TEST(JsonGetStringExceptionTest, TypeError)
{
EXPECT_THROW_MSG(json(json::value_t::null).get<json::string_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::null).get<std::string>(), json::type_error,
"[json.exception.type_error.302] type must be string, but is null");
EXPECT_THROW_MSG(json(json::value_t::object).get<json::string_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::object).get<std::string>(), json::type_error,
"[json.exception.type_error.302] type must be string, but is object");
EXPECT_THROW_MSG(json(json::value_t::array).get<json::string_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::array).get<std::string>(), json::type_error,
"[json.exception.type_error.302] type must be string, but is array");
EXPECT_THROW_MSG(json(json::value_t::boolean).get<json::string_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::boolean).get<std::string>(), json::type_error,
"[json.exception.type_error.302] type must be string, but is boolean");
EXPECT_THROW_MSG(json(json::value_t::number_integer).get<json::string_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::number_integer).get<std::string>(), json::type_error,
"[json.exception.type_error.302] type must be string, but is number");
EXPECT_THROW_MSG(json(json::value_t::number_unsigned).get<json::string_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::number_unsigned).get<std::string>(), json::type_error,
"[json.exception.type_error.302] type must be string, but is number");
EXPECT_THROW_MSG(json(json::value_t::number_float).get<json::string_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::number_float).get<std::string>(), json::type_error,
"[json.exception.type_error.302] type must be string, but is number");
}
@@ -239,11 +239,11 @@ class JsonGetBooleanTest : public ::testing::Test {
JsonGetBooleanTest() : j(b_reference) {}
protected:
json::boolean_t b_reference {true};
bool b_reference {true};
json j;
};
typedef ::testing::Types<json::boolean_t, bool> JsonGetBooleanTestTypes;
typedef ::testing::Types<bool, bool> JsonGetBooleanTestTypes;
TYPED_TEST_CASE(JsonGetBooleanTest, JsonGetBooleanTestTypes);
TYPED_TEST(JsonGetBooleanTest, Explicit)
@@ -261,19 +261,19 @@ TYPED_TEST(JsonGetBooleanTest, Implicit)
// exception in case of a non-string type
TEST(JsonGetBooleanExceptionTest, TypeError)
{
EXPECT_THROW_MSG(json(json::value_t::null).get<json::boolean_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::null).get<bool>(), json::type_error,
"[json.exception.type_error.302] type must be boolean, but is null");
EXPECT_THROW_MSG(json(json::value_t::object).get<json::boolean_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::object).get<bool>(), json::type_error,
"[json.exception.type_error.302] type must be boolean, but is object");
EXPECT_THROW_MSG(json(json::value_t::array).get<json::boolean_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::array).get<bool>(), json::type_error,
"[json.exception.type_error.302] type must be boolean, but is array");
EXPECT_THROW_MSG(json(json::value_t::string).get<json::boolean_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::string).get<bool>(), json::type_error,
"[json.exception.type_error.302] type must be boolean, but is string");
EXPECT_THROW_MSG(json(json::value_t::number_integer).get<json::boolean_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::number_integer).get<bool>(), json::type_error,
"[json.exception.type_error.302] type must be boolean, but is number");
EXPECT_THROW_MSG(json(json::value_t::number_unsigned).get<json::boolean_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::number_unsigned).get<bool>(), json::type_error,
"[json.exception.type_error.302] type must be boolean, but is number");
EXPECT_THROW_MSG(json(json::value_t::number_float).get<json::boolean_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::number_float).get<bool>(), json::type_error,
"[json.exception.type_error.302] type must be boolean, but is number");
}
@@ -283,9 +283,9 @@ class JsonGetIntegerTest : public ::testing::Test {
JsonGetIntegerTest() : j(n_reference), j_unsigned(n_unsigned_reference) {}
protected:
json::number_integer_t n_reference {42};
int64_t n_reference {42};
json j;
json::number_unsigned_t n_unsigned_reference {42u};
uint64_t n_unsigned_reference {42u};
json j_unsigned;
};
@@ -353,19 +353,19 @@ TYPED_TEST(JsonGetIntegerTest, Implicit)
// exception in case of a non-number type
TEST(JsonGetIntegerExceptionTest, TypeError)
{
EXPECT_THROW_MSG(json(json::value_t::null).get<json::number_integer_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::null).get<int64_t>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is null");
EXPECT_THROW_MSG(json(json::value_t::object).get<json::number_integer_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::object).get<int64_t>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is object");
EXPECT_THROW_MSG(json(json::value_t::array).get<json::number_integer_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::array).get<int64_t>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is array");
EXPECT_THROW_MSG(json(json::value_t::string).get<json::number_integer_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::string).get<int64_t>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is string");
EXPECT_THROW_MSG(json(json::value_t::boolean).get<json::number_integer_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::boolean).get<int64_t>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is boolean");
EXPECT_NO_THROW(json(json::value_t::number_float).get<json::number_integer_t>());
EXPECT_NO_THROW(json(json::value_t::number_float).get<json::number_unsigned_t>());
EXPECT_NO_THROW(json(json::value_t::number_float).get<int64_t>());
EXPECT_NO_THROW(json(json::value_t::number_float).get<uint64_t>());
}
template <typename T>
@@ -374,11 +374,11 @@ class JsonGetFloatTest : public ::testing::Test {
JsonGetFloatTest() : j(n_reference) {}
protected:
json::number_float_t n_reference {42.23};
double n_reference {42.23};
json j;
};
typedef ::testing::Types<json::number_float_t, float, double>
typedef ::testing::Types<double, float, double>
JsonGetFloatTestTypes;
TYPED_TEST_CASE(JsonGetFloatTest, JsonGetFloatTestTypes);
@@ -400,19 +400,19 @@ TYPED_TEST(JsonGetFloatTest, Implicit)
// exception in case of a non-string type
TEST(JsonGetFloatExceptionTest, TypeError)
{
EXPECT_THROW_MSG(json(json::value_t::null).get<json::number_float_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::null).get<double>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is null");
EXPECT_THROW_MSG(json(json::value_t::object).get<json::number_float_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::object).get<double>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is object");
EXPECT_THROW_MSG(json(json::value_t::array).get<json::number_float_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::array).get<double>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is array");
EXPECT_THROW_MSG(json(json::value_t::string).get<json::number_float_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::string).get<double>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is string");
EXPECT_THROW_MSG(json(json::value_t::boolean).get<json::number_float_t>(), json::type_error,
EXPECT_THROW_MSG(json(json::value_t::boolean).get<double>(), json::type_error,
"[json.exception.type_error.302] type must be number, but is boolean");
EXPECT_NO_THROW(json(json::value_t::number_integer).get<json::number_float_t>());
EXPECT_NO_THROW(json(json::value_t::number_unsigned).get<json::number_float_t>());
EXPECT_NO_THROW(json(json::value_t::number_integer).get<double>());
EXPECT_NO_THROW(json(json::value_t::number_unsigned).get<double>());
}
TEST(JsonGetEnumTest, Case)

View File

@@ -1,745 +0,0 @@
/*----------------------------------------------------------------------------*/
/* Modifications Copyright (c) FIRST 2017. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project. */
/*----------------------------------------------------------------------------*/
/*
__ _____ _____ _____
__| | __| | | | JSON for Modern C++ (test suite)
| | |__ | | | | | | version 2.1.1
|_____|_____|_____|_|___| https://github.com/nlohmann/json
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "gtest/gtest.h"
#include "unit-json.h"
using wpi::json;
class JsonIteratorWrapperObjectTest : public ::testing::Test {
protected:
json j = {{"A", 1}, {"B", 2}};
};
TEST_F(JsonIteratorWrapperObjectTest, Value)
{
int counter = 1;
for (auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "A");
EXPECT_EQ(i.value(), json(1));
break;
}
case 2:
{
EXPECT_EQ(i.key(), "B");
EXPECT_EQ(i.value(), json(2));
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperObjectTest, Reference)
{
int counter = 1;
for (auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "A");
EXPECT_EQ(i.value(), json(1));
// change the value
i.value() = json(11);
EXPECT_EQ(i.value(), json(11));
break;
}
case 2:
{
EXPECT_EQ(i.key(), "B");
EXPECT_EQ(i.value(), json(2));
// change the value
i.value() = json(22);
EXPECT_EQ(i.value(), json(22));
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
// check if values where changed
EXPECT_EQ(j, json({{"A", 11}, {"B", 22}}));
}
TEST_F(JsonIteratorWrapperObjectTest, ConstValue)
{
int counter = 1;
for (const auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "A");
EXPECT_EQ(i.value(), json(1));
break;
}
case 2:
{
EXPECT_EQ(i.key(), "B");
EXPECT_EQ(i.value(), json(2));
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperObjectTest, ConstReference)
{
json j = {{"A", 1}, {"B", 2}};
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "A");
EXPECT_EQ(i.value(), json(1));
break;
}
case 2:
{
EXPECT_EQ(i.key(), "B");
EXPECT_EQ(i.value(), json(2));
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
class JsonIteratorWrapperConstObjectTest : public ::testing::Test {
protected:
const json j = {{"A", 1}, {"B", 2}};
};
TEST_F(JsonIteratorWrapperConstObjectTest, Value)
{
int counter = 1;
for (auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "A");
EXPECT_EQ(i.value(), json(1));
break;
}
case 2:
{
EXPECT_EQ(i.key(), "B");
EXPECT_EQ(i.value(), json(2));
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperConstObjectTest, Reference)
{
int counter = 1;
for (auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "A");
EXPECT_EQ(i.value(), json(1));
break;
}
case 2:
{
EXPECT_EQ(i.key(), "B");
EXPECT_EQ(i.value(), json(2));
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperConstObjectTest, ConstValue)
{
int counter = 1;
for (const auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "A");
EXPECT_EQ(i.value(), json(1));
break;
}
case 2:
{
EXPECT_EQ(i.key(), "B");
EXPECT_EQ(i.value(), json(2));
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperConstObjectTest, ConstReference)
{
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "A");
EXPECT_EQ(i.value(), json(1));
break;
}
case 2:
{
EXPECT_EQ(i.key(), "B");
EXPECT_EQ(i.value(), json(2));
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
class JsonIteratorWrapperArrayTest : public ::testing::Test {
protected:
json j = {"A", "B"};
};
TEST_F(JsonIteratorWrapperArrayTest, Value)
{
int counter = 1;
for (auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "0");
EXPECT_EQ(i.value(), "A");
break;
}
case 2:
{
EXPECT_EQ(i.key(), "1");
EXPECT_EQ(i.value(), "B");
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperArrayTest, Reference)
{
int counter = 1;
for (auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "0");
EXPECT_EQ(i.value(), "A");
// change the value
i.value() = "AA";
EXPECT_EQ(i.value(), "AA");
break;
}
case 2:
{
EXPECT_EQ(i.key(), "1");
EXPECT_EQ(i.value(), "B");
// change the value
i.value() = "BB";
EXPECT_EQ(i.value(), "BB");
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
// check if values where changed
EXPECT_EQ(j, json({"AA", "BB"}));
}
TEST_F(JsonIteratorWrapperArrayTest, ConstValue)
{
int counter = 1;
for (const auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "0");
EXPECT_EQ(i.value(), "A");
break;
}
case 2:
{
EXPECT_EQ(i.key(), "1");
EXPECT_EQ(i.value(), "B");
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperArrayTest, ConstReference)
{
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "0");
EXPECT_EQ(i.value(), "A");
break;
}
case 2:
{
EXPECT_EQ(i.key(), "1");
EXPECT_EQ(i.value(), "B");
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
class JsonIteratorWrapperConstArrayTest : public ::testing::Test {
protected:
const json j = {"A", "B"};
};
TEST_F(JsonIteratorWrapperConstArrayTest, Value)
{
int counter = 1;
for (auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "0");
EXPECT_EQ(i.value(), "A");
break;
}
case 2:
{
EXPECT_EQ(i.key(), "1");
EXPECT_EQ(i.value(), "B");
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperConstArrayTest, Reference)
{
int counter = 1;
for (auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "0");
EXPECT_EQ(i.value(), "A");
break;
}
case 2:
{
EXPECT_EQ(i.key(), "1");
EXPECT_EQ(i.value(), "B");
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperConstArrayTest, ConstValue)
{
int counter = 1;
for (const auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "0");
EXPECT_EQ(i.value(), "A");
break;
}
case 2:
{
EXPECT_EQ(i.key(), "1");
EXPECT_EQ(i.value(), "B");
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
TEST_F(JsonIteratorWrapperConstArrayTest, ConstReference)
{
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
switch (counter++)
{
case 1:
{
EXPECT_EQ(i.key(), "0");
EXPECT_EQ(i.value(), "A");
break;
}
case 2:
{
EXPECT_EQ(i.key(), "1");
EXPECT_EQ(i.value(), "B");
break;
}
default:
{
break;
}
}
}
EXPECT_EQ(counter, 3);
}
class JsonIteratorWrapperPrimitiveTest : public ::testing::Test {
protected:
json j = 1;
};
TEST_F(JsonIteratorWrapperPrimitiveTest, Value)
{
int counter = 1;
for (auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
++counter;
EXPECT_EQ(i.key(), "");
EXPECT_EQ(i.value(), json(1));
}
EXPECT_EQ(counter, 2);
}
TEST_F(JsonIteratorWrapperPrimitiveTest, Reference)
{
int counter = 1;
for (auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
++counter;
EXPECT_EQ(i.key(), "");
EXPECT_EQ(i.value(), json(1));
// change value
i.value() = json(2);
}
EXPECT_EQ(counter, 2);
// check if value has changed
EXPECT_EQ(j, json(2));
}
TEST_F(JsonIteratorWrapperPrimitiveTest, ConstValue)
{
int counter = 1;
for (const auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
++counter;
EXPECT_EQ(i.key(), "");
EXPECT_EQ(i.value(), json(1));
}
EXPECT_EQ(counter, 2);
}
TEST_F(JsonIteratorWrapperPrimitiveTest, ConstReference)
{
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
++counter;
EXPECT_EQ(i.key(), "");
EXPECT_EQ(i.value(), json(1));
}
EXPECT_EQ(counter, 2);
}
class JsonIteratorWrapperConstPrimitiveTest : public ::testing::Test {
protected:
const json j = 1;
};
TEST_F(JsonIteratorWrapperConstPrimitiveTest, Value)
{
int counter = 1;
for (auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
++counter;
EXPECT_EQ(i.key(), "");
EXPECT_EQ(i.value(), json(1));
}
EXPECT_EQ(counter, 2);
}
TEST_F(JsonIteratorWrapperConstPrimitiveTest, Reference)
{
int counter = 1;
for (auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
++counter;
EXPECT_EQ(i.key(), "");
EXPECT_EQ(i.value(), json(1));
}
EXPECT_EQ(counter, 2);
}
TEST_F(JsonIteratorWrapperConstPrimitiveTest, ConstValue)
{
int counter = 1;
for (const auto i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
++counter;
EXPECT_EQ(i.key(), "");
EXPECT_EQ(i.value(), json(1));
}
EXPECT_EQ(counter, 2);
}
TEST_F(JsonIteratorWrapperConstPrimitiveTest, ConstReference)
{
int counter = 1;
for (const auto& i : json::iterator_wrapper(j))
{
SCOPED_TRACE(counter);
++counter;
EXPECT_EQ(i.key(), "");
EXPECT_EQ(i.value(), json(1));
}
EXPECT_EQ(counter, 2);
}

View File

@@ -42,13 +42,13 @@ TEST(JsonVersionTest, Meta)
json j = json::meta();
EXPECT_EQ(j["name"], "WPI version of JSON for Modern C++");
EXPECT_EQ(j["copyright"], "(C) 2013-2017 Niels Lohmann, (C) 2017 FIRST");
EXPECT_EQ(j["url"], "https://github.com/wpilibsuite/wpiutil");
EXPECT_EQ(j["copyright"], "(C) 2013-2017 Niels Lohmann, (C) 2017-2018 FIRST");
EXPECT_EQ(j["url"], "https://github.com/wpilibsuite/allwpilib");
EXPECT_EQ(j["version"], json(
{
{"string", "2.1.1"},
{"major", 2},
{"string", "3.1.2"},
{"major", 3},
{"minor", 1},
{"patch", 1}
{"patch", 2}
}));
}

View File

@@ -718,7 +718,7 @@ TEST(JsonSwapTest, NonObjectT)
TEST(JsonSwapTest, StringT)
{
json j = "Hello world";
json::string_t s = "Hallo Welt";
std::string s = "Hallo Welt";
j.swap(s);
@@ -732,7 +732,7 @@ TEST(JsonSwapTest, StringT)
TEST(JsonSwapTest, NonStringT)
{
json j = 17;
json::string_t s = "Hallo Welt";
std::string s = "Hallo Welt";
EXPECT_THROW_MSG(j.swap(s), json::type_error,
"[json.exception.type_error.310] cannot use swap() with number");

View File

@@ -50,7 +50,7 @@ TEST(MessagePackDiscardedTest, Case)
TEST(MessagePackNullTest, Case)
{
json j = nullptr;
std::string expected = "\xc0";
std::vector<uint8_t> expected = {0xc0};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -61,7 +61,7 @@ TEST(MessagePackNullTest, Case)
TEST(MessagePackBooleanTest, True)
{
json j = true;
std::string expected = "\xc3";
std::vector<uint8_t> expected = {0xc3};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -72,7 +72,7 @@ TEST(MessagePackBooleanTest, True)
TEST(MessagePackBooleanTest, False)
{
json j = false;
std::string expected = "\xc2";
std::vector<uint8_t> expected = {0xc2};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -94,8 +94,8 @@ TEST(MessagePackSignedTest, Neg0)
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -119,14 +119,14 @@ TEST(MessagePackSignedTest, Pos0)
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
j.get_ref<int64_t&>() = static_cast<int64_t>(i);
// check type
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -134,7 +134,7 @@ TEST(MessagePackSignedTest, Pos0)
EXPECT_EQ(result.size(), 1u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(i));
EXPECT_EQ(result[0], static_cast<uint8_t>(i));
// roundtrip
EXPECT_EQ(json::from_msgpack(result), j);
@@ -150,15 +150,15 @@ TEST(MessagePackSignedTest, Pos1)
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
j.get_ref<int64_t&>() = static_cast<int64_t>(i);
// check type
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xcc));
expected.push_back(static_cast<char>(i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xcc));
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -166,7 +166,7 @@ TEST(MessagePackSignedTest, Pos1)
EXPECT_EQ(result.size(), 2u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xcc));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xcc));
uint8_t restored = static_cast<uint8_t>(result[1]);
EXPECT_EQ(restored, i);
@@ -184,16 +184,16 @@ TEST(MessagePackSignedTest, Pos2)
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
j.get_ref<int64_t&>() = static_cast<int64_t>(i);
// check type
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xcd));
expected.push_back(static_cast<char>((i >> 8) & 0xff));
expected.push_back(static_cast<char>(i & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xcd));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -201,7 +201,7 @@ TEST(MessagePackSignedTest, Pos2)
EXPECT_EQ(result.size(), 3u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xcd));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xcd));
uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
EXPECT_EQ(restored, i);
@@ -216,18 +216,18 @@ TEST_P(MessagePackSignedPos4Test, Case)
{
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(GetParam());
j.get_ref<int64_t&>() = static_cast<int64_t>(GetParam());
// check type
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xce));
expected.push_back(static_cast<char>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xce));
expected.push_back(static_cast<uint8_t>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -235,7 +235,7 @@ TEST_P(MessagePackSignedPos4Test, Case)
EXPECT_EQ(result.size(), 5u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xce));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xce));
uint32_t restored = (static_cast<uint32_t>(static_cast<uint8_t>(result[1])) << 030) +
(static_cast<uint32_t>(static_cast<uint8_t>(result[2])) << 020) +
(static_cast<uint32_t>(static_cast<uint8_t>(result[3])) << 010) +
@@ -262,22 +262,22 @@ TEST_P(MessagePackSignedPos8Test, Case)
{
// create JSON value with integer number
json j = -1;
j.get_ref<json::number_integer_t&>() =
static_cast<json::number_integer_t>(GetParam());
j.get_ref<int64_t&>() =
static_cast<int64_t>(GetParam());
// check type
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xcf));
expected.push_back(static_cast<char>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 010) & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xcf));
expected.push_back(static_cast<uint8_t>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 010) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
// compare result + size
@@ -286,7 +286,7 @@ TEST_P(MessagePackSignedPos8Test, Case)
EXPECT_EQ(result.size(), 9u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xcf));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xcf));
uint64_t restored = (static_cast<uint64_t>(static_cast<uint8_t>(result[1])) << 070) +
(static_cast<uint64_t>(static_cast<uint8_t>(result[2])) << 060) +
(static_cast<uint64_t>(static_cast<uint8_t>(result[3])) << 050) +
@@ -323,9 +323,9 @@ TEST(MessagePackSignedTest, Neg1)
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xd0));
expected.push_back(static_cast<char>(i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xd0));
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -333,7 +333,7 @@ TEST(MessagePackSignedTest, Neg1)
EXPECT_EQ(result.size(), 2u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xd0));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xd0));
EXPECT_EQ(static_cast<int8_t>(result[1]), i);
// roundtrip
@@ -355,10 +355,10 @@ TEST(MessagePackSignedTest, Neg2)
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xd1));
expected.push_back(static_cast<char>((i >> 8) & 0xff));
expected.push_back(static_cast<char>(i & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xd1));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -366,7 +366,7 @@ TEST(MessagePackSignedTest, Neg2)
EXPECT_EQ(result.size(), 3u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xd1));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xd1));
int16_t restored = static_cast<int16_t>((static_cast<uint8_t>(result[1]) << 8) +
static_cast<uint8_t>(result[2]));
EXPECT_EQ(restored, i);
@@ -387,12 +387,12 @@ TEST_P(MessagePackSignedNeg4Test, Case)
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xd2));
expected.push_back(static_cast<char>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xd2));
expected.push_back(static_cast<uint8_t>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -400,7 +400,7 @@ TEST_P(MessagePackSignedNeg4Test, Case)
EXPECT_EQ(result.size(), 5u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xd2));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xd2));
uint32_t restored = (static_cast<uint32_t>(static_cast<uint8_t>(result[1])) << 030) +
(static_cast<uint32_t>(static_cast<uint8_t>(result[2])) << 020) +
(static_cast<uint32_t>(static_cast<uint8_t>(result[3])) << 010) +
@@ -433,16 +433,16 @@ TEST_P(MessagePackSignedNeg8Test, Case)
EXPECT_TRUE(j.is_number_integer());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xd3));
expected.push_back(static_cast<char>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 010) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xd3));
expected.push_back(static_cast<uint8_t>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 010) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -450,7 +450,7 @@ TEST_P(MessagePackSignedNeg8Test, Case)
EXPECT_EQ(result.size(), 9u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xd3));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xd3));
int64_t restored = (static_cast<int64_t>(static_cast<uint8_t>(result[1])) << 070) +
(static_cast<int64_t>(static_cast<uint8_t>(result[2])) << 060) +
(static_cast<int64_t>(static_cast<uint8_t>(result[3])) << 050) +
@@ -487,8 +487,8 @@ TEST(MessagePackUnsignedTest, Pos0)
EXPECT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -496,7 +496,7 @@ TEST(MessagePackUnsignedTest, Pos0)
EXPECT_EQ(result.size(), 1u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(i));
EXPECT_EQ(result[0], static_cast<uint8_t>(i));
// roundtrip
EXPECT_EQ(json::from_msgpack(result), j);
@@ -517,9 +517,9 @@ TEST(MessagePackUnsignedTest, Pos1)
EXPECT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xcc));
expected.push_back(static_cast<char>(i));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xcc));
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -527,7 +527,7 @@ TEST(MessagePackUnsignedTest, Pos1)
EXPECT_EQ(result.size(), 2u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xcc));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xcc));
uint8_t restored = static_cast<uint8_t>(result[1]);
EXPECT_EQ(restored, i);
@@ -550,10 +550,10 @@ TEST(MessagePackUnsignedTest, Pos2)
EXPECT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xcd));
expected.push_back(static_cast<char>((i >> 8) & 0xff));
expected.push_back(static_cast<char>(i & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xcd));
expected.push_back(static_cast<uint8_t>((i >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(i & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -561,7 +561,7 @@ TEST(MessagePackUnsignedTest, Pos2)
EXPECT_EQ(result.size(), 3u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xcd));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xcd));
uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
EXPECT_EQ(restored, i);
@@ -581,12 +581,12 @@ TEST_P(MessagePackUnsignedPos4Test, Case)
EXPECT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xce));
expected.push_back(static_cast<char>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xce));
expected.push_back(static_cast<uint8_t>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -594,7 +594,7 @@ TEST_P(MessagePackUnsignedPos4Test, Case)
EXPECT_EQ(result.size(), 5u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xce));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xce));
uint32_t restored = (static_cast<uint32_t>(static_cast<uint8_t>(result[1])) << 030) +
(static_cast<uint32_t>(static_cast<uint8_t>(result[2])) << 020) +
(static_cast<uint32_t>(static_cast<uint8_t>(result[3])) << 010) +
@@ -620,16 +620,16 @@ TEST_P(MessagePackUnsignedPos8Test, Case)
EXPECT_TRUE(j.is_number_unsigned());
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xcf));
expected.push_back(static_cast<char>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 010) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xcf));
expected.push_back(static_cast<uint8_t>((GetParam() >> 070) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 060) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 050) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 040) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 030) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 020) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 010) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -637,7 +637,7 @@ TEST_P(MessagePackUnsignedPos8Test, Case)
EXPECT_EQ(result.size(), 9u);
// check individual bytes
EXPECT_EQ(result[0], static_cast<char>(0xcf));
EXPECT_EQ(result[0], static_cast<uint8_t>(0xcf));
uint64_t restored = (static_cast<uint64_t>(static_cast<uint8_t>(result[1])) << 070) +
(static_cast<uint64_t>(static_cast<uint8_t>(result[2])) << 060) +
(static_cast<uint64_t>(static_cast<uint8_t>(result[3])) << 050) +
@@ -661,7 +661,7 @@ TEST(MessagePackFloatTest, Number)
{
double v = 3.1415925;
json j = v;
std::string expected = "\xcb\x40\x09\x21\xfb\x3f\xa6\xde\xfc";
std::vector<uint8_t> expected = {0xcb,0x40,0x09,0x21,0xfb,0x3f,0xa6,0xde,0xfc};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -691,12 +691,15 @@ TEST(MessagePackStringTest, String1)
json j = s;
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(first_bytes[N]));
expected.append(s);
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(first_bytes[N]));
for (size_t i = 0; i < N; ++i)
{
expected.push_back('x');
}
// check first byte
EXPECT_EQ((first_bytes[N] & 0x1f), static_cast<char>(N));
EXPECT_EQ((first_bytes[N] & 0x1f), static_cast<uint8_t>(N));
// compare result + size
const auto result = json::to_msgpack(j);
@@ -725,10 +728,13 @@ TEST(MessagePackStringTest, String2)
json j = s;
// create expected byte vector
std::string expected;
expected.push_back(static_cast<char>(0xd9));
expected.push_back(static_cast<char>(N));
expected.append(s);
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xd9));
expected.push_back(static_cast<uint8_t>(N));
for (size_t i = 0; i < N; ++i)
{
expected.push_back('x');
}
// compare result + size
const auto result = json::to_msgpack(j);
@@ -751,11 +757,14 @@ TEST_P(MessagePackString3Test, Case)
json j = s;
// create expected byte vector (hack: create string first)
std::string expected;
expected.push_back(static_cast<char>(0xda));
expected.push_back(static_cast<char>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
expected.append(s);
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xda));
expected.push_back(static_cast<uint8_t>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
for (size_t i = 0; i < GetParam(); ++i)
{
expected.push_back('x');
}
// compare result + size
const auto result = json::to_msgpack(j);
@@ -790,13 +799,16 @@ TEST_P(MessagePackString5Test, Case)
json j = s;
// create expected byte vector (hack: create string first)
std::string expected;
expected.push_back(static_cast<char>(0xdb));
expected.push_back(static_cast<char>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<char>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<char>(GetParam() & 0xff));
expected.append(s);
std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(0xdb));
expected.push_back(static_cast<uint8_t>((GetParam() >> 24) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 16) & 0xff));
expected.push_back(static_cast<uint8_t>((GetParam() >> 8) & 0xff));
expected.push_back(static_cast<uint8_t>(GetParam() & 0xff));
for (size_t i = 0; i < GetParam(); ++i)
{
expected.push_back('x');
}
// compare result + size
const auto result = json::to_msgpack(j);
@@ -821,7 +833,7 @@ INSTANTIATE_TEST_CASE_P(MessagePackString5Tests, MessagePackString5Test,
TEST(MessagePackArrayTest, Empty)
{
json j = json::array();
std::string expected = "\x90";
std::vector<uint8_t> expected = {0x90};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -833,7 +845,7 @@ TEST(MessagePackArrayTest, Empty)
TEST(MessagePackArrayTest, Null)
{
json j = {nullptr};
std::string expected = "\x91\xc0";
std::vector<uint8_t> expected = {0x91,0xc0};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -845,7 +857,7 @@ TEST(MessagePackArrayTest, Null)
TEST(MessagePackArrayTest, Simple)
{
json j = json::parse("[1,2,3,4,5]");
std::string expected = "\x95\x01\x02\x03\x04\x05";
std::vector<uint8_t> expected = {0x95,0x01,0x02,0x03,0x04,0x05};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -857,7 +869,7 @@ TEST(MessagePackArrayTest, Simple)
TEST(MessagePackArrayTest, NestEmpty)
{
json j = json::parse("[[[[]]]]");
std::string expected = "\x91\x91\x91\x90";
std::vector<uint8_t> expected = {0x91,0x91,0x91,0x90};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -869,8 +881,8 @@ TEST(MessagePackArrayTest, NestEmpty)
TEST(MessagePackArrayTest, UInt16)
{
json j(16, nullptr);
std::string expected(j.size() + 3, static_cast<char>(0xc0)); // all null
expected[0] = static_cast<char>(0xdc); // array 16
std::vector<uint8_t> expected(j.size() + 3, static_cast<uint8_t>(0xc0)); // all null
expected[0] = static_cast<uint8_t>(0xdc); // array 16
expected[1] = 0x00; // size (0x0010), byte 0
expected[2] = 0x10; // size (0x0010), byte 1
const auto result = json::to_msgpack(j);
@@ -884,8 +896,8 @@ TEST(MessagePackArrayTest, UInt16)
TEST(MessagePackArrayTest, UInt32)
{
json j(65536, nullptr);
std::string expected(j.size() + 5, static_cast<char>(0xc0)); // all null
expected[0] = static_cast<char>(0xdd); // array 32
std::vector<uint8_t> expected(j.size() + 5, static_cast<uint8_t>(0xc0)); // all null
expected[0] = static_cast<uint8_t>(0xdd); // array 32
expected[1] = 0x00; // size (0x00100000), byte 0
expected[2] = 0x01; // size (0x00100000), byte 1
expected[3] = 0x00; // size (0x00100000), byte 2
@@ -907,7 +919,7 @@ TEST(MessagePackArrayTest, UInt32)
TEST(MessagePackObjectTest, Empty)
{
json j = json::object();
std::string expected = "\x80";
std::vector<uint8_t> expected = {0x80};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -919,7 +931,7 @@ TEST(MessagePackObjectTest, Empty)
TEST(MessagePackObjectTest, EmptyKey)
{
json j = {{"", nullptr}};
std::string expected = "\x81\xa0\xc0";
std::vector<uint8_t> expected = {0x81,0xa0,0xc0};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -931,7 +943,7 @@ TEST(MessagePackObjectTest, EmptyKey)
TEST(MessagePackObjectTest, NestedEmpty)
{
json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}");
std::string expected = "\x81\xa1\x61\x81\xa1\x62\x81\xa1\x63\x80";
std::vector<uint8_t> expected = {0x81,0xa1,0x61,0x81,0xa1,0x62,0x81,0xa1,0x63,0x80};
const auto result = json::to_msgpack(j);
EXPECT_EQ(result, expected);
@@ -955,7 +967,7 @@ TEST(MessagePackObjectTest, UInt16)
// size and the overall size. The rest is then handled in the
// roundtrip check.
EXPECT_EQ(result.size(), 67u); // 1 type, 2 size, 16*4 content
EXPECT_EQ(result[0], static_cast<char>(0xde)); // map 16
EXPECT_EQ(result[0], static_cast<uint8_t>(0xde)); // map 16
EXPECT_EQ(result[1], 0x00); // byte 0 of size (0x0010)
EXPECT_EQ(result[2], 0x10); // byte 1 of size (0x0010)
@@ -984,7 +996,7 @@ TEST(MessagePackObjectTest, UInt32)
// size and the overall size. The rest is then handled in the
// roundtrip check.
EXPECT_EQ(result.size(), 458757u); // 1 type, 4 size, 65536*7 content
EXPECT_EQ(result[0], static_cast<char>(0xdf)); // map 32
EXPECT_EQ(result[0], static_cast<uint8_t>(0xdf)); // map 32
EXPECT_EQ(result[1], 0x00); // byte 0 of size (0x00010000)
EXPECT_EQ(result[2], 0x01); // byte 1 of size (0x00010000)
EXPECT_EQ(result[3], 0x00); // byte 2 of size (0x00010000)
@@ -997,50 +1009,50 @@ TEST(MessagePackObjectTest, UInt32)
// from float32
TEST(MessagePackFloat32Test, Case)
{
auto given = std::string("\xca\x41\xc8\x00\x01", 5);
auto given = std::vector<uint8_t>({0xca,0x41,0xc8,0x00,0x01});
json j = json::from_msgpack(given);
EXPECT_LT(std::fabs(j.get<double>() - 25), 0.001);
}
TEST(MessagePackErrorTest, TooShortByteVector)
{
EXPECT_THROW_MSG(json::from_msgpack("\xcc"), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcc})), json::parse_error,
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack("\xcd"), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcd})), json::parse_error,
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xcd\x00", 2)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcd,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack("\xce"), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xce})), json::parse_error,
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xce\x00", 2)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xce,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xce\x00\x00", 3)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xce,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xce\x00\x00\x00", 4)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xce,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 5: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack("\xcf"), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcf})), json::parse_error,
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xcf\x00", 2)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcf,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xcf\x00\x00", 3)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcf,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xcf\x00\x00\x00", 4)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcf,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 5: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xcf\x00\x00\x00\x00", 5)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcf,0x00,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 6: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xcf\x00\x00\x00\x00\x00", 6)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcf,0x00,0x00,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 7: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xcf\x00\x00\x00\x00\x00\x00", 7)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcf,0x00,0x00,0x00,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 8: unexpected end of input");
EXPECT_THROW_MSG(json::from_msgpack(wpi::StringRef("\xcf\x00\x00\x00\x00\x00\x00\x00", 8)), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xcf,0x00,0x00,0x00,0x00,0x00,0x00,0x00})), json::parse_error,
"[json.exception.parse_error.110] parse error at 9: unexpected end of input");
}
TEST(MessagePackErrorTest, UnsupportedBytesConcrete)
{
EXPECT_THROW_MSG(json::from_msgpack("\xc1"), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xc1})), json::parse_error,
"[json.exception.parse_error.112] parse error at 1: error reading MessagePack; last byte: 0xc1");
EXPECT_THROW_MSG(json::from_msgpack("\xc6"), json::parse_error,
EXPECT_THROW_MSG(json::from_msgpack(std::vector<uint8_t>({0xc6})), json::parse_error,
"[json.exception.parse_error.112] parse error at 1: error reading MessagePack; last byte: 0xc6");
}
@@ -1058,7 +1070,7 @@ TEST(MessagePackErrorTest, UnsupportedBytesAll)
0xd4, 0xd5, 0xd6, 0xd7, 0xd8
})
{
EXPECT_THROW(json::from_msgpack(std::string(1, static_cast<char>(byte))), json::parse_error);
EXPECT_THROW(json::from_msgpack(std::vector<uint8_t>({static_cast<uint8_t>(byte)})), json::parse_error);
}
}
#if 0

View File

@@ -78,11 +78,11 @@ TEST(JsonPointerTest, ObjectT)
// check if null pointers are returned correctly
EXPECT_NE(value.get_ptr<json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<double*>(), nullptr);
}
// pointer access to const object_t
@@ -107,11 +107,11 @@ TEST(JsonPointerTest, ConstObjectT)
// check if null pointers are returned correctly
EXPECT_NE(value.get_ptr<const json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<const bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<const int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const double*>(), nullptr);
}
// pointer access to array_t
@@ -136,11 +136,11 @@ TEST(JsonPointerTest, ArrayT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<json::object_t*>(), nullptr);
EXPECT_NE(value.get_ptr<json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<double*>(), nullptr);
}
// pointer access to const array_t
@@ -165,17 +165,17 @@ TEST(JsonPointerTest, ConstArrayT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<const json::object_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<const bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<const int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const double*>(), nullptr);
}
// pointer access to string_t
TEST(JsonPointerTest, StringT)
{
using test_type = json::string_t;
using test_type = std::string;
json value = "hello";
// check if pointers are returned correctly
@@ -194,17 +194,17 @@ TEST(JsonPointerTest, StringT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::array_t*>(), nullptr);
EXPECT_NE(value.get_ptr<json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_float_t*>(), nullptr);
EXPECT_NE(value.get_ptr<std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<double*>(), nullptr);
}
// pointer access to const string_t
TEST(JsonPointerTest, ConstStringT)
{
using test_type = const json::string_t;
using test_type = const std::string;
const json value = "hello";
// check if pointers are returned correctly
@@ -223,17 +223,17 @@ TEST(JsonPointerTest, ConstStringT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<const json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::array_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_float_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<const bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<const int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const double*>(), nullptr);
}
// pointer access to boolean_t
TEST(JsonPointerTest, BooleanT)
{
using test_type = json::boolean_t;
using test_type = bool;
json value = false;
// check if pointers are returned correctly
@@ -252,17 +252,17 @@ TEST(JsonPointerTest, BooleanT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::string_t*>(), nullptr);
EXPECT_NE(value.get_ptr<json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<std::string*>(), nullptr);
EXPECT_NE(value.get_ptr<bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<double*>(), nullptr);
}
// pointer access to const boolean_t
TEST(JsonPointerTest, ConstBooleanT)
{
using test_type = const json::boolean_t;
using test_type = const bool;
const json value = false;
// check if pointers are returned correctly
@@ -281,17 +281,17 @@ TEST(JsonPointerTest, ConstBooleanT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<const json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::string_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const std::string*>(), nullptr);
EXPECT_NE(value.get_ptr<const bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<const int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const double*>(), nullptr);
}
// pointer access to number_integer_t
TEST(JsonPointerTest, IntegerT)
{
using test_type = json::number_integer_t;
using test_type = int64_t;
json value = 23;
// check if pointers are returned correctly
@@ -310,17 +310,17 @@ TEST(JsonPointerTest, IntegerT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::boolean_t*>(), nullptr);
EXPECT_NE(value.get_ptr<json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<bool*>(), nullptr);
EXPECT_NE(value.get_ptr<int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<double*>(), nullptr);
}
// pointer access to const number_integer_t
TEST(JsonPointerTest, ConstIntegerT)
{
using test_type = const json::number_integer_t;
using test_type = const int64_t;
const json value = 23;
// check if pointers are returned correctly
@@ -339,17 +339,17 @@ TEST(JsonPointerTest, ConstIntegerT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<const json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::boolean_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<const bool*>(), nullptr);
EXPECT_NE(value.get_ptr<const int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const double*>(), nullptr);
}
// pointer access to number_unsigned_t
TEST(JsonPointerTest, UnsignedT)
{
using test_type = json::number_unsigned_t;
using test_type = uint64_t;
json value = 23u;
// check if pointers are returned correctly
@@ -368,17 +368,17 @@ TEST(JsonPointerTest, UnsignedT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::boolean_t*>(), nullptr);
EXPECT_NE(value.get_ptr<json::number_integer_t*>(), nullptr);
EXPECT_NE(value.get_ptr<json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<bool*>(), nullptr);
EXPECT_NE(value.get_ptr<int64_t*>(), nullptr);
EXPECT_NE(value.get_ptr<uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<double*>(), nullptr);
}
// pointer access to const number_unsigned_t
TEST(JsonPointerTest, ConstUnsignedT)
{
using test_type = const json::number_unsigned_t;
using test_type = const uint64_t;
const json value = 23u;
// check if pointers are returned correctly
@@ -397,17 +397,17 @@ TEST(JsonPointerTest, ConstUnsignedT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<const json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::boolean_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const json::number_integer_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const json::number_unsigned_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<const bool*>(), nullptr);
EXPECT_NE(value.get_ptr<const int64_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const uint64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const double*>(), nullptr);
}
// pointer access to number_float_t
TEST(JsonPointerTest, FloatT)
{
using test_type = json::number_float_t;
using test_type = double;
json value = 42.23;
// check if pointers are returned correctly
@@ -426,17 +426,17 @@ TEST(JsonPointerTest, FloatT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<json::number_unsigned_t*>(), nullptr);
EXPECT_NE(value.get_ptr<json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<uint64_t*>(), nullptr);
EXPECT_NE(value.get_ptr<double*>(), nullptr);
}
// pointer access to const number_float_t
TEST(JsonPointerTest, ConstFloatT)
{
using test_type = const json::number_float_t;
using test_type = const double;
const json value = 42.23;
// check if pointers are returned correctly
@@ -455,9 +455,9 @@ TEST(JsonPointerTest, ConstFloatT)
// check if null pointers are returned correctly
EXPECT_EQ(value.get_ptr<const json::object_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::array_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::string_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::boolean_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_integer_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const json::number_unsigned_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const json::number_float_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const std::string*>(), nullptr);
EXPECT_EQ(value.get_ptr<const bool*>(), nullptr);
EXPECT_EQ(value.get_ptr<const int64_t*>(), nullptr);
EXPECT_EQ(value.get_ptr<const uint64_t*>(), nullptr);
EXPECT_NE(value.get_ptr<const double*>(), nullptr);
}

View File

@@ -45,6 +45,8 @@ using wpi::json;
#include <unordered_map>
#include <unordered_set>
#include "wpi/raw_ostream.h"
TEST(JsonReadmeTest, Basic)
{
// create an empty structure (null)

View File

@@ -55,10 +55,10 @@ TEST(JsonReferenceTest, ObjectT)
// check if mismatching references throw correctly
EXPECT_NO_THROW(value.get_ref<json::object_t&>());
EXPECT_ANY_THROW(value.get_ref<json::array_t&>());
EXPECT_ANY_THROW(value.get_ref<json::string_t&>());
EXPECT_ANY_THROW(value.get_ref<json::boolean_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_integer_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_float_t&>());
EXPECT_ANY_THROW(value.get_ref<std::string&>());
EXPECT_ANY_THROW(value.get_ref<bool&>());
EXPECT_ANY_THROW(value.get_ref<int64_t&>());
EXPECT_ANY_THROW(value.get_ref<double&>());
}
// const reference access to const object_t
@@ -94,16 +94,16 @@ TEST(JsonReferenceTest, ArrayT)
// check if mismatching references throw correctly
EXPECT_ANY_THROW(value.get_ref<json::object_t&>());
EXPECT_NO_THROW(value.get_ref<json::array_t&>());
EXPECT_ANY_THROW(value.get_ref<json::string_t&>());
EXPECT_ANY_THROW(value.get_ref<json::boolean_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_integer_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_float_t&>());
EXPECT_ANY_THROW(value.get_ref<std::string&>());
EXPECT_ANY_THROW(value.get_ref<bool&>());
EXPECT_ANY_THROW(value.get_ref<int64_t&>());
EXPECT_ANY_THROW(value.get_ref<double&>());
}
// reference access to string_t
TEST(JsonReferenceTest, StringT)
{
using test_type = json::string_t;
using test_type = std::string;
json value = "hello";
// check if references are returned correctly
@@ -118,16 +118,16 @@ TEST(JsonReferenceTest, StringT)
// check if mismatching references throw correctly
EXPECT_ANY_THROW(value.get_ref<json::object_t&>());
EXPECT_ANY_THROW(value.get_ref<json::array_t&>());
EXPECT_NO_THROW(value.get_ref<json::string_t&>());
EXPECT_ANY_THROW(value.get_ref<json::boolean_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_integer_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_float_t&>());
EXPECT_NO_THROW(value.get_ref<std::string&>());
EXPECT_ANY_THROW(value.get_ref<bool&>());
EXPECT_ANY_THROW(value.get_ref<int64_t&>());
EXPECT_ANY_THROW(value.get_ref<double&>());
}
// reference access to boolean_t
TEST(JsonReferenceTest, BooleanT)
{
using test_type = json::boolean_t;
using test_type = bool;
json value = false;
// check if references are returned correctly
@@ -142,16 +142,16 @@ TEST(JsonReferenceTest, BooleanT)
// check if mismatching references throw correctly
EXPECT_ANY_THROW(value.get_ref<json::object_t&>());
EXPECT_ANY_THROW(value.get_ref<json::array_t&>());
EXPECT_ANY_THROW(value.get_ref<json::string_t&>());
EXPECT_NO_THROW(value.get_ref<json::boolean_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_integer_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_float_t&>());
EXPECT_ANY_THROW(value.get_ref<std::string&>());
EXPECT_NO_THROW(value.get_ref<bool&>());
EXPECT_ANY_THROW(value.get_ref<int64_t&>());
EXPECT_ANY_THROW(value.get_ref<double&>());
}
// reference access to number_integer_t
TEST(JsonReferenceTest, IntegerT)
{
using test_type = json::number_integer_t;
using test_type = int64_t;
json value = 23;
// check if references are returned correctly
@@ -166,16 +166,16 @@ TEST(JsonReferenceTest, IntegerT)
// check if mismatching references throw correctly
EXPECT_ANY_THROW(value.get_ref<json::object_t&>());
EXPECT_ANY_THROW(value.get_ref<json::array_t&>());
EXPECT_ANY_THROW(value.get_ref<json::string_t&>());
EXPECT_ANY_THROW(value.get_ref<json::boolean_t&>());
EXPECT_NO_THROW(value.get_ref<json::number_integer_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_float_t&>());
EXPECT_ANY_THROW(value.get_ref<std::string&>());
EXPECT_ANY_THROW(value.get_ref<bool&>());
EXPECT_NO_THROW(value.get_ref<int64_t&>());
EXPECT_ANY_THROW(value.get_ref<double&>());
}
// reference access to number_float_t
TEST(JsonReferenceTest, FloatT)
{
using test_type = json::number_float_t;
using test_type = double;
json value = 42.23;
// check if references are returned correctly
@@ -190,8 +190,8 @@ TEST(JsonReferenceTest, FloatT)
// check if mismatching references throw correctly
EXPECT_ANY_THROW(value.get_ref<json::object_t&>());
EXPECT_ANY_THROW(value.get_ref<json::array_t&>());
EXPECT_ANY_THROW(value.get_ref<json::string_t&>());
EXPECT_ANY_THROW(value.get_ref<json::boolean_t&>());
EXPECT_ANY_THROW(value.get_ref<json::number_integer_t&>());
EXPECT_NO_THROW(value.get_ref<json::number_float_t&>());
EXPECT_ANY_THROW(value.get_ref<std::string&>());
EXPECT_ANY_THROW(value.get_ref<bool&>());
EXPECT_ANY_THROW(value.get_ref<int64_t&>());
EXPECT_NO_THROW(value.get_ref<double&>());
}

View File

@@ -39,6 +39,7 @@ using wpi::json;
#include "wpi/Format.h"
#include "wpi/StringExtras.h"
#include "wpi/raw_ostream.h"
#include <fstream>