forked from gsl-lite/gsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.t.cpp
370 lines (324 loc) · 13.8 KB
/
util.t.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//
// gsl-lite is based on GSL: Guidelines Support Library.
// For more information see https://github.com/gsl-lite/gsl-lite
//
// Copyright (c) 2015-2019 Martin Moene
// Copyright (c) 2019-2021 Moritz Beutel
// Copyright (c) 2015-2018 Microsoft Corporation. All rights reserved.
//
// This code is licensed under the MIT License (MIT).
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "gsl-lite.t.hpp"
#include <functional>
#if gsl_STDLIB_CPP11_OR_GREATER
# include <limits>
# include <cstdint>
#endif // gsl_STDLIB_CPP11_OR_GREATER
#define gsl_STDLIB_CPP11_OR_GREATER_WRT_FINAL ( gsl_STDLIB_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 110 )
using namespace gsl;
CASE( "finally: Allows to run lambda on leaving scope" )
{
#if gsl_STDLIB_CPP11_OR_GREATER_WRT_FINAL
struct F { static void incr( int & i ) { i += 1; } };
int i = 0;
{
auto _ = finally( [&]() { F::incr( i ); } );
EXPECT( i == 0 );
}
EXPECT( i == 1 );
#else
EXPECT( !!"lambda is not available (no C++11)" );
#endif
}
CASE( "finally: Allows to run function (bind) on leaving scope" )
{
#if gsl_STDLIB_CPP11_OR_GREATER_WRT_FINAL
struct F { static void incr( int & i ) { i += 1; } };
int i = 0;
{
auto _ = finally( std::bind( &F::incr, std::ref( i ) ) );
EXPECT( i == 0 );
}
EXPECT( i == 1 );
#else
EXPECT( !!"auto and std::ref perhaps not available (no C++11)" );
#endif
}
namespace{ int g_i = 0; }
CASE( "finally: Allows to run function (pointer) on leaving scope" )
{
struct F { static void incr() { g_i += 1; } };
#if gsl_STDLIB_CPP11_OR_GREATER_WRT_FINAL
g_i = 0;
{
auto _ = finally( &F::incr );
EXPECT( g_i == 0 );
}
EXPECT( g_i == 1 );
#else
g_i = 0;
{
final_action _ = finally( &F::incr );
EXPECT( g_i == 0 );
}
EXPECT( g_i == 1 );
#endif
}
CASE( "finally: Allows to move final_action" )
{
#if gsl_STDLIB_CPP11_OR_GREATER_WRT_FINAL
struct F { static void incr( int & i ) { i += 1; } };
int i = 0;
{
auto _1 = finally( [&]() { F::incr( i ); } );
{
auto _2 = std::move( _1 );
EXPECT( i == 0 );
}
EXPECT( i == 1 );
{
auto _2 = std::move( _1 );
EXPECT( i == 1 );
}
EXPECT( i == 1 );
}
EXPECT( i == 1 );
#else
struct F { static void incr() { g_i += 1; } };
g_i = 0;
{
final_action _1 = finally( &F::incr );
{
final_action _2 = _1;
EXPECT( g_i == 0 );
}
EXPECT( g_i == 1 );
{
final_action _2 = _1;
EXPECT( g_i == 1 );
}
EXPECT( g_i == 1 );
}
EXPECT( g_i == 1 );
#endif
}
CASE( "finally: Allows moving final_action to throw" "[.]")
{
#if gsl_HAVE( EXCEPTIONS )
# if gsl_STDLIB_CPP11_OR_GREATER_WRT_FINAL
struct action
{
int & i_;
void operator()(){ i_ += 1; }
action( int & i ) : i_( i ) {}
action( action && other ) : i_( other.i_) { throw std::runtime_error("action move-ctor"); }
};
int i = 0;
{
{
EXPECT_THROWS( (void) finally( action( i ) ) );
}
EXPECT( i == 1 );
}
// ...
# else
EXPECT( !!"lambda is not available (no C++11)" );
# endif
#endif // gsl_HAVE( EXCEPTIONS )
}
CASE( "on_return: Allows to perform action on leaving scope without exception (gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD)" )
{
#if gsl_HAVE( EXCEPTIONS )
# if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD )
# if gsl_STDLIB_CPP11_OR_GREATER_WRT_FINAL
struct F {
static void incr() { g_i += 1; }
static void pass() { try { auto _ = on_return( &F::incr ); /*throw std::exception();*/ } catch (...) {} }
static void fail() { try { auto _ = on_return( &F::incr ); throw std::exception(); } catch (...) {} }
};
# else
struct F {
static void incr() { g_i += 1; }
static void pass() { try { final_action_return _ = on_return( &F::incr ); /*throw std::exception();*/ } catch (...) {} }
static void fail() { try { final_action_return _ = on_return( &F::incr ); throw std::exception(); } catch (...) {} }
};
# endif
struct G {
~G() { F::pass(); }
};
{ g_i = 0; F::pass(); EXPECT( g_i == 1 ); }
{ g_i = 0; F::fail(); EXPECT( g_i == 0 ); }
{ g_i = 0; try { G g; throw std::exception(); } catch (...) {}; EXPECT( g_i == 1 ); }
# else
EXPECT( !!"on_return not available (no gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD)" );
# endif
#endif // gsl_HAVE( EXCEPTIONS )
}
CASE( "on_error: Allows to perform action on leaving scope via an exception (gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD)" )
{
#if gsl_HAVE( EXCEPTIONS )
# if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD )
# if gsl_STDLIB_CPP11_OR_GREATER_WRT_FINAL
struct F {
static void incr() { g_i += 1; }
static void pass() { try { auto _ = on_error( &F::incr ); /*throw std::exception();*/ } catch (...) {} }
static void fail() { try { auto _ = on_error( &F::incr ); throw std::exception(); } catch (...) {} }
};
# else
struct F {
static void incr() { g_i += 1; }
static void pass() { try { final_action_error _ = on_error( &F::incr ); /*throw std::exception();*/ } catch (...) {} }
static void fail() { try { final_action_error _ = on_error( &F::incr ); throw std::exception(); } catch (...) {} }
};
# endif
struct G {
~G() { F::pass(); }
};
{ g_i = 0; F::pass(); EXPECT( g_i == 0 ); }
{ g_i = 0; F::fail(); EXPECT( g_i == 1 ); }
{ g_i = 0; try { G g; throw std::exception(); } catch (...) {}; EXPECT( g_i == 0 ); }
# else
EXPECT( !!"on_error not available (no gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD)" );
# endif
#endif // gsl_HAVE( EXCEPTIONS )
}
CASE( "narrow_cast<>: Allows narrowing without value loss" )
{
EXPECT( narrow_cast<char>( 120 ) == 120 );
}
CASE( "narrow_cast<>: Allows narrowing with value loss" )
{
EXPECT( narrow_cast<unsigned char>( 300 ) == 44 );
}
#if gsl_STDLIB_CPP11_OR_GREATER
const std::uint8_t u8 = std::uint8_t((std::numeric_limits< std::uint8_t>::max)() - 1);
const std::uint16_t u16 = std::uint16_t((std::numeric_limits<std::uint16_t>::max)() - 1);
const std::int8_t i8n = std::int8_t((std::numeric_limits< std::int8_t>::min)() + 1);
const std::int16_t i16n = std::int16_t((std::numeric_limits< std::int16_t>::min)() + 1);
const std::int8_t i8p = std::int8_t((std::numeric_limits< std::int8_t>::max)() - 1);
const std::int16_t i16p = std::int16_t((std::numeric_limits< std::int16_t>::max)() - 1);
#endif // gsl_CPP11_OR_GREATER
CASE( "narrow<>(): Allows narrowing without value loss" )
{
#if gsl_HAVE( EXCEPTIONS )
EXPECT( narrow<char>( 120 ) == 120 );
# if gsl_STDLIB_CPP11_OR_GREATER
std::uint8_t lu8;
std::uint16_t lu16;
std::int8_t li8;
std::int16_t li16;
// uint <-> uint
EXPECT_NO_THROW((lu16 = narrow<std::uint16_t>( std::uint8_t( u8)))); EXPECT(lu16 == u8);
EXPECT_NO_THROW(( lu8 = narrow< std::uint8_t>( std::uint8_t( u8)))); EXPECT( lu8 == u8);
EXPECT_NO_THROW(( lu8 = narrow< std::uint8_t>(std::uint16_t( u8)))); EXPECT( lu8 == u8);
// int <-> int
EXPECT_NO_THROW((li16 = narrow< std::int16_t>( std::int8_t(i8n)))); EXPECT(li16 == i8n);
EXPECT_NO_THROW((li16 = narrow< std::int16_t>( std::int8_t(i8p)))); EXPECT(li16 == i8p);
EXPECT_NO_THROW(( li8 = narrow< std::int8_t>( std::int8_t(i8n)))); EXPECT( li8 == i8n);
EXPECT_NO_THROW(( li8 = narrow< std::int8_t>( std::int8_t(i8p)))); EXPECT( li8 == i8p);
EXPECT_NO_THROW(( li8 = narrow< std::int8_t>( std::int16_t(i8n)))); EXPECT( li8 == i8n);
EXPECT_NO_THROW(( li8 = narrow< std::int8_t>( std::int16_t(i8p)))); EXPECT( li8 == i8p);
// uint <-> int
EXPECT_NO_THROW((lu16 = narrow<std::uint16_t>( std::int8_t(i8p)))); EXPECT(lu16 == i8p);
EXPECT_NO_THROW((li16 = narrow< std::int16_t>( std::uint8_t(i8p)))); EXPECT(li16 == i8p);
EXPECT_NO_THROW(( lu8 = narrow< std::uint8_t>( std::int8_t(i8p)))); EXPECT( lu8 == i8p);
EXPECT_NO_THROW(( li8 = narrow< std::int8_t>( std::uint8_t(i8p)))); EXPECT( li8 == i8p);
EXPECT_NO_THROW(( lu8 = narrow< std::uint8_t>( std::int16_t(i8p)))); EXPECT( lu8 == i8p);
EXPECT_NO_THROW(( li8 = narrow< std::int8_t>(std::uint16_t(i8p)))); EXPECT( li8 == i8p);
# endif // gsl_STDLIB_CPP11_OR_GREATER
#endif // gsl_HAVE( EXCEPTIONS )
}
CASE( "narrow<>(): Terminates when narrowing with value loss" )
{
#if gsl_HAVE( EXCEPTIONS )
EXPECT_THROWS_AS( (void) narrow<char>( 300 ), narrowing_error );
# if gsl_STDLIB_CPP11_OR_GREATER
// uint <-> uint
EXPECT_THROWS_AS( (void) narrow< std::uint8_t>( std::uint16_t( u16) ), narrowing_error );
// int <-> int
EXPECT_THROWS_AS( (void) narrow< std::int8_t>( std::int16_t(i16n) ), narrowing_error );
EXPECT_THROWS_AS( (void) narrow< std::int8_t>( std::int16_t(i16p) ), narrowing_error );
// uint <-> int
EXPECT_THROWS_AS( (void) narrow< std::int8_t>( std::uint8_t( u8 ) ), narrowing_error );
EXPECT_THROWS_AS( (void) narrow< std::uint8_t>( std::int16_t(i16p) ), narrowing_error );
EXPECT_THROWS_AS( (void) narrow< std::int8_t>( std::uint16_t( u8 ) ), narrowing_error );
EXPECT_THROWS_AS( (void) narrow< std::int8_t>( std::uint16_t(u16 ) ), narrowing_error );
# endif // gsl_STDLIB_CPP11_OR_GREATER
#endif // gsl_HAVE( EXCEPTIONS )
}
CASE( "narrow<>(): Terminates when narrowing with sign loss" )
{
#if gsl_HAVE( EXCEPTIONS )
EXPECT_THROWS_AS( (void) narrow<unsigned>( -42 ), narrowing_error );
# if gsl_STDLIB_CPP11_OR_GREATER
// uint <-> int
EXPECT_THROWS_AS( (void) narrow<std::uint16_t>( std::int8_t( i8n) ), narrowing_error );
EXPECT_THROWS_AS( (void) narrow< std::uint8_t>( std::int8_t( i8n) ), narrowing_error );
EXPECT_THROWS_AS( (void) narrow< std::uint8_t>( std::int16_t( i8n) ), narrowing_error );
EXPECT_THROWS_AS( (void) narrow< std::uint8_t>( std::int16_t(i16n) ), narrowing_error );
# endif // gsl_STDLIB_CPP11_OR_GREATER
#endif // gsl_HAVE( EXCEPTIONS )
}
CASE( "narrow_failfast<>(): Allows narrowing without value loss" )
{
EXPECT( narrow_failfast<char>( 120 ) == 120 );
#if gsl_STDLIB_CPP11_OR_GREATER
std::uint8_t lu8;
std::uint16_t lu16;
std::int8_t li8;
std::int16_t li16;
// uint <-> uint
EXPECT_NO_THROW((lu16 = narrow_failfast<std::uint16_t>( std::uint8_t( u8)))); EXPECT(lu16 == u8);
EXPECT_NO_THROW(( lu8 = narrow_failfast< std::uint8_t>( std::uint8_t( u8)))); EXPECT( lu8 == u8);
EXPECT_NO_THROW(( lu8 = narrow_failfast< std::uint8_t>(std::uint16_t( u8)))); EXPECT( lu8 == u8);
// int <-> int
EXPECT_NO_THROW((li16 = narrow_failfast< std::int16_t>( std::int8_t(i8n)))); EXPECT(li16 == i8n);
EXPECT_NO_THROW((li16 = narrow_failfast< std::int16_t>( std::int8_t(i8p)))); EXPECT(li16 == i8p);
EXPECT_NO_THROW(( li8 = narrow_failfast< std::int8_t>( std::int8_t(i8n)))); EXPECT( li8 == i8n);
EXPECT_NO_THROW(( li8 = narrow_failfast< std::int8_t>( std::int8_t(i8p)))); EXPECT( li8 == i8p);
EXPECT_NO_THROW(( li8 = narrow_failfast< std::int8_t>( std::int16_t(i8n)))); EXPECT( li8 == i8n);
EXPECT_NO_THROW(( li8 = narrow_failfast< std::int8_t>( std::int16_t(i8p)))); EXPECT( li8 == i8p);
// uint <-> int
EXPECT_NO_THROW((lu16 = narrow_failfast<std::uint16_t>( std::int8_t(i8p)))); EXPECT(lu16 == i8p);
EXPECT_NO_THROW((li16 = narrow_failfast< std::int16_t>( std::uint8_t(i8p)))); EXPECT(li16 == i8p);
EXPECT_NO_THROW(( lu8 = narrow_failfast< std::uint8_t>( std::int8_t(i8p)))); EXPECT( lu8 == i8p);
EXPECT_NO_THROW(( li8 = narrow_failfast< std::int8_t>( std::uint8_t(i8p)))); EXPECT( li8 == i8p);
EXPECT_NO_THROW(( lu8 = narrow_failfast< std::uint8_t>( std::int16_t(i8p)))); EXPECT( lu8 == i8p);
EXPECT_NO_THROW(( li8 = narrow_failfast< std::int8_t>(std::uint16_t(i8p)))); EXPECT( li8 == i8p);
#endif // gsl_STDLIB_CPP11_OR_GREATER
}
CASE( "narrow_failfast<>(): Terminates when narrowing with value loss" )
{
EXPECT_THROWS_AS( (void) narrow_failfast<char>( 300 ), fail_fast );
#if gsl_STDLIB_CPP11_OR_GREATER
// uint <-> uint
EXPECT_THROWS_AS( (void) narrow_failfast< std::uint8_t>( std::uint16_t( u16) ), fail_fast );
// int <-> int
EXPECT_THROWS_AS( (void) narrow_failfast< std::int8_t>( std::int16_t(i16n) ), fail_fast );
EXPECT_THROWS_AS( (void) narrow_failfast< std::int8_t>( std::int16_t(i16p) ), fail_fast );
// uint <-> int
EXPECT_THROWS_AS( (void) narrow_failfast< std::int8_t>( std::uint8_t( u8 ) ), fail_fast );
EXPECT_THROWS_AS( (void) narrow_failfast< std::uint8_t>( std::int16_t(i16p) ), fail_fast );
EXPECT_THROWS_AS( (void) narrow_failfast< std::int8_t>( std::uint16_t( u8 ) ), fail_fast );
EXPECT_THROWS_AS( (void) narrow_failfast< std::int8_t>( std::uint16_t(u16 ) ), fail_fast );
#endif // gsl_STDLIB_CPP11_OR_GREATER
}
CASE( "narrow_failfast<>(): Terminates when narrowing with sign loss" )
{
EXPECT_THROWS_AS( (void) narrow_failfast<unsigned>( -42 ), fail_fast );
#if gsl_STDLIB_CPP11_OR_GREATER
// uint <-> int
EXPECT_THROWS_AS( (void) narrow_failfast<std::uint16_t>( std::int8_t( i8n) ), fail_fast );
EXPECT_THROWS_AS( (void) narrow_failfast< std::uint8_t>( std::int8_t( i8n) ), fail_fast );
EXPECT_THROWS_AS( (void) narrow_failfast< std::uint8_t>( std::int16_t( i8n) ), fail_fast );
EXPECT_THROWS_AS( (void) narrow_failfast< std::uint8_t>( std::int16_t(i16n) ), fail_fast );
#endif // gsl_STDLIB_CPP11_OR_GREATER
}
// end of file