mapnik::value - always upcast to the higher definition numeric value (rhs or lhs) in operator== and operator!=

add unit test for comparing numeric mapnik::values
This commit is contained in:
artemp 2015-09-24 17:35:17 +01:00
parent 499a2bdd74
commit 8c2f15c94a
2 changed files with 60 additions and 12 deletions

View file

@ -72,16 +72,15 @@ using value_base = util::variant<value_null, value_bool, value_integer,value_dou
namespace impl {
struct equals
{
bool operator() (value_integer lhs, value_double rhs) const
{
return lhs == static_cast<value_integer>(rhs);
return static_cast<value_double>(lhs) == rhs;
}
bool operator() (value_bool lhs, value_double rhs) const
{
return lhs == static_cast<value_bool>(rhs);
return static_cast<value_double>(lhs) == rhs;
}
bool operator() (value_double lhs, value_integer rhs) const
@ -91,7 +90,7 @@ struct equals
bool operator() (value_bool lhs, value_integer rhs) const
{
return lhs == static_cast<value_bool>(rhs);
return static_cast<value_integer>(lhs) == rhs;
}
bool operator() (value_integer lhs, value_bool rhs) const
@ -101,7 +100,7 @@ struct equals
bool operator() (value_double lhs, value_bool rhs) const
{
return lhs == static_cast<value_double>(rhs);
return static_cast<value_double>(lhs) == rhs;
}
bool operator() (value_unicode_string const& lhs,
@ -140,12 +139,12 @@ struct not_equals
bool operator() (value_integer lhs, value_double rhs) const
{
return lhs != rhs;
return static_cast<value_double>(lhs) != rhs;
}
bool operator() (value_bool lhs, value_double rhs) const
{
return lhs != static_cast<value_bool>(rhs);
return static_cast<value_double>(lhs) != rhs;
}
bool operator() (value_double lhs, value_integer rhs) const
@ -155,7 +154,7 @@ struct not_equals
bool operator() (value_bool lhs, value_integer rhs) const
{
return lhs != static_cast<value_bool>(rhs);
return static_cast<value_integer>(lhs) != rhs;
}
bool operator() (value_integer lhs, value_bool rhs) const

View file

@ -0,0 +1,49 @@
#include "catch.hpp"
#include <mapnik/value_types.hpp>
#include <mapnik/value.hpp>
#include <mapnik/unicode.hpp>
TEST_CASE("comparison")
{
SECTION("operator==()")
{
mapnik::value v0 = 1; // mapnik::value_integer
mapnik::value v1 = 1.001; // mapnik::value_double
mapnik::value v2 = true; // mapnik::value_boolean
REQUIRE(!(v0 == v1));
REQUIRE(!(v1 == v0));
REQUIRE(!(v1 == v2));
REQUIRE(!(v2 == v1));
REQUIRE(v2 == v0);
REQUIRE(v0 == v2);
}
SECTION("operator!=()")
{
mapnik::value v0 = 1; // mapnik::value_integer
mapnik::value v1 = 1.001; // mapnik::value_double
mapnik::value v2 = true; // mapnik::value_boolean
mapnik::value v3 = mapnik::value_null(); //
REQUIRE(v0 != v1);
REQUIRE(v1 != v0);
REQUIRE(v1 != v2);
REQUIRE(v2 != v1);
REQUIRE(!(v2 != v0));
REQUIRE(!(v0 != v2));
REQUIRE(v3 != v0);
REQUIRE(v3 != v1);
REQUIRE(v3 != v2);
REQUIRE(v0 != v3);
REQUIRE(v1 != v3);
REQUIRE(v2 != v3);
}
}