summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2023-11-06 09:41:27 +0100
committerStanislaw Halik <sthalik@misaki.pl>2023-11-06 09:41:27 +0100
commit8f43d67836b0083d28a1642fbf7382c8a1b493c3 (patch)
treebf5c025bcb1845555619612d728f01d43a196b77
parent98c7d987251a550c41db29ef000573733245f56e (diff)
a
-rw-r--r--serialize/magnum-vector.hpp2
-rw-r--r--serialize/wall-atlas.cpp23
-rw-r--r--serialize/wall-atlas.hpp4
-rw-r--r--test/CMakeLists.txt4
-rw-r--r--test/json/wall-atlas-01_header.json7
-rw-r--r--test/json/wall-atlas-02_groups.json12
-rw-r--r--test/json/wall-atlas-header1.json9
-rw-r--r--test/wall-atlas.cpp58
8 files changed, 78 insertions, 41 deletions
diff --git a/serialize/magnum-vector.hpp b/serialize/magnum-vector.hpp
index a45103d8..ddd09228 100644
--- a/serialize/magnum-vector.hpp
+++ b/serialize/magnum-vector.hpp
@@ -32,7 +32,7 @@ void adl_serializer<Magnum::Math::Vector<N, T>>::from_json(const json& j, vec& v
val[i] = array[i];
}
-template<typename T> struct adl_serializer<Magnum::Math::Vector2<T>> : adl_serializer<Magnum::Math::Vector<2, T>> {};
+template<typename T> requires (!std::is_integral_v<T>) struct adl_serializer<Magnum::Math::Vector2<T>> : adl_serializer<Magnum::Math::Vector<2, T>> {};
template<typename T> struct adl_serializer<Magnum::Math::Vector3<T>> : adl_serializer<Magnum::Math::Vector<3, T>> {};
template<typename T> struct adl_serializer<Magnum::Math::Vector4<T>> : adl_serializer<Magnum::Math::Vector<4, T>> {};
template<typename T> struct adl_serializer<Magnum::Math::Color3<T>> : adl_serializer<Magnum::Math::Vector<3, T>> {};
diff --git a/serialize/wall-atlas.cpp b/serialize/wall-atlas.cpp
index 8ebe106a..7480c53f 100644
--- a/serialize/wall-atlas.cpp
+++ b/serialize/wall-atlas.cpp
@@ -5,6 +5,7 @@
#include <utility>
#include <string_view>
#include <Corrade/Containers/StringStl.h>
+#include <Corrade/Containers/StringStlView.h>
#include <nlohmann/json.hpp>
// todo add test on dummy files that generates 100% coverage on the j.contains() blocks!
@@ -38,7 +39,7 @@ StringView direction_index_to_name(size_t i)
return direction_names[i];
}
-[[nodiscard]] Group read_group_metadata(const json& jgroup)
+Group read_group_metadata(const json& jgroup)
{
fm_assert(jgroup.is_object());
@@ -60,6 +61,26 @@ StringView direction_index_to_name(size_t i)
return val;
}
+Direction read_direction_metadata(const json& jroot, Direction_ dir)
+{
+ std::string_view s = direction_index_to_name((size_t)dir);
+ if (!jroot.contains(s))
+ return {};
+ const auto& jdir = jroot[s];
+
+ Direction val;
+
+ for (auto [s_, memfn, tag] : Direction::members)
+ {
+ std::string_view s = s_;
+ if (!jdir.contains(s))
+ continue;
+ val.*memfn = read_group_metadata(jdir[s]);
+ }
+
+ return val;
+}
+
void write_group_metadata(json& jgroup, const Group& val)
{
constexpr Group default_value;
diff --git a/serialize/wall-atlas.hpp b/serialize/wall-atlas.hpp
index 9f1ba71c..16c43ab6 100644
--- a/serialize/wall-atlas.hpp
+++ b/serialize/wall-atlas.hpp
@@ -19,7 +19,9 @@ namespace floormat::Wall::detail {
uint8_t direction_index_from_name(StringView s);
StringView direction_index_to_name(size_t i);
[[nodiscard]] Group read_group_metadata(const nlohmann::json& jgroup);
-void write_group_metadata(nlohmann::json& jgroup, const Group& val);
+[[nodiscard]] Direction read_direction_metadata(const nlohmann::json& jroot, Direction_ dir);
Info read_info_header(const nlohmann::json& jroot);
+void write_group_metadata(nlohmann::json& jgroup, const Group& val);
+
} // namespace floormat::Wall::detail
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 3764142f..e5fc6435 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -20,12 +20,12 @@ endif()
fm_add_install_executable(${self})
-file(GLOB files "save/*.dat" CONFIGURE_ARGS)
+file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/save/*.dat" CONFIGURE_ARGS)
foreach(file ${files})
install(FILES "${file}" DESTINATION "${CMAKE_BINARY_DIR}/test/save")
endforeach()
-file(GLOB files "json/*.json" CONFIGURE_ARGS)
+file(GLOB files "${CMAKE_CURRENT_SOURCE_DIR}/json/*.json" CONFIGURE_ARGS)
foreach(file ${files})
install(FILES "${file}" DESTINATION "${CMAKE_BINARY_DIR}/test/json")
endforeach()
diff --git a/test/json/wall-atlas-01_header.json b/test/json/wall-atlas-01_header.json
new file mode 100644
index 00000000..6859c6da
--- /dev/null
+++ b/test/json/wall-atlas-01_header.json
@@ -0,0 +1,7 @@
+{
+ "name": "foo",
+ "depth": 42,
+ "frames": [],
+ "n": {},
+ "w": {}
+}
diff --git a/test/json/wall-atlas-02_groups.json b/test/json/wall-atlas-02_groups.json
new file mode 100644
index 00000000..cf342eac
--- /dev/null
+++ b/test/json/wall-atlas-02_groups.json
@@ -0,0 +1,12 @@
+{
+ "name": "foo",
+ "depth": 42,
+ "frames": [],
+ "n": {},
+ "w": {
+ "wall": {},
+ "side": {
+ "pixel-size": "42 x 192"
+ }
+ }
+}
diff --git a/test/json/wall-atlas-header1.json b/test/json/wall-atlas-header1.json
deleted file mode 100644
index a5591245..00000000
--- a/test/json/wall-atlas-header1.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{
- "name": "foo",
- "depth": 42,
- "directions": {
- "w": {},
- "n": {}
- },
- "frames": []
-}
diff --git a/test/wall-atlas.cpp b/test/wall-atlas.cpp
index 85f1e3f4..f5ddf2b5 100644
--- a/test/wall-atlas.cpp
+++ b/test/wall-atlas.cpp
@@ -23,44 +23,47 @@ StringView json_path()
return path;
}
-void test_read_info(StringView filename)
+void test_read_header(StringView filename)
{
const auto jroot = json_helper::from_json_(Path::join(json_path(), filename));
- fm_assert(jroot.contains("directions"s));
auto info = read_info_header(jroot);
fm_assert(info.name == "foo"_s);
fm_assert(info.depth == 42);
-
-#if 0
- fm_assert(def.dir_indexes[N] == 0 || def.dir_indexes[N] == 1);
- fm_assert(def.dir_indexes[E] == none);
- fm_assert(def.dir_indexes[S] == none);
-
- if (def.dir_indexes[N] == 0)
- fm_assert(def.dir_indexes[W] == 1);
- else if (def.dir_indexes[N] == 1)
- fm_assert(def.dir_indexes[W] == 0);
- else
- fm_assert(false);
-#endif
}
void test_read_empty_direction(StringView filename)
{
const auto jroot = json_helper::from_json_(Path::join(json_path(), filename));
- test_read_info(filename);
+ test_read_header(filename);
fm_assert(!jroot.empty());
- fm_assert(jroot.contains("directions"s));
- const auto& jdir = jroot["directions"s];
- fm_assert( jdir.contains("n"s) );
- fm_assert(!jdir.contains("e"s) );
- fm_assert(!jdir.contains("s"s) );
- fm_assert( jdir.contains("w"s) );
+ fm_assert( jroot.contains("n"s) );
+ fm_assert(!jroot.contains("e"s) );
+ fm_assert(!jroot.contains("s"s) );
+ fm_assert( jroot.contains("w"s) );
+
+ fm_assert(jroot["n"s].is_object() && jroot["n"s].empty());
+ fm_assert(jroot["w"s].is_object() && jroot["w"s].empty());
+}
+
+void test_read_groups(StringView filename)
+{
+ const auto jroot = json_helper::from_json_(Path::join(json_path(), filename));
+ read_info_header(jroot);
- { auto g = read_group_metadata(jdir["w"]);
- fm_assert(g.is_empty());
- }
+ fm_assert(jroot["depth"s] == 42);
+ fm_assert( jroot.contains("n"s) );
+ fm_assert(!jroot.contains("e"s) );
+ fm_assert(!jroot.contains("s"s) );
+ fm_assert( jroot.contains("w"s) );
+ fm_assert(jroot["n"s].is_object() && jroot["n"s].empty());
+ fm_assert(jroot["w"s].is_object() && !jroot["w"s].empty());
+ fm_assert(read_direction_metadata(jroot, Direction_::N).is_empty());
+ fm_assert(read_direction_metadata(jroot, Direction_::E).is_empty());
+ fm_assert(read_direction_metadata(jroot, Direction_::S).is_empty());
+ const auto dir = read_direction_metadata(jroot, Direction_::W);
+ fm_assert(dir.wall.pixel_size == Vector2ui{});
+ fm_assert(dir.side.pixel_size == Vector2ui{42, 192});
}
} // namespace
@@ -70,6 +73,7 @@ void test_read_empty_direction(StringView filename)
void floormat::test_app::test_wall_atlas()
{
using namespace floormat::Wall::detail;
- test_read_info("wall-atlas-header1.json"_s);
- test_read_empty_direction("wall-atlas-header1.json"_s);
+ test_read_header("wall-atlas-01_header.json"_s);
+ test_read_empty_direction("wall-atlas-01_header.json"_s);
+ test_read_groups("wall-atlas-02_groups.json"_s);
}