view tests/libmlk/util/main.cpp @ 193:209bdaa13a49

Tests: rename directories to match targets
author David Demelier <markand@malikania.fr>
date Sat, 27 Oct 2018 07:16:28 +0200
parents tests/libcommon/util/main.cpp@3107ce017c3a
children
line wrap: on
line source

/*
 * main.cpp -- test util
 *
 * Copyright (c) 2013-2018 David Demelier <markand@malikania.fr>
 *
 * 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.
 */

#define BOOST_TEST_MODULE "Util"
#include <boost/test/unit_test.hpp>

#include <malikania/util.hpp>
#include <malikania/size.hpp>

using namespace mlk;
using namespace nlohmann;

namespace nlohmann {

namespace detail {

auto operator<<(std::ostream& out, json::value_t type) -> std::ostream&
{
	out << json(type).type_name();
	return out;
}

} // !detail

} // !nlohmann

/*
 * util::clamp
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(clamp)

BOOST_AUTO_TEST_CASE(normal)
{
	BOOST_REQUIRE_EQUAL(5, util::clamp(5, 0, 10));
}

BOOST_AUTO_TEST_CASE(minimum)
{
	BOOST_REQUIRE_EQUAL(0, util::clamp(0, 0, 10));
}

BOOST_AUTO_TEST_CASE(maximum)
{
	BOOST_REQUIRE_EQUAL(10, util::clamp(10, 0, 10));
}

BOOST_AUTO_TEST_CASE(less)
{
	BOOST_REQUIRE_EQUAL(0, util::clamp(-10, 0, 10));
}

BOOST_AUTO_TEST_CASE(higher)
{
	BOOST_REQUIRE_EQUAL(10, util::clamp(20, 0, 10));
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * util::json::require
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(json_require)

BOOST_AUTO_TEST_CASE(simple)
{
	json object{
		{ "b", true },
		{ "i", 123 },
		{ "s", "blabla" }
	};

	BOOST_REQUIRE_EQUAL(util::json::require(
		object, "/b"_json_pointer, json::value_t::boolean).type(), json::value_t::boolean);
	BOOST_REQUIRE_EQUAL(util::json::require(
		object, "/i"_json_pointer, json::value_t::number_integer).type(), json::value_t::number_integer);
	BOOST_REQUIRE_EQUAL(util::json::require(
		object, "/s"_json_pointer, json::value_t::string).type(), json::value_t::string);
}

BOOST_AUTO_TEST_CASE(nonexistent)
{
	auto json = json::object();

	try {
		util::json::require(json, "/non-existent"_json_pointer, json::value_t::string);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::null);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string);
	}
}

BOOST_AUTO_TEST_CASE(invalid)
{
	json object{
		{ "not-string", 123 }
	};

	try {
		util::json::require(object, "/not-string"_json_pointer, json::value_t::string);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string);
	}
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * util::json::require_array
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(json_require_array)

BOOST_AUTO_TEST_CASE(simple)
{
	json object{
		{ "a", { 1, 2, 3 } },
		{ "l1", {
				{ "l2", { 4, 5, 6 } }
			}
		}
	};

	auto a = util::json::require_array(object, "/a"_json_pointer);
	auto l2 = util::json::require_array(object, "/l1/l2"_json_pointer);

	BOOST_REQUIRE(a.is_array());
	BOOST_REQUIRE(l2.is_array());
}

BOOST_AUTO_TEST_CASE(invalid)
{
	json object{
		{ "not-array", 123 }
	};

	try {
		util::json::require_array(object, "/not-array"_json_pointer);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::array);
	}
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * util::json::require_bool
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(json_require_bool)

BOOST_AUTO_TEST_CASE(simple)
{
	json object{
		{ "b", true }
	};

	BOOST_REQUIRE_EQUAL(util::json::require_bool(object, "/b"_json_pointer), true);
}

BOOST_AUTO_TEST_CASE(invalid)
{
	json object{
		{ "not-bool", 123 }
	};

	try {
		util::json::require_bool(object, "/not-bool"_json_pointer);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::boolean);
	}
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * util::json::require_int
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(json_require_int)

BOOST_AUTO_TEST_CASE(simple)
{
	json object{
		{ "i", 123 }
	};

	BOOST_REQUIRE_EQUAL(util::json::require_int(object, "/i"_json_pointer), 123);
}

BOOST_AUTO_TEST_CASE(invalid)
{
	json object{
		{ "not-int", true }
	};

	try {
		util::json::require_int(object, "/not-int"_json_pointer);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::boolean);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_integer);
	}
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * util::json::require_object
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(json_require_object)

BOOST_AUTO_TEST_CASE(simple)
{
	json object{
		{
			"network", json::object({
				{ "host", "localhost" },
				{ "port", 9090 }
			})
		}
	};

	BOOST_REQUIRE(util::json::require_object(object, "/network"_json_pointer).is_object());
}

BOOST_AUTO_TEST_CASE(invalid)
{
	json object{
		{ "not-object", 123 }
	};

	try {
		util::json::require_object(object, "/not-object"_json_pointer);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::object);
	}
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * util::json::require_string
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(json_require_string)

BOOST_AUTO_TEST_CASE(simple)
{
	json object{
		{ "s", "hello" }
	};

	BOOST_REQUIRE_EQUAL(util::json::require_string(object, "/s"_json_pointer), "hello");
}

BOOST_AUTO_TEST_CASE(invalid)
{
	json object{
		{ "not-string", 123 }
	};

	try {
		util::json::require_string(object, "/not-string"_json_pointer);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::number_integer);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::string);
	}
}

BOOST_AUTO_TEST_SUITE_END()

/*
 * util::json::require_uint
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_SUITE(json_require_uint)

BOOST_AUTO_TEST_CASE(simple)
{
	json object{
		{ "u1", 123U }
	};

	BOOST_REQUIRE_EQUAL(util::json::require_uint(object, "/u1"_json_pointer), 123U);
}

BOOST_AUTO_TEST_CASE(invalid)
{
	json object{
		{ "not-uint", true }
	};

	try {
		util::json::require_uint(object, "/not-uint"_json_pointer);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::boolean);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
	}
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(json_require_size)

/*
 * util::json::require_size
 * ------------------------------------------------------------------
 */

BOOST_AUTO_TEST_CASE(simple)
{
	json object{
		{ "size", {
				{ "width", 10 },
				{ "height", 20 }
			}
		}
	};

	BOOST_TEST((util::json::require_size(object, "/size"_json_pointer) == mlk::size{10, 20}));
}

BOOST_AUTO_TEST_CASE(not_object)
{
	json object{
		{ "size", false }
	};

	try {
		util::json::require_size(object, "/size"_json_pointer);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::null);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
	}
}

BOOST_AUTO_TEST_CASE(not_int_width)
{
	json object{
		{ "size", {
				{ "width", "10" },
				{ "height", 20 }
			}
		}
	};

	try {
		util::json::require_size(object, "/size"_json_pointer);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::string);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
	}
}

BOOST_AUTO_TEST_CASE(not_int_height)
{
	json object{
		{ "size", {
				{ "width", 10 },
				{ "height", "20" }
			}
		}
	};

	try {
		util::json::require_size(object, "/size"_json_pointer);
		BOOST_FAIL("exception expected");
	} catch (const util::json::property_error& ex) {
		BOOST_REQUIRE_EQUAL(ex.type(), json::value_t::string);
		BOOST_REQUIRE_EQUAL(ex.expected(), json::value_t::number_unsigned);
	}
}

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(json_get_size)

BOOST_AUTO_TEST_CASE(simple)
{
	json object{
		{ "size", {
				{ "width", 10 },
				{ "height", 20 }
			}
		}
	};

	BOOST_TEST((util::json::get_size(object, "/size"_json_pointer) == mlk::size{10, 20}));
}

BOOST_AUTO_TEST_CASE(not_object)
{
	const json object{{ "size", false }};
	const mlk::size def{10, 20};

	BOOST_TEST(util::json::get_size(object, "/size"_json_pointer, def) == def);
}

BOOST_AUTO_TEST_CASE(not_int_width)
{
	const json object{
		{ "size", {
				{ "width", "10" },
				{ "height", 20 }
			}
		}
	};

	const mlk::size def{10, 20};

	BOOST_TEST(util::json::get_size(object, "/size"_json_pointer, def) == def);
}

BOOST_AUTO_TEST_CASE(not_int_height)
{
	const json object{
		{ "size", {
				{ "width", 10 },
				{ "height", "20" }
			}
		}
	};

	const mlk::size def{10, 20};

	BOOST_TEST(util::json::get_size(object, "/size"_json_pointer, def) == def);
}

BOOST_AUTO_TEST_SUITE_END()