1 // SPDX-License-Identifier: GPL-2.0+
3 * Simple unit test library
5 * Copyright (c) 2013 Google, Inc
11 #include <asm/state.h>
13 #include <asm/global_data.h>
14 #include <test/test.h>
17 DECLARE_GLOBAL_DATA_PTR;
19 void ut_fail(struct unit_test_state *uts, const char *fname, int line,
20 const char *func, const char *cond)
22 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
23 printf("%s:%d, %s(): %s\n", fname, line, func, cond);
27 void ut_failf(struct unit_test_state *uts, const char *fname, int line,
28 const char *func, const char *cond, const char *fmt, ...)
32 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
33 printf("%s:%d, %s(): %s: ", fname, line, func, cond);
41 ulong ut_check_free(void)
43 struct mallinfo info = mallinfo();
48 long ut_check_delta(ulong last)
50 return ut_check_free() - last;
53 static int readline_check(struct unit_test_state *uts)
57 ret = console_record_readline(uts->actual_str, sizeof(uts->actual_str));
59 ut_fail(uts, __FILE__, __LINE__, __func__,
60 "Console record buffer too small - increase CONFIG_CONSOLE_RECORD_OUT_SIZE");
62 } else if (ret == -ENOENT) {
63 strcpy(uts->actual_str, "<no-more-output>");
69 int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
76 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
78 if (len >= sizeof(uts->expect_str)) {
79 ut_fail(uts, __FILE__, __LINE__, __func__,
80 "unit_test_state->expect_str too small");
83 ret = readline_check(uts);
87 return strcmp(uts->expect_str, uts->actual_str);
90 int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
97 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
99 if (len >= sizeof(uts->expect_str)) {
100 ut_fail(uts, __FILE__, __LINE__, __func__,
101 "unit_test_state->expect_str too small");
104 ret = readline_check(uts);
108 return strncmp(uts->expect_str, uts->actual_str,
109 strlen(uts->expect_str));
112 int ut_check_skipline(struct unit_test_state *uts)
116 if (!console_record_avail())
118 ret = readline_check(uts);
125 int ut_check_skip_to_linen(struct unit_test_state *uts, const char *fmt, ...)
132 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
134 if (len >= sizeof(uts->expect_str)) {
135 ut_fail(uts, __FILE__, __LINE__, __func__,
136 "unit_test_state->expect_str too small");
140 if (!console_record_avail())
142 ret = readline_check(uts);
146 if (!strncmp(uts->expect_str, uts->actual_str,
147 strlen(uts->expect_str)))
152 int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...)
159 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
161 if (len >= sizeof(uts->expect_str)) {
162 ut_fail(uts, __FILE__, __LINE__, __func__,
163 "unit_test_state->expect_str too small");
167 if (!console_record_avail())
169 ret = readline_check(uts);
173 if (!strcmp(uts->expect_str, uts->actual_str))
178 int ut_check_console_end(struct unit_test_state *uts)
182 if (!console_record_avail())
184 ret = readline_check(uts);
191 int ut_check_console_dump(struct unit_test_state *uts, int total_bytes)
193 char *str = uts->actual_str;
196 /* Handle empty dump */
200 for (upto = 0; upto < total_bytes;) {
204 len = console_record_readline(str, sizeof(uts->actual_str));
205 if (str[8] != ':' || str[9] != ' ')
208 bytes = len - 8 - 2 - 3 * 16 - 2;
212 return upto == total_bytes ? 0 : 1;
215 void ut_silence_console(struct unit_test_state *uts)
217 #ifdef CONFIG_SANDBOX
218 struct sandbox_state *state = state_get_current();
220 if (!state->show_test_output)
221 gd->flags |= GD_FLG_SILENT;
225 void ut_unsilence_console(struct unit_test_state *uts)
227 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
230 void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays)
232 #ifdef CONFIG_SANDBOX
233 state_set_skip_delays(skip_delays);