2222#ifndef WOLFMQTT_UNIT_TEST_H
2323#define WOLFMQTT_UNIT_TEST_H
2424
25- #include <stdio.h>
25+ #include "wolfmqtt/mqtt_types.h"
2626#include <stdlib.h>
2727#include <string.h>
2828
@@ -98,8 +98,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
9898#define ASSERT_TRUE (cond ) \
9999 do { \
100100 if (!(cond)) { \
101- printf (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
102- "ASSERT_TRUE(" #cond ") failed\n ", __FILE__, __LINE__); \
101+ PRINTF (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
102+ "ASSERT_TRUE(" #cond ") failed", __FILE__, __LINE__); \
103103 ut_current_test_failed = 1; \
104104 return; \
105105 } \
@@ -108,8 +108,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
108108#define ASSERT_FALSE (cond ) \
109109 do { \
110110 if (cond) { \
111- printf (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
112- "ASSERT_FALSE(" #cond ") failed\n ", __FILE__, __LINE__); \
111+ PRINTF (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
112+ "ASSERT_FALSE(" #cond ") failed", __FILE__, __LINE__); \
113113 ut_current_test_failed = 1; \
114114 return; \
115115 } \
@@ -120,9 +120,9 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
120120 long long _exp = (long long)(expected); \
121121 long long _act = (long long)(actual); \
122122 if (_exp != _act) { \
123- printf (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
123+ PRINTF (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
124124 "ASSERT_EQ(" #expected ", " #actual ") failed: " \
125- "expected %lld, got %lld\n ", __FILE__, __LINE__, _exp, _act); \
125+ "expected %lld, got %lld", __FILE__, __LINE__, _exp, _act); \
126126 ut_current_test_failed = 1; \
127127 return; \
128128 } \
@@ -133,8 +133,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
133133 long long _a = (long long)(a); \
134134 long long _b = (long long)(b); \
135135 if (_a == _b) { \
136- printf (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
137- "ASSERT_NE(" #a ", " #b ") failed: both are %lld\n ", \
136+ PRINTF (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
137+ "ASSERT_NE(" #a ", " #b ") failed: both are %lld", \
138138 __FILE__, __LINE__, _a); \
139139 ut_current_test_failed = 1; \
140140 return; \
@@ -144,8 +144,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
144144#define ASSERT_NULL (ptr ) \
145145 do { \
146146 if ((ptr) != NULL) { \
147- printf (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
148- "ASSERT_NULL(" #ptr ") failed: pointer is not NULL\n ", \
147+ PRINTF (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
148+ "ASSERT_NULL(" #ptr ") failed: pointer is not NULL", \
149149 __FILE__, __LINE__); \
150150 ut_current_test_failed = 1; \
151151 return; \
@@ -155,8 +155,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
155155#define ASSERT_NOT_NULL (ptr ) \
156156 do { \
157157 if ((ptr) == NULL) { \
158- printf (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
159- "ASSERT_NOT_NULL(" #ptr ") failed: pointer is NULL\n ", \
158+ PRINTF (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
159+ "ASSERT_NOT_NULL(" #ptr ") failed: pointer is NULL", \
160160 __FILE__, __LINE__); \
161161 ut_current_test_failed = 1; \
162162 return; \
@@ -168,9 +168,9 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
168168 const char* _exp = (expected); \
169169 const char* _act = (actual); \
170170 if (_exp == NULL || _act == NULL || strcmp(_exp, _act) != 0) { \
171- printf (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
171+ PRINTF (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
172172 "ASSERT_STR_EQ(" #expected ", " #actual ") failed: " \
173- "expected \"%s\", got \"%s\"\n ", __FILE__, __LINE__, \
173+ "expected \"%s\", got \"%s\"", __FILE__, __LINE__, \
174174 _exp ? _exp : "(null)", _act ? _act : "(null)"); \
175175 ut_current_test_failed = 1; \
176176 return; \
@@ -183,8 +183,8 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
183183 const void* _act = (actual); \
184184 size_t _size = (size); \
185185 if (_exp == NULL || _act == NULL || memcmp(_exp, _act, _size) != 0) { \
186- printf (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
187- "ASSERT_MEM_EQ(" #expected ", " #actual ", %zu) failed\n ", \
186+ PRINTF (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: " \
187+ "ASSERT_MEM_EQ(" #expected ", " #actual ", %zu) failed", \
188188 __FILE__, __LINE__, _size); \
189189 ut_current_test_failed = 1; \
190190 return; \
@@ -193,7 +193,7 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
193193
194194#define FAIL (msg ) \
195195 do { \
196- printf (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: %s\n ", \
196+ PRINTF (" " UT_COLOR_RED "FAIL" UT_COLOR_RESET ": %s:%d: %s", \
197197 __FILE__, __LINE__, (msg)); \
198198 ut_current_test_failed = 1; \
199199 return; \
@@ -215,10 +215,10 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
215215 if (ut_suite_teardown) ut_suite_teardown(); \
216216 if (ut_current_test_failed) { \
217217 ut_suite_fail_count++; \
218- printf (" " UT_COLOR_RED "[FAIL]" UT_COLOR_RESET " %s\n ", #name); \
218+ PRINTF (" " UT_COLOR_RED "[FAIL]" UT_COLOR_RESET " %s", #name); \
219219 } else { \
220220 ut_suite_pass_count++; \
221- printf (" " UT_COLOR_GREEN "[PASS]" UT_COLOR_RESET " %s\n ", #name); \
221+ PRINTF (" " UT_COLOR_GREEN "[PASS]" UT_COLOR_RESET " %s", #name); \
222222 } \
223223 } while (0)
224224
@@ -230,19 +230,19 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
230230 ut_suite_name = (name); \
231231 ut_suite_setup = (setup_fn); \
232232 ut_suite_teardown = (teardown_fn); \
233- printf ("\n" UT_COLOR_CYAN "=== Test Suite: %s ===" UT_COLOR_RESET "\n" , \
233+ PRINTF ("\n" UT_COLOR_CYAN "=== Test Suite: %s ===" UT_COLOR_RESET, \
234234 ut_suite_name); \
235235 } while (0)
236236
237237/* End a test suite and report results */
238238#define TEST_SUITE_END () \
239239 do { \
240240 int total = ut_suite_pass_count + ut_suite_fail_count; \
241- printf (UT_COLOR_CYAN "--- Suite Results: %s ---" UT_COLOR_RESET "\n" , \
241+ PRINTF (UT_COLOR_CYAN "--- Suite Results: %s ---" UT_COLOR_RESET, \
242242 ut_suite_name); \
243- printf (" Passed: " UT_COLOR_GREEN "%d" UT_COLOR_RESET \
243+ PRINTF (" Passed: " UT_COLOR_GREEN "%d" UT_COLOR_RESET \
244244 ", Failed: " UT_COLOR_RED "%d" UT_COLOR_RESET \
245- ", Total: %d\n\n ", \
245+ ", Total: %d\n", \
246246 ut_suite_pass_count, ut_suite_fail_count, total); \
247247 ut_global_pass_count += ut_suite_pass_count; \
248248 ut_global_fail_count += ut_suite_fail_count; \
@@ -257,32 +257,32 @@ static int ut_current_test_failed UT_MAYBE_UNUSED;
257257 do { \
258258 ut_global_pass_count = 0; \
259259 ut_global_fail_count = 0; \
260- printf (UT_COLOR_CYAN \
260+ PRINTF (UT_COLOR_CYAN \
261261 "============================================\n" \
262262 " wolfMQTT Unit Test Runner\n" \
263263 "============================================" \
264- UT_COLOR_RESET "\n" ); \
264+ UT_COLOR_RESET); \
265265 } while (0)
266266
267267/* Report global results and return exit code */
268268#define TEST_RUNNER_END () \
269269 do { \
270270 int total = ut_global_pass_count + ut_global_fail_count; \
271- printf (UT_COLOR_CYAN \
271+ PRINTF (UT_COLOR_CYAN \
272272 "============================================\n" \
273273 " Final Results\n" \
274274 "============================================" \
275- UT_COLOR_RESET "\n" ); \
276- printf (" Total Passed: " UT_COLOR_GREEN "%d" UT_COLOR_RESET "\n" , \
275+ UT_COLOR_RESET); \
276+ PRINTF (" Total Passed: " UT_COLOR_GREEN "%d" UT_COLOR_RESET, \
277277 ut_global_pass_count); \
278- printf (" Total Failed: " UT_COLOR_RED "%d" UT_COLOR_RESET "\n" , \
278+ PRINTF (" Total Failed: " UT_COLOR_RED "%d" UT_COLOR_RESET, \
279279 ut_global_fail_count); \
280- printf (" Total Tests: %d\n ", total); \
280+ PRINTF (" Total Tests: %d", total); \
281281 if (ut_global_fail_count > 0) { \
282- printf ("\n" UT_COLOR_RED "TESTS FAILED" UT_COLOR_RESET "\n" ); \
282+ PRINTF ("\n" UT_COLOR_RED "TESTS FAILED" UT_COLOR_RESET); \
283283 return 1; \
284284 } else { \
285- printf ("\n" UT_COLOR_GREEN "ALL TESTS PASSED" UT_COLOR_RESET "\n" ); \
285+ PRINTF ("\n" UT_COLOR_GREEN "ALL TESTS PASSED" UT_COLOR_RESET); \
286286 return 0; \
287287 } \
288288 } while (0)
0 commit comments