Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
falbrechtskirchinger committed Jul 19, 2022
1 parent 2e6d361 commit c27d0d2
Show file tree
Hide file tree
Showing 11 changed files with 22,288 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ pretty:
--preserve-date \
--suffix=none \
--formatted \
$(SRCS) $(AMALGAMATED_FILE) tests/src/*.cpp tests/src/*.hpp tests/benchmarks/src/benchmarks.cpp docs/examples/*.cpp
$(SRCS) $(AMALGAMATED_FILE) tests/src/*.cpp tests/src/*.hpp tests/abi/*.cpp tests/abi/*.hpp tests/benchmarks/src/benchmarks.cpp docs/examples/*.cpp

# call the Clang-Format on all source files
pretty_format:
for FILE in $(SRCS) $(AMALGAMATED_FILE) tests/src/*.cpp tests/src/*.hpp benchmarks/src/benchmarks.cpp docs/examples/*.cpp; do echo $$FILE; clang-format -i $$FILE; done
for FILE in $(SRCS) $(AMALGAMATED_FILE) tests/src/*.cpp tests/src/*.hpp tests/abi/*.cpp tests/abi/*.hpp benchmarks/src/benchmarks.cpp docs/examples/*.cpp; do echo $$FILE; clang-format -i $$FILE; done

# create single header file
amalgamate: $(AMALGAMATED_FILE)
Expand Down
3 changes: 3 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ json_test_add_test_for(src/unit-comparison.cpp
MAIN test_main CXX_STANDARDS ${test_cxx_standards} ${test_force}
)

# test ABI compatibility
add_subdirectory(abi)

# *DO NOT* use json_test_set_test_options() below this line

#############################################################################
Expand Down
45 changes: 45 additions & 0 deletions tests/abi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
add_library(abi_compat_common INTERFACE)
target_compile_definitions(abi_compat_common INTERFACE
DOCTEST_CONFIG_SUPER_FAST_ASSERTS
JSON_TEST_KEEP_MACROS)
target_compile_features(abi_compat_common INTERFACE cxx_std_11)
target_compile_options(abi_compat_common INTERFACE
$<$<CXX_COMPILER_ID:MSVC>:/EHsc;$<$<CONFIG:Release>:/Od>>
# MSVC: Force to always compile with W4
$<$<CXX_COMPILER_ID:MSVC>:/W4>

# https://github.com/nlohmann/json/pull/3229
$<$<CXX_COMPILER_ID:Intel>:-diag-disable=2196>

$<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-Wno-deprecated;-Wno-float-equal>
$<$<CXX_COMPILER_ID:GNU>:-Wno-deprecated-declarations>
$<$<CXX_COMPILER_ID:Intel>:-diag-disable=1786>)
target_include_directories(abi_compat_common INTERFACE
../thirdparty/doctest
include)
target_link_libraries(abi_compat_common INTERFACE ${NLOHMANN_JSON_TARGET_NAME})

add_library(abi_compat_main STATIC main.cpp)
target_link_libraries(abi_compat_main PUBLIC abi_compat_common)

add_executable(abi_compat_pre_inline_ns
use_v3_10_5.cpp
use_this.cpp
)
target_link_libraries(abi_compat_pre_inline_ns PRIVATE abi_compat_main)
add_test(
NAME test-abi_compat_pre_inline_ns
COMMAND abi_compat_pre_inline_ns ${DOCTEST_TEST_FILTER})

add_library(abi_compat_diag_on STATIC diag_on.cpp)
target_link_libraries(abi_compat_diag_on PUBLIC abi_compat_common)

add_library(abi_compat_diag_off STATIC diag_off.cpp)
target_link_libraries(abi_compat_diag_off PUBLIC abi_compat_common)

add_executable(abi_compat_diag diag.cpp)
target_link_libraries(abi_compat_diag PRIVATE
abi_compat_main abi_compat_diag_on abi_compat_diag_off)
add_test(
NAME test-abi_compat_diag
COMMAND abi_compat_diag ${DOCTEST_TEST_FILTER})
19 changes: 19 additions & 0 deletions tests/abi/diag.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "doctest_compatibility.h"

#include "diag.hpp"

TEST_CASE("ABI compatible diagnostics")
{
SECTION("basic_json size")
{
CHECK(json_sizeof_diag_on() == json_sizeof_diag_on_explicit());
CHECK(json_sizeof_diag_off() == json_sizeof_diag_off_explicit());
CHECK(json_sizeof_diag_on() > json_sizeof_diag_off());
}

SECTION("basic_json at")
{
CHECK_THROWS_WITH(json_at_diag_on(), "[json.exception.out_of_range.403] (/foo) key 'bar' not found");
CHECK_THROWS_WITH(json_at_diag_off(), "[json.exception.out_of_range.403] key 'bar' not found");
}
}
12 changes: 12 additions & 0 deletions tests/abi/diag.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <cstddef>

std::size_t json_sizeof_diag_on();
std::size_t json_sizeof_diag_on_explicit();

std::size_t json_sizeof_diag_off();
std::size_t json_sizeof_diag_off_explicit();

void json_at_diag_on();
void json_at_diag_off();
22 changes: 22 additions & 0 deletions tests/abi/diag_off.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#undef JSON_DIAGNOSTICS
#define JSON_DIAGNOSTICS 0
#include <nlohmann/json.hpp>

#include "diag.hpp"

std::size_t json_sizeof_diag_off()
{
return sizeof(nlohmann::json);
}

std::size_t json_sizeof_diag_off_explicit()
{
return sizeof(::NLOHMANN_JSON_NAMESPACE::json);
}

void json_at_diag_off()
{
using nlohmann::json;
json j = json{{"foo", json::object()}};
j.at(json::json_pointer("/foo/bar"));
}
22 changes: 22 additions & 0 deletions tests/abi/diag_on.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#undef JSON_DIAGNOSTICS
#define JSON_DIAGNOSTICS 1
#include <nlohmann/json.hpp>

#include "diag.hpp"

std::size_t json_sizeof_diag_on()
{
return sizeof(nlohmann::json);
}

std::size_t json_sizeof_diag_on_explicit()
{
return sizeof(::NLOHMANN_JSON_NAMESPACE::json);
}

void json_at_diag_on()
{
using nlohmann::json;
json j = json{{"foo", json::object()}};
j.at(json::json_pointer("/foo/bar"));
}
Loading

0 comments on commit c27d0d2

Please sign in to comment.