size_equal_only.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include <sstream>
  2. #include <msgpack.hpp>
  3. #include <gtest/gtest.h>
  4. TEST(size_equal_only, array)
  5. {
  6. std::stringstream ss;
  7. int buf[3] = { 1, 2, 3 };
  8. msgpack::type::size_equal_only<int[3]> seo(buf);
  9. msgpack::pack(ss, seo);
  10. msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
  11. int ret_buf1[3];
  12. oh.get().convert(ret_buf1);
  13. EXPECT_EQ(buf[0], ret_buf1[0]);
  14. EXPECT_EQ(buf[1], ret_buf1[1]);
  15. EXPECT_EQ(buf[2], ret_buf1[2]);
  16. int ret_buf2[4];
  17. oh.get().convert(ret_buf2);
  18. EXPECT_EQ(buf[0], ret_buf2[0]);
  19. EXPECT_EQ(buf[1], ret_buf2[1]);
  20. EXPECT_EQ(buf[2], ret_buf2[2]);
  21. int ret_buf3[3];
  22. msgpack::type::size_equal_only<int[3]> ret_seo3(ret_buf3);
  23. oh.get().convert(ret_seo3);
  24. EXPECT_EQ(buf[0], ret_buf3[0]);
  25. EXPECT_EQ(buf[1], ret_buf3[1]);
  26. EXPECT_EQ(buf[2], ret_buf3[2]);
  27. int ret_buf4[4];
  28. msgpack::type::size_equal_only<int[4]> ret_seo4(ret_buf4);
  29. try {
  30. oh.get().convert(ret_seo4);
  31. EXPECT_TRUE(false);
  32. }
  33. catch (msgpack::type_error const&) {
  34. EXPECT_TRUE(true);
  35. }
  36. }
  37. TEST(size_equal_only, vector)
  38. {
  39. std::stringstream ss;
  40. std::vector<int> buf;
  41. buf.push_back(1);
  42. buf.push_back(2);
  43. buf.push_back(3);
  44. msgpack::type::size_equal_only<std::vector<int> > seo(buf);
  45. msgpack::pack(ss, seo);
  46. msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
  47. std::vector<int> ret_buf1;
  48. oh.get().convert(ret_buf1);
  49. EXPECT_EQ(buf, ret_buf1);
  50. std::vector<int> ret_buf2;
  51. ret_buf2.resize(3);
  52. msgpack::type::size_equal_only<std::vector<int> > ret_seo2(ret_buf2);
  53. oh.get().convert(ret_seo2);
  54. EXPECT_EQ(buf, ret_buf2);
  55. std::vector<int> ret_buf3;
  56. ret_buf2.resize(4);
  57. msgpack::type::size_equal_only<std::vector<int> > ret_seo3(ret_buf3);
  58. try {
  59. oh.get().convert(ret_seo3);
  60. EXPECT_TRUE(false);
  61. }
  62. catch (msgpack::type_error const&) {
  63. EXPECT_TRUE(true);
  64. }
  65. }
  66. TEST(size_equal_only, msgpack_tuple)
  67. {
  68. std::stringstream ss;
  69. msgpack::type::tuple<int, bool, std::string> buf(1, false, "ABC");
  70. msgpack::type::size_equal_only<msgpack::type::tuple<int, bool, std::string> > seo(buf);
  71. msgpack::pack(ss, seo);
  72. msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
  73. msgpack::type::tuple<int, bool, std::string> ret_buf1;
  74. oh.get().convert(ret_buf1);
  75. EXPECT_EQ(buf.get<0>(), ret_buf1.get<0>());
  76. EXPECT_EQ(buf.get<1>(), ret_buf1.get<1>());
  77. EXPECT_EQ(buf.get<2>(), ret_buf1.get<2>());
  78. msgpack::type::tuple<int, bool, std::string> ret_buf2;
  79. msgpack::type::size_equal_only<msgpack::type::tuple<int, bool, std::string> > ret_seo2(ret_buf2);
  80. oh.get().convert(ret_seo2);
  81. EXPECT_EQ(buf.get<0>(), ret_buf2.get<0>());
  82. EXPECT_EQ(buf.get<1>(), ret_buf2.get<1>());
  83. EXPECT_EQ(buf.get<2>(), ret_buf2.get<2>());
  84. msgpack::type::tuple<int, bool, std::string, int> ret_buf3;
  85. oh.get().convert(ret_buf3);
  86. EXPECT_EQ(buf.get<0>(), ret_buf3.get<0>());
  87. EXPECT_EQ(buf.get<1>(), ret_buf3.get<1>());
  88. EXPECT_EQ(buf.get<2>(), ret_buf3.get<2>());
  89. msgpack::type::tuple<int, bool, std::string, int> ret_buf4;
  90. msgpack::type::size_equal_only<msgpack::type::tuple<int, bool, std::string, int> > ret_seo4(ret_buf4);
  91. try {
  92. oh.get().convert(ret_seo4);
  93. EXPECT_TRUE(false);
  94. }
  95. catch (msgpack::type_error const&) {
  96. EXPECT_TRUE(true);
  97. }
  98. msgpack::type::tuple<int, bool, std::string> ret_buf5;
  99. oh.get().convert(ret_buf5);
  100. EXPECT_EQ(buf.get<0>(), ret_buf5.get<0>());
  101. EXPECT_EQ(buf.get<1>(), ret_buf5.get<1>());
  102. msgpack::type::tuple<int, bool, std::string, int> ret_buf6;
  103. msgpack::type::size_equal_only<msgpack::type::tuple<int, bool, std::string, int> > ret_seo6(ret_buf6);
  104. try {
  105. oh.get().convert(ret_seo6);
  106. EXPECT_TRUE(false);
  107. }
  108. catch (msgpack::type_error const&) {
  109. EXPECT_TRUE(true);
  110. }
  111. }
  112. #if !defined(MSGPACK_USE_CPP03)
  113. TEST(size_equal_only, tuple)
  114. {
  115. std::stringstream ss;
  116. std::tuple<int, bool, std::string> buf(1, false, "ABC");
  117. auto seo = msgpack::type::make_size_equal_only(buf);
  118. msgpack::pack(ss, seo);
  119. msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
  120. std::tuple<int, bool, std::string> ret_buf1;
  121. oh.get().convert(ret_buf1);
  122. EXPECT_EQ(buf, ret_buf1);
  123. std::tuple<int, bool, std::string> ret_buf2;
  124. auto ret_seo2 = msgpack::type::make_size_equal_only(ret_buf2);
  125. oh.get().convert(ret_seo2);
  126. EXPECT_EQ(buf, ret_buf2);
  127. std::tuple<int, bool, std::string, int> ret_buf3;
  128. oh.get().convert(ret_buf3);
  129. EXPECT_EQ(std::get<0>(buf), std::get<0>(ret_buf3));
  130. EXPECT_EQ(std::get<1>(buf), std::get<1>(ret_buf3));
  131. EXPECT_EQ(std::get<2>(buf), std::get<2>(ret_buf3));
  132. std::tuple<int, bool, std::string, int> ret_buf4;
  133. auto ret_seo4 = msgpack::type::make_size_equal_only(ret_buf4);
  134. try {
  135. oh.get().convert(ret_seo4);
  136. EXPECT_TRUE(false);
  137. }
  138. catch (msgpack::type_error const&) {
  139. EXPECT_TRUE(true);
  140. }
  141. std::tuple<int, bool, std::string> ret_buf5;
  142. oh.get().convert(ret_buf5);
  143. EXPECT_EQ(std::get<0>(buf), std::get<0>(ret_buf5));
  144. EXPECT_EQ(std::get<1>(buf), std::get<1>(ret_buf5));
  145. std::tuple<int, bool, std::string, int> ret_buf6;
  146. auto ret_seo6 = msgpack::type::make_size_equal_only(ret_buf6);
  147. try {
  148. oh.get().convert(ret_seo6);
  149. EXPECT_TRUE(false);
  150. }
  151. catch (msgpack::type_error const&) {
  152. EXPECT_TRUE(true);
  153. }
  154. }
  155. struct foo1 {
  156. foo1() = default;
  157. foo1(int i, bool b):t(i, b), seo(t) {}
  158. std::tuple<int, bool> t;
  159. msgpack::type::size_equal_only<std::tuple<int, bool> > seo;
  160. MSGPACK_DEFINE(seo);
  161. };
  162. struct foo2 {
  163. foo2() = default;
  164. foo2(int i, bool b, std::string const& s):t(i, b, s), seo(t) {}
  165. std::tuple<int, bool, std::string> t;
  166. msgpack::type::size_equal_only<std::tuple<int, bool, std::string> > seo;
  167. MSGPACK_DEFINE(seo);
  168. };
  169. TEST(size_equal_only, custom_class)
  170. {
  171. std::stringstream ss;
  172. foo1 f1(42, true);
  173. msgpack::pack(ss, f1);
  174. msgpack::object_handle oh = msgpack::unpack(ss.str().data(), ss.str().size());
  175. foo2 f2(123, false, "ABC");
  176. try {
  177. oh.get().convert(f2);
  178. EXPECT_TRUE(false);
  179. }
  180. catch (msgpack::type_error const&) {
  181. EXPECT_TRUE(true);
  182. }
  183. }
  184. #endif // !defined(MSGPACK_USE_CPP03)