VariantTest.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2014 by Ralf Kaestner *
3  * ralf.kaestner@gmail.com *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the Lesser GNU General Public License as published by*
7  * the Free Software Foundation; either version 3 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * Lesser GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the Lesser GNU General Public License *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  ******************************************************************************/
18 
19 #include <gtest/gtest.h>
20 
21 #include <std_msgs/Bool.h>
22 #include <std_msgs/String.h>
23 
24 #include <variant_msgs/Test.h>
25 
31 
32 using namespace variant_topic_tools;
33 
34 TEST(Variant, Builtin) {
35  BuiltinVariant v1 = DataType("float64").createVariant();
36  BuiltinVariant v2 = DataType("bool").createVariant();
37  BuiltinVariant v3 = DataType("duration").createVariant();
38  BuiltinVariant v4 = DataType("time").createVariant();
39 
40  EXPECT_TRUE(v1.hasType());
41  EXPECT_FALSE(v1.isEmpty());
42  EXPECT_EQ(typeid(double), v1.getValueTypeInfo());
43  EXPECT_EQ(0.0, v1.getValue<double>());
44  EXPECT_FALSE(v1.isEmpty());
45  EXPECT_ANY_THROW(v1.getValue<int>());
46  EXPECT_ANY_THROW(v1.setValue(0));
47 
48  v1 = M_PI;
49 
50  EXPECT_EQ(M_PI, v1.getValue<double>());
51  EXPECT_TRUE(v1 == M_PI);
52  EXPECT_FALSE(v1 != M_PI);
53  EXPECT_TRUE(v1 != 1.0);
54  EXPECT_FALSE(v1 == 1.0);
55 
56  v1 = 1.0;
57 
58  EXPECT_TRUE(v1 != 1);
59  EXPECT_FALSE(v1 == 1);
60  EXPECT_EQ(1.0, v1.getNumericValue());
61 
62  v1.clear();
63 
64  EXPECT_FALSE(v1.hasType());
65  EXPECT_TRUE(v1.isEmpty());
66 
67  EXPECT_EQ(typeid(uint8_t), v2.getValueTypeInfo());
68  EXPECT_EQ(0.0, v2.getNumericValue());
69 
70  EXPECT_EQ(0.0, v3.getNumericValue());
71 
72  EXPECT_EQ(0.0, v4.getNumericValue());
73 }
74 
75 TEST(Variant, Array) {
76  DataTypeRegistry registry;
77 
78  ArrayVariant v1 = DataType("int32[3]").createVariant();
79  ArrayVariant v2 = DataType("float64[]").createVariant();
80 
81  EXPECT_TRUE(v1.hasType());
82  EXPECT_FALSE(v1.isEmpty());
83  EXPECT_EQ(3, v1.getNumMembers());
84  EXPECT_NO_THROW(v1[0] = 0);
85  EXPECT_NO_THROW(v1[1] = 0);
86  EXPECT_NO_THROW(v1[2] = 0);
87  EXPECT_NO_THROW(v1.resize(3));
88  EXPECT_ANY_THROW(v1.resize(4));
89  EXPECT_TRUE(v1[0].hasType());
90  EXPECT_FALSE(v1[0].isEmpty());
91  EXPECT_NO_THROW(v1[0] = 0);
92  EXPECT_NO_THROW(v1[1] = 1);
93  EXPECT_NO_THROW(v1[2] = 2);
94  EXPECT_ANY_THROW(v1[3] = 3);
95  EXPECT_EQ(0, v1[0].getValue<int>());
96  EXPECT_EQ(1, v1[1].getValue<int>());
97  EXPECT_EQ(2, v1[2].getValue<int>());
98  EXPECT_ANY_THROW(v1.clear());
99 
100  EXPECT_TRUE(v2.hasType());
101  EXPECT_TRUE(v2.isEmpty());
102  EXPECT_EQ(0, v2.getNumMembers());
103  EXPECT_NO_THROW(v2.resize(3));
104  EXPECT_FALSE(v2.isEmpty());
105  EXPECT_EQ(3, v2.getNumMembers());
106  EXPECT_TRUE(v2[0].hasType());
107  EXPECT_FALSE(v2[0].isEmpty());
108  EXPECT_NO_THROW(v2[0] = 0.0);
109  EXPECT_NO_THROW(v2[1] = 1.0);
110  EXPECT_NO_THROW(v2[2] = 2.0);
111  EXPECT_ANY_THROW(v2[3] = 3);
112  EXPECT_EQ(0.0, v2[0].getValue<double>());
113  EXPECT_EQ(1.0, v2[1].getValue<double>());
114  EXPECT_EQ(2.0, v2[2].getValue<double>());
115  EXPECT_NO_THROW(v2.clear());
116  EXPECT_TRUE(v2.isEmpty());
117 
118  registry.clear();
119 }
120 
122  DataTypeRegistry registry;
123 
124  MessageVariant v1(MessageDefinition::create<std_msgs::Bool>().
125  getMessageDataType().createVariant());
126  MessageVariant v2(MessageDefinition::create<std_msgs::String>().
127  getMessageDataType().createVariant());
128  MessageVariant v3(MessageDefinition::create<variant_msgs::Test>().
129  getMessageDataType().createVariant());
130  MessageVariant v4(registry.getDataType<variant_msgs::Test>().
131  createVariant());
132 
133  EXPECT_TRUE(v1.hasType());
134  EXPECT_FALSE(v1.isEmpty());
135  EXPECT_EQ(typeid(void), v1.getValueTypeInfo());
136  EXPECT_EQ(1, v1.getNumMembers());
137  EXPECT_TRUE(v1["data"].hasType());
138  EXPECT_FALSE(v1["data"].isEmpty());
139  EXPECT_NO_THROW(v1["data"] = true);
140  EXPECT_EQ(true, v1["data"].getValue<bool>());
141 
142  EXPECT_TRUE(v2.hasType());
143  EXPECT_FALSE(v2.isEmpty());
144  EXPECT_EQ(typeid(void), v2.getValueTypeInfo());
145  EXPECT_EQ(1, v2.getNumMembers());
146  EXPECT_TRUE(v2["data"].hasType());
147  EXPECT_FALSE(v2["data"].isEmpty());
148  EXPECT_NO_THROW(v2["data"] = "Test");
149  EXPECT_EQ("Test", v2["data"].getValue<std::string>());
150 
151  EXPECT_TRUE(v3.hasType());
152  EXPECT_FALSE(v3.isEmpty());
153  EXPECT_EQ(typeid(void), v3.getValueTypeInfo());
154  EXPECT_TRUE(v3["header"].hasType());
155  EXPECT_FALSE(v3["header"].isEmpty());
156  EXPECT_EQ(typeid(void), v3["header"].getValueTypeInfo());
157  EXPECT_NO_THROW(v3["builtin_string"] = "Test");
158  EXPECT_EQ("Test", v3["builtin_string"].getValue<std::string>());
159 
160  EXPECT_TRUE(v4.hasType());
161  EXPECT_FALSE(v4.isEmpty());
162  EXPECT_EQ(typeid(variant_msgs::Test), v4.getValueTypeInfo());
163  EXPECT_TRUE(v4["header"].hasType());
164  EXPECT_FALSE(v4["header"].isEmpty());
165  EXPECT_EQ(typeid(std_msgs::Header), v4["header"].getValueTypeInfo());
166  EXPECT_NO_THROW(v4["builtin_string"] = "Test");
167  EXPECT_EQ("Test", v4["builtin_string"].getValue<std::string>());
168  EXPECT_EQ(typeid(bool[3]),
169  v4["builtin_boolean_array"].getType().getTypeInfo());
170  EXPECT_EQ(typeid(boost::array<uint8_t, 3>),
171  v4["builtin_boolean_array"].getValueTypeInfo());
172 
173  registry.clear();
174 }
Header file providing the MessageDefinition class interface.
void clear()
Clear the array.
Generic message type.
Definition: Message.h:43
void resize(size_t numMembers)
Resize the array.
bool hasType() const
True, if the variant has a type.
Definition: Variant.cpp:63
Header file providing the MessageVariant class interface.
double getNumericValue() const
Retrieve the built-in variant&#39;s numeric value.
void clear()
Clear the data type registry.
TEST(Variant, Builtin)
Definition: VariantTest.cpp:34
void setValue(const T &value)
Set the variant&#39;s value.
type_traits::DataType< T >::ValueType & getValue()
Retrieve the variant&#39;s value (non-const version)
Header file providing the DataTypeRegistry class interface.
Header file providing the ArrayVariant class interface.
DataType getDataType(const std::string &identifier)
Retrieve a data type from the registry by identifier (non-const version)
bool isEmpty() const
True, if the collection is empty.
size_t getNumMembers() const
Retrieve the number of members of the collection.
bool isEmpty() const
True, if the variant is empty, i.e., does not have a value assigned.
Definition: Variant.cpp:95
Header file providing the BuiltinVariant class interface.
Variant createVariant() const
Create a variant from this data type.
Definition: DataType.cpp:168
const std::type_info & getValueTypeInfo() const
Retrieve the value type information of this variant.
Definition: Variant.cpp:56


variant_topic_tools
Author(s): Ralf Kaestner
autogenerated on Sat Jan 9 2021 03:56:50