-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathchecks.cc
308 lines (278 loc) · 9.28 KB
/
checks.cc
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
// -*- mode: c++; coding: utf-8 -*-
// ra-ra/test - Runtime checks.
// (c) Daniel Llorens - 2019-2024
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
#ifdef RA_DO_CHECK
#undef RA_DO_CHECK
#endif
#define RA_DO_CHECK 1 // the point here
#include "ra/base.hh"
#include <exception>
// -------------------------------------
// bit from example/throw.cc which FIXME maybe a prepared option.
struct ra_error: public std::exception
{
std::string s;
template <class ... A> ra_error(A && ... a): s(ra::format(std::forward<A>(a) ...)) {}
virtual char const * what() const throw () { return s.c_str(); }
};
#define RA_ASSERT( cond, ... ) \
{ if (!( cond )) [[unlikely]] throw ra_error("ra::", std::source_location::current(), " (" STRINGIZE(cond) ") " __VA_OPT__(,) __VA_ARGS__); }
// -------------------------------------
#include "ra/test.hh"
#include "mpdebug.hh"
using std::cout, std::endl, std::flush, std::string, ra::TestRecorder;
using ra::int_c, ra::mp::int_list;
int main()
{
cout << "******* " << RA_DO_CHECK << "******" << endl;
TestRecorder tr(std::cout);
tr.section("bad cell rank");
{
bool yes = false;
ra::Big<int> a0 = 0;
std::string msg;
try {
std::cout << ra::iter<1>(a0) << std::endl;
} catch (ra_error & e) {
msg = e.what();
yes = true;
}
tr.info(msg).test(yes);
}
// ------------------------------
// see test/iota.cc
// ------------------------------
tr.section("ot of range with iota");
{
std::string msg;
try {
cout << ra::iota(10).at(std::array {11}) << endl;
} catch (ra_error & e) {
msg = e.what();
}
tr.info(msg).test(0<msg.size());
}
// ------------------------------
// see test/fromb.cc
// ------------------------------
tr.section("out of range with iota subscripts");
{
std::string msg;
try {
ra::Small<int, 10> a = ra::_0;
cout << a(ra::iota(ra::int_c<1>(), 10)) << endl;
} catch (ra_error & e) {
msg = e.what();
}
tr.info(msg).test(0<msg.size());
}
// ------------------------------
// see test/compatibility.cc
// ------------------------------
tr.section("bad indices to ptr().at()");
{
int x = 0;
try {
int p[] = {10, 20, 30};
tr.test_eq(p[2], ra::ptr(p).at(std::array {2}));
x = 1;
tr.test_eq(p[2], ra::ptr((int *)p, ra::int_c<2>{}).at(std::array {2}));
x = 2;
} catch (ra_error & e) {
x = x+10;
}
tr.info("ptr.at()").test_eq(11, x);
x = 0;
try {
int p[] = {10, 20, 30};
tr.test_eq(p[2], ra::ptr((int *)p, 2).at(std::array {2}));
x = 1;
} catch (ra_error & e) {
x = x+10;
}
tr.info("ptr.at()").test_eq(10, x);
}
// ------------------------------
// FIXME checking at Match isn't enough, and ply() doesn't check nicely
// ------------------------------
/*
ra::Big<double> z(10, 0.);
auto w = z(ra::insert<1>, ra::all);
// causes abort() in ply(). Checks are skipped bc there's only one positive rank arg
w *= -1;
// causes abort() in ply(). Checks are skipped bc there's only one argument
for_each([](auto & w) { w *= -1; }, -w) ;
// causes abort() in ply(). Checks are skipped bc no expr is built.
for_each([](auto & w) { w *= -1; }, w) ;
std::cout << w << std::endl;
*/
// ------------------------------
// see test/frame-new.cc
// ------------------------------
tr.section("static match in dynamic case");
{
ra::Big<int> a({2, 3, 4}, 0);
tr.test_eq(2, agree_s(a, 99));
}
tr.section("dynamic (implicit) match");
{
ra::Big<int, 3> a({2, 3, 4}, (ra::_0+1)*100 + (ra::_1+1)*10 + (ra::_2+1));
ra::Big<int, 4> b({2, 4, 4, 5}, (ra::_0+1)*1000 + (ra::_1+1)*100 + (ra::_2+1)*10 + (ra::_3+1));
tr.test_eq(1, agree_s(a, b));
tr.test_eq(0, agree(a, b));
#define EXPR expr([](auto && a, auto && b) { return a+b; }, start(a), start(b))
int x = 0;
try {
tr.test_eq(ra::ANY, EXPR.len_s(1));
x = 1;
} catch (ra_error & e) {
}
tr.test_eq(0, x);
#undef EXPR
}
// If the size of an expr is static, dynamic checks may still need to be run if any of the terms of the expr has dynamic size. This is checked in Match::check_s().
tr.section("static mismatch");
{
ra::Small<int, 2, 2> a;
ra::Small<int, 3, 2> b;
static_assert(0==agree_s(a, b));
}
tr.section("static match");
{
ra::Small<int, 2, 2> a;
ra::Small<int, 2> b;
static_assert(2==agree_s(a, b));
}
tr.section("static match with dynamic term");
{
ra::Small<int> a;
ra::Big<int, 1> b({1}, 77);
tr.info("with rank ", ra::rank_s<decltype(a+b)>()).test_eq(2, agree_s(a, b));
}
tr.section("dynamic terms in match, static rank");
{
int error = 0;
std::string s;
try {
ra::Small<int, 2> a {2, 3};
ra::Big<int, 1> b({1}, 77);
std::cout << "LEN " << decltype(a+b)::len_s(0) << std::endl;
tr.info("with rank ", ra::rank_s<decltype(a+b)>()).test_eq(1, decltype(a+b)::check_s());
tr.test(!agree(a, b));
a = b;
} catch (ra_error & e) {
error = 1;
s = e.s;
}
tr.info("dynamic size checks on static size expr (", s, ")").test_eq(1, error);
}
tr.section("dynamic terms in match, dynamic rank. See frame-new.cc");
{
auto f2 = [](ra::Big<int, 2> const & a) { cout << ra::start(shape(a)) << endl; };
int error = 0;
std::string s;
try {
ra::Big<int> a {};
// flag the error when casting rank-0 to rank-2 array. FIXME check that copying is still possible.
f2(a);
error = 0;
} catch (ra_error & e) {
error = 1;
s = e.s;
}
tr.info("dynamic size checks on static size expr (", s, ")").test_eq(1, error);
}
// ------------------------------
// see test/big-0.cc
// ------------------------------
tr.section("check rank errors with dynamic rank (I)");
{
int x = 0;
try {
ra::Big<int> a = 0;
cout << shape(a, 0) << endl;
x = 1;
} catch (ra_error & e) {
x = 2;
}
tr.info("caught error").test_eq(2, x);
}
tr.section("check rank errors with dynamic rank (II)");
{
int x = 0;
// initialization is to rank 1, size 0.
try {
ra::Big<int> a;
a = ra::Big<int, 1> { 1 };
x = 1;
} catch (ra_error & e) {
x = 2;
}
tr.info("uninitialized dynamic rank").test_eq(2, x);
}
tr.section("iffy conversions");
// won't fail until runtime bc val[0] might have size 1, but FIXME this is very likely a coding error.
// FIXME shape doesn't show in the error message as it should.
{
ra::Big<double, 2> val = { { 7, 0, 0, 0.5, 1.5, 1, 1, 1 } };
ra::Big<double, 2> bal = { { 7 }, { 0 } };
double x = bal[0];
tr.test_eq(7, x);
int error = 0;
string s;
try {
double x = val[0];
cout << "x " << x << " (!)" << endl;
} catch (ra_error & e) {
error = 1;
s = e.s;
}
tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
}
// ------------------------------
// see test/frame-old.cc
// ------------------------------
#define EXPR ra::expr(plus2double_print, a.iter(), b.iter())
tr.section("frame matching should-be-error cases. See frame-old.cc");
{
ra::Unique<double, 1> a({3}, 10);
ra::Unique<double, 1> b({4}, 1);
auto plus2double_print = [](double a, double b) { cout << (a - b) << " "; };
int error = 0;
string s;
try {
tr.info("dynamic test is needed").test_eq(1, decltype(EXPR)::check_s());
tr.test(!agree(a, b));
ply_ravel(EXPR);
} catch (ra_error & e) {
error = 1;
s = e.s;
}
tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
}
tr.section("frame matching should-be-error cases - dynamic rank. See frame-old.cc");
{
ra::Unique<double> a({3}, 10);
ra::Unique<double> b({4}, 1);
auto plus2double_print = [](double a, double b) { cout << (a - b) << " "; };
int error = 0;
string s;
try {
std::cout << "A: " << a.iter().len(0) << endl;
std::cout << "B: " << b.iter().len(0) << endl;
tr.info("dynamic test is needed").test_eq(1, decltype(EXPR)::check_s());
tr.test(!agree(a, b));
ply_ravel(EXPR);
} catch (ra_error & e) {
error = 1;
s = e.s;
}
tr.info("caught error L" STRINGIZE(__LINE__) ": ", s).test_eq(1, error);
}
#undef EXPR
return tr.summary();
}