forked from gsl-lite/gsl-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.t.cpp
226 lines (201 loc) · 6.33 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
//
// gsl-lite is based on GSL: Guidelines Support Library.
// For more information see https://github.com/martinmoene/gsl-lite
//
// Copyright (c) 2015 Martin Moene
// Copyright (c) 2015 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>
#define gsl_CPP11_OR_GREATER_WRT_FINAL ( gsl_CPP11_OR_GREATER || gsl_COMPILER_MSVC_VERSION >= 110 )
using namespace gsl;
CASE( "finally: Allows to run lambda on leaving scope" )
{
#if gsl_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_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_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_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_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( finally( action( i ) ) );
}
EXPECT( i == 1 );
}
// ...
#else
EXPECT( !!"lambda is not available (no C++11)" );
#endif
}
CASE( "on_return: Allows to perform action on leaving scope without exception (gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD)" )
{
#if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD )
#if gsl_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
}
CASE( "on_error: Allows to perform action on leaving scope via an exception (gsl_FEATURE_EXPERIMENTAL_RETURN_GUARD)" )
{
#if gsl_FEATURE( EXPERIMENTAL_RETURN_GUARD )
#if gsl_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
}
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 );
}
CASE( "narrow<>(): Allows narrowing without value loss" )
{
EXPECT( narrow<char>( 120 ) == 120 );
}
CASE( "narrow<>(): Terminates when narrowing with value loss" )
{
EXPECT_THROWS_AS( narrow<char>( 300 ), narrowing_error );
}
CASE( "narrow<>(): Terminates when narrowing with sign loss" )
{
EXPECT_THROWS_AS( narrow<unsigned>( -42 ), narrowing_error );
}
// end of file