blob: 275f27ffeaec878200dc0ff9a0bf75d65db19b83 [file] [log] [blame]
Simon Glass2e7d35d2014-02-26 15:59:21 -07001/*
Joe Hershbergere721b882015-05-20 14:27:27 -05002 * Simple unit test library
Simon Glass2e7d35d2014-02-26 15:59:21 -07003 *
4 * Copyright (c) 2013 Google, Inc
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
Joe Hershbergere721b882015-05-20 14:27:27 -05009#ifndef __TEST_UT_H
10#define __TEST_UT_H
Simon Glass2e7d35d2014-02-26 15:59:21 -070011
Joe Hershbergere721b882015-05-20 14:27:27 -050012struct unit_test_state;
Simon Glass2e7d35d2014-02-26 15:59:21 -070013
14/**
15 * ut_fail() - Record failure of a unit test
16 *
Joe Hershbergere721b882015-05-20 14:27:27 -050017 * @uts: Test state
Simon Glass2e7d35d2014-02-26 15:59:21 -070018 * @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 Hershbergere721b882015-05-20 14:27:27 -050023void ut_fail(struct unit_test_state *uts, const char *fname, int line,
Simon Glass2e7d35d2014-02-26 15:59:21 -070024 const char *func, const char *cond);
25
26/**
27 * ut_failf() - Record failure of a unit test
28 *
Joe Hershbergere721b882015-05-20 14:27:27 -050029 * @uts: Test state
Simon Glass2e7d35d2014-02-26 15:59:21 -070030 * @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 Hershbergere721b882015-05-20 14:27:27 -050036void ut_failf(struct unit_test_state *uts, const char *fname, int line,
Simon Glass2e7d35d2014-02-26 15:59:21 -070037 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 Hershbergere721b882015-05-20 14:27:27 -050044 ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
Simon Glass2e7d35d2014-02-26 15:59:21 -070045 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 Hershbergere721b882015-05-20 14:27:27 -050051 ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
Simon Glass2e7d35d2014-02-26 15:59:21 -070052 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 Hershbergere721b882015-05-20 14:27:27 -050061 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass2e7d35d2014-02-26 15:59:21 -070062 #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 Hershbergere721b882015-05-20 14:27:27 -050073 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass2e7d35d2014-02-26 15:59:21 -070074 #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 Hershbergere721b882015-05-20 14:27:27 -050085 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glass2e7d35d2014-02-26 15:59:21 -070086 #expr1 " = " #expr2, \
87 "Expected %p, got %p", val1, val2); \
88 return -1; \
89 } \
90}
91
Simon Glassecc2ed52014-12-10 08:55:55 -070092/* Assert that a pointer is not NULL */
93#define ut_assertnonnull(expr) { \
94 const void *val = (expr); \
95 \
96 if (val == NULL) { \
Joe Hershbergere721b882015-05-20 14:27:27 -050097 ut_failf(uts, __FILE__, __LINE__, __func__, \
Simon Glassecc2ed52014-12-10 08:55:55 -070098 #expr " = NULL", \
99 "Expected non-null, got NULL"); \
100 return -1; \
101 } \
102}
103
Simon Glass2e7d35d2014-02-26 15:59:21 -0700104/* Assert that an operation succeeds (returns 0) */
105#define ut_assertok(cond) ut_asserteq(0, cond)
106
107#endif