Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Simple unit test library |
| 4 | * |
| 5 | * Copyright (c) 2013 Google, Inc |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Simon Glass | 400175b | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 9 | #include <console.h> |
Simon Glass | 8109863 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 10 | #include <malloc.h> |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 11 | #include <test/test.h> |
| 12 | #include <test/ut.h> |
| 13 | |
Simon Glass | 9ce8b40 | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 14 | DECLARE_GLOBAL_DATA_PTR; |
| 15 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 16 | void ut_fail(struct unit_test_state *uts, const char *fname, int line, |
| 17 | const char *func, const char *cond) |
| 18 | { |
Simon Glass | 9ce8b40 | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 19 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 20 | printf("%s:%d, %s(): %s\n", fname, line, func, cond); |
| 21 | uts->fail_count++; |
| 22 | } |
| 23 | |
| 24 | void ut_failf(struct unit_test_state *uts, const char *fname, int line, |
| 25 | const char *func, const char *cond, const char *fmt, ...) |
| 26 | { |
| 27 | va_list args; |
| 28 | |
Simon Glass | 9ce8b40 | 2015-11-08 23:47:50 -0700 | [diff] [blame] | 29 | gd->flags &= ~(GD_FLG_SILENT | GD_FLG_RECORD); |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 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'); |
| 35 | uts->fail_count++; |
| 36 | } |
Simon Glass | 8109863 | 2019-12-29 21:19:23 -0700 | [diff] [blame] | 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 | |
Simon Glass | 400175b | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 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 | |
Simon Glass | 33d7edf | 2020-07-28 19:41:10 -0600 | [diff] [blame] | 62 | int ut_check_console_linen(struct unit_test_state *uts, const char *fmt, ...) |
| 63 | { |
| 64 | va_list args; |
| 65 | |
| 66 | va_start(args, fmt); |
| 67 | vsnprintf(uts->expect_str, sizeof(uts->expect_str), fmt, args); |
| 68 | va_end(args); |
| 69 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); |
| 70 | |
| 71 | return strncmp(uts->expect_str, uts->actual_str, |
| 72 | strlen(uts->expect_str)); |
| 73 | } |
| 74 | |
| 75 | int ut_check_skipline(struct unit_test_state *uts) |
| 76 | { |
| 77 | if (!console_record_avail()) |
| 78 | return -ENFILE; |
| 79 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
Simon Glass | 400175b | 2020-01-27 08:49:56 -0700 | [diff] [blame] | 84 | int ut_check_console_end(struct unit_test_state *uts) |
| 85 | { |
| 86 | if (!console_record_avail()) |
| 87 | return 0; |
| 88 | |
| 89 | console_record_readline(uts->actual_str, sizeof(uts->actual_str)); |
| 90 | |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | int ut_check_console_dump(struct unit_test_state *uts, int total_bytes) |
| 95 | { |
| 96 | char *str = uts->actual_str; |
| 97 | int upto; |
| 98 | |
| 99 | /* Handle empty dump */ |
| 100 | if (!total_bytes) |
| 101 | return 0; |
| 102 | |
| 103 | for (upto = 0; upto < total_bytes;) { |
| 104 | int len; |
| 105 | int bytes; |
| 106 | |
| 107 | len = console_record_readline(str, sizeof(uts->actual_str)); |
| 108 | if (str[8] != ':' || str[9] != ' ') |
| 109 | return 1; |
| 110 | |
| 111 | bytes = len - 8 - 2 - 3 * 16 - 4; |
| 112 | upto += bytes; |
| 113 | } |
| 114 | |
| 115 | return upto == total_bytes ? 0 : 1; |
| 116 | } |