summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu-Ping Wu <yupingso@chromium.org>2020-05-28 15:45:23 +0800
committerCommit Bot <commit-bot@chromium.org>2020-05-29 11:54:56 +0000
commit5e3b9473173ea452e298d8424b3e5db2e48bcb4a (patch)
tree21a6f84149762b0af114833859c3333dc047ccb4
parent8fe5f7ef0564d39eb966561e26a185c58b63246d (diff)
downloadvboot-5e3b9473173ea452e298d8424b3e5db2e48bcb4a.tar.gz
vboot: have consistent output for testing macros
Extract print_passed() and print_failed() from test_eq(), and use them consistently in all testing functions in test_common.h. BRANCH=none BUG=none TEST=make runtests Change-Id: I1d4dbf13c9ae1a8130ea7af2733044d0556fa974 Signed-off-by: Yu-Ping Wu <yupingso@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2217547 Reviewed-by: Joel Kitching <kitching@chromium.org>
-rw-r--r--tests/test_common.c83
1 files changed, 39 insertions, 44 deletions
diff --git a/tests/test_common.c b/tests/test_common.c
index 21740b60..7e994f36 100644
--- a/tests/test_common.c
+++ b/tests/test_common.c
@@ -17,16 +17,28 @@ int gTestSuccess = 1;
int gTestAbortArmed = 0;
jmp_buf gTestJmpEnv;
+static void print_passed(const char *preamble, const char *desc,
+ const char *comment)
+{
+ fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP,
+ preamble, comment ? comment : desc);
+}
+
+static void print_failed(const char *preamble, const char *desc,
+ const char *comment)
+{
+ fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP,
+ preamble, comment ? comment : desc);
+}
+
int test_eq(int result, int expected,
const char *preamble, const char *desc, const char *comment)
{
if (result == expected) {
- fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP,
- preamble, comment ? comment : desc);
+ print_passed(preamble, desc, comment);
return 1;
} else {
- fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP,
- preamble, comment ? comment : desc);
+ print_failed(preamble, desc, comment);
fprintf(stderr, " Expected: %#x (%d), got: %#x (%d)\n",
expected, expected, result, result);
gTestSuccess = 0;
@@ -38,12 +50,10 @@ 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);
+ print_passed(preamble, desc, comment);
return 1;
} else {
- fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
- preamble, desc, comment);
+ print_failed(preamble, desc, comment);
fprintf(stderr, " Didn't expect %#x (%d), but got it.\n",
not_expected, not_expected);
gTestSuccess = 0;
@@ -55,13 +65,11 @@ 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);
+ print_passed(preamble, desc, comment);
return 1;
} else {
- fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
- preamble, desc, comment);
- fprintf(stderr, " Expected: 0x%lx, got: 0x%lx\n",
+ print_failed(preamble, desc, comment);
+ fprintf(stderr, " Expected: %#lx, got: %#lx\n",
(long)expected, (long)result);
gTestSuccess = 0;
return 0;
@@ -72,13 +80,11 @@ 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);
+ print_passed(preamble, desc, comment);
return 1;
} else {
- 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",
+ print_failed(preamble, desc, comment);
+ fprintf(stderr, " Didn't expect %#lx, but got it\n",
(long)not_expected);
gTestSuccess = 0;
return 0;
@@ -89,17 +95,15 @@ 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);
+ print_failed(preamble, desc, comment);
fprintf(stderr, " String compare with NULL\n");
gTestSuccess = 0;
return 0;
} else if (!strcmp(result, expected)) {
- fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
- preamble, desc, comment);
+ print_passed(preamble, desc, comment);
return 1;
} else {
- fprintf(stderr, "%s " COL_RED "FAILED\n" COL_STOP, comment);
+ print_failed(preamble, desc, comment);
fprintf(stderr, " Expected: \"%s\", got: \"%s\"\n",
expected, result);
gTestSuccess = 0;
@@ -111,18 +115,17 @@ 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: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
- preamble, desc, comment);
+ print_failed(preamble, desc, comment);
fprintf(stderr, " String compare with NULL\n");
gTestSuccess = 0;
return 0;
} else if (strcmp(result, not_expected)) {
+ print_passed(preamble, desc, comment);
fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
preamble, desc, comment);
return 1;
} else {
- fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
- preamble, desc, comment);
+ print_failed(preamble, desc, comment);
fprintf(stderr, " Didn't expect: \"%s\", but got it\n",
not_expected);
gTestSuccess = 0;
@@ -134,11 +137,9 @@ int test_succ(int result,
const char *preamble, const char *desc, const char *comment)
{
if (result == 0) {
- fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP,
- preamble, comment ? comment : desc);
+ print_passed(preamble, desc, comment);
} else {
- fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP,
- preamble, comment ? comment : desc);
+ print_failed(preamble, desc, comment);
fprintf(stderr, " Expected SUCCESS, got: %#x (%d)\n",
result, result);
gTestSuccess = 0;
@@ -150,11 +151,9 @@ int test_true(int result,
const char *preamble, const char *desc, const char *comment)
{
if (result) {
- fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
- preamble, desc, comment);
+ print_passed(preamble, desc, comment);
} else {
- fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
- preamble, desc, comment);
+ print_failed(preamble, desc, comment);
fprintf(stderr, " Expected TRUE, got 0\n");
gTestSuccess = 0;
}
@@ -165,12 +164,10 @@ int test_false(int result,
const char *preamble, const char *desc, const char *comment)
{
if (!result) {
- fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
- preamble, desc, comment);
+ print_passed(preamble, desc, comment);
} else {
- fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
- preamble, desc, comment);
- fprintf(stderr, " Expected FALSE, got: 0x%lx\n",
+ print_failed(preamble, desc, comment);
+ fprintf(stderr, " Expected FALSE, got: %#lx\n",
(long)result);
gTestSuccess = 0;
}
@@ -181,11 +178,9 @@ int test_abort(int aborted,
const char *preamble, const char *desc, const char *comment)
{
if (aborted) {
- fprintf(stderr, "%s: %s ... " COL_GREEN "PASSED\n" COL_STOP,
- preamble, comment ? comment : desc);
+ print_passed(preamble, desc, comment);
} else {
- fprintf(stderr, "%s: %s ... " COL_RED "FAILED\n" COL_STOP,
- preamble, comment ? comment : desc);
+ print_failed(preamble, desc, comment);
fprintf(stderr, " Expected ABORT, but did not get it\n");
gTestSuccess = 0;
}