From 5200f57c83679938e47cc1c904f8de2bde0be30a Mon Sep 17 00:00:00 2001 From: injae <8687lee@gmail.com> Date: Mon, 28 Feb 2022 21:42:47 +0900 Subject: [PATCH] yaml-cpp: 0.6.3 -> 0.7.0, rapidjson Map bugfix, and unittest --- CMakeLists.txt | 32 ++++++++++++++++++++- cppm.toml | 11 +++++-- examples/example.cpp | 2 +- include/serdepp/adaptor/rapidjson.hpp | 5 +++- tests/catch_main.cpp | 7 +++++ tests/nlohmann_json.cpp | 23 +++++++++++++++ tests/rapid_json.cpp | 28 ++++++++++++++++++ tests/reflection.cpp | 18 ++++++++++++ tests/test_struct.hpp | 25 ++++++++++++++++ tests/toml11.cpp | 25 ++++++++++++++++ tests/yaml_cpp.cpp | 30 +++++++++++++++++++ thirdparty/Catch2/2.9.1/cppkg.toml | 5 ++++ thirdparty/yaml-cpp/0.7.0/cppkg.toml | 7 +++++ thirdparty/yaml-cpp/0.7.0/yaml-cpp.cmake.in | 8 ++++++ 14 files changed, 220 insertions(+), 6 deletions(-) create mode 100644 tests/catch_main.cpp create mode 100644 tests/nlohmann_json.cpp create mode 100644 tests/rapid_json.cpp create mode 100644 tests/reflection.cpp create mode 100644 tests/test_struct.hpp create mode 100644 tests/toml11.cpp create mode 100644 tests/yaml_cpp.cpp create mode 100644 thirdparty/Catch2/2.9.1/cppkg.toml create mode 100644 thirdparty/yaml-cpp/0.7.0/cppkg.toml create mode 100644 thirdparty/yaml-cpp/0.7.0/yaml-cpp.cmake.in diff --git a/CMakeLists.txt b/CMakeLists.txt index 4831b0b..e01342c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,11 +13,16 @@ find_cppkg(benchmark 1.5.2 MODULE benchmark::benchmark TYPE lib OPTIONAL OFF) find_cppkg(RapidJSON 1.1.1 MODULE rapidjson TYPE lib OPTIONAL OFF) find_cppkg(nlohmann_json 3.10.5 MODULE nlohmann_json::nlohmann_json TYPE lib OPTIONAL OFF) find_cppkg(toml11 3.7.0 MODULE toml11::toml11 TYPE lib OPTIONAL OFF) -find_cppkg(yaml-cpp 0.6.3 MODULE yaml-cpp TYPE lib OPTIONAL OFF) +find_cppkg(yaml-cpp 0.7.0 MODULE yaml-cpp TYPE lib OPTIONAL OFF) find_cppkg(fmt 8.0.1 MODULE fmt::fmt-header-only TYPE lib OPTIONAL OFF) find_cppkg(magic_enum 0.7.3 MODULE magic_enum::magic_enum TYPE lib) find_cppkg(nameof 0.10.0 MODULE nameof::nameof TYPE lib) +if(CMAKE_BUILD_TYPE STREQUAL "Debug") +find_cppkg(Catch2 2.9.1 MODULE Catch2::Catch2 TYPE lib) +find_cppkg(benchmark 1.5.2 MODULE benchmark::benchmark TYPE lib OPTIONAL OFF) +endif() + cppm_target_define(serdepp STATIC SOURCES @@ -84,6 +89,23 @@ SOURCES endif() +cppm_unit_test_area() +if(SERDEPP_BUILD_TESTING) + +cppm_target_define(unittest BINARY +SOURCES + tests/rapid_json.cpp + tests/catch_main.cpp + tests/yaml_cpp.cpp + tests/toml11.cpp + tests/reflection.cpp + tests/nlohmann_json.cpp + tests/test_struct.hpp +) + +endif() +end_cppm_unit_test_area() + if(SERDEPP_BUILD_BENCHMARKS) cppm_target_define(benchmark BINARY @@ -105,6 +127,9 @@ endif() set(serdepp_global_deps PRIVATE benchmark PUBLIC RapidJSON nlohmann_json toml11 yaml-cpp fmt magic_enum nameof) +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + list(APPEND serdepp_global_deps PRIVATE Catch2 benchmark) +endif() cppm_target_dependencies(serdepp ${serdepp_global_deps}) @@ -152,6 +177,10 @@ cppm_target_dependencies(pointer ${serdepp_global_deps} serdepp) +cppm_target_dependencies(unittest + ${serdepp_global_deps} + serdepp) + cppm_target_dependencies(benchmark ${serdepp_global_deps} serdepp) @@ -166,6 +195,7 @@ cppm_target_dependencies(syntax_sugar_benchmark cppm_target_install(serdepp) +cppm_target_install(unittest) cppm_target_install(serde_example1) cppm_target_install(serde_example) cppm_target_install(simple_example) diff --git a/cppm.toml b/cppm.toml index ea9d200..8b1c627 100644 --- a/cppm.toml +++ b/cppm.toml @@ -82,11 +82,15 @@ name = "syntax_sugar_benchmark" source = ["benchmark/syntax_benchmark.cpp"] +[[tests]] +name = "unittest" +source = ["tests/.*"] + [dependencies] nameof = { version="0.10.0", link="public"} magic_enum = { version= "0.7.3", link="public"} fmt = {version="8.0.1", link="public", optional=true} - yaml-cpp = { version="0.6.3", link="public", optional=true} + yaml-cpp = { version="0.7.0", link="public", optional=true} toml11 = { version="3.7.0", link="public", optional=true} nlohmann_json = { version="3.10.5", link="public", optional=true} RapidJSON = { version="1.1.1", link="public", optional=true} @@ -97,5 +101,6 @@ #simdjson = {version="0.9.7", link="public"} #ryml = { version= "0.2.0", link="public", optional=true} #sol2 = { version="3.2.2"} -#[dev-dependencies] -# benchmark = {version="1.5.2", optional=true} +[dev-dependencies] + benchmark = {version="1.5.2", optional=true} + Catch2 = "2.9.1" diff --git a/examples/example.cpp b/examples/example.cpp index e17b5ef..45d9ab7 100644 --- a/examples/example.cpp +++ b/examples/example.cpp @@ -57,7 +57,7 @@ int main() "b" : "2", "c" : "3" }, "nm" : { "a" : {"version" : "hello" }, - "b" : "hello2" } + "b" : "hello2" } })"_json; // try { diff --git a/include/serdepp/adaptor/rapidjson.hpp b/include/serdepp/adaptor/rapidjson.hpp index 6065714..e7bc6eb 100644 --- a/include/serdepp/adaptor/rapidjson.hpp +++ b/include/serdepp/adaptor/rapidjson.hpp @@ -3,6 +3,8 @@ #ifndef __SERDEPP_ADAPTOR_RAPID_JSON_HPP__ #define __SERDEPP_ADAPTOR_RAPID_JSON_HPP__ +#define RAPIDJSON_HAS_STDSTRING 1 + #include "serdepp/serializer.hpp" #include #include @@ -221,8 +223,9 @@ namespace serde { map.SetObject(); for(auto& [key_, value_] : data) { Value val; + Value skey(key_.c_str(), key_.length(), s.GetAllocator()); val.CopyFrom(serialize(value_), s.GetAllocator()); - map.AddMember(StringRef(key_.c_str(), key_.length()), val.Move(), s.GetAllocator()); + map.AddMember(skey, val.Move(), s.GetAllocator()); } if(key.empty()) { s.CopyFrom(map.Move(), s.GetAllocator()); diff --git a/tests/catch_main.cpp b/tests/catch_main.cpp new file mode 100644 index 0000000..b955482 --- /dev/null +++ b/tests/catch_main.cpp @@ -0,0 +1,7 @@ +#define CATCH_CONFIG_MAIN + +#include + +TEST_CASE("1: test (pass)", "[multi-file:1]") { + REQUIRE(1==1); +} diff --git a/tests/nlohmann_json.cpp b/tests/nlohmann_json.cpp new file mode 100644 index 0000000..5d6d722 --- /dev/null +++ b/tests/nlohmann_json.cpp @@ -0,0 +1,23 @@ + +#include +#include "test_struct.hpp" +#include + + +nlohmann::json json_v = R"({ +"str" : "hello", +"i": 10, +"vec": [ "one", "two", "three"], +"sm": { "one" : "tone", "two" : "ttwo"}, +"opt": "hello", +"sub" : { "str": "hello" } +})"_json; + +using namespace serde; + +TEST_CASE("2: nlohmann json struct (pass)", "[multi-file:2]") { + REQUIRE(json_v.dump() == serialize(deserialize(json_v)).dump()); +} + + + diff --git a/tests/rapid_json.cpp b/tests/rapid_json.cpp new file mode 100644 index 0000000..0998101 --- /dev/null +++ b/tests/rapid_json.cpp @@ -0,0 +1,28 @@ +#include +#include "test_struct.hpp" +#include + +std::string str(rapidjson::Document& doc) { + using namespace rapidjson; + StringBuffer buffer; + Writer writer(buffer); + doc.Accept(writer); + return buffer.GetString(); +}; + +using namespace serde; + +TEST_CASE("5: toml11 struct (pass)", "[multi-file:5]") { + rapidjson::Document json_v; + json_v.Parse(R"({ +"str" : "hello", +"i": 10, +"vec": [ "one", "two", "three"], +"opt": "hello", +"sm": { "one" : "tone", "two" : "ttwo"}, +"sub" : { "str": "hello" } +})"); + rapidjson::Document json_c = serialize(deserialize(json_v)); + + REQUIRE(str(json_v) == str(json_c)); +} diff --git a/tests/reflection.cpp b/tests/reflection.cpp new file mode 100644 index 0000000..f8caee8 --- /dev/null +++ b/tests/reflection.cpp @@ -0,0 +1,18 @@ +#include +#include "test_struct.hpp" +#include + +using namespace serde; + +TEST_CASE("3: reflection struct (pass)", "[multi-file:3]") { + REQUIRE(std::is_same_v, + std::tuple, + std::optional, + std::optional, + std::map, + nested>>); +} + + diff --git a/tests/test_struct.hpp b/tests/test_struct.hpp new file mode 100644 index 0000000..6945b1a --- /dev/null +++ b/tests/test_struct.hpp @@ -0,0 +1,25 @@ +#include + +#pragma once + +#ifndef __CATCH_TEST_STRUCT_HPP__ +#define __CATCH_TEST_STRUCT_HPP__ + +struct nested { + DERIVE_SERDE(nested, _SF_(str)) + std::string str; +}; + +struct test { + DERIVE_SERDE(test, _SF_(str)_SF_(i)_SF_(vec)_SF_(opt)_SF_(none_opt)_SF_(sm)_SF_(sub)) + std::string str; + int i; + std::vector vec; + std::optional none_opt; + std::optional opt; + std::map sm; + nested sub; +}; + +#endif + diff --git a/tests/toml11.cpp b/tests/toml11.cpp new file mode 100644 index 0000000..acc0f9a --- /dev/null +++ b/tests/toml11.cpp @@ -0,0 +1,25 @@ +#include +#include "test_struct.hpp" +#include + +using namespace serde; + +using namespace toml::literals; + +toml::value toml_vl = R"( +str = "hello" +i = 10 +vec = [ "one", "two", "three" ] +opt = "hello" + +[sm] +one = "tone" +two = "ttwo" + +[sub] +str = "hello" +)"_toml; + +TEST_CASE("4: toml11 struct (pass)", "[multi-file:4]") { + REQUIRE(toml_vl == serialize(deserialize(toml_vl))); +} diff --git a/tests/yaml_cpp.cpp b/tests/yaml_cpp.cpp new file mode 100644 index 0000000..3772205 --- /dev/null +++ b/tests/yaml_cpp.cpp @@ -0,0 +1,30 @@ +#include +#include "test_struct.hpp" +#include + + +YAML::Node yaml_v = YAML::Load(R"( +str: hello +i: 10 +vec: +- one +- two +- three +opt: hello +sm: + one: tone + two: ttwo +sub: + str: hello +)"); + +using namespace serde; + +TEST_CASE("5: yaml-cpp struct (pass)", "[multi-file:5]") { + std::ostringstream origin; + origin << yaml_v; + std::ostringstream convert; + convert << serialize(deserialize(yaml_v)); + + REQUIRE(origin.str() == convert.str()); +} diff --git a/thirdparty/Catch2/2.9.1/cppkg.toml b/thirdparty/Catch2/2.9.1/cppkg.toml new file mode 100644 index 0000000..2302478 --- /dev/null +++ b/thirdparty/Catch2/2.9.1/cppkg.toml @@ -0,0 +1,5 @@ +[Catch2] +version = "2.9.1" +description = "modern, C++-native, header-only, test framework for unit-tests" +module = "Catch2::Catch2" +url="https://github.com/catchorg/Catch2/archive/v2.9.1.tar.gz" \ No newline at end of file diff --git a/thirdparty/yaml-cpp/0.7.0/cppkg.toml b/thirdparty/yaml-cpp/0.7.0/cppkg.toml new file mode 100644 index 0000000..e31746b --- /dev/null +++ b/thirdparty/yaml-cpp/0.7.0/cppkg.toml @@ -0,0 +1,7 @@ +[yaml-cpp] +version="0.7.0" #(require) +type="lib" #lib(default) | bin | cmake +description="" #(require) +module="yaml-cpp" #(require) if none_module=true -> no require +flags="-DYAML_CPP_BUILD_TESTS=OFF" +url="https://github.com/jbeder/yaml-cpp/archive/refs/tags/yaml-cpp-0.7.0.zip" #(require) diff --git a/thirdparty/yaml-cpp/0.7.0/yaml-cpp.cmake.in b/thirdparty/yaml-cpp/0.7.0/yaml-cpp.cmake.in new file mode 100644 index 0000000..35946fd --- /dev/null +++ b/thirdparty/yaml-cpp/0.7.0/yaml-cpp.cmake.in @@ -0,0 +1,8 @@ +# Cppkg Base Dependency Downloader +cmake_minimum_required(VERSION 3.6) +project(yaml-cpp-0.6.3-install) + +set(CPPM_VERSION ${CPPM_VERSION}) +include(${CMAKE_CURRENT_SOURCE_DIR}/cmake/cppm_loader.cmake) +download_package(yaml-cpp 0.6.3 URL https://github.com/jbeder/yaml-cpp/archive/yaml-cpp-0.6.3.tar.gz TYPE lib CMAKE_ARGS ${CMAKE_ARGS} -DYAML_CPP_BUILD_TESTS=OFF) +