blob: 79fa697d7fee31f2e97470b92c65fecbb8ebaec4 [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.
Todd Lipcon53ae9f32009-12-07 00:42:38 +000018 *
19 * Contains some contributions under the Thrift Software License.
20 * Please see doc/old-thrift-license.txt in the Thrift distribution for
21 * details.
David Reissea2cba82009-03-30 21:35:00 +000022 */
23
David Reiss8320a922007-08-14 19:59:26 +000024#include <cassert>
25#include <map>
26#include <iostream>
Roger Meier49ff8b12012-04-13 09:12:31 +000027#include <thrift/protocol/TDebugProtocol.h>
28#include <thrift/protocol/TBinaryProtocol.h>
29#include <thrift/transport/TBufferTransports.h>
David Reiss8320a922007-08-14 19:59:26 +000030#include "gen-cpp/OptionalRequiredTest_types.h"
31
32using std::cout;
33using std::endl;
34using std::map;
35using std::string;
36using namespace thrift::test;
T Jake Lucianib5e62212009-01-31 22:36:20 +000037using namespace apache::thrift;
38using namespace apache::thrift::transport;
39using namespace apache::thrift::protocol;
David Reiss8320a922007-08-14 19:59:26 +000040
David Reiss8320a922007-08-14 19:59:26 +000041/*
42template<typename Struct>
43void trywrite(const Struct& s, bool should_work) {
44 bool worked;
45 try {
46 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
47 s.write(&protocol);
48 worked = true;
49 } catch (TProtocolException & ex) {
50 worked = false;
51 }
52 assert(worked == should_work);
53}
54*/
55
56template <typename Struct1, typename Struct2>
Konrad Grochowski16a23a62014-11-13 15:33:38 +010057void write_to_read(const Struct1& w, Struct2& r) {
David Reiss8320a922007-08-14 19:59:26 +000058 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
59 w.write(&protocol);
60 r.read(&protocol);
61}
62
David Reiss8320a922007-08-14 19:59:26 +000063int main() {
64
65 cout << "This old school struct should have three fields." << endl;
66 {
67 OldSchool o;
68 cout << ThriftDebugString(o) << endl;
69 }
70 cout << endl;
71
72 cout << "Setting a value before setting isset." << endl;
73 {
74 Simple s;
75 cout << ThriftDebugString(s) << endl;
76 s.im_optional = 10;
77 cout << ThriftDebugString(s) << endl;
78 s.__isset.im_optional = true;
79 cout << ThriftDebugString(s) << endl;
80 }
81 cout << endl;
82
83 cout << "Setting isset before setting a value." << endl;
84 {
85 Simple s;
86 cout << ThriftDebugString(s) << endl;
87 s.__isset.im_optional = true;
88 cout << ThriftDebugString(s) << endl;
89 s.im_optional = 10;
90 cout << ThriftDebugString(s) << endl;
91 }
92 cout << endl;
93
94 // Write-to-read with optional fields.
95 {
96 Simple s1, s2, s3;
97 s1.im_optional = 10;
98 assert(!s1.__isset.im_default);
Konrad Grochowski16a23a62014-11-13 15:33:38 +010099 // assert(!s1.__isset.im_required); // Compile error.
David Reiss8320a922007-08-14 19:59:26 +0000100 assert(!s1.__isset.im_optional);
101
102 write_to_read(s1, s2);
103
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100104 assert(s2.__isset.im_default);
105 // assert( s2.__isset.im_required); // Compile error.
David Reiss8320a922007-08-14 19:59:26 +0000106 assert(!s2.__isset.im_optional);
107 assert(s3.im_optional == 0);
108
109 s1.__isset.im_optional = true;
110 write_to_read(s1, s3);
111
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100112 assert(s3.__isset.im_default);
113 // assert( s3.__isset.im_required); // Compile error.
114 assert(s3.__isset.im_optional);
David Reiss8320a922007-08-14 19:59:26 +0000115 assert(s3.im_optional == 10);
116 }
117
118 // Writing between optional and default.
119 {
120 Tricky1 t1;
121 Tricky2 t2;
122
123 t2.im_optional = 10;
124 write_to_read(t2, t1);
125 write_to_read(t1, t2);
126 assert(!t1.__isset.im_default);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100127 assert(t2.__isset.im_optional);
David Reiss8320a922007-08-14 19:59:26 +0000128 assert(t1.im_default == t2.im_optional);
129 assert(t1.im_default == 0);
130 }
131
132 // Writing between default and required.
133 {
134 Tricky1 t1;
135 Tricky3 t3;
136 write_to_read(t1, t3);
137 write_to_read(t3, t1);
138 assert(t1.__isset.im_default);
139 }
140
141 // Writing between optional and required.
142 {
143 Tricky2 t2;
144 Tricky3 t3;
145 t2.__isset.im_optional = true;
146 write_to_read(t2, t3);
147 write_to_read(t3, t2);
148 }
149
150 // Mu-hu-ha-ha-ha!
151 {
152 Tricky2 t2;
153 Tricky3 t3;
154 try {
155 write_to_read(t2, t3);
156 abort();
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100157 } catch (const TProtocolException&) {
David Reiss8320a922007-08-14 19:59:26 +0000158 }
David Reiss8320a922007-08-14 19:59:26 +0000159
160 write_to_read(t3, t2);
161 assert(t2.__isset.im_optional);
162 }
163
164 cout << "Complex struct, simple test." << endl;
165 {
166 Complex c;
167 cout << ThriftDebugString(c) << endl;
168 }
169
David Reiss8320a922007-08-14 19:59:26 +0000170 {
171 Tricky1 t1;
172 Tricky2 t2;
173 // Compile error.
174 //(void)(t1 == t2);
175 }
176
177 {
178 OldSchool o1, o2, o3;
179 assert(o1 == o2);
180 o1.im_int = o2.im_int = 10;
181 assert(o1 == o2);
182 o1.__isset.im_int = true;
183 o2.__isset.im_int = false;
184 assert(o1 == o2);
185 o1.im_int = 20;
186 o1.__isset.im_int = false;
187 assert(o1 != o2);
188 o1.im_int = 10;
189 assert(o1 == o2);
190 o1.im_str = o2.im_str = "foo";
191 assert(o1 == o2);
192 o1.__isset.im_str = o2.__isset.im_str = true;
193 assert(o1 == o2);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100194 map<int32_t, string> mymap;
David Reiss8320a922007-08-14 19:59:26 +0000195 mymap[1] = "bar";
196 mymap[2] = "baz";
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100197 o1.im_big.push_back(map<int32_t, string>());
David Reiss8320a922007-08-14 19:59:26 +0000198 assert(o1 != o2);
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100199 o2.im_big.push_back(map<int32_t, string>());
David Reiss8320a922007-08-14 19:59:26 +0000200 assert(o1 == o2);
201 o2.im_big.push_back(mymap);
202 assert(o1 != o2);
203 o1.im_big.push_back(mymap);
204 assert(o1 == o2);
205
206 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
207 o1.write(&protocol);
208
209 o1.im_big.push_back(mymap);
210 mymap[3] = "qux";
211 o2.im_big.push_back(mymap);
212 assert(o1 != o2);
213 o1.im_big.back()[3] = "qux";
214 assert(o1 == o2);
215
216 o3.read(&protocol);
217 o3.im_big.push_back(mymap);
218 assert(o1 == o3);
219
Konrad Grochowski16a23a62014-11-13 15:33:38 +0100220 // cout << ThriftDebugString(o3) << endl;
David Reiss8320a922007-08-14 19:59:26 +0000221 }
222
223 {
224 Tricky2 t1, t2;
225 assert(t1.__isset.im_optional == false);
226 assert(t2.__isset.im_optional == false);
227 assert(t1 == t2);
228 t1.im_optional = 5;
229 assert(t1 == t2);
230 t2.im_optional = 5;
231 assert(t1 == t2);
232 t1.__isset.im_optional = true;
233 assert(t1 != t2);
234 t2.__isset.im_optional = true;
235 assert(t1 == t2);
236 t1.im_optional = 10;
237 assert(t1 != t2);
238 t2.__isset.im_optional = false;
239 assert(t1 != t2);
240 }
241
Jake Farrellf9f01fa2012-01-27 04:48:26 +0000242 {
243 OptionalDefault t1, t2;
244 cout << ThriftDebugString(t1) << endl;
245 assert(t1.__isset.opt_int == true);
246 assert(t1.__isset.opt_str == true);
247 assert(t1.opt_int == t2.opt_int);
248 assert(t1.opt_str == t2.opt_str);
249
250 write_to_read(t1, t2);
251 cout << ThriftDebugString(t2) << endl;
252 assert(t2.__isset.opt_int == true);
253 assert(t2.__isset.opt_str == true);
254 assert(t1.opt_int == t2.opt_int);
255 assert(t1.opt_str == t2.opt_str);
256 }
257
David Reiss8320a922007-08-14 19:59:26 +0000258 return 0;
259}