Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <gtest/gtest.h>
00031
00032 #include <rve_properties/property_value.h>
00033
00034 using namespace rve_properties;
00035
00036 struct Foo
00037 {
00038 uint64_t a;
00039 uint64_t b;
00040 uint64_t c;
00041 uint64_t d;
00042
00043 bool operator==(const Foo& rhs) const
00044 {
00045 return a == rhs.a && b == rhs.b && c == rhs.c && d == rhs.d;
00046 }
00047 };
00048
00049 namespace rve_properties
00050 {
00051
00052 static uint32_t g_foo_to_stream_count = 0;
00053
00054 template<>
00055 struct ValueTraits<Foo>
00056 {
00057 inline static const char* typeName() { return "Foo"; }
00058
00059 inline static std::string toString(void* mem)
00060 {
00061 ++g_foo_to_stream_count;
00062 Foo* f = reinterpret_cast<Foo*>(mem);
00063 std::ostringstream os;
00064 os << f->a << " " << f->b << " " << f->c << " " << f->d;
00065 return os.str();
00066 }
00067
00068 inline static void fromString(void* mem, const std::string& str)
00069 {
00070 Foo* f = reinterpret_cast<Foo*>(mem);
00071 std::istringstream is(str);
00072 is >> f->a;
00073 is.ignore();
00074 is >> f->b;
00075 is.ignore();
00076 is >> f->c;
00077 is.ignore();
00078 is >> f->d;
00079 }
00080 };
00081
00082 }
00083
00084 TEST(PropertyValue, setThenGetSameType)
00085 {
00086 PropertyValue pv;
00087 uint32_t val = 5;
00088 pv.set(val);
00089 val = 10;
00090 val = pv.get<uint32_t>();
00091 EXPECT_EQ(val, 5U);
00092 }
00093
00094 TEST(PropertyValue, setIntThenGetFloat)
00095 {
00096 PropertyValue pv;
00097 pv.set<uint32_t>(25);
00098 float val = pv.get<float>();
00099 EXPECT_EQ(val, 25.0f);
00100 }
00101
00102 std::istream& operator>>(std::istream& is, Foo& foo)
00103 {
00104 is >> foo.a;
00105 is.ignore();
00106 is >> foo.b;
00107 is.ignore();
00108 is >> foo.c;
00109 is.ignore();
00110 is >> foo.d;
00111 return is;
00112 }
00113
00114 TEST(PropertyValue, setThenGetSamelargeStruct)
00115 {
00116 g_foo_to_stream_count = 0;
00117
00118 Foo f;
00119 f.a = 10;
00120 f.b = 100;
00121 f.c = 1000;
00122 f.d = 10000;
00123 PropertyValue pv;
00124 pv.set(f);
00125 f = pv.get<Foo>();
00126
00127 EXPECT_EQ(f.a, 10U);
00128 EXPECT_EQ(f.b, 100U);
00129 EXPECT_EQ(f.c, 1000U);
00130 EXPECT_EQ(f.d, 10000U);
00131 EXPECT_EQ(g_foo_to_stream_count, 0U);
00132 }
00133
00134 TEST(PropertyValue, setLargeStructThenGetString)
00135 {
00136 g_foo_to_stream_count = 0;
00137
00138 Foo f;
00139 f.a = 10;
00140 f.b = 100;
00141 f.c = 1000;
00142 f.d = 10000;
00143 PropertyValue pv;
00144 pv.set(f);
00145 std::string str;
00146 pv.get(str);
00147
00148 EXPECT_STREQ(str.c_str(), "10 100 1000 10000");
00149
00150 EXPECT_EQ(g_foo_to_stream_count, 1U);
00151 }
00152
00153 TEST(PropertyValue, setThenGetString)
00154 {
00155 PropertyValue pv;
00156 pv.set(std::string("foo bar baz"));
00157 std::string s;
00158 pv.get(s);
00159 EXPECT_STREQ(s.c_str(), "foo bar baz");
00160 }
00161
00162 TEST(PropertyValue, setCharStarThenGetString)
00163 {
00164 PropertyValue pv;
00165 pv.set("foo bar baz");
00166 std::string s;
00167 pv.get(s);
00168 EXPECT_STREQ(s.c_str(), "foo bar baz");
00169 }
00170
00171 bool g_destructor_called = false;
00172 struct Bar
00173 {
00174 ~Bar()
00175 {
00176 g_destructor_called = true;
00177 }
00178 };
00179
00180 std::ostream& operator<<(std::ostream& os, const Bar& bar)
00181 {
00182 return os;
00183 }
00184
00185 std::istream& operator>>(std::istream& is, Bar& bar)
00186 {
00187 return is;
00188 }
00189
00190 TEST(PropertyValue, destructor)
00191 {
00192 PropertyValue pv;
00193 pv.set(Bar());
00194
00195 g_destructor_called = false;
00196
00197 pv.set(5);
00198 EXPECT_TRUE(g_destructor_called);
00199 }
00200
00201 TEST(PropertyValue, copySmall)
00202 {
00203 PropertyValue pv;
00204 pv.set(5);
00205 PropertyValue pv2;
00206 pv2 = pv;
00207 EXPECT_EQ(pv2.get<uint32_t>(), 5U);
00208 }
00209
00210 TEST(PropertyValue, copyLarge)
00211 {
00212 Foo foo;
00213 foo.a = 10;
00214 foo.b = 100;
00215 foo.c = 1000;
00216 foo.d = 10000;
00217 PropertyValue pv;
00218 pv.set(foo);
00219 PropertyValue pv2;
00220 pv2 = pv;
00221 EXPECT_TRUE(pv2.get<Foo>() == foo);
00222 }
00223
00224 int main(int argc, char** argv)
00225 {
00226 std::cout << "sizeof(PropertyValue): " << sizeof(PropertyValue) << "\n";
00227 testing::InitGoogleTest(&argc, argv);
00228 return RUN_ALL_TESTS();
00229 }