-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtest_any.cpp
332 lines (268 loc) · 9.16 KB
/
test_any.cpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "solid/system/cassert.hpp"
#include "solid/system/exception.hpp"
#include "solid/utility/any.hpp"
#include <array>
#include <fstream>
#include <iostream>
#include <memory>
#include <string>
// #define TEST_BOOST_ANY
#ifdef TEST_BOOST_ANY
#include "boost/any.hpp"
#endif
using namespace solid;
using namespace std;
struct TestNoCopy {
std::string str;
TestNoCopy(const std::string& _rstr)
: str(_rstr)
{
}
TestNoCopy(const TestNoCopy&) = delete;
TestNoCopy& operator=(const TestNoCopy&) = delete;
TestNoCopy(TestNoCopy&& _utnc) noexcept
: str(std::move(_utnc.str))
{
}
TestNoCopy& operator=(TestNoCopy&& _utnc) noexcept
{
str = std::move(_utnc.str);
return *this;
}
};
#ifdef TEST_BOOST_ANY
std::string test_boost_any(const boost::any& _rany)
{
boost::any any0(_rany);
std::string retval;
TestNoCopy* p = boost::any_cast<TestNoCopy*>(any0);
if (p) {
retval = p->str;
}
return retval;
}
#endif
void test_any_no_copy_copy(const Any<32>& _rany)
{
bool caught_exception = false;
try {
Any<32> tmp_any(_rany);
solid_check(!tmp_any.has_value());
tmp_any.cast<TestNoCopy>()->str.clear();
} catch (std::exception& rex) {
cout << "Exception: " << rex.what() << endl;
caught_exception = true;
}
solid_check(_rany.has_value());
solid_check(caught_exception);
caught_exception = false;
Any<32> tmp_any;
try {
tmp_any = _rany;
} catch (std::exception& rex) {
cout << "Exception: " << rex.what() << endl;
caught_exception = true;
}
solid_check(!tmp_any.has_value());
solid_check(_rany.has_value());
solid_check(caught_exception);
}
std::string test_any_no_copy_move(Any<32>& _rany)
{
Any<32> tmp_any(std::move(_rany));
solid_check(!_rany.has_value() || (_rany.cast<TestNoCopy>() && _rany.cast<TestNoCopy>()->str.empty()));
solid_check(tmp_any.has_value() && tmp_any.cast<TestNoCopy>() != nullptr && !tmp_any.cast<TestNoCopy>()->str.empty());
TestNoCopy* p = tmp_any.cast<TestNoCopy>();
solid_check(p != nullptr);
return p->str;
}
struct Data {
Data()
: age(0)
{
}
Data(const std::string& _name, const std::string& _address, uint16_t _age)
: name(_name)
, address(_address)
, age(_age)
{
}
std::string name;
std::string address;
uint16_t age;
};
struct Test {
int i_;
Test(int _i)
: i_(_i)
{
cout << "Test(): " << this << " : " << i_ << endl;
}
Test(const Test& _t)
: i_(_t.i_)
{
cout << "Test(const Test&): " << this << " : " << i_ << endl;
}
Test(Test&& _t) noexcept
: i_(_t.i_)
{
_t.i_ = 0;
cout << "Test(Test&&): " << this << " : " << i_ << endl;
}
Test& operator=(const Test& _t) = delete;
Test& operator=(Test&& _t) = delete;
~Test()
{
cout << "~Test(): " << this << " : " << i_ << endl;
}
};
int test_any(int /*argc*/, char* /*argv*/[])
{
#ifdef TEST_BOOST_ANY
{
boost::any any0(TestNoCopy("a string"));
TestNoCopy* p = boost::any_cast<TestNoCopy*>(any0);
if (p) {
cout << "p->str = " << p->str << endl;
} else {
cout << "p == nullptr" << endl;
}
cout << "copy string = " << test_boost_any(any0) << endl;
}
#endif
#if 0
for(size_t i = 0; i < 64; ++i){
cout<<i<<" small_capacity = "<<any_impl::compute_small_capacity(i)<<endl;
}
#endif
Any<> any0;
auto any32(make_any<string, 32>(string("best string ever")));
cout << "sizeof(any0) = " << sizeof(any0) << endl;
cout << "sizeof(any32) = " << sizeof(any32) << endl;
cout << "sizeof(string) = " << sizeof(string) << endl;
solid_check(any32.has_value());
solid_check(any32.cast<string>() != nullptr);
solid_check(any32.get_if<string>() != nullptr);
solid_check(any32.cast<int>() == nullptr);
solid_check(any32.get_if<int>() == nullptr);
cout << "value = " << *any32.cast<string>() << endl;
any0 = std::move(any32);
solid_check(!any32.has_value() || (any32.cast<string>() != nullptr && any32.cast<string>()->empty()));
solid_check(any0.has_value());
solid_check(any0.cast<string>() != nullptr);
solid_check(any0.cast<int>() == nullptr);
cout << "value = " << *any0.cast<string>() << endl;
any32 = any0;
cout << "any32.is_small = " << any32.is_small() << endl;
solid_check(any32.has_value());
solid_check(any0.has_value());
Any<16> any16_0(any32);
Any<16> any16_1(any16_0);
cout << "sizeof(any16_0) = " << sizeof(any16_0) << endl;
cout << "any16_0.is_small = " << any16_0.is_small() << endl;
solid_check(any32.has_value());
solid_check(any16_0.has_value());
solid_check(any16_1.has_value());
solid_check(*any16_1.cast<string>() == *any32.cast<string>() && *any16_1.cast<string>() == *any16_0.cast<string>());
Any<16> any16_2(std::move(any16_0));
solid_check(!any16_0.has_value());
solid_check(any16_2.has_value());
solid_check(*any16_2.cast<string>() == *any32.cast<string>());
solid_check(*any16_2.get_if<string>() == *any32.get_if<string>());
auto any_nc_0(make_any<TestNoCopy, 32>("a string"));
test_any_no_copy_copy(any_nc_0);
cout << "test_any_no_copy_move: " << test_any_no_copy_move(any_nc_0) << endl;
{
std::shared_ptr<Data> ptr = std::make_shared<Data>("gigel", "gigel@viscol.ro", 33);
cout << "ptr.get = " << ptr.get() << endl;
auto any_ptr1 = make_any<std::shared_ptr<Data>, 256>(ptr);
cout << "any_ptr1->get = " << any_ptr1.cast<std::shared_ptr<Data>>()->get() << endl;
Any<256> any_ptr2 = any_ptr1;
cout << "any_ptr1->get = " << any_ptr1.cast<std::shared_ptr<Data>>()->get() << endl;
cout << "any_ptr2->get = " << any_ptr2.cast<std::shared_ptr<Data>>()->get() << endl;
cout << "ptr.get = " << ptr.get() << endl;
cout << "ptr usecount = " << ptr.use_count() << endl;
solid_check(ptr.use_count() == 3);
solid_check(ptr.get() == any_ptr1.cast<std::shared_ptr<Data>>()->get() && any_ptr2.cast<std::shared_ptr<Data>>()->get() == ptr.get());
auto any_ptr3 = make_any<std::shared_ptr<Data>, 256>(std::move(ptr));
solid_check(any_ptr2.cast<std::shared_ptr<Data>>()->get() == any_ptr2.cast<std::shared_ptr<Data>>()->get());
solid_check(any_ptr2.cast<std::shared_ptr<Data>>()->use_count() == 3);
solid_check(ptr.use_count() == 0);
}
{
Any<sizeof(Test)> any_t{Test{10}};
Any<> any_0{std::move(any_t)};
solid_check(!any_t.has_value());
solid_check(any_0.has_value());
any_t = std::move(any_0);
solid_check(!any_0.has_value());
solid_check(any_t.has_value());
any_0 = Test{5};
solid_check(any_0.has_value());
}
{
using Array4T = std::array<size_t, 4>;
Any<8> any{Array4T{{1, 2, 3, 4}}};
solid_check((*any.cast<Array4T>())[0] == 1);
solid_check((*any.cast<Array4T>())[1] == 2);
solid_check((*any.cast<Array4T>())[2] == 3);
solid_check((*any.cast<Array4T>())[3] == 4);
(*any.cast<Array4T>())[3] = 10;
solid_check((*any.cast<Array4T>())[3] == 10);
}
{
using Array4T = std::array<size_t, 4>;
Any<128> any(Array4T{{1, 2, 3, 4}});
solid_check((*any.cast<Array4T>())[0] == 1);
solid_check((*any.cast<Array4T>())[1] == 2);
solid_check((*any.cast<Array4T>())[2] == 3);
solid_check((*any.cast<Array4T>())[3] == 4);
solid_check((*any.get_if<Array4T>())[3] == 4);
(*any.cast<Array4T>())[3] = 10;
solid_check((*any.cast<Array4T>())[3] == 10);
}
{
using Array4T = std::array<size_t, 4>;
Any<128> any;
any = Array4T{{1, 2, 3, 4}};
solid_check((*any.cast<Array4T>())[0] == 1);
solid_check((*any.cast<Array4T>())[1] == 2);
solid_check((*any.cast<Array4T>())[2] == 3);
solid_check((*any.cast<Array4T>())[3] == 4);
(*any.cast<Array4T>())[3] = 10;
solid_check((*any.cast<Array4T>())[3] == 10);
}
{
using Array4T = std::array<size_t, 4>;
Array4T arr{{1, 2, 3, 4}};
auto lambda = [arr](const char* /*_txt*/) mutable {
solid_check(arr[3] == 4);
arr[3] = 10;
};
Any<128> any;
any = lambda;
(*any.cast<decltype(lambda)>())("Test");
}
{
using Array4T = std::array<size_t, 4>;
Array4T arr{{1, 2, 3, 4}};
auto lambda = [arr](const char* /*_txt*/) mutable {
solid_check(arr[3] == 4);
arr[3] = 10;
};
Any<128> any(lambda);
(*any.cast<decltype(lambda)>())("Test");
}
{
using Array4T = std::array<size_t, 4>;
Array4T arr{{1, 2, 3, 4}};
std::ifstream ifs;
auto lambda = [arr, ifs = std::move(ifs)](const char* /*_txt*/) mutable {
solid_check(arr[3] == 4);
arr[3] = 10;
};
Any<128> any(std::move(lambda));
(*any.cast<decltype(lambda)>())("Test");
}
return 0;
}