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