Commit ee0f23fd authored by Niels Lohmann's avatar Niels Lohmann

🚧 bug fixes and more tests

parent bc238124
...@@ -6490,8 +6490,8 @@ class basic_json ...@@ -6490,8 +6490,8 @@ class basic_json
v.push_back(0x3b); v.push_back(0x3b);
add_to_vector(v, 8, positive_number); add_to_vector(v, 8, positive_number);
} }
break;
} }
break;
} }
case value_t::number_unsigned: case value_t::number_unsigned:
......
...@@ -6490,8 +6490,8 @@ class basic_json ...@@ -6490,8 +6490,8 @@ class basic_json
v.push_back(0x3b); v.push_back(0x3b);
add_to_vector(v, 8, positive_number); add_to_vector(v, 8, positive_number);
} }
break;
} }
break;
} }
case value_t::number_unsigned: case value_t::number_unsigned:
......
...@@ -77,4 +77,4 @@ test-%: src/unit-%.cpp ../src/json.hpp src/catch.hpp ...@@ -77,4 +77,4 @@ test-%: src/unit-%.cpp ../src/json.hpp src/catch.hpp
TEST_PATTERN = "*" TEST_PATTERN = "*"
TEST_PREFIX = "" TEST_PREFIX = ""
check: $(TESTCASES) check: $(TESTCASES)
@cd .. ; for testcase in $(TESTCASES); do echo "Executing $$testcase..."; $(TEST_PREFIX)test/$$testcase $(TEST_PATTERN); done @cd .. ; for testcase in $(TESTCASES); do echo "Executing $$testcase..."; $(TEST_PREFIX)test/$$testcase $(TEST_PATTERN) || exit 1; done
...@@ -77,9 +77,24 @@ TEST_CASE("CBOR") ...@@ -77,9 +77,24 @@ TEST_CASE("CBOR")
{ {
SECTION("signed") SECTION("signed")
{ {
SECTION("-24..-1") SECTION("-9263 (int 16)")
{ {
for (auto i = -24; i <= -1; ++i) json j = -9263;
std::vector<uint8_t> expected = {0x39, 0x24, 0x2e};
const auto result = json::to_cbor(j);
CHECK(result == expected);
int16_t restored = -1 - ((result[1] << 8) + result[2]);
CHECK(restored == -9263);
// roundtrip
CHECK(json::from_cbor(result) == j);
}
SECTION("-256..-24")
{
for (auto i = -256; i < -24; ++i)
{ {
CAPTURE(i); CAPTURE(i);
...@@ -91,98 +106,113 @@ TEST_CASE("CBOR") ...@@ -91,98 +106,113 @@ TEST_CASE("CBOR")
// create expected byte vector // create expected byte vector
std::vector<uint8_t> expected; std::vector<uint8_t> expected;
expected.push_back(0x20 - 1 - static_cast<uint8_t>(i)); expected.push_back(0x38);
expected.push_back(static_cast<uint8_t>(-1 - i));
// compare result + size // compare result + size
const auto result = json::to_cbor(j); const auto result = json::to_cbor(j);
CHECK(result == expected); CHECK(result == expected);
CHECK(result.size() == 1); CHECK(result.size() == 2);
// check individual bytes // check individual bytes
CHECK(static_cast<int8_t>(0x20 - 1 - result[0]) == i); CHECK(result[0] == 0x38);
CHECK(static_cast<int16_t>(-1 - result[1]) == i);
// roundtrip // roundtrip
CHECK(json::from_cbor(result) == j); CHECK(json::from_cbor(result) == j);
} }
} }
/* SECTION("-24..-1")
SECTION("0..127 (positive fixnum)")
{ {
for (size_t i = 0; i <= 255; ++i) for (auto i = -24; i <= -1; ++i)
{ {
CAPTURE(i); CAPTURE(i);
// create JSON value with integer number // create JSON value with integer number
json j = -1; json j = i;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
// check type // check type
CHECK(j.is_number_integer()); CHECK(j.is_number_integer());
// create expected byte vector // create expected byte vector
std::vector<uint8_t> expected; std::vector<uint8_t> expected;
expected.push_back(static_cast<uint8_t>(i)); expected.push_back(0x20 - 1 - static_cast<uint8_t>(i));
// compare result + size // compare result + size
const auto result = json::to_msgpack(j); const auto result = json::to_cbor(j);
CHECK(result == expected); CHECK(result == expected);
CHECK(result.size() == 1); CHECK(result.size() == 1);
// check individual bytes // check individual bytes
CHECK(result[0] == i); CHECK(static_cast<int8_t>(0x20 - 1 - result[0]) == i);
// roundtrip // roundtrip
CHECK(json::from_msgpack(result) == j); CHECK(json::from_cbor(result) == j);
} }
} }
*/
SECTION("-256..-24") SECTION("0..22")
{ {
for (auto i = -256; i < -24; ++i) for (size_t i = 0; i <= 22; ++i)
{ {
CAPTURE(i); CAPTURE(i);
// create JSON value with integer number // create JSON value with integer number
json j = i; json j = -1;
j.get_ref<json::number_integer_t&>() = static_cast<json::number_integer_t>(i);
// check type // check type
CHECK(j.is_number_integer()); CHECK(j.is_number_integer());
// create expected byte vector // create expected byte vector
std::vector<uint8_t> expected; std::vector<uint8_t> expected;
expected.push_back(0x38); expected.push_back(static_cast<uint8_t>(i));
expected.push_back(static_cast<uint8_t>(-1 - i));
// compare result + size // compare result + size
const auto result = json::to_cbor(j); const auto result = json::to_cbor(j);
CHECK(result == expected); CHECK(result == expected);
CHECK(result.size() == 2); CHECK(result.size() == 1);
// check individual bytes // check individual bytes
CHECK(result[0] == 0x38); CHECK(result[0] == i);
CHECK(static_cast<int16_t>(-1 - result[1]) == i);
// roundtrip // roundtrip
CHECK(json::from_cbor(result) == j); CHECK(json::from_cbor(result) == j);
} }
} }
SECTION("-9263 (int 16)") SECTION("23..255")
{ {
json j = -9263; for (size_t i = 23; i <= 255; ++i)
std::vector<uint8_t> expected = {0x39, 0x24, 0x2e}; {
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>(0x18));
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_cbor(j); const auto result = json::to_cbor(j);
CHECK(result == expected); CHECK(result == expected);
CHECK(result.size() == 2);
int16_t restored = -1 - ((result[1] << 8) + result[2]); // check individual bytes
CHECK(restored == -9263); CHECK(result[0] == 0x18);
CHECK(result[1] == i);
// roundtrip // roundtrip
CHECK(json::from_cbor(result) == j); CHECK(json::from_cbor(result) == j);
} }
}
/* /*
SECTION("-32768..-129 (int 16)") SECTION("-32768..-129 (int 16)")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment