From 7d16824fda7086fbeb0a1deae3fc0abdf0c4832d Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Wed, 30 Nov 2016 10:17:47 -0800 Subject: [PATCH] [core] Functions must have at least one stop This is already enforced by the style validator in mapbox-gl-style-spec. Enforce it here too. --- include/mbgl/style/conversion/function.hpp | 4 +++ include/mbgl/style/function.hpp | 6 +++-- src/mbgl/style/property_evaluator.cpp | 27 ++----------------- .../style_parser/stop-zoom-value.info.json | 1 + test/style/functions.test.cpp | 6 ----- 5 files changed, 11 insertions(+), 33 deletions(-) diff --git a/include/mbgl/style/conversion/function.hpp b/include/mbgl/style/conversion/function.hpp index f14b5089b..6a0b67618 100644 --- a/include/mbgl/style/conversion/function.hpp +++ b/include/mbgl/style/conversion/function.hpp @@ -25,6 +25,10 @@ struct Converter> { return Error { "function stops must be an array" }; } + if (arrayLength(*stopsValue) == 0) { + return Error { "function must have at least one stop" }; + } + std::vector> stops; for (std::size_t i = 0; i < arrayLength(*stopsValue); ++i) { const auto& stopValue = arrayMember(*stopsValue, i); diff --git a/include/mbgl/style/function.hpp b/include/mbgl/style/function.hpp index 97e880b28..3aefba660 100644 --- a/include/mbgl/style/function.hpp +++ b/include/mbgl/style/function.hpp @@ -12,8 +12,10 @@ public: using Stop = std::pair; using Stops = std::vector; - explicit Function(Stops stops_, float base_) - : base(base_), stops(std::move(stops_)) {} + Function(Stops stops_, float base_) + : base(base_), stops(std::move(stops_)) { + assert(stops.size() > 0); + } float getBase() const { return base; } const std::vector>& getStops() const { return stops; } diff --git a/src/mbgl/style/property_evaluator.cpp b/src/mbgl/style/property_evaluator.cpp index c19f72210..bfebc3872 100644 --- a/src/mbgl/style/property_evaluator.cpp +++ b/src/mbgl/style/property_evaluator.cpp @@ -10,30 +10,6 @@ namespace mbgl { namespace style { -template -T defaultStopsValue(); - -template <> bool defaultStopsValue() { return true; } -template <> float defaultStopsValue() { return 1.0f; } -template <> Color defaultStopsValue() { return { 0, 0, 0, 1 }; } -template <> std::vector defaultStopsValue() { return {{ 1, 0 }}; } -template <> std::vector defaultStopsValue() { return {{}}; } -template <> std::array defaultStopsValue() { return {{ 0, 0 }}; } -template <> std::array defaultStopsValue() { return {{ 0, 0, 0, 0 }}; } - -template <> std::string defaultStopsValue() { return {}; } -template <> TranslateAnchorType defaultStopsValue() { return {}; } -template <> RotateAnchorType defaultStopsValue() { return {}; } -template <> CirclePitchScaleType defaultStopsValue() { return {}; } -template <> LineCapType defaultStopsValue() { return {}; } -template <> LineJoinType defaultStopsValue() { return {}; } -template <> SymbolPlacementType defaultStopsValue() { return {}; } -template <> TextAnchorType defaultStopsValue() { return {}; } -template <> TextJustifyType defaultStopsValue() { return {}; } -template <> TextTransformType defaultStopsValue() { return {}; } -template <> AlignmentType defaultStopsValue() { return {}; } -template <> IconTextFitType defaultStopsValue() { return {}; }; - template T PropertyEvaluator::operator()(const Function& fn) const { float base = fn.getBase(); @@ -80,7 +56,8 @@ T PropertyEvaluator::operator()(const Function& fn) const { return smaller_val; } else { // No stop defined. - return defaultStopsValue(); + assert(false); + return T(); } } diff --git a/test/fixtures/style_parser/stop-zoom-value.info.json b/test/fixtures/style_parser/stop-zoom-value.info.json index 386dce9d2..39d7c6849 100644 --- a/test/fixtures/style_parser/stop-zoom-value.info.json +++ b/test/fixtures/style_parser/stop-zoom-value.info.json @@ -1,6 +1,7 @@ { "default": { "log": [ + [1, "WARNING", "ParseStyle", "function must have at least one stop"], [1, "WARNING", "ParseStyle", "function stop must have two elements"] ] } diff --git a/test/style/functions.test.cpp b/test/style/functions.test.cpp index 8ca2f951e..c50787814 100644 --- a/test/style/functions.test.cpp +++ b/test/style/functions.test.cpp @@ -50,12 +50,6 @@ TEST(Function, Stops) { EXPECT_EQ(3.0, evaluate(slope_2, 15)); EXPECT_EQ(3.0, evaluate(slope_2, 22)); - // Test no values. - Function slope_3({}, 1.75); - EXPECT_EQ(1, evaluate(slope_3, 2)); - EXPECT_EQ(1, evaluate(slope_3, 6)); - EXPECT_EQ(1, evaluate(slope_3, 12)); - // Explicit constant slope in fringe regions. Function slope_4({ { 0, 2 }, { 8, 10 } }, 1);