changeset 89:0bedc450a9d2

Tools: mlk-map add tests
author David Demelier <markand@malikania.fr>
date Sun, 05 Feb 2017 20:03:59 +0100
parents 469b6d558ab0
children 58b43650d8f0
files tests/tools/CMakeLists.txt tests/tools/map/CMakeLists.txt tests/tools/map/main.cpp tests/tools/map/minimal-b64-gzip.tmx tests/tools/map/minimal-b64-zlib.tmx tests/tools/map/minimal-b64.tmx tests/tools/map/minimal-csv.tmx tests/tools/map/minimal-xml.tmx tests/tools/map/minimal.png tests/tools/map/minimal.xcf tools/map/main.cpp
diffstat 11 files changed, 383 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tools/CMakeLists.txt	Sun Feb 05 20:03:59 2017 +0100
@@ -0,0 +1,19 @@
+#
+# CMakeLists.txt -- CMake build system for malikania
+#
+# Copyright (c) 2013-2017 Malikania Authors
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+add_subdirectory(map)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tools/map/CMakeLists.txt	Sun Feb 05 20:03:59 2017 +0100
@@ -0,0 +1,61 @@
+#
+# CMakeLists.txt -- CMake build system for malikania
+#
+# Copyright (c) 2013-2017 Malikania Authors
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+#
+
+set(
+    maps
+    minimal-b64
+    minimal-b64-gzip
+    minimal-b64-zlib
+    minimal-csv
+    minimal-xml
+)
+
+foreach (m ${maps})
+    add_custom_command(
+        OUTPUT
+            ${CMAKE_CURRENT_BINARY_DIR}/${m}.json
+            ${CMAKE_CURRENT_BINARY_DIR}/${m}.map
+        COMMAND
+            $<TARGET_FILE:mlk-map>
+                ${CMAKE_CURRENT_SOURCE_DIR}/${m}.tmx
+                ${CMAKE_CURRENT_BINARY_DIR}/${m}.json
+                ${CMAKE_CURRENT_BINARY_DIR}/${m}.map
+        DEPENDS
+            mlk-map
+            ${CMAKE_CURRENT_SOURCE_DIR}/${m}.tmx
+        VERBATIM
+        COMMENT "Creating minimal.json and minimal.map using mlk-map(1)"
+    )
+
+    list(APPEND sources ${CMAKE_CURRENT_SOURCE_DIR}/${m}.tmx)
+    list(
+        APPEND
+        outputs
+        ${CMAKE_CURRENT_BINARY_DIR}/${m}.json
+        ${CMAKE_CURRENT_BINARY_DIR}/${m}.map
+    )
+endforeach()
+
+malikania_create_test(
+    NAME map
+    LIBRARIES json
+    SOURCES
+        ${sources}
+        ${outputs}
+        ${CMAKE_CURRENT_SOURCE_DIR}/main.cpp
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tools/map/main.cpp	Sun Feb 05 20:03:59 2017 +0100
@@ -0,0 +1,120 @@
+/*
+ * main.cpp -- test mlk-map
+ *
+ * Copyright (c) 2013-2017 Malikania Authors
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <algorithm>
+#include <cerrno>
+#include <cstring>
+#include <fstream>
+#include <stdexcept>
+
+#define BOOST_TEST_MODULE "mlk-map"
+#include <boost/test/unit_test.hpp>
+
+#include <boost/endian/conversion.hpp>
+
+#include <json.hpp>
+
+using namespace nlohmann;
+
+namespace {
+
+const std::vector<std::uint32_t> true_background{
+    1,  2,  3,  4,  5,  6,  7, 8, 9, 10,
+    1,  1,  1,  1,  1,  1,  1, 1, 1, 1,
+    1,  1,  1,  1,  1,  1,  1, 1, 1, 1,
+    1,  1,  1,  1,  1,  1,  1, 1, 1, 1,
+    1,  1,  1,  1,  1,  1,  1, 1, 1, 1,
+    1,  1,  1,  1,  1,  1,  1, 1, 1, 1,
+    1,  1,  1,  1,  1,  1,  1, 1, 1, 1,
+    1,  1,  1,  1,  1,  1,  1, 1, 1, 1,
+    1,  1,  1,  1,  1,  1,  1, 1, 1, 1,
+    11, 12, 13, 14, 15, 16, 1, 1, 1, 1
+};
+
+std::vector<std::uint32_t> load(const std::string& file, int total, bool back)
+{
+    std::vector<std::uint32_t> result;
+    std::ifstream in(file, std::ifstream::in | std::ifstream::binary);
+    std::size_t offset = back ? 0 : total / 2 * sizeof (std::uint32_t);
+
+    if (!in) {
+        throw std::runtime_error(std::strerror(errno));
+    }
+
+    result.resize(total);
+    in.read(reinterpret_cast<char*>(&result[offset]), total * sizeof (uint32_t));
+
+    if (!in) {
+        throw std::runtime_error(std::strerror(errno));
+    }
+
+    if (boost::endian::order::native != boost::endian::order::little) {
+        std::transform(result.begin(), result.end(), result.begin(), [] (auto c) {
+            return boost::endian::endian_reverse(c);
+        });
+    }
+
+    return result;
+}
+
+#define TEST_BODY(file, title)                                                      \
+    auto json = json::parse(std::ifstream(BINARY_DIRECTORY "/" file ".json"));      \
+                                                                                    \
+    BOOST_REQUIRE_EQUAL(json["title"].get<std::string>(), title);                   \
+    BOOST_REQUIRE_EQUAL(json["tileset"].get<std::string>(), "minimal.png");         \
+    BOOST_REQUIRE_EQUAL(json["cell"][0].get<int>(), 32);                            \
+    BOOST_REQUIRE_EQUAL(json["cell"][1].get<int>(), 32);                            \
+    BOOST_REQUIRE_EQUAL(json["size"][0].get<int>(), 10);                            \
+    BOOST_REQUIRE_EQUAL(json["size"][1].get<int>(), 10);                            \
+                                                                                    \
+    auto total = json["size"][0].get<int>() * json["size"][1].get<int>();           \
+    auto back = load(BINARY_DIRECTORY "/" file ".map", total, true);                \
+                                                                                    \
+    BOOST_REQUIRE_EQUAL_COLLECTIONS(back.begin(), back.end(),                       \
+        true_background.begin(), true_background.end());
+
+} // !namespace
+
+BOOST_AUTO_TEST_SUITE(maps)
+
+BOOST_AUTO_TEST_CASE(b64)
+{
+    TEST_BODY("minimal-b64", "minimal b64 map");
+}
+
+BOOST_AUTO_TEST_CASE(b64_gzip)
+{
+    TEST_BODY("minimal-b64-gzip", "minimal b64 gzip map");
+}
+
+BOOST_AUTO_TEST_CASE(b64_zlib)
+{
+    TEST_BODY("minimal-b64-zlib", "minimal b64 zlib map");
+}
+
+BOOST_AUTO_TEST_CASE(csv)
+{
+    TEST_BODY("minimal-csv", "minimal csv map")
+}
+
+BOOST_AUTO_TEST_CASE(xml)
+{
+    TEST_BODY("minimal-xml", "minimal xml map");
+}
+
+BOOST_AUTO_TEST_SUITE_END()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tools/map/minimal-b64-gzip.tmx	Sun Feb 05 20:03:59 2017 +0100
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<map version="1.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" nextobjectid="1">
+ <properties>
+  <property name="title" value="minimal b64 gzip map"/>
+ </properties>
+ <tileset firstgid="1" name="minimal" tilewidth="32" tileheight="32" tilecount="16" columns="4">
+  <image source="minimal.png" width="128" height="128"/>
+ </tileset>
+ <layer name="background" width="10" height="10">
+  <data encoding="base64" compression="gzip">
+   H4sIAAAAAAAAA+3KpwEAIAwAwYQWOuw/LT8ADos4dyoiDh4BEQmGjIIK/Z40dAxMLOzLO18ZBoCQAQAA
+  </data>
+ </layer>
+</map>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tools/map/minimal-b64-zlib.tmx	Sun Feb 05 20:03:59 2017 +0100
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<map version="1.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" nextobjectid="1">
+ <properties>
+  <property name="title" value="minimal b64 zlib map"/>
+ </properties>
+ <tileset firstgid="1" name="minimal" tilewidth="32" tileheight="32" tilecount="16" columns="4">
+  <image source="minimal.png" width="128" height="128"/>
+ </tileset>
+ <layer name="background" width="10" height="10">
+  <data encoding="base64" compression="zlib">
+   eJztyqcBACAMAMGEFjrsPy0/AA6LOHcqIg4eAREJhoyCCv2eNHQMTCzsyzua2ADd
+  </data>
+ </layer>
+</map>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tools/map/minimal-b64.tmx	Sun Feb 05 20:03:59 2017 +0100
@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<map version="1.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" nextobjectid="1">
+ <properties>
+  <property name="title" value="minimal b64 map"/>
+ </properties>
+ <tileset firstgid="1" name="minimal" tilewidth="32" tileheight="32" tilecount="16" columns="4">
+  <image source="minimal.png" width="128" height="128"/>
+ </tileset>
+ <layer name="background" width="10" height="10">
+  <data encoding="base64">
+   AQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAAAQAAAAEAAAABAAAACwAAAAwAAAANAAAADgAAAA8AAAAQAAAAAQAAAAEAAAABAAAAAQAAAA==
+  </data>
+ </layer>
+</map>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tools/map/minimal-csv.tmx	Sun Feb 05 20:03:59 2017 +0100
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<map version="1.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" nextobjectid="1">
+ <properties>
+  <property name="title" value="minimal csv map"/>
+ </properties>
+ <tileset firstgid="1" name="minimal" tilewidth="32" tileheight="32" tilecount="16" columns="4">
+  <image source="minimal.png" width="128" height="128"/>
+ </tileset>
+ <layer name="background" width="10" height="10">
+  <data encoding="csv">
+1,2,3,4,5,6,7,8,9,10,
+1,1,1,1,1,1,1,1,1,1,
+1,1,1,1,1,1,1,1,1,1,
+1,1,1,1,1,1,1,1,1,1,
+1,1,1,1,1,1,1,1,1,1,
+1,1,1,1,1,1,1,1,1,1,
+1,1,1,1,1,1,1,1,1,1,
+1,1,1,1,1,1,1,1,1,1,
+1,1,1,1,1,1,1,1,1,1,
+11,12,13,14,15,16,1,1,1,1
+</data>
+ </layer>
+</map>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/tools/map/minimal-xml.tmx	Sun Feb 05 20:03:59 2017 +0100
@@ -0,0 +1,113 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<map version="1.0" orientation="orthogonal" renderorder="right-down" width="10" height="10" tilewidth="32" tileheight="32" nextobjectid="1">
+ <properties>
+  <property name="title" value="minimal xml map"/>
+ </properties>
+ <tileset firstgid="1" name="minimal" tilewidth="32" tileheight="32" tilecount="16" columns="4">
+  <image source="minimal.png" width="128" height="128"/>
+ </tileset>
+ <layer name="background" width="10" height="10">
+  <data>
+   <tile gid="1"/>
+   <tile gid="2"/>
+   <tile gid="3"/>
+   <tile gid="4"/>
+   <tile gid="5"/>
+   <tile gid="6"/>
+   <tile gid="7"/>
+   <tile gid="8"/>
+   <tile gid="9"/>
+   <tile gid="10"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="11"/>
+   <tile gid="12"/>
+   <tile gid="13"/>
+   <tile gid="14"/>
+   <tile gid="15"/>
+   <tile gid="16"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+   <tile gid="1"/>
+  </data>
+ </layer>
+</map>
Binary file tests/tools/map/minimal.png has changed
Binary file tests/tools/map/minimal.xcf has changed
--- a/tools/map/main.cpp	Sat Feb 04 16:15:20 2017 +0100
+++ b/tools/map/main.cpp	Sun Feb 05 20:03:59 2017 +0100
@@ -143,13 +143,12 @@
  *
  * The data is an array of 32 bits unsigned int in the little endian format.
  */
-std::vector<std::uint32_t> load_layer_data_array(const std::string& bytes)
+std::vector<std::uint32_t> load_layer_data_array(std::string bytes)
 {
-    auto ptr = reinterpret_cast<const std::uint32_t*>(&bytes[0]);
-    auto length = bytes.length() / 4;
+    std::uint32_t* array = reinterpret_cast<std::uint32_t*>(&bytes[0]);
     std::vector<std::uint32_t> data;
 
-    for (unsigned i = 0; i < length; ++i) {
+    for (auto ptr = array; ptr != &array[bytes.length() / 4]; ++ptr) {
         data.push_back(*ptr);
     }
 
@@ -177,7 +176,7 @@
     in.push(boost::make_iterator_range(bytes));
     io::copy(in, io::back_inserter(out));
 
-    return load_layer_data_array(out);
+    return load_layer_data_array(std::move(out));
 }
 
 /*
@@ -200,7 +199,7 @@
         auto b64 = base64::decode(boost::trim_copy(tree.get_value<std::string>()));
 
         if (!compression) {
-            data = load_layer_data_array(b64);
+            data = load_layer_data_array(std::move(b64));
         } else if (*compression == "zlib" || *compression == "gzip") {
             data = load_layer_data_array_compressed(b64, *compression);
         }