colibri_imx6: fix video stdout in default environment
[oweals/u-boot.git] / test / str_ut.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2020 Google LLC
4  */
5
6 #include <common.h>
7 #include <vsprintf.h>
8 #include <test/suites.h>
9 #include <test/test.h>
10 #include <test/ut.h>
11
12 /* This is large enough for any of the test strings */
13 #define TEST_STR_SIZE   200
14
15 static const char str1[] = "I'm sorry I'm late.";
16 static const char str2[] = "1099abNo, don't bother apologising.";
17 static const char str3[] = "0xbI'm sorry you're alive.";
18
19 /* Declare a new str test */
20 #define STR_TEST(_name, _flags)         UNIT_TEST(_name, _flags, str_test)
21
22 static int str_test_upper(struct unit_test_state *uts)
23 {
24         char out[TEST_STR_SIZE];
25
26         /* Make sure it adds a terminator */
27         out[strlen(str1)] = 'a';
28         str_to_upper(str1, out, SIZE_MAX);
29         ut_asserteq_str("I'M SORRY I'M LATE.", out);
30
31         /* In-place operation */
32         strcpy(out, str2);
33         str_to_upper(out, out, SIZE_MAX);
34         ut_asserteq_str("1099ABNO, DON'T BOTHER APOLOGISING.", out);
35
36         /* Limited length */
37         str_to_upper(str1, out, 7);
38         ut_asserteq_str("I'M SORO, DON'T BOTHER APOLOGISING.", out);
39
40         /* In-place with limited length */
41         strcpy(out, str2);
42         str_to_upper(out, out, 7);
43         ut_asserteq_str("1099ABNo, don't bother apologising.", out);
44
45         /* Copy an empty string to a buffer with space*/
46         out[1] = 0x7f;
47         str_to_upper("", out, SIZE_MAX);
48         ut_asserteq('\0', *out);
49         ut_asserteq(0x7f, out[1]);
50
51         /* Copy an empty string to a buffer with no space*/
52         out[0] = 0x7f;
53         str_to_upper("", out, 0);
54         ut_asserteq(0x7f, out[0]);
55
56         return 0;
57 }
58 STR_TEST(str_test_upper, 0);
59
60 static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
61                        ulong expect_val, int expect_endp_offset, bool upper)
62 {
63         char out[TEST_STR_SIZE];
64         char *endp;
65         ulong val;
66
67         strcpy(out, str);
68         if (upper)
69                 str_to_upper(out, out, -1);
70
71         val = simple_strtoul(out, &endp, base);
72         ut_asserteq(expect_val, val);
73         ut_asserteq(expect_endp_offset, endp - out);
74
75         return 0;
76 }
77
78 static int str_simple_strtoul(struct unit_test_state *uts)
79 {
80         int upper;
81
82         /* Check that it is case-insentive */
83         for (upper = 0; upper < 2; upper++) {
84                 /* Base 10 and base 16 */
85                 ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper));
86                 ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper));
87
88                 /* Invalid string */
89                 ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper));
90
91                 /* Base 0 */
92                 ut_assertok(run_strtoul(uts, str1, 0, 0, 0, upper));
93                 ut_assertok(run_strtoul(uts, str2, 0, 1099, 4, upper));
94                 ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3, upper));
95
96                 /* Base 2 */
97                 ut_assertok(run_strtoul(uts, str1, 2, 0, 0, upper));
98                 ut_assertok(run_strtoul(uts, str2, 2, 2, 2, upper));
99         }
100
101         /* Check endp being NULL */
102         ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
103
104         return 0;
105 }
106 STR_TEST(str_simple_strtoul, 0);
107
108 int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
109 {
110         struct unit_test *tests = ll_entry_start(struct unit_test,
111                                                  str_test);
112         const int n_ents = ll_entry_count(struct unit_test, str_test);
113
114         return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
115 }