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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279 | #include "gate/tests.hpp"
#include "gate/wrappers.hpp"
#include "gate/lambdas.hpp"
#include "gate/objects.hpp"
using namespace gate;
static int const expectedIntValue = 42;
static void value_cb_ok(int& v)
{
GATEXX_TEST_CHECK(v == expectedIntValue);
}
static void value_cb_fail(int& v)
{
GATEXX_TEST_CHECK(false);
}
static void error_cb_ok()
{
GATEXX_TEST_CHECK(true);
}
static void error_cb_fail()
{
GATEXX_TEST_CHECK(false);
}
GATEXX_TEST_UNIT(PtrEmpty)
{
Ptr<int> ptr;
GATEXX_TEST_CHECK(ptr.empty());
GATEXX_TEST_CHECK(!ptr.hasValue());
GATEXX_TEST_CHECK(ptr.get() == NULL);
Delegate1<int&> ok_cb(&value_cb_fail);
Delegate0 fail_cb(&error_cb_ok);
GATEXX_TEST_CHECK_NOTHROW(ptr.andThen(ok_cb).orElse(fail_cb));
GATEXX_TEST_CHECK_THROW(ptr.orThrow());
}
GATEXX_TEST_UNIT(PtrValue)
{
int* ptr_int = new int(expectedIntValue);
Ptr<int> ptr(ptr_int);
GATEXX_TEST_CHECK(ptr.hasValue());
GATEXX_TEST_CHECK(!ptr.empty());
GATEXX_TEST_CHECK(*ptr == expectedIntValue);
GATEXX_TEST_CHECK(ptr.get() == ptr_int);
GATEXX_TEST_CHECK(ptr.release() == ptr_int);
GATEXX_TEST_CHECK(ptr.empty());
GATEXX_TEST_CHECK_NOTHROW(ptr.reset(ptr_int));
GATEXX_TEST_CHECK(ptr.get() == ptr_int);
GATEXX_TEST_CHECK_NOTHROW(ptr.andThen(&value_cb_ok).orElse(&error_cb_fail));
GATEXX_TEST_CHECK_NOTHROW(ptr.orThrow());
GATEXX_TEST_CHECK_NOTHROW(ptr.reset());
GATEXX_TEST_CHECK(ptr.empty());
}
static bool checkpoint1Reached = false;
static bool checkpoint2Reached = false;
GATEXX_TEST_UNIT(OptionalEmpty)
{
Optional<int> o;
GATEXX_TEST_CHECK(!o.hasValue());
GATEXX_TEST_CHECK(o.empty());
GATEXX_TEST_CHECK(o.get() == NULL);
Delegate1<int&> ok_cb(&value_cb_fail);
Delegate0 fail_cb(&error_cb_ok);
GATEXX_TEST_CHECK_NOTHROW(o.andThen(ok_cb).orElse(fail_cb));
GATEXX_TEST_CHECK_THROW(o.orThrow());
}
GATEXX_TEST_UNIT(OptionalValue)
{
Optional<int> o;
GATEXX_TEST_CHECK_NOTHROW(o.create(expectedIntValue));
GATEXX_TEST_CHECK(o.hasValue());
GATEXX_TEST_CHECK(!o.empty());
GATEXX_TEST_CHECK(o.get() != NULL);
GATEXX_TEST_CHECK_EQUAL(o.value(), expectedIntValue);
GATEXX_TEST_CHECK_EQUAL(o.value(22), expectedIntValue);
GATEXX_TEST_CHECK_EQUAL(*o, expectedIntValue);
GATEXX_TEST_CHECK_NOTHROW(o.andThen(&value_cb_ok).orElse(&error_cb_fail));
GATEXX_TEST_CHECK_NOTHROW(o.orThrow());
GATEXX_TEST_CHECK_NOTHROW(o.reset());
GATEXX_TEST_CHECK(o.empty());
}
GATE_LAMBDA(handleValue1, void, (int& v), {
GATEXX_TEST_CHECK(false);
checkpoint1Reached = true;
});
GATE_LAMBDA(handleEmpty1, void, (),
{
checkpoint2Reached = true;
});
GATEXX_TEST_UNIT(OptionalEmptyMonadic)
{
checkpoint1Reached = false;
checkpoint2Reached = false;
Optional<int> o; // empty
Delegate1<int&> ok_cb(handleValue1);
Delegate0 fail_cb(handleEmpty1);
o
.andThen(ok_cb)
.orElse(fail_cb);
GATEXX_TEST_CHECK(!checkpoint1Reached);
GATEXX_TEST_CHECK(checkpoint2Reached);
}
GATE_LAMBDA(handleValue2, void, (int& v), {
GATEXX_TEST_CHECK_EQUAL(v, expectedIntValue);
checkpoint1Reached = true;
});
GATE_LAMBDA(handleEmpty2, void, (),
{
GATEXX_TEST_CHECK(false);
checkpoint2Reached = true;
});
GATEXX_TEST_UNIT(OptionalValueMonadic)
{
checkpoint1Reached = false;
checkpoint2Reached = false;
Optional<int> o;
o.create(expectedIntValue)
.andThen(handleValue2)
.orElse(handleEmpty2);
GATEXX_TEST_CHECK(checkpoint1Reached);
GATEXX_TEST_CHECK(!checkpoint2Reached);
}
struct Good
{
int value;
Good(int v) : value(v) {}<--- Struct 'Good' has a constructor with 1 argument that is not explicit. [+]Struct 'Good' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
};
struct Bad
{
int value;
Bad(int v) : value(v) {}<--- Struct 'Bad' has a constructor with 1 argument that is not explicit. [+]Struct 'Bad' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
};
typedef Expected<Good, Bad> LocalResult;
LocalResult generate_result(bool returnError)
{
if (returnError)
{
return Bad(1);
}
else
{
return Good(2);
}
}
GATEXX_TEST_UNIT(ExpectedWithValue)
{
LocalResult xpres = generate_result(false);
GATEXX_TEST_CHECK(xpres.hasValue());
GATEXX_TEST_CHECK(xpres.get() != NULL);
GATEXX_TEST_CHECK(xpres.getErrorPtr() == NULL);
GATEXX_TEST_CHECK(xpres.value().value == 2);
GATEXX_TEST_CHECK(xpres.value(Good(3)).value == 2);
GATEXX_TEST_CHECK_NOTHROW(GATEXX_TEST_CHECK(xpres->value == 2));
}
GATEXX_TEST_UNIT(ExpectedWithError)
{
LocalResult xpres = generate_result(true);
GATEXX_TEST_CHECK(!xpres.hasValue());
GATEXX_TEST_CHECK(xpres.get() == NULL);
GATEXX_TEST_CHECK(xpres.getErrorPtr() != NULL);
GATEXX_TEST_CHECK(xpres.error().value == 1);
GATEXX_TEST_CHECK(xpres.value(Good(3)).value == 3);
}
static bool_t goodStateHandled = false;
static bool_t badStateHandled = false;
GATE_LAMBDA(handleGood, void, (Good& g), {
GATEXX_TEST_CHECK(g.value == 2);
goodStateHandled = true;
});
GATE_LAMBDA(handleBad, void, (Bad& b), {
GATEXX_TEST_CHECK(b.value == 1);
badStateHandled = true;
});
GATEXX_TEST_UNIT(ExpectedMonadic)
{
Delegate1<Good&> ok_cb(handleGood);
Delegate1<Bad&> fail_cb(handleBad);
{
goodStateHandled = false;
badStateHandled = false;
LocalResult success = generate_result(false);
GATEXX_TEST_CHECK_NOTHROW(success.andThen(ok_cb).orElse(fail_cb).orThrow());
GATEXX_TEST_CHECK(goodStateHandled);
GATEXX_TEST_CHECK(!badStateHandled);
}
{
goodStateHandled = false;
badStateHandled = false;
LocalResult failed = generate_result(true);
GATEXX_TEST_CHECK_THROW(failed.andThen(ok_cb).orElse(fail_cb).orThrow());
GATEXX_TEST_CHECK(!goodStateHandled);
GATEXX_TEST_CHECK(badStateHandled);
}
}
GATEXX_TEST_UNIT(RefEmpty)
{
Ref<int> r;
GATEXX_TEST_CHECK(r.empty());
GATEXX_TEST_CHECK(!r.hasValue());
GATEXX_TEST_CHECK(r.get() == NULL);
GATEXX_TEST_CHECK(r.size() == sizeof(int));
}
GATEXX_TEST_UNIT(RefValue)
{
Ptr<int> p(new int(2));
Ref<int> r(gate::moveRef(p));
GATEXX_TEST_CHECK(r.hasValue());
GATEXX_TEST_CHECK(!r.empty());
GATEXX_TEST_CHECK(r.get() != NULL);
GATEXX_TEST_CHECK(r.size() == sizeof(int));
GATEXX_TEST_CHECK(r.value() == 2);
Ref<int> r2(r);
GATEXX_TEST_CHECK(r2.hasValue());
GATEXX_TEST_CHECK(!r2.empty());
GATEXX_TEST_CHECK(r2.value() == 2);
}
GATEXX_TEST_UNIT(Variant)
{
Variant v = VariantFactory<int, double, bool>::create(12.0);
GATEXX_TEST_CHECK(v.getSize() == 8);
GATEXX_TEST_CHECK(v.is<double>());
}
GATEXX_TEST_UNIT(MemoryBlock)
{
static char const content[] = "Hello World";
static gate::size_t const contentsize = sizeof(content) - 1;
{
MemoryBlock rawmb(32);
gate_memoryblock_t* ptr_mb = rawmb.c_impl();
MemoryBlock rawmb2(ptr_mb);
gate_object_retain(ptr_mb);
GATEXX_TEST_CHECK(rawmb.equals(rawmb2));
}
MemoryBlock mb(content, contentsize);
GATEXX_TEST_CHECK(mb.c_impl() != NULL);
GATEXX_TEST_CHECK(!!mb);
GATEXX_TEST_CHECK(mb.getInterfaceName() == GATE_INTERFACE_NAME_MEMORYBLOCK);
GATEXX_TEST_CHECK(mb.getContent() != NULL);
GATEXX_TEST_CHECK(mb.getSize() == contentsize);
GATEXX_TEST_CHECK(mb.equals(content, contentsize));
MemoryBlock mb2 = mb;
MemoryBlock mb3 = mb;
mb3 = mb2;
}
|