test.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2021 Duowan Inc. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing,
  11. * software distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <algorithm>
  17. #include <iostream>
  18. #include<gtest/gtest.h>
  19. #include "xpack/json.h"
  20. #include "bson.h"
  21. #include "string.h"
  22. using namespace std;
  23. // BuiltInTypes
  24. struct BuiltInTypes {
  25. signed char sch;
  26. char ch;
  27. unsigned char uch;
  28. short sh;
  29. unsigned short ush;
  30. int i;
  31. unsigned int ui;
  32. long l;
  33. unsigned long ul;
  34. long long ll;
  35. unsigned long long ull;
  36. float f;
  37. double d;
  38. long double ld;
  39. bool b;
  40. XPACK(X(F(ATTR), sch, ch, uch, sh, ush), O(i, ui, l, ul, ll, ull, f, d, ld, b));
  41. };
  42. // simple struct used by other
  43. struct Base {
  44. int bi;
  45. string bs;
  46. XPACK(O(bi, bs));
  47. };
  48. // test in another namespace
  49. namespace otherns {
  50. // test enum
  51. enum Enum {
  52. E0 = 0,
  53. E1 = 1,
  54. };
  55. struct OtherNS:public Base {
  56. // test bit field
  57. short h:8;
  58. short l:8;
  59. Enum e;
  60. };
  61. }
  62. // XPACK_OUT must define in global namespace
  63. XPACK_OUT(otherns::OtherNS, I(Base), B(F(0), h, l), E(F(0), e));
  64. struct XTest :public otherns::OtherNS {
  65. string as1; // alias name
  66. string as2;
  67. BuiltInTypes types;
  68. vector<int> vi; // vector int
  69. vector<vector<int> > vvi; // vector vector int
  70. vector<string> vs; // vector string
  71. vector<vector<string> > vvs; // vector vector string
  72. vector<Base> vst; // vector struct
  73. vector<vector<Base> > vvst; // vector vector struct
  74. set<int> si;
  75. list<int> li;
  76. map<string, int> mi;
  77. map<string, Base> mst;
  78. unordered_map<string, Base> umst;
  79. shared_ptr<Base> spst;
  80. char charray[16];
  81. #ifdef XPACK_SUPPORT_QT
  82. // Qt
  83. QString qstr;
  84. QList<Base> qlst;
  85. QVector<Base> qvst;
  86. QMap<string, Base> qmst;
  87. QMap<QString, Base> qmqsst;
  88. XPACK(I(otherns::OtherNS, Base), A(as1, "a1 json:alias1 bson:alias1", as2, "a2 json:alias2 bson:alias2"),
  89. O(types, vi, vvi, vs, vvs, vst, vvst),
  90. O(si,li,mi, mst, umst, spst, charray),
  91. O(qstr, qlst, qvst, qmst, qmqsst));
  92. #else
  93. XPACK(I(otherns::OtherNS, Base), A(as1, "a1 json:alias1 bson:alias1", as2, "a2 json:alias2 bson:alias2"),
  94. O(types, vi, vvi, vs, vvs, vst, vvst),
  95. O(si,li,mi, mst, umst, spst, charray));
  96. #endif
  97. };
  98. void childeq(const XTest&cd) {
  99. EXPECT_EQ(cd.bi, 1024);
  100. EXPECT_EQ(cd.bs, "1024");
  101. EXPECT_EQ(cd.h, 10);
  102. EXPECT_EQ(cd.l, 24);
  103. EXPECT_EQ(cd.e, 1);
  104. EXPECT_EQ(cd.as1, "hello");
  105. EXPECT_EQ(cd.as2, "world");
  106. EXPECT_EQ(cd.vi.size(), 3U);
  107. EXPECT_EQ(cd.vi[0], 1);
  108. EXPECT_EQ(cd.vi[1], 2);
  109. EXPECT_EQ(cd.vi[2], 4);
  110. EXPECT_EQ(cd.vvi.size(), 2U);
  111. EXPECT_EQ(cd.vvi[0][0], 8);
  112. EXPECT_EQ(cd.vvi[0][1], 16);
  113. EXPECT_EQ(cd.vvi[0][2], 32);
  114. EXPECT_EQ(cd.vvi[1][0], 64);
  115. EXPECT_EQ(cd.vvi[1][1], 128);
  116. EXPECT_EQ(cd.vvi[1][2], 256);
  117. EXPECT_EQ(cd.vs.size(), 3U);
  118. EXPECT_EQ(cd.vs[0], "hello");
  119. EXPECT_EQ(cd.vs[1], "hallo");
  120. EXPECT_EQ(cd.vs[2], "你好");
  121. EXPECT_EQ(cd.vvs.size(), 2U);
  122. EXPECT_EQ(cd.vvs[0][0], "Python");
  123. EXPECT_EQ(cd.vvs[0][1], "Perl");
  124. EXPECT_EQ(cd.vvs[0][2], "Bash");
  125. EXPECT_EQ(cd.vvs[1][0], "C++");
  126. EXPECT_EQ(cd.vvs[1][1], "Golang");
  127. EXPECT_EQ(cd.vvs[1][2], "Rust");
  128. EXPECT_EQ(cd.vst.size(), 2U);
  129. EXPECT_EQ(cd.vst[0].bi, 1);
  130. EXPECT_EQ(cd.vst[0].bs, "2");
  131. EXPECT_EQ(cd.vst[1].bi, 3);
  132. EXPECT_EQ(cd.vst[1].bs, "4");
  133. EXPECT_EQ(cd.vvst.size(), 2U);
  134. EXPECT_EQ(cd.vvst[0][0].bi, 5);
  135. EXPECT_EQ(cd.vvst[0][0].bs, "6");
  136. EXPECT_EQ(cd.vvst[0][1].bi, 7);
  137. EXPECT_EQ(cd.vvst[0][1].bs, "8");
  138. EXPECT_EQ(cd.vvst[1][0].bi, 9);
  139. EXPECT_EQ(cd.vvst[1][0].bs, "10");
  140. EXPECT_EQ(cd.si.size(), 3U);
  141. EXPECT_TRUE(cd.si.find(1)!=cd.si.end());
  142. EXPECT_TRUE(cd.si.find(3)!=cd.si.end());
  143. EXPECT_TRUE(cd.si.find(5)!=cd.si.end());
  144. auto siter = cd.li.begin();
  145. EXPECT_EQ(cd.li.size(), 3U);
  146. EXPECT_EQ(*siter, 2); ++siter;
  147. EXPECT_EQ(*siter, 4); ++siter;
  148. EXPECT_EQ(*siter, 6); ++siter;
  149. EXPECT_EQ(cd.mi.find("a")->second, 1);
  150. EXPECT_EQ(cd.mi.find("b")->second, 2);
  151. EXPECT_EQ(cd.mi.find("c")->second, 3);
  152. EXPECT_EQ(cd.mst.find("d")->second.bi, 1);
  153. EXPECT_EQ(cd.mst.find("d")->second.bs, "2");
  154. EXPECT_EQ(cd.mst.find("e")->second.bi, 3);
  155. EXPECT_EQ(cd.mst.find("e")->second.bs, "4");
  156. EXPECT_EQ(cd.umst.find("f")->second.bi, 1);
  157. EXPECT_EQ(cd.umst.find("f")->second.bs, "2");
  158. EXPECT_EQ(cd.umst.find("g")->second.bi, 3);
  159. EXPECT_EQ(cd.umst.find("g")->second.bs, "4");
  160. EXPECT_EQ(cd.spst->bi, 10);
  161. EXPECT_EQ(cd.spst->bs, "24");
  162. EXPECT_TRUE(strcmp(cd.charray, "hello world")==0);
  163. EXPECT_EQ(cd.qstr, "1024");
  164. #ifdef XPACK_SUPPORT_QT
  165. auto qlstiter = cd.qlst.begin();
  166. EXPECT_EQ(cd.qlst.size(), 2);
  167. EXPECT_EQ(qlstiter->bi, 1);
  168. EXPECT_EQ(qlstiter->bs, "2");++qlstiter;
  169. EXPECT_EQ(qlstiter->bi, 3);
  170. EXPECT_EQ(qlstiter->bs, "4");
  171. auto qvstiter = cd.qvst.begin();
  172. EXPECT_EQ(cd.qvst.size(), 2);
  173. EXPECT_EQ(qvstiter->bi, 5);
  174. EXPECT_EQ(qvstiter->bs, "6");++qvstiter;
  175. EXPECT_EQ(qvstiter->bi, 7);
  176. EXPECT_EQ(qvstiter->bs, "8");
  177. EXPECT_EQ(cd.qmst.find("d")->bi, 1);
  178. EXPECT_EQ(cd.qmst.find("d")->bs, "2");
  179. EXPECT_EQ(cd.qmst.find("e")->bi, 3);
  180. EXPECT_EQ(cd.qmst.find("e")->bs, "4");
  181. EXPECT_EQ(cd.qmqsst.find("e")->bi, 5);
  182. EXPECT_EQ(cd.qmqsst.find("e")->bs, "6");
  183. EXPECT_EQ(cd.qmqsst.find("f")->bi, 7);
  184. EXPECT_EQ(cd.qmqsst.find("f")->bs, "8");
  185. #endif
  186. EXPECT_EQ(cd.types.sch, 48);
  187. EXPECT_EQ(cd.types.ch, 49);
  188. EXPECT_EQ(cd.types.uch, 50);
  189. EXPECT_EQ(cd.types.sh, 10);
  190. EXPECT_EQ(cd.types.ush, 24);
  191. EXPECT_EQ(cd.types.i, 10);
  192. EXPECT_EQ(cd.types.ui, 24U);
  193. EXPECT_EQ(cd.types.l, 10);
  194. EXPECT_EQ(cd.types.ul, 24U);
  195. EXPECT_EQ(cd.types.ll, 10);
  196. EXPECT_EQ(cd.types.ull, 24U);
  197. EXPECT_FLOAT_EQ(cd.types.f, 2.718);
  198. EXPECT_DOUBLE_EQ(cd.types.d, 3.14);
  199. EXPECT_DOUBLE_EQ(cd.types.ld, 0.618);
  200. EXPECT_TRUE(cd.types.b);
  201. }
  202. TEST(bson, testBson) {
  203. XTest cd;
  204. xpack::json::decode_file("test.json", cd);
  205. xpack::BsonEncoder en;
  206. en.encode(NULL, cd, NULL);
  207. string js = en.Json();
  208. cout<<"bson:"<<js<<endl;
  209. XTest cd1;
  210. xpack::bson::decode(en.String(), cd1);
  211. childeq(cd1);
  212. XTest cd2;
  213. xpack::json::decode(js, cd2);
  214. childeq(cd2);
  215. XTest cd3;
  216. std::replace( js.begin(), js.end(), '"', '\'');
  217. xpack::BsonBuilder bb(js);
  218. xpack::bson::decode(bb.Encode(), cd3);
  219. childeq(cd3);
  220. }
  221. int main(int argc, char *argv[]) {
  222. testing::InitGoogleTest(&argc, argv);
  223. return RUN_ALL_TESTS();
  224. }