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_true((strcmp(buffer, "eeeee") == 0), "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_true((strcmp(buffer, "10 chars!\0") == 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_equal(strcmp(buf, exp_alnum), 0, "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_equal(strcmp(buf, exp_alpha), 0, "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_equal(strcmp(buf, exp_digit), 0, "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_equal(strcmp(buf, exp_graph), 0, "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_equal(strcmp(buf, exp_print), 0, "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_equal(strcmp(buf, exp_upper), 0, "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_equal(strcmp(buf, exp_space), 0, "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_equal(strcmp(buf, exp_xdigit), 0, "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_equal(strcmp(str1, "aabbccd"), 0, "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_equal(strcmp(ncat, "ddeeaa"), 0, "strncat failed");
663 
664 	zassert_is_null(strrchr(ncat, 'z'),
665 		       "strrchr not found this word. failed");
666 	ptr = strrchr(ncat, 'e');
667 	zassert_equal(strcmp(ptr, "eaa"), 0, "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_true((strcmp(stop, " This stopped it") == 0),
732 		"strtol get stop failed");
733 
734 	ret = strtol(str_abnormal, &stop, 0);
735 	zassert_equal(ret, 0, "strtol base = 0 failed");
736 	zassert_true((strcmp(stop, "ABCDEFGH") == 0),
737 		"strtol get stop failed");
738 
739 #if LONG_MAX > 2147483647
740 	char border1[] = "-9223372036854775809";
741 	char border2[] = "+9223372036854775808";
742 	char border3[] = "+9223372036854775806";
743 	char border4[] = "922337203685477580000000";
744 
745 	ret = strtol(border1, NULL, 10);
746 	zassert_equal(ret, LONG_MIN, "strtol base = 10 failed");
747 	ret = strtol(border2, NULL, 10);
748 	zassert_equal(ret, LONG_MAX, "strtol base = 10 failed");
749 	ret = strtol(border3, NULL, 10);
750 	zassert_equal(ret, 9223372036854775806, "strtol base = 10 failed");
751 	ret = strtol(border4, NULL, 10);
752 	zassert_equal(ret, LONG_MAX, "strtol base = 10 failed");
753 #else
754 	char border1[] = "-2147483649";
755 	char border2[] = "+2147483648";
756 	char border3[] = "+2147483646";
757 	char border4[] = "214748364000000";
758 
759 	ret = strtol(border1, NULL, 10);
760 	zassert_equal(ret, LONG_MIN, "strtol base = 10 failed");
761 	ret = strtol(border2, NULL, 10);
762 	zassert_equal(ret, LONG_MAX, "strtol base = 10 failed");
763 	ret = strtol(border3, NULL, 10);
764 	zassert_equal(ret, 2147483646, "strtol base = 10 failed");
765 	ret = strtol(border4, NULL, 10);
766 	zassert_equal(ret, LONG_MAX, "strtol base = 10 failed");
767 #endif
768 }
769 
770 /**
771  *
772  * @brief test strtoul function
773  *
774  * @see strtoul().
775  *
776  */
ZTEST(libc_common,test_strtoul)777 ZTEST(libc_common, test_strtoul)
778 {
779 	static const char buf1[] = "+10379aegi";
780 	static const char buf2[] = "   -10379aegi";
781 	static const char buf3[] = "-010379aegi";
782 	static const char buf4[] = "0x10379aegi";
783 	static const char buf5[] = "0X10379aegi";
784 	static const char buf6[] = "01037aegi";
785 	static const char buf7[] = "1037aegi";
786 	static const char buf8[] = "++1037aegi";
787 	static const char buf9[] = "A1037aegi";
788 	static const char buf10[] = "a1037aegi";
789 	static const char str_normal[] = "-1011 This stopped it";
790 	static const char str_abnormal[] = "ABCDEFGH";
791 	char *stop = NULL;
792 	long ret;
793 
794 	/* test function strtol() */
795 	ret = strtoul(buf3, NULL, 8);
796 	zassert_equal(ret, -543, "strtol base = 8 failed");
797 	ret = strtoul(buf1, NULL, 10);
798 	zassert_equal(ret, 10379, "strtol base = 10 failed");
799 	ret = strtoul(buf2, NULL, 10);
800 	zassert_equal(ret, -10379, "strtol base = 10 failed");
801 	ret = strtoul(buf4, NULL, 16);
802 	zassert_equal(ret, 17004974, "strtol base = 16 failed");
803 	ret = strtoul(buf4, NULL, 0);
804 	zassert_equal(ret, 17004974, "strtol base = 16 failed");
805 	ret = strtoul(buf5, NULL, 0);
806 	zassert_equal(ret, 17004974, "strtol base = 16 failed");
807 	ret = strtoul(buf6, NULL, 0);
808 	zassert_equal(ret, 543, "strtol base = 8 failed");
809 	ret = strtoul(buf7, NULL, 0);
810 	zassert_equal(ret, 1037, "strtol base = 10 failed");
811 	ret = strtoul(buf8, NULL, 10);
812 	zassert_not_equal(ret, 1037, "strtol base = 10 failed");
813 	ret = strtoul(buf9, NULL, 10);
814 	zassert_not_equal(ret, 1037, "strtol base = 10 failed");
815 	ret = strtoul(buf10, NULL, 10);
816 	zassert_not_equal(ret, 1037, "strtol base = 10 failed");
817 
818 	ret = strtoul(str_normal, &stop, 10);
819 	zassert_equal(ret, -1011, "strtol base = 10 failed");
820 	zassert_true((strcmp(stop, " This stopped it") == 0),
821 		"strtol get stop failed");
822 
823 	ret = strtoul(str_abnormal, &stop, 0);
824 	zassert_equal(ret, 0, "strtol base = 0 failed");
825 	zassert_true((strcmp(stop, "ABCDEFGH") == 0),
826 		"strtol get stop failed");
827 
828 #if LONG_MAX > 2147483647
829 	char border1[] = "18446744073709551615";
830 	char border2[] = "-18446744073709551615000";
831 	char border3[] = "18446744073709551619";
832 
833 	ret = strtoul(border1, NULL, 10);
834 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
835 	ret = strtoul(border2, NULL, 10);
836 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
837 	ret = strtoul(border3, NULL, 10);
838 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
839 
840 #else
841 	char border1[] = "+4294967295";
842 	char border2[] = "-4294967295000";
843 	char border3[] = "+4294967299";
844 
845 	ret = strtoul(border1, NULL, 10);
846 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
847 	ret = strtoul(border2, NULL, 10);
848 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
849 	ret = strtoul(border3, NULL, 10);
850 	zassert_equal(ret, ULONG_MAX, "strtol base = 10 failed");
851 #endif
852 }
853 
854 /**
855  *
856  * @brief test strtoll function
857  *
858  * @see strtoll().
859  *
860  */
test_strtoll(void)861 void test_strtoll(void)
862 {
863 	static const char buf1[] = "+10379aegi";
864 	static const char buf2[] = "   -10379aegi";
865 	static const char buf3[] = "-010379aegi";
866 	static const char buf4[] = "0x10379aegi";
867 	static const char buf5[] = "0X10379aegi";
868 	static const char buf6[] = "01037aegi";
869 	static const char buf7[] = "1037aegi";
870 	static const char buf8[] = "++1037aegi";
871 	static const char buf9[] = "A1037aegi";
872 	static const char buf10[] = "a1037aegi";
873 	static const char str_normal[] = "-1011 This stopped it";
874 	static const char str_abnormal[] = "ABCDEFGH";
875 	char *stop = NULL;
876 	long long ret;
877 
878 	/* test function strtoll() */
879 	ret = strtoll(buf3, NULL, 8);
880 	zassert_equal(ret, -543, "strtoll base = 8 failed");
881 	ret = strtoll(buf1, NULL, 10);
882 	zassert_equal(ret, 10379, "strtoll base = 10 failed");
883 	ret = strtoll(buf2, NULL, 10);
884 	zassert_equal(ret, -10379, "strtoll base = 10 failed");
885 	ret = strtoll(buf4, NULL, 16);
886 	zassert_equal(ret, 17004974, "strtoll base = 16 failed");
887 	ret = strtoll(buf4, NULL, 0);
888 	zassert_equal(ret, 17004974, "strtoll base = 16 failed");
889 	ret = strtoll(buf5, NULL, 0);
890 	zassert_equal(ret, 17004974, "strtoll base = 16 failed");
891 	ret = strtoll(buf6, NULL, 0);
892 	zassert_equal(ret, 543, "strtoll base = 8 failed");
893 	ret = strtoll(buf7, NULL, 0);
894 	zassert_equal(ret, 1037, "strtoll base = 10 failed");
895 	ret = strtoll(buf8, NULL, 10);
896 	zassert_not_equal(ret, 1037, "strtoll base = 10 failed");
897 	ret = strtoll(buf9, NULL, 10);
898 	zassert_not_equal(ret, 1037, "strtoll base = 10 failed");
899 	ret = strtoll(buf10, NULL, 10);
900 	zassert_not_equal(ret, 1037, "strtoll base = 10 failed");
901 
902 	ret = strtoll(str_normal, &stop, 10);
903 	zassert_equal(ret, -1011, "strtoll base = 10 failed");
904 	zassert_true((strcmp(stop, " This stopped it") == 0), "strtoll get stop failed");
905 
906 	ret = strtoll(str_abnormal, &stop, 0);
907 	zassert_equal(ret, 0, "strtoll base = 0 failed");
908 	zassert_true((strcmp(stop, "ABCDEFGH") == 0), "strtoll get stop failed");
909 
910 	char border1[] = "-9223372036854775808";
911 	char border2[] = "+9223372036854775807";
912 	char border3[] = "+9223372036854775806";
913 	char border4[] = "922337203685477580000000";
914 	char border5[] = "0x0000000000000000000000000000000000001";
915 	char border6[] = "10000000000000000000000000000000000001";
916 	char border7[] = "-10000000000000000000000000000000000001";
917 
918 	ret = strtoll(border1, NULL, 10);
919 	zassert_equal(ret, LLONG_MIN, "strtoll base = 10 failed");
920 	ret = strtoll(border2, NULL, 10);
921 	zassert_equal(ret, LLONG_MAX, "strtoll base = 10 failed");
922 	ret = strtoll(border3, NULL, 10);
923 	zassert_equal(ret, 9223372036854775806, "strtoll base = 10 failed");
924 	ret = strtoll(border4, NULL, 10);
925 	zassert_equal(ret, LLONG_MAX, "strtoll base = 10 failed");
926 	ret = strtoull(border5, NULL, 16);
927 	zassert_equal(ret, 1, "strtoull base = 16 failed, %s != 0x%x", border5, ret);
928 	ret = strtoull(border6, NULL, 10);
929 	zassert_equal(errno, ERANGE, "strtoull base = 10 failed, %s != %lld", border6, ret);
930 	ret = strtoull(border7, NULL, 10);
931 	zassert_equal(errno, ERANGE, "strtoull base = 10 failed, %s != %lld", border7, ret);
932 }
933 
934 /**
935  *
936  * @brief test strtoull function
937  *
938  * @see strtoull().
939  *
940  */
test_strtoull(void)941 void test_strtoull(void)
942 {
943 	static const char buf1[] = "+10379aegi";
944 	static const char buf2[] = "   -10379aegi";
945 	static const char buf3[] = "-010379aegi";
946 	static const char buf4[] = "0x10379aegi";
947 	static const char buf5[] = "0X10379aegi";
948 	static const char buf6[] = "01037aegi";
949 	static const char buf7[] = "1037aegi";
950 	static const char buf8[] = "++1037aegi";
951 	static const char buf9[] = "A1037aegi";
952 	static const char buf10[] = "a1037aegi";
953 	static const char str_normal[] = "-1011 This stopped it";
954 	static const char str_abnormal[] = "ABCDEFGH";
955 	char *stop = NULL;
956 	unsigned long long ret;
957 
958 	/* test function strtoull() */
959 	ret = strtoull(buf3, NULL, 8);
960 	zassert_equal(ret, -543, "strtoull base = 8 failed");
961 	ret = strtoull(buf1, NULL, 10);
962 	zassert_equal(ret, 10379, "strtoull base = 10 failed");
963 	ret = strtoull(buf2, NULL, 10);
964 	zassert_equal(ret, -10379, "strtoull base = 10 failed");
965 	ret = strtoull(buf4, NULL, 16);
966 	zassert_equal(ret, 17004974, "strtoull base = 16 failed");
967 	ret = strtoull(buf4, NULL, 0);
968 	zassert_equal(ret, 17004974, "strtoull base = 16 failed");
969 	ret = strtoull(buf5, NULL, 0);
970 	zassert_equal(ret, 17004974, "strtoull base = 16 failed");
971 	ret = strtoull(buf6, NULL, 0);
972 	zassert_equal(ret, 543, "strtoull base = 8 failed");
973 	ret = strtoull(buf7, NULL, 0);
974 	zassert_equal(ret, 1037, "strtoull base = 10 failed");
975 	ret = strtoull(buf8, NULL, 10);
976 	zassert_not_equal(ret, 1037, "strtoull base = 10 failed");
977 	ret = strtoull(buf9, NULL, 10);
978 	zassert_not_equal(ret, 1037, "strtoull base = 10 failed");
979 	ret = strtoull(buf10, NULL, 10);
980 	zassert_not_equal(ret, 1037, "strtoull base = 10 failed");
981 
982 	ret = strtoull(str_normal, &stop, 10);
983 	zassert_equal(ret, -1011, "strtoull base = 10 failed");
984 	zassert_true((strcmp(stop, " This stopped it") == 0), "strtoull get stop failed");
985 
986 	ret = strtoull(str_abnormal, &stop, 0);
987 	zassert_equal(ret, 0, "strtoull base = 0 failed");
988 	zassert_true((strcmp(stop, "ABCDEFGH") == 0), "strtoull get stop failed");
989 
990 	char border1[] = "+18446744073709551615";
991 	char border2[] = "-18446744073709551615000";
992 	char border3[] = "+18446744073709551619";
993 	char border4[] = "0x0000000000000000000000000000000000001";
994 	char border5[] = "10000000000000000000000000000000000001";
995 	char border6[] = "-10000000000000000000000000000000000001";
996 
997 	ret = strtoull(border1, NULL, 10);
998 	zassert_equal(ret, ULLONG_MAX, "strtoull base = 10 failed");
999 	ret = strtoull(border2, NULL, 10);
1000 	zassert_equal(ret, ULLONG_MAX, "strtoull base = 10 failed");
1001 	ret = strtoull(border3, NULL, 10);
1002 	zassert_equal(ret, ULLONG_MAX, "strtoull base = 10 failed");
1003 	ret = strtoull(border4, NULL, 16);
1004 	zassert_equal(ret, 1, "strtoull base = 16 failed, %s != 0x%x", border4, ret);
1005 	ret = strtoull(border5, NULL, 10);
1006 	zassert_equal(errno, ERANGE, "strtoull base = 10 failed, %s != %lld", border5, ret);
1007 	ret = strtoull(border6, NULL, 10);
1008 	zassert_equal(errno, ERANGE, "strtoull base = 10 failed, %s != %lld", border6, ret);
1009 }
1010 
1011 /**
1012  *
1013  * @brief test convert function
1014  *
1015  */
ZTEST(libc_common,test_tolower_toupper)1016 ZTEST(libc_common, test_tolower_toupper)
1017 {
1018 	static const char test[] = "Az09Za{#!";
1019 	static const char toup[] = "AZ09ZA{#!";
1020 	static const char tolw[] = "az09za{#!";
1021 	char up[11];
1022 	char lw[11];
1023 	int i = 0;
1024 
1025 	for (; i < strlen(test); i++) {
1026 		up[i] = toupper(test[i]);
1027 		lw[i] = tolower(test[i]);
1028 	}
1029 	lw[i] = up[i] = '\0';
1030 
1031 	zassert_equal(strcmp(up, toup), 0, "toupper error");
1032 	zassert_equal(strcmp(lw, tolw), 0, "tolower error");
1033 }
1034 
test_strtok_r_do(char * str,char * sep,int tlen,const char * const * toks,bool expect)1035 void test_strtok_r_do(char *str, char *sep, int tlen,
1036 		      const char * const *toks, bool expect)
1037 {
1038 	int len = 0;
1039 	char *state, *tok, buf[64+1] = {0};
1040 
1041 	strncpy(buf, str, 64);
1042 
1043 	tok = strtok_r(buf, sep, &state);
1044 	while (tok && len < tlen) {
1045 		if (strcmp(tok, toks[len]) != 0) {
1046 			break;
1047 		}
1048 		tok = strtok_r(NULL, sep, &state);
1049 		len++;
1050 	}
1051 	if (expect) {
1052 		zassert_equal(len, tlen,
1053 			      "strtok_r error '%s' / '%s'", str, sep);
1054 	} else {
1055 		zassert_not_equal(len, tlen,
1056 				  "strtok_r error '%s' / '%s'", str, sep);
1057 	}
1058 }
1059 
ZTEST(libc_common,test_strtok_r)1060 ZTEST(libc_common, test_strtok_r)
1061 {
1062 	static const char * const tc01[] = { "1", "2", "3", "4", "5" };
1063 
1064 	test_strtok_r_do("1,2,3,4,5",           ",",  5, tc01, true);
1065 	test_strtok_r_do(",, 1 ,2  ,3   4,5  ", ", ", 5, tc01, true);
1066 	test_strtok_r_do("1,,,2 3,,,4 5",       ", ", 5, tc01, true);
1067 	test_strtok_r_do("1,2 3,,,4 5  ",       ", ", 5, tc01, true);
1068 	test_strtok_r_do("0,1,,,2 3,,,4 5",     ", ", 5, tc01, false);
1069 	test_strtok_r_do("1,,,2 3,,,4 5",       ",",  5, tc01, false);
1070 	test_strtok_r_do("A,,,2,3,,,4 5",       ",",  5, tc01, false);
1071 	test_strtok_r_do("1,,,2,3,,,",          ",",  5, tc01, false);
1072 	test_strtok_r_do("1|2|3,4|5",           "| ", 5, tc01, false);
1073 }
1074 
1075 /**
1076  *
1077  * @brief Test time function
1078  *
1079  * @see gmtime(),gmtime_r().
1080  */
ZTEST(libc_common,test_time)1081 ZTEST(libc_common, test_time)
1082 {
1083 	time_t tests1 = 0;
1084 	time_t tests2 = -5;
1085 	time_t tests3 = (time_t) -214748364800;
1086 	time_t tests4 = 951868800;
1087 
1088 	struct tm tp;
1089 
1090 	zassert_not_null(gmtime(&tests1), "gmtime failed");
1091 	zassert_not_null(gmtime(&tests2), "gmtime failed");
1092 
1093 	tp.tm_wday = -5;
1094 	zassert_not_null(gmtime_r(&tests3, &tp), "gmtime_r failed");
1095 	zassert_not_null(gmtime_r(&tests4, &tp), "gmtime_r failed");
1096 }
1097 
1098 /**
1099  *
1100  * @brief Test rand function
1101  *
1102  */
ZTEST(libc_common,test_rand)1103 ZTEST(libc_common, test_rand)
1104 {
1105 #ifdef CONFIG_MINIMAL_LIBC
1106 	int a;
1107 
1108 	a = rand();
1109 	/* The default seed is 1 */
1110 	zassert_equal(a, 1103527590, "rand failed");
1111 #else
1112 	ztest_test_skip();
1113 #endif
1114 }
1115 
1116 /**
1117  *
1118  * @brief Test srand function
1119  *
1120  */
ZTEST(libc_common,test_srand)1121 ZTEST(libc_common, test_srand)
1122 {
1123 #ifdef CONFIG_MINIMAL_LIBC
1124 	int a;
1125 
1126 	srand(0);
1127 	a = rand();
1128 	zassert_equal(a, 12345, "srand with seed 0 failed");
1129 
1130 	srand(1);
1131 	a = rand();
1132 	zassert_equal(a, 1103527590, "srand with seed 1 failed");
1133 
1134 	srand(10);
1135 	a = rand();
1136 	zassert_equal(a, 297746555, "srand with seed 10 failed");
1137 
1138 	srand(UINT_MAX - 1);
1139 	a = rand();
1140 	zassert_equal(a, 2087949151, "srand with seed UINT_MAX - 1 failed");
1141 
1142 	srand(UINT_MAX);
1143 	a = rand();
1144 	zassert_equal(a, 1043980748, "srand with seed UINT_MAX failed");
1145 #else
1146 	ztest_test_skip();
1147 #endif
1148 }
1149 
1150 /**
1151  *
1152  * @brief Test rand function for reproducibility
1153  *
1154  */
ZTEST(libc_common,test_rand_reproducibility)1155 ZTEST(libc_common, test_rand_reproducibility)
1156 {
1157 #ifdef CONFIG_MINIMAL_LIBC
1158 	int a;
1159 	int b;
1160 	int c;
1161 
1162 	srand(0);
1163 	a = rand();
1164 	zassert_equal(a, 12345, "srand with seed 0 failed");
1165 	srand(0);
1166 	b = rand();
1167 	zassert_equal(b, 12345, "srand with seed 0 failed (2nd)");
1168 	srand(0);
1169 	c = rand();
1170 	zassert_equal(c, 12345, "srand with seed 0 failed (3rd)");
1171 
1172 	srand(1);
1173 	a = rand();
1174 	zassert_equal(a, 1103527590, "srand with seed 1 failed");
1175 	srand(1);
1176 	b = rand();
1177 	zassert_equal(b, 1103527590, "srand with seed 1 failed (2nd)");
1178 	srand(1);
1179 	c = rand();
1180 	zassert_equal(c, 1103527590, "srand with seed 1 failed (3rd)");
1181 
1182 	srand(10);
1183 	a = rand();
1184 	zassert_equal(a, 297746555, "srand with seed 10 failed");
1185 	srand(10);
1186 	b = rand();
1187 	zassert_equal(b, 297746555, "srand with seed 10 failed (2nd)");
1188 	srand(10);
1189 	c = rand();
1190 	zassert_equal(c, 297746555, "srand with seed 10 failed (3rd)");
1191 
1192 	srand(UINT_MAX - 1);
1193 	a = rand();
1194 	zassert_equal(a, 2087949151, "srand with seed UINT_MAX - 1 failed");
1195 	srand(UINT_MAX - 1);
1196 	b = rand();
1197 	zassert_equal(b, 2087949151, "srand with seed UINT_MAX - 1 failed (2nd)");
1198 	srand(UINT_MAX - 1);
1199 	c = rand();
1200 	zassert_equal(c, 2087949151, "srand with seed UINT_MAX - 1 failed (3rd)");
1201 
1202 	srand(UINT_MAX);
1203 	a = rand();
1204 	zassert_equal(a, 1043980748, "srand with seed UINT_MAX failed");
1205 	srand(UINT_MAX);
1206 	b = rand();
1207 	zassert_equal(b, 1043980748, "srand with seed UINT_MAX failed (2nd)");
1208 	srand(UINT_MAX);
1209 	c = rand();
1210 	zassert_equal(c, 1043980748, "srand with seed UINT_MAX failed (3rd)");
1211 #else
1212 	ztest_test_skip();
1213 #endif
1214 }
1215 
1216 /**
1217  *
1218  * @brief test abort functions
1219  *
1220  * @see abort().
1221  */
ZTEST(libc_common,test_abort)1222 ZTEST(libc_common, test_abort)
1223 {
1224 #ifdef CONFIG_EXTERNAL_LIBC
1225 	ztest_test_skip();
1226 #else
1227 	int a = 0;
1228 
1229 	ztest_set_fault_valid(true);
1230 	abort();
1231 	zassert_equal(a, 0, "abort failed");
1232 #endif
1233 }
1234 
1235 /**
1236  *
1237  * @brief test exit functions
1238  *
1239  */
1240 #ifndef CONFIG_EXTERNAL_LIBC
exit_program(void * p1,void * p2,void * p3)1241 static void exit_program(void *p1, void *p2, void *p3)
1242 {
1243 	exit(1);
1244 }
1245 
1246 static K_THREAD_STACK_DEFINE(tstack, STACK_SIZE);
1247 static struct k_thread tdata;
1248 
1249 #endif
1250 
ZTEST(libc_common,test_exit)1251 ZTEST(libc_common, test_exit)
1252 {
1253 #ifdef CONFIG_EXTERNAL_LIBC
1254 	ztest_test_skip();
1255 #else
1256 	int a = 0;
1257 
1258 	k_tid_t tid = k_thread_create(&tdata, tstack, STACK_SIZE, exit_program,
1259 					NULL, NULL, NULL, K_PRIO_PREEMPT(0), 0, K_NO_WAIT);
1260 	k_sleep(K_MSEC(10));
1261 	k_thread_abort(tid);
1262 	zassert_equal(a, 0, "exit failed");
1263 #endif
1264 }
1265