2018-05-07 05:58:06 +08:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
2014-02-27 06:59:21 +08:00
|
|
|
/*
|
2015-05-21 03:27:27 +08:00
|
|
|
* Simple unit test library
|
2014-02-27 06:59:21 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) 2013 Google, Inc
|
|
|
|
*/
|
|
|
|
|
2015-05-21 03:27:27 +08:00
|
|
|
#ifndef __TEST_UT_H
|
|
|
|
#define __TEST_UT_H
|
2014-02-27 06:59:21 +08:00
|
|
|
|
2015-07-07 02:54:37 +08:00
|
|
|
#include <linux/err.h>
|
|
|
|
|
2015-05-21 03:27:27 +08:00
|
|
|
struct unit_test_state;
|
2014-02-27 06:59:21 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* ut_fail() - Record failure of a unit test
|
|
|
|
*
|
2015-05-21 03:27:27 +08:00
|
|
|
* @uts: Test state
|
2016-05-01 10:18:00 +08:00
|
|
|
* @fname: Filename where the error occurred
|
|
|
|
* @line: Line number where the error occurred
|
|
|
|
* @func: Function name where the error occurred
|
2014-02-27 06:59:21 +08:00
|
|
|
* @cond: The condition that failed
|
|
|
|
*/
|
2015-05-21 03:27:27 +08:00
|
|
|
void ut_fail(struct unit_test_state *uts, const char *fname, int line,
|
2014-02-27 06:59:21 +08:00
|
|
|
const char *func, const char *cond);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ut_failf() - Record failure of a unit test
|
|
|
|
*
|
2015-05-21 03:27:27 +08:00
|
|
|
* @uts: Test state
|
2016-05-01 10:18:00 +08:00
|
|
|
* @fname: Filename where the error occurred
|
|
|
|
* @line: Line number where the error occurred
|
|
|
|
* @func: Function name where the error occurred
|
2014-02-27 06:59:21 +08:00
|
|
|
* @cond: The condition that failed
|
|
|
|
* @fmt: printf() format string for the error, followed by args
|
|
|
|
*/
|
2015-05-21 03:27:27 +08:00
|
|
|
void ut_failf(struct unit_test_state *uts, const char *fname, int line,
|
2014-02-27 06:59:21 +08:00
|
|
|
const char *func, const char *cond, const char *fmt, ...)
|
|
|
|
__attribute__ ((format (__printf__, 6, 7)));
|
|
|
|
|
|
|
|
|
|
|
|
/* Assert that a condition is non-zero */
|
|
|
|
#define ut_assert(cond) \
|
|
|
|
if (!(cond)) { \
|
2015-05-21 03:27:27 +08:00
|
|
|
ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \
|
2015-05-21 03:27:34 +08:00
|
|
|
return CMD_RET_FAILURE; \
|
2014-02-27 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Assert that a condition is non-zero, with printf() string */
|
|
|
|
#define ut_assertf(cond, fmt, args...) \
|
|
|
|
if (!(cond)) { \
|
2015-05-21 03:27:27 +08:00
|
|
|
ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \
|
2014-02-27 06:59:21 +08:00
|
|
|
fmt, ##args); \
|
2015-05-21 03:27:34 +08:00
|
|
|
return CMD_RET_FAILURE; \
|
2014-02-27 06:59:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Assert that two int expressions are equal */
|
|
|
|
#define ut_asserteq(expr1, expr2) { \
|
2020-01-27 23:49:41 +08:00
|
|
|
unsigned int _val1 = (expr1), _val2 = (expr2); \
|
2014-02-27 06:59:21 +08:00
|
|
|
\
|
2020-01-27 23:49:41 +08:00
|
|
|
if (_val1 != _val2) { \
|
2015-05-21 03:27:27 +08:00
|
|
|
ut_failf(uts, __FILE__, __LINE__, __func__, \
|
2014-02-27 06:59:21 +08:00
|
|
|
#expr1 " == " #expr2, \
|
2020-01-27 23:49:41 +08:00
|
|
|
"Expected %#x (%d), got %#x (%d)", \
|
|
|
|
_val1, _val1, _val2, _val2); \
|
2015-05-21 03:27:34 +08:00
|
|
|
return CMD_RET_FAILURE; \
|
2014-02-27 06:59:21 +08:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assert that two string expressions are equal */
|
|
|
|
#define ut_asserteq_str(expr1, expr2) { \
|
2020-01-27 23:49:41 +08:00
|
|
|
const char *_val1 = (expr1), *_val2 = (expr2); \
|
2014-02-27 06:59:21 +08:00
|
|
|
\
|
2020-01-27 23:49:41 +08:00
|
|
|
if (strcmp(_val1, _val2)) { \
|
2015-05-21 03:27:27 +08:00
|
|
|
ut_failf(uts, __FILE__, __LINE__, __func__, \
|
2014-02-27 06:59:21 +08:00
|
|
|
#expr1 " = " #expr2, \
|
2020-01-27 23:49:41 +08:00
|
|
|
"Expected \"%s\", got \"%s\"", _val1, _val2); \
|
2015-05-21 03:27:34 +08:00
|
|
|
return CMD_RET_FAILURE; \
|
2014-02-27 06:59:21 +08:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2018-09-27 15:19:32 +08:00
|
|
|
/* Assert that two memory areas are equal */
|
|
|
|
#define ut_asserteq_mem(expr1, expr2, len) { \
|
2020-01-27 23:49:41 +08:00
|
|
|
const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \
|
2018-09-27 15:19:32 +08:00
|
|
|
const uint __len = len; \
|
|
|
|
\
|
2020-01-27 23:49:41 +08:00
|
|
|
if (memcmp(_val1, _val2, __len)) { \
|
2018-09-27 15:19:32 +08:00
|
|
|
char __buf1[64 + 1] = "\0"; \
|
|
|
|
char __buf2[64 + 1] = "\0"; \
|
2020-01-27 23:49:41 +08:00
|
|
|
bin2hex(__buf1, _val1, min(__len, (uint)32)); \
|
|
|
|
bin2hex(__buf2, _val2, min(__len, (uint)32)); \
|
2018-09-27 15:19:32 +08:00
|
|
|
ut_failf(uts, __FILE__, __LINE__, __func__, \
|
|
|
|
#expr1 " = " #expr2, \
|
|
|
|
"Expected \"%s\", got \"%s\"", \
|
|
|
|
__buf1, __buf2); \
|
|
|
|
return CMD_RET_FAILURE; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-02-27 06:59:21 +08:00
|
|
|
/* Assert that two pointers are equal */
|
|
|
|
#define ut_asserteq_ptr(expr1, expr2) { \
|
2020-01-27 23:49:41 +08:00
|
|
|
const void *_val1 = (expr1), *_val2 = (expr2); \
|
2014-02-27 06:59:21 +08:00
|
|
|
\
|
2020-01-27 23:49:41 +08:00
|
|
|
if (_val1 != _val2) { \
|
2015-05-21 03:27:27 +08:00
|
|
|
ut_failf(uts, __FILE__, __LINE__, __func__, \
|
2014-02-27 06:59:21 +08:00
|
|
|
#expr1 " = " #expr2, \
|
2020-01-27 23:49:41 +08:00
|
|
|
"Expected %p, got %p", _val1, _val2); \
|
2015-05-21 03:27:34 +08:00
|
|
|
return CMD_RET_FAILURE; \
|
2014-02-27 06:59:21 +08:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2018-06-21 22:47:16 +08:00
|
|
|
/* Assert that a pointer is NULL */
|
|
|
|
#define ut_assertnull(expr) { \
|
2020-01-27 23:49:41 +08:00
|
|
|
const void *_val = (expr); \
|
2018-06-21 22:47:16 +08:00
|
|
|
\
|
2020-01-27 23:49:41 +08:00
|
|
|
if (_val) { \
|
2018-06-21 22:47:16 +08:00
|
|
|
ut_failf(uts, __FILE__, __LINE__, __func__, \
|
|
|
|
#expr " != NULL", \
|
2020-01-27 23:49:41 +08:00
|
|
|
"Expected NULL, got %p", _val); \
|
2018-06-21 22:47:16 +08:00
|
|
|
return CMD_RET_FAILURE; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-12-10 23:55:55 +08:00
|
|
|
/* Assert that a pointer is not NULL */
|
|
|
|
#define ut_assertnonnull(expr) { \
|
2020-01-27 23:49:41 +08:00
|
|
|
const void *_val = (expr); \
|
2014-12-10 23:55:55 +08:00
|
|
|
\
|
2020-01-27 23:49:41 +08:00
|
|
|
if (!_val) { \
|
2015-05-21 03:27:27 +08:00
|
|
|
ut_failf(uts, __FILE__, __LINE__, __func__, \
|
2014-12-10 23:55:55 +08:00
|
|
|
#expr " = NULL", \
|
|
|
|
"Expected non-null, got NULL"); \
|
2015-05-21 03:27:34 +08:00
|
|
|
return CMD_RET_FAILURE; \
|
2014-12-10 23:55:55 +08:00
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2015-07-07 02:54:37 +08:00
|
|
|
/* Assert that a pointer is not an error pointer */
|
2017-05-19 10:10:00 +08:00
|
|
|
#define ut_assertok_ptr(expr) { \
|
2020-01-27 23:49:41 +08:00
|
|
|
const void *_val = (expr); \
|
2015-07-07 02:54:37 +08:00
|
|
|
\
|
2020-01-27 23:49:41 +08:00
|
|
|
if (IS_ERR(_val)) { \
|
2015-07-07 02:54:37 +08:00
|
|
|
ut_failf(uts, __FILE__, __LINE__, __func__, \
|
|
|
|
#expr " = NULL", \
|
|
|
|
"Expected pointer, got error %ld", \
|
2020-01-27 23:49:41 +08:00
|
|
|
PTR_ERR(_val)); \
|
2015-07-07 02:54:37 +08:00
|
|
|
return CMD_RET_FAILURE; \
|
|
|
|
} \
|
|
|
|
}
|
|
|
|
|
2014-02-27 06:59:21 +08:00
|
|
|
/* Assert that an operation succeeds (returns 0) */
|
|
|
|
#define ut_assertok(cond) ut_asserteq(0, cond)
|
|
|
|
|
2019-12-30 12:19:23 +08:00
|
|
|
/**
|
|
|
|
* ut_check_free() - Return the number of bytes free in the malloc() pool
|
|
|
|
*
|
|
|
|
* @return bytes free
|
|
|
|
*/
|
|
|
|
ulong ut_check_free(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ut_check_delta() - Return the number of bytes allocated/freed
|
|
|
|
*
|
|
|
|
* @last: Last value from ut_check_free
|
|
|
|
* @return free memory delta from @last; positive means more memory has been
|
|
|
|
* allocated, negative means less has been allocated (i.e. some is freed)
|
|
|
|
*/
|
|
|
|
long ut_check_delta(ulong last);
|
|
|
|
|
2014-02-27 06:59:21 +08:00
|
|
|
#endif
|