Skip to content

Commit

Permalink
ztest API Migration: lib/c_lib
Browse files Browse the repository at this point in the history
Migrate the unit tests at `lib/c_lib` to use the new ztest API.

Signed-off-by: Tristan Honscheid <[email protected]>
  • Loading branch information
tristan-google authored and nashif committed Apr 26, 2022
1 parent d8f0c1a commit 8283eb3
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 97 deletions.
1 change: 1 addition & 0 deletions tests/lib/c_lib/prj.conf
Original file line number Diff line number Diff line change
@@ -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
129 changes: 34 additions & 95 deletions tests/lib/c_lib/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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");
Expand All @@ -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");
Expand All @@ -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);

Expand Down Expand Up @@ -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';
Expand All @@ -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 */
Expand All @@ -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;
Expand All @@ -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";

Expand All @@ -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");
Expand All @@ -262,7 +264,7 @@ void test_strcpy(void)
*
* @see strncpy().
*/
void test_strncpy(void)
ZTEST(test_c_lib, test_strncpy)
{
int ret;

Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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";
Expand Down Expand Up @@ -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 };
Expand All @@ -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;

Expand All @@ -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");
Expand All @@ -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";
Expand Down Expand Up @@ -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";

Expand All @@ -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};
Expand Down Expand Up @@ -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};
Expand All @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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{#!";
Expand Down Expand Up @@ -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" };

Expand All @@ -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;
Expand All @@ -1061,7 +1063,7 @@ void test_time(void)
* @brief Test rand function
*
*/
void test_rand(void)
ZTEST(test_c_lib, test_rand)
{
int a;

Expand All @@ -1075,7 +1077,7 @@ void test_rand(void)
* @brief Test srand function
*
*/
void test_srand(void)
ZTEST(test_c_lib, test_srand)
{
int a;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1168,7 +1170,7 @@ void test_rand_reproducibility(void)
*
* @see abort().
*/
void test_abort(void)
ZTEST(test_c_lib, test_abort)
{
int a = 0;

Expand All @@ -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;

Expand All @@ -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);
}
9 changes: 7 additions & 2 deletions tests/lib/c_lib/src/test_qsort.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 8283eb3

Please sign in to comment.