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