blob: a3259c086c9026a5b63e7761d40d36437af2347d [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 Grochowski240120c2014-11-18 11:33:31 +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 Grochowski240120c2014-11-18 11:33:31 +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 Grochowski240120c2014-11-18 11:33:31 +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
Konrad Grochowski240120c2014-11-18 11:33:31 +010047
David Reissdb0ea152008-02-18 01:49:37 +000048 Nesting n;
49 n.my_ooe = ooe;
50 n.my_ooe.integer16 = 16;
51 n.my_ooe.integer32 = 32;
52 n.my_ooe.integer64 = 64;
Konrad Grochowski240120c2014-11-18 11:33:31 +010053 n.my_ooe.double_precision = (std::sqrt(5.0)+1)/2;
54 n.my_ooe.some_characters = ":R (me going \"rrrr\")";
David Reissdb0ea152008-02-18 01:49:37 +000055 n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20"
56 "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e"
57 "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc";
Konrad Grochowski240120c2014-11-18 11:33:31 +010058 n.my_bonk.type = 31337;
David Reissdb0ea152008-02-18 01:49:37 +000059 n.my_bonk.message = "I am a bonk... xor!";
60
T Jake Lucianib5e62212009-01-31 22:36:20 +000061 cout << apache::thrift::ThriftJSONString(n) << endl << endl;
David Reissdb0ea152008-02-18 01:49:37 +000062
Konrad Grochowski240120c2014-11-18 11:33:31 +010063
David Reissdb0ea152008-02-18 01:49:37 +000064 HolyMoley hm;
65
66 hm.big.push_back(ooe);
67 hm.big.push_back(n.my_ooe);
68 hm.big[0].a_bite = 0x22;
69 hm.big[1].a_bite = 0x33;
70
71 std::vector<std::string> stage1;
72 stage1.push_back("and a one");
73 stage1.push_back("and a two");
74 hm.contain.insert(stage1);
75 stage1.clear();
76 stage1.push_back("then a one, two");
77 stage1.push_back("three!");
78 stage1.push_back("FOUR!!");
79 hm.contain.insert(stage1);
80 stage1.clear();
81 hm.contain.insert(stage1);
82
83 std::vector<Bonk> stage2;
84 hm.bonks["nothing"] = stage2;
Konrad Grochowski240120c2014-11-18 11:33:31 +010085 stage2.resize(stage2.size()+1);
David Reissdb0ea152008-02-18 01:49:37 +000086 stage2.back().type = 1;
87 stage2.back().message = "Wait.";
Konrad Grochowski240120c2014-11-18 11:33:31 +010088 stage2.resize(stage2.size()+1);
David Reissdb0ea152008-02-18 01:49:37 +000089 stage2.back().type = 2;
90 stage2.back().message = "What?";
91 hm.bonks["something"] = stage2;
92 stage2.clear();
Konrad Grochowski240120c2014-11-18 11:33:31 +010093 stage2.resize(stage2.size()+1);
David Reissdb0ea152008-02-18 01:49:37 +000094 stage2.back().type = 3;
95 stage2.back().message = "quoth";
Konrad Grochowski240120c2014-11-18 11:33:31 +010096 stage2.resize(stage2.size()+1);
David Reissdb0ea152008-02-18 01:49:37 +000097 stage2.back().type = 4;
98 stage2.back().message = "the raven";
Konrad Grochowski240120c2014-11-18 11:33:31 +010099 stage2.resize(stage2.size()+1);
David Reissdb0ea152008-02-18 01:49:37 +0000100 stage2.back().type = 5;
101 stage2.back().message = "nevermore";
102 hm.bonks["poe"] = stage2;
103
T Jake Lucianib5e62212009-01-31 22:36:20 +0000104 cout << apache::thrift::ThriftJSONString(hm) << endl << endl;
David Reissdb0ea152008-02-18 01:49:37 +0000105
106 boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
107 boost::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
108
Konrad Grochowski240120c2014-11-18 11:33:31 +0100109
David Reissdb0ea152008-02-18 01:49:37 +0000110 cout << "Testing ooe" << endl;
111
112 ooe.write(proto.get());
113 OneOfEach ooe2;
114 ooe2.read(proto.get());
115
116 assert(ooe == ooe2);
117
Konrad Grochowski240120c2014-11-18 11:33:31 +0100118
David Reissdb0ea152008-02-18 01:49:37 +0000119 cout << "Testing hm" << endl;
120
121 hm.write(proto.get());
122 HolyMoley hm2;
123 hm2.read(proto.get());
124
125 assert(hm == hm2);
126
Christian Lavoiecbf87cb2010-11-28 14:34:26 +0000127 hm2.big[0].a_bite = 0x00;
David Reiss4a8f46c2008-03-07 20:12:07 +0000128
129 assert(hm != hm2);
130
David Reissdb0ea152008-02-18 01:49:37 +0000131 Doubles dub;
Konrad Grochowski240120c2014-11-18 11:33:31 +0100132 dub.nan = HUGE_VAL/HUGE_VAL;
David Reissdb0ea152008-02-18 01:49:37 +0000133 dub.inf = HUGE_VAL;
134 dub.neginf = -HUGE_VAL;
Konrad Grochowski240120c2014-11-18 11:33:31 +0100135 dub.repeating = 10.0/3.0;
David Reissdb0ea152008-02-18 01:49:37 +0000136 dub.big = 1E+305;
Roger Meier22888ce2014-02-09 11:31:02 +0100137 dub.tiny = 1E-305;
David Reissdb0ea152008-02-18 01:49:37 +0000138 dub.zero = 0.0;
139 dub.negzero = -0.0;
T Jake Lucianib5e62212009-01-31 22:36:20 +0000140 cout << apache::thrift::ThriftJSONString(dub) << endl << endl;
David Reissdb0ea152008-02-18 01:49:37 +0000141
David Reiss4a8f46c2008-03-07 20:12:07 +0000142 cout << "Testing base" << endl;
143
144 Base64 base;
145 base.a = 123;
146 base.b1 = "1";
147 base.b2 = "12";
148 base.b3 = "123";
149 base.b4 = "1234";
150 base.b5 = "12345";
151 base.b6 = "123456";
152
153 base.write(proto.get());
154 Base64 base2;
155 base2.read(proto.get());
156
157 assert(base == base2);
158
David Reissdb0ea152008-02-18 01:49:37 +0000159 return 0;
160}