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
12 #include <protocol/TDebugProtocol.h>
13 #include <protocol/TBinaryProtocol.h>
14 #include <transport/TTransportUtils.h>
15 #include "gen-cpp/OptionalRequiredTest_types.h"
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) {
32 TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
35 } catch (TProtocolException & ex) {
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
));
52 cout
<< "This old school struct should have three fields." << endl
;
55 cout
<< ThriftDebugString(o
) << endl
;
59 cout
<< "Setting a value before setting isset." << endl
;
62 cout
<< ThriftDebugString(s
) << endl
;
64 cout
<< ThriftDebugString(s
) << endl
;
65 s
.__isset
.im_optional
= true;
66 cout
<< ThriftDebugString(s
) << endl
;
70 cout
<< "Setting isset before setting a value." << endl
;
73 cout
<< ThriftDebugString(s
) << endl
;
74 s
.__isset
.im_optional
= true;
75 cout
<< ThriftDebugString(s
) << endl
;
77 cout
<< ThriftDebugString(s
) << endl
;
81 // Write-to-read with optional fields.
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.
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.
123 write_to_read(t1
, t3
);
124 write_to_read(t3
, t1
);
125 assert(t1
.__isset
.im_default
);
128 // Writing between optional and required.
132 t2
.__isset
.im_optional
= true;
133 write_to_read(t2
, t3
);
134 write_to_read(t3
, t2
);
142 write_to_read(t2
, t3
);
145 catch (TProtocolException
& ex
) {}
147 write_to_read(t3
, t2
);
148 assert(t2
.__isset
.im_optional
);
151 cout
<< "Complex struct, simple test." << endl
;
154 cout
<< ThriftDebugString(c
) << endl
;
166 OldSchool o1
, o2
, o3
;
168 o1
.im_int
= o2
.im_int
= 10;
170 o1
.__isset
.im_int
= true;
171 o2
.__isset
.im_int
= false;
174 o1
.__isset
.im_int
= false;
178 o1
.im_str
= o2
.im_str
= "foo";
180 o1
.__isset
.im_str
= o2
.__isset
.im_str
= true;
182 map
<int32_t,string
> mymap
;
185 o1
.im_big
.push_back(map
<int32_t,string
>());
187 o2
.im_big
.push_back(map
<int32_t,string
>());
189 o2
.im_big
.push_back(mymap
);
191 o1
.im_big
.push_back(mymap
);
194 TBinaryProtocol
protocol(boost::shared_ptr
<TTransport
>(new TMemoryBuffer
));
197 o1
.im_big
.push_back(mymap
);
199 o2
.im_big
.push_back(mymap
);
201 o1
.im_big
.back()[3] = "qux";
205 o3
.im_big
.push_back(mymap
);
208 //cout << ThriftDebugString(o3) << endl;
213 assert(t1
.__isset
.im_optional
== false);
214 assert(t2
.__isset
.im_optional
== false);
220 t1
.__isset
.im_optional
= true;
222 t2
.__isset
.im_optional
= true;
226 t2
.__isset
.im_optional
= false;