]>
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> |
e721b882 JH |
11 | #include <test/test.h> |
12 | #include <test/ut.h> | |
2e7d35d2 | 13 | |
9ce8b402 SG |
14 | DECLARE_GLOBAL_DATA_PTR; |
15 | ||
e721b882 | 16 | void ut_fail(struct unit_test_state *uts, const char *fname, int line, |
2e7d35d2 SG |
17 | const char *func, const char *cond) |
18 | { | |
9ce8b402 | 19 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
2e7d35d2 | 20 | printf("%s:%d, %s(): %s\n", fname, line, func, cond); |
e721b882 | 21 | uts->fail_count++; |
2e7d35d2 SG |
22 | } |
23 | ||
e721b882 | 24 | void ut_failf(struct unit_test_state *uts, const char *fname, int line, |
2e7d35d2 SG |
25 | const char *func, const char *cond, const char *fmt, ...) |
26 | { | |
27 | va_list args; | |
28 | ||
9ce8b402 | 29 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
2e7d35d2 SG |
30 | printf("%s:%d, %s(): %s: ", fname, line, func, cond); |
31 | va_start(args, fmt); | |
32 | vprintf(fmt, args); | |
33 | va_end(args); | |
34 | putc('\n'); | |
e721b882 | 35 | uts->fail_count++; |
2e7d35d2 | 36 | } |
8109863f SG |
37 | |
38 | ulong ut_check_free(void) | |
39 | { | |
40 | struct mallinfo info = mallinfo(); | |
41 | ||
42 | return info.uordblks; | |
43 | } | |
44 | ||
45 | long ut_check_delta(ulong last) | |
46 | { | |
47 | return ut_check_free() - last; | |
48 | } | |
49 | ||
400175b0 SG |
50 | int ut_check_console_line(struct unit_test_state *uts, const char *fmt, ...) |
51 | { | |
52 | va_list args; | |
53 | ||
54 | va_start(args, fmt); | |
55 | vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); | |
56 | va_end(args); | |
57 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); | |
58 | ||
59 | return strcmp(uts->expect_str, uts->actual_str); | |
60 | } | |
61 | ||
62 | int ut_check_console_end(struct unit_test_state *uts) | |
63 | { | |
64 | if (!console_record_avail()) | |
65 | return 0; | |
66 | ||
67 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); | |
68 | ||
69 | return 1; | |
70 | } | |
71 | ||
72 | int ut_check_console_dump(struct unit_test_state *uts, int total_bytes) | |
73 | { | |
74 | char *str = uts->actual_str; | |
75 | int upto; | |
76 | ||
77 | /* Handle empty dump */ | |
78 | if (!total_bytes) | |
79 | return 0; | |
80 | ||
81 | for (upto = 0; upto < total_bytes;) { | |
82 | int len; | |
83 | int bytes; | |
84 | ||
85 | len = console_record_readline(str, sizeof(uts->actual_str)); | |
86 | if (str[8] != ':' || str[9] != ' ') | |
87 | return 1; | |
88 | ||
89 | bytes = len - 8 - 2 - 3 * 16 - 4; | |
90 | upto += bytes; | |
91 | } | |
92 | ||
93 | return upto == total_bytes ? 0 : 1; | |
94 | } |