test: Add the beginnings of some string tests
[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 run_strtoul(struct unit_test_state *uts, const char *str, int base,
23                        ulong expect_val, int expect_endp_offset)
24 {
25         char *endp;
26         ulong val;
27
28         val = simple_strtoul(str, &endp, base);
29         ut_asserteq(expect_val, val);
30         ut_asserteq(expect_endp_offset, endp - str);
31
32         return 0;
33 }
34
35 static int str_simple_strtoul(struct unit_test_state *uts)
36 {
37         /* Base 10 and base 16 */
38         ut_assertok(run_strtoul(uts, str2, 10, 1099, 4));
39         ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6));
40
41         /* Invalid string */
42         ut_assertok(run_strtoul(uts, str1, 10, 0, 0));
43
44         /* Base 0 */
45         ut_assertok(run_strtoul(uts, str1, 0, 0, 0));
46         ut_assertok(run_strtoul(uts, str2, 0, 1099, 4));
47         ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3));
48
49         /* Base 2 */
50         ut_assertok(run_strtoul(uts, str1, 2, 0, 0));
51         ut_assertok(run_strtoul(uts, str2, 2, 2, 2));
52
53         /* Check endp being NULL */
54         ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
55
56         return 0;
57 }
58 STR_TEST(str_simple_strtoul, 0);
59
60 int do_ut_str(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
61 {
62         struct unit_test *tests = ll_entry_start(struct unit_test,
63                                                  str_test);
64         const int n_ents = ll_entry_count(struct unit_test, str_test);
65
66         return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
67 }