123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- /*
- * Copyright (C) 2021 Duowan Inc. All rights reserved.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #include <algorithm>
- #include <iostream>
- #include<gtest/gtest.h>
- #include "xpack/json.h"
- #include "bson.h"
- #include "string.h"
- using namespace std;
- // BuiltInTypes
- struct BuiltInTypes {
- signed char sch;
- char ch;
- unsigned char uch;
- short sh;
- unsigned short ush;
- int i;
- unsigned int ui;
- long l;
- unsigned long ul;
- long long ll;
- unsigned long long ull;
- float f;
- double d;
- long double ld;
- bool b;
- XPACK(X(F(ATTR), sch, ch, uch, sh, ush), O(i, ui, l, ul, ll, ull, f, d, ld, b));
- };
- // simple struct used by other
- struct Base {
- int bi;
- string bs;
- XPACK(O(bi, bs));
- };
- // test in another namespace
- namespace otherns {
- // test enum
- enum Enum {
- E0 = 0,
- E1 = 1,
- };
- struct OtherNS:public Base {
- // test bit field
- short h:8;
- short l:8;
- Enum e;
- };
- }
- // XPACK_OUT must define in global namespace
- XPACK_OUT(otherns::OtherNS, I(Base), B(F(0), h, l), E(F(0), e));
- struct XTest :public otherns::OtherNS {
- string as1; // alias name
- string as2;
- BuiltInTypes types;
- vector<int> vi; // vector int
- vector<vector<int> > vvi; // vector vector int
- vector<string> vs; // vector string
- vector<vector<string> > vvs; // vector vector string
- vector<Base> vst; // vector struct
- vector<vector<Base> > vvst; // vector vector struct
- set<int> si;
- list<int> li;
- map<string, int> mi;
- map<string, Base> mst;
- unordered_map<string, Base> umst;
- shared_ptr<Base> spst;
- char charray[16];
- #ifdef XPACK_SUPPORT_QT
- // Qt
- QString qstr;
- QList<Base> qlst;
- QVector<Base> qvst;
- QMap<string, Base> qmst;
- QMap<QString, Base> qmqsst;
- XPACK(I(otherns::OtherNS, Base), A(as1, "a1 json:alias1 bson:alias1", as2, "a2 json:alias2 bson:alias2"),
- O(types, vi, vvi, vs, vvs, vst, vvst),
- O(si,li,mi, mst, umst, spst, charray),
- O(qstr, qlst, qvst, qmst, qmqsst));
- #else
- XPACK(I(otherns::OtherNS, Base), A(as1, "a1 json:alias1 bson:alias1", as2, "a2 json:alias2 bson:alias2"),
- O(types, vi, vvi, vs, vvs, vst, vvst),
- O(si,li,mi, mst, umst, spst, charray));
- #endif
- };
- void childeq(const XTest&cd) {
- EXPECT_EQ(cd.bi, 1024);
- EXPECT_EQ(cd.bs, "1024");
- EXPECT_EQ(cd.h, 10);
- EXPECT_EQ(cd.l, 24);
- EXPECT_EQ(cd.e, 1);
- EXPECT_EQ(cd.as1, "hello");
- EXPECT_EQ(cd.as2, "world");
- EXPECT_EQ(cd.vi.size(), 3U);
- EXPECT_EQ(cd.vi[0], 1);
- EXPECT_EQ(cd.vi[1], 2);
- EXPECT_EQ(cd.vi[2], 4);
- EXPECT_EQ(cd.vvi.size(), 2U);
- EXPECT_EQ(cd.vvi[0][0], 8);
- EXPECT_EQ(cd.vvi[0][1], 16);
- EXPECT_EQ(cd.vvi[0][2], 32);
- EXPECT_EQ(cd.vvi[1][0], 64);
- EXPECT_EQ(cd.vvi[1][1], 128);
- EXPECT_EQ(cd.vvi[1][2], 256);
- EXPECT_EQ(cd.vs.size(), 3U);
- EXPECT_EQ(cd.vs[0], "hello");
- EXPECT_EQ(cd.vs[1], "hallo");
- EXPECT_EQ(cd.vs[2], "你好");
- EXPECT_EQ(cd.vvs.size(), 2U);
- EXPECT_EQ(cd.vvs[0][0], "Python");
- EXPECT_EQ(cd.vvs[0][1], "Perl");
- EXPECT_EQ(cd.vvs[0][2], "Bash");
- EXPECT_EQ(cd.vvs[1][0], "C++");
- EXPECT_EQ(cd.vvs[1][1], "Golang");
- EXPECT_EQ(cd.vvs[1][2], "Rust");
- EXPECT_EQ(cd.vst.size(), 2U);
- EXPECT_EQ(cd.vst[0].bi, 1);
- EXPECT_EQ(cd.vst[0].bs, "2");
- EXPECT_EQ(cd.vst[1].bi, 3);
- EXPECT_EQ(cd.vst[1].bs, "4");
- EXPECT_EQ(cd.vvst.size(), 2U);
- EXPECT_EQ(cd.vvst[0][0].bi, 5);
- EXPECT_EQ(cd.vvst[0][0].bs, "6");
- EXPECT_EQ(cd.vvst[0][1].bi, 7);
- EXPECT_EQ(cd.vvst[0][1].bs, "8");
- EXPECT_EQ(cd.vvst[1][0].bi, 9);
- EXPECT_EQ(cd.vvst[1][0].bs, "10");
- EXPECT_EQ(cd.si.size(), 3U);
- EXPECT_TRUE(cd.si.find(1)!=cd.si.end());
- EXPECT_TRUE(cd.si.find(3)!=cd.si.end());
- EXPECT_TRUE(cd.si.find(5)!=cd.si.end());
- auto siter = cd.li.begin();
- EXPECT_EQ(cd.li.size(), 3U);
- EXPECT_EQ(*siter, 2); ++siter;
- EXPECT_EQ(*siter, 4); ++siter;
- EXPECT_EQ(*siter, 6); ++siter;
- EXPECT_EQ(cd.mi.find("a")->second, 1);
- EXPECT_EQ(cd.mi.find("b")->second, 2);
- EXPECT_EQ(cd.mi.find("c")->second, 3);
- EXPECT_EQ(cd.mst.find("d")->second.bi, 1);
- EXPECT_EQ(cd.mst.find("d")->second.bs, "2");
- EXPECT_EQ(cd.mst.find("e")->second.bi, 3);
- EXPECT_EQ(cd.mst.find("e")->second.bs, "4");
- EXPECT_EQ(cd.umst.find("f")->second.bi, 1);
- EXPECT_EQ(cd.umst.find("f")->second.bs, "2");
- EXPECT_EQ(cd.umst.find("g")->second.bi, 3);
- EXPECT_EQ(cd.umst.find("g")->second.bs, "4");
- EXPECT_EQ(cd.spst->bi, 10);
- EXPECT_EQ(cd.spst->bs, "24");
- EXPECT_TRUE(strcmp(cd.charray, "hello world")==0);
- EXPECT_EQ(cd.qstr, "1024");
- #ifdef XPACK_SUPPORT_QT
- auto qlstiter = cd.qlst.begin();
- EXPECT_EQ(cd.qlst.size(), 2);
- EXPECT_EQ(qlstiter->bi, 1);
- EXPECT_EQ(qlstiter->bs, "2");++qlstiter;
- EXPECT_EQ(qlstiter->bi, 3);
- EXPECT_EQ(qlstiter->bs, "4");
- auto qvstiter = cd.qvst.begin();
- EXPECT_EQ(cd.qvst.size(), 2);
- EXPECT_EQ(qvstiter->bi, 5);
- EXPECT_EQ(qvstiter->bs, "6");++qvstiter;
- EXPECT_EQ(qvstiter->bi, 7);
- EXPECT_EQ(qvstiter->bs, "8");
- EXPECT_EQ(cd.qmst.find("d")->bi, 1);
- EXPECT_EQ(cd.qmst.find("d")->bs, "2");
- EXPECT_EQ(cd.qmst.find("e")->bi, 3);
- EXPECT_EQ(cd.qmst.find("e")->bs, "4");
- EXPECT_EQ(cd.qmqsst.find("e")->bi, 5);
- EXPECT_EQ(cd.qmqsst.find("e")->bs, "6");
- EXPECT_EQ(cd.qmqsst.find("f")->bi, 7);
- EXPECT_EQ(cd.qmqsst.find("f")->bs, "8");
- #endif
- EXPECT_EQ(cd.types.sch, 48);
- EXPECT_EQ(cd.types.ch, 49);
- EXPECT_EQ(cd.types.uch, 50);
- EXPECT_EQ(cd.types.sh, 10);
- EXPECT_EQ(cd.types.ush, 24);
- EXPECT_EQ(cd.types.i, 10);
- EXPECT_EQ(cd.types.ui, 24U);
- EXPECT_EQ(cd.types.l, 10);
- EXPECT_EQ(cd.types.ul, 24U);
- EXPECT_EQ(cd.types.ll, 10);
- EXPECT_EQ(cd.types.ull, 24U);
- EXPECT_FLOAT_EQ(cd.types.f, 2.718);
- EXPECT_DOUBLE_EQ(cd.types.d, 3.14);
- EXPECT_DOUBLE_EQ(cd.types.ld, 0.618);
- EXPECT_TRUE(cd.types.b);
- }
- TEST(bson, testBson) {
- XTest cd;
- xpack::json::decode_file("test.json", cd);
- xpack::BsonEncoder en;
- en.encode(NULL, cd, NULL);
- string js = en.Json();
- cout<<"bson:"<<js<<endl;
- XTest cd1;
- xpack::bson::decode(en.String(), cd1);
- childeq(cd1);
- XTest cd2;
- xpack::json::decode(js, cd2);
- childeq(cd2);
- XTest cd3;
- std::replace( js.begin(), js.end(), '"', '\'');
- xpack::BsonBuilder bb(js);
- xpack::bson::decode(bb.Encode(), cd3);
- childeq(cd3);
- }
- int main(int argc, char *argv[]) {
- testing::InitGoogleTest(&argc, argv);
- return RUN_ALL_TESTS();
- }
|