Skip to content

Commit 8ce64b9

Browse files
committed
Move the custom BinaryType tests into their own translation unit
The two sections added to unit-regression2.cpp brought a third full basic_json instantiation into a translation unit that was already large. With Clang on MinGW that pushed the object over the reach of a 32-bit relocation and test-regression2_cpp20.exe failed to link: relocation truncated to fit: IMAGE_REL_AMD64_REL32 against `.rdata' unit-regression2.cpp is restored to exactly what it was before, and the coverage moves to unit-custom-binary-type.cpp, next to the object and array type tests it belongs with. The signed value type is now also covered in C++11, where std::byte is not available. Signed-off-by: Niels Lohmann <mail@nlohmann.me>
1 parent 3b28316 commit 8ce64b9

2 files changed

Lines changed: 79 additions & 33 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// __ _____ _____ _____
2+
// __| | __| | | | JSON for Modern C++ (supporting code)
3+
// | | |__ | | | | | | version 3.12.0
4+
// |_____|_____|_____|_|___| https://github.com/nlohmann/json
5+
//
6+
// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann <https://nlohmann.me>
7+
// SPDX-License-Identifier: MIT
8+
9+
#include "doctest_compatibility.h"
10+
11+
#include <nlohmann/json.hpp>
12+
13+
#include <cstdint>
14+
#include <functional>
15+
#include <map>
16+
#include <memory>
17+
#include <string>
18+
#include <vector>
19+
20+
#ifdef JSON_HAS_CPP_17
21+
#include <cstddef>
22+
#endif
23+
24+
namespace
25+
{
26+
27+
// a BinaryType whose value type is signed: the elements must still be
28+
// processed as the numbers 0..255
29+
using char_binary_json = nlohmann::basic_json <
30+
std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t,
31+
double, std::allocator, nlohmann::adl_serializer, std::vector<char>, void >;
32+
33+
#ifdef JSON_HAS_CPP_17
34+
// a BinaryType whose value type is not an integer type at all
35+
using byte_binary_json = nlohmann::basic_json <
36+
std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t,
37+
double, std::allocator, nlohmann::adl_serializer, std::vector<std::byte>, void >;
38+
#endif
39+
40+
} // namespace
41+
42+
TEST_CASE("binary type whose value type is not std::uint8_t")
43+
{
44+
SECTION("a signed value type does not dump negative numbers")
45+
{
46+
const std::vector<char> chars{'\0', '\x01', '\xFF'};
47+
CHECK(char_binary_json::binary(chars).dump() == R"({"bytes":[0,1,255],"subtype":null})");
48+
CHECK(char_binary_json::binary(chars, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
49+
CHECK(char_binary_json::binary({}).dump() == R"({"bytes":[],"subtype":null})");
50+
}
51+
52+
SECTION("the default binary type is unchanged")
53+
{
54+
CHECK(nlohmann::json::binary({0, 1, 255}, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
55+
}
56+
57+
#ifdef JSON_HAS_CPP_17
58+
SECTION("dumping a value type that is not an integer")
59+
{
60+
const std::vector<std::byte> bytes{std::byte{0}, std::byte{1}, std::byte{0xFF}};
61+
CHECK(byte_binary_json::binary(bytes).dump() == R"({"bytes":[0,1,255],"subtype":null})");
62+
CHECK(byte_binary_json::binary(bytes, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
63+
CHECK(byte_binary_json::binary({}).dump() == R"({"bytes":[],"subtype":null})");
64+
}
65+
66+
SECTION("hashing and the binary formats")
67+
{
68+
const std::vector<std::byte> bytes{std::byte{0}, std::byte{1}, std::byte{0xFF}};
69+
const auto j = byte_binary_json::binary(bytes);
70+
71+
CHECK(std::hash<byte_binary_json> {}(j) == std::hash<byte_binary_json> {}(j));
72+
CHECK(byte_binary_json::from_cbor(byte_binary_json::to_cbor(j)) == j);
73+
CHECK(byte_binary_json::from_msgpack(byte_binary_json::to_msgpack(j)) == j);
74+
75+
// UBJSON has no binary type, so binary values are written as an array
76+
CHECK(byte_binary_json::from_ubjson(byte_binary_json::to_ubjson(j)) == byte_binary_json({0, 1, 255}));
77+
}
78+
#endif
79+
}

tests/src/unit-regression2.cpp

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,39 +1154,6 @@ TEST_CASE("regression tests 2")
11541154
CHECK(!default_json.is_binary());
11551155
}
11561156

1157-
SECTION("dumping a binary value with a custom BinaryType")
1158-
{
1159-
// the elements of a binary value are dumped as the numbers 0..255,
1160-
// whatever the value type of the configured BinaryType is
1161-
const std::vector<std::byte> bytes{std::byte{0}, std::byte{1}, std::byte{0xFF}};
1162-
CHECK(json_4804::binary(bytes).dump() == R"({"bytes":[0,1,255],"subtype":null})");
1163-
CHECK(json_4804::binary(bytes, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
1164-
CHECK(json_4804::binary({}).dump() == R"({"bytes":[],"subtype":null})");
1165-
1166-
// a signed byte type must not dump negative numbers
1167-
using json_char_binary = nlohmann::basic_json <
1168-
std::map, std::vector, std::string, bool, std::int64_t, std::uint64_t,
1169-
double, std::allocator, nlohmann::adl_serializer, std::vector<char>, void >;
1170-
const std::vector<char> chars{'\0', '\x01', '\xFF'};
1171-
CHECK(json_char_binary::binary(chars).dump() == R"({"bytes":[0,1,255],"subtype":null})");
1172-
1173-
// the default binary type is unchanged
1174-
CHECK(json::binary({0, 1, 255}, 42).dump() == R"({"bytes":[0,1,255],"subtype":42})");
1175-
}
1176-
1177-
SECTION("hashing and UBJSON with a custom BinaryType")
1178-
{
1179-
const std::vector<std::byte> bytes{std::byte{0}, std::byte{1}, std::byte{0xFF}};
1180-
const auto j = json_4804::binary(bytes);
1181-
1182-
CHECK(std::hash<json_4804> {}(j) == std::hash<json_4804> {}(j));
1183-
CHECK(json_4804::from_cbor(json_4804::to_cbor(j)) == j);
1184-
CHECK(json_4804::from_msgpack(json_4804::to_msgpack(j)) == j);
1185-
1186-
// UBJSON has no binary type, so binary values are written as arrays
1187-
CHECK(json_4804::from_ubjson(json_4804::to_ubjson(j)) == json_4804({0, 1, 255}));
1188-
}
1189-
11901157
SECTION("discussion #4209 - custom BinaryType extraction from parsed array")
11911158
{
11921159
// Test that extracting a custom BinaryType from a parsed JSON array still works

0 commit comments

Comments
 (0)