From 2893ee4551052da3aba1a54c63e0883ad6601855 Mon Sep 17 00:00:00 2001 From: artemp Date: Tue, 3 Nov 2015 10:10:24 +0000 Subject: [PATCH] mapnik::value - add missing specialisations for mapnik::value_bool in comparison operators + update unit tests --- include/mapnik/value.hpp | 112 ++++++++++++++++++++++++----- test/unit/core/comparison_test.cpp | 54 ++++++++++++++ 2 files changed, 149 insertions(+), 17 deletions(-) diff --git a/include/mapnik/value.hpp b/include/mapnik/value.hpp index a1b635368..acbbc5e5c 100644 --- a/include/mapnik/value.hpp +++ b/include/mapnik/value.hpp @@ -137,12 +137,17 @@ struct not_equals return lhs != rhs; } - bool operator() (value_integer lhs, value_double rhs) const + bool operator() (value_bool lhs, value_double rhs) const { return static_cast(lhs) != rhs; } - bool operator() (value_bool lhs, value_double rhs) const + bool operator() (value_bool lhs, value_integer rhs) const + { + return static_cast(lhs) != rhs; + } + + bool operator() (value_integer lhs, value_double rhs) const { return static_cast(lhs) != rhs; } @@ -152,11 +157,6 @@ struct not_equals return lhs != static_cast(rhs); } - bool operator() (value_bool lhs, value_integer rhs) const - { - return static_cast(lhs) != rhs; - } - bool operator() (value_integer lhs, value_bool rhs) const { return lhs != static_cast(rhs); @@ -185,7 +185,6 @@ struct not_equals }; struct greater_than - { template bool operator()(const T &, const U &) const @@ -199,14 +198,34 @@ struct greater_than return lhs > rhs; } + bool operator() (value_bool lhs, value_double rhs) const + { + return static_cast(lhs) > rhs; + } + + bool operator() (value_double lhs, value_bool rhs) const + { + return lhs > static_cast(rhs); + } + + bool operator() (value_bool lhs, value_integer rhs) const + { + return static_cast(lhs) > rhs; + } + + bool operator() (value_integer lhs, value_bool rhs) const + { + return lhs > static_cast(rhs); + } + bool operator() (value_integer lhs, value_double rhs) const { - return lhs > rhs; + return static_cast(lhs) > rhs; } bool operator() (value_double lhs, value_integer rhs) const { - return lhs > rhs; + return static_cast(lhs) > rhs; } bool operator() (value_unicode_string const& lhs, value_unicode_string const& rhs) const @@ -221,7 +240,6 @@ struct greater_than }; struct greater_or_equal - { template bool operator()(const T &, const U &) const @@ -235,14 +253,34 @@ struct greater_or_equal return lhs >= rhs; } + bool operator() (value_bool lhs, value_double rhs) const + { + return static_cast(lhs) >= rhs; + } + + bool operator() (value_double lhs, value_bool rhs) const + { + return lhs >= static_cast(rhs); + } + + bool operator() (value_bool lhs, value_integer rhs) const + { + return static_cast(lhs) >= rhs; + } + + bool operator() (value_integer lhs, value_bool rhs) const + { + return lhs >= static_cast(rhs); + } + bool operator() (value_integer lhs, value_double rhs) const { - return lhs >= rhs; + return static_cast(lhs) >= rhs; } bool operator() (value_double lhs, value_integer rhs) const { - return lhs >= rhs; + return lhs >= static_cast(rhs); } bool operator() (value_unicode_string const& lhs, value_unicode_string const& rhs) const @@ -271,14 +309,34 @@ struct less_than return lhs < rhs; } + bool operator() (value_bool lhs, value_double rhs) const + { + return static_cast(lhs) < rhs; + } + + bool operator() (value_double lhs, value_bool rhs) const + { + return lhs < static_cast(rhs); + } + + bool operator() (value_bool lhs, value_integer rhs) const + { + return static_cast(lhs) < rhs; + } + + bool operator() (value_integer lhs, value_bool rhs) const + { + return lhs < static_cast(rhs); + } + bool operator() (value_integer lhs, value_double rhs) const { - return lhs < rhs; + return static_cast(lhs) < rhs; } bool operator() (value_double lhs, value_integer rhs) const { - return lhs < rhs; + return lhs < static_cast(rhs); } bool operator()(value_unicode_string const& lhs, @@ -308,14 +366,34 @@ struct less_or_equal return lhs <= rhs; } + bool operator() (value_bool lhs, value_double rhs) const + { + return static_cast(lhs) <= rhs; + } + + bool operator() (value_double lhs, value_bool rhs) const + { + return lhs <= static_cast(rhs); + } + + bool operator() (value_bool lhs, value_integer rhs) const + { + return static_cast(lhs) <= rhs; + } + + bool operator() (value_integer lhs, value_bool rhs) const + { + return lhs <= static_cast(rhs); + } + bool operator() (value_integer lhs, value_double rhs) const { - return lhs <= rhs; + return static_cast(lhs) <= rhs; } bool operator() (value_double lhs, value_integer rhs) const { - return lhs <= rhs; + return lhs <= static_cast(rhs); } bool operator()(value_unicode_string const& lhs, diff --git a/test/unit/core/comparison_test.cpp b/test/unit/core/comparison_test.cpp index 4916ae047..e8662fabe 100644 --- a/test/unit/core/comparison_test.cpp +++ b/test/unit/core/comparison_test.cpp @@ -45,4 +45,58 @@ TEST_CASE("comparison") REQUIRE(v2 != v3); } + SECTION("operator<,<=,>,>=") + { + mapnik::value v0 = 1; + mapnik::value v1 = 1.01; + mapnik::value v2 = true; + mapnik::value v3 = 2; + // value_integer | value_double + // 1 < 1.01 => true + // 1.01 > 1 => true + REQUIRE(v0 < v1); + REQUIRE(v1 > v0); + // 1 <= 1.01 => true + // 1.01 >= 1 => true + REQUIRE(v0 <= v1); + REQUIRE(v1 >= v0); + + // value_bool | value_integer + // true < 1 => false + // true > 1 => false + REQUIRE(!(v2 < v0)); + REQUIRE(!(v2 > v0)); + // true <= 1 => true + // true >= 1 => true + REQUIRE(v2 <= v0); + REQUIRE(v2 >= v0); + // 1 > true => false + // 1 < true => false + REQUIRE(!(v0 > v2)); + REQUIRE(!(v0 < v2)); + // 1 >= true => true + // 1 <= true => true + REQUIRE(v0 >= v2); + REQUIRE(v0 <= v2); + + // value_bool | value_doble + // true < 1.01 => true + // 1.01 > true => true + REQUIRE(v2 < v1); + REQUIRE(v1 > v2); + // true <= 1.01 => true + // 1.01 >= true => true + REQUIRE(v2 <= v1); + REQUIRE(v1 >= v2); + + // value_integer | value_integer + // 1 < 2 => true + // 2 > 1 => true + REQUIRE(v0 < v3); + REQUIRE(v3 > v0); + // 1 <= 2 => true + // 2 >= 1 => true + REQUIRE(v0 <= v3); + REQUIRE(v3 >= v0); + } }