Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 1 | /* |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 2 | * Simple unit test library |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2013 Google, Inc |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 9 | #ifndef __TEST_UT_H |
| 10 | #define __TEST_UT_H |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 11 | |
Simon Glass | 85aeda4 | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 12 | #include <linux/err.h> |
| 13 | |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 14 | struct unit_test_state; |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * ut_fail() - Record failure of a unit test |
| 18 | * |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 19 | * @uts: Test state |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 20 | * @fname: Filename where the error occured |
| 21 | * @line: Line number where the error occured |
| 22 | * @func: Function name where the error occured |
| 23 | * @cond: The condition that failed |
| 24 | */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 25 | void ut_fail(struct unit_test_state *uts, const char *fname, int line, |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 26 | const char *func, const char *cond); |
| 27 | |
| 28 | /** |
| 29 | * ut_failf() - Record failure of a unit test |
| 30 | * |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 31 | * @uts: Test state |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 32 | * @fname: Filename where the error occured |
| 33 | * @line: Line number where the error occured |
| 34 | * @func: Function name where the error occured |
| 35 | * @cond: The condition that failed |
| 36 | * @fmt: printf() format string for the error, followed by args |
| 37 | */ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 38 | void ut_failf(struct unit_test_state *uts, const char *fname, int line, |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 39 | const char *func, const char *cond, const char *fmt, ...) |
| 40 | __attribute__ ((format (__printf__, 6, 7))); |
| 41 | |
| 42 | |
| 43 | /* Assert that a condition is non-zero */ |
| 44 | #define ut_assert(cond) \ |
| 45 | if (!(cond)) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 46 | ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 47 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* Assert that a condition is non-zero, with printf() string */ |
| 51 | #define ut_assertf(cond, fmt, args...) \ |
| 52 | if (!(cond)) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 53 | ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 54 | fmt, ##args); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 55 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* Assert that two int expressions are equal */ |
| 59 | #define ut_asserteq(expr1, expr2) { \ |
| 60 | unsigned int val1 = (expr1), val2 = (expr2); \ |
| 61 | \ |
| 62 | if (val1 != val2) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 63 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 64 | #expr1 " == " #expr2, \ |
| 65 | "Expected %d, got %d", val1, val2); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 66 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 67 | } \ |
| 68 | } |
| 69 | |
| 70 | /* Assert that two string expressions are equal */ |
| 71 | #define ut_asserteq_str(expr1, expr2) { \ |
| 72 | const char *val1 = (expr1), *val2 = (expr2); \ |
| 73 | \ |
| 74 | if (strcmp(val1, val2)) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 75 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 76 | #expr1 " = " #expr2, \ |
| 77 | "Expected \"%s\", got \"%s\"", val1, val2); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 78 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 79 | } \ |
| 80 | } |
| 81 | |
| 82 | /* Assert that two pointers are equal */ |
| 83 | #define ut_asserteq_ptr(expr1, expr2) { \ |
| 84 | const void *val1 = (expr1), *val2 = (expr2); \ |
| 85 | \ |
| 86 | if (val1 != val2) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 87 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 88 | #expr1 " = " #expr2, \ |
| 89 | "Expected %p, got %p", val1, val2); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 90 | return CMD_RET_FAILURE; \ |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 91 | } \ |
| 92 | } |
| 93 | |
Simon Glass | ecc2ed5 | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 94 | /* Assert that a pointer is not NULL */ |
| 95 | #define ut_assertnonnull(expr) { \ |
| 96 | const void *val = (expr); \ |
| 97 | \ |
| 98 | if (val == NULL) { \ |
Joe Hershberger | e721b88 | 2015-05-20 14:27:27 -0500 | [diff] [blame] | 99 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
Simon Glass | ecc2ed5 | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 100 | #expr " = NULL", \ |
| 101 | "Expected non-null, got NULL"); \ |
Joe Hershberger | fe3f6a6 | 2015-05-20 14:27:34 -0500 | [diff] [blame] | 102 | return CMD_RET_FAILURE; \ |
Simon Glass | ecc2ed5 | 2014-12-10 08:55:55 -0700 | [diff] [blame] | 103 | } \ |
| 104 | } |
| 105 | |
Simon Glass | 85aeda4 | 2015-07-06 12:54:37 -0600 | [diff] [blame] | 106 | /* Assert that a pointer is not an error pointer */ |
| 107 | #define ut_assertok_ptr(expr) { \ |
| 108 | const void *val = (expr); \ |
| 109 | \ |
| 110 | if (IS_ERR(val)) { \ |
| 111 | ut_failf(uts, __FILE__, __LINE__, __func__, \ |
| 112 | #expr " = NULL", \ |
| 113 | "Expected pointer, got error %ld", \ |
| 114 | PTR_ERR(val)); \ |
| 115 | return CMD_RET_FAILURE; \ |
| 116 | } \ |
| 117 | } |
| 118 | |
Simon Glass | 2e7d35d | 2014-02-26 15:59:21 -0700 | [diff] [blame] | 119 | /* Assert that an operation succeeds (returns 0) */ |
| 120 | #define ut_assertok(cond) ut_asserteq(0, cond) |
| 121 | |
| 122 | #endif |