From 28911bb1e2dcf82f404eaf72328aed943374bf00 Mon Sep 17 00:00:00 2001 From: Stepland <16676308+Stepland@users.noreply.github.com> Date: Wed, 19 Feb 2020 17:32:56 +0100 Subject: [PATCH] More tests --- test/cereal_specialize_enums.cpp | 110 +++++++++++++++++++++++++++++++ test/cereal_unordered_map.cpp | 7 +- test/meson.build | 18 +++-- 3 files changed, 126 insertions(+), 9 deletions(-) create mode 100644 test/cereal_specialize_enums.cpp diff --git a/test/cereal_specialize_enums.cpp b/test/cereal_specialize_enums.cpp new file mode 100644 index 0000000..fa86abe --- /dev/null +++ b/test/cereal_specialize_enums.cpp @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +enum class Keys { + blue, + green, + yellow, + orange, + red, +}; + +enum Values { + YES, + NO, + MAYBE +}; + +namespace cereal { + template < + class Archive, + traits::EnableIf::value> = traits::sfinae + > inline + void save(Archive& ar, const std::unordered_map& map ) { + for(auto&& i : magic_enum::enum_entries()) { + auto it = map.find(i.first); + if (it != map.end()) { + ar( + cereal::make_nvp( + std::string{i.second}, + std::string{magic_enum::enum_name(it->second)} + ) + ); + } + } + } + + template < + class Archive, + traits::EnableIf::value> = traits::sfinae + > inline + void load(Archive& ar, std::unordered_map& map ) { + map.clear(); + while(true) { + const auto namePtr = ar.getNodeName(); + if (not namePtr) { + break; + } + std::string key_str = namePtr; + std::string value_str; + ar(value_str); + + auto key = magic_enum::enum_cast(key_str); + if (not key.has_value()) { + throw std::runtime_error("Invalid Key : "+key_str); + } + + auto value = magic_enum::enum_cast(value_str); + if (not value.has_value()) { + throw std::runtime_error("Invalid Value : "+value_str); + } + + map.emplace(*key, *value); + } + } +} // namespace cereal + +struct MyThingWithAnUnorderedMap { + std::unordered_map m_map; +}; + +struct Indirect { + MyThingWithAnUnorderedMap m; + + template + void serialize(Archive & archive) { + archive( + cereal::make_nvp("map", m.m_map) + ); + } +}; + +int main(int argc, char const *argv[]) { + Indirect ref; + ref.m.m_map = {{Keys::blue, YES}, {Keys::green, MAYBE}}; + { + std::ofstream file; + file.open("cereal_test_specialize_enums.json", std::ofstream::trunc | std::ofstream::out); + cereal::JSONOutputArchive archive{file}; + archive(ref); + } + + Indirect test; + { + std::ifstream file; + file.open("cereal_test_specialize_enums.json", std::ifstream::in); + cereal::JSONInputArchive archive{file}; + archive(test); + } + assert((ref.m.m_map == test.m.m_map)); + std::remove("cereal_test_specialize_enums.json"); + return 0; +} \ No newline at end of file diff --git a/test/cereal_unordered_map.cpp b/test/cereal_unordered_map.cpp index 9821ad1..5d5c941 100644 --- a/test/cereal_unordered_map.cpp +++ b/test/cereal_unordered_map.cpp @@ -1,3 +1,5 @@ +#include +#include #include #include #include @@ -52,8 +54,7 @@ int main(int argc, char const *argv[]) { cereal::JSONInputArchive archive{file}; archive(test); } - if (ref.m.m_map != test.m.m_map) { - return -1; - } + assert((ref.m.m_map == test.m.m_map)); + std::remove("cereal_test_specialize_enums.json"); return 0; } diff --git a/test/meson.build b/test/meson.build index dc264dd..7ea98dc 100644 --- a/test/meson.build +++ b/test/meson.build @@ -1,7 +1,13 @@ -cereal_unordered_map = executable( - 'cereal_unordered_map', - 'cereal_unordered_map.cpp', - include_directories : inc -) +test_files = [ + 'cereal_specialize_enums.cpp', + 'cereal_unordered_map.cpp' +] -test('cereal unordered_map test', cereal_unordered_map) \ No newline at end of file +foreach test_file : test_files + test_executable = executable( + test_file+'.out', + test_file, + include_directories : inc + ) + test(test_file+' test', test_executable) +endforeach \ No newline at end of file