Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions tests/src/test_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#pragma once

#include <cstdint> // uint8_t
#include <cstddef> // size_t
#include <fstream> // ifstream, istreambuf_iterator, ios
#include <vector> // vector

Expand All @@ -24,6 +25,23 @@ namespace utils
template<typename T>
inline void ignore_return_value(T&& /*unused*/) noexcept {}

// Advance i toward last (inclusive) by stride, always visiting last.
// stride 7 is coprime to 256, so every low-byte residue is still hit.
template<typename T>
T next_integer_sample(T i, T last, T stride)
{
if (i >= last)
{
return static_cast<T>(last + 1);
}
if (stride > 0 && i > static_cast<T>(last - stride))
{
return last;
}
const T n = static_cast<T>(i + stride);
return n < last ? n : last;
}

inline std::vector<std::uint8_t> read_binary_file(const std::string& filename)
{
std::ifstream file(filename, std::ios::binary);
Expand Down
32 changes: 7 additions & 25 deletions tests/src/unit-bjdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ TEST_CASE("BJData")

SECTION("-32768..-129 (int16)")
{
for (int32_t i = -32768; i <= -129; ++i)
for (int32_t i = -32768; i <= -129; i = utils::next_integer_sample(i, -129, 7))
{
CAPTURE(i)

Expand Down Expand Up @@ -577,7 +577,7 @@ TEST_CASE("BJData")

SECTION("256..32767 (int16)")
{
for (size_t i = 256; i <= 32767; ++i)
for (size_t i = 256; i <= 32767; i = utils::next_integer_sample(i, static_cast<size_t>(32767), static_cast<size_t>(7)))
{
CAPTURE(i)

Expand Down Expand Up @@ -910,7 +910,7 @@ TEST_CASE("BJData")

SECTION("256..32767 (int16)")
{
for (size_t i = 256; i <= 32767; ++i)
for (size_t i = 256; i <= 32767; i = utils::next_integer_sample(i, static_cast<size_t>(32767), static_cast<size_t>(7)))
{
CAPTURE(i)

Expand Down Expand Up @@ -4251,45 +4251,27 @@ TEST_CASE("BJData roundtrips" * doctest::skip())
{
CAPTURE(filename)

std::ifstream f_json(filename);
const json j1 = json::parse(f_json);
auto packed = utils::read_binary_file(filename + ".bjdata");

{
INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
// parse JSON file
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);

// parse BJData file
auto packed = utils::read_binary_file(filename + ".bjdata");
json j2;
CHECK_NOTHROW(j2 = json::from_bjdata(packed));

// compare parsed JSON values
CHECK(j1 == j2);
}

{
INFO_WITH_TEMP(filename + ": std::ifstream");
// parse JSON file
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);

// parse BJData file
std::ifstream f_bjdata(filename + ".bjdata", std::ios::binary);
json j2;
CHECK_NOTHROW(j2 = json::from_bjdata(f_bjdata));

// compare parsed JSON values
CHECK(j1 == j2);
}

{
INFO_WITH_TEMP(filename + ": output to output adapters");
// parse JSON file
std::ifstream f_json(filename);
json const j1 = json::parse(f_json);

// parse BJData file
auto packed = utils::read_binary_file(filename + ".bjdata");

{
INFO_WITH_TEMP(filename + ": output adapters: std::vector<uint8_t>");
std::vector<uint8_t> vec;
Expand Down
42 changes: 8 additions & 34 deletions tests/src/unit-cbor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ TEST_CASE("CBOR")

SECTION("-65536..-257")
{
for (int32_t i = -65536; i <= -257; ++i)
for (int32_t i = -65536; i <= -257; i = utils::next_integer_sample(i, -257, 7))
{
CAPTURE(i)

Expand Down Expand Up @@ -478,7 +478,7 @@ TEST_CASE("CBOR")

SECTION("256..65535")
{
for (size_t i = 256; i <= 65535; ++i)
for (size_t i = 256; i <= 65535; i = utils::next_integer_sample(i, static_cast<size_t>(65535), static_cast<size_t>(7)))
{
CAPTURE(i)

Expand Down Expand Up @@ -613,7 +613,7 @@ TEST_CASE("CBOR")

SECTION("-32768..-129 (int 16)")
{
for (int16_t i = -32768; i <= static_cast<std::int16_t>(-129); ++i)
for (int16_t i = -32768; i <= static_cast<std::int16_t>(-129); i = utils::next_integer_sample(i, static_cast<int16_t>(-129), static_cast<int16_t>(7)))
{
CAPTURE(i)

Expand Down Expand Up @@ -718,7 +718,7 @@ TEST_CASE("CBOR")

SECTION("256..65535 (two-byte uint16_t)")
{
for (size_t i = 256; i <= 65535; ++i)
for (size_t i = 256; i <= 65535; i = utils::next_integer_sample(i, static_cast<size_t>(65535), static_cast<size_t>(7)))
{
CAPTURE(i)

Expand Down Expand Up @@ -2429,60 +2429,34 @@ TEST_CASE("CBOR roundtrips" * doctest::skip())
{
CAPTURE(filename)

std::ifstream f_json(filename);
const json j1 = json::parse(f_json);
const auto packed = utils::read_binary_file(filename + ".cbor");

{
INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
// parse JSON file
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);

// parse CBOR file
const auto packed = utils::read_binary_file(filename + ".cbor");
json j2;
CHECK_NOTHROW(j2 = json::from_cbor(packed));

// compare parsed JSON values
CHECK(j1 == j2);
}

{
INFO_WITH_TEMP(filename + ": std::ifstream");
// parse JSON file
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);

// parse CBOR file
std::ifstream f_cbor(filename + ".cbor", std::ios::binary);
json j2;
CHECK_NOTHROW(j2 = json::from_cbor(f_cbor));

// compare parsed JSON values
CHECK(j1 == j2);
}

{
INFO_WITH_TEMP(filename + ": uint8_t* and size");
// parse JSON file
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);

// parse CBOR file
const auto packed = utils::read_binary_file(filename + ".cbor");
json j2;
CHECK_NOTHROW(j2 = json::from_cbor({packed.data(), packed.size()}));

// compare parsed JSON values
CHECK(j1 == j2);
}

{
INFO_WITH_TEMP(filename + ": output to output adapters");
// parse JSON file
std::ifstream f_json(filename);
json const j1 = json::parse(f_json);

// parse CBOR file
const auto packed = utils::read_binary_file(filename + ".cbor");

if (exclude_packed.count(filename) == 0u)
{
{
Expand Down
2 changes: 1 addition & 1 deletion tests/src/unit-large_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ TEST_CASE("tests on very large JSONs")
{
SECTION("issue #1419 - Segmentation fault (stack overflow) due to unbounded recursion")
{
const auto depth = 5000000;
const auto depth = 500000;

std::string s(static_cast<std::size_t>(2 * depth), '[');
std::fill(s.begin() + depth, s.end(), ']');
Expand Down
40 changes: 7 additions & 33 deletions tests/src/unit-msgpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ TEST_CASE("MessagePack")

SECTION("256..65535 (int 16)")
{
for (size_t i = 256; i <= 65535; ++i)
for (size_t i = 256; i <= 65535; i = utils::next_integer_sample(i, static_cast<size_t>(65535), static_cast<size_t>(7)))
{
CAPTURE(i)

Expand Down Expand Up @@ -440,7 +440,7 @@ TEST_CASE("MessagePack")

SECTION("-32768..-129 (int 16)")
{
for (int16_t i = -32768; i <= static_cast<std::int16_t>(-129); ++i)
for (int16_t i = -32768; i <= static_cast<std::int16_t>(-129); i = utils::next_integer_sample(i, static_cast<int16_t>(-129), static_cast<int16_t>(7)))
{
CAPTURE(i)

Expand Down Expand Up @@ -646,7 +646,7 @@ TEST_CASE("MessagePack")

SECTION("256..65535 (uint 16)")
{
for (size_t i = 256; i <= 65535; ++i)
for (size_t i = 256; i <= 65535; i = utils::next_integer_sample(i, static_cast<size_t>(65535), static_cast<size_t>(7)))
{
CAPTURE(i)

Expand Down Expand Up @@ -1983,60 +1983,34 @@ TEST_CASE("MessagePack roundtrips" * doctest::skip())
{
CAPTURE(filename)

std::ifstream f_json(filename);
const json j1 = json::parse(f_json);
auto packed = utils::read_binary_file(filename + ".msgpack");

{
INFO_WITH_TEMP(filename + ": std::vector<uint8_t>");
// parse JSON file
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);

// parse MessagePack file
auto packed = utils::read_binary_file(filename + ".msgpack");
json j2;
CHECK_NOTHROW(j2 = json::from_msgpack(packed));

// compare parsed JSON values
CHECK(j1 == j2);
}

{
INFO_WITH_TEMP(filename + ": std::ifstream");
// parse JSON file
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);

// 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);
}

{
INFO_WITH_TEMP(filename + ": uint8_t* and size");
// parse JSON file
std::ifstream f_json(filename);
const json j1 = json::parse(f_json);

// parse MessagePack file
auto packed = utils::read_binary_file(filename + ".msgpack");
json j2;
CHECK_NOTHROW(j2 = json::from_msgpack({packed.data(), packed.size()}));

// compare parsed JSON values
CHECK(j1 == j2);
}

{
INFO_WITH_TEMP(filename + ": output to output adapters");
// parse JSON file
std::ifstream f_json(filename);
json const j1 = json::parse(f_json);

// parse MessagePack file
auto packed = utils::read_binary_file(filename + ".msgpack");

if (exclude_packed.count(filename) == 0u)
{
{
Expand Down
Loading
Loading