| 30 |
ashish |
1 |
/*
|
|
|
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 |
|
|
|
20 |
#include <iostream>
|
|
|
21 |
#include <cmath>
|
|
|
22 |
#include <transport/TBufferTransports.h>
|
|
|
23 |
#include <protocol/TJSONProtocol.h>
|
|
|
24 |
#include "gen-cpp/DebugProtoTest_types.h"
|
|
|
25 |
|
|
|
26 |
int main() {
|
|
|
27 |
using std::cout;
|
|
|
28 |
using std::endl;
|
|
|
29 |
using namespace thrift::test::debug;
|
|
|
30 |
using apache::thrift::transport::TMemoryBuffer;
|
|
|
31 |
using apache::thrift::protocol::TJSONProtocol;
|
|
|
32 |
|
|
|
33 |
OneOfEach ooe;
|
|
|
34 |
ooe.im_true = true;
|
|
|
35 |
ooe.im_false = false;
|
|
|
36 |
ooe.a_bite = 0xd6;
|
|
|
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 = "JSON THIS! \"\1";
|
|
|
42 |
ooe.zomg_unicode = "\xd7\n\a\t";
|
|
|
43 |
ooe.base64 = "\1\2\3\255";
|
|
|
44 |
cout << apache::thrift::ThriftJSONString(ooe) << endl << endl;
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
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;
|
|
|
52 |
n.my_ooe.double_precision = (std::sqrt(5.0)+1)/2;
|
|
|
53 |
n.my_ooe.some_characters = ":R (me going \"rrrr\")";
|
|
|
54 |
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";
|
|
|
57 |
n.my_bonk.type = 31337;
|
|
|
58 |
n.my_bonk.message = "I am a bonk... xor!";
|
|
|
59 |
|
|
|
60 |
cout << apache::thrift::ThriftJSONString(n) << endl << endl;
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
HolyMoley hm;
|
|
|
64 |
|
|
|
65 |
hm.big.push_back(ooe);
|
|
|
66 |
hm.big.push_back(n.my_ooe);
|
|
|
67 |
hm.big[0].a_bite = 0x22;
|
|
|
68 |
hm.big[1].a_bite = 0x33;
|
|
|
69 |
|
|
|
70 |
std::vector<std::string> stage1;
|
|
|
71 |
stage1.push_back("and a one");
|
|
|
72 |
stage1.push_back("and a two");
|
|
|
73 |
hm.contain.insert(stage1);
|
|
|
74 |
stage1.clear();
|
|
|
75 |
stage1.push_back("then a one, two");
|
|
|
76 |
stage1.push_back("three!");
|
|
|
77 |
stage1.push_back("FOUR!!");
|
|
|
78 |
hm.contain.insert(stage1);
|
|
|
79 |
stage1.clear();
|
|
|
80 |
hm.contain.insert(stage1);
|
|
|
81 |
|
|
|
82 |
std::vector<Bonk> stage2;
|
|
|
83 |
hm.bonks["nothing"] = stage2;
|
|
|
84 |
stage2.resize(stage2.size()+1);
|
|
|
85 |
stage2.back().type = 1;
|
|
|
86 |
stage2.back().message = "Wait.";
|
|
|
87 |
stage2.resize(stage2.size()+1);
|
|
|
88 |
stage2.back().type = 2;
|
|
|
89 |
stage2.back().message = "What?";
|
|
|
90 |
hm.bonks["something"] = stage2;
|
|
|
91 |
stage2.clear();
|
|
|
92 |
stage2.resize(stage2.size()+1);
|
|
|
93 |
stage2.back().type = 3;
|
|
|
94 |
stage2.back().message = "quoth";
|
|
|
95 |
stage2.resize(stage2.size()+1);
|
|
|
96 |
stage2.back().type = 4;
|
|
|
97 |
stage2.back().message = "the raven";
|
|
|
98 |
stage2.resize(stage2.size()+1);
|
|
|
99 |
stage2.back().type = 5;
|
|
|
100 |
stage2.back().message = "nevermore";
|
|
|
101 |
hm.bonks["poe"] = stage2;
|
|
|
102 |
|
|
|
103 |
cout << apache::thrift::ThriftJSONString(hm) << endl << endl;
|
|
|
104 |
|
|
|
105 |
boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer());
|
|
|
106 |
boost::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer));
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
cout << "Testing ooe" << endl;
|
|
|
110 |
|
|
|
111 |
ooe.write(proto.get());
|
|
|
112 |
OneOfEach ooe2;
|
|
|
113 |
ooe2.read(proto.get());
|
|
|
114 |
|
|
|
115 |
assert(ooe == ooe2);
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
cout << "Testing hm" << endl;
|
|
|
119 |
|
|
|
120 |
hm.write(proto.get());
|
|
|
121 |
HolyMoley hm2;
|
|
|
122 |
hm2.read(proto.get());
|
|
|
123 |
|
|
|
124 |
assert(hm == hm2);
|
|
|
125 |
|
|
|
126 |
hm2.big[0].a_bite = 0xFF;
|
|
|
127 |
|
|
|
128 |
assert(hm != hm2);
|
|
|
129 |
|
|
|
130 |
Doubles dub;
|
|
|
131 |
dub.nan = HUGE_VAL/HUGE_VAL;
|
|
|
132 |
dub.inf = HUGE_VAL;
|
|
|
133 |
dub.neginf = -HUGE_VAL;
|
|
|
134 |
dub.repeating = 10.0/3.0;
|
|
|
135 |
dub.big = 1E+305;
|
|
|
136 |
dub.small = 1E-305;
|
|
|
137 |
dub.zero = 0.0;
|
|
|
138 |
dub.negzero = -0.0;
|
|
|
139 |
cout << apache::thrift::ThriftJSONString(dub) << endl << endl;
|
|
|
140 |
|
|
|
141 |
cout << "Testing base" << endl;
|
|
|
142 |
|
|
|
143 |
Base64 base;
|
|
|
144 |
base.a = 123;
|
|
|
145 |
base.b1 = "1";
|
|
|
146 |
base.b2 = "12";
|
|
|
147 |
base.b3 = "123";
|
|
|
148 |
base.b4 = "1234";
|
|
|
149 |
base.b5 = "12345";
|
|
|
150 |
base.b6 = "123456";
|
|
|
151 |
|
|
|
152 |
base.write(proto.get());
|
|
|
153 |
Base64 base2;
|
|
|
154 |
base2.read(proto.get());
|
|
|
155 |
|
|
|
156 |
assert(base == base2);
|
|
|
157 |
|
|
|
158 |
return 0;
|
|
|
159 |
}
|