🚧 bug fixes and more tests

This commit is contained in:
Niels Lohmann 2016-11-28 23:20:03 +01:00
parent bc238124ee
commit ee0f23fdc6
4 changed files with 98 additions and 68 deletions

View file

@ -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:

View file

@ -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:

View file

@ -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

View file

@ -77,66 +77,20 @@ 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};
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(0x20 - 1 - 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() == 1);
// check individual bytes int16_t restored = -1 - ((result[1] << 8) + result[2]);
CHECK(static_cast<int8_t>(0x20 - 1 - result[0]) == i); CHECK(restored == -9263);
// roundtrip // roundtrip
CHECK(json::from_cbor(result) == j); CHECK(json::from_cbor(result) == j);
} }
}
/*
SECTION("0..127 (positive fixnum)")
{
for (size_t i = 0; 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(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("-256..-24") SECTION("-256..-24")
{ {
@ -169,20 +123,96 @@ TEST_CASE("CBOR")
} }
} }
SECTION("-9263 (int 16)") SECTION("-24..-1")
{ {
json j = -9263; for (auto i = -24; i <= -1; ++i)
std::vector<uint8_t> expected = {0x39, 0x24, 0x2e}; {
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(0x20 - 1 - 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() == 1);
int16_t restored = -1 - ((result[1] << 8) + result[2]); // check individual bytes
CHECK(restored == -9263); CHECK(static_cast<int8_t>(0x20 - 1 - result[0]) == i);
// roundtrip // roundtrip
CHECK(json::from_cbor(result) == j); CHECK(json::from_cbor(result) == j);
} }
}
SECTION("0..22")
{
for (size_t i = 0; i <= 22; ++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_cbor(j);
CHECK(result == expected);
CHECK(result.size() == 1);
// check individual bytes
CHECK(result[0] == i);
// roundtrip
CHECK(json::from_cbor(result) == j);
}
}
SECTION("23..255")
{
for (size_t i = 23; 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(static_cast<uint8_t>(0x18));
expected.push_back(static_cast<uint8_t>(i));
// compare result + size
const auto result = json::to_cbor(j);
CHECK(result == expected);
CHECK(result.size() == 2);
// check individual bytes
CHECK(result[0] == 0x18);
CHECK(result[1] == i);
// roundtrip
CHECK(json::from_cbor(result) == j);
}
}
/* /*
SECTION("-32768..-129 (int 16)") SECTION("-32768..-129 (int 16)")