view tests/tools/map/main.cpp @ 185:975dffc6567a

Tests: use ptree directly in mlk-map, closes #704 @1h
author David Demelier <markand@malikania.fr>
date Sat, 20 Oct 2018 20:38:43 +0200
parents 3107ce017c3a
children
line wrap: on
line source

/*
 * main.cpp -- test mlk-map
 *
 * Copyright (c) 2013-2018 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
};

auto load(const std::string& file, int total, bool back) -> std::vector<std::uint32_t>
{
	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 path = CMAKE_CURRENT_BINARY_DIR "/" file ".json";                      \
    auto json = json::parse(std::ifstream(path));                               \
                                                                                \
    BOOST_TEST(json["title"].get<std::string>() == title);                      \
    BOOST_TEST(json["tileset"].get<std::string>() == "minimal.png");            \
    BOOST_TEST(json["cell"][0].get<int>() == 32);                               \
    BOOST_TEST(json["cell"][1].get<int>() == 32);                               \
    BOOST_TEST(json["size"][0].get<int>() == 10);                               \
    BOOST_TEST(json["size"][1].get<int>() == 10);                               \
                                                                                \
    auto map = CMAKE_CURRENT_BINARY_DIR "/" file ".map";                        \
    auto total = json["size"][0].get<int>() * json["size"][1].get<int>();       \
    auto back = load(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()