diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-07-16 14:38:46 -0700 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-07-16 14:39:19 -0700 |
commit | 17b1c6fd414c997720765e96008cddfa3ba1c607 (patch) | |
tree | 54a82474d46568674a783785d8f3c3ee3d66a376 | |
parent | 3adad61b82ed140195756a5fce6717d0c63e79d9 (diff) | |
download | qtlocation-mapboxgl-17b1c6fd414c997720765e96008cddfa3ba1c607.tar.gz |
update value semanatics to be closer to js and add filter expression tests
-rw-r--r-- | include/llmr/style/filter_expression.hpp | 6 | ||||
-rw-r--r-- | include/llmr/style/filter_expression_private.hpp | 87 | ||||
-rw-r--r-- | include/llmr/style/value.hpp | 130 | ||||
-rw-r--r-- | include/llmr/style/value_comparison.hpp | 100 | ||||
-rw-r--r-- | src/style/filter_expression.cpp | 87 | ||||
-rw-r--r-- | test/expressions.cpp | 130 | ||||
-rw-r--r-- | test/test.gyp | 17 | ||||
-rw-r--r-- | test/variant.cpp | 189 |
8 files changed, 495 insertions, 251 deletions
diff --git a/include/llmr/style/filter_expression.hpp b/include/llmr/style/filter_expression.hpp index 5c7a400d80..61e0fae3c8 100644 --- a/include/llmr/style/filter_expression.hpp +++ b/include/llmr/style/filter_expression.hpp @@ -11,8 +11,6 @@ namespace llmr { -class VectorTileTagExtractor; - class FilterComparison { public: enum class Operator : uint8_t { @@ -50,7 +48,7 @@ public: FilterComparison(const std::string &field) : field(field) {}; const std::string &getField() const; - inline bool compare(const VectorTileTagExtractor &extractor) const; + template <typename Extractor> inline bool compare(const Extractor &extractor) const; template <typename ...Args> inline void add(Args&& ...args) { @@ -95,7 +93,7 @@ public: bool empty() const; - bool compare(const VectorTileTagExtractor &extractor) const; + template <typename Extractor> bool compare(const Extractor &extractor) const; void add(const FilterComparison &comparison); void add(const FilterExpression &expression); void setGeometryType(GeometryType g); diff --git a/include/llmr/style/filter_expression_private.hpp b/include/llmr/style/filter_expression_private.hpp new file mode 100644 index 0000000000..9a1f9fed30 --- /dev/null +++ b/include/llmr/style/filter_expression_private.hpp @@ -0,0 +1,87 @@ +#ifndef LLMR_STYLE_FILTER_EXPRESSION_PRIVATE +#define LLMR_STYLE_FILTER_EXPRESSION_PRIVATE + +#include "filter_expression.hpp" + +namespace llmr { + +template <typename Extractor> +inline bool FilterComparison::compare(const Extractor &extractor) const { + const std::forward_list<Value> values = extractor.getValues(field); + + // All instances are ANDed together. + for (const Instance &instance : instances) { + if (!instance.compare(values)) { + return false; + } + } + return true; +} + +template <typename Extractor> +bool FilterExpression::compare(const Extractor &extractor) const { + if (type != GeometryType::Any && extractor.getType() != type && extractor.getType() != GeometryType::Any) { + return false; + } + + switch (op) { + case Operator::And: + for (const FilterComparison &comparison : comparisons) { + if (!comparison.compare(extractor)) { + return false; + } + } + for (const FilterExpression &expression: expressions) { + if (!expression.compare(extractor)) { + return false; + } + } + return true; + case Operator::Or: + for (const FilterComparison &comparison : comparisons) { + if (comparison.compare(extractor)) { + return true; + } + } + for (const FilterExpression &expression: expressions) { + if (expression.compare(extractor)) { + return true; + } + } + return false; + case Operator::Xor: { + int count = 0; + for (const FilterComparison &comparison : comparisons) { + count += comparison.compare(extractor); + if (count > 1) { + return false; + } + } + for (const FilterExpression &expression: expressions) { + count += expression.compare(extractor); + if (count > 1) { + return false; + } + } + return count == 1; + } + case Operator::Nor: + for (const FilterComparison &comparison : comparisons) { + if (comparison.compare(extractor)) { + return false; + } + } + for (const FilterExpression &expression: expressions) { + if (expression.compare(extractor)) { + return false; + } + } + return true; + default: + return true; + } +} + +} + +#endif diff --git a/include/llmr/style/value.hpp b/include/llmr/style/value.hpp index fac316d3d5..ed14c233e1 100644 --- a/include/llmr/style/value.hpp +++ b/include/llmr/style/value.hpp @@ -4,6 +4,9 @@ #include <llmr/util/variant.hpp> #include <llmr/util/pbf.hpp> +#include <cstdlib> +#include <cerrno> + namespace llmr { typedef util::variant<bool, int64_t, uint64_t, double, std::string> Value; @@ -13,128 +16,21 @@ std::string toString(const Value &value); Value parseValue(pbf data); namespace util { - -namespace detail { - -inline int string_to_bool(std::string str) { - std::transform(str.begin(), str.end(), str.begin(), ::tolower); - if (str == "0" || str == "0.0" || str == "false" || str == "null" || !str.length()) return 0; - else if (str == "true") return 1; - else return -1; +inline bool parseNumericString(const std::string &str, double &result) { + char *end = nullptr; + const char *begin = str.c_str(); + result = std::strtod(begin, &end); + while (*end != '\0' && isspace(*end)) end++; // eat whitespace after the end + return errno == 0 && end - begin == long(str.size()); } - -template <typename T> -struct string_to_number {}; - -template <> struct string_to_number<bool> { - bool operator()(const std::string &str) const { - // Converts 0 => false, 1 => true and -1 => true. - return string_to_bool(str); - } -}; - -template <> struct string_to_number<double> { - double operator()(std::string const &str) const { - int val = string_to_bool(str); - return val < 0 ? std::stod(str) : val; - } -}; - -template <> struct string_to_number<int64_t> { - int64_t operator()(std::string const &str) const { - int val = string_to_bool(str); - return val < 0 ? std::stoll(str) : val; - } -}; - -template <> struct string_to_number<uint64_t> { - uint64_t operator()(std::string const &str) const { - int val = string_to_bool(str); - return val < 0 ? std::stoull(str) : val; - } -}; - -template <typename Operator> -struct relaxed_operator_visitor { - typedef bool result_type; - - bool operator()(bool lhs, bool rhs) const { return Operator()(lhs, rhs); } - - template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(bool lhs, T rhs) const { return Operator()(lhs, bool(rhs)); } - - template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(T lhs, bool rhs) const { return Operator()(bool(lhs), rhs); } - - bool operator()(int64_t lhs, uint64_t rhs) const { return lhs < 0 ? false : Operator()(uint64_t(lhs), rhs); } - bool operator()(uint64_t lhs, int64_t rhs) const { return rhs < 0 ? false : Operator()(lhs, uint64_t(rhs)); } - - template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(const std::string &lhs, T rhs) const try { return Operator()(string_to_number<T>()(lhs), rhs); } - catch(...) { return false; } - - template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(T lhs, const std::string &rhs) const try { return Operator()(lhs, string_to_number<T>()(rhs)); } - catch(...) { return false; } - - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return Operator()(lhs, rhs); } -}; - -struct relaxed_equal_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs == rhs; } -}; - -struct relaxed_greater_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs > rhs; } -}; - -struct relaxed_greater_equal_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs >= rhs; } -}; - -struct relaxed_less_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs < rhs; } -}; - -struct relaxed_less_equal_operator { - template <typename T0, typename T1> - bool operator()(T0 lhs, T1 rhs) const { return lhs <= rhs; } -}; - - -} // end namespace detail - -inline bool relaxed_equal(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_equal_operator>(), lhs, rhs); -} - -inline bool relaxed_greater(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_operator>(), lhs, rhs); } -inline bool relaxed_greater_equal(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_equal_operator>(), lhs, rhs); -} - -inline bool relaxed_less(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_operator>(), lhs, rhs); -} - -inline bool relaxed_less_equal(Value const &lhs, Value const &rhs) { - return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_equal_operator>(), lhs, rhs); -} - -} - - template <typename T> T toNumber(const Value &value) { - if (value.is<std::string>()) return util::detail::string_to_number<T>()(value.get<std::string>()); + if (value.is<std::string>()) { + double val; + return util::parseNumericString(value.get<std::string>(), val) ? val : 0; + } else if (value.is<bool>()) return value.get<bool>(); else if (value.is<int64_t>()) return value.get<int64_t>(); else if (value.is<uint64_t>()) return value.get<uint64_t>(); diff --git a/include/llmr/style/value_comparison.hpp b/include/llmr/style/value_comparison.hpp new file mode 100644 index 0000000000..d69142bc5b --- /dev/null +++ b/include/llmr/style/value_comparison.hpp @@ -0,0 +1,100 @@ +#ifndef LLMR_STYLE_VALUE_COMPARISON +#define LLMR_STYLE_VALUE_COMPARISON + +#include "value.hpp" +#include <cstdlib> +#include <cerrno> + +namespace llmr { + +namespace util { + +namespace detail { + +template <typename Operator> +struct relaxed_operator_visitor { + typedef bool result_type; + + inline bool operator()(bool lhs, bool rhs) const { return Operator()(lhs, rhs); } + + template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> + inline bool operator()(bool lhs, T rhs) const { return Operator()(T(lhs), rhs); } + + template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> + inline bool operator()(T lhs, bool rhs) const { return Operator()(lhs, T(rhs)); } + + inline bool operator()(int64_t lhs, uint64_t rhs) const { + return lhs < 0 ? false : Operator()(uint64_t(lhs), rhs); + } + inline bool operator()(uint64_t lhs, int64_t rhs) const { + return rhs < 0 ? false : Operator()(lhs, uint64_t(rhs)); + } + + template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> + inline bool operator()(const std::string &lhs, T rhs) const { + double value; + return parseNumericString(lhs, value) ? Operator()(value, double(rhs)) : false; + } + + template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> + inline bool operator()(T lhs, const std::string &rhs) const { + double value; + return parseNumericString(rhs, value) ? Operator()(double(lhs), value) : false; + } + + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return Operator()(lhs, rhs); } +}; + +struct relaxed_equal_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs == rhs; } +}; + +struct relaxed_greater_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs > rhs; } +}; + +struct relaxed_greater_equal_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs >= rhs; } +}; + +struct relaxed_less_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs < rhs; } +}; + +struct relaxed_less_equal_operator { + template <typename T0, typename T1> + inline bool operator()(T0 lhs, T1 rhs) const { return lhs <= rhs; } +}; + +} // end namespace detail + +inline bool relaxed_equal(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_equal_operator>(), lhs, rhs); +} + +inline bool relaxed_greater(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_operator>(), lhs, rhs); +} + +inline bool relaxed_greater_equal(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_greater_equal_operator>(), lhs, rhs); +} + +inline bool relaxed_less(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_operator>(), lhs, rhs); +} + +inline bool relaxed_less_equal(Value const &lhs, Value const &rhs) { + return apply_visitor(detail::relaxed_operator_visitor<detail::relaxed_less_equal_operator>(), lhs, rhs); +} + +} + +} + +#endif diff --git a/src/style/filter_expression.cpp b/src/style/filter_expression.cpp index cba7e21f19..38bb742729 100644 --- a/src/style/filter_expression.cpp +++ b/src/style/filter_expression.cpp @@ -1,6 +1,8 @@ -#include <llmr/style/filter_expression.hpp> +#include <llmr/style/filter_expression_private.hpp> #include <llmr/map/vector_tile.hpp> +#include <llmr/style/value_comparison.hpp> + namespace llmr { @@ -105,20 +107,6 @@ const std::string &FilterComparison::getField() const { return field; } -inline bool FilterComparison::compare(const VectorTileTagExtractor &extractor) const { - const std::forward_list<Value> values = extractor.getValues(field); - - // All instances are ANDed together. - for (const Instance &instance : instances) { - if (!instance.compare(values)) { - return false; - } - } - return true; -} - - - std::ostream& operator <<(std::ostream &s, const FilterComparison &comparison) { s << "comparison" << std::endl; for (const FilterComparison::Instance &instance : comparison.instances) { @@ -183,71 +171,6 @@ std::ostream& operator <<(std::ostream &s, FilterExpression::GeometryType type) return s; } - - -bool FilterExpression::compare(const VectorTileTagExtractor &extractor) const { - if (type != GeometryType::Any && extractor.getType() != type && extractor.getType() != GeometryType::Any) { - return false; - } - - switch (op) { - case Operator::And: - for (const FilterComparison &comparison : comparisons) { - if (!comparison.compare(extractor)) { - return false; - } - } - for (const FilterExpression &expression: expressions) { - if (!expression.compare(extractor)) { - return false; - } - } - return true; - case Operator::Or: - for (const FilterComparison &comparison : comparisons) { - if (comparison.compare(extractor)) { - return true; - } - } - for (const FilterExpression &expression: expressions) { - if (expression.compare(extractor)) { - return true; - } - } - return false; - case Operator::Xor: { - int count = 0; - for (const FilterComparison &comparison : comparisons) { - count += comparison.compare(extractor); - if (count > 1) { - return false; - } - } - for (const FilterExpression &expression: expressions) { - count += expression.compare(extractor); - if (count > 1) { - return false; - } - } - return count == 1; - } - case Operator::Nor: - for (const FilterComparison &comparison : comparisons) { - if (comparison.compare(extractor)) { - return false; - } - } - for (const FilterExpression &expression: expressions) { - if (expression.compare(extractor)) { - return false; - } - } - return true; - default: - return true; - } -} - bool FilterExpression::empty() const { return type == GeometryType::Any && comparisons.empty() && expressions.empty(); } @@ -281,4 +204,8 @@ std::ostream& operator <<(std::ostream &s, const FilterExpression &expression) { return s; } +// Explicit instantiation +template bool FilterComparison::compare(const VectorTileTagExtractor &extractor) const; +template bool FilterExpression::compare(const VectorTileTagExtractor &extractor) const; + } diff --git a/test/expressions.cpp b/test/expressions.cpp new file mode 100644 index 0000000000..dc28f16ee2 --- /dev/null +++ b/test/expressions.cpp @@ -0,0 +1,130 @@ +#include <iostream> +#include "gtest/gtest.h" + +#include <llmr/style/filter_expression.hpp> +#include <llmr/style/filter_expression_private.hpp> + +#include <map> + + + +class MockExtractor { +public: + MockExtractor(const std::multimap<std::string, llmr::Value> &values) : values(values) {} + MockExtractor() {} + + inline std::forward_list<llmr::Value> getValues(const std::string &key) const { + std::forward_list<llmr::Value> result; + auto rit = result.before_begin(); + // Find all values with the requested key. + const auto ret = values.equal_range(key); + for (auto it = ret.first; it != ret.second; it++) { + // Append the newly found value to the forward list. + rit = result.emplace_after(rit, it->second); + } + return result; + } + +private: + const std::multimap<std::string, llmr::Value> values; +}; + + +TEST(FilterComparison, SingleStringValue) { + using namespace llmr; + + FilterComparison comparison("test"); + comparison.add(FilterComparison::Operator::Equal, std::forward_list<Value> { std::string("bar") }); + // comparison is { "test": { "==": "bar" } } + + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", std::string("bar") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", std::string("bar") }, { "test", std::string("booz") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "other", std::string("bar") }, { "test", std::string("bar") }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "other", std::string("bar") }}))); + ASSERT_FALSE(comparison.compare(MockExtractor())); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", std::string("barst") }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", int64_t(-18932) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", uint64_t(18932) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", double(32.8) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", bool(false) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", bool(true) }}))); +} + + +TEST(FilterComparison, SingleDoubleValue) { + using namespace llmr; + + FilterComparison comparison("test"); + comparison.add(FilterComparison::Operator::Equal, std::forward_list<Value> { double(32.8) }); + // comparison is { "test": { "==": 32.8 } } + + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", double(32.8) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", std::string("32.8") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", double(32.8) }, { "test", std::string("booz") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "other", std::string("bar") }, { "test", double(32.8) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "other", double(32.8) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor())); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", double(32.9) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", int64_t(-18932) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", uint64_t(18932) }}))); +} + +TEST(FilterComparison, SingleUintValue) { + using namespace llmr; + + FilterComparison comparison("test"); + comparison.add(FilterComparison::Operator::Equal, std::forward_list<Value> { uint64_t(42) }); + // comparison is { "test": { "==": 42 } } + + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", uint64_t(42) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", double(42) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", int64_t(42) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", std::string("42") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", uint64_t(42) }, { "test", std::string("booz") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "other", std::string("bar") }, { "test", uint64_t(42) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "other", uint64_t(42) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor())); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", double(43) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", int64_t(-18932) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", uint64_t(18932) }}))); +} + +TEST(FilterComparison, SingleIntValue) { + using namespace llmr; + + FilterComparison comparison("test"); + comparison.add(FilterComparison::Operator::Equal, std::forward_list<Value> { int64_t(-42) }); + // comparison is { "test": { "==": -42 } } + + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", int64_t(-42) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", double(-42) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", std::string("-42") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", int64_t(-42) }, { "test", std::string("booz") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "other", std::string("bar") }, { "test", int64_t(-42) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "other", int64_t(-42) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor())); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", int64_t(-43) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", double(-43) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", uint64_t(-42) }}))); +} + +TEST(FilterComparison, SingleBoolValue) { + using namespace llmr; + + FilterComparison comparison("test"); + comparison.add(FilterComparison::Operator::Equal, std::forward_list<Value> { bool(true) }); + // comparison is { "test": { "==": 32.8 } } + + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", bool(true) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", int64_t(1) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", uint64_t(1) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", double(1) }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", std::string("1") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "test", bool(true) }, { "test", std::string("booz") }}))); + ASSERT_TRUE(comparison.compare(MockExtractor({{ "other", std::string("bar") }, { "test", bool(true) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "other", bool(true) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor())); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", bool(false) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", int64_t(-18932) }}))); + ASSERT_FALSE(comparison.compare(MockExtractor({{ "test", uint64_t(18932) }}))); +} diff --git a/test/test.gyp b/test/test.gyp index 95f22c2575..0f065a5beb 100644 --- a/test/test.gyp +++ b/test/test.gyp @@ -123,6 +123,22 @@ ] }, { + "target_name": "expressions", + "product_name": "test_expressions", + "type": "executable", + "libraries": [ + "-lpthread", + ], + "sources": [ + "./main.cpp", + "./expressions.cpp", + ], + "dependencies": [ + "../deps/gtest/gtest.gyp:gtest", + "../llmr.gyp:llmr-x86" + ] + }, + { "target_name": "tile", "product_name": "test_tile", "type": "executable", @@ -188,6 +204,7 @@ "tile", "functions", "headless", + "expressions", ], } ] diff --git a/test/variant.cpp b/test/variant.cpp index 9b0de9f3a6..fd27853696 100644 --- a/test/variant.cpp +++ b/test/variant.cpp @@ -2,6 +2,7 @@ #include "gtest/gtest.h" #include <llmr/style/value.hpp> +#include <llmr/style/value_comparison.hpp> #pragma GCC diagnostic push #ifndef __clang__ @@ -24,40 +25,117 @@ TEST(Variant, toString) { } +TEST(Variant, RelaxedEqualityStringToBool) { + // 1-ish values are true + EXPECT_TRUE(util::relaxed_equal(std::string("1"), bool(true))); + EXPECT_TRUE(util::relaxed_equal(std::string("1.0"), bool(true))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 1"), bool(true))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 0x1"), bool(true))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 0X1"), bool(true))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 1.0"), bool(true))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 1 "), bool(true))); + + EXPECT_TRUE(util::relaxed_equal(bool(true), std::string("1"))); + EXPECT_TRUE(util::relaxed_equal(bool(true), std::string("1.0"))); + EXPECT_TRUE(util::relaxed_equal(bool(true), std::string(" 1"))); + EXPECT_TRUE(util::relaxed_equal(bool(true), std::string(" 0x1"))); + EXPECT_TRUE(util::relaxed_equal(bool(true), std::string(" 0X1"))); + EXPECT_TRUE(util::relaxed_equal(bool(true), std::string(" 1.0"))); + EXPECT_TRUE(util::relaxed_equal(bool(true), std::string(" 1 "))); + + // 0-ish values are false + EXPECT_TRUE(util::relaxed_equal(std::string("0"), bool(false))); + EXPECT_TRUE(util::relaxed_equal(std::string("0.0"), bool(false))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 0"), bool(false))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 0x0"), bool(false))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 0X0"), bool(false))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 0.0"), bool(false))); + EXPECT_TRUE(util::relaxed_equal(std::string(" 0 "), bool(false))); + EXPECT_TRUE(util::relaxed_equal(std::string(""), bool(false))); + EXPECT_TRUE(util::relaxed_equal(std::string(" "), bool(false))); + + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string("0"))); + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string("0.0"))); + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string(" 0"))); + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string(" 0x0"))); + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string(" 0X0"))); + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string(" 0.0"))); + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string(" 0 "))); + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string(""))); + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string(" "))); + + + // other string values are neither true nor false + EXPECT_FALSE(util::relaxed_equal(std::string(" 1.0 g"), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string("foo"), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string("true"), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string("false"), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string(" inf "), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string(" inFINITY "), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string(" nan "), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string(" NAN "), bool(true))); + + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string(" 1.0 g"))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string("foo"))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string("true"))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string("false"))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string(" inf "))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string(" inFINITY "))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string(" nan "))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string(" NAN "))); + + + EXPECT_FALSE(util::relaxed_equal(std::string(" 1.0 g"), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string("foo"), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string("true"), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string("false"), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string(" inf "), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string(" inFINITY "), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string(" nan "), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string(" NAN "), bool(false))); + + EXPECT_FALSE(util::relaxed_equal(bool(false), std::string(" 1.0 g"))); + EXPECT_FALSE(util::relaxed_equal(bool(false), std::string("foo"))); + EXPECT_FALSE(util::relaxed_equal(bool(false), std::string("true"))); + EXPECT_FALSE(util::relaxed_equal(bool(false), std::string("false"))); + EXPECT_FALSE(util::relaxed_equal(bool(false), std::string(" inf "))); + EXPECT_FALSE(util::relaxed_equal(bool(false), std::string(" inFINITY "))); + EXPECT_FALSE(util::relaxed_equal(bool(false), std::string(" nan "))); + EXPECT_FALSE(util::relaxed_equal(bool(false), std::string(" NAN "))); +} + TEST(Variant, RelaxedEquality) { // Compare to bool - EXPECT_TRUE(util::relaxed_equal(bool(true), int64_t(386))); - EXPECT_TRUE(util::relaxed_equal(bool(true), int64_t(-7042))); + EXPECT_TRUE(util::relaxed_equal(bool(false), bool(false))); EXPECT_TRUE(util::relaxed_equal(bool(false), int64_t(0))); - EXPECT_TRUE(util::relaxed_equal(bool(true), uint64_t(386))); EXPECT_TRUE(util::relaxed_equal(bool(false), uint64_t(0))); - EXPECT_TRUE(util::relaxed_equal(bool(true), double(386))); EXPECT_TRUE(util::relaxed_equal(bool(false), double(0))); - EXPECT_TRUE(util::relaxed_equal(bool(true), std::string("386"))); + EXPECT_TRUE(util::relaxed_equal(bool(false), std::string("0"))); EXPECT_TRUE(util::relaxed_equal(bool(false), std::string(""))); - EXPECT_TRUE(util::relaxed_equal(bool(false), std::string("false"))); - EXPECT_TRUE(util::relaxed_equal(bool(false), std::string("False"))); - EXPECT_TRUE(util::relaxed_equal(bool(false), std::string("null"))); - EXPECT_TRUE(util::relaxed_equal(bool(false), std::string("nuLL"))); - EXPECT_TRUE(util::relaxed_equal(bool(false), std::string("NULL"))); + EXPECT_TRUE(util::relaxed_equal(bool(true), bool(true))); - EXPECT_TRUE(util::relaxed_equal(bool(false), bool(false))); + EXPECT_TRUE(util::relaxed_equal(bool(true), int64_t(1))); + EXPECT_TRUE(util::relaxed_equal(bool(true), uint64_t(1))); + EXPECT_TRUE(util::relaxed_equal(bool(true), double(1))); + EXPECT_TRUE(util::relaxed_equal(bool(true), std::string("1"))); - EXPECT_FALSE(util::relaxed_equal(bool(true), int64_t(0))); - EXPECT_FALSE(util::relaxed_equal(bool(true), uint64_t(0))); - EXPECT_FALSE(util::relaxed_equal(bool(true), double(0))); - EXPECT_FALSE(util::relaxed_equal(bool(true), std::string(""))); - EXPECT_FALSE(util::relaxed_equal(bool(true), std::string("false"))); - EXPECT_FALSE(util::relaxed_equal(bool(true), std::string("FALSE"))); - EXPECT_FALSE(util::relaxed_equal(bool(true), std::string("null"))); - EXPECT_FALSE(util::relaxed_equal(bool(true), std::string("Null"))); - EXPECT_FALSE(util::relaxed_equal(bool(false), int64_t(-754))); - EXPECT_FALSE(util::relaxed_equal(bool(false), uint64_t(3))); + + EXPECT_FALSE(util::relaxed_equal(bool(false), bool(true))); + EXPECT_FALSE(util::relaxed_equal(bool(false), int64_t(386))); + EXPECT_FALSE(util::relaxed_equal(bool(false), int64_t(-7042))); + EXPECT_FALSE(util::relaxed_equal(bool(false), uint64_t(386))); EXPECT_FALSE(util::relaxed_equal(bool(false), double(3.145))); + EXPECT_FALSE(util::relaxed_equal(bool(false), std::string("386"))); EXPECT_FALSE(util::relaxed_equal(bool(false), std::string("test"))); - EXPECT_FALSE(util::relaxed_equal(bool(true), bool(false))); - EXPECT_FALSE(util::relaxed_equal(bool(false), bool(true))); + EXPECT_FALSE(util::relaxed_equal(bool(true), bool(false))); + EXPECT_FALSE(util::relaxed_equal(bool(true), int64_t(386))); + EXPECT_FALSE(util::relaxed_equal(bool(true), int64_t(-7042))); + EXPECT_FALSE(util::relaxed_equal(bool(true), uint64_t(386))); + EXPECT_FALSE(util::relaxed_equal(bool(true), double(3.145))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string("386"))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string("test"))); + EXPECT_FALSE(util::relaxed_equal(bool(true), std::string(""))); // Compare to int64_t EXPECT_TRUE(util::relaxed_equal(int64_t(386), int64_t(386))); @@ -67,8 +145,6 @@ TEST(Variant, RelaxedEquality) { EXPECT_TRUE(util::relaxed_equal(int64_t(386), std::string("386"))); EXPECT_TRUE(util::relaxed_equal(int64_t(1), bool(true))); EXPECT_TRUE(util::relaxed_equal(int64_t(0), bool(false))); - EXPECT_TRUE(util::relaxed_equal(int64_t(1), std::string("true"))); - EXPECT_TRUE(util::relaxed_equal(int64_t(0), std::string("false"))); EXPECT_FALSE(util::relaxed_equal(int64_t(386), int64_t(387))); EXPECT_FALSE(util::relaxed_equal(int64_t(-7042), int64_t(-7043))); @@ -77,6 +153,8 @@ TEST(Variant, RelaxedEquality) { EXPECT_FALSE(util::relaxed_equal(int64_t(386), std::string("387"))); EXPECT_FALSE(util::relaxed_equal(int64_t(1), bool(false))); EXPECT_FALSE(util::relaxed_equal(int64_t(0), bool(true))); + EXPECT_FALSE(util::relaxed_equal(int64_t(1), std::string("true"))); + EXPECT_FALSE(util::relaxed_equal(int64_t(0), std::string("false"))); EXPECT_FALSE(util::relaxed_equal(int64_t(1), std::string("false"))); EXPECT_FALSE(util::relaxed_equal(int64_t(0), std::string("true"))); @@ -89,8 +167,6 @@ TEST(Variant, RelaxedEquality) { EXPECT_TRUE(util::relaxed_equal(uint64_t(386), std::string("386"))); EXPECT_TRUE(util::relaxed_equal(uint64_t(1), bool(true))); EXPECT_TRUE(util::relaxed_equal(uint64_t(0), bool(false))); - EXPECT_TRUE(util::relaxed_equal(uint64_t(1), std::string("true"))); - EXPECT_TRUE(util::relaxed_equal(uint64_t(0), std::string("false"))); EXPECT_FALSE(util::relaxed_equal(uint64_t(386), int64_t(387))); EXPECT_FALSE(util::relaxed_equal(uint64_t(386), uint64_t(387))); @@ -99,6 +175,8 @@ TEST(Variant, RelaxedEquality) { EXPECT_FALSE(util::relaxed_equal(uint64_t(386), std::string("387"))); EXPECT_FALSE(util::relaxed_equal(uint64_t(1), bool(false))); EXPECT_FALSE(util::relaxed_equal(uint64_t(0), bool(true))); + EXPECT_FALSE(util::relaxed_equal(uint64_t(1), std::string("true"))); + EXPECT_FALSE(util::relaxed_equal(uint64_t(0), std::string("false"))); EXPECT_FALSE(util::relaxed_equal(uint64_t(1), std::string("false"))); EXPECT_FALSE(util::relaxed_equal(uint64_t(0), std::string("true"))); @@ -112,12 +190,6 @@ TEST(Variant, RelaxedEquality) { EXPECT_TRUE(util::relaxed_equal(double(386), std::string("386"))); EXPECT_TRUE(util::relaxed_equal(double(1), bool(true))); EXPECT_TRUE(util::relaxed_equal(double(0), bool(false))); - EXPECT_TRUE(util::relaxed_equal(double(1), std::string("true"))); - EXPECT_TRUE(util::relaxed_equal(double(1), std::string("TRue"))); - EXPECT_TRUE(util::relaxed_equal(double(0), std::string("false"))); - EXPECT_TRUE(util::relaxed_equal(double(0), std::string("FALSe"))); - EXPECT_TRUE(util::relaxed_equal(double(0), std::string("null"))); - EXPECT_TRUE(util::relaxed_equal(double(0), std::string("Null"))); EXPECT_FALSE(util::relaxed_equal(double(3.159385), double(3.159383))); EXPECT_FALSE(util::relaxed_equal(double(386), int64_t(387))); @@ -127,6 +199,12 @@ TEST(Variant, RelaxedEquality) { EXPECT_FALSE(util::relaxed_equal(double(386), std::string("387"))); EXPECT_FALSE(util::relaxed_equal(double(1), bool(false))); EXPECT_FALSE(util::relaxed_equal(double(0), bool(true))); + EXPECT_FALSE(util::relaxed_equal(double(1), std::string("true"))); + EXPECT_FALSE(util::relaxed_equal(double(1), std::string("TRue"))); + EXPECT_FALSE(util::relaxed_equal(double(0), std::string("false"))); + EXPECT_FALSE(util::relaxed_equal(double(0), std::string("FALSe"))); + EXPECT_FALSE(util::relaxed_equal(double(0), std::string("null"))); + EXPECT_FALSE(util::relaxed_equal(double(0), std::string("Null"))); EXPECT_FALSE(util::relaxed_equal(double(1), std::string("false"))); EXPECT_FALSE(util::relaxed_equal(double(1), std::string("False"))); EXPECT_FALSE(util::relaxed_equal(double(1), std::string("null"))); @@ -141,20 +219,21 @@ TEST(Variant, RelaxedEquality) { EXPECT_TRUE(util::relaxed_equal(std::string("-386"), int64_t(-386))); EXPECT_TRUE(util::relaxed_equal(std::string("386"), uint64_t(386))); EXPECT_TRUE(util::relaxed_equal(std::string("386.36"), double(386.36))); - EXPECT_TRUE(util::relaxed_equal(std::string("true"), bool(true))); - EXPECT_TRUE(util::relaxed_equal(std::string("True"), bool(true))); - EXPECT_TRUE(util::relaxed_equal(std::string("some string"), bool(true))); - EXPECT_TRUE(util::relaxed_equal(std::string("false"), bool(false))); - EXPECT_TRUE(util::relaxed_equal(std::string("fAlse"), bool(false))); - EXPECT_TRUE(util::relaxed_equal(std::string("null"), bool(false))); - EXPECT_TRUE(util::relaxed_equal(std::string("NULl"), bool(false))); EXPECT_TRUE(util::relaxed_equal(std::string(""), bool(false))); + EXPECT_TRUE(util::relaxed_equal(std::string("1"), bool(true))); EXPECT_FALSE(util::relaxed_equal(std::string("lord"), std::string("baron"))); EXPECT_FALSE(util::relaxed_equal(std::string("386"), int64_t(387))); EXPECT_FALSE(util::relaxed_equal(std::string("-386"), int64_t(-387))); EXPECT_FALSE(util::relaxed_equal(std::string("386"), uint64_t(387))); EXPECT_FALSE(util::relaxed_equal(std::string("386.36"), double(386.37))); + EXPECT_FALSE(util::relaxed_equal(std::string("some string"), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string("true"), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string("True"), bool(true))); + EXPECT_FALSE(util::relaxed_equal(std::string("false"), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string("fAlse"), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string("null"), bool(false))); + EXPECT_FALSE(util::relaxed_equal(std::string("NULl"), bool(false))); EXPECT_FALSE(util::relaxed_equal(std::string("true"), bool(false))); EXPECT_FALSE(util::relaxed_equal(std::string("TRUE"), bool(false))); EXPECT_FALSE(util::relaxed_equal(std::string("some string"), bool(false))); @@ -190,11 +269,23 @@ TEST(Variant, RelaxedGreater) { EXPECT_TRUE(util::relaxed_greater(double(387), double(386.7))); EXPECT_TRUE(util::relaxed_greater(bool(true), double(0))); + EXPECT_TRUE(util::relaxed_greater(std::string("387"), bool(true))); EXPECT_TRUE(util::relaxed_greater(std::string("387"), bool(false))); EXPECT_TRUE(util::relaxed_greater(uint64_t(387), bool(false))); EXPECT_TRUE(util::relaxed_greater(int64_t(387), bool(false))); EXPECT_TRUE(util::relaxed_greater(double(387), bool(false))); - EXPECT_TRUE(util::relaxed_greater(bool(true), double(false))); + EXPECT_TRUE(util::relaxed_greater(double(387), bool(false))); + EXPECT_TRUE(util::relaxed_greater(uint64_t(387), bool(true))); + EXPECT_TRUE(util::relaxed_greater(int64_t(387), bool(true))); + EXPECT_TRUE(util::relaxed_greater(double(387), bool(true))); + EXPECT_TRUE(util::relaxed_greater(double(387), bool(true))); + + EXPECT_TRUE(util::relaxed_greater(bool(true), double(0))); + EXPECT_TRUE(util::relaxed_greater(bool(true), double(0.5))); + EXPECT_TRUE(util::relaxed_greater(bool(true), bool(false))); + EXPECT_TRUE(util::relaxed_greater(bool(true), int64_t(0))); + EXPECT_TRUE(util::relaxed_greater(bool(true), uint64_t(0))); + EXPECT_TRUE(util::relaxed_greater(bool(true), bool(false))); @@ -222,12 +313,8 @@ TEST(Variant, RelaxedGreater) { EXPECT_FALSE(util::relaxed_greater(int64_t(387), double(388.7))); EXPECT_FALSE(util::relaxed_greater(double(387), double(388.7))); EXPECT_FALSE(util::relaxed_greater(bool(true), double(388))); - - EXPECT_FALSE(util::relaxed_greater(std::string("387"), bool(true))); - EXPECT_FALSE(util::relaxed_greater(uint64_t(387), bool(true))); - EXPECT_FALSE(util::relaxed_greater(int64_t(387), bool(true))); - EXPECT_FALSE(util::relaxed_greater(double(387), bool(true))); EXPECT_FALSE(util::relaxed_greater(bool(true), bool(true))); + EXPECT_FALSE(util::relaxed_greater(bool(false), bool(false))); } @@ -344,6 +431,11 @@ TEST(Variant, RelaxedLess) { EXPECT_TRUE(util::relaxed_less(double(0), bool(true))); EXPECT_TRUE(util::relaxed_less(bool(false), bool(true))); + EXPECT_TRUE(util::relaxed_less(bool(true), std::string("386"))); + EXPECT_TRUE(util::relaxed_less(bool(true), uint64_t(386))); + EXPECT_TRUE(util::relaxed_less(bool(true), int64_t(386))); + EXPECT_TRUE(util::relaxed_less(bool(true), double(386))); + @@ -351,31 +443,28 @@ TEST(Variant, RelaxedLess) { EXPECT_FALSE(util::relaxed_less(uint64_t(387), std::string("386"))); EXPECT_FALSE(util::relaxed_less(int64_t(387), std::string("386"))); EXPECT_FALSE(util::relaxed_less(double(387), std::string("386"))); - EXPECT_FALSE(util::relaxed_less(bool(true), std::string("386"))); EXPECT_FALSE(util::relaxed_less(std::string("387"), uint64_t(386))); EXPECT_FALSE(util::relaxed_less(uint64_t(387), uint64_t(386))); EXPECT_FALSE(util::relaxed_less(int64_t(387), uint64_t(386))); EXPECT_FALSE(util::relaxed_less(double(387), uint64_t(386))); - EXPECT_FALSE(util::relaxed_less(bool(true), uint64_t(386))); EXPECT_FALSE(util::relaxed_less(std::string("387"), int64_t(386))); EXPECT_FALSE(util::relaxed_less(uint64_t(387), int64_t(386))); EXPECT_FALSE(util::relaxed_less(int64_t(387), int64_t(386))); EXPECT_FALSE(util::relaxed_less(double(387), int64_t(386))); - EXPECT_FALSE(util::relaxed_less(bool(true), int64_t(386))); EXPECT_FALSE(util::relaxed_less(std::string("387"), double(386.7))); EXPECT_FALSE(util::relaxed_less(uint64_t(387), double(386.7))); EXPECT_FALSE(util::relaxed_less(int64_t(387), double(386.7))); EXPECT_FALSE(util::relaxed_less(double(387), double(386.7))); - EXPECT_FALSE(util::relaxed_less(bool(true), double(386))); EXPECT_FALSE(util::relaxed_less(std::string("387"), bool(true))); EXPECT_FALSE(util::relaxed_less(uint64_t(387), bool(true))); EXPECT_FALSE(util::relaxed_less(int64_t(387), bool(true))); EXPECT_FALSE(util::relaxed_less(double(387), bool(true))); EXPECT_FALSE(util::relaxed_less(bool(true), bool(true))); + EXPECT_FALSE(util::relaxed_less(bool(false), bool(false))); } |