🚨 fix implicit conversion warning when opening binary file
This commit is contained in:
parent
fb22233f34
commit
e571c67c0d
7 changed files with 52 additions and 70 deletions
2
Makefile
2
Makefile
|
@ -525,7 +525,7 @@ pretty:
|
||||||
--preserve-date \
|
--preserve-date \
|
||||||
--suffix=none \
|
--suffix=none \
|
||||||
--formatted \
|
--formatted \
|
||||||
$(SRCS) $(AMALGAMATED_FILE) test/src/*.cpp benchmarks/src/benchmarks.cpp doc/examples/*.cpp
|
$(SRCS) $(AMALGAMATED_FILE) test/src/*.cpp test/utils/*.hpp benchmarks/src/benchmarks.cpp doc/examples/*.cpp
|
||||||
|
|
||||||
# create single header file
|
# create single header file
|
||||||
amalgamate: $(AMALGAMATED_FILE)
|
amalgamate: $(AMALGAMATED_FILE)
|
||||||
|
|
|
@ -151,7 +151,7 @@ foreach(file ${files})
|
||||||
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal>
|
$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal>
|
||||||
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
|
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
|
||||||
)
|
)
|
||||||
target_include_directories(${testcase} PRIVATE ${CMAKE_BINARY_DIR}/include thirdparty/doctest thirdparty/fifo_map)
|
target_include_directories(${testcase} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/utils ${CMAKE_BINARY_DIR}/include thirdparty/doctest thirdparty/fifo_map)
|
||||||
target_link_libraries(${testcase} PRIVATE ${NLOHMANN_JSON_TARGET_NAME})
|
target_link_libraries(${testcase} PRIVATE ${NLOHMANN_JSON_TARGET_NAME})
|
||||||
|
|
||||||
if (JSON_Coverage)
|
if (JSON_Coverage)
|
||||||
|
|
|
@ -35,6 +35,7 @@ using nlohmann::json;
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <test_data.hpp>
|
#include <test_data.hpp>
|
||||||
|
#include <test_utils.hpp>
|
||||||
|
|
||||||
TEST_CASE("BSON")
|
TEST_CASE("BSON")
|
||||||
{
|
{
|
||||||
|
@ -1263,10 +1264,7 @@ TEST_CASE("BSON roundtrips" * doctest::skip())
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse BSON file
|
// parse BSON file
|
||||||
std::ifstream f_bson(filename + ".bson", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".bson");
|
||||||
std::vector<std::uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_bson)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_bson(packed));
|
CHECK_NOTHROW(j2 = json::from_bson(packed));
|
||||||
|
|
||||||
|
@ -1296,10 +1294,7 @@ TEST_CASE("BSON roundtrips" * doctest::skip())
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse BSON file
|
// parse BSON file
|
||||||
std::ifstream f_bson(filename + ".bson", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".bson");
|
||||||
std::vector<std::uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_bson)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_bson({packed.data(), packed.size()}));
|
CHECK_NOTHROW(j2 = json::from_bson({packed.data(), packed.size()}));
|
||||||
|
|
||||||
|
@ -1314,10 +1309,7 @@ TEST_CASE("BSON roundtrips" * doctest::skip())
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse BSON file
|
// parse BSON file
|
||||||
std::ifstream f_bson(filename + ".bson", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".bson");
|
||||||
std::vector<std::uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_bson)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
|
|
||||||
{
|
{
|
||||||
INFO_WITH_TEMP(filename + ": output adapters: std::vector<std::uint8_t>");
|
INFO_WITH_TEMP(filename + ": output adapters: std::vector<std::uint8_t>");
|
||||||
|
|
|
@ -39,6 +39,7 @@ using nlohmann::json;
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <test_data.hpp>
|
#include <test_data.hpp>
|
||||||
|
#include <test_utils.hpp>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
@ -1920,9 +1921,7 @@ TEST_CASE("single CBOR roundtrip")
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse CBOR file
|
// parse CBOR file
|
||||||
std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".cbor");
|
||||||
std::vector<uint8_t> packed((std::istreambuf_iterator<char>(f_cbor)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_cbor(packed));
|
CHECK_NOTHROW(j2 = json::from_cbor(packed));
|
||||||
|
|
||||||
|
@ -1994,10 +1993,7 @@ TEST_CASE("CBOR regressions")
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// parse CBOR file
|
// parse CBOR file
|
||||||
std::ifstream f_cbor(filename, std::ios::binary);
|
auto vec1 = utils::read_binary_file(filename);
|
||||||
std::vector<uint8_t> vec1(
|
|
||||||
(std::istreambuf_iterator<char>(f_cbor)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j1 = json::from_cbor(vec1);
|
json j1 = json::from_cbor(vec1);
|
||||||
|
|
||||||
try
|
try
|
||||||
|
@ -2204,10 +2200,7 @@ TEST_CASE("CBOR roundtrips" * doctest::skip())
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse CBOR file
|
// parse CBOR file
|
||||||
std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".cbor");
|
||||||
std::vector<uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_cbor)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_cbor(packed));
|
CHECK_NOTHROW(j2 = json::from_cbor(packed));
|
||||||
|
|
||||||
|
@ -2237,10 +2230,7 @@ TEST_CASE("CBOR roundtrips" * doctest::skip())
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse CBOR file
|
// parse CBOR file
|
||||||
std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".cbor");
|
||||||
std::vector<uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_cbor)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_cbor({packed.data(), packed.size()}));
|
CHECK_NOTHROW(j2 = json::from_cbor({packed.data(), packed.size()}));
|
||||||
|
|
||||||
|
@ -2255,10 +2245,7 @@ TEST_CASE("CBOR roundtrips" * doctest::skip())
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse CBOR file
|
// parse CBOR file
|
||||||
std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".cbor");
|
||||||
std::vector<uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_cbor)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
|
|
||||||
if (!exclude_packed.count(filename))
|
if (!exclude_packed.count(filename))
|
||||||
{
|
{
|
||||||
|
@ -2493,15 +2480,11 @@ TEST_CASE("examples from RFC 7049 Appendix A")
|
||||||
|
|
||||||
SECTION("byte arrays")
|
SECTION("byte arrays")
|
||||||
{
|
{
|
||||||
std::ifstream f_cbor(TEST_DATA_DIRECTORY "/binary_data/cbor_binary.cbor", std::ios::binary);
|
auto packed = utils::read_binary_file(TEST_DATA_DIRECTORY "/binary_data/cbor_binary.cbor");
|
||||||
std::vector<uint8_t> packed((std::istreambuf_iterator<char>(f_cbor)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j;
|
json j;
|
||||||
CHECK_NOTHROW(j = json::from_cbor(packed));
|
CHECK_NOTHROW(j = json::from_cbor(packed));
|
||||||
|
|
||||||
std::ifstream f_bin(TEST_DATA_DIRECTORY "/binary_data/cbor_binary.out", std::ios::binary);
|
auto expected = utils::read_binary_file(TEST_DATA_DIRECTORY "/binary_data/cbor_binary.out");
|
||||||
std::vector<uint8_t> expected((std::istreambuf_iterator<char>(f_bin)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
CHECK(j == json::binary(expected));
|
CHECK(j == json::binary(expected));
|
||||||
|
|
||||||
CHECK(json::to_cbor(json::binary(std::vector<uint8_t> {}, 0x42)) == std::vector<uint8_t> {0xd8, 0x42, 0x40});
|
CHECK(json::to_cbor(json::binary(std::vector<uint8_t> {}, 0x42)) == std::vector<uint8_t> {0xd8, 0x42, 0x40});
|
||||||
|
|
|
@ -37,6 +37,7 @@ using nlohmann::json;
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <test_data.hpp>
|
#include <test_data.hpp>
|
||||||
|
#include <test_utils.hpp>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
@ -1609,9 +1610,7 @@ TEST_CASE("single MessagePack roundtrip")
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse MessagePack file
|
// parse MessagePack file
|
||||||
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".msgpack");
|
||||||
std::vector<uint8_t> packed((std::istreambuf_iterator<char>(f_msgpack)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_msgpack(packed));
|
CHECK_NOTHROW(j2 = json::from_msgpack(packed));
|
||||||
|
|
||||||
|
@ -1824,10 +1823,7 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip())
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse MessagePack file
|
// parse MessagePack file
|
||||||
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".msgpack");
|
||||||
std::vector<uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_msgpack)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_msgpack(packed));
|
CHECK_NOTHROW(j2 = json::from_msgpack(packed));
|
||||||
|
|
||||||
|
@ -1857,10 +1853,7 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip())
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse MessagePack file
|
// parse MessagePack file
|
||||||
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".msgpack");
|
||||||
std::vector<uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_msgpack)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_msgpack({packed.data(), packed.size()}));
|
CHECK_NOTHROW(j2 = json::from_msgpack({packed.data(), packed.size()}));
|
||||||
|
|
||||||
|
@ -1875,10 +1868,7 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip())
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse MessagePack file
|
// parse MessagePack file
|
||||||
std::ifstream f_msgpack(filename + ".msgpack", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".msgpack");
|
||||||
std::vector<uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_msgpack)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
|
|
||||||
if (!exclude_packed.count(filename))
|
if (!exclude_packed.count(filename))
|
||||||
{
|
{
|
||||||
|
|
|
@ -35,6 +35,7 @@ using nlohmann::json;
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <test_data.hpp>
|
#include <test_data.hpp>
|
||||||
|
#include <test_utils.hpp>
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
{
|
{
|
||||||
|
@ -2457,11 +2458,8 @@ TEST_CASE("UBJSON roundtrips" * doctest::skip())
|
||||||
std::ifstream f_json(filename);
|
std::ifstream f_json(filename);
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse MessagePack file
|
// parse UBJSON file
|
||||||
std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".ubjson");
|
||||||
std::vector<uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_ubjson)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_ubjson(packed));
|
CHECK_NOTHROW(j2 = json::from_ubjson(packed));
|
||||||
|
|
||||||
|
@ -2475,7 +2473,7 @@ TEST_CASE("UBJSON roundtrips" * doctest::skip())
|
||||||
std::ifstream f_json(filename);
|
std::ifstream f_json(filename);
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse MessagePack file
|
// parse UBJSON file
|
||||||
std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary);
|
std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary);
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_ubjson(f_ubjson));
|
CHECK_NOTHROW(j2 = json::from_ubjson(f_ubjson));
|
||||||
|
@ -2490,11 +2488,8 @@ TEST_CASE("UBJSON roundtrips" * doctest::skip())
|
||||||
std::ifstream f_json(filename);
|
std::ifstream f_json(filename);
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse MessagePack file
|
// parse UBJSON file
|
||||||
std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".ubjson");
|
||||||
std::vector<uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_ubjson)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
json j2;
|
json j2;
|
||||||
CHECK_NOTHROW(j2 = json::from_ubjson({packed.data(), packed.size()}));
|
CHECK_NOTHROW(j2 = json::from_ubjson({packed.data(), packed.size()}));
|
||||||
|
|
||||||
|
@ -2508,11 +2503,8 @@ TEST_CASE("UBJSON roundtrips" * doctest::skip())
|
||||||
std::ifstream f_json(filename);
|
std::ifstream f_json(filename);
|
||||||
json j1 = json::parse(f_json);
|
json j1 = json::parse(f_json);
|
||||||
|
|
||||||
// parse MessagePack file
|
// parse UBJSON file
|
||||||
std::ifstream f_ubjson(filename + ".ubjson", std::ios::binary);
|
auto packed = utils::read_binary_file(filename + ".ubjson");
|
||||||
std::vector<uint8_t> packed(
|
|
||||||
(std::istreambuf_iterator<char>(f_ubjson)),
|
|
||||||
std::istreambuf_iterator<char>());
|
|
||||||
|
|
||||||
{
|
{
|
||||||
INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
|
INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
|
||||||
|
|
25
test/utils/test_utils.hpp
Normal file
25
test/utils/test_utils.hpp
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint> // uint8_t
|
||||||
|
#include <fstream> // ifstream, istreambuf_iterator, ios
|
||||||
|
#include <vector> // vector
|
||||||
|
|
||||||
|
namespace utils
|
||||||
|
{
|
||||||
|
|
||||||
|
inline std::vector<std::uint8_t> read_binary_file(const std::string& filename)
|
||||||
|
{
|
||||||
|
std::ifstream file(filename, std::ios::binary);
|
||||||
|
file.unsetf(std::ios::skipws);
|
||||||
|
|
||||||
|
file.seekg(0, std::ios::end);
|
||||||
|
const auto size = file.tellg();
|
||||||
|
file.seekg(0, std::ios::beg);
|
||||||
|
|
||||||
|
std::vector<std::uint8_t> byte_vector;
|
||||||
|
byte_vector.reserve(static_cast<std::size_t>(size));
|
||||||
|
byte_vector.insert(byte_vector.begin(), std::istream_iterator<std::uint8_t>(file), std::istream_iterator<std::uint8_t>());
|
||||||
|
return byte_vector;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace utils
|
Loading…
Reference in a new issue