File: test_testing.cc

package info (click to toggle)
metview 5.26.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 614,288 kB
  • sloc: cpp: 560,215; ansic: 44,633; xml: 19,933; f90: 17,905; sh: 7,269; python: 5,565; yacc: 2,318; lex: 1,372; perl: 701; makefile: 87
file content (393 lines) | stat: -rw-r--r-- 9,832 bytes parent folder | download | duplicates (2)
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include <algorithm>

#include "eckit/types/FloatCompare.h"
#include "eckit/value/Value.h"

#define ECKIT_TESTING_SELF_REGISTER_CASES 0
#include "eckit/testing/Test.h"

// Disable warnings for old-style casts in these tests. They are intentional
#ifdef __clang__
#pragma clang diagnostic ignored "-Wold-style-cast"
#endif

using namespace eckit::testing;

namespace eckit_test {  // Not eckit namespace on purpose to test downstream usage of macros

typedef std::vector<Test> Tests;

//----------------------------------------------------------------------------------------------------------------------

// These unit tests test the eckit testing framework itself.

//----------------------------------------------------------------------------------------------------------------------


// We need some global counters to keep count of some of the CASE functionality
std::vector<int> global_counters(5, 0);


void ThrowStd() {
    throw std::exception();
}


Tests tests = {{CASE("CASE with no EXPECT passes"){

               }},

               {CASE("EXPECT macros are defined correctly"){EXPECT(true);
EXPECT_NOT(false);
EXPECT_EQUAL(1, 1);
EXPECT_NOT_EQUAL(1, 2);
EXPECT_NO_THROW({ bool b = true; });
EXPECT_THROWS(throw std::exception());
EXPECT_THROWS_AS(throw std::exception(), std::exception);
EXPECT_MSG(1 == 1, [=]() { std::cerr << eckit::Colour::red << "1 != 1" << eckit::Colour::reset << std::endl; };);
}  // namespace eckit_test
}  // namespace eckit
,

    {CASE("EXPECT does not cause the test to fail on success"){

        Test pass = {CASE("P"){EXPECT(true);
}
}
;

std::vector<std::string> f;
bool ret = pass.run(TestVerbosity::Silent, f);
if (!ret || f.size() != 0) {
    throw eckit::testing::TestException("Unexpected error encountered in EXPECT", Here());
}
}
}
,

    {CASE("EXPECT causes an error to be reported on failure"){

        Test fail = {CASE("F"){EXPECT(false);
}
}
;

std::vector<std::string> f;
bool ret = fail.run(TestVerbosity::Silent, f);
if (ret || f.size() == 0) {
    throw eckit::testing::TestException("No error reported when EXPECT failed", Here());
}
if (ret || f.size() > 1) {
    throw eckit::testing::TestException("Too many errors reported when EXPECT failed", Here());
}
}
}
,

    {CASE("EXPECT succeeds for success (true) and failure (false)"){Tests pass = {{CASE("P"){EXPECT(true);
}
}
}
;
Tests fail = {{CASE("F"){EXPECT(false);
}
}
}
;

EXPECT(0 == run(pass, TestVerbosity::Silent));
EXPECT(1 == run(fail, TestVerbosity::Silent));
}
}
,

    {CASE("EXPECT succeeds for integer comparison"){EXPECT(7 == 7);
EXPECT(7 != 8);
EXPECT(7 >= 6);
EXPECT(7 <= 8);
EXPECT(7 > 6);
EXPECT(7 < 8);
EXPECT_NOT(7 == 8);
EXPECT_NOT(7 != 7);
EXPECT_NOT(7 <= 6);
EXPECT_NOT(7 >= 8);
EXPECT_NOT(7 < 6);
EXPECT_NOT(7 > 8);
}
}
, {CASE("Expect succeeds for integer vs. real comparison"){EXPECT(7.0 == 7);
EXPECT(7.0 != 8);
EXPECT(7 == 7.0);
EXPECT(7 != 8.0);

EXPECT_NOT(7.0 == 8);
EXPECT_NOT(7 != 7.0);
}
}
,

    {CASE("Expect succeeds for string comparison"){

        std::string a("a");
std::string b("b");

EXPECT(a == a);
EXPECT(a != b);
EXPECT(b >= a);
EXPECT(a <= b);
EXPECT(b > a);
EXPECT(a < b);

EXPECT_NOT(a == b);
EXPECT_NOT(a != a);
EXPECT_NOT(b <= a);
EXPECT_NOT(a >= b);
EXPECT_NOT(b < a);
EXPECT_NOT(a > b);
}
}
,

    {CASE("Expect expression RHS can use * / % + -"){EXPECT(7 == 1 * 7);
EXPECT(7 == 7 / 1);
EXPECT(0 == 7 % 1);
EXPECT(7 == 1 + 6);
EXPECT(7 == 8 - 1);
}
}
,

    {CASE("Expect expression LHS can use * / % + -"){EXPECT(1 * 7 == 7);
EXPECT(7 / 1 == 7);
EXPECT(7 % 1 == 0);
EXPECT(1 + 6 == 7);
EXPECT(8 - 1 == 7);
}
}
, {CASE("run() returns the correct failure count"){Tests pass = {{CASE("P"){EXPECT(1 == 1);
}
}
}
;
Tests fail_1 = {{CASE("F1"){EXPECT(0 == 1);
}
}
}
;
Tests fail_3 = {{CASE("F1"){EXPECT(0 == 1);
}
}
, {CASE("F2"){EXPECT(0 == 1);
}
}
, {
    CASE("F3") {
        EXPECT(0 == 1);
    }
}
}
;

EXPECT(0 == run(pass, TestVerbosity::Silent));
EXPECT(1 == run(fail_1, TestVerbosity::Silent));
EXPECT(3 == run(fail_3, TestVerbosity::Silent));
}
}
, {CASE("run() returns -1 if no CASE() is found"){Tests empty = {};
EXPECT(-1 == run(empty, TestVerbosity::Silent));
}
}
, {CASE("A fresh fixture is created for each section"){

      int i = 7;

SECTION("S1") {
    i = 42;
}
SECTION("S2") {
    EXPECT(i == 7);
}
}
}
, {CASE("Deprecated SETUP macro may be used."){

      SETUP("Context"){int i = 7;

SECTION("S1") {
    i = 42;
}
SECTION("S2") {
    EXPECT(i == 7);
}
}
}
}
, {CASE("Fixture setup runs one more time than the number of sections."){

      ++global_counters[0];

SECTION("S1") {
    ++global_counters[1];
}
SECTION("S2") {
    ++global_counters[2];
}
}
}
, {CASE("Sections run correctly inside a for loop"){

      ++global_counters[3];

for (int j = 0; j < 10; j++) {
    std::stringstream ss;
    ss << "test-" << j;
    SECTION(ss.str()) {
        ++global_counters[4];
    }
}
}
}
, {CASE("Expect runs multiple times in for-loop"){

      int i = 0;
for (int j = 0; j < 10; j++) {
    EXPECT(i++ == j);
}
EXPECT(i == 10);
}
}
, {CASE("Collections compare correctly"){

      SETUP("Create Collections"){std::vector<int> a = {1, 2, 3};
std::vector<int> b = {1, 2, 3};

SECTION("Compare Collections") {
    EXPECT(a == b);
    EXPECT(a <= b);
    EXPECT(a >= b);
    EXPECT_NOT(a != b);
    EXPECT_NOT(a > b);
    EXPECT_NOT(a < b);
    a[0] = 0;
    EXPECT_NOT(a == b);
    EXPECT_NOT(a >= b);
    EXPECT(a <= b);
    EXPECT(a < b);
    EXPECT(a != b);
    a[0] = 2;
    EXPECT_NOT(a == b);
    EXPECT_NOT(a <= b);
    EXPECT(a >= b);
    EXPECT(a > b);
    EXPECT(a != b);
}
}
}
}
, {CASE("Test comparisons of c-style arrays"){

      std::vector<int> a = {1, 2, 3};
std::vector<int> b = {1, 2, 3};
int arr[3]         = {1, 2, 3};

std::vector<std::string> s = {"1", "22", "333"};
std::string arr_s[3]       = {"1", "22", "333"};

std::vector<double> d1 = {1., 2., 3.};
std::vector<double> d2 = {1., 2., 3.};

// Check basic comparisons and initializations
EXPECT(make_view(a.data(), a.data() + 3) == make_view(b.data(), b.data() + 3));
EXPECT(make_view(a.data(), 3) == make_view(b.data(), 3));
EXPECT(make_view(a.data(), a.data() + 3) == make_view(arr, arr + 3));
EXPECT(make_view(a.data(), 3) == make_view(arr, 3));
EXPECT(make_view(a) == make_view(arr, arr + 3));
EXPECT(make_view(s) == make_view(arr_s, arr_s + 3));
EXPECT(make_view(s.data(), s.data() + 3) == make_view(arr_s, arr_s + 3));
EXPECT(make_view(a) == make_view(b.data(), b.data() + 3));
EXPECT(make_view(a) == make_view(b));
EXPECT(make_view(d1.data(), 3) == make_view(d2.data(), 3));
EXPECT(make_view(d1.data(), 3) == make_view(d2.data(), 3));

EXPECT(make_view(a.data(), a.data() + 3).size() == 3);
EXPECT(make_view(a.data(), 3).size() == 3);
EXPECT(make_view(a).size() == a.size());
EXPECT(make_view(s).size() == s.size());

// Check sub-array comparisons
EXPECT(make_view(a.data(), a.data() + 2) == make_view(b.data(), b.data() + 2));
EXPECT(make_view(&a[1], &a[1] + 2) == make_view(&b[1], &b[1] + 2));
EXPECT(make_view(&a[1], &a[1] + 2) != make_view(b.data(), b.data() + 2));        // values not equal
EXPECT(make_view(a.data(), a.data() + 3) != make_view(b.data(), b.data() + 2));  // not same size
EXPECT(make_view(a.data(), 2) == make_view(b.data(), 2));
EXPECT(make_view(a.data(), 2) == make_view(b.data(), 2));

// Check != is the same as !(==)
EXPECT(!(make_view(a.data(), a.data() + 3) != make_view(b.data(), b.data() + 3)));
EXPECT(!(make_view(a.data(), a.data() + 3) == make_view(b.data(), b.data() + 2)));  // not same size
EXPECT(!(make_view(&a[1], &a[1] + 2) == make_view(b.data(), b.data() + 2)));        // values not equal

// Check comparisons against std::vector
EXPECT(a == make_view(b));
EXPECT(make_view(a) == b);

// Check type recognition of make_view vs explicit constructor
EXPECT(ArrayView<int>(a) == make_view(a));
EXPECT(ArrayView<std::string>(s) == make_view(s));

// Check approximately equals
d2[2] = 2.99;
EXPECT(is_approximately_equal(make_view(d1), make_view(d2), 0.02));
EXPECT(!is_approximately_equal(make_view(d1), make_view(d2), 0.0001));  // array vs array
EXPECT(!is_approximately_equal(d1, d2, 0.0001));                        // vector vs vector
EXPECT(!is_approximately_equal(make_view(d1), d2, 0.0001));             // array vs vector
EXPECT(!is_approximately_equal(d1, make_view(d2), 0.0001));             // vector vs array
}
}
,
}
;  // end of tests


//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit


using eckit_test::global_counters;


int main(int argc, char* argv[]) {

    int retval1 = 0;

    if (global_counters.size() != 5 ||
        std::any_of(global_counters.begin(), global_counters.end(), [](int a) { return a != 0; })) {
        eckit::Log::info() << "Global counters incorrectly configured" << std::endl;
        retval1 = 1;
    }

    eckit::Main::initialise(argc, argv);
    int retval2 = eckit::testing::run_tests(eckit_test::tests, argc, argv);

    int retval3 = 0;
    if (global_counters[0] != 3 || global_counters[1] != 1 || global_counters[2] != 1 || global_counters[3] != 11 ||
        global_counters[4] != 10) {

        eckit::Log::info() << "Global counters incorrect. Relevant tests FAILED" << std::endl;
        eckit::Log::info() << "Global counters: " << global_counters << std::endl;
        retval3 = 1;
    }

    return std::abs(retval1) + std::abs(retval2) + std::abs(retval3);
}