From 7e5f460ec457fe310156e399198a41eb0ce1e98c Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 24 Jul 2021 09:03:29 -0600 Subject: global: Convert simple_strtoul() with hex to hextoul() It is a pain to have to specify the value 16 in each call. Add a new hextoul() function and update the code to use it. Add a proper comment to simple_strtoul() while we are here. Signed-off-by: Simon Glass --- test/str_ut.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'test') diff --git a/test/str_ut.c b/test/str_ut.c index 359d7d4ea1f..4c3b566d2c1 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -105,6 +105,18 @@ static int str_simple_strtoul(struct unit_test_state *uts) } STR_TEST(str_simple_strtoul, 0); +static int str_hextoul(struct unit_test_state *uts) +{ + char *endp; + + /* Just a simple test, since we know this uses simple_strtoul() */ + ut_asserteq(0x1099ab, hextoul(str2, &endp)); + ut_asserteq(6, endp - str2); + + return 0; +} +STR_TEST(str_hextoul, 0); + int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct unit_test *tests = UNIT_TEST_SUITE_START(str_test); -- cgit v1.2.3 From 0b1284eb52578e15ec611adc5fee1a9ae68dadea Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 24 Jul 2021 09:03:30 -0600 Subject: global: Convert simple_strtoul() with decimal to dectoul() It is a pain to have to specify the value 10 in each call. Add a new dectoul() function and update the code to use it. Signed-off-by: Simon Glass --- test/str_ut.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'test') diff --git a/test/str_ut.c b/test/str_ut.c index 4c3b566d2c1..19f2c127135 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -117,6 +117,18 @@ static int str_hextoul(struct unit_test_state *uts) } STR_TEST(str_hextoul, 0); +static int str_dectoul(struct unit_test_state *uts) +{ + char *endp; + + /* Just a simple test, since we know this uses simple_strtoul() */ + ut_asserteq(1099, dectoul(str2, &endp)); + ut_asserteq(4, endp - str2); + + return 0; +} +STR_TEST(str_dectoul, 0); + int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { struct unit_test *tests = UNIT_TEST_SUITE_START(str_test); -- cgit v1.2.3 From 96b23440c1b74cd95022e3ebb08a60fedb04f3b9 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 24 Jul 2021 09:03:32 -0600 Subject: lib: Drop unnecessary check for hex digit If we see 0x then we can assume this is the start of a hex value. It does not seem necessary to check for a hex digit after that since it will happen when parsing the value anyway. Drop this check to simplify the code and reduce size. Add a few more test cases for when a 0x prefix is used. Signed-off-by: Simon Glass --- test/str_ut.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'test') diff --git a/test/str_ut.c b/test/str_ut.c index 19f2c127135..8133b213bfa 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -84,6 +84,8 @@ static int str_simple_strtoul(struct unit_test_state *uts) /* Base 10 and base 16 */ ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper)); ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper)); + ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper)); + ut_assertok(run_strtoul(uts, str3, 10, 0, 1, upper)); /* Invalid string */ ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper)); -- cgit v1.2.3 From 4d3177d367e89619d22017a96addcfcb498e69a3 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 24 Jul 2021 09:03:33 -0600 Subject: lib: Add tests for simple_strtoull() Add some tests that check the behaviour of this function. These are the same as for simple_strtoul() but with a few longer values. Signed-off-by: Simon Glass --- test/str_ut.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) (limited to 'test') diff --git a/test/str_ut.c b/test/str_ut.c index 8133b213bfa..880cc928ec8 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -15,6 +15,8 @@ static const char str1[] = "I'm sorry I'm late."; static const char str2[] = "1099abNo, don't bother apologising."; static const char str3[] = "0xbI'm sorry you're alive."; +static const char str4[] = "1234567890123 I lost closer friends"; +static const char str5[] = "0x9876543210the last time I was deloused"; /* Declare a new str test */ #define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test) @@ -107,6 +109,65 @@ static int str_simple_strtoul(struct unit_test_state *uts) } STR_TEST(str_simple_strtoul, 0); +static int run_strtoull(struct unit_test_state *uts, const char *str, int base, + unsigned long long expect_val, int expect_endp_offset, + bool upper) +{ + char out[TEST_STR_SIZE]; + char *endp; + unsigned long long val; + + strcpy(out, str); + if (upper) + str_to_upper(out, out, -1); + + val = simple_strtoull(out, &endp, base); + ut_asserteq(expect_val, val); + ut_asserteq(expect_endp_offset, endp - out); + + return 0; +} + +static int str_simple_strtoull(struct unit_test_state *uts) +{ + int upper; + + /* Check that it is case-insentive */ + for (upper = 0; upper < 2; upper++) { + /* Base 10 and base 16 */ + ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper)); + ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper)); + ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper)); + ut_assertok(run_strtoull(uts, str3, 10, 0, 1, upper)); + + /* Large values */ + ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13, + upper)); + ut_assertok(run_strtoull(uts, str4, 16, 0x1234567890123, 13, + upper)); + ut_assertok(run_strtoull(uts, str5, 0, 0x9876543210, 12, + upper)); + + /* Invalid string */ + ut_assertok(run_strtoull(uts, str1, 10, 0, 0, upper)); + + /* Base 0 */ + ut_assertok(run_strtoull(uts, str1, 0, 0, 0, upper)); + ut_assertok(run_strtoull(uts, str2, 0, 1099, 4, upper)); + ut_assertok(run_strtoull(uts, str3, 0, 0xb, 3, upper)); + + /* Base 2 */ + ut_assertok(run_strtoull(uts, str1, 2, 0, 0, upper)); + ut_assertok(run_strtoull(uts, str2, 2, 2, 2, upper)); + } + + /* Check endp being NULL */ + ut_asserteq(1099, simple_strtoull(str2, NULL, 0)); + + return 0; +} +STR_TEST(str_simple_strtoull, 0); + static int str_hextoul(struct unit_test_state *uts) { char *endp; -- cgit v1.2.3 From ab833ef60a13b60bfa8e236ca774e91b22255a5a Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 24 Jul 2021 09:03:34 -0600 Subject: lib: Add octal tests for simple_strtoul/l() This function support decoding octal but no tests are included yet. Add some. Signed-off-by: Simon Glass --- test/str_ut.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'test') diff --git a/test/str_ut.c b/test/str_ut.c index 880cc928ec8..0d1bf398099 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -17,6 +17,8 @@ static const char str2[] = "1099abNo, don't bother apologising."; static const char str3[] = "0xbI'm sorry you're alive."; static const char str4[] = "1234567890123 I lost closer friends"; static const char str5[] = "0x9876543210the last time I was deloused"; +static const char str6[] = "0778octal is seldom used"; +static const char str7[] = "707it is a piece of computing history"; /* Declare a new str test */ #define STR_TEST(_name, _flags) UNIT_TEST(_name, _flags, str_test) @@ -89,6 +91,10 @@ static int str_simple_strtoul(struct unit_test_state *uts) ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper)); ut_assertok(run_strtoul(uts, str3, 10, 0, 1, upper)); + /* Octal */ + ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper)); + ut_assertok(run_strtoul(uts, str7, 8, 0x1c7, 3, upper)); + /* Invalid string */ ut_assertok(run_strtoul(uts, str1, 10, 0, 0, upper)); @@ -140,6 +146,10 @@ static int str_simple_strtoull(struct unit_test_state *uts) ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper)); ut_assertok(run_strtoull(uts, str3, 10, 0, 1, upper)); + /* Octal */ + ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper)); + ut_assertok(run_strtoull(uts, str7, 8, 0x1c7, 3, upper)); + /* Large values */ ut_assertok(run_strtoull(uts, str4, 10, 1234567890123, 13, upper)); -- cgit v1.2.3 From e6951139c0544116330b12e287fe45e30bbab11c Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Sat, 24 Jul 2021 09:03:38 -0600 Subject: lib: Allow using 0x when a decimal value is requested U-Boot mostly uses hex for value input, largely because addresses are much easier to understand in hex. But in some cases a decimal value is requested, such as where the value is small or hex does not make sense in the context. In these cases it is sometimes useful to be able to provide a hex value in any case, if only to resolve any ambiguity. Add this functionality, for increased flexibility. Signed-off-by: Simon Glass --- test/str_ut.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test') diff --git a/test/str_ut.c b/test/str_ut.c index 0d1bf398099..d2840d51524 100644 --- a/test/str_ut.c +++ b/test/str_ut.c @@ -89,7 +89,7 @@ static int str_simple_strtoul(struct unit_test_state *uts) ut_assertok(run_strtoul(uts, str2, 10, 1099, 4, upper)); ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6, upper)); ut_assertok(run_strtoul(uts, str3, 16, 0xb, 3, upper)); - ut_assertok(run_strtoul(uts, str3, 10, 0, 1, upper)); + ut_assertok(run_strtoul(uts, str3, 10, 0xb, 3, upper)); /* Octal */ ut_assertok(run_strtoul(uts, str6, 0, 63, 3, upper)); @@ -144,7 +144,7 @@ static int str_simple_strtoull(struct unit_test_state *uts) ut_assertok(run_strtoull(uts, str2, 10, 1099, 4, upper)); ut_assertok(run_strtoull(uts, str2, 16, 0x1099ab, 6, upper)); ut_assertok(run_strtoull(uts, str3, 16, 0xb, 3, upper)); - ut_assertok(run_strtoull(uts, str3, 10, 0, 1, upper)); + ut_assertok(run_strtoull(uts, str3, 10, 0xb, 3, upper)); /* Octal */ ut_assertok(run_strtoull(uts, str6, 0, 63, 3, upper)); -- cgit v1.2.3