]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
2e7d35d2 | 2 | /* |
e721b882 | 3 | * Simple unit test library |
2e7d35d2 SG |
4 | * |
5 | * Copyright (c) 2013 Google, Inc | |
2e7d35d2 SG |
6 | */ |
7 | ||
8 | #include <common.h> | |
400175b0 | 9 | #include <console.h> |
8109863f | 10 | #include <malloc.h> |
ef7e2649 SG |
11 | #ifdef CONFIG_SANDBOX |
12 | #include <asm/state.h> | |
13 | #endif | |
401d1c4f | 14 | #include <asm/global_data.h> |
e721b882 JH |
15 | #include <test/test.h> |
16 | #include <test/ut.h> | |
2e7d35d2 | 17 | |
9ce8b402 SG |
18 | DECLARE_GLOBAL_DATA_PTR; |
19 | ||
e721b882 | 20 | void ut_fail(struct unit_test_state *uts, const char *fname, int line, |
2e7d35d2 SG |
21 | const char *func, const char *cond) |
22 | { | |
9ce8b402 | 23 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
2e7d35d2 | 24 | printf("%s:%d, %s(): %s\n", fname, line, func, cond); |
e721b882 | 25 | uts->fail_count++; |
2e7d35d2 SG |
26 | } |
27 | ||
e721b882 | 28 | void ut_failf(struct unit_test_state *uts, const char *fname, int line, |
2e7d35d2 SG |
29 | const char *func, const char *cond, const char *fmt, ...) |
30 | { | |
31 | va_list args; | |
32 | ||
9ce8b402 | 33 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
2e7d35d2 SG |
34 | printf("%s:%d, %s(): %s: ", fname, line, func, cond); |
35 | va_start(args, fmt); | |
36 | vprintf(fmt, args); | |
37 | va_end(args); | |
38 | putc('\n'); | |
e721b882 | 39 | uts->fail_count++; |
2e7d35d2 | 40 | } |
8109863f SG |
41 | |
42 | ulong ut_check_free(void) | |
43 | { | |
44 | struct mallinfo info = mallinfo(); | |
45 | ||
46 | return info.uordblks; | |
47 | } | |
48 | ||
49 | long ut_check_delta(ulong last) | |
50 | { | |
51 | return ut_check_free() - last; | |
52 | } | |
53 | ||
400175b0 SG |
54 | int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) |
55 | { | |
56 | va_list args; | |
57 | ||
58 | va_start(args, fmt); | |
59 | vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); | |
60 | va_end(args); | |
61 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); | |
62 | ||
63 | return strcmp(uts->expect_str, uts->actual_str); | |
64 | } | |
65 | ||
33d7edfd SG |
66 | int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...) |
67 | { | |
68 | va_list args; | |
69 | ||
70 | va_start(args, fmt); | |
71 | vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); | |
72 | va_end(args); | |
73 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); | |
74 | ||
75 | return strncmp(uts->expect_str, uts->actual_str, | |
76 | strlen(uts->expect_str)); | |
77 | } | |
78 | ||
79 | int ut_check_skipline(struct unit_test_state *uts) | |
80 | { | |
81 | if (!console_record_avail()) | |
82 | return -ENFILE; | |
83 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); | |
84 | ||
85 | return 0; | |
86 | } | |
87 | ||
400175b0 SG |
88 | int ut_check_console_end(struct unit_test_state *uts) |
89 | { | |
90 | if (!console_record_avail()) | |
91 | return 0; | |
92 | ||
93 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); | |
94 | ||
95 | return 1; | |
96 | } | |
97 | ||
98 | int ut_check_console_dump(struct unit_test_state *uts, int total_bytes) | |
99 | { | |
100 | char *str = uts->actual_str; | |
101 | int upto; | |
102 | ||
103 | /* Handle empty dump */ | |
104 | if (!total_bytes) | |
105 | return 0; | |
106 | ||
107 | for (upto = 0; upto < total_bytes;) { | |
108 | int len; | |
109 | int bytes; | |
110 | ||
111 | len = console_record_readline(str, sizeof(uts->actual_str)); | |
112 | if (str[8] != ':' || str[9] != ' ') | |
113 | return 1; | |
114 | ||
115 | bytes = len - 8 - 2 - 3 * 16 - 4; | |
116 | upto += bytes; | |
117 | } | |
118 | ||
119 | return upto == total_bytes ? 0 : 1; | |
120 | } | |
ef7e2649 SG |
121 | |
122 | void ut_silence_console(struct unit_test_state *uts) | |
123 | { | |
124 | #ifdef CONFIG_SANDBOX | |
125 | struct sandbox_state *state = state_get_current(); | |
126 | ||
127 | if (!state->show_test_output) | |
128 | gd->flags |= GD_FLG_SILENT; | |
129 | #endif | |
130 | } | |
131 | ||
132 | void ut_unsilence_console(struct unit_test_state *uts) | |
133 | { | |
134 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); | |
135 | } |