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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "gate/tests.h"
#include "gate/streams.h"
#include "gate/strings.h"
#include "gate/console.h"
#include "gate/arrays.h"
typedef struct gate_test_session_class
{
gate_stream_t* stream;
gate_test_report_t reports[256];
unsigned reports_used;
gate_bool_t save_checks;
gate_linkedlist_t checks;
} gate_test_session_t;
typedef struct gate_test_history_class
{
char const* test_name;
char message[128];
gate_bool_t failed;
char const* origin;
unsigned line;
} gate_test_history_t;
static gate_test_session_t test_session = GATE_INIT_EMPTY;
static gate_test_report_t* gate_test_register(char const* unit_name)
{
gate_test_report_t* ptr = NULL;
gate_size_t unit_name_len = gate_str_length(unit_name);
gate_size_t const max_reports = sizeof(test_session.reports) / sizeof(test_session.reports[0]);
if (test_session.reports_used < max_reports)
{
ptr = &test_session.reports[test_session.reports_used];
++test_session.reports_used;
gate_mem_clear(ptr, sizeof(gate_test_report_t));
gate_str_print_text(ptr->name, sizeof(ptr->name), unit_name, unit_name_len);
ptr->test_counter = 0;
ptr->error_counter = 0;
}
else
{
ptr = &test_session.reports[max_reports - 1];
}
return ptr;
}
static gate_test_report_t* gate_test_current_unit()
{
if (test_session.reports_used == 0)
{
return gate_test_register("unnamed_unit_test");
}
return &test_session.reports[test_session.reports_used - 1];
}
static gate_stream_t* gate_test_current_stream()
{
return test_session.stream;
}
static void gate_test_print_source(gate_stream_t* stream, char const* filepath, unsigned codeline)
{
if (filepath)
{
gate_stream_print(stream,
GATE_PRINT_CSTR, filepath,
GATE_PRINT_CSTR, "(",
GATE_PRINT_UI32, (gate_uint32_t)codeline,
GATE_PRINT_CSTR, "): ",
GATE_PRINT_END
);
}
}
static void gate_test_print_message(char const* msg_type, char const* message, char const* filepath, unsigned codeline)
{
gate_stream_t* stream = gate_test_current_stream();
gate_test_print_source(stream, filepath, codeline);
gate_stream_print(stream,
GATE_PRINT_CSTR, msg_type,
GATE_PRINT_CSTR, message,
GATE_PRINT_NEWLINE,
GATE_PRINT_END);
}
void gate_test_init(gate_stream_t* stream)
{
gate_console_t* con;<--- The scope of the variable 'con' can be reduced. [+]The scope of the variable 'con' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_mem_clear(&test_session, sizeof(test_session));
test_session.reports_used = 0;
if (stream == NULL)
{
con = gate_console();
if (con)
{
test_session.stream = (gate_stream_t*)con;
}
else
{
test_session.stream = gate_nullstream();
}
}
else
{
test_session.stream = stream;
}
gate_linkedlist_create(&test_session.checks, sizeof(gate_test_history_t), NULL, NULL);
}
void gate_test_uninit()
{
gate_linkedlist_destroy(&test_session.checks);
gate_mem_clear(&test_session, sizeof(test_session));
}
void gate_test_unit_begin(char const* unit_name, char const* filepath, unsigned codeline)
{
gate_test_register(unit_name);
gate_test_print_message("TEST-BEGIN: ", unit_name, filepath, codeline);
}
static gate_bool_t volatile global_trace_on;
static gate_bool_t volatile global_history_on;
void gate_test_trace_enable(gate_bool_t on)
{
global_trace_on = on;
}
void gate_test_history_enable(gate_bool_t on)
{
global_history_on = on;
}
void gate_test_trace_message(char const* message, char const* filepath, unsigned codeline)
{
if (global_trace_on)
{
gate_test_print_message("TRACE: ", message, filepath, codeline);
}
}
void gate_test_trace_value(gate_int64_t value, char const* filepath, unsigned codeline)
{
char buffer[128] = GATE_INIT_EMPTY;
gate_str_print_int64(buffer, sizeof(buffer), value);
gate_test_trace_message(buffer, filepath, codeline);
}
static void add_test_history_entry(gate_bool_t failed, char const* message, char const* filepath, unsigned codeline)
{
gate_test_report_t* ptr = gate_test_current_unit();
gate_test_history_t entry;
entry.test_name = ptr->name;
gate_str_print_text(entry.message, sizeof(entry.message), message, gate_str_length(message));
entry.failed = failed;
entry.origin = filepath;
entry.line = codeline;
gate_linkedlist_add(&test_session.checks, &entry);
}
void gate_test_success_message(char const* message, char const* filepath, unsigned codeline)
{
if (global_trace_on || global_history_on)
{
gate_test_print_message("SUCCESS: ", message, filepath, codeline);
}
if (global_history_on)
{
add_test_history_entry(false, message, filepath, codeline);
}
}
void gate_test_error_message(char const* message, char const* filepath, unsigned codeline)
{
gate_test_print_message("ERROR: ", message, filepath, codeline);
if (global_history_on)
{
add_test_history_entry(true, message, filepath, codeline);
}
}
void gate_test_count_test()
{
gate_test_report_t* ptr = gate_test_current_unit();
if (ptr)
{
++ptr->test_counter;
}
}
void gate_test_count_error()
{
gate_test_report_t* ptr = gate_test_current_unit();
if (ptr)
{
++ptr->error_counter;
}
}
unsigned gate_test_get_reports(gate_test_report_t* target_report_array, unsigned max_reports)
{
unsigned count = (max_reports < test_session.reports_used) ? max_reports : test_session.reports_used;
gate_mem_copy(target_report_array, &test_session.reports[0], count * sizeof(gate_test_report_t));
return count;
}
static void print_history_junit(gate_stream_t* stream, gate_linkedlist_t* checks)
{
}
gate_bool_t gate_test_print_history(gate_stream_t* stream, gate_enumint_t flags)
{
if (GATE_FLAG_ENABLED(flags, GATE_TEST_HISTORY_FORMAT_JUNIT))
{
print_history_junit(stream, &test_session.checks);
return true;
}
return false;
}
void gate_test_print_reports()
{
gate_test_report_t* ptr;<--- The scope of the variable 'ptr' can be reduced. [+]The scope of the variable 'ptr' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gate_stream_t* stream = gate_test_current_stream();
unsigned ndx;
gate_uint16_t total_tests = 0;
gate_uint16_t total_errors = 0;
gate_stream_print(stream,
GATE_PRINT_CSTR, "----------------------------------------",
GATE_PRINT_NEWLINE,
GATE_PRINT_END);
for (ndx = 0; ndx != test_session.reports_used; ++ndx)
{
ptr = &test_session.reports[ndx];
total_tests += ptr->test_counter;
total_errors += ptr->error_counter;
if (ptr->error_counter == 0)
{
gate_stream_print_cstr(stream, "UNIT OK: ");
}
else
{
gate_stream_print_cstr(stream, "UNIT FAILED: ");
}
gate_stream_print(stream,
GATE_PRINT_CSTR, ptr->name,
GATE_PRINT_CSTR, ", ",
GATE_PRINT_UI16, ptr->test_counter,
GATE_PRINT_CSTR, " tests",
GATE_PRINT_END);
if (ptr->error_counter != 0)
{
gate_stream_print(stream,
GATE_PRINT_CSTR, ", ",
GATE_PRINT_UI16, ptr->error_counter,
GATE_PRINT_CSTR, " failures",
GATE_PRINT_END);
}
gate_stream_print(stream,
GATE_PRINT_NEWLINE,
GATE_PRINT_END);
}
gate_stream_print(stream,
GATE_PRINT_CSTR, "----------------------------------------",
GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "TOTAL: ",
GATE_PRINT_UI16, total_tests,
GATE_PRINT_CSTR, " tests, ",
GATE_PRINT_END);
if (total_errors == 0)
{
gate_stream_print(stream,
GATE_PRINT_CSTR, "No errors detected",
GATE_PRINT_NEWLINE,
GATE_PRINT_END);
}
else
{
gate_stream_print(stream,
GATE_PRINT_UI16, total_errors,
GATE_PRINT_CSTR, " errors detected",
GATE_PRINT_NEWLINE,
GATE_PRINT_END);
}
}
gate_bool_t gate_test_failed()
{
gate_test_report_t* ptr;<--- The scope of the variable 'ptr' can be reduced. [+]The scope of the variable 'ptr' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
unsigned ndx;
for (ndx = 0; ndx != test_session.reports_used; ++ndx)
{
ptr = &test_session.reports[ndx];
if (ptr->error_counter != 0)
{
return true;
}
}
return false;
}
gate_bool_t gate_test_cases_run(gate_test_case_t const* test_cases, gate_size_t test_cases_count)
{
gate_size_t ndx;
gate_bool_t succeeded = true;
gate_test_case_t const* ptr_case = NULL;
GATE_TEST_INIT_DEFAULT();
for (ndx = 0; ndx != test_cases_count; ++ndx)
{
ptr_case = &test_cases[ndx];<--- Variable 'ptr_case' is assigned a value that is never used.
succeeded = test_cases[ndx].test_function();
}
return !gate_test_failed();
}
static gate_bool_t is_test_case_selected(char const* tc_name, char const* const* patterns, gate_size_t patterns_count)
{
if (patterns_count == 0)
{
/* no pattern defined -> all TCs are selected */
return true;
}
else
{
gate_size_t const tc_name_len = gate_str_length(tc_name);
gate_size_t ndx;
for (ndx = 0; ndx != patterns_count; ++ndx)
{
char const* const pattern = patterns[ndx];
size_t const pattern_len = gate_str_length(pattern);
if (gate_str_like(tc_name, tc_name_len, pattern, pattern_len))
{
/* matching pattern found */
return true;
}
}
return false;
}
}
int gate_test_main(gate_test_case_t const* test_cases, gate_size_t test_cases_count,
char const* const* arguments, gate_size_t argcount)
{
gate_test_case_t selected_test_cases[256];
gate_size_t selected_test_cases_count = 0;
const gate_size_t selected_test_cases_max = sizeof(selected_test_cases) / sizeof(selected_test_cases[0]);
char const* test_patterns[256];
gate_size_t test_patterns_count = 0;
const gate_size_t test_pattern_max = sizeof(test_patterns) / sizeof(test_patterns[0]);
gate_bool_t succeeded;
gate_size_t ndx;
gate_bool_t show_help = false;
gate_bool_t enable_trace = false;
gate_bool_t list_tests = false;
gate_stream_t* con = (gate_stream_t*)gate_console();
GATE_TEST_INIT_DEFAULT();
for (ndx = 0; ndx != argcount; ++ndx)
{
char const* const arg = arguments[ndx];
if (gate_str_comp_ic(arg, "--help") == 0)
{
show_help = true;
}
else if (gate_str_comp_ic(arg, "--list") == 0)
{
list_tests = true;
}
else if (gate_str_comp_ic(arg, "--trace") == 0)
{
enable_trace = true;
}
else
{
if (test_patterns_count < test_pattern_max)
{
test_patterns[test_patterns_count] = arg;
++test_patterns_count;
}
}
}
if (show_help)
{
gate_stream_print(con,
GATE_PRINT_CSTR, "Usage:", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "testapp --help", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "\tPrint test program usage description", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "testapp --list", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "\tLists all registered test cases by their anmes", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "testapp", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "\tExecutes all registered test cases", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "testapp test_case_name1 test_case_name2", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "\tExecutes only test cases named by arguments", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "testapp --trace test_case_name1 test_case_name2", GATE_PRINT_NEWLINE,
GATE_PRINT_CSTR, "\tExecutes given test cases with trace-mode enabled", GATE_PRINT_NEWLINE,
GATE_PRINT_END);
return 0;
}
if (list_tests)
{
for (ndx = 0; ndx != test_cases_count; ++ndx)
{
gate_test_case_t const* tc = &test_cases[ndx];
gate_stream_print(con,
GATE_PRINT_CSTR, tc->name, GATE_PRINT_NEWLINE,
GATE_PRINT_END);
}
return 0;
}
for (ndx = 0; (ndx != test_cases_count) && (selected_test_cases_count < selected_test_cases_max); ++ndx)
{
gate_test_case_t const* tc = &test_cases[ndx];
if (is_test_case_selected(tc->name, test_patterns, test_patterns_count))
{
selected_test_cases[selected_test_cases_count] = test_cases[ndx];
++selected_test_cases_count;
}
}
if (enable_trace)
{
gate_test_trace_on();
}
succeeded = gate_test_cases_run(selected_test_cases, selected_test_cases_count);
gate_test_print_reports();
gate_test_uninit();
return succeeded ? 0 : 1;
}
|