blob: 607744b3853ede044232e1df943523b6bf233424 [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>
David Reiss00dcccf2007-07-21 01:18:10 +000024
Claudius Heine5ef662b2015-06-24 10:03:50 +020025#define BOOST_TEST_MODULE DebugProtoTest
26#include <boost/test/unit_test.hpp>
David Reiss00dcccf2007-07-21 01:18:10 +000027
Claudius Heine5ef662b2015-06-24 10:03:50 +020028using namespace thrift::test::debug;
David Reiss00dcccf2007-07-21 01:18:10 +000029
Claudius Heine5ef662b2015-06-24 10:03:50 +020030static std::auto_ptr<OneOfEach> ooe;
David Reiss00dcccf2007-07-21 01:18:10 +000031
Claudius Heine5ef662b2015-06-24 10:03:50 +020032void testCaseSetup_1() {
33 ooe.reset(new OneOfEach);
34 ooe->im_true = true;
35 ooe->im_false = false;
36 ooe->a_bite = 0x7f;
37 ooe->integer16 = 27000;
38 ooe->integer32 = 1 << 24;
39 ooe->integer64 = (uint64_t)6000 * 1000 * 1000;
40 ooe->double_precision = M_PI;
41 ooe->some_characters = "Debug THIS!";
42 ooe->zomg_unicode = "\xd7\n\a\t";
43}
David Reiss00dcccf2007-07-21 01:18:10 +000044
Claudius Heine5ef662b2015-06-24 10:03:50 +020045BOOST_AUTO_TEST_CASE(test_debug_proto_1) {
46 testCaseSetup_1();
David Reiss00dcccf2007-07-21 01:18:10 +000047
Claudius Heine5ef662b2015-06-24 10:03:50 +020048 const std::string expected_result(
49 "OneOfEach {\n"
50 " 01: im_true (bool) = true,\n"
51 " 02: im_false (bool) = false,\n"
52 " 03: a_bite (byte) = 0x7f,\n"
53 " 04: integer16 (i16) = 27000,\n"
54 " 05: integer32 (i32) = 16777216,\n"
55 " 06: integer64 (i64) = 6000000000,\n"
56 " 07: double_precision (double) = 3.1415926535897931,\n"
57 " 08: some_characters (string) = \"Debug THIS!\",\n"
58 " 09: zomg_unicode (string) = \"\\xd7\\n\\a\\t\",\n"
59 " 10: what_who (bool) = false,\n"
60 " 11: base64 (string) = \"\",\n"
61 " 12: byte_list (list) = list<byte>[3] {\n"
62 " [0] = 0x01,\n"
63 " [1] = 0x02,\n"
64 " [2] = 0x03,\n"
65 " },\n"
66 " 13: i16_list (list) = list<i16>[3] {\n"
67 " [0] = 1,\n"
68 " [1] = 2,\n"
69 " [2] = 3,\n"
70 " },\n"
71 " 14: i64_list (list) = list<i64>[3] {\n"
72 " [0] = 1,\n"
73 " [1] = 2,\n"
74 " [2] = 3,\n"
75 " },\n"
76 "}");
77 const std::string result(apache::thrift::ThriftDebugString(*ooe));
David Reiss00dcccf2007-07-21 01:18:10 +000078
Claudius Heine5ef662b2015-06-24 10:03:50 +020079 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
80 "Expected:\n" << expected_result << "\nGotten:\n" << result);
81}
82
83static std::auto_ptr<Nesting> n;
84
85void testCaseSetup_2() {
86 testCaseSetup_1();
87
88 n.reset(new Nesting);
89 n->my_ooe = *ooe;
90 n->my_ooe.integer16 = 16;
91 n->my_ooe.integer32 = 32;
92 n->my_ooe.integer64 = 64;
93 n->my_ooe.double_precision = (std::sqrt(5.0) + 1) / 2;
94 n->my_ooe.some_characters = ":R (me going \"rrrr\")";
95 n->my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20\xd0\x9d\xce"
96 "\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0"
97 "\xb0\xcf\x81\xe2\x84\x8e\x20\xce\x91\x74\x74"
98 "\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80"
99 "\xbc";
100 n->my_bonk.type = 31337;
101 n->my_bonk.message = "I am a bonk... xor!";
102}
103
104BOOST_AUTO_TEST_CASE(test_debug_proto_2) {
105 testCaseSetup_2();
106
107 const std::string expected_result(
108 "Nesting {\n"
109 " 01: my_bonk (struct) = Bonk {\n"
110 " 01: type (i32) = 31337,\n"
111 " 02: message (string) = \"I am a bonk... xor!\",\n"
112 " },\n"
113 " 02: my_ooe (struct) = OneOfEach {\n"
114 " 01: im_true (bool) = true,\n"
115 " 02: im_false (bool) = false,\n"
116 " 03: a_bite (byte) = 0x7f,\n"
117 " 04: integer16 (i16) = 16,\n"
118 " 05: integer32 (i32) = 32,\n"
119 " 06: integer64 (i64) = 64,\n"
120 " 07: double_precision (double) = 1.6180339887498949,\n"
121 " 08: some_characters (string) = \":R (me going \\\"rrrr\\\")\",\n"
122 " 09: zomg_unicode (string) = \"\\xd3\\x80\\xe2\\x85\\xae\\xce\\x9d \\xd"
123 "0\\x9d\\xce\\xbf\\xe2\\x85\\xbf\\xd0\\xbe\\xc9\\xa1\\xd0\\xb3\\xd0\\xb0"
124 "\\xcf\\x81\\xe2\\x84\\x8e \\xce\\x91tt\\xce\\xb1\\xe2\\x85\\xbd\\xce\\xb"
125 "a\\xc7\\x83\\xe2\\x80\\xbc\",\n"
126 " 10: what_who (bool) = false,\n"
127 " 11: base64 (string) = \"\",\n"
128 " 12: byte_list (list) = list<byte>[3] {\n"
129 " [0] = 0x01,\n"
130 " [1] = 0x02,\n"
131 " [2] = 0x03,\n"
132 " },\n"
133 " 13: i16_list (list) = list<i16>[3] {\n"
134 " [0] = 1,\n"
135 " [1] = 2,\n"
136 " [2] = 3,\n"
137 " },\n"
138 " 14: i64_list (list) = list<i64>[3] {\n"
139 " [0] = 1,\n"
140 " [1] = 2,\n"
141 " [2] = 3,\n"
142 " },\n"
143 " },\n"
144 "}");
145 const std::string result(apache::thrift::ThriftDebugString(*n));
146
147 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
148 "Expected:\n" << expected_result << "\nGotten:\n" << result);
149}
150
151static std::auto_ptr<HolyMoley> hm;
152
153void testCaseSetup_3() {
154 testCaseSetup_2();
155
156 hm.reset(new HolyMoley);
157
158 hm->big.push_back(*ooe);
159 hm->big.push_back(n->my_ooe);
160 hm->big[0].a_bite = 0x22;
161 hm->big[1].a_bite = 0x33;
David Reiss00dcccf2007-07-21 01:18:10 +0000162
163 std::vector<std::string> stage1;
164 stage1.push_back("and a one");
165 stage1.push_back("and a two");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200166 hm->contain.insert(stage1);
David Reiss00dcccf2007-07-21 01:18:10 +0000167 stage1.clear();
168 stage1.push_back("then a one, two");
169 stage1.push_back("three!");
170 stage1.push_back("FOUR!!");
Claudius Heine5ef662b2015-06-24 10:03:50 +0200171 hm->contain.insert(stage1);
David Reiss00dcccf2007-07-21 01:18:10 +0000172 stage1.clear();
Claudius Heine5ef662b2015-06-24 10:03:50 +0200173 hm->contain.insert(stage1);
David Reiss00dcccf2007-07-21 01:18:10 +0000174
175 std::vector<Bonk> stage2;
Claudius Heine5ef662b2015-06-24 10:03:50 +0200176 hm->bonks["nothing"] = stage2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100177 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000178 stage2.back().type = 1;
179 stage2.back().message = "Wait.";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100180 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000181 stage2.back().type = 2;
182 stage2.back().message = "What?";
Claudius Heine5ef662b2015-06-24 10:03:50 +0200183 hm->bonks["something"] = stage2;
David Reiss00dcccf2007-07-21 01:18:10 +0000184 stage2.clear();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100185 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000186 stage2.back().type = 3;
187 stage2.back().message = "quoth";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100188 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000189 stage2.back().type = 4;
190 stage2.back().message = "the raven";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100191 stage2.resize(stage2.size() + 1);
David Reiss00dcccf2007-07-21 01:18:10 +0000192 stage2.back().type = 5;
193 stage2.back().message = "nevermore";
Claudius Heine5ef662b2015-06-24 10:03:50 +0200194 hm->bonks["poe"] = stage2;
195}
David Reiss00dcccf2007-07-21 01:18:10 +0000196
Claudius Heine5ef662b2015-06-24 10:03:50 +0200197BOOST_AUTO_TEST_CASE(test_debug_proto_3) {
198 testCaseSetup_3();
David Reiss00dcccf2007-07-21 01:18:10 +0000199
Claudius Heine5ef662b2015-06-24 10:03:50 +0200200 const std::string expected_result(
201 "HolyMoley {\n"
202 " 01: big (list) = list<struct>[2] {\n"
203 " [0] = OneOfEach {\n"
204 " 01: im_true (bool) = true,\n"
205 " 02: im_false (bool) = false,\n"
206 " 03: a_bite (byte) = 0x22,\n"
207 " 04: integer16 (i16) = 27000,\n"
208 " 05: integer32 (i32) = 16777216,\n"
209 " 06: integer64 (i64) = 6000000000,\n"
210 " 07: double_precision (double) = 3.1415926535897931,\n"
211 " 08: some_characters (string) = \"Debug THIS!\",\n"
212 " 09: zomg_unicode (string) = \"\\xd7\\n\\a\\t\",\n"
213 " 10: what_who (bool) = false,\n"
214 " 11: base64 (string) = \"\",\n"
215 " 12: byte_list (list) = list<byte>[3] {\n"
216 " [0] = 0x01,\n"
217 " [1] = 0x02,\n"
218 " [2] = 0x03,\n"
219 " },\n"
220 " 13: i16_list (list) = list<i16>[3] {\n"
221 " [0] = 1,\n"
222 " [1] = 2,\n"
223 " [2] = 3,\n"
224 " },\n"
225 " 14: i64_list (list) = list<i64>[3] {\n"
226 " [0] = 1,\n"
227 " [1] = 2,\n"
228 " [2] = 3,\n"
229 " },\n"
230 " },\n"
231 " [1] = OneOfEach {\n"
232 " 01: im_true (bool) = true,\n"
233 " 02: im_false (bool) = false,\n"
234 " 03: a_bite (byte) = 0x33,\n"
235 " 04: integer16 (i16) = 16,\n"
236 " 05: integer32 (i32) = 32,\n"
237 " 06: integer64 (i64) = 64,\n"
238 " 07: double_precision (double) = 1.6180339887498949,\n"
239 " 08: some_characters (string) = \":R (me going \\\"rrrr\\\")\",\n"
240 " 09: zomg_unicode (string) = \"\\xd3\\x80\\xe2\\x85\\xae\\xce\\x9d \\"
241 "xd0\\x9d\\xce\\xbf\\xe2\\x85\\xbf\\xd0\\xbe\\xc9\\xa1\\xd0\\xb3\\xd0\\xb"
242 "0\\xcf\\x81\\xe2\\x84\\x8e \\xce\\x91tt\\xce\\xb1\\xe2\\x85\\xbd\\xce\\x"
243 "ba\\xc7\\x83\\xe2\\x80\\xbc\",\n"
244 " 10: what_who (bool) = false,\n"
245 " 11: base64 (string) = \"\",\n"
246 " 12: byte_list (list) = list<byte>[3] {\n"
247 " [0] = 0x01,\n"
248 " [1] = 0x02,\n"
249 " [2] = 0x03,\n"
250 " },\n"
251 " 13: i16_list (list) = list<i16>[3] {\n"
252 " [0] = 1,\n"
253 " [1] = 2,\n"
254 " [2] = 3,\n"
255 " },\n"
256 " 14: i64_list (list) = list<i64>[3] {\n"
257 " [0] = 1,\n"
258 " [1] = 2,\n"
259 " [2] = 3,\n"
260 " },\n"
261 " },\n"
262 " },\n"
263 " 02: contain (set) = set<list>[3] {\n"
264 " list<string>[0] {\n"
265 " },\n"
266 " list<string>[2] {\n"
267 " [0] = \"and a one\",\n"
268 " [1] = \"and a two\",\n"
269 " },\n"
270 " list<string>[3] {\n"
271 " [0] = \"then a one, two\",\n"
272 " [1] = \"three!\",\n"
273 " [2] = \"FOUR!!\",\n"
274 " },\n"
275 " },\n"
276 " 03: bonks (map) = map<string,list>[3] {\n"
277 " \"nothing\" -> list<struct>[0] {\n"
278 " },\n"
279 " \"poe\" -> list<struct>[3] {\n"
280 " [0] = Bonk {\n"
281 " 01: type (i32) = 3,\n"
282 " 02: message (string) = \"quoth\",\n"
283 " },\n"
284 " [1] = Bonk {\n"
285 " 01: type (i32) = 4,\n"
286 " 02: message (string) = \"the raven\",\n"
287 " },\n"
288 " [2] = Bonk {\n"
289 " 01: type (i32) = 5,\n"
290 " 02: message (string) = \"nevermore\",\n"
291 " },\n"
292 " },\n"
293 " \"something\" -> list<struct>[2] {\n"
294 " [0] = Bonk {\n"
295 " 01: type (i32) = 1,\n"
296 " 02: message (string) = \"Wait.\",\n"
297 " },\n"
298 " [1] = Bonk {\n"
299 " 01: type (i32) = 2,\n"
300 " 02: message (string) = \"What?\",\n"
301 " },\n"
302 " },\n"
303 " },\n"
304 "}");
305 const std::string result(apache::thrift::ThriftDebugString(*hm));
306
307 BOOST_CHECK_MESSAGE(!expected_result.compare(result),
308 "Expected:\n" << expected_result << "\nGotten:\n" << result);
David Reiss00dcccf2007-07-21 01:18:10 +0000309}