David Reiss | 8320a92 | 2007-08-14 19:59:26 +0000 | [diff] [blame^] | 1 | /* |
| 2 | ../compiler/cpp/thrift -cpp OptionalRequiredTest.thrift |
| 3 | g++ -Wall -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 | */ |
| 8 | |
| 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" |
| 16 | |
| 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; |
| 25 | |
| 26 | |
| 27 | /* |
| 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; |
| 37 | } |
| 38 | assert(worked == should_work); |
| 39 | } |
| 40 | */ |
| 41 | |
| 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); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | int main() { |
| 51 | |
| 52 | cout << "This old school struct should have three fields." << endl; |
| 53 | { |
| 54 | OldSchool o; |
| 55 | cout << ThriftDebugString(o) << endl; |
| 56 | } |
| 57 | cout << endl; |
| 58 | |
| 59 | cout << "Setting a value before setting isset." << endl; |
| 60 | { |
| 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; |
| 67 | } |
| 68 | cout << endl; |
| 69 | |
| 70 | cout << "Setting isset before setting a value." << endl; |
| 71 | { |
| 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; |
| 78 | } |
| 79 | cout << endl; |
| 80 | |
| 81 | // Write-to-read with optional fields. |
| 82 | { |
| 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); |
| 88 | |
| 89 | write_to_read(s1, s2); |
| 90 | |
| 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); |
| 95 | |
| 96 | s1.__isset.im_optional = true; |
| 97 | write_to_read(s1, s3); |
| 98 | |
| 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); |
| 103 | } |
| 104 | |
| 105 | // Writing between optional and default. |
| 106 | { |
| 107 | Tricky1 t1; |
| 108 | Tricky2 t2; |
| 109 | |
| 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); |
| 117 | } |
| 118 | |
| 119 | // Writing between default and required. |
| 120 | { |
| 121 | Tricky1 t1; |
| 122 | Tricky3 t3; |
| 123 | write_to_read(t1, t3); |
| 124 | write_to_read(t3, t1); |
| 125 | assert(t1.__isset.im_default); |
| 126 | } |
| 127 | |
| 128 | // Writing between optional and required. |
| 129 | { |
| 130 | Tricky2 t2; |
| 131 | Tricky3 t3; |
| 132 | t2.__isset.im_optional = true; |
| 133 | write_to_read(t2, t3); |
| 134 | write_to_read(t3, t2); |
| 135 | } |
| 136 | |
| 137 | // Mu-hu-ha-ha-ha! |
| 138 | { |
| 139 | Tricky2 t2; |
| 140 | Tricky3 t3; |
| 141 | try { |
| 142 | write_to_read(t2, t3); |
| 143 | abort(); |
| 144 | } |
| 145 | catch (TProtocolException& ex) {} |
| 146 | |
| 147 | write_to_read(t3, t2); |
| 148 | assert(t2.__isset.im_optional); |
| 149 | } |
| 150 | |
| 151 | cout << "Complex struct, simple test." << endl; |
| 152 | { |
| 153 | Complex c; |
| 154 | cout << ThriftDebugString(c) << endl; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | { |
| 159 | Tricky1 t1; |
| 160 | Tricky2 t2; |
| 161 | // Compile error. |
| 162 | //(void)(t1 == t2); |
| 163 | } |
| 164 | |
| 165 | { |
| 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); |
| 193 | |
| 194 | TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer)); |
| 195 | o1.write(&protocol); |
| 196 | |
| 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); |
| 203 | |
| 204 | o3.read(&protocol); |
| 205 | o3.im_big.push_back(mymap); |
| 206 | assert(o1 == o3); |
| 207 | |
| 208 | //cout << ThriftDebugString(o3) << endl; |
| 209 | } |
| 210 | |
| 211 | { |
| 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); |
| 228 | } |
| 229 | |
| 230 | return 0; |
| 231 | } |