app/test: new assert macros and test suite runner

- Adding common test assertion macros for unit testing
- Structs for encapsulating unit tests / test suites data.
- test suite runner method

Signed-off-by: Declan Doherty <declan.doherty@intel.com>
Acked-by: Pablo de Lara <pablo.de.lara.guarch@intel.com>
This commit is contained in:
Declan Doherty 2014-07-21 15:52:16 +01:00 committed by Thomas Monjalon
parent 638e82a139
commit ffac67b1f7
2 changed files with 135 additions and 0 deletions

View File

@ -153,3 +153,56 @@ main(int argc, char **argv)
return 0;
}
int
unit_test_suite_runner(struct unit_test_suite *suite)
{
int retval, i = 0;
if (suite->suite_name)
printf("Test Suite : %s\n", suite->suite_name);
if (suite->setup)
if (suite->setup() != 0)
return -1;
while (suite->unit_test_cases[i].testcase) {
/* Run test case setup */
if (suite->unit_test_cases[i].setup) {
retval = suite->unit_test_cases[i].setup();
if (retval != 0)
return retval;
}
/* Run test case */
if (suite->unit_test_cases[i].testcase() == 0) {
printf("TestCase %2d: %s\n", i,
suite->unit_test_cases[i].success_msg ?
suite->unit_test_cases[i].success_msg :
"passed");
}
else {
printf("TestCase %2d: %s\n", i, suite->unit_test_cases[i].fail_msg ?
suite->unit_test_cases[i].fail_msg :
"failed");
return -1;
}
/* Run test case teardown */
if (suite->unit_test_cases[i].teardown) {
retval = suite->unit_test_cases[i].teardown();
if (retval != 0)
return retval;
}
i++;
}
/* Run test suite teardown */
if (suite->teardown)
if (suite->teardown() != 0)
return -1;
return 0;
}

View File

@ -34,6 +34,88 @@
#ifndef _TEST_H_
#define _TEST_H_
#define TEST_ASSERT(cond, msg, ...) do { \
if (!(cond)) { \
printf("TestCase %s() line %d failed: " \
msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
return -1; \
} \
} while (0)
#define TEST_ASSERT_EQUAL(a, b, msg, ...) { \
if (!(a == b)) { \
printf("TestCase %s() line %d failed: " \
msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
return -1; \
} \
} while (0)
#define TEST_ASSERT_NOT_EQUAL(a, b, msg, ...) do { \
if (!(a != b)) { \
printf("TestCase %s() line %d failed: " \
msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
return -1; \
} \
} while (0)
#define TEST_ASSERT_SUCCESS(val, msg, ...) do { \
if (!(val == 0)) { \
printf("TestCase %s() line %d failed: " \
msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
return -1; \
} \
} while (0)
#define TEST_ASSERT_FAIL(val, msg, ...) do { \
if (!(val != -1)) { \
printf("TestCase %s() line %d failed: " \
msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
return -1; \
} \
} while (0)
#define TEST_ASSERT_NULL(val, msg, ...) do { \
if (!(val == NULL)) { \
printf("TestCase %s() line %d failed: " \
msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
return -1; \
} \
} while (0)
#define TEST_ASSERT_NOT_NULL(val, msg, ...) do { \
if (!(val != NULL)) { \
printf("TestCase %s() line %d failed: " \
msg "\n", __func__, __LINE__, ##__VA_ARGS__); \
return -1; \
} \
} while (0)
struct unit_test_case {
int (*setup)(void);
int (*teardown)(void);
int (*testcase)(void);
const char *success_msg;
const char *fail_msg;
};
#define TEST_CASE(fn) { NULL, NULL, fn, #fn " succeeded", #fn " failed"}
#define TEST_CASE_ST(setup, teardown, testcase) \
{ setup, teardown, testcase, #testcase " succeeded", \
#testcase " failed "}
#define TEST_CASES_END() { NULL, NULL, NULL, NULL, NULL }
struct unit_test_suite {
const char *suite_name;
int (*setup)(void);
int (*teardown)(void);
struct unit_test_case unit_test_cases[];
};
int unit_test_suite_runner(struct unit_test_suite *suite);
/* icc on baremetal gives us troubles with function named 'main' */
#ifdef RTE_EXEC_ENV_BAREMETAL
#define main _main