From 65778eb19e1b4d7d5d145bb2167df0eb01935da7 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 2 Dec 2014 15:58:48 +0100 Subject: added new unit test for the Variant class and fixed some bugs --- test/core/variant/VariantTest.cpp | 118 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 test/core/variant/VariantTest.cpp (limited to 'test/core/variant/VariantTest.cpp') 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 . +*/ + +#include + +#include + +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()); +} + + +} + -- cgit v1.2.3 From 80c32c744807afa81178f45af1867fb7c3366c81 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Tue, 2 Dec 2014 16:21:23 +0100 Subject: expanded unit test and fixed further stack overflow caused by missuse of braced initializer list --- src/core/variant/Variant.hpp | 8 ++++---- test/core/variant/VariantTest.cpp | 2 ++ 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'test/core/variant/VariantTest.cpp') diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp index 6b5d03f..f438d3e 100644 --- a/src/core/variant/Variant.hpp +++ b/src/core/variant/Variant.hpp @@ -168,13 +168,13 @@ private: doubleVal = v.doubleVal; break; case Type::STRING: - ptrVal = new stringType{v.asString()}; + ptrVal = new stringType(v.asString()); break; case Type::ARRAY: - ptrVal = new arrayType{v.asArray()}; + ptrVal = new arrayType(v.asArray()); break; case Type::MAP: - ptrVal = new mapType{v.asMap()}; + ptrVal = new mapType(v.asMap()); break; } } @@ -585,7 +585,7 @@ public: } else { destroy(); type = Type::STRING; - ptrVal = new stringType{s}; + ptrVal = new stringType(s); } } diff --git a/test/core/variant/VariantTest.cpp b/test/core/variant/VariantTest.cpp index dfa2f1b..3a23887 100644 --- a/test/core/variant/VariantTest.cpp +++ b/test/core/variant/VariantTest.cpp @@ -111,6 +111,8 @@ TEST(Variant, mapValue) ASSERT_EQ("entry1", map.find("key1")->second.asString()); ASSERT_EQ("entry2", map.find("key2")->second.asString()); + + const Variant v2{{{"key1", Variant::arrayType{1, 2, 3}}, {"key2", "entry2"}}}; } -- cgit v1.2.3 From 2ca83f15d5ca81ce8b45fd99d959aee49a6f2eea Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Wed, 3 Dec 2014 00:03:01 +0100 Subject: added type conversion functions and creation from nullptr --- src/core/variant/Variant.cpp | 115 +++++++++++++++++++++++++++++++++++--- src/core/variant/Variant.hpp | 69 +++++++++++++++++++---- test/core/variant/VariantTest.cpp | 8 ++- 3 files changed, 172 insertions(+), 20 deletions(-) (limited to 'test/core/variant/VariantTest.cpp') diff --git a/src/core/variant/Variant.cpp b/src/core/variant/Variant.cpp index c86905c..d33cd4f 100644 --- a/src/core/variant/Variant.cpp +++ b/src/core/variant/Variant.cpp @@ -16,10 +16,26 @@ along with this program. If not, see . */ +#include + +#include + #include "Variant.hpp" namespace ousia { +/* Class Variant::TypeException */ + +Variant::TypeException::TypeException(Type actualType, Type requestedType) + : OusiaException(std::string("Variant: Requested \"") + + Variant::getTypeName(requestedType) + + std::string("\" but is \"") + + Variant::getTypeName(actualType) + std::string("\"")), + actualType(actualType), + requestedType(requestedType) +{ +} + /* Class Variant */ const char *Variant::getTypeName(Type type) @@ -32,7 +48,7 @@ const char *Variant::getTypeName(Type type) case Type::INT: return "integer"; case Type::DOUBLE: - return "number"; + return "double"; case Type::STRING: return "string"; case Type::ARRAY: @@ -43,16 +59,97 @@ const char *Variant::getTypeName(Type type) return "unknown"; } -/* Class VariantTypeException */ +Variant::boolType Variant::toBool() const +{ + switch (getType()) { + case Type::NULLPTR: + return false; + case Type::BOOL: + return asBool(); + case Type::INT: + return asInt() != 0; + case Type::DOUBLE: + return asDouble() != 0.0; + case Type::STRING: + return true; + case Type::ARRAY: + return true; + case Type::MAP: + return true; + } + return false; +} -Variant::TypeException::TypeException(Type actualType, Type requestedType) - : OusiaException(std::string("Variant: Requested \"") + - Variant::getTypeName(actualType) + - std::string("\" but is \"") + - Variant::getTypeName(requestedType) + std::string("\"")), - actualType(actualType), - requestedType(requestedType) +Variant::intType Variant::toInt() const { + switch (getType()) { + case Type::NULLPTR: + return 0; + case Type::BOOL: + return asBool() ? 1 : 0; + case Type::INT: + return asInt(); + case Type::DOUBLE: + return asDouble(); + case Type::STRING: + return 0; // TODO: Parse string as int + case Type::ARRAY: { + const arrayType &a = asArray(); + return (a.size() == 1) ? a[0].toInt() : 0; + } + case Type::MAP: + return 0; + } + return false; } + +Variant::doubleType Variant::toDouble() const +{ + switch (getType()) { + case Type::NULLPTR: + return 0.0; + case Type::BOOL: + return asBool() ? 1.0 : 0.0; + case Type::INT: + return asInt(); + case Type::DOUBLE: + return asDouble(); + case Type::STRING: + return 0.0; // TODO: Parse string as double + case Type::ARRAY: { + const arrayType &a = asArray(); + return (a.size() == 1) ? a[0].toDouble() : 0; + } + case Type::MAP: + return 0; + } + return false; +} + +Variant::stringType Variant::toString(bool escape) const +{ + switch (getType()) { + case Type::NULLPTR: + return "null"; + case Type::BOOL: + return asBool() ? "true" : "false"; + case Type::INT: + return std::to_string(asInt()); + case Type::DOUBLE: + return std::to_string(asDouble()); + case Type::STRING: { + // TODO: Use proper serialization function + std::stringstream ss; + ss << "\"" << asString() << "\""; + return ss.str(); + } + case Type::ARRAY: + return Utils::join(asArray(), ", ", "[", "]"); + case Type::MAP: + return Utils::join(asMap(), ", ", "{", "}"); + } + return ""; +} + } diff --git a/src/core/variant/Variant.hpp b/src/core/variant/Variant.hpp index f438d3e..d65e14a 100644 --- a/src/core/variant/Variant.hpp +++ b/src/core/variant/Variant.hpp @@ -20,8 +20,8 @@ * @file Variant.hpp * * The Variant class is used to efficiently represent a variables of varying - * type. Variant instances are used to represent user data and to exchange - * information between the host application and the script clients. + * type. Variant instances are used to represent data given by the end user and + * to exchange information between the host application and the script clients. * * @author Andreas Stöckel (astoecke@techfak.uni-bielefeld.de) */ @@ -33,6 +33,7 @@ #include #include #include +#include // TODO: Use // http://nikic.github.io/2012/02/02/Pointer-magic-for-efficient-dynamic-value-representations.html @@ -259,6 +260,11 @@ public: */ ~Variant() { destroy(); } + /** + * Constructor for null values. Initializes the variant as null value. + */ + Variant(std::nullptr_t) : ptrVal(nullptr) { setNull(); } + /** * Constructor for boolean values. * @@ -294,10 +300,7 @@ public: * * @param a is a reference to the array */ - Variant(arrayType a) : ptrVal(nullptr) - { - 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 @@ -305,10 +308,7 @@ public: * * @param m is a reference to the map. */ - Variant(mapType m) : ptrVal(nullptr) - { - setMap(std::move(m)); - } + Variant(mapType m) : ptrVal(nullptr) { setMap(std::move(m)); } /** * Copy assignment operator. @@ -527,6 +527,36 @@ public: */ mapType &asMap() { return asObj(Type::MAP); } + /** + * Returns the value of the Variant as boolean, performs type conversion. + * + * @return the Variant value converted to a boolean value. + */ + boolType toBool() const; + + /** + * Returns the value of the Variant as integer, performs type conversion. + * + * @return the Variant value converted to an integer value. + */ + intType toInt() const; + + /** + * Returns the value of the Variant as double, performs type conversion. + * + * @return the Variant value converted to a double value. + */ + doubleType toDouble() const; + + /** + * Returns the value of the Variant as string, performs type conversion. + * + * @return the value of the variant as string. + * @param escape if set to true, adds double quotes to strings and escapes + * them properly (resulting in a more or less JSONesque output). + */ + stringType toString(bool escape = false) const; + /** * Sets the variant to null. */ @@ -637,7 +667,26 @@ public: * Returns the name of the type of this variant instance. */ const char *getTypeName() { return Variant::getTypeName(getType()); } + + /** + * Prints the Variant to the output stream. + */ + friend std::ostream &operator<<(std::ostream &os, const Variant &v) + { + return os << v.toString(true); + } + + /** + * Prints a key value pair to the output stream. + */ + friend std::ostream &operator<<(std::ostream &os, + const mapType::value_type &v) + { + // TODO: Use proper serialization function + return os << "\"" << v.first << "\": " << v.second.toString(true); + } }; + } #endif /* _OUSIA_VARIANT_HPP_ */ diff --git a/test/core/variant/VariantTest.cpp b/test/core/variant/VariantTest.cpp index 3a23887..270c350 100644 --- a/test/core/variant/VariantTest.cpp +++ b/test/core/variant/VariantTest.cpp @@ -16,6 +16,8 @@ along with this program. If not, see . */ +#include + #include #include @@ -32,6 +34,9 @@ TEST(Variant, nullValue) v = nullptr; ASSERT_TRUE(v.isNull()); + + Variant v2{nullptr}; + ASSERT_TRUE(v.isNull()); } TEST(Variant, booleanValue) @@ -112,7 +117,8 @@ TEST(Variant, mapValue) ASSERT_EQ("entry1", map.find("key1")->second.asString()); ASSERT_EQ("entry2", map.find("key2")->second.asString()); - const Variant v2{{{"key1", Variant::arrayType{1, 2, 3}}, {"key2", "entry2"}}}; + const Variant v2{{{"key1", Variant::arrayType{1, 2}}, {"key2", "entry2"}}}; + ASSERT_EQ(2, v2.asMap().find("key1")->second.asArray()[1].asInt()); } -- cgit v1.2.3