1 // SPDX-License-Identifier: GPL-2.0+
3 * Simple unit test library
5 * Copyright (c) 2013 Google, Inc
12 #include <asm/state.h>
14 #include <asm/global_data.h>
15 #include <test/test.h>
18 DECLARE_GLOBAL_DATA_PTR;
20 void ut_fail(struct unit_test_state *uts, const char *fname, int line,
21 const char *func, const char *cond)
23 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
24 printf("%s:%d, %s(): %s\n", fname, line, func, cond);
28 void ut_failf(struct unit_test_state *uts, const char *fname, int line,
29 const char *func, const char *cond, const char *fmt, ...)
33 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
34 printf("%s:%d, %s(): %s: ", fname, line, func, cond);
42 ulong ut_check_free(void)
44 struct mallinfo info = mallinfo();
49 long ut_check_delta(ulong last)
51 return ut_check_free() - last;
54 static int readline_check(struct unit_test_state *uts)
58 ret = console_record_readline(uts->actual_str, sizeof(uts->actual_str));
60 ut_fail(uts, __FILE__, __LINE__, __func__,
61 "Console record buffer too small - increase CONFIG_CONSOLE_RECORD_OUT_SIZE");
68 int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...)
75 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
77 if (len >= sizeof(uts->expect_str)) {
78 ut_fail(uts, __FILE__, __LINE__, __func__,
79 "unit_test_state->expect_str too small");
82 ret = readline_check(uts);
86 return strcmp(uts->expect_str, uts->actual_str);
89 int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...)
96 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
98 if (len >= sizeof(uts->expect_str)) {
99 ut_fail(uts, __FILE__, __LINE__, __func__,
100 "unit_test_state->expect_str too small");
103 ret = readline_check(uts);
107 return strncmp(uts->expect_str, uts->actual_str,
108 strlen(uts->expect_str));
111 int ut_check_skipline(struct unit_test_state *uts)
115 if (!console_record_avail())
117 ret = readline_check(uts);
124 int ut_check_skip_to_line(struct unit_test_state *uts, const char *fmt, ...)
131 len = vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args);
133 if (len >= sizeof(uts->expect_str)) {
134 ut_fail(uts, __FILE__, __LINE__, __func__,
135 "unit_test_state->expect_str too small");
139 if (!console_record_avail())
141 ret = readline_check(uts);
145 if (!strcmp(uts->expect_str, uts->actual_str))
150 int ut_check_console_end(struct unit_test_state *uts)
154 if (!console_record_avail())
156 ret = readline_check(uts);
163 int ut_check_console_dump(struct unit_test_state *uts, int total_bytes)
165 char *str = uts->actual_str;
168 /* Handle empty dump */
172 for (upto = 0; upto < total_bytes;) {
176 len = console_record_readline(str, sizeof(uts->actual_str));
177 if (str[8] != ':' || str[9] != ' ')
180 bytes = len - 8 - 2 - 3 * 16 - 2;
184 return upto == total_bytes ? 0 : 1;
187 void ut_silence_console(struct unit_test_state *uts)
189 #ifdef CONFIG_SANDBOX
190 struct sandbox_state *state = state_get_current();
192 if (!state->show_test_output)
193 gd->flags |= GD_FLG_SILENT;
197 void ut_unsilence_console(struct unit_test_state *uts)
199 gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD);
202 void ut_set_skip_delays(struct unit_test_state *uts, bool skip_delays)
204 #ifdef CONFIG_SANDBOX
205 state_set_skip_delays(skip_delays);