[lib][unittest] spiff up the unittest lib

Roll some improvements back from zircon kernel.
This commit is contained in:
Travis Geiselbrecht
2021-03-22 22:03:22 -07:00
parent 6ea5645ca3
commit e3a43bb899
3 changed files with 344 additions and 86 deletions

View File

@@ -81,5 +81,6 @@ static int do_unittests(int argc, const console_cmd_args *argv) {
STATIC_COMMAND_START
STATIC_COMMAND("unittests", "run all unit tests", do_unittests)
STATIC_COMMAND("ut", "run all unit tests", do_unittests)
STATIC_COMMAND_END(name);

View File

@@ -1,5 +1,6 @@
/*
* Copyright (c) 2013, Google, Inc. All rights reserved
* Copyright 2016 The Fuchsia Authors
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
@@ -75,11 +76,12 @@ void unittest_set_output_function (_printf_engine_output_func fun, void *arg);
/*
* Macros to format the error string
*/
#define EXPECTED_STRING "%s\n expected "
#define UNITTEST_TRACEF(str, x...) do { \
unittest_printf(" [FAILED] \n %s:%d: " str, \
__PRETTY_FUNCTION__, __LINE__, ## x); \
} while (0)
#define EXPECTED_STRING "%s\n expected "
#define UNITTEST_FAIL_TRACEF_FORMAT "\n [FAILED]\n %s:%d: "
#define UNITTEST_FAIL_TRACEF(str, x...) \
do { \
unittest_printf(UNITTEST_FAIL_TRACEF_FORMAT str, __PRETTY_FUNCTION__, __LINE__, ##x); \
} while (0)
/*
* BEGIN_TEST_CASE and END_TEST_CASE define a function that calls
@@ -131,125 +133,217 @@ void unittest_set_output_function (_printf_engine_output_func fun, void *arg);
#define END_TEST return all_ok
/*
* Use the EXPECT_* macros to check test results.
* UTCHECK_* macros are used to check test results. Generally, one should
* prefer to use either the EXPECT_* (non-terminating) or ASSERT_*
* (terminating) forms of the macros. See below.
*
* The parameter after |term| is an optional message (const char*) to be printed
* if the check fails.
*/
#define EXPECT_EQ(expected, actual, msg) \
#define UTCHECK_EQ(expected, actual, term, msg) \
{ \
typeof(actual) _e = expected; \
typeof(actual) _a = actual; \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e != _a) { \
UNITTEST_TRACEF (EXPECTED_STRING "%s (%d), " \
"actual %s (%d)\n", \
msg, #expected, (int)_e, #actual, (int)_a); \
UNITTEST_FAIL_TRACEF (EXPECTED_STRING "%s (%ld), " \
"actual %s (%ld)\n", \
msg, #expected, (long)_e, #actual, (long)_a); \
if (term) { \
return false; \
} \
all_ok = false; \
} \
}
#define EXPECT_NEQ(expected, actual, msg) \
#define UTCHECK_NE(expected, actual, term, msg) \
{ \
const typeof(expected) _e = expected; \
if (_e == actual) { \
UNITTEST_TRACEF(EXPECTED_STRING "%s (%d), %s" \
" to differ, but they are the same %d\n", \
msg, #expected, (int)_e, #actual); \
const typeof(actual) _a = actual; \
if (_e == _a) { \
UNITTEST_FAIL_TRACEF(EXPECTED_STRING "%s (%ld), %s" \
" to differ, but they are the same\n", \
msg, #expected, (long)_e, #actual); \
if (term) { \
return false; \
} \
all_ok = false; \
} \
}
#define EXPECT_LE(expected, actual, msg) \
#define UTCHECK_LE(expected, actual, term, msg) \
{ \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e > _a) { \
UNITTEST_TRACEF(EXPECTED_STRING "%s (%d) to be" \
" less-than-or-equal-to actual %s (%d)\n", \
msg, #expected, (int)_e, #actual, (int)_a); \
UNITTEST_FAIL_TRACEF(EXPECTED_STRING "%s (%ld) to be" \
" less-than-or-equal-to actual %s (%ld)\n", \
msg, #expected, (long)_e, #actual, (long)_a); \
if (term) { \
return false; \
} \
all_ok = false; \
} \
}
#define EXPECT_LT(expected, actual, msg) \
{ \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e >= _a) { \
UNITTEST_TRACEF(EXPECTED_STRING "%s (%d) to be" \
" less-than actual %s (%d)\n", \
msg, #expected, (int)_e, #actual, (int)_a); \
all_ok = false; \
} \
#define UTCHECK_LT(expected, actual, term, msg) \
{ \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e >= _a) { \
UNITTEST_FAIL_TRACEF(EXPECTED_STRING "%s (%ld) to be" \
" less-than actual %s (%ld)\n", \
msg, #expected, (long)_e, #actual, (long)_a); \
if (term) { \
return false; \
} \
all_ok = false; \
} \
}
#define EXPECT_GE(expected, actual, msg) \
{ \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e < _a) { \
UNITTEST_TRACEF(EXPECTED_STRING "%s (%d) to be" \
" greater-than-or-equal-to actual %s (%d)\n", \
msg, #expected, (int)_e, #actual, (int)_a); \
all_ok = false; \
} \
#define UTCHECK_GE(expected, actual, term, msg) \
{ \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e < _a) { \
UNITTEST_FAIL_TRACEF(EXPECTED_STRING "%s (%ld) to be" \
" greater-than-or-equal-to actual %s (%ld)\n", \
msg, #expected, (long)_e, #actual, (long)_a); \
if (term) { \
return false; \
} \
all_ok = false; \
} \
}
#define EXPECT_GT(expected, actual, msg) \
{ \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e <= _a) { \
UNITTEST_TRACEF(EXPECTED_STRING "%s (%d) to be" \
" greater-than actual %s (%d)\n", \
msg, #expected, (int)_e, #actual, (int)_a); \
all_ok = false; \
} \
#define UTCHECK_GT(expected, actual, term, msg) \
{ \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e <= _a) { \
UNITTEST_FAIL_TRACEF(EXPECTED_STRING "%s (%ld) to be" \
" greater-than actual %s (%ld)\n", \
msg, #expected, (long)_e, #actual, (long)_a); \
if (term) { \
return false; \
} \
all_ok = false; \
} \
}
#define EXPECT_TRUE(actual, msg) \
if (!(actual)) { \
UNITTEST_TRACEF("%s: %s is false\n", msg, #actual); \
all_ok = false; \
#define UTCHECK_TRUE(actual, term, msg) \
if (!(actual)) { \
UNITTEST_FAIL_TRACEF("%s: %s is false\n", msg, #actual); \
if (term) { \
return false; \
} \
all_ok = false; \
}
#define EXPECT_FALSE(actual, msg) \
if (actual) { \
UNITTEST_TRACEF("%s: %s is true\n", msg, #actual); \
all_ok = false; \
#define UTCHECK_FALSE(actual, term, msg) \
if (actual) { \
UNITTEST_FAIL_TRACEF("%s: %s is true\n", msg, #actual); \
if (term) { \
return false; \
} \
all_ok = false; \
}
#define EXPECT_BYTES_EQ(expected, actual, length, msg) \
if (!expect_bytes_eq(expected, actual, length, msg)) { \
all_ok = false; \
#define UTCHECK_NULL(actual, term, msg) \
if (actual != NULL) { \
UNITTEST_FAIL_TRACEF("%s: %s is non-null\n", msg, #actual); \
if (term) { \
return false; \
} \
all_ok = false; \
}
#define EXPECT_BYTES_NE(bytes1, bytes2, length, msg) \
if (!memcmp(bytes1, bytes2, length)) { \
UNITTEST_TRACEF("%s and %s are the same; " \
"expected different\n", #bytes1, #bytes2); \
hexdump8(bytes1, length); \
all_ok = false; \
#define UTCHECK_NONNULL(actual, term, msg) \
if (actual == NULL) { \
UNITTEST_FAIL_TRACEF("%s: %s is null\n", msg, #actual); \
if (term) { \
return false; \
} \
all_ok = false; \
}
#define UTCHECK_BYTES_EQ(expected, actual, length, term, msg) \
if (!expect_bytes_eq(expected, actual, length, msg)) { \
if (term) { \
return false; \
} \
all_ok = false; \
}
#define UTCHECK_BYTES_NE(bytes1, bytes2, length, term, msg) \
if (!memcmp(bytes1, bytes2, length)) { \
UNITTEST_FAIL_TRACEF("%s and %s are the same; " \
"expected different\n", #bytes1, #bytes2); \
hexdump8(bytes1, length); \
if (term) { \
return false; \
} \
all_ok = false; \
}
/* For comparing uint64_t, like hw_id_t. */
#define EXPECT_EQ_LL(expected, actual, msg) \
{ \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e != _a) { \
UNITTEST_TRACEF("%s: expected %llu, actual %llu\n", \
msg, _e, _a); \
all_ok = false; \
} \
#define UTCHECK_EQ_LL(expected, actual, term, msg) \
{ \
const typeof(actual) _e = expected; \
const typeof(actual) _a = actual; \
if (_e != _a) { \
UNITTEST_FAIL_TRACEF("%s: expected %llu, actual %llu\n", \
msg, _e, _a); \
if (term) { \
return false; \
} \
all_ok = false; \
} \
}
/*
* The ASSERT_* macros are similar to the EXPECT_* macros except that
* they return on failure.
/* EXPECT_* macros check the supplied condition and will print a diagnostic
* message and flag the test as having failed if the condition fails. The test
* will continue to run, even if the condition fails.
*
* The last parameter is an optional const char* message to be included in the
* print diagnostic message.
*/
#define ASSERT_NOT_NULL(p) \
if (!p) { \
UNITTEST_TRACEF("ERROR: NULL pointer\n"); \
return false; \
}
#define EXPECT_EQ(expected, actual, msg) UTCHECK_EQ(expected, actual, false, msg)
#define EXPECT_NE(expected, actual, msg) UTCHECK_NE(expected, actual, false, msg)
#define EXPECT_LE(expected, actual, msg) UTCHECK_LE(expected, actual, false, msg)
#define EXPECT_LT(expected, actual, msg) UTCHECK_LT(expected, actual, false, msg)
#define EXPECT_GE(expected, actual, msg) UTCHECK_GE(expected, actual, false, msg)
#define EXPECT_GT(expected, actual, msg) UTCHECK_GT(expected, actual, false, msg)
#define EXPECT_TRUE(actual, msg) UTCHECK_TRUE(actual, false, msg)
#define EXPECT_FALSE(actual, msg) UTCHECK_FALSE(actual, false, msg)
#define EXPECT_BYTES_EQ(expected, actual, length, msg) \
UTCHECK_BYTES_EQ(expected, actual, length, false, msg)
#define EXPECT_BYTES_NE(bytes1, bytes2, length, msg) \
UTCHECK_BYTES_NE(bytes1, bytes2, length, false, msg)
#define EXPECT_EQ_LL(expected, actual, msg) UTCHECK_EQ_LL(expected, actual, false, msg)
#define EXPECT_NULL(actual, msg) UTCHECK_NULL(actual, false, msg)
#define EXPECT_NONNULL(actual, msg) UTCHECK_NONNULL(actual, false, msg)
#define EXPECT_OK(actual, msg) UTCHECK_EQ(ZX_OK, actual, false, msg)
/* ASSERT_* macros check the condition and will print a message and immediately
* abort a test with a filure status if the condition fails.
*/
#define ASSERT_EQ(expected, actual, msg) UTCHECK_EQ(expected, actual, true, msg)
#define ASSERT_NE(expected, actual, msg) UTCHECK_NE(expected, actual, true, msg)
#define ASSERT_LE(expected, actual, msg) UTCHECK_LE(expected, actual, true, msg)
#define ASSERT_LT(expected, actual, msg) UTCHECK_LT(expected, actual, true, msg)
#define ASSERT_GE(expected, actual, msg) UTCHECK_GE(expected, actual, true, msg)
#define ASSERT_GT(expected, actual, msg) UTCHECK_GT(expected, actual, true, msg)
#define ASSERT_TRUE(actual, msg) UTCHECK_TRUE(actual, true, msg)
#define ASSERT_FALSE(actual, msg) UTCHECK_FALSE(actual, true, msg)
#define ASSERT_BYTES_EQ(expected, actual, length, msg) \
UTCHECK_BYTES_EQ(expected, actual, length, true, msg)
#define ASSERT_BYTES_NE(bytes1, bytes2, length, msg) \
UTCHECK_BYTES_NE(bytes1, bytes2, length, true, msg)
#define ASSERT_EQ_LL(expected, actual, msg) UTCHECK_EQ_LL(expected, actual, true, msg)
#define ASSERT_NULL(actual, msg) UTCHECK_NULL(actual, true, msg)
#define ASSERT_NONNULL(actual, msg) UTCHECK_NONNULL(actual, true, msg)
#define ASSERT_OK(actual, msg) UTCHECK_EQ(ZX_OK, actual, true, msg)
/*
* The list of test cases is made up of these elements.

View File

@@ -17,6 +17,12 @@
#include <stddef.h>
#include <stdint.h>
#if LK_DEBUGLEVEL > 2
#define TEST_FAILURE_CASES 1
#else
#define TEST_FAILURE_CASES 0
#endif
// Default output function is the printf
static _printf_engine_output_func out_func = _fprintf_output_func;
// Buffer the argument to be sent to the output function
@@ -54,11 +60,168 @@ void unittest_set_output_function (_printf_engine_output_func fun, void *arg) {
}
/* test case for unittests itself */
BEGIN_TEST_CASE(unittest)
static bool unittest_printf_tests(void) {
BEGIN_TEST;
unittest_printf("\n");
unittest_printf("test printf\n");
unittest_printf("test printf with args %d\n", 1);
END_TEST;
}
static bool unittest_simple_pass(void) {
BEGIN_TEST;
// simple true conditions
// note: most of these will probably compile out
EXPECT_EQ(0, 0, "0=0");
EXPECT_EQ(1, 1, "1=1");
EXPECT_NE(0, 1, "0=1");
EXPECT_NE(1, 0, "1=0");
EXPECT_LE(0, 1, "0<=1");
EXPECT_LE(1, 1, "1<=1");
EXPECT_LT(0, 1, "0<1");
EXPECT_GE(1, 0, "1>=0");
EXPECT_GE(1, 1, "1>=1");
EXPECT_GT(1, 0, "1>0");
EXPECT_TRUE(true, "true");
EXPECT_FALSE(false, "false");
EXPECT_NULL((void *)0, "null");
EXPECT_NONNULL((void *)1, "nonnull");
END_TEST;
}
static bool unittest_simple_fail(void) {
BEGIN_TEST;
// failure conditions
EXPECT_EQ(0, 1, "0=1");
EXPECT_EQ(1, 0, "1=0");
EXPECT_NE(1, 1, "1=1");
EXPECT_LE(2, 1, "2<=1");
EXPECT_LE(2, 1, "2<=1");
EXPECT_LT(2, 1, "2<1");
EXPECT_GE(1, 2, "1>=2");
EXPECT_GE(1, 2, "1>=2");
EXPECT_GT(1, 2, "1>2");
EXPECT_TRUE(false, "false");
EXPECT_FALSE(true, "true");
EXPECT_NULL((void *)1, "null");
EXPECT_NONNULL((void *)0, "nonnull");
END_TEST;
}
static bool unittest_assert_pass(void) {
BEGIN_TEST;
// simple true conditions
// note: most of these will probably compile out
ASSERT_EQ(0, 0, "0=0");
ASSERT_EQ(1, 1, "1=1");
ASSERT_NE(0, 1, "0=1");
ASSERT_NE(1, 0, "1=0");
ASSERT_LE(0, 1, "0<=1");
ASSERT_LE(1, 1, "1<=1");
ASSERT_LT(0, 1, "0<1");
ASSERT_GE(1, 0, "1>=0");
ASSERT_GE(1, 1, "1>=1");
ASSERT_GT(1, 0, "1>0");
ASSERT_TRUE(true, "true");
ASSERT_FALSE(false, "false");
EXPECT_NULL((void *)0, "null");
EXPECT_NONNULL((void *)1, "nonnull");
END_TEST;
}
static bool unittest_assert_fail_eq(void) {
BEGIN_TEST;
ASSERT_EQ(0, 1, "0=1");
unittest_printf("should not see this\n");
END_TEST;
}
static bool unittest_assert_fail_ne(void) {
BEGIN_TEST;
ASSERT_NE(1, 1, "1=1");
unittest_printf("should not see this\n");
END_TEST;
}
static bool unittest_assert_fail_le(void) {
BEGIN_TEST;
ASSERT_LE(2, 1, "2<=1");
unittest_printf("should not see this\n");
END_TEST;
}
static bool unittest_assert_fail_lt(void) {
BEGIN_TEST;
ASSERT_LT(2, 1, "2<1");
unittest_printf("should not see this\n");
END_TEST;
}
static bool unittest_assert_fail_ge(void) {
BEGIN_TEST;
ASSERT_GE(1, 2, "1>=2");
unittest_printf("should not see this\n");
END_TEST;
}
static bool unittest_assert_fail_gt(void) {
BEGIN_TEST;
ASSERT_GT(1, 2, "1>2");
unittest_printf("should not see this\n");
END_TEST;
}
static bool unittest_assert_fail_true(void) {
BEGIN_TEST;
ASSERT_GT(1, 2, "1>2");
unittest_printf("should not see this\n");
END_TEST;
}
static bool unittest_assert_fail_false(void) {
BEGIN_TEST;
ASSERT_GT(1, 2, "1>2");
unittest_printf("should not see this\n");
END_TEST;
}
static bool unittest_assert_fail_null(void) {
BEGIN_TEST;
ASSERT_NULL((void *)1, "null");
unittest_printf("should not see this\n");
END_TEST;
}
static bool unittest_assert_fail_nonnull(void) {
BEGIN_TEST;
ASSERT_NONNULL((void *)0, "nonnull");
unittest_printf("should not see this\n");
END_TEST;
}
BEGIN_TEST_CASE(unittest)
RUN_TEST(unittest_printf_tests);
RUN_TEST(unittest_simple_pass);
RUN_TEST(unittest_assert_pass);
#if TEST_FAILURE_CASES
RUN_TEST(unittest_simple_fail);
RUN_TEST(unittest_assert_fail_eq);
RUN_TEST(unittest_assert_fail_ne);
RUN_TEST(unittest_assert_fail_le);
RUN_TEST(unittest_assert_fail_lt);
RUN_TEST(unittest_assert_fail_ge);
RUN_TEST(unittest_assert_fail_gt);
RUN_TEST(unittest_assert_fail_true);
RUN_TEST(unittest_assert_fail_false);
RUN_TEST(unittest_assert_fail_null);
RUN_TEST(unittest_assert_fail_nonnull);
#endif
END_TEST_CASE(unittest)