r1275@opsdev009 (orig r69342): mcslee | 2007-11-12 20:00:29 -0800
[amiethrift.git] / test / OptionalRequiredTest.cpp
blob643cbe953c00592e64d29b3a0b9d3bf9cdb4994d
1 /*
2 ../compiler/cpp/thrift -cpp OptionalRequiredTest.thrift
3 g++ -Wall -g -I../lib/cpp/src -I/usr/local/include/boost-1_33_1 \
4 OptionalRequiredTest.cpp gen-cpp/OptionalRequiredTest_types.cpp \
5 ../lib/cpp/.libs/libthrift.a -o OptionalRequiredTest
6 ./OptionalRequiredTest
7 */
9 #include <cassert>
10 #include <map>
11 #include <iostream>
12 #include <protocol/TDebugProtocol.h>
13 #include <protocol/TBinaryProtocol.h>
14 #include <transport/TTransportUtils.h>
15 #include "gen-cpp/OptionalRequiredTest_types.h"
17 using std::cout;
18 using std::endl;
19 using std::map;
20 using std::string;
21 using namespace thrift::test;
22 using namespace facebook::thrift;
23 using namespace facebook::thrift::transport;
24 using namespace facebook::thrift::protocol;
28 template<typename Struct>
29 void trywrite(const Struct& s, bool should_work) {
30 bool worked;
31 try {
32 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
33 s.write(&protocol);
34 worked = true;
35 } catch (TProtocolException & ex) {
36 worked = false;
38 assert(worked == should_work);
42 template <typename Struct1, typename Struct2>
43 void write_to_read(const Struct1 & w, Struct2 & r) {
44 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
45 w.write(&protocol);
46 r.read(&protocol);
50 int main() {
52 cout << "This old school struct should have three fields." << endl;
54 OldSchool o;
55 cout << ThriftDebugString(o) << endl;
57 cout << endl;
59 cout << "Setting a value before setting isset." << endl;
61 Simple s;
62 cout << ThriftDebugString(s) << endl;
63 s.im_optional = 10;
64 cout << ThriftDebugString(s) << endl;
65 s.__isset.im_optional = true;
66 cout << ThriftDebugString(s) << endl;
68 cout << endl;
70 cout << "Setting isset before setting a value." << endl;
72 Simple s;
73 cout << ThriftDebugString(s) << endl;
74 s.__isset.im_optional = true;
75 cout << ThriftDebugString(s) << endl;
76 s.im_optional = 10;
77 cout << ThriftDebugString(s) << endl;
79 cout << endl;
81 // Write-to-read with optional fields.
83 Simple s1, s2, s3;
84 s1.im_optional = 10;
85 assert(!s1.__isset.im_default);
86 //assert(!s1.__isset.im_required); // Compile error.
87 assert(!s1.__isset.im_optional);
89 write_to_read(s1, s2);
91 assert( s2.__isset.im_default);
92 //assert( s2.__isset.im_required); // Compile error.
93 assert(!s2.__isset.im_optional);
94 assert(s3.im_optional == 0);
96 s1.__isset.im_optional = true;
97 write_to_read(s1, s3);
99 assert( s3.__isset.im_default);
100 //assert( s3.__isset.im_required); // Compile error.
101 assert( s3.__isset.im_optional);
102 assert(s3.im_optional == 10);
105 // Writing between optional and default.
107 Tricky1 t1;
108 Tricky2 t2;
110 t2.im_optional = 10;
111 write_to_read(t2, t1);
112 write_to_read(t1, t2);
113 assert(!t1.__isset.im_default);
114 assert( t2.__isset.im_optional);
115 assert(t1.im_default == t2.im_optional);
116 assert(t1.im_default == 0);
119 // Writing between default and required.
121 Tricky1 t1;
122 Tricky3 t3;
123 write_to_read(t1, t3);
124 write_to_read(t3, t1);
125 assert(t1.__isset.im_default);
128 // Writing between optional and required.
130 Tricky2 t2;
131 Tricky3 t3;
132 t2.__isset.im_optional = true;
133 write_to_read(t2, t3);
134 write_to_read(t3, t2);
137 // Mu-hu-ha-ha-ha!
139 Tricky2 t2;
140 Tricky3 t3;
141 try {
142 write_to_read(t2, t3);
143 abort();
145 catch (TProtocolException& ex) {}
147 write_to_read(t3, t2);
148 assert(t2.__isset.im_optional);
151 cout << "Complex struct, simple test." << endl;
153 Complex c;
154 cout << ThriftDebugString(c) << endl;
159 Tricky1 t1;
160 Tricky2 t2;
161 // Compile error.
162 //(void)(t1 == t2);
166 OldSchool o1, o2, o3;
167 assert(o1 == o2);
168 o1.im_int = o2.im_int = 10;
169 assert(o1 == o2);
170 o1.__isset.im_int = true;
171 o2.__isset.im_int = false;
172 assert(o1 == o2);
173 o1.im_int = 20;
174 o1.__isset.im_int = false;
175 assert(o1 != o2);
176 o1.im_int = 10;
177 assert(o1 == o2);
178 o1.im_str = o2.im_str = "foo";
179 assert(o1 == o2);
180 o1.__isset.im_str = o2.__isset.im_str = true;
181 assert(o1 == o2);
182 map<int32_t,string> mymap;
183 mymap[1] = "bar";
184 mymap[2] = "baz";
185 o1.im_big.push_back(map<int32_t,string>());
186 assert(o1 != o2);
187 o2.im_big.push_back(map<int32_t,string>());
188 assert(o1 == o2);
189 o2.im_big.push_back(mymap);
190 assert(o1 != o2);
191 o1.im_big.push_back(mymap);
192 assert(o1 == o2);
194 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
195 o1.write(&protocol);
197 o1.im_big.push_back(mymap);
198 mymap[3] = "qux";
199 o2.im_big.push_back(mymap);
200 assert(o1 != o2);
201 o1.im_big.back()[3] = "qux";
202 assert(o1 == o2);
204 o3.read(&protocol);
205 o3.im_big.push_back(mymap);
206 assert(o1 == o3);
208 //cout << ThriftDebugString(o3) << endl;
212 Tricky2 t1, t2;
213 assert(t1.__isset.im_optional == false);
214 assert(t2.__isset.im_optional == false);
215 assert(t1 == t2);
216 t1.im_optional = 5;
217 assert(t1 == t2);
218 t2.im_optional = 5;
219 assert(t1 == t2);
220 t1.__isset.im_optional = true;
221 assert(t1 != t2);
222 t2.__isset.im_optional = true;
223 assert(t1 == t2);
224 t1.im_optional = 10;
225 assert(t1 != t2);
226 t2.__isset.im_optional = false;
227 assert(t1 != t2);
230 return 0;