blob: aa07f93acf899bb0a1a2fc1e78ceb226753d661b [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 Reissdb0ea152008-02-18 01:49:37 +000021#include <iostream>
22#include <cmath>
Roger Meier49ff8b12012-04-13 09:12:31 +000023#include <thrift/transport/TBufferTransports.h>
24#include <thrift/protocol/TJSONProtocol.h>
David Reissdb0ea152008-02-18 01:49:37 +000025#include "gen-cpp/DebugProtoTest_types.h"
26
27int main() {
28 using std::cout;
29 using std::endl;
David Reiss2a4bfd62008-04-07 23:45:00 +000030 using namespace thrift::test::debug;
T Jake Lucianib5e62212009-01-31 22:36:20 +000031 using apache::thrift::transport::TMemoryBuffer;
32 using apache::thrift::protocol::TJSONProtocol;
David Reissdb0ea152008-02-18 01:49:37 +000033
34 OneOfEach ooe;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010035 ooe.im_true = true;
36 ooe.im_false = false;
37 ooe.a_bite = 0x7f;
David Reissdb0ea152008-02-18 01:49:37 +000038 ooe.integer16 = 27000;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010039 ooe.integer32 = 1 << 24;
David Reissdb0ea152008-02-18 01:49:37 +000040 ooe.integer64 = (uint64_t)6000 * 1000 * 1000;
41 ooe.double_precision = M_PI;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010042 ooe.some_characters = "JSON THIS! \"\1";
43 ooe.zomg_unicode = "\xd7\n\a\t";
David Reissdb0ea152008-02-18 01:49:37 +000044 ooe.base64 = "\1\2\3\255";
T Jake Lucianib5e62212009-01-31 22:36:20 +000045 cout << apache::thrift::ThriftJSONString(ooe) << endl << endl;
David Reissdb0ea152008-02-18 01:49:37 +000046
David Reissdb0ea152008-02-18 01:49:37 +000047 Nesting n;
48 n.my_ooe = ooe;
49 n.my_ooe.integer16 = 16;
50 n.my_ooe.integer32 = 32;
51 n.my_ooe.integer64 = 64;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010052 n.my_ooe.double_precision = (std::sqrt(5.0) + 1) / 2;
53 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
David Reissdb0ea152008-02-18 01:49:37 +000054 n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"
55 "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"
56 "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc";
Konrad Grochowski16a23a62014-11-13 15:33:38 +010057 n.my_bonk.type = 31337;
David Reissdb0ea152008-02-18 01:49:37 +000058 n.my_bonk.message = "I am a bonk... xor!";
59
T Jake Lucianib5e62212009-01-31 22:36:20 +000060 cout << apache::thrift::ThriftJSONString(n) << endl << endl;
David Reissdb0ea152008-02-18 01:49:37 +000061
David Reissdb0ea152008-02-18 01:49:37 +000062 HolyMoley hm;
63
64 hm.big.push_back(ooe);
65 hm.big.push_back(n.my_ooe);
66 hm.big[0].a_bite = 0x22;
67 hm.big[1].a_bite = 0x33;
68
69 std::vector<std::string> stage1;
70 stage1.push_back("and a one");
71 stage1.push_back("and a two");
72 hm.contain.insert(stage1);
73 stage1.clear();
74 stage1.push_back("then a one, two");
75 stage1.push_back("three!");
76 stage1.push_back("FOUR!!");
77 hm.contain.insert(stage1);
78 stage1.clear();
79 hm.contain.insert(stage1);
80
81 std::vector<Bonk> stage2;
82 hm.bonks["nothing"] = stage2;
Konrad Grochowski16a23a62014-11-13 15:33:38 +010083 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +000084 stage2.back().type = 1;
85 stage2.back().message = "Wait.";
Konrad Grochowski16a23a62014-11-13 15:33:38 +010086 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +000087 stage2.back().type = 2;
88 stage2.back().message = "What?";
89 hm.bonks["something"] = stage2;
90 stage2.clear();
Konrad Grochowski16a23a62014-11-13 15:33:38 +010091 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +000092 stage2.back().type = 3;
93 stage2.back().message = "quoth";
Konrad Grochowski16a23a62014-11-13 15:33:38 +010094 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +000095 stage2.back().type = 4;
96 stage2.back().message = "the raven";
Konrad Grochowski16a23a62014-11-13 15:33:38 +010097 stage2.resize(stage2.size() + 1);
David Reissdb0ea152008-02-18 01:49:37 +000098 stage2.back().type = 5;
99 stage2.back().message = "nevermore";
100 hm.bonks["poe"] = stage2;
101
T Jake Lucianib5e62212009-01-31 22:36:20 +0000102 cout << apache::thrift::ThriftJSONString(hm) << endl << endl;
David Reissdb0ea152008-02-18 01:49:37 +0000103
104 boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
105 boost::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
106
David Reissdb0ea152008-02-18 01:49:37 +0000107 cout << "Testing ooe" << endl;
108
109 ooe.write(proto.get());
110 OneOfEach ooe2;
111 ooe2.read(proto.get());
112
113 assert(ooe == ooe2);
114
David Reissdb0ea152008-02-18 01:49:37 +0000115 cout << "Testing hm" << endl;
116
117 hm.write(proto.get());
118 HolyMoley hm2;
119 hm2.read(proto.get());
120
121 assert(hm == hm2);
122
Christian Lavoiecbf87cb2010-11-28 14:34:26 +0000123 hm2.big[0].a_bite = 0x00;
David Reiss4a8f46c2008-03-07 20:12:07 +0000124
125 assert(hm != hm2);
126
David Reissdb0ea152008-02-18 01:49:37 +0000127 Doubles dub;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100128 dub.nan = HUGE_VAL / HUGE_VAL;
David Reissdb0ea152008-02-18 01:49:37 +0000129 dub.inf = HUGE_VAL;
130 dub.neginf = -HUGE_VAL;
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100131 dub.repeating = 10.0 / 3.0;
David Reissdb0ea152008-02-18 01:49:37 +0000132 dub.big = 1E+305;
Roger Meier22888ce2014-02-09 11:31:02 +0100133 dub.tiny = 1E-305;
David Reissdb0ea152008-02-18 01:49:37 +0000134 dub.zero = 0.0;
135 dub.negzero = -0.0;
T Jake Lucianib5e62212009-01-31 22:36:20 +0000136 cout << apache::thrift::ThriftJSONString(dub) << endl << endl;
David Reissdb0ea152008-02-18 01:49:37 +0000137
David Reiss4a8f46c2008-03-07 20:12:07 +0000138 cout << "Testing base" << endl;
139
140 Base64 base;
141 base.a = 123;
142 base.b1 = "1";
143 base.b2 = "12";
144 base.b3 = "123";
145 base.b4 = "1234";
146 base.b5 = "12345";
147 base.b6 = "123456";
148
149 base.write(proto.get());
150 Base64 base2;
151 base2.read(proto.get());
152
153 assert(base == base2);
154
David Reissdb0ea152008-02-18 01:49:37 +0000155 return 0;
156}