From 97f6ec9bbb3615c9a505e29c76b7ecc6993b6791 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Thu, 16 Oct 2014 01:02:12 +0000 Subject: fixed typeo in RangeSet, added unit test for Variant git-svn-id: file:///var/local/svn/basicwriter@59 daaaf23c-2e50-4459-9457-1e69db5a47bf --- CMakeLists.txt | 1 + src/core/model/RangeSet.hpp | 10 +++---- src/core/script/Variant.hpp | 2 +- test/core/model/RangeSet.cpp | 22 +++++++------- test/core/script/Variant.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+), 17 deletions(-) create mode 100644 test/core/script/Variant.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 8c59356..8fb3ac9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,7 @@ IF(test) # Add all unit test files ADD_EXECUTABLE(ousia_test test/core/model/RangeSet + test/core/script/Variant ) TARGET_LINK_LIBRARIES(ousia_test diff --git a/src/core/model/RangeSet.hpp b/src/core/model/RangeSet.hpp index ef86363..841d476 100644 --- a/src/core/model/RangeSet.hpp +++ b/src/core/model/RangeSet.hpp @@ -97,13 +97,13 @@ struct Range { } /** - * Checks whether the given range overlapps with another range. Not that + * Checks whether the given range overlaps with another range. Not that * this check is only meaningful if both ranges are valid. * * @param r is the range that should be checked for overlapping with this * range. */ - bool overlapps(const Range &r) const + bool overlaps(const Range &r) const { return (((r.start >= start) || (r.end >= start)) && ((r.start <= end) || (r.end <= end))); @@ -208,7 +208,7 @@ protected: std::set, RangeComp> ranges; /** - * Returns an iterator to the first element in the ranges list that overlapps + * Returns an iterator to the first element in the ranges list that overlaps * with the given range. * * @param r is the range for which the first overlapping element should be @@ -229,7 +229,7 @@ protected: } // Iterate until an overlapping element is found - while (!(it->overlapps(r) || (allowNeighbours && it->neighbours(r))) + while (!(it->overlaps(r) || (allowNeighbours && it->neighbours(r))) && (it != ranges.end())) { it++; } @@ -247,7 +247,7 @@ public: // Calculate a new range that covers both the new range and all old // ranges in the set -- delete all old elements on the way auto it = firstOverlapping(r, true); - while ((it->overlapps(r) || it->neighbours(r)) && it != ranges.end()) { + while ((it->overlaps(r) || it->neighbours(r)) && it != ranges.end()) { r = r.merge(*it); it = ranges.erase(it); } diff --git a/src/core/script/Variant.hpp b/src/core/script/Variant.hpp index 208bfa5..b2b650b 100644 --- a/src/core/script/Variant.hpp +++ b/src/core/script/Variant.hpp @@ -233,7 +233,7 @@ public: } } - std::string getStringValue() const + const std::string& getStringValue() const { switch (type) { case VariantType::string: diff --git a/test/core/model/RangeSet.cpp b/test/core/model/RangeSet.cpp index 4f04538..bfc7749 100644 --- a/test/core/model/RangeSet.cpp +++ b/test/core/model/RangeSet.cpp @@ -41,18 +41,18 @@ TEST(Range, InRange) ASSERT_TRUE(r.inRange(15)); } -TEST(Range, Overlapps) +TEST(Range, overlaps) { - ASSERT_FALSE(Range(10, 20).overlapps(Range(0, 9))); - ASSERT_FALSE(Range(10, 20).overlapps(Range(21, 30))); - ASSERT_TRUE(Range(10, 20).overlapps(Range(0, 10))); - ASSERT_TRUE(Range(10, 20).overlapps(Range(20, 30))); - ASSERT_TRUE(Range(10, 20).overlapps(Range(5, 15))); - ASSERT_TRUE(Range(10, 20).overlapps(Range(15, 25))); - ASSERT_TRUE(Range(10, 20).overlapps(Range(15, 19))); - ASSERT_TRUE(Range(10, 20).overlapps(Range(15, 15))); - ASSERT_TRUE(Range(10, 20).overlapps(Range(10, 20))); - ASSERT_TRUE(Range(10, 20).overlapps(Range(0, 30))); + ASSERT_FALSE(Range(10, 20).overlaps(Range(0, 9))); + ASSERT_FALSE(Range(10, 20).overlaps(Range(21, 30))); + ASSERT_TRUE(Range(10, 20).overlaps(Range(0, 10))); + ASSERT_TRUE(Range(10, 20).overlaps(Range(20, 30))); + ASSERT_TRUE(Range(10, 20).overlaps(Range(5, 15))); + ASSERT_TRUE(Range(10, 20).overlaps(Range(15, 25))); + ASSERT_TRUE(Range(10, 20).overlaps(Range(15, 19))); + ASSERT_TRUE(Range(10, 20).overlaps(Range(15, 15))); + ASSERT_TRUE(Range(10, 20).overlaps(Range(10, 20))); + ASSERT_TRUE(Range(10, 20).overlaps(Range(0, 30))); } TEST(Range, CoveredBy) diff --git a/test/core/script/Variant.cpp b/test/core/script/Variant.cpp new file mode 100644 index 0000000..164bcab --- /dev/null +++ b/test/core/script/Variant.cpp @@ -0,0 +1,71 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include + +namespace ousia { +namespace script { + +TEST(Variant, getIntegerValue) +{ + Variant vi{(int64_t)42}; + Variant vf{42.0}; + + ASSERT_EQ(42, vi.getIntegerValue()); + ASSERT_EQ(42, vf.getIntegerValue()); +} + +TEST(Variant, getNumberValue) +{ + Variant vi{(int64_t)42}; + Variant vf{42.5}; + + ASSERT_EQ(42, vi.getNumberValue()); + ASSERT_EQ(42.5, vf.getNumberValue()); +} + +TEST(Variant, getStringValue) +{ + Variant v{"hello world"}; + ASSERT_EQ("hello world", v.getStringValue()); +} + +TEST(Variant, getArrayValue) +{ + Variant v{{"test1", (int64_t)42}}; + ASSERT_EQ(2, v.getArrayValue().size()); + ASSERT_EQ("test1", v.getArrayValue()[0].getStringValue()); + ASSERT_EQ(42, v.getArrayValue()[1].getIntegerValue()); +} + +TEST(Variant, getMapValue) +{ + Variant v{{{"key1", "entry1"}, {"key2", "entry2"}}}; + + auto map = v.getMapValue(); + ASSERT_EQ(2, map.size()); + + ASSERT_EQ("entry1", (*map.find("key1")).second.getStringValue()); + ASSERT_EQ("entry2", (*map.find("key2")).second.getStringValue()); +} + +} +} + -- cgit v1.2.3