test: Make TEST_* report test location

This patch converts TEST_* functions to macros, which print file name
and line # of the check. This will allow us to locate a failed test
quickly.

New TEST_* macros also automatically generate a test name if
testname == NULL. This will save us time to think of a name for
every single check we write in a test.

BUG=none
BRANCH=tot
TEST=make runtests

Change-Id: Ibdeb99681985c3f348836d256fa3484f2f0c315f
Signed-off-by: Daisuke Nojiri <dnojiri@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/343233
Reviewed-by: Randall Spangler <rspangler@chromium.org>
This commit is contained in:
Daisuke Nojiri
2016-05-06 14:38:06 -07:00
committed by chrome-bot
parent f3f9e00ef0
commit 099eeb9b97
4 changed files with 158 additions and 66 deletions

View File

@@ -17,8 +17,6 @@
#include "test_common.h"
#include "vboot_register.h"
#define TEST_EQ_S(result, expect) TEST_EQ(result, expect, #result "==" #expect)
static struct bdb_header *bdb, *bdb0, *bdb1;
static uint32_t vboot_register;
static uint32_t vboot_register_persist;

View File

@@ -14,8 +14,6 @@
#include "host.h"
#include "test_common.h"
#define TEST_EQ_S(result, expect) TEST_EQ(result, expect, #result "==" #expect)
void check_header_tests(void)
{
struct bdb_header sgood = {

View File

@@ -18,128 +18,157 @@
/* Global test success flag. */
int gTestSuccess = 1;
int TEST_EQ(int result, int expected_result, const char* testname) {
if (result == expected_result) {
fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
int test_eq(int result, int expected,
const char *preamble, const char *desc, const char *comment)
{
if (result == expected) {
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
return 1;
} else {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " Expected: 0x%x (%d), got: 0x%x (%d)\n",
expected_result, expected_result, result, result);
expected, expected, result, result);
gTestSuccess = 0;
return 0;
}
}
int TEST_NEQ(int result, int not_expected_result, const char* testname) {
if (result != not_expected_result) {
fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
int test_neq(int result, int not_expected,
const char *preamble, const char *desc, const char *comment)
{
if (result != not_expected) {
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
return 1;
} else {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " Didn't expect 0x%x (%d), but got it.\n",
not_expected_result, not_expected_result);
not_expected, not_expected);
gTestSuccess = 0;
return 0;
}
}
int TEST_PTR_EQ(const void* result, const void* expected_result,
const char* testname) {
if (result == expected_result) {
fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
int test_ptr_eq(const void* result, const void* expected,
const char *preamble, const char *desc, const char *comment)
{
if (result == expected) {
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
return 1;
} else {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, " Expected: 0x%lx, got: 0x%lx\n", (long)expected_result,
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " Expected: 0x%lx, got: 0x%lx\n", (long)expected,
(long)result);
gTestSuccess = 0;
return 0;
}
}
int TEST_PTR_NEQ(const void* result, const void* not_expected_result,
const char* testname) {
if (result != not_expected_result) {
fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
int test_ptr_neq(const void* result, const void* not_expected,
const char *preamble, const char *desc, const char *comment)
{
if (result != not_expected) {
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
return 1;
} else {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " Didn't expect 0x%lx, but got it\n",
(long)not_expected_result);
(long)not_expected);
gTestSuccess = 0;
return 0;
}
}
int TEST_STR_EQ(const char* result, const char* expected_result,
const char* testname) {
if (!result || !expected_result) {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
int test_str_eq(const char* result, const char* expected,
const char *preamble, const char *desc, const char *comment)
{
if (!result || !expected) {
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " String compare with NULL\n");
gTestSuccess = 0;
return 0;
} else if (!strcmp(result, expected_result)) {
fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
} else if (!strcmp(result, expected)) {
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
return 1;
} else {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, " Expected: \"%s\", got: \"%s\"\n", expected_result,
fprintf(stderr, "%s " COL_RED "FAILED\n" COL_STOP, comment);
fprintf(stderr, " Expected: \"%s\", got: \"%s\"\n", expected,
result);
gTestSuccess = 0;
return 0;
}
}
int TEST_STR_NEQ(const char* result, const char* not_expected,
const char* testname) {
int test_str_neq(const char* result, const char* not_expected,
const char *preamble, const char *desc, const char *comment)
{
if (!result || !not_expected) {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " String compare with NULL\n");
gTestSuccess = 0;
return 0;
} else if (strcmp(result, not_expected)) {
fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
return 1;
} else {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " Didn't expect: \"%s\", but got it\n", not_expected);
gTestSuccess = 0;
return 0;
}
}
int TEST_SUCC(int result, const char* testname) {
int test_succ(int result,
const char *preamble, const char *desc, const char *comment)
{
if (result == 0) {
fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
} else {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " Expected SUCCESS, got: 0x%lx\n", (long)result);
gTestSuccess = 0;
}
return !result;
}
int TEST_TRUE(int result, const char* testname) {
int test_true(int result,
const char *preamble, const char *desc, const char *comment)
{
if (result) {
fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
} else {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " Expected TRUE, got 0\n");
gTestSuccess = 0;
}
return result;
}
int TEST_FALSE(int result, const char* testname) {
int test_false(int result,
const char *preamble, const char *desc, const char *comment)
{
if (!result) {
fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
} else {
fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
preamble, desc, comment);
fprintf(stderr, " Expected FALSE, got: 0x%lx\n", (long)result);
gTestSuccess = 0;
}

View File

@@ -7,49 +7,116 @@
#ifndef VBOOT_REFERENCE_TEST_COMMON_H_
#define VBOOT_REFERENCE_TEST_COMMON_H_
#include <stdio.h>
/* Used to get a line number as a constant string. Need to stringify it twice */
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
extern int gTestSuccess;
/* Return 1 if result is equal to expected_result, else return 0.
* Also update the global gTestSuccess flag if test fails. */
int TEST_EQ(int result, int expected_result, const char* testname);
int test_eq(int result, int expected,
const char *preamble, const char *desc, const char *comment);
#define TEST_EQ(result, expected, comment) \
test_eq(result, expected, \
__FILE__ ":" TOSTRING(__LINE__), \
#result " == " #expected, \
comment)
#define TEST_EQ_S(result, expected) TEST_EQ(result, expected, NULL);
/* Return 0 if result is equal to not_expected_result, else return 1.
* Also update the global gTestSuccess flag if test fails. */
int TEST_NEQ(int result, int not_expected_result, const char* testname);
int test_neq(int result, int not_expected,
const char *preamble, const char *desc, const char *comment);
#define TEST_NEQ(result, not_expected, comment) \
test_neq(result, not_expected, \
__FILE__ ":" TOSTRING(__LINE__), \
#result " != " #not_expected, \
comment)
/* Return 1 if result pointer is equal to expected_result pointer,
* else return 0. Does not check pointer contents, only the pointer
* itself. Also update the global gTestSuccess flag if test fails. */
int TEST_PTR_EQ(const void* result, const void* expected_result,
const char* testname);
int test_ptr_eq(const void* result, const void* expected,
const char *preamble, const char *desc, const char *comment);
#define TEST_PTR_EQ(result, expected, comment) \
test_ptr_eq(result, expected, \
__FILE__ ":" TOSTRING(__LINE__), \
#result " == " #expected, \
comment)
/* Return 1 if result pointer is not equal to expected_result pointer,
* else return 0. Does not check pointer contents, only the pointer
* itself. Also update the global gTestSuccess flag if test fails. */
int TEST_PTR_NEQ(const void* result, const void* expected_result,
const char* testname);
int test_ptr_neq(const void* result, const void* not_expected,
const char *preamble, const char *desc, const char *comment);
#define TEST_PTR_NEQ(result, not_expected, comment) \
test_ptr_neq(result, not_expected, \
__FILE__ ":" TOSTRING(__LINE__), \
#result " != " #not_expected, \
comment)
/* Return 1 if result string is equal to expected_result string,
* else return 0. Also update the global gTestSuccess flag if test fails. */
int TEST_STR_EQ(const char* result, const char* expected_result,
const char* testname);
int test_str_eq(const char* result, const char* expected,
const char *preamble, const char *desc, const char *comment);
/* Return 1 if result string is not equal to not_expected_result string,
#define TEST_STR_EQ(result, expected, comment) \
test_str_eq(result, expected, \
__FILE__ ":" TOSTRING(__LINE__), \
#result " == " #expected, \
comment)
/* Return 1 if result string is not equal to not_expected string,
* else return 0. Also update the global gTestSuccess flag if test fails. */
int TEST_STR_NEQ(const char* result, const char* not_expected_result,
const char* testname);
int test_str_neq(const char* result, const char* not_expected,
const char *preamble, const char *desc, const char *comment);
#define TEST_STR_NEQ(result, not_expected, comment) \
test_str_neq(result, not_expected, \
__FILE__ ":" TOSTRING(__LINE__), \
#result " != " #not_expected, \
comment)
/* Return 1 if the result is true, else return 0.
* Also update the global gTestSuccess flag if test fails. */
int TEST_TRUE(int result, const char* testname);
int test_true(int result,
const char *preamble, const char *desc, const char *comment);
#define TEST_TRUE(result, comment) \
test_true(result, \
__FILE__ ":" TOSTRING(__LINE__), \
#result " == true", \
comment)
/* Return 1 if the result is false, else return 0.
* Also update the global gTestSuccess flag if test fails. */
int TEST_FALSE(int result, const char* testname);
int test_false(int result,
const char *preamble, const char *desc, const char *comment);
#define TEST_FALSE(result, comment) \
test_false(result, \
__FILE__ ":" TOSTRING(__LINE__), \
#result " == false", \
comment)
/* Return 1 if result is 0 (VB_ERROR_SUCCESS / VB2_SUCCESS), else return 0.
* Also update the global gTestSuccess flag if test fails. */
int TEST_SUCC(int result, const char* testname);
int test_succ(int result,
const char *preamble, const char *desc, const char *comment);
#define TEST_SUCC(result, comment) \
test_succ(result, \
__FILE__ ":" TOSTRING(__LINE__), \
#result " == 0", \
comment)
/* ANSI Color coding sequences.
*