1 | /** |
2 | * Copyright 2021 Alibaba, Inc. and its affiliates. 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, software |
11 | * 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 | * \author jiliang.ljl |
17 | * \date Feb 2021 |
18 | * \brief |
19 | */ |
20 | |
21 | #include "common/protobuf_helper.h" |
22 | #include <ailego/utility/time_helper.h> |
23 | #include <gmock/gmock.h> |
24 | #include <google/protobuf/util/json_util.h> |
25 | #include <gtest/gtest.h> |
26 | #include <json2pb/json_to_pb.h> |
27 | #include <json2pb/pb_to_json.h> |
28 | #include "proto/proxima_be.pb.h" |
29 | #include "test.pb.h" |
30 | |
31 | namespace proxima { |
32 | namespace be { |
33 | |
34 | using ::proxima::be::proto::test::ProtoTest; |
35 | |
36 | proto::CollectionConfig GetMeta(const std::string &name, |
37 | uint64_t doc_per_segment) { |
38 | proto::CollectionConfig meta; |
39 | meta.set_collection_name(name); |
40 | meta.add_forward_column_names("f1" ); |
41 | meta.add_forward_column_names("f2" ); |
42 | meta.set_max_docs_per_segment(doc_per_segment); |
43 | auto cm1 = meta.add_index_column_params(); |
44 | cm1->set_column_name("column1" ); |
45 | auto kv = cm1->add_extra_params(); |
46 | kv->set_key("k1" ); |
47 | kv->set_value("v1" ); |
48 | kv = cm1->add_extra_params(); |
49 | kv->set_key("k2" ); |
50 | kv->set_value("v2" ); |
51 | auto cm2 = meta.add_index_column_params(); |
52 | for (size_t i = 0; i < 10; i++) { |
53 | kv = cm2->add_extra_params(); |
54 | kv->set_key("k" + std::to_string(i)); |
55 | kv->set_value("v" + std::to_string(i)); |
56 | } |
57 | return meta; |
58 | } |
59 | |
60 | static int ToJson(const google::protobuf::Message &response, |
61 | std::string *json) { |
62 | google::protobuf::util::JsonOptions options; |
63 | options.add_whitespace = false; |
64 | options.always_print_primitive_fields = true; |
65 | options.preserve_proto_field_names = true; |
66 | |
67 | google::protobuf::util::Status status = |
68 | google::protobuf::util::MessageToJsonString(response, json, options); |
69 | return status.error_code(); |
70 | } |
71 | |
72 | static std::string ToJson(const google::protobuf::Message &response) { |
73 | std::string json; |
74 | google::protobuf::util::JsonOptions options; |
75 | options.add_whitespace = false; |
76 | options.preserve_proto_field_names = true; |
77 | |
78 | google::protobuf::util::MessageToJsonString(response, &json, options); |
79 | return json; |
80 | } |
81 | |
82 | static bool ToJsonBrpc(const google::protobuf::Message &response, |
83 | std::string *json) { |
84 | json2pb::Pb2JsonOptions options; |
85 | options.enable_protobuf_map = false; |
86 | options.bytes_to_base64 = true; |
87 | options.always_print_primitive_fields = true; |
88 | return json2pb::ProtoMessageToJson(response, json); |
89 | } |
90 | |
91 | static void TestGoogleJsonDeserializeOk( |
92 | const std::string &json, const google::protobuf::Message &original, |
93 | google::protobuf::Message *back) { |
94 | back->Clear(); |
95 | google::protobuf::util::JsonParseOptions options; |
96 | // ignore params which can't be automatic parse from json |
97 | options.ignore_unknown_fields = true; |
98 | google::protobuf::util::Status status = |
99 | google::protobuf::util::JsonStringToMessage(json, back, options); |
100 | ASSERT_TRUE(status.ok()); |
101 | EXPECT_EQ(original.SerializeAsString(), back->SerializeAsString()); |
102 | } |
103 | |
104 | TEST(ProtobufHelper, Test) { |
105 | auto meta = GetMeta("test" , 100); |
106 | std::string out; |
107 | bool ret = ProtobufHelper::MessageToJson(meta, &out); |
108 | printf("reflection:\n %s\n" , out.c_str()); |
109 | EXPECT_TRUE(ret); |
110 | std::string out2; |
111 | ToJson(meta, &out2); |
112 | printf("google:\n %s\n" , out2.c_str()); |
113 | std::string out3; |
114 | ToJsonBrpc(meta, &out3); |
115 | printf("brpc:\n %s\n" , out3.c_str()); |
116 | |
117 | proto::CollectionConfig meta_back; |
118 | TestGoogleJsonDeserializeOk(out, meta, &meta_back); |
119 | } |
120 | |
121 | TEST(ProtobufHelper, TestOneof) { |
122 | auto assert_expect = [](const proto::QueryRequest &req) { |
123 | proto::QueryRequest back; |
124 | std::string out; |
125 | bool ret = ProtobufHelper::MessageToJson(req, &out); |
126 | EXPECT_TRUE(ret); |
127 | printf("reflection\n%s\n" , out.c_str()); |
128 | std::string out2; |
129 | ToJson(req, &out2); |
130 | printf("google:\n%s\n" , out2.c_str()); |
131 | std::string out3; |
132 | ToJsonBrpc(req, &out3); |
133 | printf("brpc:\n%s\n" , out2.c_str()); |
134 | TestGoogleJsonDeserializeOk(out, req, &back); |
135 | }; |
136 | // set first one of |
137 | { |
138 | proto::QueryRequest req; |
139 | req.mutable_knn_param()->set_batch_count(10); |
140 | assert_expect(req); |
141 | } |
142 | |
143 | // set none of one of |
144 | { |
145 | proto::QueryRequest req; |
146 | assert_expect(req); |
147 | } |
148 | } |
149 | |
150 | TEST(ProtobufHelper, TestOptions) { |
151 | auto meta = GetMeta("test" , 100); |
152 | ProtobufHelper::PrintOptions options; |
153 | options.always_print_primitive_fields = false; |
154 | std::string out; |
155 | bool ret = ProtobufHelper::MessageToJson(meta, options, &out); |
156 | printf("reflection:\n %s\n" , out.c_str()); |
157 | EXPECT_TRUE(ret); |
158 | proto::CollectionConfig meta_back; |
159 | TestGoogleJsonDeserializeOk(out, meta, &meta_back); |
160 | } |
161 | |
162 | TEST(ProtobufHelper, Benchmark) { |
163 | std::vector<proto::CollectionConfig> metas; |
164 | const int COUNT = 10000; |
165 | std::vector<std::string> outs1(COUNT); |
166 | std::vector<std::string> outs2(COUNT); |
167 | std::vector<std::string> outs3(COUNT); |
168 | for (size_t i = 0; i < COUNT; i++) { |
169 | metas.push_back(GetMeta("test" + std::to_string(i), i)); |
170 | } |
171 | uint64_t time1 = 0; |
172 | uint64_t time2 = 0; |
173 | uint64_t time3 = 0; |
174 | { |
175 | ailego::ElapsedTime timer; |
176 | for (size_t i = 0; i < COUNT; i++) { |
177 | ProtobufHelper::MessageToJson(metas[i], &outs1[i]); |
178 | } |
179 | time1 = timer.micro_seconds(); |
180 | } |
181 | { |
182 | ailego::ElapsedTime timer; |
183 | for (size_t i = 0; i < COUNT; i++) { |
184 | ToJson(metas[i], &outs2[i]); |
185 | } |
186 | time2 = timer.micro_seconds(); |
187 | } |
188 | { |
189 | ailego::ElapsedTime timer; |
190 | for (size_t i = 0; i < COUNT; i++) { |
191 | ToJsonBrpc(metas[i], &outs3[i]); |
192 | } |
193 | time3 = timer.micro_seconds(); |
194 | } |
195 | printf("reflection: %zu\n" , (size_t)time1); |
196 | printf("pb MessageToJsonString: %zu\n" , (size_t)time2); |
197 | printf("brpc: %zu\n" , (size_t)time3); |
198 | } |
199 | |
200 | static bool ToMessagePb(const std::string &json, |
201 | google::protobuf::Message *msg) { |
202 | google::protobuf::util::JsonParseOptions options; |
203 | // ignore params which can't be automatic parse from json |
204 | options.ignore_unknown_fields = true; |
205 | |
206 | google::protobuf::util::Status status = |
207 | google::protobuf::util::JsonStringToMessage(json, msg, options); |
208 | if (!status.ok()) { |
209 | fprintf(stderr, "ParseRequestFromJson failed. status[%s] json[%s]\n" , |
210 | status.ToString().c_str(), json.c_str()); |
211 | return false; |
212 | } |
213 | return true; |
214 | } |
215 | |
216 | static bool ToMessageBrpc(const std::string &json, |
217 | google::protobuf::Message *msg, |
218 | std::string *error = nullptr) { |
219 | return json2pb::JsonToProtoMessage(json, msg, error); |
220 | } |
221 | |
222 | const std::string kTestJson = |
223 | R"foo({"collection_name": "test_collection", "debug_mode": false, "knn_param": {"column_name": "test_column", "topk": 20, "batch_count": 1, "dimension": 480, "data_type": "DT_VECTOR_FP32", "is_linear": false, "matrix": "[b'[0.133300781,0.00359916687,0.0234069824,0.0513000488,0.0621032715,-0.0505065918,-0.0451049805,0,-0.00680160522,-0.0156021118,0.00349998474,0.0130996704,0.029296875,0.049987793,0.0753173828,-0.00400161743,0.00540161133,0.0786132812,0.00680160522,0.0455932617,-0.0396118164,-0.0433044434,0.0462036133,0.0588989258,-0.059387207,-0.0390014648,-0.0272979736,-0.0301055908,-0.047088623,-0.0339050293,0.00230026245,0.0523986816,-0.0598144531,-0.0748901367,-0.0380859375,-0.0624084473,0.0437927246,-0.00949859619,-0.00239944458,0.0272064209,0.0645751953,0.0313110352,0.0160980225,0.0207977295,-0.0252075195,0.0817871094,0.0969238281,0.0265045166,0.0859985352,0.000599861145,0.0362854004,-0.0130004883,-0.0443115234,0.0141983032,0.0555114746,-0.0555114746,0.0339050293,-0.0409851074,0.0327148438,-0.0440979004,0.00540161133,0.0200958252,0.103210449,-0.020401001,-0.00510025024,-0.061706543,0.0307006836,-0.0349121094,-0.000599861145,0.0853881836,-0.0477905273,0.073425293,-0.0137023926,0.0314941406,-0.0231018066,-0.00900268555,0.0349121094,-0.0225067139,0.0314941406,0.0527038574,-0.0141983032,-0.028793335,-0.0278015137,-0.069519043,0.0407104492,-0.000899791718,-0.0437927246,0.0883178711,0.0484008789,-0.0260009766,-0.054107666,-0.00709915161,-0.0142974854,-0.0654296875,0.00869750977,-0.0517883301,-0.0397949219,-0.0310058594,-0.032409668,0.0567016602,-0.0066986084,-0.0494995117,0.0204925537,-0.0290985107,0.0883789062,-0.0252075195,-0.0729980469,-0.0510864258,0.0254058838,-0.0158996582,0.000599861145,0.0349121094,-0.00759887695,-0.07421875,0.0567932129,-0.0294036865,-0.0645751953,-0.0559997559,0.0783081055,0.0150985718,0.00239944458,-0.103271484,0.00859832764,-0.0283050537,-0.00270080566,-0.0481872559,-0.0307006836,0.0314941406,-0.0234985352,-0.00340080261,0.0390014648,-0.0328063965,0.00949859619,-0.0648193359,-0.0361938477,-0.00450134277,0.0342102051,-0.0433044434,-0.0433044434,-0.0066986084,0.0513000488,-0.00479888916,0.0505981445,0.0124969482,0.0114974976,0.0408935547,0.0338134766,0.0060005188,-0.0149002075,-0.0425109863,-0.0577087402,-0.014503479,-0.00789642334,-0.00699996948,0.00450134277,0.0867919922,0.00410079956,0.0016002655,-0.00879669189,0.00680160522,-0.0544128418,-0.0581970215,0.0276031494,0.0227966309,0.065612793,-0.0187072754,0.0914916992,-0.0269927979,0.00469970703,-0.0586853027,0.0902709961,0.0441894531,-0.0775146484,-0.000400066376,-0.0563049316,0.028793335,-0.071472168,-0.0142974854,-0.000500202179,0.025604248,-0.0433044434,0.00340080261,0.0591125488,-0.025894165,0.00550079346,0.0614013672,-0.0830078125,-0.0247955322,-0.0265960693,-0.057800293,-0.0706787109,0.057800293,0,0.00239944458,-0.0440979004,-0.0955200195,0.00910186768,-0.0092010498,-0.0202026367,-0.0521850586,0.0314025879,-0.0827026367,0.0111999512,0.036895752,-0.0178985596,0.0100021362,0.0281066895,-0.000899791718,0.0979003906,0.0249023438,-0.0469970703,0.0270996094,0.0240936279,0.0787963867,-0.0407104492,0.102416992,0.0103988647,0.0853271484,0.0811767578,-0.0189971924,0.00849914551,0.00270080566,-0.0254974365,-0.0772094727,-0.032989502,-0.0157928467,-0.0932006836,-0.0225067139,0.0592041016,0.0186004639,0.0933837891,0.0247955322,-0.00609970093,0.0127029419,0.00390052795,-0.0281066895,-0.0109024048,0.0328063965,0.0599060059,0.0659790039,-0.00569915771,0.0635986328,-0.0444946289,0.0184936523,0.0405883789,0.0136032104,0.00469970703,0.0390014648,-0.033203125,-0.0592956543,-0.0285949707,0,-0.0278930664,0.0396118164,0.0880126953,-0.0234985352,0.0060005188,-0.0104980469,0.0258026123,0.0109024048,-0.0133972168,-0.0243988037,0.00490188599,-0.013999939,0.0393981934,-0.00340080261,-0.0468139648,-0.016998291,0.037902832,0.00370025635,0.0422973633,-0.0339050293,0.0180969238,-0.0104980469,0.0187072754,-0.00770187378,0.0180053711,-0.0374145508,-0.00469970703,0.0759887695,-0.0480041504,0.0218963623,-0.025100708,0.0728149414,0.114196777,-0.0169067383,-0.0859985352,-0.0249023438,-0.021697998,0.0225982666,0.0761108398,-0.0454101562,0.00930023193,0.0272064209,0.0548095703,0.0574035645,-0.0260925293,0.000899791718,-0.00250053406,-0.00820159912,-0.0200958252,0.0361022949,-0.000500202179,-0.0897827148,-0.0133972168,0.0589904785,-0.00340080261,0.0320129395,0.00419998169,0.0651245117,-0.0419006348,-0.0367126465,-0.00410079956,-0.0634155273,-0.0616149902,-0.0132980347,0.00939941406,0.053314209,-0.10748291,0.0354003906,-0.0207061768,0.00680160522,0.0411071777,0.114990234,-0.0321044922,0.00109958649,0.037902832,-0.00429916382,-0.016494751,-0.0108032227,0.00559997559,0.0827026367,0.0563049316,-0.0254974365,-0.0582885742,0.0534057617,-0.013999939,-0.0797729492,-0.00619888306,-0.0375976562,0.0585021973,-0.106506348,0.0274047852,-0.0364990234,0.0473937988,0.0892944336,0.00500106812,-0.0178070068,0.0320129395,-0.0477905273,0.0679931641,-0.0544128418,0.0502929688,0.0175018311,-0.029800415,0.0220947266,0.00239944458,0.00289916992,0.0737915039,0.0991210938,-0.0318908691,-0.0182952881,0.0184936523,0.0252075195,-0.00309944153,-0.0157012939,0.0321960449,-0.0234069824,-0.0281066895,0.0502929688,-0.00289916992,0.0476989746,0.0404968262,-0.102905273,-0.0100021362,0.0318908691,0.0715942383,-0.0822753906,-0.0108032227,-0.0659790039,0.0074005127,-0.0245056152,0.0640258789,-0.0114974976,-0.0634155273,-0.0412902832,-0.0328979492,0.0102996826,-0.100585938,-0.00939941406,-0.0294952393,-0.00890350342,-0.00419998169,-0.0301055908,-0.0204925537,0.00200080872,-0.0581970215,0.000800132751,-0.0213928223,-0.0606994629,-0.0736083984,-0.0296936035,-0.0488891602,-0.0382995605,0.044708252,0.0184936523,0.0013999939,0.0173950195,-0.0173034668,0.102478027,0.0614929199,-0.0231018066,-0.00439834595,-0.0139007568,-0.0466918945,0.0315856934,0.0805053711,-0.0222015381,-0.0132980347,0.100524902,-0.0131988525,-0.0988769531,-0.0541992188,0.016204834,0.0130996704,0.0234985352,0.0361022949,-0.033996582,-0.0130004883,-0.0498046875,-0.00550079346,0.0676879883,0.0392150879,0.0117034912,-0.0112991333,-0.0610961914,0.0325927734,-0.00510025024,-0.0770263672,0.0419006348,-0.00579833984,-0.0437927246,-0.0254058838,-0.0585021973,-0.00579833984,0.0662231445,-0.0444030762,0.0236968994,-0.0618896484,-0.0260009766,-0.00250053406,-0.0736083984,-0.0111999512,0.0491943359,0.0656738281,0.078918457,0.094909668,0.0600891113,0.0430908203,0.0155029297,0.0548095703,0.0726928711,0.00129985809,-0.0480957031,-0.0167999268,0.0313110352,0.0452880859,0.0169067383,-0.0115966797,-0.00390052795,0.0930175781,-0.00949859619,0.0357971191,0.0354919434,-0.00289916992,0.0645141602,0.119995117,0.0452880859,0.0265960693,-0.0698242188]']"}})foo" ; |
224 | |
225 | const std::string kCamelcaseJson = |
226 | R"foo({"collectionName": "test_collection", "debugMode": false, "knn_param": {"column_name": "test_column", "topk": 20, "batch_count": 1, "dimension": 480, "data_type": "DT_VECTOR_FP32", "is_linear": false, "matrix": "[b'[0.133300781,0.00359916687,0.0234069824,0.0513000488,0.0621032715,-0.0505065918,-0.0451049805,0,-0.00680160522,-0.0156021118,0.00349998474,0.0130996704,0.029296875,0.049987793,0.0753173828,-0.00400161743,0.00540161133,0.0786132812,0.00680160522,0.0455932617,-0.0396118164,-0.0433044434,0.0462036133,0.0588989258,-0.059387207,-0.0390014648,-0.0272979736,-0.0301055908,-0.047088623,-0.0339050293,0.00230026245,0.0523986816,-0.0598144531,-0.0748901367,-0.0380859375,-0.0624084473,0.0437927246,-0.00949859619,-0.00239944458,0.0272064209,0.0645751953,0.0313110352,0.0160980225,0.0207977295,-0.0252075195,0.0817871094,0.0969238281,0.0265045166,0.0859985352,0.000599861145,0.0362854004,-0.0130004883,-0.0443115234,0.0141983032,0.0555114746,-0.0555114746,0.0339050293,-0.0409851074,0.0327148438,-0.0440979004,0.00540161133,0.0200958252,0.103210449,-0.020401001,-0.00510025024,-0.061706543,0.0307006836,-0.0349121094,-0.000599861145,0.0853881836,-0.0477905273,0.073425293,-0.0137023926,0.0314941406,-0.0231018066,-0.00900268555,0.0349121094,-0.0225067139,0.0314941406,0.0527038574,-0.0141983032,-0.028793335,-0.0278015137,-0.069519043,0.0407104492,-0.000899791718,-0.0437927246,0.0883178711,0.0484008789,-0.0260009766,-0.054107666,-0.00709915161,-0.0142974854,-0.0654296875,0.00869750977,-0.0517883301,-0.0397949219,-0.0310058594,-0.032409668,0.0567016602,-0.0066986084,-0.0494995117,0.0204925537,-0.0290985107,0.0883789062,-0.0252075195,-0.0729980469,-0.0510864258,0.0254058838,-0.0158996582,0.000599861145,0.0349121094,-0.00759887695,-0.07421875,0.0567932129,-0.0294036865,-0.0645751953,-0.0559997559,0.0783081055,0.0150985718,0.00239944458,-0.103271484,0.00859832764,-0.0283050537,-0.00270080566,-0.0481872559,-0.0307006836,0.0314941406,-0.0234985352,-0.00340080261,0.0390014648,-0.0328063965,0.00949859619,-0.0648193359,-0.0361938477,-0.00450134277,0.0342102051,-0.0433044434,-0.0433044434,-0.0066986084,0.0513000488,-0.00479888916,0.0505981445,0.0124969482,0.0114974976,0.0408935547,0.0338134766,0.0060005188,-0.0149002075,-0.0425109863,-0.0577087402,-0.014503479,-0.00789642334,-0.00699996948,0.00450134277,0.0867919922,0.00410079956,0.0016002655,-0.00879669189,0.00680160522,-0.0544128418,-0.0581970215,0.0276031494,0.0227966309,0.065612793,-0.0187072754,0.0914916992,-0.0269927979,0.00469970703,-0.0586853027,0.0902709961,0.0441894531,-0.0775146484,-0.000400066376,-0.0563049316,0.028793335,-0.071472168,-0.0142974854,-0.000500202179,0.025604248,-0.0433044434,0.00340080261,0.0591125488,-0.025894165,0.00550079346,0.0614013672,-0.0830078125,-0.0247955322,-0.0265960693,-0.057800293,-0.0706787109,0.057800293,0,0.00239944458,-0.0440979004,-0.0955200195,0.00910186768,-0.0092010498,-0.0202026367,-0.0521850586,0.0314025879,-0.0827026367,0.0111999512,0.036895752,-0.0178985596,0.0100021362,0.0281066895,-0.000899791718,0.0979003906,0.0249023438,-0.0469970703,0.0270996094,0.0240936279,0.0787963867,-0.0407104492,0.102416992,0.0103988647,0.0853271484,0.0811767578,-0.0189971924,0.00849914551,0.00270080566,-0.0254974365,-0.0772094727,-0.032989502,-0.0157928467,-0.0932006836,-0.0225067139,0.0592041016,0.0186004639,0.0933837891,0.0247955322,-0.00609970093,0.0127029419,0.00390052795,-0.0281066895,-0.0109024048,0.0328063965,0.0599060059,0.0659790039,-0.00569915771,0.0635986328,-0.0444946289,0.0184936523,0.0405883789,0.0136032104,0.00469970703,0.0390014648,-0.033203125,-0.0592956543,-0.0285949707,0,-0.0278930664,0.0396118164,0.0880126953,-0.0234985352,0.0060005188,-0.0104980469,0.0258026123,0.0109024048,-0.0133972168,-0.0243988037,0.00490188599,-0.013999939,0.0393981934,-0.00340080261,-0.0468139648,-0.016998291,0.037902832,0.00370025635,0.0422973633,-0.0339050293,0.0180969238,-0.0104980469,0.0187072754,-0.00770187378,0.0180053711,-0.0374145508,-0.00469970703,0.0759887695,-0.0480041504,0.0218963623,-0.025100708,0.0728149414,0.114196777,-0.0169067383,-0.0859985352,-0.0249023438,-0.021697998,0.0225982666,0.0761108398,-0.0454101562,0.00930023193,0.0272064209,0.0548095703,0.0574035645,-0.0260925293,0.000899791718,-0.00250053406,-0.00820159912,-0.0200958252,0.0361022949,-0.000500202179,-0.0897827148,-0.0133972168,0.0589904785,-0.00340080261,0.0320129395,0.00419998169,0.0651245117,-0.0419006348,-0.0367126465,-0.00410079956,-0.0634155273,-0.0616149902,-0.0132980347,0.00939941406,0.053314209,-0.10748291,0.0354003906,-0.0207061768,0.00680160522,0.0411071777,0.114990234,-0.0321044922,0.00109958649,0.037902832,-0.00429916382,-0.016494751,-0.0108032227,0.00559997559,0.0827026367,0.0563049316,-0.0254974365,-0.0582885742,0.0534057617,-0.013999939,-0.0797729492,-0.00619888306,-0.0375976562,0.0585021973,-0.106506348,0.0274047852,-0.0364990234,0.0473937988,0.0892944336,0.00500106812,-0.0178070068,0.0320129395,-0.0477905273,0.0679931641,-0.0544128418,0.0502929688,0.0175018311,-0.029800415,0.0220947266,0.00239944458,0.00289916992,0.0737915039,0.0991210938,-0.0318908691,-0.0182952881,0.0184936523,0.0252075195,-0.00309944153,-0.0157012939,0.0321960449,-0.0234069824,-0.0281066895,0.0502929688,-0.00289916992,0.0476989746,0.0404968262,-0.102905273,-0.0100021362,0.0318908691,0.0715942383,-0.0822753906,-0.0108032227,-0.0659790039,0.0074005127,-0.0245056152,0.0640258789,-0.0114974976,-0.0634155273,-0.0412902832,-0.0328979492,0.0102996826,-0.100585938,-0.00939941406,-0.0294952393,-0.00890350342,-0.00419998169,-0.0301055908,-0.0204925537,0.00200080872,-0.0581970215,0.000800132751,-0.0213928223,-0.0606994629,-0.0736083984,-0.0296936035,-0.0488891602,-0.0382995605,0.044708252,0.0184936523,0.0013999939,0.0173950195,-0.0173034668,0.102478027,0.0614929199,-0.0231018066,-0.00439834595,-0.0139007568,-0.0466918945,0.0315856934,0.0805053711,-0.0222015381,-0.0132980347,0.100524902,-0.0131988525,-0.0988769531,-0.0541992188,0.016204834,0.0130996704,0.0234985352,0.0361022949,-0.033996582,-0.0130004883,-0.0498046875,-0.00550079346,0.0676879883,0.0392150879,0.0117034912,-0.0112991333,-0.0610961914,0.0325927734,-0.00510025024,-0.0770263672,0.0419006348,-0.00579833984,-0.0437927246,-0.0254058838,-0.0585021973,-0.00579833984,0.0662231445,-0.0444030762,0.0236968994,-0.0618896484,-0.0260009766,-0.00250053406,-0.0736083984,-0.0111999512,0.0491943359,0.0656738281,0.078918457,0.094909668,0.0600891113,0.0430908203,0.0155029297,0.0548095703,0.0726928711,0.00129985809,-0.0480957031,-0.0167999268,0.0313110352,0.0452880859,0.0169067383,-0.0115966797,-0.00390052795,0.0930175781,-0.00949859619,0.0357971191,0.0354919434,-0.00289916992,0.0645141602,0.119995117,0.0452880859,0.0265960693,-0.0698242188]']"}})foo" ; |
227 | |
228 | TEST(ProtobufHelper, DesJson) { |
229 | for (auto json : {kTestJson, kCamelcaseJson}) { |
230 | proxima::be::proto::QueryRequest pb_req; |
231 | EXPECT_TRUE(ToMessagePb(kTestJson, &pb_req)); |
232 | |
233 | proxima::be::proto::QueryRequest brpc_req; |
234 | std::string error; |
235 | EXPECT_TRUE(ToMessageBrpc(kTestJson, &brpc_req, &error)); |
236 | EXPECT_EQ(error, "" ); |
237 | |
238 | proxima::be::proto::QueryRequest se_req; |
239 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(kTestJson, &se_req)); |
240 | |
241 | EXPECT_EQ(pb_req.SerializeAsString(), se_req.SerializeAsString()); |
242 | EXPECT_EQ(pb_req.DebugString(), se_req.DebugString()); |
243 | // brpc does not support camel case |
244 | // EXPECT_EQ(pb_req.DebugString(), brpc_req.DebugString()); |
245 | // EXPECT_EQ(pb_req.SerializeAsString(), brpc_req.SerializeAsString()); |
246 | } |
247 | } |
248 | |
249 | TEST(ProtobufHelper, DesJsonBench) { |
250 | const int COUNT = 10000; |
251 | std::vector<proto::QueryRequest> pb_reqs(COUNT); |
252 | std::vector<proto::QueryRequest> brpc_reqs(COUNT); |
253 | std::vector<proto::QueryRequest> se_reqs(COUNT); |
254 | uint64_t time_pb = 0; |
255 | uint64_t time_brpc = 0; |
256 | uint64_t time_se = 0; |
257 | { |
258 | ailego::ElapsedTime timer; |
259 | for (size_t i = 0; i < COUNT; i++) { |
260 | ToMessagePb(kTestJson, &pb_reqs[i]); |
261 | } |
262 | time_pb = timer.micro_seconds(); |
263 | } |
264 | { |
265 | ailego::ElapsedTime timer; |
266 | for (size_t i = 0; i < COUNT; i++) { |
267 | ToMessageBrpc(kTestJson, &brpc_reqs[i]); |
268 | } |
269 | time_brpc = timer.micro_seconds(); |
270 | } |
271 | { |
272 | ailego::ElapsedTime timer; |
273 | for (size_t i = 0; i < COUNT; i++) { |
274 | ProtobufHelper::JsonToMessage(kTestJson, &se_reqs[i]); |
275 | } |
276 | time_se = timer.micro_seconds(); |
277 | } |
278 | printf("be: %zu\n" , (size_t)time_se); |
279 | printf("pb: %zu\n" , (size_t)time_pb); |
280 | printf("brpc: %zu\n" , (size_t)time_brpc); |
281 | } |
282 | |
283 | TEST(ProtobufHelperDesJson, int32) { |
284 | ProtoTest t; |
285 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"i32":1})" , &t)); |
286 | EXPECT_EQ(t.i32(), 1); |
287 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
288 | R"({"i32":)" + std::to_string(std::numeric_limits<int32_t>::max()) + "}" , |
289 | &t)); |
290 | EXPECT_EQ(t.i32(), std::numeric_limits<int32_t>::max()); |
291 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
292 | R"({"i32":)" + std::to_string(std::numeric_limits<int32_t>::min()) + "}" , |
293 | &t)); |
294 | EXPECT_EQ(t.i32(), std::numeric_limits<int32_t>::min()); |
295 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"i32":null})" , &t)); |
296 | EXPECT_EQ(t.i32(), 0); |
297 | |
298 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32":1.0})" , &t)); |
299 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32":"1"})" , &t)); |
300 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32":[1]})" , &t)); |
301 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32":{"k":"v"}})" , &t)); |
302 | } |
303 | |
304 | TEST(ProtobufHelperDesJson, uint32) { |
305 | ProtoTest t; |
306 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"u32":1})" , &t)); |
307 | EXPECT_EQ(t.u32(), 1); |
308 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
309 | R"({"u32":)" + std::to_string(std::numeric_limits<uint32_t>::max()) + "}" , |
310 | &t)); |
311 | EXPECT_EQ(t.u32(), std::numeric_limits<uint32_t>::max()); |
312 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
313 | R"({"u32":)" + std::to_string(std::numeric_limits<uint32_t>::min()) + "}" , |
314 | &t)); |
315 | EXPECT_EQ(t.u32(), std::numeric_limits<uint32_t>::min()); |
316 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"u32":null})" , &t)); |
317 | EXPECT_EQ(t.u32(), 0); |
318 | |
319 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32":1.0})" , &t)); |
320 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32":"1"})" , &t)); |
321 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32":[1]})" , &t)); |
322 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32":{"k":"v"}})" , &t)); |
323 | } |
324 | |
325 | TEST(ProtobufHelperDesJson, int64) { |
326 | ProtoTest t; |
327 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"i64":1})" , &t)); |
328 | EXPECT_EQ(t.i64(), 1); |
329 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
330 | R"({"i64":)" + std::to_string(std::numeric_limits<int64_t>::max()) + "}" , |
331 | &t)); |
332 | EXPECT_EQ(t.i64(), std::numeric_limits<int64_t>::max()); |
333 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
334 | R"({"i64":)" + std::to_string(std::numeric_limits<int64_t>::min()) + "}" , |
335 | &t)); |
336 | EXPECT_EQ(t.i64(), std::numeric_limits<int64_t>::min()); |
337 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"i64":null})" , &t)); |
338 | EXPECT_EQ(t.i64(), 0); |
339 | |
340 | // test string |
341 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"i64":"1"})" , &t)); |
342 | EXPECT_EQ(t.i64(), 1); |
343 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
344 | R"({"i64":")" + std::to_string(std::numeric_limits<int64_t>::max()) + |
345 | "\"}" , |
346 | &t)); |
347 | EXPECT_EQ(t.i64(), std::numeric_limits<int64_t>::max()); |
348 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
349 | R"({"i64":")" + std::to_string(std::numeric_limits<int64_t>::min()) + |
350 | "\"}" , |
351 | &t)); |
352 | EXPECT_EQ(t.i64(), std::numeric_limits<int64_t>::min()); |
353 | |
354 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i64":1.0})" , &t)); |
355 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i64":[1]})" , &t)); |
356 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i64":{"k":"v"}})" , &t)); |
357 | } |
358 | |
359 | TEST(ProtobufHelperDesJson, uint64) { |
360 | ProtoTest t; |
361 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"u64":1})" , &t)); |
362 | EXPECT_EQ(t.u64(), 1); |
363 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
364 | R"({"u64":)" + std::to_string(std::numeric_limits<uint64_t>::max()) + "}" , |
365 | &t)); |
366 | EXPECT_EQ(t.u64(), std::numeric_limits<uint64_t>::max()); |
367 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
368 | R"({"u64":)" + std::to_string(std::numeric_limits<uint64_t>::min()) + "}" , |
369 | &t)); |
370 | EXPECT_EQ(t.u64(), std::numeric_limits<uint64_t>::min()); |
371 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"u64":null})" , &t)); |
372 | EXPECT_EQ(t.u64(), 0); |
373 | |
374 | // test string |
375 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"u64":"1"})" , &t)); |
376 | EXPECT_EQ(t.u64(), 1); |
377 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
378 | R"({"u64":")" + std::to_string(std::numeric_limits<uint64_t>::max()) + |
379 | "\"}" , |
380 | &t)); |
381 | EXPECT_EQ(t.u64(), std::numeric_limits<uint64_t>::max()); |
382 | EXPECT_TRUE(ProtobufHelper::JsonToMessage( |
383 | R"({"u64":")" + std::to_string(std::numeric_limits<uint64_t>::min()) + |
384 | "\"}" , |
385 | &t)); |
386 | EXPECT_EQ(t.u64(), std::numeric_limits<uint64_t>::min()); |
387 | |
388 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u64":1.0})" , &t)); |
389 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u64":[1]})" , &t)); |
390 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u64":{"k":"v"}})" , &t)); |
391 | } |
392 | |
393 | static void FloatMatch(const ProtoTest &s) { |
394 | ProtoTest d; |
395 | std::cout << ToJson(s) << std::endl; |
396 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
397 | EXPECT_FLOAT_EQ(d.f32(), s.f32()); |
398 | } |
399 | |
400 | static void FloatNan(const ProtoTest &s) { |
401 | ProtoTest d; |
402 | std::cout << ToJson(s) << std::endl; |
403 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
404 | EXPECT_TRUE(std::isnan(d.f32())); |
405 | } |
406 | |
407 | TEST(ProtobufHelperDesJson, float) { |
408 | ProtoTest s; |
409 | FloatMatch(s); |
410 | |
411 | s.set_f32(1.0); |
412 | FloatMatch(s); |
413 | |
414 | s.set_f32(std::numeric_limits<float>::max()); |
415 | FloatMatch(s); |
416 | s.set_f32(-std::numeric_limits<float>::max()); |
417 | FloatMatch(s); |
418 | |
419 | s.set_f32(std::numeric_limits<float>::min()); |
420 | FloatMatch(s); |
421 | s.set_f32(-std::numeric_limits<float>::min()); |
422 | FloatMatch(s); |
423 | |
424 | s.set_f32(std::numeric_limits<float>::infinity()); |
425 | FloatMatch(s); |
426 | s.set_f32(-std::numeric_limits<float>::infinity()); |
427 | FloatMatch(s); |
428 | |
429 | s.set_f32(std::numeric_limits<float>::quiet_NaN()); |
430 | FloatNan(s); |
431 | s.set_f32(-std::numeric_limits<float>::quiet_NaN()); |
432 | FloatNan(s); |
433 | |
434 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f32":"1"})" , &s)); |
435 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f32":[1]})" , &s)); |
436 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f32":{"k":"v"}})" , &s)); |
437 | } |
438 | |
439 | static void DoubleMatch(const ProtoTest &s) { |
440 | ProtoTest d; |
441 | std::cout << ToJson(s) << std::endl; |
442 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
443 | EXPECT_FLOAT_EQ(d.f64(), s.f64()); |
444 | } |
445 | |
446 | static void DoubleNan(const ProtoTest &s) { |
447 | ProtoTest d; |
448 | std::cout << ToJson(s) << std::endl; |
449 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
450 | EXPECT_TRUE(std::isnan(d.f64())); |
451 | } |
452 | |
453 | TEST(ProtobufHelperDesJson, double) { |
454 | ProtoTest s; |
455 | DoubleMatch(s); |
456 | |
457 | s.set_f64(1.0); |
458 | DoubleMatch(s); |
459 | |
460 | s.set_f64(std::numeric_limits<double>::max()); |
461 | DoubleMatch(s); |
462 | s.set_f64(-std::numeric_limits<double>::max()); |
463 | DoubleMatch(s); |
464 | |
465 | s.set_f64(std::numeric_limits<double>::min()); |
466 | DoubleMatch(s); |
467 | s.set_f64(-std::numeric_limits<double>::min()); |
468 | DoubleMatch(s); |
469 | |
470 | s.set_f64(std::numeric_limits<double>::infinity()); |
471 | DoubleMatch(s); |
472 | s.set_f64(-std::numeric_limits<double>::infinity()); |
473 | DoubleMatch(s); |
474 | |
475 | s.set_f64(std::numeric_limits<double>::quiet_NaN()); |
476 | DoubleNan(s); |
477 | s.set_f64(-std::numeric_limits<double>::quiet_NaN()); |
478 | DoubleNan(s); |
479 | |
480 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f64":"1"})" , &s)); |
481 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f64":[1]})" , &s)); |
482 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f64":{"k":"v"}})" , &s)); |
483 | } |
484 | |
485 | static void BoolMatch(const ProtoTest &s) { |
486 | ProtoTest d; |
487 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
488 | EXPECT_EQ(d.b(), s.b()); |
489 | } |
490 | |
491 | TEST(ProtobufHelperDesJson, bool) { |
492 | ProtoTest s; |
493 | BoolMatch(s); |
494 | |
495 | s.set_b(true); |
496 | BoolMatch(s); |
497 | |
498 | s.set_b(false); |
499 | BoolMatch(s); |
500 | |
501 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"b":1})" , &s)); |
502 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"b":1.0})" , &s)); |
503 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"b":"1"})" , &s)); |
504 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"b":[1]})" , &s)); |
505 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"b":{"k":"v"}})" , &s)); |
506 | } |
507 | |
508 | static void EnumMatch(const ProtoTest &s) { |
509 | ProtoTest d; |
510 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
511 | EXPECT_EQ(d.e(), s.e()); |
512 | } |
513 | |
514 | TEST(ProtobufHelperDesJson, enum) { |
515 | ProtoTest s; |
516 | EnumMatch(s); |
517 | |
518 | s.set_e(proto::test::ProtoTest_Enum_MON); |
519 | EnumMatch(s); |
520 | |
521 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"e":1})" , &s)); |
522 | EXPECT_EQ(s.e(), proto::test::ProtoTest_Enum_TUE); |
523 | |
524 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"e":"TUE"})" , &s)); |
525 | EXPECT_EQ(s.e(), proto::test::ProtoTest_Enum_TUE); |
526 | |
527 | // invalid value |
528 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"e":10000})" , &s)); |
529 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"e":"Invalid"})" , &s)); |
530 | |
531 | // invalid type |
532 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"e":1.0})" , &s)); |
533 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"e":"1"})" , &s)); |
534 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"e":[1]})" , &s)); |
535 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"e":{"k":"v"}})" , &s)); |
536 | } |
537 | |
538 | static void MessageMatch(const ProtoTest &s) { |
539 | ProtoTest d; |
540 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
541 | EXPECT_EQ(d.nest().i32(), s.nest().i32()); |
542 | EXPECT_EQ(d.nest().u32(), s.nest().u32()); |
543 | } |
544 | |
545 | TEST(ProtobufHelperDesJson, Message) { |
546 | ProtoTest s; |
547 | MessageMatch(s); |
548 | |
549 | s.mutable_nest()->set_i32(42); |
550 | MessageMatch(s); |
551 | |
552 | s.mutable_nest()->set_i32(42); |
553 | s.mutable_nest()->set_u32(2); |
554 | MessageMatch(s); |
555 | |
556 | s.mutable_nest()->set_i32(std::numeric_limits<int32_t>::max()); |
557 | s.mutable_nest()->set_u32(std::numeric_limits<uint32_t>::min()); |
558 | MessageMatch(s); |
559 | |
560 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nest":1})" , &s)); |
561 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nest":1.0})" , &s)); |
562 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nest":"1"})" , &s)); |
563 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nest":[1]})" , &s)); |
564 | } |
565 | |
566 | static void StringMatch(const ProtoTest &s) { |
567 | ProtoTest d; |
568 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
569 | EXPECT_EQ(d.str(), s.str()); |
570 | } |
571 | |
572 | TEST(ProtobufHelperDesJson, string) { |
573 | ProtoTest s; |
574 | StringMatch(s); |
575 | |
576 | s.set_str("42" ); |
577 | StringMatch(s); |
578 | |
579 | s.set_str("long long long long long long long long long long long long ago" ); |
580 | StringMatch(s); |
581 | |
582 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"str":1})" , &s)); |
583 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"str":1.0})" , &s)); |
584 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"str":[1]})" , &s)); |
585 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"str":{"k":"v"}})" , &s)); |
586 | } |
587 | |
588 | static void BinaryMatch(const ProtoTest &s) { |
589 | ProtoTest d; |
590 | std::cout << ToJson(s) << std::endl; |
591 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
592 | EXPECT_EQ(d.binary(), s.binary()); |
593 | } |
594 | |
595 | TEST(ProtobufHelperDesJson, binary) { |
596 | ProtoTest s; |
597 | BinaryMatch(s); |
598 | |
599 | s.set_binary("42" ); |
600 | BinaryMatch(s); |
601 | |
602 | s.set_binary( |
603 | "long long long long long long long long long long long long ago" ); |
604 | BinaryMatch(s); |
605 | |
606 | float v = 42.0; |
607 | s.set_binary(reinterpret_cast<char *>(&v), sizeof(v)); |
608 | BinaryMatch(s); |
609 | |
610 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"binary":1})" , &s)); |
611 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"binary":1.0})" , &s)); |
612 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"binary":[1]})" , &s)); |
613 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"binary":{"k":"v"}})" , &s)); |
614 | } |
615 | |
616 | static void Int32ArrayMatch(const ProtoTest &s) { |
617 | ProtoTest d; |
618 | std::cout << ToJson(s) << std::endl; |
619 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
620 | EXPECT_EQ(d.i32s_size(), s.i32s_size()); |
621 | for (int i = 0; i < d.i32s_size(); i++) { |
622 | EXPECT_EQ(d.i32s(i), s.i32s(i)); |
623 | } |
624 | } |
625 | |
626 | TEST(ProtobufHelperDesJson, Int32Array) { |
627 | ProtoTest s; |
628 | Int32ArrayMatch(s); |
629 | |
630 | s.add_i32s(42); |
631 | Int32ArrayMatch(s); |
632 | |
633 | s.add_i32s(std::numeric_limits<int32_t>::max()); |
634 | Int32ArrayMatch(s); |
635 | |
636 | s.add_i32s(std::numeric_limits<int32_t>::min()); |
637 | Int32ArrayMatch(s); |
638 | |
639 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32s":1})" , &s)); |
640 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32s":1.0})" , &s)); |
641 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32s":"string"})" , &s)); |
642 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32s":["string"}])" , &s)); |
643 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32s":[1,2,3.0])" , &s)); |
644 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i32s":{"k":"v"}})" , &s)); |
645 | } |
646 | |
647 | static void Uint32ArrayMatch(const ProtoTest &s) { |
648 | ProtoTest d; |
649 | std::cout << ToJson(s) << std::endl; |
650 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
651 | EXPECT_EQ(d.u32s_size(), s.u32s_size()); |
652 | for (int i = 0; i < d.u32s_size(); i++) { |
653 | EXPECT_EQ(d.u32s(i), s.u32s(i)); |
654 | } |
655 | } |
656 | |
657 | TEST(ProtobufHelperDesJson, Uint32Array) { |
658 | ProtoTest s; |
659 | Uint32ArrayMatch(s); |
660 | |
661 | s.add_u32s(42); |
662 | Uint32ArrayMatch(s); |
663 | |
664 | s.add_u32s(std::numeric_limits<uint32_t>::max()); |
665 | Uint32ArrayMatch(s); |
666 | |
667 | s.add_u32s(std::numeric_limits<uint32_t>::min()); |
668 | Uint32ArrayMatch(s); |
669 | |
670 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32s":1})" , &s)); |
671 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32s":1.0})" , &s)); |
672 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32s":"string"})" , &s)); |
673 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32s":["string"}])" , &s)); |
674 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32s":[1,2,3.0])" , &s)); |
675 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u32s":{"k":"v"}})" , &s)); |
676 | } |
677 | |
678 | static void Int64ArrayMatch(const ProtoTest &s) { |
679 | ProtoTest d; |
680 | std::cout << ToJson(s) << std::endl; |
681 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
682 | EXPECT_EQ(d.i64s_size(), s.i64s_size()); |
683 | for (int i = 0; i < d.i64s_size(); i++) { |
684 | EXPECT_EQ(d.i64s(i), s.i64s(i)); |
685 | } |
686 | } |
687 | |
688 | TEST(ProtobufHelperDesJson, Int64Array) { |
689 | ProtoTest s; |
690 | Int64ArrayMatch(s); |
691 | |
692 | s.add_i64s(42); |
693 | Int64ArrayMatch(s); |
694 | |
695 | s.add_i64s(std::numeric_limits<int64_t>::max()); |
696 | Int64ArrayMatch(s); |
697 | |
698 | s.add_i64s(std::numeric_limits<int64_t>::min()); |
699 | Int64ArrayMatch(s); |
700 | |
701 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i64s":1})" , &s)); |
702 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i64s":1.0})" , &s)); |
703 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i64s":"string"})" , &s)); |
704 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i64s":["string"}])" , &s)); |
705 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i64s":[1,2,3.0])" , &s)); |
706 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"i64s":{"k":"v"}})" , &s)); |
707 | } |
708 | |
709 | static void Uint64ArrayMatch(const ProtoTest &s) { |
710 | ProtoTest d; |
711 | std::cout << ToJson(s) << std::endl; |
712 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
713 | EXPECT_EQ(d.u64s_size(), s.u64s_size()); |
714 | for (int i = 0; i < d.u64s_size(); i++) { |
715 | EXPECT_EQ(d.u64s(i), s.u64s(i)); |
716 | } |
717 | } |
718 | |
719 | TEST(ProtobufHelperDesJson, Uint64Array) { |
720 | ProtoTest s; |
721 | Uint64ArrayMatch(s); |
722 | |
723 | s.add_u64s(42); |
724 | Uint64ArrayMatch(s); |
725 | |
726 | s.add_u64s(std::numeric_limits<uint64_t>::max()); |
727 | Uint64ArrayMatch(s); |
728 | |
729 | s.add_u64s(std::numeric_limits<uint64_t>::min()); |
730 | Uint64ArrayMatch(s); |
731 | |
732 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u64s":1})" , &s)); |
733 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u64s":1.0})" , &s)); |
734 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u64s":"string"})" , &s)); |
735 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u64s":["string"}])" , &s)); |
736 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u64s":[1,2,3.0])" , &s)); |
737 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"u64s":{"k":"v"}})" , &s)); |
738 | } |
739 | |
740 | static void FloatArrayMatch(const ProtoTest &s) { |
741 | ProtoTest d; |
742 | std::cout << ToJson(s) << std::endl; |
743 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
744 | EXPECT_EQ(d.f32s_size(), s.f32s_size()); |
745 | for (int i = 0; i < d.f32s_size(); i++) { |
746 | EXPECT_THAT(d.f32s(i), testing::NanSensitiveFloatEq(s.f32s(i))); |
747 | } |
748 | } |
749 | |
750 | TEST(ProtobufHelperDesJson, FloatArray) { |
751 | ProtoTest s; |
752 | FloatArrayMatch(s); |
753 | |
754 | s.add_f32s(42); |
755 | FloatArrayMatch(s); |
756 | |
757 | s.add_f32s(std::numeric_limits<float>::max()); |
758 | FloatArrayMatch(s); |
759 | s.add_f32s(-std::numeric_limits<float>::max()); |
760 | FloatArrayMatch(s); |
761 | |
762 | s.add_f32s(std::numeric_limits<float>::min()); |
763 | FloatArrayMatch(s); |
764 | s.add_f32s(-std::numeric_limits<float>::min()); |
765 | FloatArrayMatch(s); |
766 | |
767 | s.add_f32s(std::numeric_limits<float>::infinity()); |
768 | FloatArrayMatch(s); |
769 | s.add_f32s(-std::numeric_limits<float>::infinity()); |
770 | FloatArrayMatch(s); |
771 | |
772 | s.add_f32s(std::numeric_limits<float>::quiet_NaN()); |
773 | FloatArrayMatch(s); |
774 | s.add_f32s(-std::numeric_limits<float>::quiet_NaN()); |
775 | FloatArrayMatch(s); |
776 | |
777 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f32s":1})" , &s)); |
778 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f32s":1.0})" , &s)); |
779 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f32s":"string"})" , &s)); |
780 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f32s":["string"}])" , &s)); |
781 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f32s":[1,2,[]])" , &s)); |
782 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f32s":{"k":"v"}})" , &s)); |
783 | } |
784 | |
785 | static void DoubleArrayMatch(const ProtoTest &s) { |
786 | ProtoTest d; |
787 | std::cout << ToJson(s) << std::endl; |
788 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
789 | EXPECT_EQ(d.f64s_size(), s.f64s_size()); |
790 | for (int i = 0; i < d.f64s_size(); i++) { |
791 | EXPECT_THAT(d.f64s(i), testing::NanSensitiveDoubleEq(s.f64s(i))); |
792 | } |
793 | } |
794 | |
795 | TEST(ProtobufHelperDesJson, DoubleArray) { |
796 | ProtoTest s; |
797 | DoubleArrayMatch(s); |
798 | |
799 | s.add_f64s(42); |
800 | DoubleArrayMatch(s); |
801 | |
802 | s.add_f64s(std::numeric_limits<float>::max()); |
803 | DoubleArrayMatch(s); |
804 | s.add_f64s(-std::numeric_limits<float>::max()); |
805 | DoubleArrayMatch(s); |
806 | |
807 | s.add_f64s(std::numeric_limits<float>::min()); |
808 | DoubleArrayMatch(s); |
809 | s.add_f64s(-std::numeric_limits<float>::min()); |
810 | DoubleArrayMatch(s); |
811 | |
812 | s.add_f64s(std::numeric_limits<float>::infinity()); |
813 | DoubleArrayMatch(s); |
814 | s.add_f64s(-std::numeric_limits<float>::infinity()); |
815 | DoubleArrayMatch(s); |
816 | |
817 | s.add_f64s(std::numeric_limits<float>::quiet_NaN()); |
818 | DoubleArrayMatch(s); |
819 | s.add_f64s(-std::numeric_limits<float>::quiet_NaN()); |
820 | DoubleArrayMatch(s); |
821 | |
822 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f64s":1})" , &s)); |
823 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f64s":1.0})" , &s)); |
824 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f64s":"string"})" , &s)); |
825 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f64s":["string"}])" , &s)); |
826 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f64s":[1,2,[]])" , &s)); |
827 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"f64s":{"k":"v"}})" , &s)); |
828 | } |
829 | |
830 | static void BoolArrayMatch(const ProtoTest &s) { |
831 | ProtoTest d; |
832 | std::cout << ToJson(s) << std::endl; |
833 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
834 | EXPECT_EQ(d.bs_size(), s.bs_size()); |
835 | for (int i = 0; i < d.bs_size(); i++) { |
836 | EXPECT_EQ(d.bs(i), s.bs(i)); |
837 | } |
838 | } |
839 | |
840 | TEST(ProtobufHelperDesJson, BoolArray) { |
841 | ProtoTest s; |
842 | BoolArrayMatch(s); |
843 | |
844 | s.add_bs(42); |
845 | BoolArrayMatch(s); |
846 | |
847 | s.add_bs(true); |
848 | BoolArrayMatch(s); |
849 | |
850 | s.add_bs(false); |
851 | BoolArrayMatch(s); |
852 | |
853 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"bs":1})" , &s)); |
854 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"bs":1.0})" , &s)); |
855 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"bs":"string"})" , &s)); |
856 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"bs":["string"}])" , &s)); |
857 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"bs":[1,2,3.0])" , &s)); |
858 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"bs":{"k":"v"}})" , &s)); |
859 | } |
860 | |
861 | static void EnumArrayMatch(const ProtoTest &s) { |
862 | ProtoTest d; |
863 | std::cout << ToJson(s) << std::endl; |
864 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
865 | EXPECT_EQ(d.es_size(), s.es_size()); |
866 | for (int i = 0; i < d.es_size(); i++) { |
867 | EXPECT_EQ(d.es(i), s.es(i)); |
868 | } |
869 | } |
870 | |
871 | TEST(ProtobufHelperDesJson, EnumArray) { |
872 | ProtoTest s; |
873 | EnumArrayMatch(s); |
874 | |
875 | s.add_es(proto::test::ProtoTest_Enum_TUE); |
876 | EnumArrayMatch(s); |
877 | |
878 | s.add_es(proto::test::ProtoTest_Enum_MON); |
879 | EnumArrayMatch(s); |
880 | |
881 | s.add_es(proto::test::ProtoTest_Enum_SUN); |
882 | EnumArrayMatch(s); |
883 | |
884 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"es":["TUE"]})" , &s)); |
885 | EXPECT_EQ(s.es_size(), 1); |
886 | EXPECT_EQ(s.es(0), proto::test::ProtoTest_Enum_TUE); |
887 | |
888 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"es":["TUE", "MON"]})" , &s)); |
889 | EXPECT_EQ(s.es_size(), 2); |
890 | EXPECT_EQ(s.es(0), proto::test::ProtoTest_Enum_TUE); |
891 | EXPECT_EQ(s.es(1), proto::test::ProtoTest_Enum_MON); |
892 | |
893 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"es":["TUE", 0]})" , &s)); |
894 | EXPECT_EQ(s.es_size(), 2); |
895 | EXPECT_EQ(s.es(0), proto::test::ProtoTest_Enum_TUE); |
896 | EXPECT_EQ(s.es(1), proto::test::ProtoTest_Enum_MON); |
897 | |
898 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(R"({"es":[1, 0]})" , &s)); |
899 | EXPECT_EQ(s.es_size(), 2); |
900 | EXPECT_EQ(s.es(0), proto::test::ProtoTest_Enum_TUE); |
901 | EXPECT_EQ(s.es(1), proto::test::ProtoTest_Enum_MON); |
902 | |
903 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"es":1})" , &s)); |
904 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"es":1.0})" , &s)); |
905 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"es":"string"})" , &s)); |
906 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"es":["string"}])" , &s)); |
907 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"es":[1,2,3.0])" , &s)); |
908 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"es":{"k":"v"}})" , &s)); |
909 | } |
910 | |
911 | static void MessageArrayMatch(const ProtoTest &s) { |
912 | ProtoTest d; |
913 | std::cout << ToJson(s) << std::endl; |
914 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
915 | EXPECT_EQ(d.nests_size(), s.nests_size()); |
916 | for (int i = 0; i < d.nests_size(); i++) { |
917 | EXPECT_EQ(d.nests(i).i32(), s.nests(i).i32()); |
918 | EXPECT_EQ(d.nests(i).u32(), s.nests(i).u32()); |
919 | } |
920 | } |
921 | |
922 | TEST(ProtobufHelperDesJson, MessageArray) { |
923 | ProtoTest s; |
924 | MessageArrayMatch(s); |
925 | |
926 | auto *n = s.add_nests(); |
927 | MessageArrayMatch(s); |
928 | |
929 | n = s.add_nests(); |
930 | n->set_i32(432); |
931 | n->set_u32(234); |
932 | MessageArrayMatch(s); |
933 | |
934 | n = s.add_nests(); |
935 | n->set_i32(std::numeric_limits<int32_t>::min()); |
936 | n->set_u32(std::numeric_limits<uint32_t>::max()); |
937 | MessageArrayMatch(s); |
938 | |
939 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nests":1})" , &s)); |
940 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nests":1.0})" , &s)); |
941 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nests":"string"})" , &s)); |
942 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nests":["string"}])" , &s)); |
943 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nests":[1,2,3.0])" , &s)); |
944 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"nests":{"k":"v"}})" , &s)); |
945 | EXPECT_FALSE( |
946 | ProtobufHelper::JsonToMessage(R"({"nests":[1, {"k":"v"}]})" , &s)); |
947 | } |
948 | |
949 | static void StringArrayMatch(const ProtoTest &s) { |
950 | ProtoTest d; |
951 | std::cout << ToJson(s) << std::endl; |
952 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
953 | EXPECT_EQ(d.strs_size(), s.strs_size()); |
954 | for (int i = 0; i < d.strs_size(); i++) { |
955 | EXPECT_EQ(d.strs(i), s.strs(i)); |
956 | } |
957 | } |
958 | |
959 | TEST(ProtobufHelperDesJson, StringArray) { |
960 | ProtoTest s; |
961 | StringArrayMatch(s); |
962 | |
963 | s.add_strs("42" ); |
964 | StringArrayMatch(s); |
965 | |
966 | s.add_strs("long long long long long long long long long long long long ago" ); |
967 | StringArrayMatch(s); |
968 | |
969 | s.add_strs("" ); |
970 | StringArrayMatch(s); |
971 | |
972 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"strs":1})" , &s)); |
973 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"strs":1.0})" , &s)); |
974 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"strs":"string"})" , &s)); |
975 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"strs":["string", 1}])" , &s)); |
976 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"strs":[1,2,3.0])" , &s)); |
977 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"strs":{"k":"v"}})" , &s)); |
978 | } |
979 | |
980 | static void BinaryArrayMatch(const ProtoTest &s) { |
981 | ProtoTest d; |
982 | std::cout << ToJson(s) << std::endl; |
983 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
984 | EXPECT_EQ(d.binarys_size(), s.binarys_size()); |
985 | for (int i = 0; i < d.binarys_size(); i++) { |
986 | EXPECT_EQ(d.binarys(i), s.binarys(i)); |
987 | } |
988 | } |
989 | |
990 | TEST(ProtobufHelperDesJson, BinaryArray) { |
991 | ProtoTest s; |
992 | BinaryArrayMatch(s); |
993 | |
994 | s.add_binarys("42" ); |
995 | BinaryArrayMatch(s); |
996 | |
997 | s.add_binarys( |
998 | "long long long long long long long long long long long long ago" ); |
999 | BinaryArrayMatch(s); |
1000 | |
1001 | s.add_binarys("" ); |
1002 | BinaryArrayMatch(s); |
1003 | |
1004 | float v = 42.0; |
1005 | s.add_binarys(reinterpret_cast<char *>(&v), sizeof(v)); |
1006 | |
1007 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"binarys":1})" , &s)); |
1008 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"binarys":1.0})" , &s)); |
1009 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"binarys":"string"})" , &s)); |
1010 | EXPECT_FALSE( |
1011 | ProtobufHelper::JsonToMessage(R"({"binarys":["string", 1}])" , &s)); |
1012 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"binarys":[1,2,3.0])" , &s)); |
1013 | EXPECT_FALSE(ProtobufHelper::JsonToMessage(R"({"binarys":{"k":"v"}})" , &s)); |
1014 | } |
1015 | |
1016 | static void AllMatch(const ProtoTest &s) { |
1017 | ProtoTest d; |
1018 | std::cout << ToJson(s) << std::endl; |
1019 | EXPECT_TRUE(ProtobufHelper::JsonToMessage(ToJson(s), &d)); |
1020 | EXPECT_EQ(d.SerializeAsString(), s.SerializeAsString()); |
1021 | } |
1022 | |
1023 | TEST(ProtobufHelperDesJson, All) { |
1024 | ProtoTest s; |
1025 | AllMatch(s); |
1026 | |
1027 | s.set_i32(42); |
1028 | AllMatch(s); |
1029 | |
1030 | s.set_u32(42); |
1031 | AllMatch(s); |
1032 | |
1033 | s.set_i64(42); |
1034 | AllMatch(s); |
1035 | |
1036 | s.set_u64(42); |
1037 | AllMatch(s); |
1038 | |
1039 | s.set_b(true); |
1040 | AllMatch(s); |
1041 | |
1042 | s.set_f32(42.0); |
1043 | AllMatch(s); |
1044 | |
1045 | s.set_f64(42.0); |
1046 | AllMatch(s); |
1047 | |
1048 | s.set_e(proto::test::ProtoTest_Enum_TUE); |
1049 | AllMatch(s); |
1050 | |
1051 | s.set_str("42" ); |
1052 | AllMatch(s); |
1053 | |
1054 | s.set_binary("42" ); |
1055 | AllMatch(s); |
1056 | |
1057 | s.mutable_nest()->set_i32(42); |
1058 | AllMatch(s); |
1059 | |
1060 | s.mutable_nest()->set_u32(42); |
1061 | AllMatch(s); |
1062 | |
1063 | s.add_i32s(42); |
1064 | AllMatch(s); |
1065 | |
1066 | s.add_i32s(42); |
1067 | AllMatch(s); |
1068 | |
1069 | s.add_u32s(42); |
1070 | AllMatch(s); |
1071 | |
1072 | s.add_u32s(42); |
1073 | AllMatch(s); |
1074 | |
1075 | s.add_i64s(42); |
1076 | AllMatch(s); |
1077 | |
1078 | s.add_i64s(42); |
1079 | AllMatch(s); |
1080 | |
1081 | s.add_u64s(42); |
1082 | AllMatch(s); |
1083 | |
1084 | s.add_u64s(42); |
1085 | AllMatch(s); |
1086 | |
1087 | s.add_bs(true); |
1088 | AllMatch(s); |
1089 | |
1090 | s.add_bs(false); |
1091 | AllMatch(s); |
1092 | |
1093 | s.add_f32s(42); |
1094 | AllMatch(s); |
1095 | |
1096 | s.add_f32s(42); |
1097 | AllMatch(s); |
1098 | |
1099 | s.add_f64s(42); |
1100 | AllMatch(s); |
1101 | |
1102 | s.add_f64s(42); |
1103 | AllMatch(s); |
1104 | |
1105 | s.add_es(proto::test::ProtoTest_Enum_TUE); |
1106 | AllMatch(s); |
1107 | |
1108 | s.add_es(proto::test::ProtoTest_Enum_MON); |
1109 | AllMatch(s); |
1110 | |
1111 | s.add_nests()->set_i32(42); |
1112 | AllMatch(s); |
1113 | |
1114 | s.add_nests()->set_u32(42); |
1115 | AllMatch(s); |
1116 | |
1117 | s.add_strs("42" ); |
1118 | AllMatch(s); |
1119 | |
1120 | s.add_strs("42" ); |
1121 | AllMatch(s); |
1122 | |
1123 | s.add_binarys("42" ); |
1124 | AllMatch(s); |
1125 | |
1126 | s.add_binarys( |
1127 | "long long long long long long long long long long long long ago" ); |
1128 | AllMatch(s); |
1129 | } |
1130 | |
1131 | TEST(ProtobufHelperDesJson, Options) { |
1132 | ProtoTest s; |
1133 | EXPECT_FALSE( |
1134 | ProtobufHelper::JsonToMessage(R"({"i32":42,"non_exist":[]})" , &s)); |
1135 | |
1136 | ProtobufHelper::JsonParseOptions opt; |
1137 | opt.ignore_unknown_fields = true; |
1138 | EXPECT_TRUE( |
1139 | ProtobufHelper::JsonToMessage(R"({"i32":42,"non_exist":[]})" , opt, &s)); |
1140 | EXPECT_EQ(s.i32(), 42); |
1141 | } |
1142 | |
1143 | } // namespace be |
1144 | } // namespace proxima |
1145 | |