Tests: libc: Improve code coverage

Add test cases for libc module  APIs.

Signed-off-by: shixiongx zhang <shixiongx.zhang@intel.com>
This commit is contained in:
shixiongx zhang 2021-03-04 14:31:06 +08:00 committed by Carles Cufí
commit 84d23fd82f
10 changed files with 635 additions and 66 deletions

View file

@ -1 +1,3 @@
CONFIG_ZTEST=y
CONFIG_ZTEST=y
CONFIG_TEST_USERSPACE=y
CONFIG_ZTEST_FATAL_HOOK=y

View file

@ -25,10 +25,19 @@
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <ctype.h>
#include <time.h>
#include <ztest_error_hook.h>
#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)

View file

@ -1,3 +1,4 @@
tests:
libraries.libc:
tags: clib
tags: clib ignore_faults
platform_exclude: native_posix native_posix_64 nrf52_bsim

View file

@ -0,0 +1,3 @@
CONFIG_ZTEST=y
CONFIG_MINIMAL_LIBC_MALLOC_ARENA_SIZE=0
CONFIG_TEST_USERSPACE=y

View file

@ -22,6 +22,15 @@
#include <errno.h>
#include <time.h>
/**
*
* @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
}

View file

@ -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

View file

@ -1,2 +1,4 @@
CONFIG_ZTEST=y
CONFIG_FPU=y
CONFIG_TEST_USERSPACE=y
CONFIG_ZTEST_FATAL_HOOK=y

View file

@ -0,0 +1,5 @@
CONFIG_STDOUT_CONSOLE=n
CONFIG_ZTEST=y
CONFIG_FPU=y
CONFIG_TEST_USERSPACE=y
CONFIG_ZTEST_FATAL_HOOK=y

View file

@ -14,6 +14,16 @@
#include <ztest.h>
#include <stdio.h>
#include <stdarg.h>
#include <ztest_error_hook.h>
/**
*
* @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
}

View file

@ -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