From 250d6a4dbe61b6798cd090abeabdc0ece8237dd3 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sat, 25 Oct 2014 21:23:38 +0000 Subject: some restructuring, added first version of the mozjs plugin git-svn-id: file:///var/local/svn/basicwriter@78 daaaf23c-2e50-4459-9457-1e69db5a47bf --- test/core/model/RangeSet.cpp | 222 -------------------------------------- test/core/script/Function.cpp | 75 ------------- test/core/script/FunctionTest.cpp | 75 +++++++++++++ test/core/script/Object.cpp | 58 ---------- test/core/script/ObjectTest.cpp | 58 ++++++++++ test/core/script/Variant.cpp | 103 ------------------ test/core/script/VariantTest.cpp | 103 ++++++++++++++++++ test/core/utils/RangeSetTest.cpp | 222 ++++++++++++++++++++++++++++++++++++++ test/core/utils/Utils.cpp | 35 ------ test/core/utils/UtilsTest.cpp | 35 ++++++ 10 files changed, 493 insertions(+), 493 deletions(-) delete mode 100644 test/core/model/RangeSet.cpp delete mode 100644 test/core/script/Function.cpp create mode 100644 test/core/script/FunctionTest.cpp delete mode 100644 test/core/script/Object.cpp create mode 100644 test/core/script/ObjectTest.cpp delete mode 100644 test/core/script/Variant.cpp create mode 100644 test/core/script/VariantTest.cpp create mode 100644 test/core/utils/RangeSetTest.cpp delete mode 100644 test/core/utils/Utils.cpp create mode 100644 test/core/utils/UtilsTest.cpp (limited to 'test') diff --git a/test/core/model/RangeSet.cpp b/test/core/model/RangeSet.cpp deleted file mode 100644 index bfc7749..0000000 --- a/test/core/model/RangeSet.cpp +++ /dev/null @@ -1,222 +0,0 @@ -/* - Ousía - Copyright (C) 2014 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 model { - -TEST(Range, IsValid) -{ - ASSERT_FALSE(Range().isValid()); - ASSERT_TRUE(Range(10).isValid()); - ASSERT_TRUE(Range(10, 20).isValid()); - ASSERT_FALSE(Range(20, 10).isValid()); -} - -TEST(Range, InRange) -{ - Range r(10, 20); - ASSERT_FALSE(r.inRange(0)); - ASSERT_FALSE(r.inRange(21)); - ASSERT_TRUE(r.inRange(10)); - ASSERT_TRUE(r.inRange(20)); - ASSERT_TRUE(r.inRange(15)); -} - -TEST(Range, overlaps) -{ - 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) -{ - ASSERT_FALSE(Range(10, 20).coveredBy(Range(0, 9))); - ASSERT_FALSE(Range(10, 20).coveredBy(Range(21, 30))); - ASSERT_FALSE(Range(10, 20).coveredBy(Range(0, 10))); - ASSERT_FALSE(Range(10, 20).coveredBy(Range(20, 30))); - ASSERT_FALSE(Range(10, 20).coveredBy(Range(5, 15))); - ASSERT_FALSE(Range(10, 20).coveredBy(Range(15, 25))); - ASSERT_FALSE(Range(10, 20).coveredBy(Range(15, 19))); - ASSERT_FALSE(Range(10, 20).coveredBy(Range(15, 15))); - ASSERT_TRUE(Range(10, 20).coveredBy(Range(10, 20))); - ASSERT_TRUE(Range(10, 20).coveredBy(Range(0, 30))); -} - -TEST(Range, Covers) -{ - ASSERT_FALSE(Range(10, 20).covers(Range(0, 9))); - ASSERT_FALSE(Range(10, 20).covers(Range(21, 30))); - ASSERT_FALSE(Range(10, 20).covers(Range(0, 10))); - ASSERT_FALSE(Range(10, 20).covers(Range(20, 30))); - ASSERT_FALSE(Range(10, 20).covers(Range(5, 15))); - ASSERT_FALSE(Range(10, 20).covers(Range(15, 25))); - ASSERT_TRUE(Range(10, 20).covers(Range(15, 19))); - ASSERT_TRUE(Range(10, 20).covers(Range(15, 15))); - ASSERT_TRUE(Range(10, 20).covers(Range(10, 20))); - ASSERT_FALSE(Range(10, 20).covers(Range(0, 30))); -} - -TEST(RangeSet, Neighbours) -{ - ASSERT_TRUE(Range(10, 19).neighbours(Range(20, 30))); - ASSERT_TRUE(Range(20, 29).neighbours(Range(10, 19))); -} - -TEST(Range, Merge) -{ - Range r1(10, 20); - Range r2(15, 25); - Range r3(5, 15); - Range rM = r1.merge(r2).merge(r3); - ASSERT_EQ(rM.start, 5); - ASSERT_EQ(rM.end, 25); -} - -TEST(RangeSet, Merge) -{ - RangeSet s; - auto &ranges = s.getRanges(); - - // Insert some non-overlapping elements into the range. We expect these to - // be just inserted into the ranges. - s.merge(Range( 0, 10)); - s.merge(Range(20, 30)); - s.merge(Range(40, 50)); - s.merge(Range(60, 70)); - { - ASSERT_EQ(ranges.size(), 4); - - auto it = ranges.begin(); - ASSERT_EQ((*it).start, 0); - ASSERT_EQ((*it).end, 10); - - it++; - ASSERT_EQ((*it).start, 20); - ASSERT_EQ((*it).end, 30); - - it++; - ASSERT_EQ((*it).start, 40); - ASSERT_EQ((*it).end, 50); - - it++; - ASSERT_EQ((*it).start, 60); - ASSERT_EQ((*it).end, 70); - } - - // Now insert an element which spans the second and third element - s.merge(Range(15, 55)); - { - ASSERT_EQ(ranges.size(), 3); - - auto it = ranges.begin(); - ASSERT_EQ((*it).start, 0); - ASSERT_EQ((*it).end, 10); - - it++; - ASSERT_EQ((*it).start, 15); - ASSERT_EQ((*it).end, 55); - - it++; - ASSERT_EQ((*it).start, 60); - ASSERT_EQ((*it).end, 70); - } - - // Now insert an element which expands the first element - s.merge(Range(-10, 11)); - { - ASSERT_EQ(ranges.size(), 3); - - auto it = ranges.begin(); - ASSERT_EQ((*it).start, -10); - ASSERT_EQ((*it).end, 11); - - it++; - ASSERT_EQ((*it).start, 15); - ASSERT_EQ((*it).end, 55); - - it++; - ASSERT_EQ((*it).start, 60); - ASSERT_EQ((*it).end, 70); - } - - // Now insert an element which merges the last two elements - s.merge(Range(13, 70)); - { - ASSERT_EQ(ranges.size(), 2); - - auto it = ranges.begin(); - ASSERT_EQ((*it).start, -10); - ASSERT_EQ((*it).end, 11); - - it++; - ASSERT_EQ((*it).start, 13); - ASSERT_EQ((*it).end, 70); - } - - // Now insert an element which merges the remaining elements - s.merge(Range(-9, 12)); - { - ASSERT_EQ(ranges.size(), 1); - - auto it = ranges.begin(); - ASSERT_EQ((*it).start, -10); - ASSERT_EQ((*it).end, 70); - } - -} - -TEST(RangeSet, Contains) -{ - RangeSet s; - - // Insert some non-overlapping elements into the range. We expect these to - // be just inserted into the ranges. - s.merge(Range( 0, 10)); - s.merge(Range(20, 30)); - s.merge(Range(40, 50)); - s.merge(Range(60, 70)); - s.merge(Range(71)); - s.merge(Range(72)); - s.merge(Range(73)); - s.merge(Range(74)); - - ASSERT_TRUE(s.contains(60)); - ASSERT_TRUE(s.contains(0)); - ASSERT_TRUE(s.contains(25)); - ASSERT_TRUE(s.contains(73)); - ASSERT_TRUE(s.contains(Range(25, 30))); - ASSERT_FALSE(s.contains(Range(25, 35))); - ASSERT_TRUE(s.contains(Range(0, 10))); - ASSERT_TRUE(s.contains(Range(70, 74))); -} - -} -} - diff --git a/test/core/script/Function.cpp b/test/core/script/Function.cpp deleted file mode 100644 index 5bacf2a..0000000 --- a/test/core/script/Function.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - 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(HostFunction, callDirect) -{ - int v = 0; - HostFunction f{[](const std::vector &args, void *data) { - *(static_cast(data)) = args[0].getIntegerValue(); - return Variant::Null; - }, - {Argument{VariantType::integer}}, - &v}; - ASSERT_EQ(VariantType::null, f.call({{(int64_t)42}}).getType()); - ASSERT_EQ(42, v); -} - -TEST(HostFunction, callDefaults) -{ - int v = 0; - HostFunction f{[](const std::vector &args, void *data) { - *(static_cast(data)) = args[0].getIntegerValue(); - return Variant{"Hallo Welt"}; - }, - {Argument{VariantType::integer, {(int64_t)42}}}, - &v}; - ASSERT_EQ("Hallo Welt", f.call().getStringValue()); - ASSERT_EQ(42, v); -} - -TEST(Setter, call) -{ - int v = 0; - Setter f{VariantType::integer, [](Variant arg, void *data) { - *(static_cast(data)) = - arg.getIntegerValue(); - }, - &v}; - f.call({(int64_t)42}); - ASSERT_EQ(42, v); -} - -TEST(Getter, call) -{ - int v = 42; - Getter f{[](void *data) { - return Variant{int64_t(*(static_cast(data)))}; - }, - &v}; - ASSERT_EQ(v, f.call().getIntegerValue()); -} -} -} - diff --git a/test/core/script/FunctionTest.cpp b/test/core/script/FunctionTest.cpp new file mode 100644 index 0000000..5bacf2a --- /dev/null +++ b/test/core/script/FunctionTest.cpp @@ -0,0 +1,75 @@ +/* + 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(HostFunction, callDirect) +{ + int v = 0; + HostFunction f{[](const std::vector &args, void *data) { + *(static_cast(data)) = args[0].getIntegerValue(); + return Variant::Null; + }, + {Argument{VariantType::integer}}, + &v}; + ASSERT_EQ(VariantType::null, f.call({{(int64_t)42}}).getType()); + ASSERT_EQ(42, v); +} + +TEST(HostFunction, callDefaults) +{ + int v = 0; + HostFunction f{[](const std::vector &args, void *data) { + *(static_cast(data)) = args[0].getIntegerValue(); + return Variant{"Hallo Welt"}; + }, + {Argument{VariantType::integer, {(int64_t)42}}}, + &v}; + ASSERT_EQ("Hallo Welt", f.call().getStringValue()); + ASSERT_EQ(42, v); +} + +TEST(Setter, call) +{ + int v = 0; + Setter f{VariantType::integer, [](Variant arg, void *data) { + *(static_cast(data)) = + arg.getIntegerValue(); + }, + &v}; + f.call({(int64_t)42}); + ASSERT_EQ(42, v); +} + +TEST(Getter, call) +{ + int v = 42; + Getter f{[](void *data) { + return Variant{int64_t(*(static_cast(data)))}; + }, + &v}; + ASSERT_EQ(v, f.call().getIntegerValue()); +} +} +} + diff --git a/test/core/script/Object.cpp b/test/core/script/Object.cpp deleted file mode 100644 index 3c392bb..0000000 --- a/test/core/script/Object.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - 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(Object, addProperty) -{ - int64_t i = 0; - - Object o{&i}; - - auto get = [](void *data) { - return Variant{*((int64_t*)data)}; - }; - auto set = [](Variant v, void *data) { - *((int64_t*)data) = v.getIntegerValue(); - }; - - std::map ps; - - o.addProperty("p1", VariantType::integer, get, set); - o.addReadonlyProperty("p2", get); - - ASSERT_TRUE(o.getProperty("p1") != nullptr); - ASSERT_TRUE(o.getProperty("p2") != nullptr); - ASSERT_FALSE(o.getMethod("p1") != nullptr); - ASSERT_FALSE(o.getMethod("p2") != nullptr); - - o.getProperty("p1")->set({(int64_t)42}); - ASSERT_EQ(42, i); - ASSERT_EQ(i, o.getProperty("p1")->get().getIntegerValue()); - - ASSERT_FALSE(o.getProperty("p2")->set.exists()); - ASSERT_EQ(i, o.getProperty("p2")->get().getIntegerValue()); -} -} -} - diff --git a/test/core/script/ObjectTest.cpp b/test/core/script/ObjectTest.cpp new file mode 100644 index 0000000..3c392bb --- /dev/null +++ b/test/core/script/ObjectTest.cpp @@ -0,0 +1,58 @@ +/* + 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(Object, addProperty) +{ + int64_t i = 0; + + Object o{&i}; + + auto get = [](void *data) { + return Variant{*((int64_t*)data)}; + }; + auto set = [](Variant v, void *data) { + *((int64_t*)data) = v.getIntegerValue(); + }; + + std::map ps; + + o.addProperty("p1", VariantType::integer, get, set); + o.addReadonlyProperty("p2", get); + + ASSERT_TRUE(o.getProperty("p1") != nullptr); + ASSERT_TRUE(o.getProperty("p2") != nullptr); + ASSERT_FALSE(o.getMethod("p1") != nullptr); + ASSERT_FALSE(o.getMethod("p2") != nullptr); + + o.getProperty("p1")->set({(int64_t)42}); + ASSERT_EQ(42, i); + ASSERT_EQ(i, o.getProperty("p1")->get().getIntegerValue()); + + ASSERT_FALSE(o.getProperty("p2")->set.exists()); + ASSERT_EQ(i, o.getProperty("p2")->get().getIntegerValue()); +} +} +} + diff --git a/test/core/script/Variant.cpp b/test/core/script/Variant.cpp deleted file mode 100644 index f229e3a..0000000 --- a/test/core/script/Variant.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - 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 -#include -#include - -namespace ousia { -namespace script { - -TEST(Variant, getBooleanValue) -{ - ASSERT_TRUE((Variant{true}).getBooleanValue()); - ASSERT_FALSE((Variant{false}).getBooleanValue()); - ASSERT_FALSE((Variant{(int64_t)0}).getBooleanValue()); - ASSERT_TRUE((Variant{(int64_t)1}).getBooleanValue()); - ASSERT_FALSE((Variant{0.0}).getBooleanValue()); - ASSERT_TRUE((Variant{1.2}).getBooleanValue()); - ASSERT_FALSE((Variant{""}).getBooleanValue()); -} - -TEST(Variant, getIntegerValue) -{ - Variant vi{(int64_t)42}; - Variant vf{42.0}; - - ASSERT_EQ(42, vi.getIntegerValue()); - ASSERT_EQ(42, vf.getIntegerValue()); - ASSERT_EQ(1, (Variant{true}).getIntegerValue()); - ASSERT_EQ(0, (Variant{false}).getIntegerValue()); -} - -TEST(Variant, getNumberValue) -{ - Variant vi{(int64_t)42}; - Variant vf{42.5}; - - ASSERT_EQ(42, vi.getNumberValue()); - ASSERT_EQ(42.5, vf.getNumberValue()); - ASSERT_EQ(1.0, (Variant{true}).getIntegerValue()); - ASSERT_EQ(0.0, (Variant{false}).getIntegerValue()); -} - -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()); -} - -TEST(Variant, getFunctionValue) -{ - int64_t i = 0; - HostFunction f{[](const std::vector &args, void *data) { - *((int64_t*)data) = args[0].getIntegerValue(); - return Variant{"Hello World"}; - }, &i}; - - Variant v{&f}; - ASSERT_TRUE(v.getFunctionValue() != nullptr); - ASSERT_EQ("Hello World", v.getFunctionValue()->call({{(int64_t)42}}).getStringValue()); - ASSERT_EQ(42, i); -} - - -} -} - diff --git a/test/core/script/VariantTest.cpp b/test/core/script/VariantTest.cpp new file mode 100644 index 0000000..f229e3a --- /dev/null +++ b/test/core/script/VariantTest.cpp @@ -0,0 +1,103 @@ +/* + 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 +#include +#include + +namespace ousia { +namespace script { + +TEST(Variant, getBooleanValue) +{ + ASSERT_TRUE((Variant{true}).getBooleanValue()); + ASSERT_FALSE((Variant{false}).getBooleanValue()); + ASSERT_FALSE((Variant{(int64_t)0}).getBooleanValue()); + ASSERT_TRUE((Variant{(int64_t)1}).getBooleanValue()); + ASSERT_FALSE((Variant{0.0}).getBooleanValue()); + ASSERT_TRUE((Variant{1.2}).getBooleanValue()); + ASSERT_FALSE((Variant{""}).getBooleanValue()); +} + +TEST(Variant, getIntegerValue) +{ + Variant vi{(int64_t)42}; + Variant vf{42.0}; + + ASSERT_EQ(42, vi.getIntegerValue()); + ASSERT_EQ(42, vf.getIntegerValue()); + ASSERT_EQ(1, (Variant{true}).getIntegerValue()); + ASSERT_EQ(0, (Variant{false}).getIntegerValue()); +} + +TEST(Variant, getNumberValue) +{ + Variant vi{(int64_t)42}; + Variant vf{42.5}; + + ASSERT_EQ(42, vi.getNumberValue()); + ASSERT_EQ(42.5, vf.getNumberValue()); + ASSERT_EQ(1.0, (Variant{true}).getIntegerValue()); + ASSERT_EQ(0.0, (Variant{false}).getIntegerValue()); +} + +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()); +} + +TEST(Variant, getFunctionValue) +{ + int64_t i = 0; + HostFunction f{[](const std::vector &args, void *data) { + *((int64_t*)data) = args[0].getIntegerValue(); + return Variant{"Hello World"}; + }, &i}; + + Variant v{&f}; + ASSERT_TRUE(v.getFunctionValue() != nullptr); + ASSERT_EQ("Hello World", v.getFunctionValue()->call({{(int64_t)42}}).getStringValue()); + ASSERT_EQ(42, i); +} + + +} +} + diff --git a/test/core/utils/RangeSetTest.cpp b/test/core/utils/RangeSetTest.cpp new file mode 100644 index 0000000..9be4c17 --- /dev/null +++ b/test/core/utils/RangeSetTest.cpp @@ -0,0 +1,222 @@ +/* + Ousía + Copyright (C) 2014 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 model { + +TEST(Range, IsValid) +{ + ASSERT_FALSE(Range().isValid()); + ASSERT_TRUE(Range(10).isValid()); + ASSERT_TRUE(Range(10, 20).isValid()); + ASSERT_FALSE(Range(20, 10).isValid()); +} + +TEST(Range, InRange) +{ + Range r(10, 20); + ASSERT_FALSE(r.inRange(0)); + ASSERT_FALSE(r.inRange(21)); + ASSERT_TRUE(r.inRange(10)); + ASSERT_TRUE(r.inRange(20)); + ASSERT_TRUE(r.inRange(15)); +} + +TEST(Range, overlaps) +{ + 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) +{ + ASSERT_FALSE(Range(10, 20).coveredBy(Range(0, 9))); + ASSERT_FALSE(Range(10, 20).coveredBy(Range(21, 30))); + ASSERT_FALSE(Range(10, 20).coveredBy(Range(0, 10))); + ASSERT_FALSE(Range(10, 20).coveredBy(Range(20, 30))); + ASSERT_FALSE(Range(10, 20).coveredBy(Range(5, 15))); + ASSERT_FALSE(Range(10, 20).coveredBy(Range(15, 25))); + ASSERT_FALSE(Range(10, 20).coveredBy(Range(15, 19))); + ASSERT_FALSE(Range(10, 20).coveredBy(Range(15, 15))); + ASSERT_TRUE(Range(10, 20).coveredBy(Range(10, 20))); + ASSERT_TRUE(Range(10, 20).coveredBy(Range(0, 30))); +} + +TEST(Range, Covers) +{ + ASSERT_FALSE(Range(10, 20).covers(Range(0, 9))); + ASSERT_FALSE(Range(10, 20).covers(Range(21, 30))); + ASSERT_FALSE(Range(10, 20).covers(Range(0, 10))); + ASSERT_FALSE(Range(10, 20).covers(Range(20, 30))); + ASSERT_FALSE(Range(10, 20).covers(Range(5, 15))); + ASSERT_FALSE(Range(10, 20).covers(Range(15, 25))); + ASSERT_TRUE(Range(10, 20).covers(Range(15, 19))); + ASSERT_TRUE(Range(10, 20).covers(Range(15, 15))); + ASSERT_TRUE(Range(10, 20).covers(Range(10, 20))); + ASSERT_FALSE(Range(10, 20).covers(Range(0, 30))); +} + +TEST(RangeSet, Neighbours) +{ + ASSERT_TRUE(Range(10, 19).neighbours(Range(20, 30))); + ASSERT_TRUE(Range(20, 29).neighbours(Range(10, 19))); +} + +TEST(Range, Merge) +{ + Range r1(10, 20); + Range r2(15, 25); + Range r3(5, 15); + Range rM = r1.merge(r2).merge(r3); + ASSERT_EQ(rM.start, 5); + ASSERT_EQ(rM.end, 25); +} + +TEST(RangeSet, Merge) +{ + RangeSet s; + auto &ranges = s.getRanges(); + + // Insert some non-overlapping elements into the range. We expect these to + // be just inserted into the ranges. + s.merge(Range( 0, 10)); + s.merge(Range(20, 30)); + s.merge(Range(40, 50)); + s.merge(Range(60, 70)); + { + ASSERT_EQ(ranges.size(), 4); + + auto it = ranges.begin(); + ASSERT_EQ((*it).start, 0); + ASSERT_EQ((*it).end, 10); + + it++; + ASSERT_EQ((*it).start, 20); + ASSERT_EQ((*it).end, 30); + + it++; + ASSERT_EQ((*it).start, 40); + ASSERT_EQ((*it).end, 50); + + it++; + ASSERT_EQ((*it).start, 60); + ASSERT_EQ((*it).end, 70); + } + + // Now insert an element which spans the second and third element + s.merge(Range(15, 55)); + { + ASSERT_EQ(ranges.size(), 3); + + auto it = ranges.begin(); + ASSERT_EQ((*it).start, 0); + ASSERT_EQ((*it).end, 10); + + it++; + ASSERT_EQ((*it).start, 15); + ASSERT_EQ((*it).end, 55); + + it++; + ASSERT_EQ((*it).start, 60); + ASSERT_EQ((*it).end, 70); + } + + // Now insert an element which expands the first element + s.merge(Range(-10, 11)); + { + ASSERT_EQ(ranges.size(), 3); + + auto it = ranges.begin(); + ASSERT_EQ((*it).start, -10); + ASSERT_EQ((*it).end, 11); + + it++; + ASSERT_EQ((*it).start, 15); + ASSERT_EQ((*it).end, 55); + + it++; + ASSERT_EQ((*it).start, 60); + ASSERT_EQ((*it).end, 70); + } + + // Now insert an element which merges the last two elements + s.merge(Range(13, 70)); + { + ASSERT_EQ(ranges.size(), 2); + + auto it = ranges.begin(); + ASSERT_EQ((*it).start, -10); + ASSERT_EQ((*it).end, 11); + + it++; + ASSERT_EQ((*it).start, 13); + ASSERT_EQ((*it).end, 70); + } + + // Now insert an element which merges the remaining elements + s.merge(Range(-9, 12)); + { + ASSERT_EQ(ranges.size(), 1); + + auto it = ranges.begin(); + ASSERT_EQ((*it).start, -10); + ASSERT_EQ((*it).end, 70); + } + +} + +TEST(RangeSet, Contains) +{ + RangeSet s; + + // Insert some non-overlapping elements into the range. We expect these to + // be just inserted into the ranges. + s.merge(Range( 0, 10)); + s.merge(Range(20, 30)); + s.merge(Range(40, 50)); + s.merge(Range(60, 70)); + s.merge(Range(71)); + s.merge(Range(72)); + s.merge(Range(73)); + s.merge(Range(74)); + + ASSERT_TRUE(s.contains(60)); + ASSERT_TRUE(s.contains(0)); + ASSERT_TRUE(s.contains(25)); + ASSERT_TRUE(s.contains(73)); + ASSERT_TRUE(s.contains(Range(25, 30))); + ASSERT_FALSE(s.contains(Range(25, 35))); + ASSERT_TRUE(s.contains(Range(0, 10))); + ASSERT_TRUE(s.contains(Range(70, 74))); +} + +} +} + diff --git a/test/core/utils/Utils.cpp b/test/core/utils/Utils.cpp deleted file mode 100644 index d349c6f..0000000 --- a/test/core/utils/Utils.cpp +++ /dev/null @@ -1,35 +0,0 @@ -/* - 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 { - -TEST(Utils, isIdentifier) -{ - ASSERT_TRUE(Utils::isIdentifier("test")); - ASSERT_TRUE(Utils::isIdentifier("t0-_est")); - ASSERT_TRUE(Utils::isIdentifier("_t0-_EST")); - ASSERT_FALSE(Utils::isIdentifier("-t0-_EST")); - ASSERT_FALSE(Utils::isIdentifier("0t-_EST")); -} - -} - diff --git a/test/core/utils/UtilsTest.cpp b/test/core/utils/UtilsTest.cpp new file mode 100644 index 0000000..d349c6f --- /dev/null +++ b/test/core/utils/UtilsTest.cpp @@ -0,0 +1,35 @@ +/* + 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 { + +TEST(Utils, isIdentifier) +{ + ASSERT_TRUE(Utils::isIdentifier("test")); + ASSERT_TRUE(Utils::isIdentifier("t0-_est")); + ASSERT_TRUE(Utils::isIdentifier("_t0-_EST")); + ASSERT_FALSE(Utils::isIdentifier("-t0-_EST")); + ASSERT_FALSE(Utils::isIdentifier("0t-_EST")); +} + +} + -- cgit v1.2.3