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