Simon Glass | b2c1cac | 2014-02-26 15:59:21 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Simple unit test library for driver model |
| 3 | * |
| 4 | * Copyright (c) 2013 Google, Inc |
| 5 | * |
| 6 | * SPDX-License-Identifier: GPL-2.0+ |
| 7 | */ |
| 8 | |
| 9 | #ifndef __DM_UT_H |
| 10 | #define __DM_UT_H |
| 11 | |
| 12 | struct dm_test_state; |
| 13 | |
| 14 | /** |
| 15 | * ut_fail() - Record failure of a unit test |
| 16 | * |
| 17 | * @dms: Test state |
| 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 | */ |
| 23 | void ut_fail(struct dm_test_state *dms, const char *fname, int line, |
| 24 | const char *func, const char *cond); |
| 25 | |
| 26 | /** |
| 27 | * ut_failf() - Record failure of a unit test |
| 28 | * |
| 29 | * @dms: Test state |
| 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 | */ |
| 36 | void ut_failf(struct dm_test_state *dms, const char *fname, int line, |
| 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)) { \ |
| 44 | ut_fail(dms, __FILE__, __LINE__, __func__, #cond); \ |
| 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)) { \ |
| 51 | ut_failf(dms, __FILE__, __LINE__, __func__, #cond, \ |
| 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) { \ |
| 61 | ut_failf(dms, __FILE__, __LINE__, __func__, \ |
| 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)) { \ |
| 73 | ut_failf(dms, __FILE__, __LINE__, __func__, \ |
| 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) { \ |
| 85 | ut_failf(dms, __FILE__, __LINE__, __func__, \ |
| 86 | #expr1 " = " #expr2, \ |
| 87 | "Expected %p, got %p", val1, val2); \ |
| 88 | return -1; \ |
| 89 | } \ |
| 90 | } |
| 91 | |
| 92 | /* Assert that an operation succeeds (returns 0) */ |
| 93 | #define ut_assertok(cond) ut_asserteq(0, cond) |
| 94 | |
| 95 | #endif |