blob: c3fbfd1c39bc45843372bed971759f9d930d8848 [file] [log] [blame]
David Reiss8320a922007-08-14 19:59:26 +00001#include <cassert>
2#include <map>
3#include <iostream>
4#include <protocol/TDebugProtocol.h>
5#include <protocol/TBinaryProtocol.h>
David Reiss28f298d2008-05-01 06:17:36 +00006#include <transport/TBufferTransports.h>
David Reiss8320a922007-08-14 19:59:26 +00007#include "gen-cpp/OptionalRequiredTest_types.h"
8
9using std::cout;
10using std::endl;
11using std::map;
12using std::string;
13using namespace thrift::test;
14using namespace facebook::thrift;
15using namespace facebook::thrift::transport;
16using namespace facebook::thrift::protocol;
17
18
19/*
20template<typename Struct>
21void trywrite(const Struct& s, bool should_work) {
22 bool worked;
23 try {
24 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
25 s.write(&protocol);
26 worked = true;
27 } catch (TProtocolException & ex) {
28 worked = false;
29 }
30 assert(worked == should_work);
31}
32*/
33
34template <typename Struct1, typename Struct2>
35void write_to_read(const Struct1 & w, Struct2 & r) {
36 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
37 w.write(&protocol);
38 r.read(&protocol);
39}
40
41
42int main() {
43
44 cout << "This old school struct should have three fields." << endl;
45 {
46 OldSchool o;
47 cout << ThriftDebugString(o) << endl;
48 }
49 cout << endl;
50
51 cout << "Setting a value before setting isset." << endl;
52 {
53 Simple s;
54 cout << ThriftDebugString(s) << endl;
55 s.im_optional = 10;
56 cout << ThriftDebugString(s) << endl;
57 s.__isset.im_optional = true;
58 cout << ThriftDebugString(s) << endl;
59 }
60 cout << endl;
61
62 cout << "Setting isset before setting a value." << endl;
63 {
64 Simple s;
65 cout << ThriftDebugString(s) << endl;
66 s.__isset.im_optional = true;
67 cout << ThriftDebugString(s) << endl;
68 s.im_optional = 10;
69 cout << ThriftDebugString(s) << endl;
70 }
71 cout << endl;
72
73 // Write-to-read with optional fields.
74 {
75 Simple s1, s2, s3;
76 s1.im_optional = 10;
77 assert(!s1.__isset.im_default);
78 //assert(!s1.__isset.im_required); // Compile error.
79 assert(!s1.__isset.im_optional);
80
81 write_to_read(s1, s2);
82
83 assert( s2.__isset.im_default);
84 //assert( s2.__isset.im_required); // Compile error.
85 assert(!s2.__isset.im_optional);
86 assert(s3.im_optional == 0);
87
88 s1.__isset.im_optional = true;
89 write_to_read(s1, s3);
90
91 assert( s3.__isset.im_default);
92 //assert( s3.__isset.im_required); // Compile error.
93 assert( s3.__isset.im_optional);
94 assert(s3.im_optional == 10);
95 }
96
97 // Writing between optional and default.
98 {
99 Tricky1 t1;
100 Tricky2 t2;
101
102 t2.im_optional = 10;
103 write_to_read(t2, t1);
104 write_to_read(t1, t2);
105 assert(!t1.__isset.im_default);
106 assert( t2.__isset.im_optional);
107 assert(t1.im_default == t2.im_optional);
108 assert(t1.im_default == 0);
109 }
110
111 // Writing between default and required.
112 {
113 Tricky1 t1;
114 Tricky3 t3;
115 write_to_read(t1, t3);
116 write_to_read(t3, t1);
117 assert(t1.__isset.im_default);
118 }
119
120 // Writing between optional and required.
121 {
122 Tricky2 t2;
123 Tricky3 t3;
124 t2.__isset.im_optional = true;
125 write_to_read(t2, t3);
126 write_to_read(t3, t2);
127 }
128
129 // Mu-hu-ha-ha-ha!
130 {
131 Tricky2 t2;
132 Tricky3 t3;
133 try {
134 write_to_read(t2, t3);
135 abort();
136 }
137 catch (TProtocolException& ex) {}
138
139 write_to_read(t3, t2);
140 assert(t2.__isset.im_optional);
141 }
142
143 cout << "Complex struct, simple test." << endl;
144 {
145 Complex c;
146 cout << ThriftDebugString(c) << endl;
147 }
148
149
150 {
151 Tricky1 t1;
152 Tricky2 t2;
153 // Compile error.
154 //(void)(t1 == t2);
155 }
156
157 {
158 OldSchool o1, o2, o3;
159 assert(o1 == o2);
160 o1.im_int = o2.im_int = 10;
161 assert(o1 == o2);
162 o1.__isset.im_int = true;
163 o2.__isset.im_int = false;
164 assert(o1 == o2);
165 o1.im_int = 20;
166 o1.__isset.im_int = false;
167 assert(o1 != o2);
168 o1.im_int = 10;
169 assert(o1 == o2);
170 o1.im_str = o2.im_str = "foo";
171 assert(o1 == o2);
172 o1.__isset.im_str = o2.__isset.im_str = true;
173 assert(o1 == o2);
174 map<int32_t,string> mymap;
175 mymap[1] = "bar";
176 mymap[2] = "baz";
177 o1.im_big.push_back(map<int32_t,string>());
178 assert(o1 != o2);
179 o2.im_big.push_back(map<int32_t,string>());
180 assert(o1 == o2);
181 o2.im_big.push_back(mymap);
182 assert(o1 != o2);
183 o1.im_big.push_back(mymap);
184 assert(o1 == o2);
185
186 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
187 o1.write(&protocol);
188
189 o1.im_big.push_back(mymap);
190 mymap[3] = "qux";
191 o2.im_big.push_back(mymap);
192 assert(o1 != o2);
193 o1.im_big.back()[3] = "qux";
194 assert(o1 == o2);
195
196 o3.read(&protocol);
197 o3.im_big.push_back(mymap);
198 assert(o1 == o3);
199
200 //cout << ThriftDebugString(o3) << endl;
201 }
202
203 {
204 Tricky2 t1, t2;
205 assert(t1.__isset.im_optional == false);
206 assert(t2.__isset.im_optional == false);
207 assert(t1 == t2);
208 t1.im_optional = 5;
209 assert(t1 == t2);
210 t2.im_optional = 5;
211 assert(t1 == t2);
212 t1.__isset.im_optional = true;
213 assert(t1 != t2);
214 t2.__isset.im_optional = true;
215 assert(t1 == t2);
216 t1.im_optional = 10;
217 assert(t1 != t2);
218 t2.__isset.im_optional = false;
219 assert(t1 != t2);
220 }
221
222 return 0;
223}