summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Rauwolf <rauwolf@itestra.de>2013-01-17 17:09:32 +0100
committerPhilip Rauwolf <rauwolf@itestra.de>2013-01-17 17:09:32 +0100
commitb5d00531c0c316d66e570002e707f6c6cd5a529a (patch)
tree3c848ae200a7f409c9a08a186c766ac48cb92be4
parent1e1d64b36d386dffb83c8c53e01d2fc32d1c67d6 (diff)
downloadgenivi-common-api-dbus-runtime-b5d00531c0c316d66e570002e707f6c6cd5a529a.tar.gz
Variants fully integrated with InputStream, bugfixing Variants and
datatype "ByteBuffer"
-rw-r--r--src/CommonAPI/DBus/DBusConnection.cpp2
-rw-r--r--src/CommonAPI/DBus/DBusDaemonProxy.cpp1
-rw-r--r--src/CommonAPI/DBus/DBusFactory.cpp1
-rw-r--r--src/CommonAPI/DBus/DBusProxy.cpp1
-rw-r--r--src/CommonAPI/DBus/DBusProxyAsyncCallbackHandler.h1
-rw-r--r--src/CommonAPI/DBus/DBusServiceRegistry.cpp1
-rw-r--r--src/CommonAPI/DBus/DBusStubAdapterHelper.h1
-rw-r--r--src/test/DBusInputStreamTest.cpp288
-rw-r--r--src/test/DBusVariantTest.cpp174
9 files changed, 385 insertions, 85 deletions
diff --git a/src/CommonAPI/DBus/DBusConnection.cpp b/src/CommonAPI/DBus/DBusConnection.cpp
index 19def0d..cbe101b 100644
--- a/src/CommonAPI/DBus/DBusConnection.cpp
+++ b/src/CommonAPI/DBus/DBusConnection.cpp
@@ -11,8 +11,6 @@
#include <cassert>
#include <future>
-#include <iostream>
-
namespace CommonAPI {
namespace DBus {
diff --git a/src/CommonAPI/DBus/DBusDaemonProxy.cpp b/src/CommonAPI/DBus/DBusDaemonProxy.cpp
index 3c2170a..b2eb01c 100644
--- a/src/CommonAPI/DBus/DBusDaemonProxy.cpp
+++ b/src/CommonAPI/DBus/DBusDaemonProxy.cpp
@@ -6,7 +6,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "DBusDaemonProxy.h"
#include "DBusProxyHelper.h"
-#include <iostream>
namespace CommonAPI {
diff --git a/src/CommonAPI/DBus/DBusFactory.cpp b/src/CommonAPI/DBus/DBusFactory.cpp
index d91a7fa..62090b5 100644
--- a/src/CommonAPI/DBus/DBusFactory.cpp
+++ b/src/CommonAPI/DBus/DBusFactory.cpp
@@ -12,7 +12,6 @@
#include <algorithm>
#include <cassert>
-#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>
diff --git a/src/CommonAPI/DBus/DBusProxy.cpp b/src/CommonAPI/DBus/DBusProxy.cpp
index 1a21ec4..7e1eda9 100644
--- a/src/CommonAPI/DBus/DBusProxy.cpp
+++ b/src/CommonAPI/DBus/DBusProxy.cpp
@@ -10,7 +10,6 @@
#include <algorithm>
#include <cassert>
-#include <iostream>
#include <dbus/dbus.h>
#include <functional>
#include <CommonAPI/Event.h>
diff --git a/src/CommonAPI/DBus/DBusProxyAsyncCallbackHandler.h b/src/CommonAPI/DBus/DBusProxyAsyncCallbackHandler.h
index 52799fc..0c6d56e 100644
--- a/src/CommonAPI/DBus/DBusProxyAsyncCallbackHandler.h
+++ b/src/CommonAPI/DBus/DBusProxyAsyncCallbackHandler.h
@@ -15,7 +15,6 @@
#include <functional>
#include <future>
#include <memory>
-#include <iostream>
namespace CommonAPI {
diff --git a/src/CommonAPI/DBus/DBusServiceRegistry.cpp b/src/CommonAPI/DBus/DBusServiceRegistry.cpp
index 092e442..e8fdf93 100644
--- a/src/CommonAPI/DBus/DBusServiceRegistry.cpp
+++ b/src/CommonAPI/DBus/DBusServiceRegistry.cpp
@@ -6,7 +6,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <utility>
#include <sstream>
-#include <iostream>
#include <string>
#include <tuple>
#include <unistd.h>
diff --git a/src/CommonAPI/DBus/DBusStubAdapterHelper.h b/src/CommonAPI/DBus/DBusStubAdapterHelper.h
index 1b673c4..7c12a29 100644
--- a/src/CommonAPI/DBus/DBusStubAdapterHelper.h
+++ b/src/CommonAPI/DBus/DBusStubAdapterHelper.h
@@ -12,7 +12,6 @@
#include "DBusOutputStream.h"
#include "DBusHelper.h"
-#include <iostream>
#include <memory>
#include <initializer_list>
#include <tuple>
diff --git a/src/test/DBusInputStreamTest.cpp b/src/test/DBusInputStreamTest.cpp
index 5d4e25c..c4abec0 100644
--- a/src/test/DBusInputStreamTest.cpp
+++ b/src/test/DBusInputStreamTest.cpp
@@ -256,8 +256,23 @@ struct TestSerializableStruct: CommonAPI::SerializableStruct {
virtual void writeToOutputStream(CommonAPI::OutputStream& outputStream) const {
outputStream << a << b << c << d << e;
}
+
+ static void writeToTypeOutputStream(CommonAPI::TypeOutputStream& typeOutputStream) {
+ typeOutputStream.writeUInt32Type();
+ typeOutputStream.writeInt16Type();
+ typeOutputStream.writeBoolType();
+ typeOutputStream.writeStringType();
+ typeOutputStream.writeDoubleType();
+ }
};
+bool operator==(const TestSerializableStruct& lhs, const TestSerializableStruct& rhs) {
+ if (&lhs == &rhs)
+ return true;
+
+ return (lhs.a == rhs.a) && (lhs.b == rhs.b) && (lhs.c == rhs.c) && (lhs.d == rhs.d) && (lhs.e == rhs.e);
+}
+
} //namespace test
} //namespace bmw
@@ -409,9 +424,8 @@ TEST_F(InputStreamTest, ReadsInt32Variants) {
TestedVariantType readVariant;
inStream >> readVariant;
- bool readSuccess;
- int32_t actualResult = readVariant.get<int32_t>(readSuccess);
- EXPECT_TRUE(readSuccess);
+ //TODO: EXPECT_NO_THROW (oder so)
+ int32_t actualResult = readVariant.get<int32_t>();
bool varEq = (referenceVariant == readVariant);
EXPECT_TRUE(varEq);
@@ -448,9 +462,7 @@ TEST_F(InputStreamTest, ReadsStringVariants) {
TestedVariantType readVariant;
inStream >> readVariant;
- bool readSuccess;
- std::string actualResult = readVariant.get<std::string>(readSuccess);
- EXPECT_TRUE(readSuccess);
+ std::string actualResult = readVariant.get<std::string>();
bool variantsAreEqual = (referenceVariant == readVariant);
EXPECT_TRUE(variantsAreEqual);
@@ -459,7 +471,7 @@ TEST_F(InputStreamTest, ReadsStringVariants) {
}
TEST_F(InputStreamTest, ReadsVariantsWithAnArrayOfStrings) {
- typedef CommonAPI::Variant<int32_t, double, std::vector<std::string>> TestedStringArrayVariantType;
+ typedef CommonAPI::Variant<int32_t, double, std::vector<std::string>> TestedVariantType;
std::string testString1 = "Hello World with CommonAPI Variants!";
std::string testString2 = "What a beautiful world if there are working Arrays within Variants!!";
@@ -493,18 +505,16 @@ TEST_F(InputStreamTest, ReadsVariantsWithAnArrayOfStrings) {
CommonAPI::DBus::DBusMessage scopedMessage(libdbusMessage);
CommonAPI::DBus::DBusInputStream inStream(scopedMessage);
- TestedStringArrayVariantType referenceVariant(testInnerVector);
+ TestedVariantType referenceVariant(testInnerVector);
//Variant: structAlign + type-index(1) + variantSignature(4) + padding(3) + arrayLength(4) + stringLength(4) +
// string(37) + padding(3) + stringLength(4) + string(69) = 129
EXPECT_EQ(129 + 7 + 129, scopedMessage.getBodyLength());
for (int i = 0; i < numOfElements; i += 1) {
- TestedStringArrayVariantType readVariant;
+ TestedVariantType readVariant;
inStream >> readVariant;
- bool readSuccess;
- std::vector<std::string> actualResult = readVariant.get<std::vector<std::string>>(readSuccess);
- EXPECT_TRUE(readSuccess);
+ std::vector<std::string> actualResult = readVariant.get<std::vector<std::string>>();
bool variantsAreEqual = (referenceVariant == readVariant);
EXPECT_TRUE(variantsAreEqual);
@@ -513,6 +523,260 @@ TEST_F(InputStreamTest, ReadsVariantsWithAnArrayOfStrings) {
}
+TEST_F(InputStreamTest, ReadsVariantsWithVariants) {
+ typedef CommonAPI::Variant<int8_t, uint64_t, CommonAPI::ByteBuffer> InnerVariantType;
+
+ typedef CommonAPI::Variant<int32_t,
+ double,
+ std::string,
+ InnerVariantType>
+ TestedVariantType;
+
+ int8_t outerVariantTypeIndex = 1;
+ int8_t innerVariant1TypeIndex = 1;
+ int8_t innerVariant2TypeIndex = 3;
+
+ const uint32_t byteBufferElementCount = numOfElements*10;
+
+ CommonAPI::ByteBuffer innerVariant1Value;
+ for (int i = 0; i < byteBufferElementCount; ++i) {
+ innerVariant1Value.push_back((char) (i+40));
+ }
+
+ int8_t innerVariant2Value = -55;
+
+
+ DBusMessageIter outerVariantStructIter;
+ DBusMessageIter outerVariantActualIter;
+ DBusMessageIter innerVariantStructIter;
+ DBusMessageIter innerVariantActualIterator;
+ DBusMessageIter innerArrayIter;
+
+
+ //begin 1. outer variant
+ dbus_message_iter_open_container(&libdbusMessageWriteIter, DBUS_TYPE_STRUCT, NULL, &outerVariantStructIter);
+ dbus_message_iter_append_basic(&outerVariantStructIter, DBUS_TYPE_BYTE, &outerVariantTypeIndex);
+ dbus_message_iter_open_container(&outerVariantStructIter, DBUS_TYPE_VARIANT, "v", &outerVariantActualIter);
+
+ //begin inner variant
+ dbus_message_iter_open_container(&outerVariantActualIter, DBUS_TYPE_STRUCT, NULL, &innerVariantStructIter);
+ dbus_message_iter_append_basic(&innerVariantStructIter, DBUS_TYPE_BYTE, &innerVariant1TypeIndex);
+ dbus_message_iter_open_container(&innerVariantStructIter, DBUS_TYPE_VARIANT, "ay", &innerVariantActualIterator);
+
+ //begin inner variant content
+ dbus_message_iter_open_container(&innerVariantActualIterator, DBUS_TYPE_ARRAY, "y", &innerArrayIter);
+ for (int i = 0; i < byteBufferElementCount; i++) {
+ dbus_message_iter_append_basic(&innerArrayIter, DBUS_TYPE_BYTE, &innerVariant1Value[i]);
+ }
+ dbus_message_iter_close_container(&innerVariantActualIterator, &innerArrayIter);
+ //end inner variant content
+
+ dbus_message_iter_close_container(&innerVariantStructIter, &innerVariantActualIterator);
+ dbus_message_iter_close_container(&outerVariantActualIter, &innerVariantStructIter);
+ //end inner variant
+
+ dbus_message_iter_close_container(&outerVariantStructIter, &outerVariantActualIter);
+ dbus_message_iter_close_container(&libdbusMessageWriteIter, &outerVariantStructIter);
+ //end 1. outer variant
+
+
+ //begin 2. outer variant
+ dbus_message_iter_open_container(&libdbusMessageWriteIter, DBUS_TYPE_STRUCT, NULL, &outerVariantStructIter);
+ dbus_message_iter_append_basic(&outerVariantStructIter, DBUS_TYPE_BYTE, &outerVariantTypeIndex);
+ dbus_message_iter_open_container(&outerVariantStructIter, DBUS_TYPE_VARIANT, "v", &outerVariantActualIter);
+
+ //begin inner variant
+ dbus_message_iter_open_container(&outerVariantActualIter, DBUS_TYPE_STRUCT, NULL, &innerVariantStructIter);
+ dbus_message_iter_append_basic(&innerVariantStructIter, DBUS_TYPE_BYTE, &innerVariant2TypeIndex);
+ dbus_message_iter_open_container(&innerVariantStructIter, DBUS_TYPE_VARIANT, "y", &innerVariantActualIterator);
+
+ //begin inner variant content
+ dbus_message_iter_append_basic(&innerVariantActualIterator, DBUS_TYPE_BYTE, &innerVariant2Value);
+ //end inner variant content
+
+ dbus_message_iter_close_container(&innerVariantStructIter, &innerVariantActualIterator);
+ dbus_message_iter_close_container(&outerVariantActualIter, &innerVariantStructIter);
+ //end inner variant
+
+ dbus_message_iter_close_container(&outerVariantStructIter, &outerVariantActualIter);
+ dbus_message_iter_close_container(&libdbusMessageWriteIter, &outerVariantStructIter);
+ //end 2. outer variant
+
+
+ CommonAPI::DBus::DBusMessage scopedMessage(libdbusMessage);
+ CommonAPI::DBus::DBusInputStream inStream(scopedMessage);
+
+ InnerVariantType referenceInnerVariant1(innerVariant1Value);
+ InnerVariantType referenceInnerVariant2(innerVariant2Value);
+
+ TestedVariantType referenceVariant1(referenceInnerVariant1);
+ TestedVariantType referenceVariant2(referenceInnerVariant2);
+
+ //Variant1: type-index(1) + varSigLen(1) + varSig(2) + struct-padding(4) + inner-type-index(1) + varLen(1) + varSig(3) +
+ // padding(3) + byteBufferLength(4) + byteBuffer(20) = 40
+ //Variant2: type-index(1) + varSigLen(1) + varSig(2) + struct-padding(4) + inner-type-index(1) + varLen(1) + varSig(2) +
+ // byte(1) = 13
+ // = 53
+ EXPECT_EQ(53, scopedMessage.getBodyLength());
+
+ TestedVariantType readVariant1;
+ TestedVariantType readVariant2;
+ inStream >> readVariant1;
+ inStream >> readVariant2;
+ EXPECT_EQ(referenceVariant1, readVariant1);
+ EXPECT_EQ(referenceVariant2, readVariant2);
+
+ InnerVariantType readInnerVariant1 = readVariant1.get<InnerVariantType>();
+ InnerVariantType readInnerVariant2 = readVariant2.get<InnerVariantType>();
+ EXPECT_EQ(referenceInnerVariant1, readInnerVariant1);
+ EXPECT_EQ(referenceInnerVariant2, readInnerVariant2);
+
+ CommonAPI::ByteBuffer readInnerValue1 = readInnerVariant1.get<CommonAPI::ByteBuffer>();
+ int8_t readInnerValue2 = readInnerVariant2.get<int8_t>();
+ EXPECT_EQ(innerVariant1Value, readInnerValue1);
+ EXPECT_EQ(innerVariant2Value, readInnerValue2);
+}
+
+
+TEST_F(InputStreamTest, ReadsVariantsWithStructs) {
+ typedef CommonAPI::Variant<int32_t,
+ double,
+ std::string,
+ bmw::test::TestSerializableStruct>
+ TestedVariantType;
+
+ int8_t variantTypeIndex = 1;
+
+ bmw::test::TestSerializableStruct testStruct;
+ testStruct.a = 15;
+ testStruct.b = -32;
+ testStruct.c = false;
+ testStruct.d = "Hello all!";
+ testStruct.e = 3.414;
+
+
+ DBusMessageIter variantStructIter;
+ DBusMessageIter variantActualIter;
+ DBusMessageIter innerStructIter;
+
+
+ //begin variant
+ dbus_message_iter_open_container(&libdbusMessageWriteIter, DBUS_TYPE_STRUCT, NULL, &variantStructIter);
+ dbus_message_iter_append_basic(&variantStructIter, DBUS_TYPE_BYTE, &variantTypeIndex);
+ dbus_message_iter_open_container(&variantStructIter, DBUS_TYPE_VARIANT, "(unbsd)", &variantActualIter);
+
+ //begin variant content
+ dbus_message_iter_open_container(&variantActualIter, DBUS_TYPE_STRUCT, NULL, &innerStructIter);
+
+ dbus_bool_t dbusBool = 0;
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_UINT32, &testStruct.a);
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_INT16, &testStruct.b);
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_BOOLEAN, &dbusBool);
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_STRING, &testStruct.d);
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_DOUBLE, &testStruct.e);
+
+ dbus_message_iter_close_container(&variantActualIter, &innerStructIter);
+ //end variant content
+
+ dbus_message_iter_close_container(&variantStructIter, &variantActualIter);
+ dbus_message_iter_close_container(&libdbusMessageWriteIter, &variantStructIter);
+ //end variant
+
+ CommonAPI::DBus::DBusMessage scopedMessage(libdbusMessage);
+ CommonAPI::DBus::DBusInputStream inStream(scopedMessage);
+ TestedVariantType referenceVariant(testStruct);
+
+ //type-index(1) + varSigLen(1) + varSig(8) + struct-padding(6) + uint32(4) + int16(2) + padding(2) + bool(4) +
+ //stringLen(4) + stringVal(11) + padding(5) + double(8) = 56
+ EXPECT_EQ(56, scopedMessage.getBodyLength());
+
+ TestedVariantType readVariant;
+ inStream >> readVariant;
+
+ bmw::test::TestSerializableStruct readStruct = readVariant.get<bmw::test::TestSerializableStruct>();
+ EXPECT_EQ(testStruct.a, readStruct.a);
+ EXPECT_EQ(testStruct.b, readStruct.b);
+ EXPECT_EQ(testStruct.c, readStruct.c);
+ EXPECT_EQ(testStruct.d, readStruct.d);
+ EXPECT_EQ(testStruct.e, readStruct.e);
+ EXPECT_EQ(testStruct, readStruct);
+ EXPECT_EQ(referenceVariant, readVariant);
+}
+
+
+TEST_F(InputStreamTest, ReadsVariantsWithAnArrayOfStructs) {
+ typedef CommonAPI::Variant<int32_t, double, std::vector<bmw::test::TestSerializableStruct>> TestedVariantType;
+
+ bmw::test::TestSerializableStruct testStruct;
+ testStruct.a = 15;
+ testStruct.b = -32;
+ testStruct.c = false;
+ testStruct.d = "Hello all!";
+ testStruct.e = 3.414;
+
+ int8_t variantTypeIndex = 1;
+
+ DBusMessageIter subIter;
+ DBusMessageIter subSubIter;
+ DBusMessageIter innerArrayIter;
+ DBusMessageIter innerStructIter;
+
+ //begin variant
+ dbus_message_iter_open_container(&libdbusMessageWriteIter, DBUS_TYPE_STRUCT, NULL, &subIter);
+ dbus_message_iter_append_basic(&subIter, DBUS_TYPE_BYTE, &variantTypeIndex);
+ dbus_message_iter_open_container(&subIter, DBUS_TYPE_VARIANT, "a(unbsd)", &subSubIter);
+
+ //begin array
+ dbus_message_iter_open_container(&subSubIter, DBUS_TYPE_ARRAY, "(unbsd)", &innerArrayIter);
+
+ //begin struct
+ dbus_message_iter_open_container(&innerArrayIter, DBUS_TYPE_STRUCT, NULL, &innerStructIter);
+
+ dbus_bool_t dbusBool = 0;
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_UINT32, &testStruct.a);
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_INT16, &testStruct.b);
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_BOOLEAN, &dbusBool);
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_STRING, &testStruct.d);
+ dbus_message_iter_append_basic(&innerStructIter, DBUS_TYPE_DOUBLE, &testStruct.e);
+
+ dbus_message_iter_close_container(&innerArrayIter, &innerStructIter);
+ //end struct
+
+ dbus_message_iter_close_container(&subSubIter, &innerArrayIter);
+ //end array
+
+ dbus_message_iter_close_container(&subIter, &subSubIter);
+ dbus_message_iter_close_container(&libdbusMessageWriteIter, &subIter);
+ //end variant
+
+ CommonAPI::DBus::DBusMessage scopedMessage(libdbusMessage);
+ CommonAPI::DBus::DBusInputStream inStream(scopedMessage);
+
+ std::vector<bmw::test::TestSerializableStruct> referenceInnerVector;
+ referenceInnerVector.push_back(testStruct);
+ TestedVariantType referenceVariant(referenceInnerVector);
+
+ //type-index(1) + varSigLen(1) + variantSig(9) + padding(1) + arrayLength(4) + uint32(4) + int16(2) + padding(2)
+ //bool(4) + stringLength(4) + string(11) + padding(5) + double(8) = 56
+ EXPECT_EQ(56, scopedMessage.getBodyLength());
+
+ TestedVariantType readVariant;
+ inStream >> readVariant;
+
+ std::vector<bmw::test::TestSerializableStruct> actualResult = readVariant.get<std::vector<bmw::test::TestSerializableStruct>>();
+ bmw::test::TestSerializableStruct readStruct = actualResult[0];
+
+ EXPECT_EQ(testStruct.a, readStruct.a);
+ EXPECT_EQ(testStruct.b, readStruct.b);
+ EXPECT_EQ(testStruct.c, readStruct.c);
+ EXPECT_EQ(testStruct.d, readStruct.d);
+ EXPECT_EQ(testStruct.e, readStruct.e);
+ EXPECT_EQ(testStruct, readStruct);
+ EXPECT_EQ(referenceInnerVector, actualResult);
+ EXPECT_EQ(referenceVariant, readVariant);
+}
+
+
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
diff --git a/src/test/DBusVariantTest.cpp b/src/test/DBusVariantTest.cpp
index 07e6f8a..172f944 100644
--- a/src/test/DBusVariantTest.cpp
+++ b/src/test/DBusVariantTest.cpp
@@ -12,97 +12,141 @@ using namespace CommonAPI;
class VariantTest: public ::testing::Test {
- void SetUp() {
- }
+ protected:
+ typedef Variant<int, double, std::string> BasicVariantType;
- void TearDown() {
- }
+ void SetUp() {
+ }
+
+ void TearDown() {
+ }
+
+ const int fromInt = 5;
+ const double fromDouble = 12.344d;
+ const std::string fromString = "123abcsadfaljkawlöfasklöerklöfjasklfjysklfjaskfjsklösdfdko4jdfasdjioögjopefgip3rtgjiprg!";
};
-TEST_F(VariantTest, VariantTestPack) {
+TEST_F(VariantTest, HandlesInts) {
+ BasicVariantType myVariant(fromInt);
+
+ EXPECT_TRUE(myVariant.isType<int>());
+ EXPECT_FALSE(myVariant.isType<double>());
+ EXPECT_FALSE(myVariant.isType<std::string>());
+
+ const int myInt = myVariant.get<int>();
+ EXPECT_EQ(myInt, fromInt);
+
+ EXPECT_ANY_THROW(myVariant.get<double>());
+ EXPECT_ANY_THROW(myVariant.get<std::string>());
+}
+
+TEST_F(VariantTest, HandlesDoubles) {
+ BasicVariantType myVariant(fromDouble);
- int fromInt = 5;
- double fromDouble = 12.344d;
- std::string fromString = "123abcsadfaljkawlöfasklöerklöfjasklfjysklfjaskfjsklösdfdko4jdfasdjioögjopefgip3rtgjiprg!";
- Variant<int, double, std::string> myVariant(fromInt);
+ EXPECT_FALSE(myVariant.isType<int>());
+ EXPECT_TRUE(myVariant.isType<double>());
+ EXPECT_FALSE(myVariant.isType<std::string>());
- Variant<int, double, std::string>* myVariants = new Variant<int, double, std::string>(fromString);
+ EXPECT_ANY_THROW(myVariant.get<int>());
- Variant<int, double, std::string> myVariantf(fromDouble);
+ const double myDouble = myVariant.get<double>();
+ EXPECT_EQ(myDouble, fromDouble);
- bool success;
+ EXPECT_ANY_THROW(myVariant.get<std::string>());
+}
- std::string myString = myVariants->get<std::string>(success);
- std::cout << "myString = " << myString << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
+TEST_F(VariantTest, HandlesStrings) {
+ BasicVariantType myVariant(fromString);
- const int& myInt = myVariant.get<int>(success);
- std::cout << "myInt = " << myInt << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
+ EXPECT_FALSE(myVariant.isType<int>());
+ EXPECT_FALSE(myVariant.isType<double>());
+ EXPECT_TRUE(myVariant.isType<std::string>());
- Variant<int, double, std::string> myVariant2 = myVariant;
- const int& myInt2 = myVariant2.get<int>(success);
- std::cout << "myInt2 = " << myInt2 << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
+ EXPECT_ANY_THROW(myVariant.get<int>());
+ EXPECT_ANY_THROW(myVariant.get<double>());
- Variant<int, double, std::string> myVariant3 = fromInt;
- const int& myInt3 = myVariant3.get<int>(success);
- std::cout << "myInt3 = " << myInt3 << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
+ const std::string myString = myVariant.get<std::string>();
+ EXPECT_EQ(myString, fromString);
+}
- myString = myVariants->get<std::string>(success);
- std::cout << "myString = " << myString << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
+TEST_F(VariantTest, HandlesStringVectors) {
+ typedef Variant<int, double, std::vector<std::string>> VectorVariantType;
- Variant<int, double, std::string> myVariantCopy(myVariant);
- const int& myIntCopy = myVariantCopy.get<int>(success);
- std::cout << "myIntCopy = " << myIntCopy << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
+ std::vector<std::string> testVector;
+ for(int i = 0; i < 10; i++) {
+ testVector.push_back(fromString);
+ }
- std::cout << "myIntCopy equals myInt= " << "(" << std::boolalpha << (myVariant == myVariantCopy) << ")\n";
- EXPECT_TRUE((myVariant == myVariantCopy));
+ VectorVariantType vectorVariant(testVector);
+ const std::vector<std::string> resultVector = vectorVariant.get<std::vector<std::string>>();
+ EXPECT_EQ(resultVector, testVector);
+}
- const int& myFake = myVariant.get<double>(success);
- std::cout << "myFake = " << myFake << " (" << std::boolalpha << success << ")\n";
- EXPECT_FALSE(success);
+TEST_F(VariantTest, HandlesAssignment) {
+ Variant<int, double, std::string> myVariant = fromInt;
- std::cout << "myInt is int = " << " (" << std::boolalpha << myVariant.isType<int>() << ")\n";
- EXPECT_TRUE(myVariant.isType<int>());
+ myVariant = fromString;
- std::cout << "myInt is std::string = " << " (" << std::boolalpha << myVariant.isType<std::string>() << ")\n";
+ EXPECT_FALSE(myVariant.isType<int>());
+ EXPECT_FALSE(myVariant.isType<double>());
+ EXPECT_TRUE(myVariant.isType<std::string>());
+
+ EXPECT_ANY_THROW(myVariant.get<int>());
+
+ const std::string myString = myVariant.get<std::string>();
+ EXPECT_EQ(myString, fromString);
+}
+
+TEST_F(VariantTest, HandlesVariantConstructionByAssignment) {
+ Variant<int, double, std::string> myVariant = fromInt;
+
+ EXPECT_TRUE(myVariant.isType<int>());
+ EXPECT_FALSE(myVariant.isType<double>());
EXPECT_FALSE(myVariant.isType<std::string>());
- const double& myDouble = myVariantf.get<double>(success);
- std::cout << "myDouble = " << myDouble << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
+ const int myInt = myVariant.get<int>();
+ EXPECT_EQ(myInt, fromInt);
+}
- Variant<int, double, std::string> myVariantsCopy(*myVariants);
- std::string myStringCopy = myVariantsCopy.get<std::string>(success);
- std::cout << "myStringCopy = " << myStringCopy << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
-// EXPECT_TRUE((myVariants == myVariantsCopy));
+TEST_F(VariantTest, HandlesVariantCopy) {
+ BasicVariantType myVariant(fromInt);
+ Variant<int, double, std::string> myVariantAssigned = myVariant;
- bool s2;
- myVariants->set<std::string>(std::string("Hello World"), s2);
- myString = myVariants->get<std::string>(success);
- std::cout << "myString = " << myString << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
+ EXPECT_TRUE(myVariantAssigned.isType<int>());
+ EXPECT_FALSE(myVariantAssigned.isType<double>());
+ EXPECT_FALSE(myVariantAssigned.isType<std::string>());
- myStringCopy = myVariantsCopy.get<std::string>(success);
- std::cout << "myStringCopy = " << myStringCopy << " (" << std::boolalpha << success << ")\n";
- EXPECT_TRUE(success);
+ const int myInt2 = myVariantAssigned.get<int>();
+ EXPECT_EQ(myInt2, fromInt);
- std::vector<std::string> testVector;
- testVector.push_back(fromString);
- CommonAPI::Variant<int32_t, double, std::vector<std::string>> vectorVariant(testVector);
- std::vector<std::string> resultVector = vectorVariant.get<std::vector<std::string>>(success);
- std::cout << "myVectorVariant read successfully= " << std::boolalpha << success << "\n";
- EXPECT_TRUE(success);
- EXPECT_EQ(resultVector, testVector);
+ Variant<int, double, std::string> myVariantCopied(myVariant);
+ EXPECT_EQ(myVariant, myVariantCopied);
+
+ EXPECT_TRUE(myVariantCopied.isType<int>());
+ EXPECT_FALSE(myVariantCopied.isType<double>());
+ EXPECT_FALSE(myVariantCopied.isType<std::string>());
+
+ const int& myIntCopy = myVariantCopied.get<int>();
+ EXPECT_EQ(myIntCopy, fromInt);
+}
- delete myVariants;
+TEST_F(VariantTest, HandlesVariantsWithinVariants) {
+ typedef Variant<int, double, BasicVariantType > VariantInVariantType;
+ BasicVariantType fromInnerVariant(fromInt);
+ VariantInVariantType myOuterVariant(fromInnerVariant);
+
+ EXPECT_FALSE(myOuterVariant.isType<int>());
+ EXPECT_FALSE(myOuterVariant.isType<double>());
+ EXPECT_TRUE(myOuterVariant.isType<BasicVariantType>());
+
+ EXPECT_ANY_THROW(myOuterVariant.get<int>());
+ EXPECT_ANY_THROW(myOuterVariant.get<double>());
+
+ const BasicVariantType myInnerVariant = myOuterVariant.get<BasicVariantType>();
+ EXPECT_EQ(fromInnerVariant, myInnerVariant);
}
+
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();