1 /*
2 * Copyright (c) 2022 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <errno.h>
8 #include <string.h>
9
10 #include <zephyr/ztest.h>
11
ZTEST(libc_strerror,test_strerror)12 ZTEST(libc_strerror, test_strerror)
13 {
14 const char *expected;
15 const char *actual;
16
17 errno = 4242;
18 if (IS_ENABLED(CONFIG_MINIMAL_LIBC_DISABLE_STRING_ERROR_TABLE)) {
19 expected = "";
20 actual = strerror(EINVAL);
21 } else {
22 expected = "Invalid argument";
23 actual = strerror(EINVAL);
24 }
25 zassert_equal(0, strcmp(expected, actual),
26 "mismatch: exp: %s act: %s", expected, actual);
27
28 /* do not change errno on success */
29 zassert_equal(4242, errno, "");
30
31 #ifndef CONFIG_EXTERNAL_LIBC
32 /* consistent behaviour w.r.t. errno with invalid input */
33 errno = 0;
34 expected = "";
35 actual = strerror(-42);
36 zassert_equal(0, strcmp(expected, actual), "mismatch: exp: %s act: %s",
37 expected, actual);
38 actual = strerror(4242);
39 zassert_equal(0, strcmp(expected, actual), "mismatch: exp: %s act: %s",
40 expected, actual);
41 /* do not change errno on failure (for consistence) */
42 zassert_equal(0, errno, "");
43 #endif
44
45 /* consistent behaviour for "Success" */
46 if (!IS_ENABLED(CONFIG_MINIMAL_LIBC_DISABLE_STRING_ERROR_TABLE)) {
47 expected = "Success";
48 actual = strerror(0);
49 zassert_equal(0, strcmp(expected, actual),
50 "mismatch: exp: %s act: %s", expected, actual);
51 }
52 }
53
54 ZTEST_SUITE(libc_strerror, NULL, NULL, NULL, NULL, NULL);
55