blob: 44eeef768d6d562856b9892460e7a72f1595848f [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Jake Farrell5d02b802014-01-07 21:42:01 -050020#define _USE_MATH_DEFINES
David Reiss00dcccf2007-07-21 01:18:10 +000021#include <cmath>
22#include "gen-cpp/DebugProtoTest_types.h"
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/protocol/TDebugProtocol.h>
cyy316723a2019-01-05 16:35:14 +080024#include <memory>
David Reiss00dcccf2007-07-21 01:18:10 +000025
Claudius Heine5ef662b2015-06-24 10:03:50 +020026#define BOOST_TEST_MODULE DebugProtoTest
27#include <boost/test/unit_test.hpp>
David Reiss00dcccf2007-07-21 01:18:10 +000028
Claudius Heine5ef662b2015-06-24 10:03:50 +020029using namespace thrift::test::debug;
David Reiss00dcccf2007-07-21 01:18:10 +000030
cyy316723a2019-01-05 16:35:14 +080031static ::std::shared_ptr<OneOfEach> ooe;
David Reiss00dcccf2007-07-21 01:18:10 +000032
Claudius Heine5ef662b2015-06-24 10:03:50 +020033void testCaseSetup_1() {
34 ooe.reset(new OneOfEach);
35 ooe->im_true = true;
36 ooe->im_false = false;
37 ooe->a_bite = 0x7f;
38 ooe->integer16 = 27000;
39 ooe->integer32 = 1 << 24;
40 ooe->integer64 = (uint64_t)6000 * 1000 * 1000;
41 ooe->double_precision = M_PI;
42 ooe->some_characters = "Debug THIS!";
43 ooe->zomg_unicode = "\xd7\n\a\t";
44}
David Reiss00dcccf2007-07-21 01:18:10 +000045
Claudius Heine5ef662b2015-06-24 10:03:50 +020046BOOST_AUTO_TEST_CASE(test_debug_proto_1) {
47 testCaseSetup_1();
David Reiss00dcccf2007-07-21 01:18:10 +000048
Claudius Heine5ef662b2015-06-24 10:03:50 +020049 const std::string expected_result(
50 "OneOfEach {\n"
51 " 01: im_true (bool) = true,\n"
52 " 02: im_false (bool) = false,\n"
53 " 03: a_bite (byte) = 0x7f,\n"
54 " 04: integer16 (i16) = 27000,\n"
55 " 05: integer32 (i32) = 16777216,\n"
56 " 06: integer64 (i64) = 6000000000,\n"
57 " 07: double_precision (double) = 3.1415926535897931,\n"
58 " 08: some_characters (string) = \"Debug THIS!\",\n"
59 " 09: zomg_unicode (string) = \"\\xd7\\n\\a\\t\",\n"
60 " 10: what_who (bool) = false,\n"
61 " 11: base64 (string) = \"\",\n"
62 " 12: byte_list (list) = list<byte>[3] {\n"
63 " [0] = 0x01,\n"
64 " [1] = 0x02,\n"
65 " [2] = 0x03,\n"
66 " },\n"
67 " 13: i16_list (list) = list<i16>[3] {\n"
68 " [0] = 1,\n"
69 " [1] = 2,\n"
70 " [2] = 3,\n"
71 " },\n"
72 " 14: i64_list (list) = list<i64>[3] {\n"
73 " [0] = 1,\n"
74 " [1] = 2,\n"
75 " [2] = 3,\n"
76 " },\n"
CJCombrink1d886ca2024-03-23 21:32:28 +010077 " 15: rfc4122_uuid (uuid) = {\n"
CJCombrink1d886ca2024-03-23 21:32:28 +010078 " [raw] = \"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\",\n"
79 " [enc] = \"00000000-0000-0000-0000-000000000000\"\n"
80 " }\n"
Claudius Heine5ef662b2015-06-24 10:03:50 +020081 "}");
82 const std::string result(apache::thrift::ThriftDebugString(*ooe));
David Reiss00dcccf2007-07-21 01:18:10 +000083
Claudius Heine5ef662b2015-06-24 10:03:50 +020084 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
85 "Expected:\n" << expected_result << "\nGotten:\n" << result);
86}
87
cyy316723a2019-01-05 16:35:14 +080088static ::std::shared_ptr<Nesting> n;
Claudius Heine5ef662b2015-06-24 10:03:50 +020089
90void testCaseSetup_2() {
91 testCaseSetup_1();
92
93 n.reset(new Nesting);
94 n->my_ooe = *ooe;
95 n->my_ooe.integer16 = 16;
96 n->my_ooe.integer32 = 32;
97 n->my_ooe.integer64 = 64;
98 n->my_ooe.double_precision = (std::sqrt(5.0) + 1) / 2;
99 n->my_ooe.some_characters = ":R (me going \"rrrr\")";
100 n->my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20\xd0\x9d\xce"
101 "\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0"
102 "\xb0\xcf\x81\xe2\x84\x8e\x20\xce\x91\x74\x74"
103 "\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80"
104 "\xbc";
CJCombrink4b909092024-04-27 19:51:39 +0200105 n->my_ooe.rfc4122_uuid = apache::thrift::TUuid{"{5e2ab188-1726-4e75-a04f-1ed9a6a89c4c}"};
Claudius Heine5ef662b2015-06-24 10:03:50 +0200106 n->my_bonk.type = 31337;
107 n->my_bonk.message = "I am a bonk... xor!";
108}
109
110BOOST_AUTO_TEST_CASE(test_debug_proto_2) {
111 testCaseSetup_2();
112
113 const std::string expected_result(
114 "Nesting {\n"
115 " 01: my_bonk (struct) = Bonk {\n"
116 " 01: type (i32) = 31337,\n"
117 " 02: message (string) = \"I am a bonk... xor!\",\n"
118 " },\n"
119 " 02: my_ooe (struct) = OneOfEach {\n"
120 " 01: im_true (bool) = true,\n"
121 " 02: im_false (bool) = false,\n"
122 " 03: a_bite (byte) = 0x7f,\n"
123 " 04: integer16 (i16) = 16,\n"
124 " 05: integer32 (i32) = 32,\n"
125 " 06: integer64 (i64) = 64,\n"
126 " 07: double_precision (double) = 1.6180339887498949,\n"
127 " 08: some_characters (string) = \":R (me going \\\"rrrr\\\")\",\n"
128 " 09: zomg_unicode (string) = \"\\xd3\\x80\\xe2\\x85\\xae\\xce\\x9d \\xd"
129 "0\\x9d\\xce\\xbf\\xe2\\x85\\xbf\\xd0\\xbe\\xc9\\xa1\\xd0\\xb3\\xd0\\xb0"
130 "\\xcf\\x81\\xe2\\x84\\x8e \\xce\\x91tt\\xce\\xb1\\xe2\\x85\\xbd\\xce\\xb"
131 "a\\xc7\\x83\\xe2\\x80\\xbc\",\n"
132 " 10: what_who (bool) = false,\n"
133 " 11: base64 (string) = \"\",\n"
134 " 12: byte_list (list) = list<byte>[3] {\n"
135 " [0] = 0x01,\n"
136 " [1] = 0x02,\n"
137 " [2] = 0x03,\n"
138 " },\n"
139 " 13: i16_list (list) = list<i16>[3] {\n"
140 " [0] = 1,\n"
141 " [1] = 2,\n"
142 " [2] = 3,\n"
143 " },\n"
144 " 14: i64_list (list) = list<i64>[3] {\n"
145 " [0] = 1,\n"
146 " [1] = 2,\n"
147 " [2] = 3,\n"
148 " },\n"
CJCombrink1d886ca2024-03-23 21:32:28 +0100149 " 15: rfc4122_uuid (uuid) = {\n"
CJCombrink1d886ca2024-03-23 21:32:28 +0100150 " [raw] = \"^*\\xb1\\x88\\x17&Nu\\xa0O\\x1e\\xd9\\xa6\\xa8\\x9cL\",\n"
151 " [enc] = \"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c\"\n"
152 " }\n"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200153 " },\n"
154 "}");
155 const std::string result(apache::thrift::ThriftDebugString(*n));
156
157 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
158 "Expected:\n" << expected_result << "\nGotten:\n" << result);
159}
160
cyy316723a2019-01-05 16:35:14 +0800161static ::std::shared_ptr<HolyMoley> hm;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200162
163void testCaseSetup_3() {
164 testCaseSetup_2();
165
166 hm.reset(new HolyMoley);
167
168 hm->big.push_back(*ooe);
169 hm->big.push_back(n->my_ooe);
170 hm->big[0].a_bite = 0x22;
171 hm->big[1].a_bite = 0x33;
David Reiss00dcccf2007-07-21 01:18:10 +0000172
173 std::vector<std::string> stage1;
174 stage1.push_back("and a one");
175 stage1.push_back("and a two");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200176 hm->contain.insert(stage1);
David Reiss00dcccf2007-07-21 01:18:10 +0000177 stage1.clear();
178 stage1.push_back("then a one, two");
179 stage1.push_back("three!");
180 stage1.push_back("FOUR!!");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200181 hm->contain.insert(stage1);
David Reiss00dcccf2007-07-21 01:18:10 +0000182 stage1.clear();
Claudius Heine5ef662b2015-06-24 10:03:50 +0200183 hm->contain.insert(stage1);
David Reiss00dcccf2007-07-21 01:18:10 +0000184
185 std::vector<Bonk> stage2;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200186 hm->bonks["nothing"] = stage2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100187 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000188 stage2.back().type = 1;
189 stage2.back().message = "Wait.";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100190 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000191 stage2.back().type = 2;
192 stage2.back().message = "What?";
Claudius Heine5ef662b2015-06-24 10:03:50 +0200193 hm->bonks["something"] = stage2;
David Reiss00dcccf2007-07-21 01:18:10 +0000194 stage2.clear();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100195 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000196 stage2.back().type = 3;
197 stage2.back().message = "quoth";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100198 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000199 stage2.back().type = 4;
200 stage2.back().message = "the raven";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100201 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000202 stage2.back().type = 5;
203 stage2.back().message = "nevermore";
Claudius Heine5ef662b2015-06-24 10:03:50 +0200204 hm->bonks["poe"] = stage2;
205}
David Reiss00dcccf2007-07-21 01:18:10 +0000206
Claudius Heine5ef662b2015-06-24 10:03:50 +0200207BOOST_AUTO_TEST_CASE(test_debug_proto_3) {
208 testCaseSetup_3();
David Reiss00dcccf2007-07-21 01:18:10 +0000209
Claudius Heine5ef662b2015-06-24 10:03:50 +0200210 const std::string expected_result(
211 "HolyMoley {\n"
212 " 01: big (list) = list<struct>[2] {\n"
213 " [0] = OneOfEach {\n"
214 " 01: im_true (bool) = true,\n"
215 " 02: im_false (bool) = false,\n"
216 " 03: a_bite (byte) = 0x22,\n"
217 " 04: integer16 (i16) = 27000,\n"
218 " 05: integer32 (i32) = 16777216,\n"
219 " 06: integer64 (i64) = 6000000000,\n"
220 " 07: double_precision (double) = 3.1415926535897931,\n"
221 " 08: some_characters (string) = \"Debug THIS!\",\n"
222 " 09: zomg_unicode (string) = \"\\xd7\\n\\a\\t\",\n"
223 " 10: what_who (bool) = false,\n"
224 " 11: base64 (string) = \"\",\n"
225 " 12: byte_list (list) = list<byte>[3] {\n"
226 " [0] = 0x01,\n"
227 " [1] = 0x02,\n"
228 " [2] = 0x03,\n"
229 " },\n"
230 " 13: i16_list (list) = list<i16>[3] {\n"
231 " [0] = 1,\n"
232 " [1] = 2,\n"
233 " [2] = 3,\n"
234 " },\n"
235 " 14: i64_list (list) = list<i64>[3] {\n"
236 " [0] = 1,\n"
237 " [1] = 2,\n"
238 " [2] = 3,\n"
239 " },\n"
CJCombrink1d886ca2024-03-23 21:32:28 +0100240 " 15: rfc4122_uuid (uuid) = {\n"
CJCombrink1d886ca2024-03-23 21:32:28 +0100241 " [raw] = \"\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\",\n"
242 " [enc] = \"00000000-0000-0000-0000-000000000000\"\n"
243 " }\n"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200244 " },\n"
245 " [1] = OneOfEach {\n"
246 " 01: im_true (bool) = true,\n"
247 " 02: im_false (bool) = false,\n"
248 " 03: a_bite (byte) = 0x33,\n"
249 " 04: integer16 (i16) = 16,\n"
250 " 05: integer32 (i32) = 32,\n"
251 " 06: integer64 (i64) = 64,\n"
252 " 07: double_precision (double) = 1.6180339887498949,\n"
253 " 08: some_characters (string) = \":R (me going \\\"rrrr\\\")\",\n"
254 " 09: zomg_unicode (string) = \"\\xd3\\x80\\xe2\\x85\\xae\\xce\\x9d \\"
255 "xd0\\x9d\\xce\\xbf\\xe2\\x85\\xbf\\xd0\\xbe\\xc9\\xa1\\xd0\\xb3\\xd0\\xb"
256 "0\\xcf\\x81\\xe2\\x84\\x8e \\xce\\x91tt\\xce\\xb1\\xe2\\x85\\xbd\\xce\\x"
257 "ba\\xc7\\x83\\xe2\\x80\\xbc\",\n"
258 " 10: what_who (bool) = false,\n"
259 " 11: base64 (string) = \"\",\n"
260 " 12: byte_list (list) = list<byte>[3] {\n"
261 " [0] = 0x01,\n"
262 " [1] = 0x02,\n"
263 " [2] = 0x03,\n"
264 " },\n"
265 " 13: i16_list (list) = list<i16>[3] {\n"
266 " [0] = 1,\n"
267 " [1] = 2,\n"
268 " [2] = 3,\n"
269 " },\n"
270 " 14: i64_list (list) = list<i64>[3] {\n"
271 " [0] = 1,\n"
272 " [1] = 2,\n"
273 " [2] = 3,\n"
274 " },\n"
CJCombrink1d886ca2024-03-23 21:32:28 +0100275 " 15: rfc4122_uuid (uuid) = {\n"
CJCombrink1d886ca2024-03-23 21:32:28 +0100276 " [raw] = \"^*\\xb1\\x88\\x17&Nu\\xa0O\\x1e\\xd9\\xa6\\xa8\\x9cL\",\n"
277 " [enc] = \"5e2ab188-1726-4e75-a04f-1ed9a6a89c4c\"\n"
278 " }\n"
Claudius Heine5ef662b2015-06-24 10:03:50 +0200279 " },\n"
280 " },\n"
281 " 02: contain (set) = set<list>[3] {\n"
282 " list<string>[0] {\n"
283 " },\n"
284 " list<string>[2] {\n"
285 " [0] = \"and a one\",\n"
286 " [1] = \"and a two\",\n"
287 " },\n"
288 " list<string>[3] {\n"
289 " [0] = \"then a one, two\",\n"
290 " [1] = \"three!\",\n"
291 " [2] = \"FOUR!!\",\n"
292 " },\n"
293 " },\n"
294 " 03: bonks (map) = map<string,list>[3] {\n"
295 " \"nothing\" -> list<struct>[0] {\n"
296 " },\n"
297 " \"poe\" -> list<struct>[3] {\n"
298 " [0] = Bonk {\n"
299 " 01: type (i32) = 3,\n"
300 " 02: message (string) = \"quoth\",\n"
301 " },\n"
302 " [1] = Bonk {\n"
303 " 01: type (i32) = 4,\n"
304 " 02: message (string) = \"the raven\",\n"
305 " },\n"
306 " [2] = Bonk {\n"
307 " 01: type (i32) = 5,\n"
308 " 02: message (string) = \"nevermore\",\n"
309 " },\n"
310 " },\n"
311 " \"something\" -> list<struct>[2] {\n"
312 " [0] = Bonk {\n"
313 " 01: type (i32) = 1,\n"
314 " 02: message (string) = \"Wait.\",\n"
315 " },\n"
316 " [1] = Bonk {\n"
317 " 01: type (i32) = 2,\n"
318 " 02: message (string) = \"What?\",\n"
319 " },\n"
320 " },\n"
321 " },\n"
322 "}");
323 const std::string result(apache::thrift::ThriftDebugString(*hm));
324
325 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
326 "Expected:\n" << expected_result << "\nGotten:\n" << result);
David Reiss00dcccf2007-07-21 01:18:10 +0000327}