blob: 7c8015050add951a3d68125182c3495af1134522 [file] [log] [blame]
Simon Glassd0509282020-04-08 08:32:55 -06001// 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
15static const char str1[] = "I'm sorry I'm late.";
16static const char str2[] = "1099abNo, don't bother apologising.";
17static 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
Simon Glass811a2602020-04-08 08:32:56 -060022static 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}
58STR_TEST(str_test_upper, 0);
59
Simon Glassd0509282020-04-08 08:32:55 -060060static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
Simon Glass811a2602020-04-08 08:32:56 -060061 ulong expect_val, int expect_endp_offset, bool upper)
Simon Glassd0509282020-04-08 08:32:55 -060062{
Simon Glass811a2602020-04-08 08:32:56 -060063 char out[TEST_STR_SIZE];
Simon Glassd0509282020-04-08 08:32:55 -060064 char *endp;
65 ulong val;
66
Simon Glass811a2602020-04-08 08:32:56 -060067 strcpy(out, str);
68 if (upper)
69 str_to_upper(out, out, -1);
70
71 val = simple_strtoul(out, &endp, base);
Simon Glassd0509282020-04-08 08:32:55 -060072 ut_asserteq(expect_val, val);
Simon Glass811a2602020-04-08 08:32:56 -060073 ut_asserteq(expect_endp_offset, endp - out);
Simon Glassd0509282020-04-08 08:32:55 -060074
75 return 0;
76}
77
78static int str_simple_strtoul(struct unit_test_state *uts)
79{
Simon Glass811a2602020-04-08 08:32:56 -060080 int upper;
Simon Glassd0509282020-04-08 08:32:55 -060081
Simon Glass811a2602020-04-08 08:32:56 -060082 /* 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));
Simon Glassd0509282020-04-08 08:32:55 -060087
Simon Glass811a2602020-04-08 08:32:56 -060088 /* 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));
Simon Glassd0509282020-04-08 08:32:55 -060095
Simon Glass811a2602020-04-08 08:32:56 -060096 /* 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 }
Simon Glassd0509282020-04-08 08:32:55 -0600100
101 /* Check endp being NULL */
102 ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
103
104 return 0;
105}
106STR_TEST(str_simple_strtoul, 0);
107
108int do_ut_str(cmd_tbl_t *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}