From 8283eb3b54480cc06af86184f265be3db316fbb7 Mon Sep 17 00:00:00 2001 From: Tristan Honscheid Date: Mon, 15 Nov 2021 16:08:24 -0700 Subject: [PATCH] ztest API Migration: lib/c_lib Migrate the unit tests at `lib/c_lib` to use the new ztest API. Signed-off-by: Tristan Honscheid --- tests/lib/c_lib/prj.conf | 1 + tests/lib/c_lib/src/main.c | 129 ++++++++----------------------- tests/lib/c_lib/src/test_qsort.c | 9 ++- 3 files changed, 42 insertions(+), 97 deletions(-) diff --git a/tests/lib/c_lib/prj.conf b/tests/lib/c_lib/prj.conf index 775239be95b..232e47d6a8c 100644 --- a/tests/lib/c_lib/prj.conf +++ b/tests/lib/c_lib/prj.conf @@ -1,4 +1,5 @@ CONFIG_ZTEST=y +CONFIG_ZTEST_NEW_API=y CONFIG_TEST_USERSPACE=y CONFIG_ZTEST_FATAL_HOOK=y CONFIG_MINIMAL_LIBC_RAND=y diff --git a/tests/lib/c_lib/src/main.c b/tests/lib/c_lib/src/main.c index d874785dcbe..74861c0e5ed 100644 --- a/tests/lib/c_lib/src/main.c +++ b/tests/lib/c_lib/src/main.c @@ -48,6 +48,8 @@ static struct k_thread tdata; #pragma GCC diagnostic ignored "-Wstringop-truncation" #endif +ZTEST_SUITE(test_c_lib, NULL, NULL, NULL, NULL, NULL); + /* * variables used during limits library testing; must be marked as "volatile" * to prevent compiler from computing results at compile time @@ -65,7 +67,7 @@ volatile long long_one = 1L; * */ -void test_limits(void) +ZTEST(test_c_lib, test_limits) { zassert_true((long_max + long_one == LONG_MIN), NULL); @@ -76,7 +78,7 @@ static ssize_t foobar(void) return -1; } -void test_ssize_t(void) +ZTEST(test_c_lib, test_ssize_t) { zassert_true(foobar() < 0, NULL); } @@ -86,7 +88,7 @@ void test_ssize_t(void) * @brief Test boolean types and values library * */ -void test_stdbool(void) +ZTEST(test_c_lib, test_stdbool) { zassert_true((true == 1), "true value"); @@ -106,7 +108,7 @@ volatile size_t size_of_long_variable = sizeof(long_variable); * @brief Test standard type definitions library * */ -void test_stddef(void) +ZTEST(test_c_lib, test_stddef) { #ifdef CONFIG_64BIT zassert_true((size_of_long_variable == 8), "sizeof"); @@ -128,7 +130,7 @@ volatile uint32_t unsigned_int = 0xffffff00; * @brief Test integer types library * */ -void test_stdint(void) +ZTEST(test_c_lib, test_stdint) { zassert_true((unsigned_int + unsigned_byte + 1u == 0U), NULL); @@ -161,7 +163,7 @@ char buffer[BUFSIZE]; * @brief Test string memset * */ -void test_memset(void) +ZTEST(test_c_lib, test_memset) { int i, ret; const char set = 'a'; @@ -183,7 +185,7 @@ void test_memset(void) * @see strlen(), strnlen(). * */ -void test_strlen(void) +ZTEST(test_c_lib, test_strlen) { (void)memset(buffer, '\0', BUFSIZE); (void)memset(buffer, 'b', 5); /* 5 is BUFSIZE / 2 */ @@ -200,7 +202,7 @@ void test_strlen(void) * @see strcmp(), strncasecmp(). * */ -void test_strcmp(void) +ZTEST(test_c_lib, test_strcmp) { strcpy(buffer, "eeeee"); char test = 0; @@ -221,7 +223,7 @@ void test_strcmp(void) * * @see strncmp(). */ -void test_strncmp(void) +ZTEST(test_c_lib, test_strncmp) { static const char pattern[] = "eeeeeeeeeeee"; @@ -248,7 +250,7 @@ void test_strncmp(void) * * @see strcpy(). */ -void test_strcpy(void) +ZTEST(test_c_lib, test_strcpy) { (void)memset(buffer, '\0', BUFSIZE); strcpy(buffer, "10 chars!\0"); @@ -262,7 +264,7 @@ void test_strcpy(void) * * @see strncpy(). */ -void test_strncpy(void) +ZTEST(test_c_lib, test_strncpy) { int ret; @@ -281,7 +283,7 @@ void test_strncpy(void) * * @see strchr(). */ -void test_strchr(void) +ZTEST(test_c_lib, test_strchr) { char *rs = NULL; int ret; @@ -305,7 +307,7 @@ void test_strchr(void) * * @see strspn(),strcspn(). */ -void test_strxspn(void) +ZTEST(test_c_lib, test_strxspn) { const char *empty = ""; const char *cset = "abc"; @@ -329,7 +331,7 @@ void test_strxspn(void) * * @see memcmp() */ -void test_memcmp(void) +ZTEST(test_c_lib, test_memcmp) { int ret; unsigned char m1[] = "a\0$def"; @@ -360,7 +362,7 @@ int cmp_func(const void *a, const void *b) return (*(int *)a - *(int *)b); } -void test_bsearch(void) +ZTEST(test_c_lib, test_bsearch) { void *result = NULL; int arr[5] = { 2, 5, 20, 50, 60 }; @@ -381,7 +383,7 @@ void test_bsearch(void) * * @see abs() */ -void test_abs(void) +ZTEST(test_c_lib, test_abs) { int val = -5, value = 5; @@ -395,7 +397,7 @@ void test_abs(void) * * @see atoi() */ -void test_atoi(void) +ZTEST(test_c_lib, test_atoi) { zassert_equal(atoi("123"), 123, "atoi error"); zassert_equal(atoi("2c5"), 2, "atoi error"); @@ -419,7 +421,7 @@ void test_atoi(void) * isprint(), isspace(), isupper(), isxdigit(). * */ -void test_checktype(void) +ZTEST(test_c_lib, test_checktype) { static const char exp_alnum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -506,7 +508,7 @@ void test_checktype(void) * * @see memchr(). */ -void test_memchr(void) +ZTEST(test_c_lib, test_memchr) { static const char str[] = "testfunction"; @@ -525,7 +527,7 @@ void test_memchr(void) * * @see memcpy(). */ -void test_memcpy(void) +ZTEST(test_c_lib, test_memcpy) { /* make sure the buffer is word aligned */ uintptr_t mem_dest[4] = {0}; @@ -575,7 +577,7 @@ void test_memcpy(void) * * @see memmove(). */ -void test_memmove(void) +ZTEST(test_c_lib, test_memmove) { char move_buffer[6] = "12123"; char move_new[6] = {0}; @@ -601,7 +603,7 @@ void test_memmove(void) * @see strcat(), strcspn(), strncat(). * */ -void test_str_operate(void) +ZTEST(test_c_lib, test_str_operate) { char str1[10] = "aabbcc", ncat[10] = "ddee"; char *str2 = "b"; @@ -646,7 +648,7 @@ void test_str_operate(void) * @see strtol(). * */ -void test_strtol(void) +ZTEST(test_c_lib, test_strtol) { static const char buf1[] = "+10379aegi"; static const char buf2[] = " -10379aegi"; @@ -735,7 +737,7 @@ void test_strtol(void) * @see strtoul(). * */ -void test_strtoul(void) +ZTEST(test_c_lib, test_strtoul) { static const char buf1[] = "+10379aegi"; static const char buf2[] = " -10379aegi"; @@ -974,7 +976,7 @@ void test_strtoull(void) * @brief test convert function * */ -void test_tolower_toupper(void) +ZTEST(test_c_lib, test_tolower_toupper) { static const char test[] = "Az09Za{#!"; static const char toup[] = "AZ09ZA{#!"; @@ -1018,7 +1020,7 @@ void test_strtok_r_do(char *str, char *sep, int tlen, } } -void test_strtok_r(void) +ZTEST(test_c_lib, test_strtok_r) { static const char * const tc01[] = { "1", "2", "3", "4", "5" }; @@ -1039,7 +1041,7 @@ void test_strtok_r(void) * * @see gmtime(),gmtime_r(). */ -void test_time(void) +ZTEST(test_c_lib, test_time) { time_t tests1 = 0; time_t tests2 = -5; @@ -1061,7 +1063,7 @@ void test_time(void) * @brief Test rand function * */ -void test_rand(void) +ZTEST(test_c_lib, test_rand) { int a; @@ -1075,7 +1077,7 @@ void test_rand(void) * @brief Test srand function * */ -void test_srand(void) +ZTEST(test_c_lib, test_srand) { int a; @@ -1105,7 +1107,7 @@ void test_srand(void) * @brief Test rand function for reproducibility * */ -void test_rand_reproducibility(void) +ZTEST(test_c_lib, test_rand_reproducibility) { int a; int b; @@ -1168,7 +1170,7 @@ void test_rand_reproducibility(void) * * @see abort(). */ -void test_abort(void) +ZTEST(test_c_lib, test_abort) { int a = 0; @@ -1187,7 +1189,7 @@ static void exit_program(void *p1, void *p2, void *p3) _exit(1); } -void test_exit(void) +ZTEST(test_c_lib, test_exit) { int a = 0; @@ -1197,66 +1199,3 @@ void test_exit(void) k_thread_abort(tid); zassert_equal(a, 0, "exit failed"); } - -/** - * - * @brief Test qsort function - * - * @see qsort() - */ -extern void test_qsort(void); - -/** - * - * @brief Test qsort_r function - * - * @see qsort_r() - */ -extern void test_qsort_r(void); - -/** - * @} - */ - -void test_main(void) -{ - ztest_test_suite(test_c_lib, - ztest_unit_test(test_limits), - ztest_unit_test(test_ssize_t), - ztest_unit_test(test_stdbool), - ztest_unit_test(test_stddef), - ztest_unit_test(test_stdint), - ztest_unit_test(test_memcmp), - ztest_unit_test(test_strchr), - ztest_unit_test(test_strcpy), - ztest_unit_test(test_strncpy), - ztest_unit_test(test_memset), - ztest_unit_test(test_strlen), - ztest_unit_test(test_strcmp), - ztest_unit_test(test_strxspn), - ztest_unit_test(test_bsearch), - ztest_unit_test(test_abs), - ztest_unit_test(test_atoi), - ztest_unit_test(test_strncmp), - ztest_unit_test(test_strtol), - ztest_unit_test(test_strtoul), - ztest_unit_test(test_strtoll), - ztest_unit_test(test_strtoull), - ztest_unit_test(test_checktype), - ztest_unit_test(test_memchr), - ztest_unit_test(test_memcpy), - ztest_unit_test(test_memmove), - ztest_unit_test(test_time), - ztest_unit_test(test_rand), - ztest_unit_test(test_srand), - ztest_unit_test(test_rand_reproducibility), - ztest_unit_test(test_abort), - ztest_unit_test(test_exit), - ztest_unit_test(test_str_operate), - ztest_unit_test(test_tolower_toupper), - ztest_unit_test(test_strtok_r), - ztest_unit_test(test_qsort), - ztest_unit_test(test_qsort_r) - ); - ztest_run_test_suite(test_c_lib); -} diff --git a/tests/lib/c_lib/src/test_qsort.c b/tests/lib/c_lib/src/test_qsort.c index ee2305d7269..44b8e9019aa 100644 --- a/tests/lib/c_lib/src/test_qsort.c +++ b/tests/lib/c_lib/src/test_qsort.c @@ -16,7 +16,12 @@ static int compare_ints(const void *a, const void *b) return (aa > bb) - (aa < bb); } -void test_qsort(void) +/** + * + * @brief Test qsort function + * + */ +ZTEST(test_c_lib, test_qsort) { { int actual_int[] = { 1, 3, 2 }; @@ -132,7 +137,7 @@ static int compare_ints_with_boolp_arg(const void *a, const void *b, void *argp) return (aa > bb) - (aa < bb); } -void test_qsort_r(void) +ZTEST(test_c_lib, test_qsort_r) { bool arg = false;