summaryrefslogtreecommitdiff
path: root/test/OptionalRequiredTest.cpp
blob: c3fbfd1c39bc45843372bed971759f9d930d8848 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <cassert>
#include <map>
#include <iostream>
#include <protocol/TDebugProtocol.h>
#include <protocol/TBinaryProtocol.h>
#include <transport/TBufferTransports.h>
#include "gen-cpp/OptionalRequiredTest_types.h"

using std::cout;
using std::endl;
using std::map;
using std::string;
using namespace thrift::test;
using namespace facebook::thrift;
using namespace facebook::thrift::transport;
using namespace facebook::thrift::protocol;


/*
template<typename Struct>
void trywrite(const Struct& s, bool should_work) {
  bool worked;
  try {
    TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
    s.write(&protocol);
    worked = true;
  } catch (TProtocolException & ex) {
    worked = false;
  }
  assert(worked == should_work);
}
*/

template <typename Struct1, typename Struct2>
void write_to_read(const Struct1 & w, Struct2 & r) {
  TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
  w.write(&protocol);
  r.read(&protocol);
}


int main() {

  cout << "This old school struct should have three fields." << endl;
  {
    OldSchool o;
    cout << ThriftDebugString(o) << endl;
  }
  cout << endl;

  cout << "Setting a value before setting isset." << endl;
  {
    Simple s;
    cout << ThriftDebugString(s) << endl;
    s.im_optional = 10;
    cout << ThriftDebugString(s) << endl;
    s.__isset.im_optional = true;
    cout << ThriftDebugString(s) << endl;
  }
  cout << endl;

  cout << "Setting isset before setting a value." << endl;
  {
    Simple s;
    cout << ThriftDebugString(s) << endl;
    s.__isset.im_optional = true;
    cout << ThriftDebugString(s) << endl;
    s.im_optional = 10;
    cout << ThriftDebugString(s) << endl;
  }
  cout << endl;

  // Write-to-read with optional fields.
  {
    Simple s1, s2, s3;
    s1.im_optional = 10;
    assert(!s1.__isset.im_default);
  //assert(!s1.__isset.im_required);  // Compile error.
    assert(!s1.__isset.im_optional);

    write_to_read(s1, s2);

    assert( s2.__isset.im_default);
  //assert( s2.__isset.im_required);  // Compile error.
    assert(!s2.__isset.im_optional);
    assert(s3.im_optional == 0);

    s1.__isset.im_optional = true;
    write_to_read(s1, s3);

    assert( s3.__isset.im_default);
  //assert( s3.__isset.im_required);  // Compile error.
    assert( s3.__isset.im_optional);
    assert(s3.im_optional == 10);
  }

  // Writing between optional and default.
  {
    Tricky1 t1;
    Tricky2 t2;

    t2.im_optional = 10;
    write_to_read(t2, t1);
    write_to_read(t1, t2);
    assert(!t1.__isset.im_default);
    assert( t2.__isset.im_optional);
    assert(t1.im_default == t2.im_optional);
    assert(t1.im_default == 0);
  }

  // Writing between default and required.
  {
    Tricky1 t1;
    Tricky3 t3;
    write_to_read(t1, t3);
    write_to_read(t3, t1);
    assert(t1.__isset.im_default);
  }

  // Writing between optional and required.
  {
    Tricky2 t2;
    Tricky3 t3;
    t2.__isset.im_optional = true;
    write_to_read(t2, t3);
    write_to_read(t3, t2);
  }

  // Mu-hu-ha-ha-ha!
  {
    Tricky2 t2;
    Tricky3 t3;
    try {
      write_to_read(t2, t3);
      abort();
    }
    catch (TProtocolException& ex) {}

    write_to_read(t3, t2);
    assert(t2.__isset.im_optional);
  }

  cout << "Complex struct, simple test." << endl;
  {
    Complex c;
    cout << ThriftDebugString(c) << endl;
  }


  {
    Tricky1 t1;
    Tricky2 t2;
    // Compile error.
    //(void)(t1 == t2);
  }

  {
    OldSchool o1, o2, o3;
    assert(o1 == o2);
    o1.im_int = o2.im_int = 10;
    assert(o1 == o2);
    o1.__isset.im_int = true;
    o2.__isset.im_int = false;
    assert(o1 == o2);
    o1.im_int = 20;
    o1.__isset.im_int = false;
    assert(o1 != o2);
    o1.im_int = 10;
    assert(o1 == o2);
    o1.im_str = o2.im_str = "foo";
    assert(o1 == o2);
    o1.__isset.im_str = o2.__isset.im_str = true;
    assert(o1 == o2);
    map<int32_t,string> mymap;
    mymap[1] = "bar";
    mymap[2] = "baz";
    o1.im_big.push_back(map<int32_t,string>());
    assert(o1 != o2);
    o2.im_big.push_back(map<int32_t,string>());
    assert(o1 == o2);
    o2.im_big.push_back(mymap);
    assert(o1 != o2);
    o1.im_big.push_back(mymap);
    assert(o1 == o2);

    TBinaryProtocol protocol(boost::shared_ptr<TTransport>(new TMemoryBuffer));
    o1.write(&protocol);

    o1.im_big.push_back(mymap);
    mymap[3] = "qux";
    o2.im_big.push_back(mymap);
    assert(o1 != o2);
    o1.im_big.back()[3] = "qux";
    assert(o1 == o2);

    o3.read(&protocol);
    o3.im_big.push_back(mymap);
    assert(o1 == o3);

    //cout << ThriftDebugString(o3) << endl;
  }

  {
    Tricky2 t1, t2;
    assert(t1.__isset.im_optional == false);
    assert(t2.__isset.im_optional == false);
    assert(t1 == t2);
    t1.im_optional = 5;
    assert(t1 == t2);
    t2.im_optional = 5;
    assert(t1 == t2);
    t1.__isset.im_optional = true;
    assert(t1 != t2);
    t2.__isset.im_optional = true;
    assert(t1 == t2);
    t1.im_optional = 10;
    assert(t1 != t2);
    t2.__isset.im_optional = false;
    assert(t1 != t2);
  }

  return 0;
}