@micro-os-plus/micro-test-plus 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,252 @@
1
+ /*
2
+ * This file is part of the µOS++ distribution.
3
+ * (https://github.com/micro-os-plus)
4
+ * Copyright (c) 2021 Liviu Ionescu.
5
+ *
6
+ * Permission to use, copy, modify, and/or distribute this software
7
+ * for any purpose is hereby granted, under the terms of the MIT license.
8
+ */
9
+
10
+ #include <micro-os-plus/micro-test-plus.h>
11
+ #include <stdio.h>
12
+ #include <cstring>
13
+
14
+ // ----------------------------------------------------------------------------
15
+
16
+ #if defined(__GNUC__)
17
+ #pragma GCC diagnostic push
18
+ #if defined(__clang__)
19
+ #pragma clang diagnostic ignored "-Wc++98-compat"
20
+ #endif
21
+ #endif
22
+
23
+ namespace micro_os_plus
24
+ {
25
+ namespace micro_test_plus
26
+ {
27
+
28
+ // ------------------------------------------------------------------------
29
+
30
+ session::session ()
31
+ {
32
+ this->argc_ = 0;
33
+ this->argv_ = nullptr;
34
+
35
+ passed_ = 0;
36
+ failed_ = 0;
37
+ test_cases_ = 0;
38
+ }
39
+
40
+ session::session (int argc, char* argv[])
41
+ {
42
+ init (argc, argv);
43
+ }
44
+
45
+ void
46
+ session::init (int argc, char* argv[])
47
+ {
48
+ this->argc_ = argc;
49
+ this->argv_ = argv;
50
+
51
+ passed_ = 0;
52
+ failed_ = 0;
53
+ test_cases_ = 0;
54
+
55
+ #if defined(__clang__)
56
+ printf ("Built with clang " __VERSION__);
57
+ #elif defined(__GNUC__)
58
+ printf ("Built with GCC " __VERSION__);
59
+ #elif defined(_MSC_VER)
60
+ // https://docs.microsoft.com/en-us/cpp/preprocessor/predefined-macros?view=msvc-170
61
+ printf ("Built with MSVC %d", _MSC_VER);
62
+ #else
63
+ printf ("Built with an unknown compiler");
64
+ #endif
65
+ #if defined(__ARM_PCS_VFP)
66
+ printf (", with FP");
67
+ #else
68
+ printf (", no FP");
69
+ #endif
70
+ #if defined(__EXCEPTIONS)
71
+ printf (", with exceptions");
72
+ #else
73
+ printf (", no exceptions");
74
+ #endif
75
+ #if defined(DEBUG)
76
+ printf (", with DEBUG");
77
+ #endif
78
+ puts (".");
79
+
80
+ #if defined(DEBUG)
81
+ printf ("argv[] = ");
82
+ for (int i = 0; i < argc; ++i)
83
+ {
84
+ printf ("'%s' ", argv[i]);
85
+ }
86
+ puts ("");
87
+ #endif
88
+ }
89
+
90
+ void
91
+ session::start_suite (const char* name)
92
+ {
93
+ name_ = name;
94
+ printf ("\n%s started\n", name);
95
+ }
96
+
97
+ void
98
+ session::run_test_case (void (*function) (session&), const char* name)
99
+ {
100
+ printf ("\n %s\n", name);
101
+
102
+ (*function) (*this);
103
+ test_cases_++;
104
+ }
105
+
106
+ #if defined(__GNUC__)
107
+ #pragma GCC diagnostic push
108
+ #pragma GCC diagnostic ignored "-Wunused-parameter"
109
+ #endif
110
+ void
111
+ session::pass (const char* message, const char* file, int line)
112
+ {
113
+ // The file name and line number are unused in this version;
114
+ // they are present only in case future versions will keep a
115
+ // log off all tests.
116
+ printf (" ✓ %s\n", message);
117
+ passed_++;
118
+ }
119
+ #if defined(__GNUC__)
120
+ #pragma GCC diagnostic pop
121
+ #endif
122
+
123
+ void
124
+ session::fail (const char* message, const char* file, int line)
125
+ {
126
+ printf (" ✗ %s", message);
127
+ print_where_ (" (in '%s:%d')", file, line);
128
+ printf ("\n");
129
+
130
+ failed_++;
131
+ }
132
+
133
+ void
134
+ session::expect_true (bool condition, const char* message,
135
+ const char* file, int line)
136
+ {
137
+ if (condition)
138
+ {
139
+ printf (" ✓ %s\n", message);
140
+ passed_++;
141
+ }
142
+ else
143
+ {
144
+ printf (" ✗ %s", message);
145
+ print_where_ (" (in '%s:%d')", file, line);
146
+ printf ("\n");
147
+ failed_++;
148
+ }
149
+ }
150
+
151
+ void
152
+ session::expect_eq (int actual, int expected, const char* message,
153
+ const char* file, int line)
154
+ {
155
+ if (actual == expected)
156
+ {
157
+ printf (" ✓ %s\n", message);
158
+ passed_++;
159
+ }
160
+ else
161
+ {
162
+ printf (" ✗ %s (expected %d, got %d", message, expected, actual);
163
+ print_where_ (", in '%s:%d'", file, line);
164
+ printf (")\n");
165
+ failed_++;
166
+ }
167
+ }
168
+
169
+ void
170
+ session::expect_eq (long actual, long expected, const char* message,
171
+ const char* file, int line)
172
+ {
173
+ if (actual == expected)
174
+ {
175
+ printf (" ✓ %s\n", message);
176
+ passed_++;
177
+ }
178
+ else
179
+ {
180
+ printf (" ✗ %s (expected %ld, got %ld", message, expected,
181
+ actual);
182
+ print_where_ (", in '%s:%d'", file, line);
183
+ printf (")\n");
184
+ failed_++;
185
+ }
186
+ }
187
+
188
+ void
189
+ session::expect_eq (const char* actual, const char* expected,
190
+ const char* message, const char* file, int line)
191
+ {
192
+ if (std::strcmp (actual, expected) == 0)
193
+ {
194
+ printf (" ✓ %s\n", message);
195
+ passed_++;
196
+ }
197
+ else
198
+ {
199
+ printf (" ✗ %s (expected '%s', got '%s'", message, expected,
200
+ actual);
201
+ print_where_ (", in '%s:%d'", file, line);
202
+ printf (")\n");
203
+ failed_++;
204
+ }
205
+ }
206
+
207
+ #if defined(__GNUC__)
208
+ #pragma GCC diagnostic push
209
+ #pragma GCC diagnostic ignored "-Wformat-nonliteral"
210
+ #endif
211
+
212
+ void
213
+ session::print_where_ (const char* format, const char* file, int line)
214
+ {
215
+ if (file != nullptr)
216
+ {
217
+ printf (format, file, line);
218
+ }
219
+ }
220
+
221
+ #if defined(__GNUC__)
222
+ #pragma GCC diagnostic pop
223
+ #endif
224
+
225
+ int
226
+ session::result (void)
227
+ {
228
+ // Also fail if none passed.
229
+ if (failed_ == 0 && passed_ != 0)
230
+ {
231
+ printf ("\n%s passed (%d tests in %d test cases)\n", name_, passed_,
232
+ test_cases_);
233
+ return 0;
234
+ }
235
+ else
236
+ {
237
+ printf (
238
+ "\n%s failed (%d tests passed, %d failed, in %d test cases)\n",
239
+ name_, passed_, failed_, test_cases_);
240
+ return 1;
241
+ }
242
+ }
243
+
244
+ // ------------------------------------------------------------------------
245
+ } // namespace micro_test_plus
246
+ } // namespace micro_os_plus
247
+
248
+ #if defined(__GNUC__)
249
+ #pragma GCC diagnostic pop
250
+ #endif
251
+
252
+ // ----------------------------------------------------------------------------