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