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