diff options
author | Kazuki Ohta <kzk@il.is.s.u-tokyo.ac.jp> | 2009-08-26 11:52:12 +0900 |
---|---|---|
committer | Kazuki Ohta <kzk@il.is.s.u-tokyo.ac.jp> | 2009-08-26 11:52:12 +0900 |
commit | 7186edc45e057aff9f10511547c3f94c16c9c773 (patch) | |
tree | 877908e5d95a21a8d7197e56493226ce2f15af2c /cpp/test.cpp | |
parent | c94772104d70290f438cf629ff693a8d7e69f827 (diff) | |
download | msgpack-python-7186edc45e057aff9f10511547c3f94c16c9c773.tar.gz |
C: more strict tests for float, double
C++: more strict tests for float, double & enum, union member
Diffstat (limited to 'cpp/test.cpp')
-rw-r--r-- | cpp/test.cpp | 141 |
1 files changed, 132 insertions, 9 deletions
diff --git a/cpp/test.cpp b/cpp/test.cpp index f2ff1ab..fc3f9b4 100644 --- a/cpp/test.cpp +++ b/cpp/test.cpp @@ -124,15 +124,19 @@ TEST(MSGPACK, simple_buffer_int64) TEST(MSGPACK, simple_buffer_float) { vector<float> v; - v.push_back(0); - v.push_back(1); - v.push_back(2); + v.push_back(0.0); + v.push_back(-0.0); + v.push_back(1.0); + v.push_back(-1.0); v.push_back(numeric_limits<float>::min()); v.push_back(numeric_limits<float>::max()); v.push_back(nanf("tag")); v.push_back(1.0/0.0); // inf - for (unsigned int i = 0; i < kLoop; i++) + v.push_back(-(1.0/0.0)); // -inf + for (unsigned int i = 0; i < kLoop; i++) { v.push_back(drand48()); + v.push_back(-drand48()); + } for (unsigned int i = 0; i < v.size() ; i++) { msgpack::sbuffer sbuf; float val1 = v[i]; @@ -157,15 +161,19 @@ TEST(MSGPACK, simple_buffer_float) TEST(MSGPACK, simple_buffer_double) { vector<double> v; - v.push_back(0); - v.push_back(1); - v.push_back(2); + v.push_back(0.0); + v.push_back(-0.0); + v.push_back(1.0); + v.push_back(-1.0); v.push_back(numeric_limits<double>::min()); v.push_back(numeric_limits<double>::max()); v.push_back(nanf("tag")); v.push_back(1.0/0.0); // inf - for (unsigned int i = 0; i < kLoop; i++) + v.push_back(-(1.0/0.0)); // -inf + for (unsigned int i = 0; i < kLoop; i++) { v.push_back(drand48()); + v.push_back(-drand48()); + } for (unsigned int i = 0; i < v.size() ; i++) { msgpack::sbuffer sbuf; double val1 = v[i]; @@ -383,7 +391,7 @@ TEST(MSGPACK_USER_DEFINED, simple_buffer_class) obj.convert(&val2); EXPECT_EQ(val1.i, val2.i); EXPECT_EQ(val1.s, val2.s); - } + } } class TestClass2 @@ -441,3 +449,118 @@ TEST(MSGPACK_USER_DEFINED, simple_buffer_class_new_to_old) EXPECT_FALSE(val2.s.empty()); } } + +class TestEnumMemberClass +{ +public: + TestEnumMemberClass() + : t1(STATE_A), t2(STATE_B), t3(STATE_C) {} + + enum TestEnumType { + STATE_INVALID = 0, + STATE_A = 1, + STATE_B = 2, + STATE_C = 3 + }; + TestEnumType t1; + TestEnumType t2; + TestEnumType t3; + + MSGPACK_DEFINE((int&)t1, (int&)t2, (int&)t3); +}; + +TEST(MSGPACK_USER_DEFINED, simple_buffer_enum_member) +{ + TestEnumMemberClass val1; + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, val1); + msgpack::zone z; + msgpack::object obj; + msgpack::unpack_return ret = + msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj); + EXPECT_EQ(ret, msgpack::UNPACK_SUCCESS); + TestEnumMemberClass val2; + val2.t1 = TestEnumMemberClass::STATE_INVALID; + val2.t2 = TestEnumMemberClass::STATE_INVALID; + val2.t3 = TestEnumMemberClass::STATE_INVALID; + obj.convert(&val2); + EXPECT_EQ(val1.t1, val2.t1); + EXPECT_EQ(val1.t2, val2.t2); + EXPECT_EQ(val1.t3, val2.t3); +} + +class TestUnionMemberClass +{ +public: + TestUnionMemberClass() {} + TestUnionMemberClass(double f) { + is_double = true; + value.f = f; + } + TestUnionMemberClass(int i) { + is_double = false; + value.i = i; + } + + union { + double f; + int i; + } value; + bool is_double; + + template <typename Packer> + void msgpack_pack(Packer& pk) const + { + if (is_double) + pk.pack(msgpack::type::tuple<bool, double>(true, value.f)); + else + pk.pack(msgpack::type::tuple<bool, int>(false, value.i)); + } + + void msgpack_unpack(msgpack::object o) + { + msgpack::type::tuple<bool, msgpack::object> tuple; + o.convert(&tuple); + + is_double = tuple.get<0>(); + if (is_double) + tuple.get<1>().convert(&value.f); + else + tuple.get<1>().convert(&value.i); + } +}; + +TEST(MSGPACK_USER_DEFINED, simple_buffer_union_member) +{ + { + // double + TestUnionMemberClass val1(1.0); + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, val1); + msgpack::zone z; + msgpack::object obj; + msgpack::unpack_return ret = + msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj); + EXPECT_EQ(ret, msgpack::UNPACK_SUCCESS); + TestUnionMemberClass val2; + obj.convert(&val2); + EXPECT_EQ(val1.is_double, val2.is_double); + EXPECT_TRUE(fabs(val1.value.f - val2.value.f) < kEPS); + } + { + // int + TestUnionMemberClass val1(1); + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, val1); + msgpack::zone z; + msgpack::object obj; + msgpack::unpack_return ret = + msgpack::unpack(sbuf.data(), sbuf.size(), NULL, &z, &obj); + EXPECT_EQ(ret, msgpack::UNPACK_SUCCESS); + TestUnionMemberClass val2; + obj.convert(&val2); + EXPECT_EQ(val1.is_double, val2.is_double); + EXPECT_EQ(val1.value.i, 1); + EXPECT_EQ(val1.value.i, val2.value.i); + } +} |