aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorSimon Glass <sjg@chromium.org>2021-07-24 09:03:33 -0600
committerTom Rini <trini@konsulko.com>2021-08-02 13:32:14 -0400
commit4d3177d367e89619d22017a96addcfcb498e69a3 (patch)
tree11c42f22437c9c223369e56a2ae1da00259ec5e1 /test
parent96b23440c1b74cd95022e3ebb08a60fedb04f3b9 (diff)
downloadu-boot-4d3177d367e89619d22017a96addcfcb498e69a3.zip
u-boot-4d3177d367e89619d22017a96addcfcb498e69a3.tar.gz
u-boot-4d3177d367e89619d22017a96addcfcb498e69a3.tar.bz2
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 <sjg@chromium.org>
Diffstat (limited to 'test')
-rw-r--r--test/str_ut.c61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/str_ut.c b/test/str_ut.c
index 8133b21..880cc92 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;