1#include <msgpack.hpp> 2#include <sstream> 3#include <iterator> 4#include <gtest/gtest.h> 5 6#ifdef HAVE_CONFIG_H 7#include "config.h" 8#endif 9 10#if !defined(MSGPACK_USE_CPP03) 11 12TEST(UNIQUE_PTR, pack_convert_nil) 13{ 14 std::stringstream ss; 15 std::unique_ptr<int> val1; 16 msgpack::pack(ss, val1); 17 msgpack::unpacked ret; 18 msgpack::unpack(ret, ss.str().data(), ss.str().size()); 19 std::unique_ptr<int> val2 = ret.get().as<std::unique_ptr<int>>(); 20 EXPECT_TRUE(val1 == val2); 21} 22 23TEST(UNIQUE_PTR, pack_convert_int) 24{ 25 std::stringstream ss; 26 std::unique_ptr<int> val1(new int(1)); 27 msgpack::pack(ss, val1); 28 msgpack::unpacked ret; 29 msgpack::unpack(ret, ss.str().data(), ss.str().size()); 30 std::unique_ptr<int> val2 = ret.get().as<std::unique_ptr<int>>(); 31 EXPECT_TRUE(*val1 == *val2); 32} 33 34TEST(UNIQUE_PTR, object_nil) 35{ 36 std::unique_ptr<int> val1; 37 msgpack::object obj(val1); 38 std::unique_ptr<int> val2 = obj.as<std::unique_ptr<int>>(); 39 EXPECT_TRUE(val1 == val2); 40} 41 42TEST(UNIQUE_PTR, object_int) 43{ 44 std::unique_ptr<int> val1(new int(1)); 45 msgpack::object obj(val1); 46 std::unique_ptr<int> val2 = obj.as<std::unique_ptr<int>>(); 47 EXPECT_TRUE(*val1 == *val2); 48} 49 50// Compile error as expected 51// object::with_zone is required not object 52/* 53TEST(UNIQUE_PTR, object_vector) 54{ 55 typedef std::unique_ptr<std::vector<int>> ovi_t; 56 ovi_t val1(new std::vector<int>()); 57 msgpack::object obj(val1); 58 ovi_t val2 = obj.as<ovi_t>(); 59 EXPECT_TRUE(val1 == val2); 60} 61*/ 62 63TEST(UNIQUE_PTR, object_with_zone_nil) 64{ 65 msgpack::zone z; 66 std::unique_ptr<int> val1; 67 msgpack::object obj(val1, z); 68 std::unique_ptr<int> val2 = obj.as<std::unique_ptr<int>>(); 69 EXPECT_TRUE(val1 == val2); 70} 71 72TEST(UNIQUE_PTR, object_with_zone_int) 73{ 74 msgpack::zone z; 75 std::unique_ptr<int> val1(new int(1)); 76 msgpack::object obj(val1, z); 77 std::unique_ptr<int> val2 = obj.as<std::unique_ptr<int>>(); 78 EXPECT_TRUE(*val1 == *val2); 79} 80 81struct no_def_con { 82 no_def_con() = delete; 83 no_def_con(int i):i(i) {} 84 int i; 85 MSGPACK_DEFINE(i); 86}; 87 88inline bool operator==(no_def_con const& lhs, no_def_con const& rhs) { 89 return lhs.i == rhs.i; 90} 91 92inline bool operator!=(no_def_con const& lhs, no_def_con const& rhs) { 93 return !(lhs == rhs); 94} 95 96namespace msgpack { 97MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) { 98namespace adaptor { 99template <> 100struct as<no_def_con> { 101 no_def_con operator()(msgpack::object const& o) const { 102 if (o.type != msgpack::type::ARRAY) throw msgpack::type_error(); 103 if (o.via.array.size != 1) throw msgpack::type_error(); 104 return no_def_con(o.via.array.ptr[0].as<int>()); 105 } 106}; 107} // adaptor 108} // MSGPACK_API_VERSION_NAMESPACE(MSGPACK_DEFAULT_API_NS) 109} // msgpack 110 111TEST(UNIQUE_PTR, pack_convert_nil_no_def_con) 112{ 113 std::stringstream ss; 114 std::unique_ptr<no_def_con> val1(new no_def_con(1)); 115 msgpack::pack(ss, val1); 116 msgpack::unpacked ret; 117 msgpack::unpack(ret, ss.str().data(), ss.str().size()); 118 std::unique_ptr<no_def_con> val2 = ret.get().as<std::unique_ptr<no_def_con>>(); 119 EXPECT_TRUE(*val1 == *val2); 120} 121 122 123#endif // !defined(MSGPACK_USE_CPP03) 124