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