-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick-fcmp
310 lines (253 loc) · 8.67 KB
/
brick-fcmp
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
#pragma once
#include <brick-assert>
#include <brick-string>
#include <brick-enumerate>
#include <typeindex>
#include <sys/time.h>
namespace brq
{
struct precondition_failed : std::exception {};
inline void precondition( bool x )
{
if ( !x )
throw precondition_failed{};
}
template< typename out_t, typename... state_t >
struct program
{
using op_t = std::function< out_t( state_t & ... ) >;
using outs_t = std::vector< out_t >;
std::vector< op_t > ops;
brq::string_builder str;
friend brq::string_builder &operator<<( brq::string_builder &b, const program &p )
{
return b << p.str;
}
auto eval( state_t... state )
{
using outs_t = std::vector< out_t >;
outs_t outs;
for ( auto &f : ops )
outs.push_back( f( state... ) );
return std::tuple< outs_t, state_t... >{ outs, state... };
}
void add( const op_t &op, std::string_view name )
{
ops.push_back( op );
str << "\n" << name;
}
};
template< typename out_t, typename... state_t >
auto enum_program( brq::nat index,
std::vector< std::string_view > names,
auto... avail )
{
using op_t = std::function< out_t( state_t & ... ) >;
using ops_t = std::vector< op_t >;
ops_t avail_vec{ avail... };
program< out_t, state_t ... > p;
for ( auto i : brq::enumerate::list( index, avail_vec.size() ) )
p.add( avail_vec[ i.short_digit() ], names[ i.short_digit() ] );
return p;
}
template< typename out_t, typename... state_t >
auto enum_program( brq::nat index, auto... avail )
{
using op_t = std::function< out_t( state_t & ... ) >;
using ops_t = std::vector< op_t >;
std::vector< std::string_view > names{ std::get< 0 >( avail )... };
ops_t avail_vec{ std::get< 1 >( avail )... };
program< out_t, state_t ... > p;
for ( auto i : brq::enumerate::list( index, avail_vec.size() ) )
p.add( avail_vec[ i.short_digit() ], names[ i.short_digit() ] );
return p;
}
inline std::type_index except_type( std::exception_ptr eptr )
{
try
{
if ( eptr )
std::rethrow_exception( eptr );
}
catch ( const std::exception &e )
{
return typeid( e );
}
return typeid( nullptr );
}
inline bool except_type_eq( std::exception_ptr a_ptr, std::exception_ptr b_ptr )
{
if ( !a_ptr && !b_ptr )
return true;
if ( !a_ptr || !b_ptr )
return false;
return except_type( a_ptr ) == except_type( b_ptr );
}
struct fcmp_base
{
virtual bool is_match() const = 0;
explicit operator bool() const { return is_match(); }
};
template< typename retval_t, typename... args_t >
struct fcmp_call_result
{
std::tuple< args_t... > args;
std::exception_ptr except;
retval_t retval{};
int64_t nsec;
};
template< typename elem_t, typename = void >
struct abbreviate
{
const elem_t &e;
abbreviate( const elem_t &e ) : e( e ) {}
friend auto &operator<<( brq::string_builder &b, const abbreviate &a )
{
return brq::format_nofail( b, a.e );
}
};
template< typename coll_t >
struct abbreviate< coll_t, std::void_t< typename coll_t::iterator > >
{
const coll_t &col;
abbreviate( const coll_t &c ) : col( c ) {}
friend auto &operator<<( brq::string_builder &b, const abbreviate &a )
{
int i = 0;
if constexpr ( requires { a.col.size(); } )
for ( auto it = a.col.begin(); it != a.col.end(); ++it, ++i )
{
if ( i < 16 )
b << ( i ? ", " : "[ " ) << brq::abbreviate( *it );
else if ( i == 16 && a.col.size() > 24 )
b << ", …";
else if ( a.col.size() - i < 8 )
b << ", " << brq::abbreviate( *it );
}
else
return b << a.col;
return b << ( i ? " ]" : "[]" );
}
};
template< typename... args_t >
struct abbreviate< std::tuple< args_t... > >
{
using indices = std::index_sequence_for< args_t... >;
const std::tuple< args_t... > &tuple;
abbreviate( const std::tuple< args_t... > &t ) : tuple( t ) {}
template< std::size_t... idx >
auto &print( std::index_sequence< idx... >, brq::string_builder &b ) const
{
int i = 0;
( ( b << ( i++ ? ", " : "[ " ) << brq::abbreviate{ std::get< idx >( tuple ) } ), ... );
return b << " ]";
}
friend auto &operator<<( brq::string_builder &b, const abbreviate &a )
{
return a.print( indices(), b );
}
};
inline constexpr int timeout_factor = 50;
template< typename retval_t, typename... args_t >
struct fcmp_result : fcmp_base
{
std::tuple< args_t... > input;
fcmp_call_result< retval_t, args_t... > f, g;
bool is_match() const override
{
return except_type( f.except ) == typeid( precondition_failed ) ||
(
g.args == f.args &&
g.retval == f.retval &&
g.nsec < timeout_factor * f.nsec &&
except_type_eq( g.except, f.except )
);
}
friend brq::string_builder &operator<<( brq::string_builder &b, const fcmp_result &r )
{
b << "\n";
b << "input arguments: " << abbreviate( r.input ) << "\n";
if ( r.f.args != r.g.args )
{
b << "output arguments:\n"
<< " expected: " << abbreviate( r.f.args ) << "\n"
<< " actual: " << abbreviate( r.g.args ) << "\n";
}
if ( r.f.retval != r.g.retval )
{
b << "return value:\n"
<< " expected: " << abbreviate( r.f.retval ) << "\n"
<< " actual: " << abbreviate( r.g.retval ) << "\n";
}
if ( r.f.nsec * timeout_factor <= r.g.nsec )
{
b << "running time:\n"
<< " expected: <" << r.f.nsec / 1000000.0 * timeout_factor << "ms\n"
<< " actual: " << r.g.nsec / 1000000.0 << "ms\n";
}
auto f_eid = except_type( r.f.except );
auto g_eid = except_type( r.g.except );
if ( f_eid != g_eid )
{
b << "exception type:\n"
<< " expected: " << f_eid.name() << "\n"
<< " actual: " << g_eid.name() << "\n";
}
return b;
}
};
inline void fcmp_itimer( int64_t usec )
{
struct ::itimerval stop{};
stop.it_value.tv_usec = usec;
setitimer( ITIMER_VIRTUAL, &stop, nullptr );
}
inline int64_t fcmp_clock()
{
struct ::timespec now;
if ( clock_gettime( CLOCK_MONOTONIC, &now ) )
perror( "clock_gettime" ), std::exit( 1 );
return int64_t( now.tv_sec ) * 1'000'000'000 + now.tv_nsec;
}
auto fcmp_call( auto fun, auto &arg, auto &rv, auto &eptr, auto &nsec )
{
int64_t start = fcmp_clock();
try
{
rv = std::apply( fun, arg );
}
catch ( ... )
{
eptr = std::current_exception();
}
nsec = std::max( fcmp_clock() - start, int64_t( 1000 ) );
}
auto fcmp( auto f, auto g, auto... args )
{
fcmp_result< decltype( g( args... ) ), decltype( args )... > r;
r.f.args = r.input = std::tuple{ args... };
brq::abbreviate input_args( r.input );
TRACE( input_args );
fcmp_call( f, r.f.args, r.f.retval, r.f.except, r.f.nsec );
if ( except_type( r.f.except ) == typeid( precondition_failed ) )
{
TRACE( "precondition failed" );
return r;
}
int retry = 3;
do
{
r.g.args = std::tuple{ args... };
fcmp_call( g, r.g.args, r.g.retval, r.g.except, r.g.nsec );
}
while ( retry-- && r.g.nsec > timeout_factor * r.f.nsec );
return r;
}
void fcmp_assert( auto f, auto g, auto && ... args )
{
auto match = fcmp( f, g, std::forward< decltype( args ) >( args )... );
brq::trace_clear();
ASSERT( match );
brq::trace_clear();
}
}