2016-11-11 16:11:13 +00:00
|
|
|
/*
|
|
|
|
__ _____ _____ _____
|
|
|
|
__| | __| | | | JSON for Modern C++ (test suite)
|
2017-02-25 15:34:38 +00:00
|
|
|
| | |__ | | | | | | version 2.1.1
|
2016-11-11 16:11:13 +00:00
|
|
|
|_____|_____|_____|_|___| https://github.com/nlohmann/json
|
|
|
|
|
|
|
|
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
|
2017-01-02 08:40:00 +00:00
|
|
|
Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
|
2016-11-11 16:11:13 +00:00
|
|
|
|
|
|
|
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 "catch.hpp"
|
|
|
|
|
|
|
|
#include "json.hpp"
|
|
|
|
using nlohmann::json;
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
|
|
TEST_CASE("MessagePack")
|
|
|
|
{
|
|
|
|
SECTION("individual values")
|
|
|
|
{
|
2016-12-07 12:47:08 +00:00
|
|
|
SECTION("discarded")
|
|
|
|
{
|
|
|
|
// discarded values are not serialized
|
|
|
|
json j = json::value_t::discarded;
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result.empty());
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:11:13 +00:00
|
|
|
SECTION("null")
|
|
|
|
{
|
|
|
|
json j = nullptr;
|
|
|
|
std::vector<uint8_t> expected = {0xc0};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("boolean")
|
|
|
|
{
|
|
|
|
SECTION("true")
|
|
|
|
{
|
|
|
|
json j = true;
|
|
|
|
std::vector<uint8_t> expected = {0xc3};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("false")
|
|
|
|
{
|
|
|
|
json j = false;
|
|
|
|
std::vector<uint8_t> expected = {0xc2};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("number")
|
|
|
|
{
|
|
|
|
SECTION("signed")
|
|
|
|
{
|
|
|
|
SECTION("-32..-1 (negative fixnum)")
|
|
|
|
{
|
|
|
|
for (auto i = -32; i <= -1; ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(static_cast<uint8_t>(i));
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 1);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(static_cast<int8_t>(result[0]) == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("0..127 (positive fixnum)")
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i <= 127; ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with integer number
|
|
|
|
json j = -1;
|
|
|
|
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(static_cast<uint8_t>(i));
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 1);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-11 12:36:08 +00:00
|
|
|
SECTION("128..255 (int 8)")
|
|
|
|
{
|
|
|
|
for (size_t i = 128; i <= 255; ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with integer number
|
|
|
|
json j = -1;
|
|
|
|
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xcc);
|
|
|
|
expected.push_back(static_cast<uint8_t>(i));
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 2);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xcc);
|
|
|
|
uint8_t restored = static_cast<uint8_t>(result[1]);
|
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("256..65535 (int 16)")
|
|
|
|
{
|
|
|
|
for (size_t i = 256; i <= 65535; ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with integer number
|
|
|
|
json j = -1;
|
|
|
|
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(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);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 3);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xcd);
|
2017-03-16 17:39:33 +00:00
|
|
|
uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
|
2016-12-11 12:36:08 +00:00
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("65536..4294967295 (int 32)")
|
|
|
|
{
|
|
|
|
for (uint32_t i :
|
|
|
|
{
|
|
|
|
65536u, 77777u, 1048576u, 4294967295u
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with integer number
|
|
|
|
json j = -1;
|
|
|
|
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xce);
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 16) & 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);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 5);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xce);
|
2017-03-16 17:39:33 +00:00
|
|
|
uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
|
2016-12-11 12:36:08 +00:00
|
|
|
(static_cast<uint32_t>(result[2]) << 020) +
|
|
|
|
(static_cast<uint32_t>(result[3]) << 010) +
|
2017-03-16 17:39:33 +00:00
|
|
|
static_cast<uint32_t>(result[4]);
|
2016-12-11 12:36:08 +00:00
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("4294967296..9223372036854775807 (int 64)")
|
|
|
|
{
|
|
|
|
for (uint64_t i :
|
|
|
|
{
|
|
|
|
4294967296lu, 9223372036854775807lu
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with integer number
|
|
|
|
json j = -1;
|
|
|
|
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xcf);
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>(i & 0xff));
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 9);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xcf);
|
2017-03-16 17:39:33 +00:00
|
|
|
uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
|
2016-12-11 12:36:08 +00:00
|
|
|
(static_cast<uint64_t>(result[2]) << 060) +
|
|
|
|
(static_cast<uint64_t>(result[3]) << 050) +
|
|
|
|
(static_cast<uint64_t>(result[4]) << 040) +
|
|
|
|
(static_cast<uint64_t>(result[5]) << 030) +
|
|
|
|
(static_cast<uint64_t>(result[6]) << 020) +
|
|
|
|
(static_cast<uint64_t>(result[7]) << 010) +
|
2017-03-16 17:39:33 +00:00
|
|
|
static_cast<uint64_t>(result[8]);
|
2016-12-11 12:36:08 +00:00
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-11 16:11:13 +00:00
|
|
|
SECTION("-128..-33 (int 8)")
|
|
|
|
{
|
|
|
|
for (auto i = -128; i <= -33; ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xd0);
|
|
|
|
expected.push_back(static_cast<uint8_t>(i));
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 2);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xd0);
|
|
|
|
CHECK(static_cast<int8_t>(result[1]) == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("-9263 (int 16)")
|
|
|
|
{
|
|
|
|
json j = -9263;
|
|
|
|
std::vector<uint8_t> expected = {0xd1, 0xdb, 0xd1};
|
|
|
|
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
2017-02-22 17:14:29 +00:00
|
|
|
int16_t restored = static_cast<int16_t>((result[1] << 8) + result[2]);
|
2016-11-11 16:11:13 +00:00
|
|
|
CHECK(restored == -9263);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("-32768..-129 (int 16)")
|
|
|
|
{
|
|
|
|
for (int16_t i = -32768; i <= -129; ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(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);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 3);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xd1);
|
2017-02-22 17:14:29 +00:00
|
|
|
int16_t restored = static_cast<int16_t>((result[1] << 8) + result[2]);
|
2016-11-11 16:11:13 +00:00
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 08:19:46 +00:00
|
|
|
|
|
|
|
SECTION("-32769..-2147483648")
|
|
|
|
{
|
2016-12-07 15:43:55 +00:00
|
|
|
std::vector<int32_t> numbers;
|
|
|
|
numbers.push_back(-32769);
|
|
|
|
numbers.push_back(-65536);
|
|
|
|
numbers.push_back(-77777);
|
|
|
|
numbers.push_back(-1048576);
|
2017-02-20 17:32:11 +00:00
|
|
|
numbers.push_back(-2147483648ll);
|
2016-12-07 15:43:55 +00:00
|
|
|
for (auto i : numbers)
|
2016-12-07 08:19:46 +00:00
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xd2);
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 16) & 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);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 5);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xd2);
|
2017-03-16 17:39:33 +00:00
|
|
|
uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
|
2016-12-07 20:25:22 +00:00
|
|
|
(static_cast<uint32_t>(result[2]) << 020) +
|
|
|
|
(static_cast<uint32_t>(result[3]) << 010) +
|
2017-03-16 17:39:33 +00:00
|
|
|
static_cast<uint32_t>(result[4]);
|
2016-12-07 08:19:46 +00:00
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 12:47:08 +00:00
|
|
|
|
|
|
|
SECTION("-9223372036854775808..-2147483649 (int 64)")
|
|
|
|
{
|
2016-12-07 15:51:15 +00:00
|
|
|
std::vector<int64_t> numbers;
|
2016-12-07 18:15:05 +00:00
|
|
|
numbers.push_back(INT64_MIN);
|
|
|
|
numbers.push_back(-2147483649ll);
|
2016-12-07 15:51:15 +00:00
|
|
|
for (auto i : numbers)
|
2016-12-07 12:47:08 +00:00
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with unsigned integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_integer());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xd3);
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>(i & 0xff));
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 9);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xd3);
|
2017-03-16 17:39:33 +00:00
|
|
|
int64_t restored = (static_cast<int64_t>(result[1]) << 070) +
|
|
|
|
(static_cast<int64_t>(result[2]) << 060) +
|
|
|
|
(static_cast<int64_t>(result[3]) << 050) +
|
|
|
|
(static_cast<int64_t>(result[4]) << 040) +
|
|
|
|
(static_cast<int64_t>(result[5]) << 030) +
|
|
|
|
(static_cast<int64_t>(result[6]) << 020) +
|
|
|
|
(static_cast<int64_t>(result[7]) << 010) +
|
|
|
|
static_cast<int64_t>(result[8]);
|
2016-12-07 12:47:08 +00:00
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 16:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("unsigned")
|
|
|
|
{
|
|
|
|
SECTION("0..127 (positive fixnum)")
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i <= 127; ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with unsigned integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_unsigned());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(static_cast<uint8_t>(i));
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 1);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("128..255 (uint 8)")
|
|
|
|
{
|
|
|
|
for (size_t i = 128; i <= 255; ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with unsigned integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_unsigned());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xcc);
|
|
|
|
expected.push_back(static_cast<uint8_t>(i));
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 2);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xcc);
|
|
|
|
uint8_t restored = static_cast<uint8_t>(result[1]);
|
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("256..65535 (uint 16)")
|
|
|
|
{
|
|
|
|
for (size_t i = 256; i <= 65535; ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with unsigned integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_unsigned());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(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);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 3);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xcd);
|
2017-03-16 17:39:33 +00:00
|
|
|
uint16_t restored = static_cast<uint16_t>(static_cast<uint8_t>(result[1]) * 256 + static_cast<uint8_t>(result[2]));
|
2016-11-11 16:11:13 +00:00
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("65536..4294967295 (uint 32)")
|
|
|
|
{
|
|
|
|
for (uint32_t i :
|
|
|
|
{
|
2016-12-07 08:19:46 +00:00
|
|
|
65536u, 77777u, 1048576u, 4294967295u
|
2016-11-11 16:11:13 +00:00
|
|
|
})
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with unsigned integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_unsigned());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xce);
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 24) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 16) & 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);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 5);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xce);
|
2017-03-16 17:39:33 +00:00
|
|
|
uint32_t restored = (static_cast<uint32_t>(result[1]) << 030) +
|
2016-11-11 16:11:13 +00:00
|
|
|
(static_cast<uint32_t>(result[2]) << 020) +
|
|
|
|
(static_cast<uint32_t>(result[3]) << 010) +
|
2017-03-16 17:39:33 +00:00
|
|
|
static_cast<uint32_t>(result[4]);
|
2016-11-11 16:11:13 +00:00
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
2016-12-07 08:19:46 +00:00
|
|
|
|
|
|
|
SECTION("4294967296..18446744073709551615 (uint 64)")
|
|
|
|
{
|
|
|
|
for (uint64_t i :
|
|
|
|
{
|
|
|
|
4294967296lu, 18446744073709551615lu
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
|
|
|
|
// create JSON value with unsigned integer number
|
|
|
|
json j = i;
|
|
|
|
|
|
|
|
// check type
|
|
|
|
CHECK(j.is_number_unsigned());
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xcf);
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 070) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 060) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 050) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 040) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 030) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 020) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>((i >> 010) & 0xff));
|
|
|
|
expected.push_back(static_cast<uint8_t>(i & 0xff));
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == 9);
|
|
|
|
|
|
|
|
// check individual bytes
|
|
|
|
CHECK(result[0] == 0xcf);
|
2017-03-16 17:39:33 +00:00
|
|
|
uint64_t restored = (static_cast<uint64_t>(result[1]) << 070) +
|
2016-12-07 08:19:46 +00:00
|
|
|
(static_cast<uint64_t>(result[2]) << 060) +
|
|
|
|
(static_cast<uint64_t>(result[3]) << 050) +
|
|
|
|
(static_cast<uint64_t>(result[4]) << 040) +
|
|
|
|
(static_cast<uint64_t>(result[5]) << 030) +
|
|
|
|
(static_cast<uint64_t>(result[6]) << 020) +
|
|
|
|
(static_cast<uint64_t>(result[7]) << 010) +
|
2017-03-16 17:39:33 +00:00
|
|
|
static_cast<uint64_t>(result[8]);
|
2016-12-07 08:19:46 +00:00
|
|
|
CHECK(restored == i);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 16:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("float")
|
|
|
|
{
|
|
|
|
SECTION("3.1415925")
|
|
|
|
{
|
|
|
|
double v = 3.1415925;
|
|
|
|
json j = v;
|
|
|
|
std::vector<uint8_t> expected =
|
|
|
|
{
|
|
|
|
0xcb, 0x40, 0x09, 0x21, 0xfb, 0x3f, 0xa6, 0xde, 0xfc
|
|
|
|
};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
2017-04-08 21:39:17 +00:00
|
|
|
CHECK(json::from_msgpack(result) == v);
|
2016-11-11 16:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("string")
|
|
|
|
{
|
|
|
|
SECTION("N = 0..31")
|
|
|
|
{
|
|
|
|
// explicitly enumerate the first byte for all 32 strings
|
|
|
|
const std::vector<uint8_t> first_bytes =
|
|
|
|
{
|
|
|
|
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
|
|
|
|
0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1,
|
|
|
|
0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
|
|
|
|
0xbb, 0xbc, 0xbd, 0xbe, 0xbf
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size_t N = 0; N < first_bytes.size(); ++N)
|
|
|
|
{
|
|
|
|
CAPTURE(N);
|
|
|
|
|
|
|
|
// create JSON value with string containing of N * 'x'
|
|
|
|
const auto s = std::string(N, 'x');
|
|
|
|
json j = s;
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(first_bytes[N]);
|
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
{
|
|
|
|
expected.push_back('x');
|
|
|
|
}
|
|
|
|
|
|
|
|
// check first byte
|
|
|
|
CHECK((first_bytes[N] & 0x1f) == N);
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == N + 1);
|
|
|
|
// check that no null byte is appended
|
|
|
|
if (N > 0)
|
|
|
|
{
|
|
|
|
CHECK(result.back() != '\x00');
|
|
|
|
}
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("N = 32..255")
|
|
|
|
{
|
|
|
|
for (size_t N = 32; N <= 255; ++N)
|
|
|
|
{
|
|
|
|
CAPTURE(N);
|
|
|
|
|
|
|
|
// create JSON value with string containing of N * 'x'
|
|
|
|
const auto s = std::string(N, 'x');
|
|
|
|
json j = s;
|
|
|
|
|
|
|
|
// create expected byte vector
|
|
|
|
std::vector<uint8_t> expected;
|
|
|
|
expected.push_back(0xd9);
|
2016-11-16 21:08:21 +00:00
|
|
|
expected.push_back(static_cast<uint8_t>(N));
|
2016-11-11 16:11:13 +00:00
|
|
|
for (size_t i = 0; i < N; ++i)
|
|
|
|
{
|
|
|
|
expected.push_back('x');
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == N + 2);
|
|
|
|
// check that no null byte is appended
|
|
|
|
CHECK(result.back() != '\x00');
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("N = 256..65535")
|
|
|
|
{
|
|
|
|
for (size_t N :
|
|
|
|
{
|
|
|
|
256u, 999u, 1025u, 3333u, 2048u, 65535u
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CAPTURE(N);
|
|
|
|
|
|
|
|
// create JSON value with string containing of N * 'x'
|
|
|
|
const auto s = std::string(N, 'x');
|
|
|
|
json j = s;
|
|
|
|
|
|
|
|
// create expected byte vector (hack: create string first)
|
|
|
|
std::vector<uint8_t> expected(N, 'x');
|
|
|
|
// reverse order of commands, because we insert at begin()
|
|
|
|
expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
|
|
|
|
expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
|
|
|
|
expected.insert(expected.begin(), 0xda);
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == N + 3);
|
|
|
|
// check that no null byte is appended
|
|
|
|
CHECK(result.back() != '\x00');
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("N = 65536..4294967295")
|
|
|
|
{
|
|
|
|
for (size_t N :
|
|
|
|
{
|
|
|
|
65536u, 77777u, 1048576u
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CAPTURE(N);
|
|
|
|
|
|
|
|
// create JSON value with string containing of N * 'x'
|
|
|
|
const auto s = std::string(N, 'x');
|
|
|
|
json j = s;
|
|
|
|
|
|
|
|
// create expected byte vector (hack: create string first)
|
|
|
|
std::vector<uint8_t> expected(N, 'x');
|
|
|
|
// reverse order of commands, because we insert at begin()
|
|
|
|
expected.insert(expected.begin(), static_cast<uint8_t>(N & 0xff));
|
|
|
|
expected.insert(expected.begin(), static_cast<uint8_t>((N >> 8) & 0xff));
|
|
|
|
expected.insert(expected.begin(), static_cast<uint8_t>((N >> 16) & 0xff));
|
|
|
|
expected.insert(expected.begin(), static_cast<uint8_t>((N >> 24) & 0xff));
|
|
|
|
expected.insert(expected.begin(), 0xdb);
|
|
|
|
|
|
|
|
// compare result + size
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
CHECK(result.size() == N + 5);
|
|
|
|
// check that no null byte is appended
|
|
|
|
CHECK(result.back() != '\x00');
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("array")
|
|
|
|
{
|
|
|
|
SECTION("empty")
|
|
|
|
{
|
|
|
|
json j = json::array();
|
|
|
|
std::vector<uint8_t> expected = {0x90};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("[null]")
|
|
|
|
{
|
|
|
|
json j = {nullptr};
|
|
|
|
std::vector<uint8_t> expected = {0x91, 0xc0};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("[1,2,3,4,5]")
|
|
|
|
{
|
|
|
|
json j = json::parse("[1,2,3,4,5]");
|
|
|
|
std::vector<uint8_t> expected = {0x95, 0x01, 0x02, 0x03, 0x04, 0x05};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("[[[[]]]]")
|
|
|
|
{
|
|
|
|
json j = json::parse("[[[[]]]]");
|
|
|
|
std::vector<uint8_t> expected = {0x91, 0x91, 0x91, 0x90};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
2016-12-07 12:47:08 +00:00
|
|
|
|
|
|
|
SECTION("array 16")
|
|
|
|
{
|
|
|
|
json j(16, nullptr);
|
|
|
|
std::vector<uint8_t> expected(j.size() + 3, 0xc0); // all null
|
|
|
|
expected[0] = 0xdc; // array 16
|
|
|
|
expected[1] = 0x00; // size (0x0010), byte 0
|
|
|
|
expected[2] = 0x10; // size (0x0010), byte 1
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("array 32")
|
|
|
|
{
|
|
|
|
json j(65536, nullptr);
|
|
|
|
std::vector<uint8_t> expected(j.size() + 5, 0xc0); // all null
|
|
|
|
expected[0] = 0xdd; // array 32
|
|
|
|
expected[1] = 0x00; // size (0x00100000), byte 0
|
|
|
|
expected[2] = 0x01; // size (0x00100000), byte 1
|
|
|
|
expected[3] = 0x00; // size (0x00100000), byte 2
|
|
|
|
expected[4] = 0x00; // size (0x00100000), byte 3
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
//CHECK(result == expected);
|
|
|
|
|
|
|
|
CHECK(result.size() == expected.size());
|
|
|
|
for (size_t i = 0; i < expected.size(); ++i)
|
|
|
|
{
|
|
|
|
CAPTURE(i);
|
|
|
|
CHECK(result[i] == expected[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
2016-11-11 16:11:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("object")
|
|
|
|
{
|
|
|
|
SECTION("empty")
|
|
|
|
{
|
|
|
|
json j = json::object();
|
|
|
|
std::vector<uint8_t> expected = {0x80};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("{\"\":null}")
|
|
|
|
{
|
|
|
|
json j = {{"", nullptr}};
|
|
|
|
std::vector<uint8_t> expected = {0x81, 0xa0, 0xc0};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("{\"a\": {\"b\": {\"c\": {}}}}")
|
|
|
|
{
|
|
|
|
json j = json::parse("{\"a\": {\"b\": {\"c\": {}}}}");
|
|
|
|
std::vector<uint8_t> expected =
|
|
|
|
{
|
|
|
|
0x81, 0xa1, 0x61, 0x81, 0xa1, 0x62, 0x81, 0xa1, 0x63, 0x80
|
|
|
|
};
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
CHECK(result == expected);
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
2016-12-07 12:47:08 +00:00
|
|
|
|
|
|
|
SECTION("map 16")
|
|
|
|
{
|
|
|
|
json j = R"({"00": null, "01": null, "02": null, "03": null,
|
|
|
|
"04": null, "05": null, "06": null, "07": null,
|
|
|
|
"08": null, "09": null, "10": null, "11": null,
|
|
|
|
"12": null, "13": null, "14": null, "15": null})"_json;
|
|
|
|
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
|
|
|
|
// Checking against an expected vector byte by byte is
|
|
|
|
// difficult, because no assumption on the order of key/value
|
|
|
|
// pairs are made. We therefore only check the prefix (type and
|
|
|
|
// size and the overall size. The rest is then handled in the
|
|
|
|
// roundtrip check.
|
|
|
|
CHECK(result.size() == 67); // 1 type, 2 size, 16*4 content
|
|
|
|
CHECK(result[0] == 0xde); // map 16
|
|
|
|
CHECK(result[1] == 0x00); // byte 0 of size (0x0010)
|
|
|
|
CHECK(result[2] == 0x10); // byte 1 of size (0x0010)
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("map 32")
|
|
|
|
{
|
|
|
|
json j;
|
|
|
|
for (auto i = 0; i < 65536; ++i)
|
|
|
|
{
|
|
|
|
// format i to a fixed width of 5
|
|
|
|
// each entry will need 7 bytes: 6 for fixstr, 1 for null
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::setw(5) << std::setfill('0') << i;
|
|
|
|
j.emplace(ss.str(), nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto result = json::to_msgpack(j);
|
|
|
|
|
|
|
|
// Checking against an expected vector byte by byte is
|
|
|
|
// difficult, because no assumption on the order of key/value
|
|
|
|
// pairs are made. We therefore only check the prefix (type and
|
|
|
|
// size and the overall size. The rest is then handled in the
|
|
|
|
// roundtrip check.
|
|
|
|
CHECK(result.size() == 458757); // 1 type, 4 size, 65536*7 content
|
|
|
|
CHECK(result[0] == 0xdf); // map 32
|
|
|
|
CHECK(result[1] == 0x00); // byte 0 of size (0x00010000)
|
|
|
|
CHECK(result[2] == 0x01); // byte 1 of size (0x00010000)
|
|
|
|
CHECK(result[3] == 0x00); // byte 2 of size (0x00010000)
|
|
|
|
CHECK(result[4] == 0x00); // byte 3 of size (0x00010000)
|
|
|
|
|
|
|
|
// roundtrip
|
|
|
|
CHECK(json::from_msgpack(result) == j);
|
|
|
|
}
|
2016-11-11 16:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-10 23:05:29 +00:00
|
|
|
|
2016-12-10 23:39:26 +00:00
|
|
|
SECTION("from float32")
|
2016-12-10 23:05:29 +00:00
|
|
|
{
|
2016-12-10 23:39:26 +00:00
|
|
|
auto given = std::vector<uint8_t>({0xca, 0x41, 0xc8, 0x00, 0x01});
|
2016-12-10 23:05:29 +00:00
|
|
|
json j = json::from_msgpack(given);
|
2016-12-10 23:39:26 +00:00
|
|
|
CHECK(j.get<double>() == Approx(25.0000019073486));
|
2016-12-10 23:05:29 +00:00
|
|
|
}
|
2017-03-07 19:05:34 +00:00
|
|
|
|
|
|
|
SECTION("errors")
|
|
|
|
{
|
|
|
|
SECTION("too short byte vector")
|
|
|
|
{
|
2017-07-07 20:41:22 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcc})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcd})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcd, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xce})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcf})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
|
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})), json::parse_error&);
|
2017-03-07 19:05:34 +00:00
|
|
|
|
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcc})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcd})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcd, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xce})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xce, 0x00, 0x00, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 5: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcf})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 2: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 3: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 4: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 5: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 6: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 7: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 8: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00})),
|
2017-04-04 14:59:19 +00:00
|
|
|
"[json.exception.parse_error.110] parse error at 9: unexpected end of input");
|
2017-03-07 19:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("unsupported bytes")
|
|
|
|
{
|
|
|
|
SECTION("concrete examples")
|
|
|
|
{
|
2017-07-07 20:41:22 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xc1})), json::parse_error&);
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xc1})),
|
|
|
|
"[json.exception.parse_error.112] parse error at 1: error reading MessagePack; last byte: 0xc1");
|
2017-07-07 20:41:22 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0xc6})), json::parse_error&);
|
2017-03-07 19:05:34 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0xc6})),
|
|
|
|
"[json.exception.parse_error.112] parse error at 1: error reading MessagePack; last byte: 0xc6");
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("all unsupported bytes")
|
|
|
|
{
|
|
|
|
for (auto byte :
|
|
|
|
{
|
|
|
|
// never used
|
|
|
|
0xc1,
|
|
|
|
// bin
|
|
|
|
0xc4, 0xc5, 0xc6,
|
|
|
|
// ext
|
|
|
|
0xc7, 0xc8, 0xc9,
|
|
|
|
// fixext
|
|
|
|
0xd4, 0xd5, 0xd6, 0xd7, 0xd8
|
|
|
|
})
|
|
|
|
{
|
2017-07-07 20:41:22 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({static_cast<uint8_t>(byte)})), json::parse_error&);
|
2017-03-07 19:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-20 20:03:36 +00:00
|
|
|
|
|
|
|
SECTION("invalid string in map")
|
|
|
|
{
|
2017-07-07 20:41:22 +00:00
|
|
|
CHECK_THROWS_AS(json::from_msgpack(std::vector<uint8_t>({0x81, 0xff, 0x01})), json::parse_error&);
|
2017-06-20 20:03:36 +00:00
|
|
|
CHECK_THROWS_WITH(json::from_msgpack(std::vector<uint8_t>({0x81, 0xff, 0x01})),
|
|
|
|
"[json.exception.parse_error.113] parse error at 2: expected a MessagePack string; last byte: 0xff");
|
|
|
|
}
|
2017-03-07 19:05:34 +00:00
|
|
|
}
|
2016-11-11 16:31:37 +00:00
|
|
|
}
|
2016-11-11 16:11:13 +00:00
|
|
|
|
2016-11-16 21:49:16 +00:00
|
|
|
|
|
|
|
// use this testcase outside [hide] to run it with Valgrind
|
|
|
|
TEST_CASE("single MessagePack roundtrip")
|
|
|
|
{
|
|
|
|
SECTION("sample.json")
|
|
|
|
{
|
|
|
|
std::string filename = "test/data/json_testsuite/sample.json";
|
|
|
|
|
|
|
|
// parse JSON file
|
|
|
|
std::ifstream f_json(filename);
|
|
|
|
json j1 = json::parse(f_json);
|
|
|
|
|
|
|
|
// parse MessagePack file
|
|
|
|
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
|
|
|
|
std::vector<uint8_t> packed((std::istreambuf_iterator<char>(f_msgpack)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
json j2;
|
|
|
|
CHECK_NOTHROW(j2 = json::from_msgpack(packed));
|
|
|
|
|
|
|
|
// compare parsed JSON values
|
|
|
|
CHECK(j1 == j2);
|
2017-02-19 20:17:05 +00:00
|
|
|
|
2017-07-27 22:21:55 +00:00
|
|
|
SECTION("roundtrips")
|
|
|
|
{
|
|
|
|
SECTION("std::ostringstream")
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
json::to_msgpack(j1, ss);
|
|
|
|
json j3 = json::from_msgpack(ss.str());
|
|
|
|
CHECK(j1 == j3);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("std::string")
|
|
|
|
{
|
|
|
|
std::string s;
|
|
|
|
json::to_msgpack(j1, s);
|
|
|
|
json j3 = json::from_msgpack(s);
|
|
|
|
CHECK(j1 == j3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-19 20:17:05 +00:00
|
|
|
// check with different start index
|
|
|
|
packed.insert(packed.begin(), 5, 0xff);
|
|
|
|
CHECK(j1 == json::from_msgpack(packed, 5));
|
2016-11-16 21:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-11 16:31:37 +00:00
|
|
|
TEST_CASE("MessagePack roundtrips", "[hide]")
|
|
|
|
{
|
2016-11-11 16:11:13 +00:00
|
|
|
SECTION("input from msgpack-python")
|
|
|
|
{
|
|
|
|
for (std::string filename :
|
|
|
|
{
|
|
|
|
"test/data/json_nlohmann_tests/all_unicode.json",
|
|
|
|
"test/data/json.org/1.json",
|
|
|
|
"test/data/json.org/2.json",
|
|
|
|
"test/data/json.org/3.json",
|
|
|
|
"test/data/json.org/4.json",
|
|
|
|
"test/data/json.org/5.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip01.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip02.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip03.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip04.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip05.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip06.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip07.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip08.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip09.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip10.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip11.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip12.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip13.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip14.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip15.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip16.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip17.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip18.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip19.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip20.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip21.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip22.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip23.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip24.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip25.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip26.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip27.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip28.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip29.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip30.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip31.json",
|
|
|
|
"test/data/json_roundtrip/roundtrip32.json",
|
2016-11-16 21:49:16 +00:00
|
|
|
"test/data/json_testsuite/sample.json", // kills AppVeyor
|
2016-11-11 16:11:13 +00:00
|
|
|
"test/data/json_tests/pass1.json",
|
|
|
|
"test/data/json_tests/pass2.json",
|
|
|
|
"test/data/json_tests/pass3.json",
|
|
|
|
"test/data/regression/floats.json",
|
|
|
|
"test/data/regression/signed_ints.json",
|
|
|
|
"test/data/regression/unsigned_ints.json",
|
|
|
|
"test/data/regression/working_file.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_arraysWithSpaces.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_empty-string.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_empty.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_ending_with_newline.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_false.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_heterogeneous.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_null.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_with_1_and_newline.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_with_leading_space.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_with_several_null.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_array_with_trailing_space.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_0e+1.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_0e1.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_after_space.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_double_close_to_zero.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_double_huge_neg_exp.json",
|
2017-03-12 17:38:05 +00:00
|
|
|
//"test/data/nst_json_testsuite/test_parsing/y_number_huge_exp.json",
|
2016-11-11 16:11:13 +00:00
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_int_with_exp.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_minus_zero.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_negative_int.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_negative_one.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_negative_zero.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_real_capital_e.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_real_capital_e_neg_exp.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_real_capital_e_pos_exp.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_real_exponent.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_real_fraction_exponent.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_real_neg_exp.json",
|
2017-03-12 17:38:05 +00:00
|
|
|
//"test/data/nst_json_testsuite/test_parsing/y_number_real_neg_overflow.json",
|
2016-11-11 16:11:13 +00:00
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_real_pos_exponent.json",
|
2017-03-12 17:38:05 +00:00
|
|
|
//"test/data/nst_json_testsuite/test_parsing/y_number_real_pos_overflow.json",
|
2016-11-11 16:11:13 +00:00
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_real_underflow.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_simple_int.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_number_simple_real.json",
|
|
|
|
//"test/data/nst_json_testsuite/test_parsing/y_number_too_big_neg_int.json",
|
|
|
|
//"test/data/nst_json_testsuite/test_parsing/y_number_too_big_pos_int.json",
|
|
|
|
//"test/data/nst_json_testsuite/test_parsing/y_number_very_big_negative_int.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_basic.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_duplicated_key.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_duplicated_key_and_value.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_empty.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_empty_key.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_escaped_null_in_key.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_extreme_numbers.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_long_strings.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_simple.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_string_unicode.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_object_with_newlines.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_1_2_3_bytes_UTF-8_sequences.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_UTF-16_Surrogates_U+1D11E_MUSICAL_SYMBOL_G_CLEF.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pair.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_accepted_surrogate_pairs.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_allowed_escapes.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_backslash_and_u_escaped_zero.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_backslash_doublequotes.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_comments.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_double_escape_a.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_double_escape_n.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_escaped_control_character.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_escaped_noncharacter.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_in_array.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_in_array_with_leading_space.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_last_surrogates_1_and_2.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_newline_uescaped.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+10FFFF.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+1FFFF.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_nonCharacterInUTF-8_U+FFFF.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_null_escape.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_one-byte-utf-8.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_pi.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_simple_ascii.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_space.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_three-byte-utf-8.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_two-byte-utf-8.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_u+2028_line_sep.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_u+2029_par_sep.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_uEscape.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_unescaped_char_delete.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_unicode.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_unicodeEscapedBackslash.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_unicode_2.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_unicode_U+200B_ZERO_WIDTH_SPACE.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_unicode_U+2064_invisible_plus.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_unicode_escaped_double_quote.json",
|
|
|
|
// "test/data/nst_json_testsuite/test_parsing/y_string_utf16.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_utf8.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_string_with_del_character.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_false.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_int.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_negative_real.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_null.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_string.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_lonely_true.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_string_empty.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_trailing_newline.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_true_in_array.json",
|
|
|
|
"test/data/nst_json_testsuite/test_parsing/y_structure_whitespace_array.json"
|
|
|
|
})
|
|
|
|
{
|
|
|
|
CAPTURE(filename);
|
|
|
|
|
|
|
|
// parse JSON file
|
|
|
|
std::ifstream f_json(filename);
|
|
|
|
json j1 = json::parse(f_json);
|
|
|
|
|
2017-07-23 21:02:24 +00:00
|
|
|
SECTION("std::vector<uint8_t>")
|
|
|
|
{
|
|
|
|
// parse MessagePack file
|
|
|
|
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
|
|
|
|
std::vector<uint8_t> packed(
|
|
|
|
(std::istreambuf_iterator<char>(f_msgpack)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
json j2;
|
|
|
|
CHECK_NOTHROW(j2 = json::from_msgpack(packed));
|
|
|
|
|
|
|
|
// compare parsed JSON values
|
|
|
|
CHECK(j1 == j2);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("std::ifstream")
|
|
|
|
{
|
|
|
|
// parse MessagePack file
|
|
|
|
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
|
|
|
|
json j2;
|
|
|
|
CHECK_NOTHROW(j2 = json::from_msgpack(f_msgpack));
|
|
|
|
|
|
|
|
// compare parsed JSON values
|
|
|
|
CHECK(j1 == j2);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("uint8_t* and size")
|
|
|
|
{
|
|
|
|
// parse MessagePack file
|
|
|
|
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
|
|
|
|
std::vector<uint8_t> packed(
|
|
|
|
(std::istreambuf_iterator<char>(f_msgpack)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
json j2;
|
|
|
|
CHECK_NOTHROW(j2 = json::from_msgpack({packed.data(), packed.size()}));
|
|
|
|
|
|
|
|
// compare parsed JSON values
|
|
|
|
CHECK(j1 == j2);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("output to output adapters")
|
|
|
|
{
|
|
|
|
// parse MessagePack file
|
|
|
|
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
|
|
|
|
std::vector<uint8_t> packed(
|
|
|
|
(std::istreambuf_iterator<char>(f_msgpack)),
|
|
|
|
std::istreambuf_iterator<char>());
|
|
|
|
|
|
|
|
SECTION("std::vector<uint8_t>")
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> vec;
|
|
|
|
json::to_msgpack(j1, vec);
|
|
|
|
CHECK(vec == packed);
|
|
|
|
}
|
|
|
|
}
|
2016-11-11 16:11:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|