diff mbox series

[v5,1/6] test: Add the beginnings of some string tests

Message ID 20200408083251.v5.1.I9544b8bcada571406f30889eda869246c2b7cc7e@changeid
State Superseded
Headers show
Series A few little patches | expand

Commit Message

Simon Glass April 8, 2020, 2:32 p.m. UTC
There are quite a few string functions in U-Boot with no tests. Make a
start by adding a test for strtoul().

Signed-off-by: Simon Glass <sjg at chromium.org>
---

Changes in v5: None
Changes in v4:
- Add a new patch with some string tests

 include/test/suites.h |  1 +
 test/Makefile         |  1 +
 test/cmd_ut.c         |  5 ++++
 test/str_ut.c         | 67 +++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 74 insertions(+)
 create mode 100644 test/str_ut.c

Comments

Heinrich Schuchardt April 8, 2020, 5:53 p.m. UTC | #1
On 2020-04-08 16:32, Simon Glass wrote:
> There are quite a few string functions in U-Boot with no tests. Make a
> start by adding a test for strtoul().
>
> Signed-off-by: Simon Glass <sjg at chromium.org>
> ---
>
> Changes in v5: None
> Changes in v4:
> - Add a new patch with some string tests
>
>  include/test/suites.h |  1 +
>  test/Makefile         |  1 +
>  test/cmd_ut.c         |  5 ++++
>  test/str_ut.c         | 67 +++++++++++++++++++++++++++++++++++++++++++
>  4 files changed, 74 insertions(+)
>  create mode 100644 test/str_ut.c
>
> diff --git a/include/test/suites.h b/include/test/suites.h
> index 0748185eaf7..6d4270fa33b 100644
> --- a/include/test/suites.h
> +++ b/include/test/suites.h
> @@ -32,6 +32,7 @@ int do_ut_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
>  int do_ut_lib(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
>  int do_ut_optee(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
>  int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
> +int do_ut_str(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
>  int do_ut_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
>  int do_ut_unicode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
>
> diff --git a/test/Makefile b/test/Makefile
> index 2fe41f489c3..917e54a3fcc 100644
> --- a/test/Makefile
> +++ b/test/Makefile
> @@ -8,6 +8,7 @@ obj-$(CONFIG_UNIT_TEST) += ut.o
>  obj-$(CONFIG_SANDBOX) += command_ut.o
>  obj-$(CONFIG_SANDBOX) += compression.o
>  obj-$(CONFIG_SANDBOX) += print_ut.o
> +obj-$(CONFIG_SANDBOX) += str_ut.o

Why should this test suite be restricted to the sandbox?

It runs fine on qemu_arm64_defconfig if enabled.

Best regards

Heinrich

>  obj-$(CONFIG_UT_TIME) += time_ut.o
>  obj-$(CONFIG_UT_UNICODE) += unicode_ut.o
>  obj-$(CONFIG_$(SPL_)LOG) += log/
> diff --git a/test/cmd_ut.c b/test/cmd_ut.c
> index a3a9d49f7ec..b342c35e68e 100644
> --- a/test/cmd_ut.c
> +++ b/test/cmd_ut.c
> @@ -71,6 +71,8 @@ static cmd_tbl_t cmd_ut_sub[] = {
>  			 "", ""),
>  	U_BOOT_CMD_MKENT(bloblist, CONFIG_SYS_MAXARGS, 1, do_ut_bloblist,
>  			 "", ""),
> +	U_BOOT_CMD_MKENT(str, CONFIG_SYS_MAXARGS, 1, do_ut_str,
> +			 "", ""),
>  #endif
>  };
>
> @@ -131,6 +133,9 @@ static char ut_help_text[] =
>  #ifdef CONFIG_UT_OVERLAY
>  	"ut overlay [test-name]\n"
>  #endif
> +#ifdef CONFIG_SANDBOX
> +	"ut str - Basic test of string functions\n"
> +#endif
>  #ifdef CONFIG_UT_TIME
>  	"ut time - Very basic test of time functions\n"
>  #endif
> diff --git a/test/str_ut.c b/test/str_ut.c
> new file mode 100644
> index 00000000000..fab8de595cb
> --- /dev/null
> +++ b/test/str_ut.c
> @@ -0,0 +1,67 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * Copyright 2020 Google LLC
> + */
> +
> +#include <common.h>
> +#include <vsprintf.h>
> +#include <test/suites.h>
> +#include <test/test.h>
> +#include <test/ut.h>
> +
> +/* This is large enough for any of the test strings */
> +#define TEST_STR_SIZE	200
> +
> +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.";
> +
> +/* Declare a new str test */
> +#define STR_TEST(_name, _flags)		UNIT_TEST(_name, _flags, str_test)
> +
> +static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
> +		       ulong expect_val, int expect_endp_offset)
> +{
> +	char *endp;
> +	ulong val;
> +
> +	val = simple_strtoul(str, &endp, base);
> +	ut_asserteq(expect_val, val);
> +	ut_asserteq(expect_endp_offset, endp - str);
> +
> +	return 0;
> +}
> +
> +static int str_simple_strtoul(struct unit_test_state *uts)
> +{
> +	/* Base 10 and base 16 */
> +	ut_assertok(run_strtoul(uts, str2, 10, 1099, 4));
> +	ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6));
> +
> +	/* Invalid string */
> +	ut_assertok(run_strtoul(uts, str1, 10, 0, 0));
> +
> +	/* Base 0 */
> +	ut_assertok(run_strtoul(uts, str1, 0, 0, 0));
> +	ut_assertok(run_strtoul(uts, str2, 0, 1099, 4));
> +	ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3));
> +
> +	/* Base 2 */
> +	ut_assertok(run_strtoul(uts, str1, 2, 0, 0));
> +	ut_assertok(run_strtoul(uts, str2, 2, 2, 2));
> +
> +	/* Check endp being NULL */
> +	ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
> +
> +	return 0;
> +}
> +STR_TEST(str_simple_strtoul, 0);
> +
> +int do_ut_str(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
> +{
> +	struct unit_test *tests = ll_entry_start(struct unit_test,
> +						 str_test);
> +	const int n_ents = ll_entry_count(struct unit_test, str_test);
> +
> +	return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
> +}
>
Tom Rini April 26, 2020, 11:26 a.m. UTC | #2
On Wed, Apr 08, 2020 at 08:32:55AM -0600, Simon Glass wrote:

> There are quite a few string functions in U-Boot with no tests. Make a
> start by adding a test for strtoul().
> 
> Signed-off-by: Simon Glass <sjg at chromium.org>

Applied to u-boot/master, thanks!
diff mbox series

Patch

diff --git a/include/test/suites.h b/include/test/suites.h
index 0748185eaf7..6d4270fa33b 100644
--- a/include/test/suites.h
+++ b/include/test/suites.h
@@ -32,6 +32,7 @@  int do_ut_env(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_lib(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_optee(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_overlay(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
+int do_ut_str(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_time(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 int do_ut_unicode(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
 
diff --git a/test/Makefile b/test/Makefile
index 2fe41f489c3..917e54a3fcc 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -8,6 +8,7 @@  obj-$(CONFIG_UNIT_TEST) += ut.o
 obj-$(CONFIG_SANDBOX) += command_ut.o
 obj-$(CONFIG_SANDBOX) += compression.o
 obj-$(CONFIG_SANDBOX) += print_ut.o
+obj-$(CONFIG_SANDBOX) += str_ut.o
 obj-$(CONFIG_UT_TIME) += time_ut.o
 obj-$(CONFIG_UT_UNICODE) += unicode_ut.o
 obj-$(CONFIG_$(SPL_)LOG) += log/
diff --git a/test/cmd_ut.c b/test/cmd_ut.c
index a3a9d49f7ec..b342c35e68e 100644
--- a/test/cmd_ut.c
+++ b/test/cmd_ut.c
@@ -71,6 +71,8 @@  static cmd_tbl_t cmd_ut_sub[] = {
 			 "", ""),
 	U_BOOT_CMD_MKENT(bloblist, CONFIG_SYS_MAXARGS, 1, do_ut_bloblist,
 			 "", ""),
+	U_BOOT_CMD_MKENT(str, CONFIG_SYS_MAXARGS, 1, do_ut_str,
+			 "", ""),
 #endif
 };
 
@@ -131,6 +133,9 @@  static char ut_help_text[] =
 #ifdef CONFIG_UT_OVERLAY
 	"ut overlay [test-name]\n"
 #endif
+#ifdef CONFIG_SANDBOX
+	"ut str - Basic test of string functions\n"
+#endif
 #ifdef CONFIG_UT_TIME
 	"ut time - Very basic test of time functions\n"
 #endif
diff --git a/test/str_ut.c b/test/str_ut.c
new file mode 100644
index 00000000000..fab8de595cb
--- /dev/null
+++ b/test/str_ut.c
@@ -0,0 +1,67 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2020 Google LLC
+ */
+
+#include <common.h>
+#include <vsprintf.h>
+#include <test/suites.h>
+#include <test/test.h>
+#include <test/ut.h>
+
+/* This is large enough for any of the test strings */
+#define TEST_STR_SIZE	200
+
+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.";
+
+/* Declare a new str test */
+#define STR_TEST(_name, _flags)		UNIT_TEST(_name, _flags, str_test)
+
+static int run_strtoul(struct unit_test_state *uts, const char *str, int base,
+		       ulong expect_val, int expect_endp_offset)
+{
+	char *endp;
+	ulong val;
+
+	val = simple_strtoul(str, &endp, base);
+	ut_asserteq(expect_val, val);
+	ut_asserteq(expect_endp_offset, endp - str);
+
+	return 0;
+}
+
+static int str_simple_strtoul(struct unit_test_state *uts)
+{
+	/* Base 10 and base 16 */
+	ut_assertok(run_strtoul(uts, str2, 10, 1099, 4));
+	ut_assertok(run_strtoul(uts, str2, 16, 0x1099ab, 6));
+
+	/* Invalid string */
+	ut_assertok(run_strtoul(uts, str1, 10, 0, 0));
+
+	/* Base 0 */
+	ut_assertok(run_strtoul(uts, str1, 0, 0, 0));
+	ut_assertok(run_strtoul(uts, str2, 0, 1099, 4));
+	ut_assertok(run_strtoul(uts, str3, 0, 0xb, 3));
+
+	/* Base 2 */
+	ut_assertok(run_strtoul(uts, str1, 2, 0, 0));
+	ut_assertok(run_strtoul(uts, str2, 2, 2, 2));
+
+	/* Check endp being NULL */
+	ut_asserteq(1099, simple_strtoul(str2, NULL, 0));
+
+	return 0;
+}
+STR_TEST(str_simple_strtoul, 0);
+
+int do_ut_str(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+{
+	struct unit_test *tests = ll_entry_start(struct unit_test,
+						 str_test);
+	const int n_ents = ll_entry_count(struct unit_test, str_test);
+
+	return cmd_ut_category("str", "str_", tests, n_ents, argc, argv);
+}