From 84d23fd82fb1d02a368ae3307df30a425d2193c6 Mon Sep 17 00:00:00 2001 From: shixiongx zhang Date: Thu, 4 Mar 2021 14:31:06 +0800 Subject: [PATCH] Tests: libc: Improve code coverage Add test cases for libc module APIs. Signed-off-by: shixiongx zhang --- tests/lib/c_lib/prj.conf | 4 +- tests/lib/c_lib/src/main.c | 301 +++++++++++++++--- tests/lib/c_lib/testcase.yaml | 3 +- tests/lib/mem_alloc/prj_negative_testing.conf | 3 + tests/lib/mem_alloc/src/main.c | 101 ++++-- tests/lib/mem_alloc/testcase.yaml | 9 +- tests/lib/sprintf/prj.conf | 2 + tests/lib/sprintf/prj_new.conf | 5 + tests/lib/sprintf/src/main.c | 264 +++++++++++++++ tests/lib/sprintf/testcase.yaml | 9 +- 10 files changed, 635 insertions(+), 66 deletions(-) create mode 100644 tests/lib/mem_alloc/prj_negative_testing.conf create mode 100644 tests/lib/sprintf/prj_new.conf diff --git a/tests/lib/c_lib/prj.conf b/tests/lib/c_lib/prj.conf index 0fdb4325efb..869fd338e6a 100644 --- a/tests/lib/c_lib/prj.conf +++ b/tests/lib/c_lib/prj.conf @@ -1 +1,3 @@ -CONFIG_ZTEST=y \ No newline at end of file +CONFIG_ZTEST=y +CONFIG_TEST_USERSPACE=y +CONFIG_ZTEST_FATAL_HOOK=y diff --git a/tests/lib/c_lib/src/main.c b/tests/lib/c_lib/src/main.c index 9848dfa4eca..b5ac88725d6 100644 --- a/tests/lib/c_lib/src/main.c +++ b/tests/lib/c_lib/src/main.c @@ -25,10 +25,19 @@ #include #include #include +#include #include #include #include #include +#include +#include + +#define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACKSIZE) +#define LIST_LEN 2 + +static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE); +static struct k_thread tdata; /* Recent GCC's are issuing a warning for the truncated strncpy() * below (the static source string is longer than the locally-defined @@ -50,6 +59,9 @@ volatile long long_one = 1L; /** * * @brief Test implementation-defined constants library + * @defgroup libc_api + * @ingroup all_tests + * @{ * */ @@ -155,9 +167,17 @@ char buffer[BUFSIZE]; void test_memset(void) { - (void)memset(buffer, 'a', BUFSIZE); - zassert_true((buffer[0] == 'a'), "memset"); - zassert_true((buffer[BUFSIZE - 1] == 'a'), "memset"); + int i, ret; + const char set = 'a'; + int size = 0; + + memset(buffer, 0, 10); + for (i = 0; i < 10; i++) { + memset(buffer + i, set, size); + memset(buffer + i, set, 1); + ret = memcmp(buffer + i, &set, 1); + zassert_true((ret == 0), "memset buffer a failed"); + } } /** @@ -170,22 +190,31 @@ void test_strlen(void) { (void)memset(buffer, '\0', BUFSIZE); (void)memset(buffer, 'b', 5); /* 5 is BUFSIZE / 2 */ - zassert_equal(strlen(buffer), 5, "strlen"); + zassert_equal(strlen(buffer), 5, "strlen failed"); + zassert_equal(strnlen(buffer, 6), 5, "strnlen failed"); + zassert_equal(strnlen(buffer, 4), 4, "strnlen failed"); } /** * * @brief Test string compare function * + * @see strcmp(), strncasecmp(). + * */ void test_strcmp(void) { strcpy(buffer, "eeeee"); + char test = 0; zassert_true((strcmp(buffer, "fffff") < 0), "strcmp less ..."); zassert_true((strcmp(buffer, "eeeee") == 0), "strcmp equal ..."); zassert_true((strcmp(buffer, "ddddd") > 0), "strcmp greater ..."); + + zassert_true((strncasecmp(buffer, "ddddd", 3) > 0), "strncasecmp failed"); + zassert_true((strncasecmp(buffer, "eeeee", 3) == 0), "strncasecmp failed"); + zassert_true((strncasecmp(&test, &test, 1) == 0), "strncasecmp failed"); } /** @@ -196,16 +225,33 @@ void test_strcmp(void) void test_strncmp(void) { + int ret; static const char pattern[] = "eeeeeeeeeeee"; /* Note we don't want to count the final \0 that sizeof will */ __ASSERT_NO_MSG(sizeof(pattern) - 1 > BUFSIZE); memcpy(buffer, pattern, BUFSIZE); - zassert_true((strncmp(buffer, "fffff", 0) == 0), "strncmp 0"); - zassert_true((strncmp(buffer, "eeeff", 3) == 0), "strncmp 3"); - zassert_true((strncmp(buffer, "eeeeeeeeeeeff", BUFSIZE) == 0), - "strncmp 10"); + ret = strncmp(buffer, "fffff", 0); + zassert_true((ret == 0), "strncmp zero characters failed"); + + ret = strncmp(buffer, "effff", 0); + zassert_true((ret == 0), "strncmp zero character failed"); + + ret = strncmp("", "", 0); + zassert_true((ret == 0), "strncmp zero character failed"); + + ret = strncmp("", "", 3); + zassert_true((ret == 0), "strncmp zero character failed"); + + ret = strncmp(buffer, "eeeff", 4); + zassert_true((ret != 0), "strncmp four characters failed"); + + ret = strncmp(buffer, "ff", BUFSIZE); + zassert_true((ret != 0), "strncmp ten characters failed"); + + ret = strncmp(buffer, "eee", 4); + zassert_true((ret != 0), "strncmp four characters failed"); } @@ -299,15 +345,21 @@ void test_strxspn(void) void test_memcmp(void) { int ret; - unsigned char m1[5] = { 1, 2, 3, 4, 5 }; - unsigned char m2[5] = { 1, 2, 3, 4, 6 }; + unsigned char m1[] = "abcdef"; + unsigned char m2[] = "abcdhj"; ret = memcmp(m1, m2, 4); - zassert_true((ret == 0), "memcmp 4"); + zassert_true((ret == 0), "memcmp four characters failed"); ret = memcmp(m1, m2, 5); - zassert_true((ret != 0), "memcmp 5"); + zassert_true((ret != 0), "memcmp five characters failed"); + + ret = memcmp(m1, m2, 0); + zassert_true((ret == 0), "memcmp zero character failed"); + + ret = memcmp(m1, m2, 6); + zassert_true((ret != 0), "memcmp six characters failed"); } /** @@ -364,6 +416,8 @@ void test_atoi(void) zassert_equal(atoi(""), 0, "atoi error"); zassert_equal(atoi("3-4e"), 3, "atoi error"); zassert_equal(atoi("8+1c"), 8, "atoi error"); + zassert_equal(atoi("+3"), 3, "atoi error"); + zassert_equal(atoi("-1"), -1, "atoi error"); } /** @@ -468,24 +522,30 @@ void test_checktype(void) */ void test_memstr(void) { + int i, j, ret; static const char str[] = "testfunction"; - int ch1 = 'e', ch2 = 'z'; - char arr[10], move_arr[6] = "12123"; - static const char num[6] = "12121"; + char arr[100], move_arr[] = "12123"; + static const char num[] = "1234567891234567891234"; - zassert_equal(memchr(str, ch1, strlen(str)), str + 1, - "memchr 'testfunction' 'e' "); - zassert_equal(memchr(str, ch2, strlen(str)), NULL, - "memchr 'testfunction' 'z'"); + zassert_is_null(memchr(str, 'a', 0), "memchr 0 error"); + zassert_not_null(memchr(str, 'e', 10), "memchr serach e"); + zassert_is_null(memchr(str, 'e', 1), "memchr e error"); - zassert_equal(memcpy(arr, num, sizeof(num)), arr, "memcpy error"); - zassert_equal(memcmp(num, arr, sizeof(num)), 0, - "memcpy failed"); + for (i = 0; i < 20; i++) { + for (j = 0; j < 20; j++) { + memcpy(&arr[i], num, 0); + ret = memcmp(&num[j], &arr[i], 0); + zassert_true((ret == 0), "memcpy failed"); + memcpy(&arr[i], &num[j], 1); + ret = memcmp(&num[j], &arr[i], 1); + zassert_true((ret == 0), "memcpy failed"); + } + } - zassert_equal(memmove(move_arr + 2, move_arr, 3), move_arr + 2, - "memmove error"); - zassert_equal(memcmp(num, move_arr, 6), 0, - "memmove failed"); + memmove(move_arr + 2, move_arr, 3); + memmove(move_arr, num, 3); + ret = memcmp(move_arr, num, 3); + zassert_true((ret == 0), "memmove failed"); } /** @@ -512,6 +572,8 @@ void test_str_operate(void) zassert_equal(ret, 6, "strcspn not found str"); zassert_true(strncat(ncat, str1, 2), "strncat failed"); + zassert_not_null(strncat(str1, str3, 2), "strncat failed"); + zassert_not_null(strncat(str1, str3, 1), "strncat failed"); zassert_equal(strcmp(ncat, "ddeeaa"), 0, "strncat failed"); zassert_is_null(strrchr(ncat, 'z'), @@ -519,8 +581,10 @@ void test_str_operate(void) ptr = strrchr(ncat, 'e'); zassert_equal(strcmp(ptr, "eaa"), 0, "strrchr failed"); - zassert_is_null(strstr(str1, "xyz"), "strstr aabbccd with xyz failed"); - zassert_not_null(strstr(str1, str2), "strstr aabbccd with b failed"); + zassert_is_null(strstr(str1, "ayz"), "strstr aabbccd with ayz failed"); + zassert_not_null(strstr(str1, str2), "strstr aabbccd with b succeed"); + zassert_not_null(strstr(str1, "bb"), "strstr aabbccd with bb succeed"); + zassert_not_null(strstr(str1, ""), "strstr aabbccd with \0 failed"); } /** @@ -530,22 +594,114 @@ void test_str_operate(void) */ void test_strtoxl(void) { - char buf1[20] = "10379aegi"; - char buf2[20] = "10379aegi"; - char buf3[20] = "010379aegi"; - char buf4[20] = "0x10379aegi"; - char *stop = NULL; + char buf1[] = "+10379aegi"; + char buf2[] = " -10379aegi"; + char buf3[] = "-010379aegi"; + char buf4[] = "0x10379aegi"; + char buf5[] = "0X10379aegi"; + char buf6[] = "01037aegi"; + char buf7[] = "1037aegi"; + char buf8[] = "++1037aegi"; + char buf9[] = "A1037aegi"; + char buf10[] = "a1037aegi"; + char buf11[] = "9223372036854775819"; + char buf17[] = "-92233720368547758199"; + char *stop; + char **stop1 = NULL; long ret; unsigned long retul; ret = strtol(buf3, &stop, 8); - zassert_equal(ret, 543, "strtol base = 8 failed"); + zassert_equal(ret, -543, "strtol base = 8 failed"); ret = strtol(buf1, &stop, 10); zassert_equal(ret, 10379, "strtol base = 10 failed"); - retul = strtoul(buf2, &stop, 10); - zassert_equal(ret, 10379, "strtoul base = 10 failed"); + ret = strtol(buf2, &stop, 10); + zassert_equal(ret, -10379, "strtol base = 10 failed"); ret = strtol(buf4, &stop, 16); zassert_equal(ret, 17004974, "strtol base = 16 failed"); + ret = strtol(buf4, &stop, 0); + zassert_equal(ret, 17004974, "strtol base = 16 failed"); + ret = strtol(buf5, &stop, 0); + zassert_equal(ret, 17004974, "strtol base = 16 failed"); + ret = strtol(buf6, &stop, 0); + zassert_equal(ret, 543, "strtol base = 8 failed"); + ret = strtol(buf7, &stop, 0); + zassert_equal(ret, 1037, "strtol base = 10 failed"); + ret = strtol(buf8, &stop, 10); + zassert_not_equal(ret, 1037, "strtol base = 10 failed"); + ret = strtol(buf9, &stop, 10); + zassert_not_equal(ret, 1037, "strtol base = 10 failed"); + ret = strtol(buf10, &stop, 10); + zassert_not_equal(ret, 1037, "strtol base = 10 failed"); + ret = strtol(buf11, stop1, 10); + zassert_equal(ret, LONG_MAX, "strtol base = 10 failed"); + ret = strtol(buf17, stop1, 10); + zassert_equal(ret, LONG_MIN, "strtol base = 10 failed"); + + retul = strtoul(buf3, &stop, 8); + zassert_equal(retul, -543, "strtoul base = 8 failed"); + retul = strtoul(buf1, &stop, 10); + zassert_equal(retul, 10379, "strtoul base = 10 failed"); + retul = strtoul(buf2, &stop, 10); + zassert_equal(retul, -10379, "strtol base = 10 failed"); + retul = strtoul(buf1, &stop, 10); + zassert_equal(retul, 10379, "strtoul base = 10 failed"); + retul = strtoul(buf4, &stop, 16); + zassert_equal(retul, 17004974, "strtoul base = 16 failed"); + retul = strtoul(buf4, &stop, 0); + zassert_equal(retul, 17004974, "strtoul base = 16 failed"); + retul = strtoul(buf5, &stop, 0); + zassert_equal(retul, 17004974, "strtoul base = 16 failed"); + retul = strtoul(buf6, &stop, 0); + zassert_equal(retul, 543, "strtoul base = 8 failed"); + retul = strtoul(buf7, &stop, 0); + zassert_equal(retul, 1037, "strtoul base = 10 failed"); + retul = strtoul(buf8, &stop, 10); + zassert_not_equal(retul, 1037, "strtoul base = 10 failed"); + retul = strtoul(buf9, &stop, 10); + zassert_not_equal(retul, 1037, "strtoul base = 10 failed"); + retul = strtoul(buf10, &stop, 10); + zassert_not_equal(retul, 1037, "strtoul base = 10 failed"); + retul = strtoul(buf17, stop1, 10); + zassert_not_equal(retul, 4294967299, "strtoul base = 10 failed"); + +#if LONG_MAX > 2147483647 + char buf12[] = "-9223372036854775809"; + char buf13[] = "9223372036854775800"; + char buf14[] = "18446744073709551719"; + char buf15[] = "-184467440737095516190"; + char buf16[] = "18446744073709551610"; + + ret = strtol(buf12, &stop, 10); + zassert_not_equal((ret + LONG_MAX), 0, "strtol base = 10 failed"); + ret = strtol(buf13, &stop, 10); + zassert_equal((ret - 9223372036854775800), 0, "strtol base = 10 failed"); + + retul = strtoul(buf14, &stop, 10); + zassert_not_equal((retul + LONG_MAX), 0, "strtoul base = 10 failed"); + retul = strtoul(buf15, &stop, 10); + zassert_not_equal((retul + LONG_MAX), 0, "strtoul base = 10 failed"); + retul = strtoul(buf16, &stop, 10); + zassert_not_equal((retul - 1844674407370955161), 0, "strtoul base = 10 failed"); +#else + char buf12[] = "-2147483649"; + char buf13[] = "2147483640"; + char buf14[] = "4294967299"; + char buf15[] = "-42949673990"; + char buf16[] = "4294967290"; + + ret = strtol(buf12, &stop, 10); + zassert_not_equal(ret, -2147483649, "strtol base = 10 failed"); + ret = strtol(buf13, &stop, 10); + zassert_equal(ret, 2147483640, "strtol base = 10 failed"); + + retul = strtoul(buf14, stop1, 10); + zassert_not_equal(retul, 4294967299, "strtoul base = 10 failed"); + retul = strtoul(buf15, &stop, 10); + zassert_not_equal(retul, -42949673990, "strtoul base = 10 failed"); + retul = strtoul(buf16, &stop, 10); + zassert_equal(retul, 4294967290, "strtoul base = 10 failed"); +#endif } /** @@ -555,9 +711,9 @@ void test_strtoxl(void) */ void test_tolower_toupper(void) { - static const char test[] = "Az09Za\t\n#!"; - static const char toup[] = "AZ09ZA\t\n#!"; - static const char tolw[] = "az09za\t\n#!"; + static const char test[] = "Az09Za{#!"; + static const char toup[] = "AZ09ZA{#!"; + static const char tolw[] = "az09za{#!"; char up[11]; char lw[11]; int i = 0; @@ -612,6 +768,68 @@ void test_strtok_r(void) test_strtok_r_do("1|2|3,4|5", "| ", 5, tc01, false); } +/** + * + * @brief Test time function + * + */ + +void test_time(void) +{ + time_t tests1 = 0; + time_t tests2 = -5; + time_t tests3 = -214748364800; + time_t tests4 = 951868800; + + struct tm tp; + + zassert_not_null(gmtime(&tests1), "gmtime failed"); + zassert_not_null(gmtime(&tests2), "gmtime failed"); + + tp.tm_wday = -5; + zassert_not_null(gmtime_r(&tests3, &tp), "gmtime_r failed"); + zassert_not_null(gmtime_r(&tests4, &tp), "gmtime_r failed"); +} + +/** + * + * @brief test abort functions + * + */ +void test_abort(void) +{ + int a = 0; + + ztest_set_fault_valid(true); + abort(); + zassert_equal(a, 0, "abort failed"); +} + +/** + * + * @brief test _exit functions + * + */ +static void exit_program(void *p1, void *p2, void *p3) +{ + _exit(1); +} + +void test_exit(void) +{ + int a = 0; + + k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, exit_program, + NULL, NULL, NULL, K_PRIO_PREEMPT(0), 0, K_NO_WAIT); + k_sleep(K_MSEC(10)); + k_thread_abort(tid); + zassert_equal(a, 0, "exit failed"); +} + +/** + * @} + */ + void test_main(void) { ztest_test_suite(test_c_lib, @@ -631,8 +849,13 @@ void test_main(void) ztest_unit_test(test_bsearch), ztest_unit_test(test_abs), ztest_unit_test(test_atoi), + ztest_unit_test(test_strncmp), + ztest_unit_test(test_strtoxl), ztest_unit_test(test_checktype), ztest_unit_test(test_memstr), + ztest_unit_test(test_time), + 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) diff --git a/tests/lib/c_lib/testcase.yaml b/tests/lib/c_lib/testcase.yaml index 94d3f060e9c..33dfe0983a9 100644 --- a/tests/lib/c_lib/testcase.yaml +++ b/tests/lib/c_lib/testcase.yaml @@ -1,3 +1,4 @@ tests: libraries.libc: - tags: clib + tags: clib ignore_faults + platform_exclude: native_posix native_posix_64 nrf52_bsim diff --git a/tests/lib/mem_alloc/prj_negative_testing.conf b/tests/lib/mem_alloc/prj_negative_testing.conf new file mode 100644 index 00000000000..c4afa8be40f --- /dev/null +++ b/tests/lib/mem_alloc/prj_negative_testing.conf @@ -0,0 +1,3 @@ +CONFIG_ZTEST=y +CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=0 +CONFIG_TEST_USERSPACE=y diff --git a/tests/lib/mem_alloc/src/main.c b/tests/lib/mem_alloc/src/main.c index 1880c3318ef..16a69c436b2 100644 --- a/tests/lib/mem_alloc/src/main.c +++ b/tests/lib/mem_alloc/src/main.c @@ -22,6 +22,15 @@ #include #include +/** + * + * @brief Test implementation-defined constants library + * @defgroup libc_api + * @ingroup all_tests + * @{ + * + */ + #define BUF_LEN 10 @@ -136,6 +145,25 @@ void test_malloc(void) free(iptr); iptr = NULL; } +#if (CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE == 0) +void test_no_mem_malloc(void) +{ + int *iptr = NULL; + + iptr = malloc(BUF_LEN); + zassert_is_null((iptr), "malloc failed, errno: %d", errno); + free(iptr); + iptr = NULL; +} +void test_no_mem_realloc(void) +{ + char *ptr = NULL; + char *reloc_ptr = NULL; + + reloc_ptr = realloc(ptr, BUF_LEN); + zassert_is_null(reloc_ptr, "realloc failed, errno: %d", errno); +} +#endif /** * @brief Test dynamic memory allocation free function @@ -151,27 +179,6 @@ void test_free(void) free(NULL); } -/** - * @brief Test dynamic memory allocation using calloc - * - * @see calloc(), free() - */ -#define CALLOC_BUFLEN (200) -static ZTEST_BMEM unsigned char zerobuf[CALLOC_BUFLEN]; - -void test_calloc(void) -{ - char *cptr = NULL; - - cptr = calloc(CALLOC_BUFLEN, sizeof(char)); - zassert_not_null((cptr), "calloc failed, errno: %d", errno); - zassert_true(((memcmp(cptr, zerobuf, CALLOC_BUFLEN)) == 0), - "calloc failed to set zero value, errno: %d", errno); - memset(cptr, 'p', CALLOC_BUFLEN); - free(cptr); - cptr = NULL; -} - /** * @brief Test dynamic memory allocation using realloc * @@ -215,11 +222,49 @@ void test_reallocarray(void) /* reallocarray not implemented for newlib */ ztest_test_skip(); } +void test_calloc(void) +{ + ztest_test_skip(); +} #else +/** + * @brief Test dynamic memory allocation using calloc + * + * @see calloc(), free() + */ +#define CALLOC_BUFLEN (200) +static ZTEST_BMEM unsigned char zerobuf[CALLOC_BUFLEN]; + +void test_calloc(void) +{ + char *cptr = NULL; + + cptr = calloc(0x7fffffff, sizeof(int)); + zassert_is_null((cptr), "calloc failed, errno: %d", errno); + + cptr = calloc(0x7fffffff, sizeof(char)); + zassert_is_null((cptr), "calloc failed, errno: %d", errno); + + + cptr = calloc(CALLOC_BUFLEN, sizeof(char)); + zassert_not_null((cptr), "calloc failed, errno: %d", errno); + zassert_true(((memcmp(cptr, zerobuf, CALLOC_BUFLEN)) == 0), + "calloc failed to set zero value, errno: %d", errno); + memset(cptr, 'p', CALLOC_BUFLEN); + free(cptr); + cptr = NULL; +} + void test_reallocarray(void) { char orig_size = BUF_LEN; char *ptr = NULL; + char *cptr = NULL; + + cptr = reallocarray(ptr, 0x7fffffff, sizeof(int)); + zassert_is_null((ptr), "reallocarray failed, errno: %d", errno); + zassert_is_null((cptr), "reallocarray failed, errno: %d"); + free(cptr); ptr = malloc(orig_size); @@ -274,6 +319,10 @@ void test_memalloc_all(void) reloc_ptr = NULL; } +/** + * @} + */ + /** * * @brief Test dynamic memory allocation upto maximum size @@ -293,6 +342,15 @@ __no_optimization void test_memalloc_max(void) void test_main(void) { +#if (CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE == 0) +#ifndef CONFIG_NEWLIB_LIBC + ztest_test_suite(test_c_lib_dynamic_memalloc, + ztest_user_unit_test(test_no_mem_malloc), + ztest_user_unit_test(test_no_mem_realloc) + ); + ztest_run_test_suite(test_c_lib_dynamic_memalloc); +#endif +#else ztest_test_suite(test_c_lib_dynamic_memalloc, ztest_user_unit_test(test_malloc_align), ztest_user_unit_test(test_malloc), @@ -304,4 +362,5 @@ void test_main(void) ztest_user_unit_test(test_memalloc_max) ); ztest_run_test_suite(test_c_lib_dynamic_memalloc); +#endif } diff --git a/tests/lib/mem_alloc/testcase.yaml b/tests/lib/mem_alloc/testcase.yaml index 96059af7a02..fc1bbba3f5e 100644 --- a/tests/lib/mem_alloc/testcase.yaml +++ b/tests/lib/mem_alloc/testcase.yaml @@ -2,16 +2,21 @@ tests: libraries.libc.minimal.mem_alloc: extra_args: CONF_FILE=prj.conf arch_exclude: posix - platform_exclude: twr_ke18f + platform_exclude: twr_ke18f native_posix_64 nrf52_bsim tags: clib minimal_libc userspace libraries.libc.newlib: min_ram: 16 extra_args: CONF_FILE=prj_newlib.conf arch_exclude: posix - platform_exclude: twr_ke18f + platform_exclude: twr_ke18f native_posix_64 nrf52_bsim filter: TOOLCHAIN_HAS_NEWLIB == 1 tags: clib newlib userspace libraries.libc.newlibnano: extra_args: CONF_FILE=prj_newlibnano.conf filter: CONFIG_HAS_NEWLIB_LIBC_NANO tags: clib newlib userspace + libraries.libc.minimal.mem_alloc_negative_testing: + extra_args: CONF_FILE=prj_negative_testing.conf + arch_exclude: posix + platform_exclude: twr_ke18f native_posix_64 nrf52_bsim + tags: clib minimal_libc userspace diff --git a/tests/lib/sprintf/prj.conf b/tests/lib/sprintf/prj.conf index b93a5675d97..fa9542e0757 100644 --- a/tests/lib/sprintf/prj.conf +++ b/tests/lib/sprintf/prj.conf @@ -1,2 +1,4 @@ CONFIG_ZTEST=y CONFIG_FPU=y +CONFIG_TEST_USERSPACE=y +CONFIG_ZTEST_FATAL_HOOK=y diff --git a/tests/lib/sprintf/prj_new.conf b/tests/lib/sprintf/prj_new.conf new file mode 100644 index 00000000000..880d26742b5 --- /dev/null +++ b/tests/lib/sprintf/prj_new.conf @@ -0,0 +1,5 @@ +CONFIG_STDOUT_CONSOLE=n +CONFIG_ZTEST=y +CONFIG_FPU=y +CONFIG_TEST_USERSPACE=y +CONFIG_ZTEST_FATAL_HOOK=y diff --git a/tests/lib/sprintf/src/main.c b/tests/lib/sprintf/src/main.c index 90f391649c3..7c8913548e0 100644 --- a/tests/lib/sprintf/src/main.c +++ b/tests/lib/sprintf/src/main.c @@ -14,6 +14,16 @@ #include #include #include +#include + +/** + * + * @brief Test implementation-defined constants library + * @defgroup libc_api + * @ingroup all_tests + * @{ + * + */ #define DEADBEEF 0xdeadbeef @@ -719,6 +729,244 @@ void test_sprintf_string(void) "sprintf(%%s) of REALLY_LONG_STRING doesn't match!\n"); } +/** + * + * @brief Test print function + * + * @see fprintf(), printf(). + * + */ + +void test_print(void) +{ + int ret, i = 3; + FILE *p = NULL; + + ret = fprintf(stdout, "%d", i); + zassert_equal(ret, 1, "fprintf failed!"); + + ret = fprintf(p, "%d", i); + zassert_not_equal(ret, 1, "fprintf failed!"); + + ret = fprintf(stdout, "", i); + zassert_not_equal(ret, 1, "fprintf failed!"); + + ret = printf("%d", 3); + zassert_equal(ret, 1, "printf failed!"); + + ret = printf("", 3); + zassert_not_equal(ret, 1, "printf failed!"); +} + +void test_null_fprint(void) +{ + int ret, i = 3; + + ztest_set_fault_valid(true); + ret = fprintf(NULL, "%d", i); + zassert_not_equal(ret, 1, "fprintf failed!"); +} + +/** + * + * @brief Test vfprintf function + * + */ + +static int WriteFrmtd_vf(FILE *stream, char *format, ...) +{ + int ret; + va_list args; + + va_start(args, format); + ret = vfprintf(stream, format, args); + va_end(args); + + return ret; +} + +void test_vfprintf(void) +{ + int ret; + + ret = WriteFrmtd_vf(stdout, "This %0-d", 3); + zassert_equal(ret, 6, "vfprintf \"This 3\" failed"); + + ret = WriteFrmtd_vf(stdout, "%999999999999ed", 3); + zassert_equal(ret, 15, "vfprintf \"3\" failed"); + + ret = WriteFrmtd_vf(stdout, "", 3); + zassert_equal(ret, 0, "vfprintf \"3\" failed"); + + ret = WriteFrmtd_vf(stdout, "/%%/%c/", 'a'); + zassert_equal(ret, 5, "vfprintf \'a\' failed"); + + ret = WriteFrmtd_vf(stdout, "11", 'a'); + zassert_equal(ret, 2, "vfprintf \'a\' failed"); +} + +void test_null_vfprintf(void) +{ + int ret; + + ret = WriteFrmtd_vf(NULL, "This %d", 3); + zassert_not_equal(ret, 6, "vfprintf \"This 3\" failed"); +} + +/** + * + * @brief Test vprintf function + * + */ + +static int WriteFrmtd_v(char *format, ...) +{ + int ret; + va_list args; + + va_start(args, format); + ret = vprintf(format, args); + va_end(args); + + return ret; +} + +void test_vprintf(void) +{ + int ret; + + ret = WriteFrmtd_v("This %d", 3); + zassert_equal(ret, 6, "vprintf \"This 3\" failed"); + + ret = WriteFrmtd_v("%999999999999ed", 3); + zassert_equal(ret, 15, "vprintf \"3\" failed"); + + ret = WriteFrmtd_v("", 3); + zassert_equal(ret, 0, "vprintf \"3\" failed"); + + ret = WriteFrmtd_v("/%%/%c/", 'a'); + zassert_equal(ret, 5, "vprintf \'a\' failed"); + + ret = WriteFrmtd_v("11", 'a'); + zassert_equal(ret, 2, "vprintf \'a\' failed"); +} + +/** + * + * @brief Test put function + * + * @see fputs(), puts(), fputc(), putc(). + */ + +void test_put(void) +{ + int ret; + FILE *p = NULL; + + ret = fputs("This 3", stdout); + zassert_equal(ret, 0, "fputs \"This 3\" failed"); + + ret = fputs("This 3", stderr); + zassert_equal(ret, 0, "fputs \"This 3\" failed"); + + ret = fputs("This 3", p); + zassert_not_equal(ret, 0, "fputs \"This 3\" failed"); + + ret = puts("This 3"); + zassert_equal(ret, 0, "puts \"This 3\" failed"); + + ret = fputc('T', stdout); + zassert_equal(ret, 84, "fputc \'T\' failed"); + + ret = fputc('T', p); + zassert_not_equal(ret, 84, "fputc \'T\' failed"); + + ret = putc('T', stdout); + zassert_equal(ret, 84, "putc \'T\' failed"); + + ret = putc('T', p); + zassert_not_equal(ret, 84, "putc \'T\' failed"); + + ret = fputc('T', stderr); + zassert_equal(ret, 84, "fputc \'T\' failed"); + + ret = fputc('T', stdin); + zassert_not_equal(ret, 84, "fputc \'T\' failed"); + + ret = fputc('T', p); + zassert_not_equal(ret, 84, "fputc \'T\' failed"); +} + +/** + * + * @brief Test fwrite function + * + */ + +void test_fwrite(void) +{ + int ret; + + ret = fwrite("This 3", 0, 0, stdout); + zassert_equal(ret, 0, "fwrite failed!"); + + ret = fwrite("This 3", 0, 4, stdout); + zassert_equal(ret, 0, "fwrite failed!"); + + ret = fwrite("This 3", 4, 4, stdout); + zassert_not_equal(ret, 0, "fwrite failed!"); + + ret = fwrite("This 3", 4, 4, stdin); + zassert_equal(ret, 0, "fwrite failed!"); +} + +void test_fwrite_err_size(void) +{ + int ret; + + ztest_set_fault_valid(true); + ret = fwrite("This 3", -1, 0, stdout); + zassert_equal(ret, 0, "fwrite failed!"); +} + +void test_fwrite_err_item(void) +{ + int ret; + + ztest_set_fault_valid(true); + ret = fwrite("This 3", 0, -1, stdout); + zassert_equal(ret, 0, "fwrite failed!"); +} + + +/** + * + * @brief Test stdout_hook_default function + * + */ + +void test_EOF(void) +{ + int ret; + + ret = fputc('T', stdout); + zassert_equal(ret, EOF, "fputc \'T\' failed"); + + ret = fputs("This 3", stdout); + zassert_equal(ret, EOF, "fputs \"This 3\" failed"); + + ret = puts("This 3"); + zassert_equal(ret, EOF, "puts \"This 3\" failed"); + + ret = WriteFrmtd_vf(stdout, "This %d", 3); + printk("%d\n", ret); + zassert_equal(ret, EOF, "vfprintf \"3\" failed"); +} + +/** + * @} + */ + /** * * @brief Test entry point @@ -728,12 +976,28 @@ void test_sprintf_string(void) void test_main(void) { +#ifndef CONFIG_STDOUT_CONSOLE + ztest_test_suite(test_sprintf, + ztest_user_unit_test(test_EOF)); + ztest_run_test_suite(test_sprintf); +#else ztest_test_suite(test_sprintf, ztest_unit_test(test_sprintf_double), ztest_unit_test(test_sprintf_integer), ztest_unit_test(test_vsprintf), ztest_unit_test(test_vsnprintf), ztest_unit_test(test_sprintf_string), + ztest_unit_test(test_snprintf), + ztest_unit_test(test_print), + ztest_unit_test(test_null_fprint), + ztest_unit_test(test_vfprintf), + ztest_unit_test(test_null_vfprintf), + ztest_unit_test(test_vprintf), + ztest_user_unit_test(test_put), + ztest_user_unit_test(test_fwrite), + ztest_user_unit_test(test_fwrite_err_size), + ztest_user_unit_test(test_fwrite_err_item), ztest_unit_test(test_sprintf_misc)); ztest_run_test_suite(test_sprintf); +#endif } diff --git a/tests/lib/sprintf/testcase.yaml b/tests/lib/sprintf/testcase.yaml index 661a1351c31..aff5053617b 100644 --- a/tests/lib/sprintf/testcase.yaml +++ b/tests/lib/sprintf/testcase.yaml @@ -1,7 +1,12 @@ tests: libraries.libc.sprintf: + extra_args: CONF_FILE=prj.conf filter: not CONFIG_SOC_MCIMX7_M4 - tags: libc + tags: libc ignore_faults integration_platforms: - - native_posix - native_posix_64 + platform_exclude: native_posix native_posix_64 nrf52_bsim + libraries.libc.sprintf_new: + extra_args: CONF_FILE=prj_new.conf + tags: libc + platform_exclude: native_posix native_posix_64 nrf52_bsim