summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/core/variant/Variant.hpp48
-rw-r--r--test/core/variant/VariantTest.cpp118
3 files changed, 153 insertions, 14 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 3a52b6e..cb4d073 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -163,6 +163,7 @@ IF(TEST)
# test/core/script/FunctionTest
# test/core/script/ObjectTest
# test/core/script/VariantTest
+ test/core/variant/VariantTest
)
TARGET_LINK_LIBRARIES(ousia_test_core
diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp
index 26b053a..6b5d03f 100644
--- a/src/core/variant/Variant.hpp
+++ b/src/core/variant/Variant.hpp
@@ -128,7 +128,7 @@ private:
* valid if type is one of Type::STRING, Type::ARRAY,
* Type::MAP.
*/
- void *ptrVal = nullptr;
+ void *ptrVal;
};
/**
@@ -153,6 +153,7 @@ private:
*/
void copy(const Variant &v)
{
+ destroy();
type = v.type;
switch (type) {
case Type::NULLPTR:
@@ -186,6 +187,7 @@ private:
*/
void move(Variant &&v)
{
+ destroy();
type = v.type;
switch (type) {
case Type::NULLPTR:
@@ -237,7 +239,7 @@ public:
*
* @param v is the Variant instance that should be cloned.
*/
- Variant(const Variant &v) { copy(v); }
+ Variant(const Variant &v) : ptrVal(nullptr) { copy(v); }
/**
* Move constructor of the Variant class.
@@ -245,12 +247,12 @@ public:
* @param v is the reference to the Variant instance that should be moved,
* this instance is invalidated afterwards.
*/
- Variant(Variant &&v) { move(std::move(v)); }
+ Variant(Variant &&v) : ptrVal(nullptr) { move(std::move(v)); }
/**
* Default constructor. Type is set to Type:null.
*/
- Variant() { setNull(); }
+ Variant() : ptrVal(nullptr) { setNull(); }
/**
* Default destructor, frees any memory that was allocated on the heap.
@@ -262,21 +264,21 @@ public:
*
* @param b boolean value.
*/
- Variant(boolType b) { setBool(b); }
+ Variant(boolType b) : ptrVal(nullptr) { setBool(b); }
/**
* Constructor for integer values.
*
* @param i integer value.
*/
- Variant(intType i) { setInt(i); }
+ Variant(intType i) : ptrVal(nullptr) { setInt(i); }
/**
* Constructor for double values.
*
* @param d double value.
*/
- Variant(doubleType d) { setDouble(d); }
+ Variant(doubleType d) : ptrVal(nullptr) { setDouble(d); }
/**
* Constructor for string values. The given string is copied and managed by
@@ -284,7 +286,7 @@ public:
*
* @param s is a reference to a C-Style string used as string value.
*/
- Variant(const char *s) { setString(s); }
+ Variant(const char *s) : ptrVal(nullptr) { setString(s); }
/**
* Constructor for array values. The given array is copied and managed by
@@ -292,7 +294,10 @@ public:
*
* @param a is a reference to the array
*/
- Variant(std::vector<Variant> a) { setArray(std::move(a)); }
+ Variant(arrayType a) : ptrVal(nullptr)
+ {
+ setArray(std::move(a));
+ }
/**
* Constructor for map values. The given map is copied and managed by the
@@ -300,7 +305,10 @@ public:
*
* @param m is a reference to the map.
*/
- Variant(std::map<std::string, Variant> m) { setMap(std::move(m)); }
+ Variant(mapType m) : ptrVal(nullptr)
+ {
+ setMap(std::move(m));
+ }
/**
* Copy assignment operator.
@@ -356,11 +364,23 @@ public:
/**
* Assign a double value.
*
- * @param i is the integer value to which the variant should be set.
+ * @param d is the double value to which the variant should be set.
*/
Variant &operator=(doubleType d)
{
- setInt(d);
+ setDouble(d);
+ return *this;
+ }
+
+ /**
+ * Assign a zero terminated const char array.
+ *
+ * @param s is the zero terminated const char array to which the variant
+ * should be set.
+ */
+ Variant &operator=(const char *s)
+ {
+ setString(s);
return *this;
}
@@ -581,7 +601,7 @@ public:
} else {
destroy();
type = Type::ARRAY;
- ptrVal = new arrayType{std::move(a)};
+ ptrVal = new arrayType(std::move(a));
}
}
@@ -597,7 +617,7 @@ public:
} else {
destroy();
type = Type::MAP;
- ptrVal = new mapType{std::move(m)};
+ ptrVal = new mapType(std::move(m));
}
}
diff --git a/test/core/variant/VariantTest.cpp b/test/core/variant/VariantTest.cpp
new file mode 100644
index 0000000..dfa2f1b
--- /dev/null
+++ b/test/core/variant/VariantTest.cpp
@@ -0,0 +1,118 @@
+/*
+ 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 <http://www.gnu.org/licenses/>.
+*/
+
+#include <gtest/gtest.h>
+
+#include <core/variant/Variant.hpp>
+
+namespace ousia {
+
+TEST(Variant, nullValue)
+{
+ Variant v;
+ ASSERT_TRUE(v.isNull());
+
+ v = 1;
+ ASSERT_FALSE(v.isNull());
+
+ v = nullptr;
+ ASSERT_TRUE(v.isNull());
+}
+
+TEST(Variant, booleanValue)
+{
+ Variant v{true};
+ ASSERT_TRUE(v.isBool());
+ ASSERT_TRUE(v.asBool());
+
+ v = false;
+ ASSERT_TRUE(v.isBool());
+ ASSERT_FALSE(v.asBool());
+
+ v.setBool(true);
+ ASSERT_TRUE(v.isBool());
+ ASSERT_TRUE(v.asBool());
+
+ v = nullptr;
+ ASSERT_FALSE(v.isBool());
+}
+
+TEST(Variant, intValue)
+{
+ Variant v{42};
+ ASSERT_TRUE(v.isInt());
+ ASSERT_EQ(42, v.asInt());
+
+ v = 43;
+ ASSERT_TRUE(v.isInt());
+ ASSERT_EQ(43, v.asInt());
+
+ v = false;
+ ASSERT_FALSE(v.isInt());
+}
+
+TEST(Variant, doubleValue)
+{
+ Variant v{42.5};
+ ASSERT_TRUE(v.isDouble());
+ ASSERT_EQ(42.5, v.asDouble());
+
+ v = 42;
+ ASSERT_FALSE(v.isDouble());
+
+ v = 43.5;
+ ASSERT_TRUE(v.isDouble());
+ ASSERT_EQ(43.5, v.asDouble());
+}
+
+TEST(Variant, stringValue)
+{
+ Variant v{"Hello World"};
+ ASSERT_TRUE(v.isString());
+ ASSERT_EQ("Hello World", v.asString());
+
+ v = "Goodbye World";
+ ASSERT_TRUE(v.isString());
+ ASSERT_EQ("Goodbye World", v.asString());
+
+ v = 42;
+ ASSERT_FALSE(v.isString());
+}
+
+TEST(Variant, arrayValue)
+{
+ const Variant v{{"test1", 42}};
+ ASSERT_EQ(2, v.asArray().size());
+ ASSERT_EQ("test1", v.asArray()[0].asString());
+ ASSERT_EQ(42, v.asArray()[1].asInt());
+}
+
+TEST(Variant, mapValue)
+{
+ const Variant v{{{"key1", "entry1"}, {"key2", "entry2"}}};
+
+ auto map = v.asMap();
+ ASSERT_EQ(2, map.size());
+
+ ASSERT_EQ("entry1", map.find("key1")->second.asString());
+ ASSERT_EQ("entry2", map.find("key2")->second.asString());
+}
+
+
+}
+