1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * @file test access to the minimal C libraries
9  *
10  * This module verifies that the various minimal C libraries can be used.
11  *
12  * IMPORTANT: The module only ensures that each supported library is present,
13  * and that a bare minimum of its functionality is operating correctly. It does
14  * NOT guarantee that ALL standards-defined functionality is present, nor does
15  * it guarantee that ALL functionality provided is working correctly.
16  */
17 
18 #if defined(CONFIG_NATIVE_LIBC)
19 #undef _POSIX_C_SOURCE
20 #define _POSIX_C_SOURCE 200809L
21 #endif
22 
23 #include <zephyr/kernel.h>
24 #include <zephyr/sys/__assert.h>
25 #include <zephyr/ztest.h>
26 
27 #include <limits.h>
28 #include <sys/types.h>
29 #include <stdbool.h>
30 #include <stddef.h>
31 #include <stdint.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <ctype.h>
38 #include <time.h>
39 #include <zephyr/ztest_error_hook.h>
40 #ifdef CONFIG_PICOLIBC
41 #include <unistd.h>
42 #endif
43 #ifdef CONFIG_NEWLIB_LIBC
44 #include <unistd.h>
45 #endif
46 
47 #define STACK_SIZE (512 + CONFIG_TEST_EXTRA_STACK_SIZE)
48 #define LIST_LEN 2
49 
50 /* Recent GCC's are issuing a warning for the truncated strncpy()
51  * below (the static source string is longer than the locally-defined
52  * destination array).  That's exactly the case we're testing, so turn
53  * it off.
54  */
55 #if defined(__GNUC__) && __GNUC__ >= 8
56 #pragma GCC diagnostic ignored "-Wstringop-truncation"
57 #endif
58 
59 ZTEST_SUITE(libc_common, NULL, NULL, NULL, NULL, NULL);
60 
61 /*
62  * variables used during limits library testing; must be marked as "volatile"
63  * to prevent compiler from computing results at compile time
64  */
65 
66 volatile long long_max = LONG_MAX;
67 volatile long long_one = 1L;
68 
69 /**
70  *
71  * @brief Test implementation-defined constants library
72  * @defgroup libc_api
73  * @ingroup all_tests
74  * @{
75  *
76  */
77 
ZTEST(libc_common,test_limits)78 ZTEST(libc_common, test_limits)
79 {
80 
81 	zassert_true((long_max + long_one == LONG_MIN));
82 }
83 
foobar(void)84 static ssize_t foobar(void)
85 {
86 	return -1;
87 }
88 
ZTEST(libc_common,test_ssize_t)89 ZTEST(libc_common, test_ssize_t)
90 {
91 	zassert_true(foobar() < 0);
92 }
93 
94 /**
95  *
96  * @brief Test boolean types and values library
97  *
98  */
ZTEST(libc_common,test_stdbool)99 ZTEST(libc_common, test_stdbool)
100 {
101 
102 	zassert_true((true == 1), "true value");
103 	zassert_true((false == 0), "false value");
104 }
105 
106 /*
107  * variables used during stddef library testing; must be marked as "volatile"
108  * to prevent compiler from computing results at compile time
109  */
110 
111 volatile long long_variable;
112 volatile size_t size_of_long_variable = sizeof(long_variable);
113 
114 /**
115  *
116  * @brief Test standard type definitions library
117  *
118  */
ZTEST(libc_common,test_stddef)119 ZTEST(libc_common, test_stddef)
120 {
121 #ifdef CONFIG_64BIT
122 	zassert_true((size_of_long_variable == 8), "sizeof");
123 #else
124 	zassert_true((size_of_long_variable == 4), "sizeof");
125 #endif
126 }
127 
128 /*
129  * variables used during stdint library testing; must be marked as "volatile"
130  * to prevent compiler from computing results at compile time
131  */
132 
133 volatile uint8_t unsigned_byte = 0xff;
134 volatile uint32_t unsigned_int = 0xffffff00;
135 
136 /**
137  *
138  * @brief Test integer types library
139  *
140  */
ZTEST(libc_common,test_stdint)141 ZTEST(libc_common, test_stdint)
142 {
143 	zassert_true((unsigned_int + unsigned_byte + 1u == 0U));
144 
145 #if (UINT8_C(1) == 1)			\
146 	&& (INT8_C(-1) == -1)		\
147 	&& (UINT16_C(2) == 2)		\
148 	&& (INT16_C(-2) == -2)		\
149 	&& (UINT32_C(4) == 4)		\
150 	&& (INT32_C(-4) == -4)		\
151 	&& (UINT64_C(8) == 8)		\
152 	&& (INT64_C(-8) == -8)		\
153 	&& (UINTMAX_C(11) == 11)	\
154 	&& (INTMAX_C(-11) == -11)
155 	zassert_true(true);
156 #else
157 	zassert_true(false, "const int expr values ...");
158 #endif
159 }
160 
161 /**
162  *
163  * @brief Test time_t to make sure it is at least 64 bits
164  *
165  */
ZTEST(libc_common,test_time_t)166 ZTEST(libc_common, test_time_t)
167 {
168 #ifdef CONFIG_EXTERNAL_LIBC
169 	ztest_test_skip();
170 #else
171 	zassert_true(sizeof(time_t) >= sizeof(uint64_t));
172 #endif
173 }
174 
175 /*
176  * variables used during string library testing
177  */
178 
179 #define BUFSIZE 10
180 
181 char buffer[BUFSIZE];
182 
183 /**
184  *
185  * @brief Test string memset
186  *
187  */
ZTEST(libc_common,test_memset)188 ZTEST(libc_common, test_memset)
189 {
190 	int i, ret;
191 	const char set = 'a';
192 	int size = 0;
193 
194 	memset(buffer, 0, 10);
195 	for (i = 0; i < 10; i++) {
196 		memset(buffer + i, set, size);
197 		memset(buffer + i, set, 1);
198 		ret = memcmp(buffer + i, &set, 1);
199 		zassert_true((ret == 0), "memset buffer a failed");
200 	}
201 }
202 
203 /**
204  *
205  * @brief Test string length function
206  *
207  * @see strlen(), strnlen().
208  *
209  */
ZTEST(libc_common,test_strlen)210 ZTEST(libc_common, test_strlen)
211 {
212 	(void)memset(buffer, '\0', BUFSIZE);
213 	(void)memset(buffer, 'b', 5); /* 5 is BUFSIZE / 2 */
214 	zassert_equal(strlen(buffer), 5, "strlen failed");
215 
216 	zassert_equal(strnlen(buffer, 3), 3, "strnlen failed");
217 	zassert_equal(strnlen(buffer, BUFSIZE), 5, "strnlen failed");
218 }
219 
220 /**
221  *
222  * @brief Test string compare function
223  *
224  * @see strcmp(), strncasecmp().
225  *
226  */
ZTEST(libc_common,test_strcmp)227 ZTEST(libc_common, test_strcmp)
228 {
229 	strcpy(buffer, "eeeee");
230 	char test = 0;
231 
232 	zassert_true((strcmp(buffer, "fffff") < 0), "strcmp less ...");
233 	zassert_str_equal(buffer, "eeeee", "strcmp equal ...");
234 	zassert_true((strcmp(buffer, "ddddd") > 0), "strcmp greater ...");
235 
236 	zassert_true((strncasecmp(buffer, "FFFFF", 3) < 0), "strncasecmp less ...");
237 	zassert_true((strncasecmp(buffer, "DDDDD", 3) > 0), "strncasecmp equal ...");
238 	zassert_true((strncasecmp(buffer, "EEEEE", 3) == 0), "strncasecmp greater ...");
239 	zassert_true((strncasecmp(&test, &test, 1) == 0), "strncasecmp failed");
240 }
241 
242 /**
243  *
244  * @brief Test string N compare function
245  *
246  * @see strncmp().
247  */
ZTEST(libc_common,test_strncmp)248 ZTEST(libc_common, test_strncmp)
249 {
250 	static const char pattern[] = "eeeeeeeeeeee";
251 
252 	/* Note we don't want to count the final \0 that sizeof will */
253 	__ASSERT_NO_MSG(sizeof(pattern) - 1 > BUFSIZE);
254 	memcpy(buffer, pattern, BUFSIZE);
255 
256 	zassert_true((strncmp(buffer, "fffff", 0) == 0), "strncmp 0");
257 	zassert_true((strncmp(buffer, "eeeff", 3) == 0), "strncmp 3");
258 	zassert_true((strncmp(buffer, "eeeff", 4) != 0), "strncmp 4");
259 	zassert_true((strncmp(buffer, "eeeeeeeeeeeff", BUFSIZE) == 0),
260 		     "strncmp 10");
261 
262 	/* test compare the same strings */
263 	buffer[BUFSIZE - 1] = '\0';
264 	zassert_true((strncmp(buffer, buffer, BUFSIZE) == 0),
265 				 "strncmp 10 with \0");
266 }
267 
268 
269 /**
270  *
271  * @brief Test string copy function
272  *
273  * @see strcpy().
274  */
ZTEST(libc_common,test_strcpy)275 ZTEST(libc_common, test_strcpy)
276 {
277 	(void)memset(buffer, '\0', BUFSIZE);
278 	strcpy(buffer, "10 chars!\0");
279 
280 	zassert_str_equal(buffer, "10 chars!\0", "strcpy");
281 }
282 
283 /**
284  *
285  * @brief Test string N copy function
286  *
287  * @see strncpy().
288  */
ZTEST(libc_common,test_strncpy)289 ZTEST(libc_common, test_strncpy)
290 {
291 	int ret;
292 
293 	(void)memset(buffer, '\0', BUFSIZE);
294 	strncpy(buffer, "This is over 10 characters", BUFSIZE);
295 
296 	/* Purposely different values */
297 	ret = strncmp(buffer, "This is over 20 characters", BUFSIZE);
298 	zassert_true((ret == 0), "strncpy");
299 
300 }
301 
302 /**
303  *
304  * @brief Test string scanning function
305  *
306  * @see strchr().
307  */
ZTEST(libc_common,test_strchr)308 ZTEST(libc_common, test_strchr)
309 {
310 	char *rs = NULL;
311 	int ret;
312 
313 	(void)memset(buffer, '\0', BUFSIZE);
314 	strncpy(buffer, "Copy 10", BUFSIZE);
315 
316 	rs = strchr(buffer, '1');
317 
318 	zassert_not_null(rs, "strchr");
319 
320 
321 	ret = strncmp(rs, "10", 2);
322 	zassert_true((ret == 0), "strchr");
323 
324 }
325 
326 /**
327  *
328  * @brief Test string prefix match functions
329  *
330  * @see strspn(),strcspn().
331  */
ZTEST(libc_common,test_strxspn)332 ZTEST(libc_common, test_strxspn)
333 {
334 	const char *empty = "";
335 	const char *cset = "abc";
336 
337 	zassert_true(strspn("", empty) == 0U, "strspn empty empty");
338 	zassert_true(strcspn("", empty) == 0U, "strcspn empty empty");
339 
340 	zassert_true(strspn("abde", cset) == 2U, "strspn match");
341 	zassert_true(strcspn("abde", cset) == 0U, "strcspn nomatch");
342 
343 	zassert_true(strspn("da", cset) == 0U, "strspn nomatch");
344 	zassert_true(strcspn("da", cset) == 1U, "strcspn match");
345 
346 	zassert_true(strspn("abac", cset) == 4U, "strspn all");
347 	zassert_true(strcspn("defg", cset) == 4U, "strcspn all");
348 }
349 
350 /**
351  *
352  * @brief Test memory comparison function
353  *
354  * @see memcmp()
355  */
ZTEST(libc_common,test_memcmp)356 ZTEST(libc_common, test_memcmp)
357 {
358 	int ret;
359 	unsigned char m1[] = "a\0$def";
360 	unsigned char m2[] = "a\0$dhj";
361 
362 
363 	ret = memcmp(m1, m2, 4);
364 	zassert_true((ret == 0), "memcmp four characters failed");
365 
366 	ret = memcmp(m1, m2, 5);
367 	zassert_true((ret != 0), "memcmp five characters failed");
368 
369 	ret = memcmp(m1, m2, 0);
370 	zassert_true((ret == 0), "memcmp zero character failed");
371 
372 	ret = memcmp(m1, m2, sizeof(m2));
373 	zassert_true((ret != 0), "memcmp 2 block of memory failed");
374 }
375 
376 /**
377  *
378  * @brief Test binary search function
379  *
380  * @see bsearch()
381  */
cmp_func(const void * a,const void * b)382 int cmp_func(const void *a, const void *b)
383 {
384 	return (*(int *)a - *(int *)b);
385 }
386 
ZTEST(libc_common,test_bsearch)387 ZTEST(libc_common, test_bsearch)
388 {
389 	void *result = NULL;
390 	int arr[5] = { 2, 5, 20, 50, 60 };
391 	size_t size = ARRAY_SIZE(arr);
392 	int key = 30;
393 
394 	result = (int *)bsearch(&key, arr, size, sizeof(int), cmp_func);
395 	zassert_is_null(result, "bsearch -key not found");
396 
397 	key = 60;
398 	result = (int *)bsearch(&key, arr, size, sizeof(int), cmp_func);
399 	zassert_not_null(result, "bsearch -key found");
400 }
401 
402 /**
403  *
404  * @brief Test abs function
405  *
406  * @see abs()
407  */
ZTEST(libc_common,test_abs)408 ZTEST(libc_common, test_abs)
409 {
410 	int val = -5, value = 5;
411 
412 	zassert_equal(abs(val), 5, "abs -5");
413 	zassert_equal(abs(value), 5, "abs 5");
414 }
415 
416 /**
417  *
418  * @brief Test atoi function
419  *
420  * @see atoi()
421  */
ZTEST(libc_common,test_atoi)422 ZTEST(libc_common, test_atoi)
423 {
424 	zassert_equal(atoi("123"), 123, "atoi error");
425 	zassert_equal(atoi("2c5"), 2, "atoi error");
426 	zassert_equal(atoi("acd"), 0, "atoi error");
427 	zassert_equal(atoi(" "), 0, "atoi error");
428 	zassert_equal(atoi(""), 0, "atoi error");
429 	zassert_equal(atoi("3-4e"), 3, "atoi error");
430 	zassert_equal(atoi("8+1c"), 8, "atoi error");
431 	zassert_equal(atoi("+3"), 3, "atoi error");
432 	zassert_equal(atoi("-1"), -1, "atoi error");
433 }
434 
435 /**
436  *
437  * @brief Test value type
438  *
439  * @details This function check the char type,
440  * and verify the return value.
441  *
442  * @see isalnum(), isalpha(), isdigit(), isgraph(),
443  * isprint(), isspace(), isupper(), isxdigit().
444  *
445  */
ZTEST(libc_common,test_checktype)446 ZTEST(libc_common, test_checktype)
447 {
448 	static const char exp_alnum[] =
449 		"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
450 	static const char exp_alpha[] =
451 		"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
452 	static const char exp_digit[] = "0123456789";
453 	static const char exp_graph[] =
454 		"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
455 	static const char exp_print[] =
456 		" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";
457 	static const char exp_space[] = {"\x9\xa\xb\xc\xd\x20"};
458 
459 	static const char exp_upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
460 	static const char exp_xdigit[] = "0123456789ABCDEFabcdef";
461 	char buf[128];
462 	char *ptr = buf;
463 
464 	for (int i = 0; i < 128; i++) {
465 		if (isalnum(i) != 0) {
466 			*ptr++ = i;
467 		}
468 	}
469 	*ptr = '\0';
470 	zassert_str_equal(buf, exp_alnum, "isalnum error");
471 
472 	ptr = buf;
473 	for (int i = 0; i < 128; i++) {
474 		if (isalpha(i) != 0) {
475 			*ptr++ = i;
476 		}
477 	}
478 	*ptr = '\0';
479 	zassert_str_equal(buf, exp_alpha, "isalpha error");
480 
481 	ptr = buf;
482 	for (int i = 0; i < 128; i++) {
483 		if (isdigit(i) != 0) {
484 			*ptr++ = i;
485 		}
486 	}
487 	*ptr = '\0';
488 	zassert_str_equal(buf, exp_digit, "isdigit error");
489 
490 	ptr = buf;
491 	for (int i = 0; i < 128; i++) {
492 		if (isgraph(i) != 0) {
493 			*ptr++ = i;
494 		}
495 	}
496 	*ptr = '\0';
497 	zassert_str_equal(buf, exp_graph, "isgraph error");
498 
499 	ptr = buf;
500 	for (int i = 0; i < 128; i++) {
501 		if (isprint(i) != 0) {
502 			*ptr++ = i;
503 		}
504 	}
505 	*ptr = '\0';
506 	zassert_str_equal(buf, exp_print, "isprint error");
507 
508 	ptr = buf;
509 	for (int i = 0; i < 128; i++) {
510 		if (isupper(i) != 0) {
511 			*ptr++ = i;
512 		}
513 	}
514 	*ptr = '\0';
515 	zassert_str_equal(buf, exp_upper, "isupper error");
516 
517 	ptr = buf;
518 	for (int i = 0; i < 128; i++) {
519 		if (isspace(i) != 0) {
520 			*ptr++ = i;
521 		}
522 	}
523 	*ptr = '\0';
524 	zassert_str_equal(buf, exp_space, "isspace error");
525 
526 	ptr = buf;
527 	for (int i = 0; i < 128; i++) {
528 		if (isxdigit(i) != 0) {
529 			*ptr++ = i;
530 		}
531 	}
532 	*ptr = '\0';
533 	zassert_str_equal(buf, exp_xdigit, "isxdigit error");
534 }
535 
536 /**
537  * @brief Test memchr function
538  *
539  * @see memchr().
540  */
ZTEST(libc_common,test_memchr)541 ZTEST(libc_common, test_memchr)
542 {
543 	static const char str[] = "testfunction";
544 
545 	/* verify the character inside the count scope */
546 	zassert_not_null(memchr(str, 'e', strlen(str)), "memchr serach e");
547 	zassert_not_null(memchr(str, '\0', strlen(str)+1), "memchr serach \0");
548 
549 	/* verify when the count parm is zero */
550 	zassert_is_null(memchr(str, 't', 0), "memchr count 0 error");
551 	/* verify the wanted character outside the count scope */
552 	zassert_is_null(memchr(str, '\0', strlen(str)), "memchr scope error");
553 }
554 
555 /**
556  * @brief Test memcpy operation
557  *
558  * @see memcpy().
559  */
ZTEST(libc_common,test_memcpy)560 ZTEST(libc_common, test_memcpy)
561 {
562 	/* make sure the buffer is word aligned */
563 	uintptr_t mem_dest[4] = {0};
564 	uintptr_t mem_src[4] = {0};
565 	unsigned char *mem_dest_tmp = NULL;
566 	unsigned char *mem_src_tmp = NULL;
567 
568 	unsigned char *mem_dest_byte = (unsigned char *)mem_dest;
569 	unsigned char *mem_src_byte = (unsigned char *)mem_src;
570 
571 	/* initialize source buffer in bytes */
572 	for (int i = 0; i < sizeof(mem_src); i++) {
573 		mem_src_byte[i] = i;
574 	}
575 
576 	/* verify when dest in not word aligned */
577 	mem_dest_tmp = mem_dest_byte + 1;
578 	mem_src_tmp = mem_src_byte;
579 	zassert_equal(memcpy(mem_dest_tmp, mem_src_tmp, 10),
580 		mem_dest_tmp, "memcpy error");
581 	zassert_equal(memcmp(mem_dest_tmp, mem_src_tmp, 10),
582 		0, "memcpy failed");
583 
584 	/* restore the environment */
585 	memset(mem_dest_byte, '\0', sizeof(mem_dest));
586 	/* verify when dest and src are all in not word aligned */
587 	mem_dest_tmp = mem_dest_byte + sizeof(uintptr_t) - 1;
588 	mem_src_tmp = mem_src_byte + sizeof(uintptr_t) - 1;
589 	zassert_equal(memcpy(mem_dest_tmp, mem_src_tmp, 10),
590 		mem_dest_tmp, "memcpy error");
591 	zassert_equal(memcmp(mem_dest_tmp, mem_src_tmp, 10),
592 		0, "memcpy failed");
593 
594 	/* restore the environment */
595 	memset(mem_dest_byte, '\0', sizeof(mem_dest));
596 	/* verify when the copy count is zero, the copy will directly return */
597 	mem_dest_tmp = mem_dest_byte + sizeof(uintptr_t) - 1;
598 	mem_src_tmp = mem_src_byte + sizeof(uintptr_t) - 1;
599 	zassert_equal(memcpy(mem_dest_tmp, mem_src_tmp, 0),
600 		mem_dest_tmp, "memcpy error");
601 	zassert_not_equal(memcmp(mem_dest_tmp, mem_src_tmp, 10),
602 		0, "memcpy failed");
603 }
604 
605 /**
606  * @brief Test memmove operation
607  *
608  * @see memmove().
609  */
ZTEST(libc_common,test_memmove)610 ZTEST(libc_common, test_memmove)
611 {
612 	char move_buffer[6] = "12123";
613 	char move_new[6] = {0};
614 	static const char move_overlap[6] = "12121";
615 
616 	/* verify <src> buffer overlaps with the start of the <dest> buffer */
617 	zassert_equal(memmove(move_buffer + 2, move_buffer, 3), move_buffer + 2,
618 		     "memmove error");
619 	zassert_equal(memcmp(move_overlap, move_buffer, sizeof(move_buffer)), 0,
620 		     "memmove failed");
621 
622 	/* verify the buffer is not overlap, then forward-copy */
623 	zassert_equal(memmove(move_new, move_buffer, sizeof(move_buffer)), move_new,
624 		     "memmove error");
625 	zassert_equal(memcmp(move_new, move_buffer, sizeof(move_buffer)), 0,
626 		     "memmove failed");
627 }
628 
629 /**
630  *
631  * @brief test str operate functions
632  *
633  * @see strcat(), strcspn(), strncat().
634  *
635  */
ZTEST(libc_common,test_str_operate)636 ZTEST(libc_common, test_str_operate)
637 {
638 	char str1[10] = "aabbcc", ncat[10] = "ddee";
639 	char *str2 = "b";
640 	char *str3 = "d";
641 	int ret;
642 	char *ptr;
643 
644 	zassert_not_null(strcat(str1, str3), "strcat false");
645 	zassert_str_equal(str1, "aabbccd", "test strcat failed");
646 
647 	ret = strcspn(str1, str2);
648 	zassert_equal(ret, 2, "strcspn failed");
649 	ret = strcspn(str1, str3);
650 	zassert_equal(ret, 6, "strcspn not found str");
651 
652 	zassert_true(strncat(ncat, str1, 2), "strncat failed");
653 	zassert_not_null(strncat(str1, str3, 2), "strncat failed");
654 #if defined(__GNUC__) && __GNUC__ >= 7
655 #pragma GCC diagnostic push
656 #pragma GCC diagnostic ignored "-Wstringop-overflow"
657 #endif
658 	zassert_not_null(strncat(str1, str3, 1), "strncat failed");
659 #if defined(__GNUC__) && __GNUC__ >= 7
660 #pragma GCC diagnostic pop
661 #endif
662 	zassert_str_equal(ncat, "ddeeaa", "strncat failed");
663 
664 	zassert_is_null(strrchr(ncat, 'z'),
665 		       "strrchr not found this word. failed");
666 	ptr = strrchr(ncat, 'e');
667 	zassert_str_equal(ptr, "eaa", "strrchr failed");
668 
669 	zassert_is_null(strstr(str1, "ayz"), "strstr aabbccd with ayz failed");
670 	zassert_not_null(strstr(str1, str2), "strstr aabbccd with b succeed");
671 	zassert_not_null(strstr(str1, "bb"), "strstr aabbccd with bb succeed");
672 	zassert_not_null(strstr(str1, ""), "strstr aabbccd with \0 failed");
673 }
674 
675 /**
676  *
677  * @brief test strtol function
678  *
679  * @detail   in 32bit system:
680  *	when base is 10, [-2147483648..2147483647]
681  *		   in 64bit system:
682  *	when base is 10,
683  *    [-9,223,372,036,854,775,808..9,223,372,036,854,775,807]
684  *
685  * @see strtol().
686  *
687  */
ZTEST(libc_common,test_strtol)688 ZTEST(libc_common, test_strtol)
689 {
690 	static const char buf1[] = "+10379aegi";
691 	static const char buf2[] = "   -10379aegi";
692 	static const char buf3[] = "-010379aegi";
693 	static const char buf4[] = "0x10379aegi";
694 	static const char buf5[] = "0X10379aegi";
695 	static const char buf6[] = "01037aegi";
696 	static const char buf7[] = "1037aegi";
697 	static const char buf8[] = "++1037aegi";
698 	static const char buf9[] = "A1037aegi";
699 	static const char buf10[] = "a1037aegi";
700 	static const char str_normal[] = "-1011 This stopped it";
701 	static const char str_abnormal[] = "ABCDEFGH";
702 	char *stop = NULL;
703 	long ret;
704 
705 	/* test function strtol() */
706 	ret = strtol(buf3, NULL, 8);
707 	zassert_equal(ret, -543, "strtol base = 8 failed");
708 	ret = strtol(buf1, NULL, 10);
709 	zassert_equal(ret, 10379, "strtol base = 10 failed");
710 	ret = strtol(buf2, NULL, 10);
711 	zassert_equal(ret, -10379, "strtol base = 10 failed");
712 	ret = strtol(buf4, NULL, 16);
713 	zassert_equal(ret, 17004974, "strtol base = 16 failed");
714 	ret = strtol(buf4, NULL, 0);
715 	zassert_equal(ret, 17004974, "strtol base = 16 failed");
716 	ret = strtol(buf5, NULL, 0);
717 	zassert_equal(ret, 17004974, "strtol base = 16 failed");
718 	ret = strtol(buf6, NULL, 0);
719 	zassert_equal(ret, 543, "strtol base = 8 failed");
720 	ret = strtol(buf7, NULL, 0);
721 	zassert_equal(ret, 1037, "strtol base = 10 failed");
722 	ret = strtol(buf8, NULL, 10);
723 	zassert_not_equal(ret, 1037, "strtol base = 10 failed");
724 	ret = strtol(buf9, NULL, 10);
725 	zassert_not_equal(ret, 1037, "strtol base = 10 failed");
726 	ret = strtol(buf10, NULL, 10);
727 	zassert_not_equal(ret, 1037, "strtol base = 10 failed");
728 
729 	ret = strtol(str_normal, &stop, 10);
730 	zassert_equal(ret, -1011, "strtol base = 10 failed");
731 	zassert_str_equal(stop, " This stopped it", "strtol get stop failed");
732 
733 	ret = strtol(str_abnormal, &stop, 0);
734 	zassert_equal(ret, 0, "strtol base = 0 failed");
735 	zassert_str_equal(stop, "ABCDEFGH", "strtol get stop failed");
736 
737 #if LONG_MAX > 2147483647
738 	char border1[] = "-9223372036854775809";
739 	char border2[] = "+9223372036854775808";
740 	char border3[] = "+9223372036854775806";
741 	char border4[] = "922337203685477580000000";
742 
743 	ret = strtol(border1, NULL, 10);
744 	zassert_equal(ret, LONG_MIN, "strtol base = 10 failed");
745 	ret = strtol(border2, NULL, 10);
746 	zassert_equal(ret, LONG_MAX, "strtol base = 10 failed");
747 	ret = strtol(border3, NULL, 10);
748 	zassert_equal(ret, 9223372036854775806, "strtol base = 10 failed");
749 	ret = strtol(border4, NULL, 10);
750 	zassert_equal(ret, LONG_MAX, "strtol base = 10 failed");
751 #else
752 	char border1[] = "-2147483649";
753 	char border2[] = "+2147483648";
754 	char border3[] = "+2147483646";
755 	char border4[] = "214748364000000";
756 
757 	ret = strtol(border1, NULL, 10);
758 	zassert_equal(ret, LONG_MIN, "strtol base = 10 failed");
759 	ret = strtol(border2, NULL, 10);
760 	zassert_equal(ret, LONG_MAX, "strtol base = 10 failed");
761 	ret = strtol(border3, NULL, 10);
762 	zassert_equal(ret, 2147483646, "strtol base = 10 failed");
763 	ret = strtol(border4, NULL, 10);
764 	zassert_equal(ret, LONG_MAX, "strtol base = 10 failed");
765 #endif
766 }
767 
768 /**
769  *
770  * @brief test strtoul function
771  *
772  * @see strtoul().
773  *
774  */
ZTEST(libc_common,test_strtoul)775 ZTEST(libc_common, test_strtoul)
776 {
777 	static const char buf1[] = "+10379aegi";
778 	static const char buf2[] = "   -10379aegi";
779 	static const char buf3[] = "-010379aegi";
780 	static const char buf4[] = "0x10379aegi";
781 	static const char buf5[] = "0X10379aegi";
782 	static const char buf6[] = "01037aegi";
783 	static const char buf7[] = "1037aegi";
784 	static const char buf8[] = "++1037aegi";
785 	static const char buf9[] = "A1037aegi";
786 	static const char buf10[] = "a1037aegi";
787 	static const char str_normal[] = "-1011 This stopped it";
788 	static const char str_abnormal[] = "ABCDEFGH";
789 	char *stop = NULL;
790 	long ret;
791 
792 	/* test function strtol() */
793 	ret = strtoul(buf3, NULL, 8);
794 	zassert_equal(ret, -543, "strtol base = 8 failed");
795 	ret = strtoul(buf1, NULL, 10);
796 	zassert_equal(ret, 10379, "strtol base = 10 failed");
797 	ret = strtoul(buf2, NULL, 10);
798 	zassert_equal(ret, -10379, "strtol base = 10 failed");
799 	ret = strtoul(buf4, NULL, 16);
800 	zassert_equal(ret, 17004974, "strtol base = 16 failed");
801 	ret = strtoul(buf4, NULL, 0);
802 	zassert_equal(ret, 17004974, "strtol base = 16 failed");
803 	ret = strtoul(buf5, NULL, 0);
804 	zassert_equal(ret, 17004974, "strtol base = 16 failed");
805 	ret = strtoul(buf6, NULL, 0);
806 	zassert_equal(ret, 543, "strtol base = 8 failed");
807 	ret = strtoul(buf7, NULL, 0);
808 	zassert_equal(ret, 1037, "strtol base = 10 failed");
809 	ret = strtoul(buf8, NULL, 10);
810 	zassert_not_equal(ret, 1037, "strtol base = 10 failed");
811 	ret = strtoul(buf9, NULL, 10);
812 	zassert_not_equal(ret, 1037, "strtol base = 10 failed");
813 	ret = strtoul(buf10, NULL, 10);
814 	zassert_not_equal(ret, 1037, "strtol base = 10 failed");
815 
816 	ret = strtoul(str_normal, &stop, 10);
817 	zassert_equal(ret, -1011, "strtol base = 10 failed");
818 	zassert_str_equal(stop, " This stopped it", "strtol get stop failed");
819 
820 	ret = strtoul(str_abnormal, &stop, 0);
821 	zassert_equal(ret, 0, "strtol base = 0 failed");
822 	zassert_str_equal(stop, "ABCDEFGH", "strtol get stop failed");
823 
824 #if LONG_MAX > 2147483647
825 	char border1[] = "18446744073709551615";
826 	char border2[] = "-18446744073709551615000";
827 	char border3[] = "18446744073709551619";
828 
829 	ret = strtoul(border1, NULL, 10);
830 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
831 	ret = strtoul(border2, NULL, 10);
832 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
833 	ret = strtoul(border3, NULL, 10);
834 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
835 
836 #else
837 	char border1[] = "+4294967295";
838 	char border2[] = "-4294967295000";
839 	char border3[] = "+4294967299";
840 
841 	ret = strtoul(border1, NULL, 10);
842 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
843 	ret = strtoul(border2, NULL, 10);
844 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
845 	ret = strtoul(border3, NULL, 10);
846 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
847 #endif
848 }
849 
850 /**
851  *
852  * @brief test strtoll function
853  *
854  * @see strtoll().
855  *
856  */
test_strtoll(void)857 void test_strtoll(void)
858 {
859 	static const char buf1[] = "+10379aegi";
860 	static const char buf2[] = "   -10379aegi";
861 	static const char buf3[] = "-010379aegi";
862 	static const char buf4[] = "0x10379aegi";
863 	static const char buf5[] = "0X10379aegi";
864 	static const char buf6[] = "01037aegi";
865 	static const char buf7[] = "1037aegi";
866 	static const char buf8[] = "++1037aegi";
867 	static const char buf9[] = "A1037aegi";
868 	static const char buf10[] = "a1037aegi";
869 	static const char str_normal[] = "-1011 This stopped it";
870 	static const char str_abnormal[] = "ABCDEFGH";
871 	char *stop = NULL;
872 	long long ret;
873 
874 	/* test function strtoll() */
875 	ret = strtoll(buf3, NULL, 8);
876 	zassert_equal(ret, -543, "strtoll base = 8 failed");
877 	ret = strtoll(buf1, NULL, 10);
878 	zassert_equal(ret, 10379, "strtoll base = 10 failed");
879 	ret = strtoll(buf2, NULL, 10);
880 	zassert_equal(ret, -10379, "strtoll base = 10 failed");
881 	ret = strtoll(buf4, NULL, 16);
882 	zassert_equal(ret, 17004974, "strtoll base = 16 failed");
883 	ret = strtoll(buf4, NULL, 0);
884 	zassert_equal(ret, 17004974, "strtoll base = 16 failed");
885 	ret = strtoll(buf5, NULL, 0);
886 	zassert_equal(ret, 17004974, "strtoll base = 16 failed");
887 	ret = strtoll(buf6, NULL, 0);
888 	zassert_equal(ret, 543, "strtoll base = 8 failed");
889 	ret = strtoll(buf7, NULL, 0);
890 	zassert_equal(ret, 1037, "strtoll base = 10 failed");
891 	ret = strtoll(buf8, NULL, 10);
892 	zassert_not_equal(ret, 1037, "strtoll base = 10 failed");
893 	ret = strtoll(buf9, NULL, 10);
894 	zassert_not_equal(ret, 1037, "strtoll base = 10 failed");
895 	ret = strtoll(buf10, NULL, 10);
896 	zassert_not_equal(ret, 1037, "strtoll base = 10 failed");
897 
898 	ret = strtoll(str_normal, &stop, 10);
899 	zassert_equal(ret, -1011, "strtoll base = 10 failed");
900 	zassert_str_equal(stop, " This stopped it", "strtoll get stop failed");
901 
902 	ret = strtoll(str_abnormal, &stop, 0);
903 	zassert_equal(ret, 0, "strtoll base = 0 failed");
904 	zassert_str_equal(stop, "ABCDEFGH", "strtoll get stop failed");
905 
906 	char border1[] = "-9223372036854775808";
907 	char border2[] = "+9223372036854775807";
908 	char border3[] = "+9223372036854775806";
909 	char border4[] = "922337203685477580000000";
910 	char border5[] = "0x0000000000000000000000000000000000001";
911 	char border6[] = "10000000000000000000000000000000000001";
912 	char border7[] = "-10000000000000000000000000000000000001";
913 
914 	ret = strtoll(border1, NULL, 10);
915 	zassert_equal(ret, LLONG_MIN, "strtoll base = 10 failed");
916 	ret = strtoll(border2, NULL, 10);
917 	zassert_equal(ret, LLONG_MAX, "strtoll base = 10 failed");
918 	ret = strtoll(border3, NULL, 10);
919 	zassert_equal(ret, 9223372036854775806, "strtoll base = 10 failed");
920 	ret = strtoll(border4, NULL, 10);
921 	zassert_equal(ret, LLONG_MAX, "strtoll base = 10 failed");
922 	ret = strtoull(border5, NULL, 16);
923 	zassert_equal(ret, 1, "strtoull base = 16 failed, %s != 0x%x", border5, ret);
924 	ret = strtoull(border6, NULL, 10);
925 	zassert_equal(errno, ERANGE, "strtoull base = 10 failed, %s != %lld", border6, ret);
926 	ret = strtoull(border7, NULL, 10);
927 	zassert_equal(errno, ERANGE, "strtoull base = 10 failed, %s != %lld", border7, ret);
928 }
929 
930 /**
931  *
932  * @brief test strtoull function
933  *
934  * @see strtoull().
935  *
936  */
test_strtoull(void)937 void test_strtoull(void)
938 {
939 	static const char buf1[] = "+10379aegi";
940 	static const char buf2[] = "   -10379aegi";
941 	static const char buf3[] = "-010379aegi";
942 	static const char buf4[] = "0x10379aegi";
943 	static const char buf5[] = "0X10379aegi";
944 	static const char buf6[] = "01037aegi";
945 	static const char buf7[] = "1037aegi";
946 	static const char buf8[] = "++1037aegi";
947 	static const char buf9[] = "A1037aegi";
948 	static const char buf10[] = "a1037aegi";
949 	static const char str_normal[] = "-1011 This stopped it";
950 	static const char str_abnormal[] = "ABCDEFGH";
951 	char *stop = NULL;
952 	unsigned long long ret;
953 
954 	/* test function strtoull() */
955 	ret = strtoull(buf3, NULL, 8);
956 	zassert_equal(ret, -543, "strtoull base = 8 failed");
957 	ret = strtoull(buf1, NULL, 10);
958 	zassert_equal(ret, 10379, "strtoull base = 10 failed");
959 	ret = strtoull(buf2, NULL, 10);
960 	zassert_equal(ret, -10379, "strtoull base = 10 failed");
961 	ret = strtoull(buf4, NULL, 16);
962 	zassert_equal(ret, 17004974, "strtoull base = 16 failed");
963 	ret = strtoull(buf4, NULL, 0);
964 	zassert_equal(ret, 17004974, "strtoull base = 16 failed");
965 	ret = strtoull(buf5, NULL, 0);
966 	zassert_equal(ret, 17004974, "strtoull base = 16 failed");
967 	ret = strtoull(buf6, NULL, 0);
968 	zassert_equal(ret, 543, "strtoull base = 8 failed");
969 	ret = strtoull(buf7, NULL, 0);
970 	zassert_equal(ret, 1037, "strtoull base = 10 failed");
971 	ret = strtoull(buf8, NULL, 10);
972 	zassert_not_equal(ret, 1037, "strtoull base = 10 failed");
973 	ret = strtoull(buf9, NULL, 10);
974 	zassert_not_equal(ret, 1037, "strtoull base = 10 failed");
975 	ret = strtoull(buf10, NULL, 10);
976 	zassert_not_equal(ret, 1037, "strtoull base = 10 failed");
977 
978 	ret = strtoull(str_normal, &stop, 10);
979 	zassert_equal(ret, -1011, "strtoull base = 10 failed");
980 	zassert_str_equal(stop, " This stopped it",
981 			  "strtoull get stop failed");
982 
983 	ret = strtoull(str_abnormal, &stop, 0);
984 	zassert_equal(ret, 0, "strtoull base = 0 failed");
985 	zassert_str_equal(stop, "ABCDEFGH", "strtoull get stop failed");
986 
987 	char border1[] = "+18446744073709551615";
988 	char border2[] = "-18446744073709551615000";
989 	char border3[] = "+18446744073709551619";
990 	char border4[] = "0x0000000000000000000000000000000000001";
991 	char border5[] = "10000000000000000000000000000000000001";
992 	char border6[] = "-10000000000000000000000000000000000001";
993 
994 	ret = strtoull(border1, NULL, 10);
995 	zassert_equal(ret, ULLONG_MAX, "strtoull base = 10 failed");
996 	ret = strtoull(border2, NULL, 10);
997 	zassert_equal(ret, ULLONG_MAX, "strtoull base = 10 failed");
998 	ret = strtoull(border3, NULL, 10);
999 	zassert_equal(ret, ULLONG_MAX, "strtoull base = 10 failed");
1000 	ret = strtoull(border4, NULL, 16);
1001 	zassert_equal(ret, 1, "strtoull base = 16 failed, %s != 0x%x", border4, ret);
1002 	ret = strtoull(border5, NULL, 10);
1003 	zassert_equal(errno, ERANGE, "strtoull base = 10 failed, %s != %lld", border5, ret);
1004 	ret = strtoull(border6, NULL, 10);
1005 	zassert_equal(errno, ERANGE, "strtoull base = 10 failed, %s != %lld", border6, ret);
1006 }
1007 
1008 /**
1009  *
1010  * @brief test convert function
1011  *
1012  */
ZTEST(libc_common,test_tolower_toupper)1013 ZTEST(libc_common, test_tolower_toupper)
1014 {
1015 	static const char test[] = "Az09Za{#!";
1016 	static const char toup[] = "AZ09ZA{#!";
1017 	static const char tolw[] = "az09za{#!";
1018 	char up[11];
1019 	char lw[11];
1020 	int i = 0;
1021 
1022 	for (; i < strlen(test); i++) {
1023 		up[i] = toupper(test[i]);
1024 		lw[i] = tolower(test[i]);
1025 	}
1026 	lw[i] = up[i] = '\0';
1027 
1028 	zassert_str_equal(up, toup, "toupper error");
1029 	zassert_str_equal(lw, tolw, "tolower error");
1030 }
1031 
test_strtok_r_do(char * str,char * sep,int tlen,const char * const * toks,bool expect)1032 void test_strtok_r_do(char *str, char *sep, int tlen,
1033 		      const char * const *toks, bool expect)
1034 {
1035 	int len = 0;
1036 	char *state, *tok, buf[64+1] = {0};
1037 
1038 	strncpy(buf, str, 64);
1039 
1040 	tok = strtok_r(buf, sep, &state);
1041 	while (tok && len < tlen) {
1042 		if (strcmp(tok, toks[len]) != 0) {
1043 			break;
1044 		}
1045 		tok = strtok_r(NULL, sep, &state);
1046 		len++;
1047 	}
1048 	if (expect) {
1049 		zassert_equal(len, tlen,
1050 			      "strtok_r error '%s' / '%s'", str, sep);
1051 	} else {
1052 		zassert_not_equal(len, tlen,
1053 				  "strtok_r error '%s' / '%s'", str, sep);
1054 	}
1055 }
1056 
ZTEST(libc_common,test_strtok_r)1057 ZTEST(libc_common, test_strtok_r)
1058 {
1059 	static const char * const tc01[] = { "1", "2", "3", "4", "5" };
1060 
1061 	test_strtok_r_do("1,2,3,4,5",           ",",  5, tc01, true);
1062 	test_strtok_r_do(",, 1 ,2  ,3   4,5  ", ", ", 5, tc01, true);
1063 	test_strtok_r_do("1,,,2 3,,,4 5",       ", ", 5, tc01, true);
1064 	test_strtok_r_do("1,2 3,,,4 5  ",       ", ", 5, tc01, true);
1065 	test_strtok_r_do("0,1,,,2 3,,,4 5",     ", ", 5, tc01, false);
1066 	test_strtok_r_do("1,,,2 3,,,4 5",       ",",  5, tc01, false);
1067 	test_strtok_r_do("A,,,2,3,,,4 5",       ",",  5, tc01, false);
1068 	test_strtok_r_do("1,,,2,3,,,",          ",",  5, tc01, false);
1069 	test_strtok_r_do("1|2|3,4|5",           "| ", 5, tc01, false);
1070 }
1071 
1072 /**
1073  *
1074  * @brief Test time function
1075  *
1076  * @see gmtime(),gmtime_r().
1077  */
ZTEST(libc_common,test_time)1078 ZTEST(libc_common, test_time)
1079 {
1080 	time_t tests1 = 0;
1081 	time_t tests2 = -5;
1082 	time_t tests3 = (time_t) -214748364800;
1083 	time_t tests4 = 951868800;
1084 
1085 	struct tm tp;
1086 
1087 	zassert_not_null(gmtime(&tests1), "gmtime failed");
1088 	zassert_not_null(gmtime(&tests2), "gmtime failed");
1089 
1090 	tp.tm_wday = -5;
1091 	zassert_not_null(gmtime_r(&tests3, &tp), "gmtime_r failed");
1092 	zassert_not_null(gmtime_r(&tests4, &tp), "gmtime_r failed");
1093 }
1094 
1095 /**
1096  *
1097  * @brief Test rand function
1098  *
1099  */
ZTEST(libc_common,test_rand)1100 ZTEST(libc_common, test_rand)
1101 {
1102 #ifdef CONFIG_MINIMAL_LIBC
1103 	int a;
1104 
1105 	a = rand();
1106 	/* The default seed is 1 */
1107 	zassert_equal(a, 1103527590, "rand failed");
1108 #else
1109 	ztest_test_skip();
1110 #endif
1111 }
1112 
1113 /**
1114  *
1115  * @brief Test srand function
1116  *
1117  */
ZTEST(libc_common,test_srand)1118 ZTEST(libc_common, test_srand)
1119 {
1120 #ifdef CONFIG_MINIMAL_LIBC
1121 	int a;
1122 
1123 	srand(0);
1124 	a = rand();
1125 	zassert_equal(a, 12345, "srand with seed 0 failed");
1126 
1127 	srand(1);
1128 	a = rand();
1129 	zassert_equal(a, 1103527590, "srand with seed 1 failed");
1130 
1131 	srand(10);
1132 	a = rand();
1133 	zassert_equal(a, 297746555, "srand with seed 10 failed");
1134 
1135 	srand(UINT_MAX - 1);
1136 	a = rand();
1137 	zassert_equal(a, 2087949151, "srand with seed UINT_MAX - 1 failed");
1138 
1139 	srand(UINT_MAX);
1140 	a = rand();
1141 	zassert_equal(a, 1043980748, "srand with seed UINT_MAX failed");
1142 #else
1143 	ztest_test_skip();
1144 #endif
1145 }
1146 
1147 /**
1148  *
1149  * @brief Test rand function for reproducibility
1150  *
1151  */
ZTEST(libc_common,test_rand_reproducibility)1152 ZTEST(libc_common, test_rand_reproducibility)
1153 {
1154 #ifdef CONFIG_MINIMAL_LIBC
1155 	int a;
1156 	int b;
1157 	int c;
1158 
1159 	srand(0);
1160 	a = rand();
1161 	zassert_equal(a, 12345, "srand with seed 0 failed");
1162 	srand(0);
1163 	b = rand();
1164 	zassert_equal(b, 12345, "srand with seed 0 failed (2nd)");
1165 	srand(0);
1166 	c = rand();
1167 	zassert_equal(c, 12345, "srand with seed 0 failed (3rd)");
1168 
1169 	srand(1);
1170 	a = rand();
1171 	zassert_equal(a, 1103527590, "srand with seed 1 failed");
1172 	srand(1);
1173 	b = rand();
1174 	zassert_equal(b, 1103527590, "srand with seed 1 failed (2nd)");
1175 	srand(1);
1176 	c = rand();
1177 	zassert_equal(c, 1103527590, "srand with seed 1 failed (3rd)");
1178 
1179 	srand(10);
1180 	a = rand();
1181 	zassert_equal(a, 297746555, "srand with seed 10 failed");
1182 	srand(10);
1183 	b = rand();
1184 	zassert_equal(b, 297746555, "srand with seed 10 failed (2nd)");
1185 	srand(10);
1186 	c = rand();
1187 	zassert_equal(c, 297746555, "srand with seed 10 failed (3rd)");
1188 
1189 	srand(UINT_MAX - 1);
1190 	a = rand();
1191 	zassert_equal(a, 2087949151, "srand with seed UINT_MAX - 1 failed");
1192 	srand(UINT_MAX - 1);
1193 	b = rand();
1194 	zassert_equal(b, 2087949151, "srand with seed UINT_MAX - 1 failed (2nd)");
1195 	srand(UINT_MAX - 1);
1196 	c = rand();
1197 	zassert_equal(c, 2087949151, "srand with seed UINT_MAX - 1 failed (3rd)");
1198 
1199 	srand(UINT_MAX);
1200 	a = rand();
1201 	zassert_equal(a, 1043980748, "srand with seed UINT_MAX failed");
1202 	srand(UINT_MAX);
1203 	b = rand();
1204 	zassert_equal(b, 1043980748, "srand with seed UINT_MAX failed (2nd)");
1205 	srand(UINT_MAX);
1206 	c = rand();
1207 	zassert_equal(c, 1043980748, "srand with seed UINT_MAX failed (3rd)");
1208 #else
1209 	ztest_test_skip();
1210 #endif
1211 }
1212 
1213 /**
1214  *
1215  * @brief test abort functions
1216  *
1217  * @see abort().
1218  */
ZTEST(libc_common,test_abort)1219 ZTEST(libc_common, test_abort)
1220 {
1221 #ifdef CONFIG_EXTERNAL_LIBC
1222 	ztest_test_skip();
1223 #else
1224 	int a = 0;
1225 
1226 	ztest_set_fault_valid(true);
1227 	abort();
1228 	zassert_equal(a, 0, "abort failed");
1229 #endif
1230 }
1231 
1232 /**
1233  *
1234  * @brief test exit functions
1235  *
1236  */
1237 #ifndef CONFIG_EXTERNAL_LIBC
exit_program(void * p1,void * p2,void * p3)1238 static void exit_program(void *p1, void *p2, void *p3)
1239 {
1240 	exit(1);
1241 }
1242 
1243 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
1244 static struct k_thread tdata;
1245 
1246 #endif
1247 
ZTEST(libc_common,test_exit)1248 ZTEST(libc_common, test_exit)
1249 {
1250 #ifdef CONFIG_EXTERNAL_LIBC
1251 	ztest_test_skip();
1252 #else
1253 	int a = 0;
1254 
1255 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, exit_program,
1256 					NULL, NULL, NULL, K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
1257 	k_sleep(K_MSEC(10));
1258 	k_thread_abort(tid);
1259 	zassert_equal(a, 0, "exit failed");
1260 #endif
1261 }
1262