[PATCH 01/10] test: Add some tests for setexpr

Simon Glass sjg at chromium.org
Sun Nov 1 22:15:35 CET 2020


This command currently has no tests. Add some for basic assignment and the
integer operations.

Note that the default size for setexpr is ulong, which varies depending on
the build machine. So for sandbox on a 64-bit host, this means that the
default size is 64 bits.

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

 include/test/suites.h |   2 +
 test/cmd/Makefile     |   1 +
 test/cmd/setexpr.c    | 162 ++++++++++++++++++++++++++++++++++++++++++
 test/cmd_ut.c         |   3 +
 4 files changed, 168 insertions(+)
 create mode 100644 test/cmd/setexpr.c

diff --git a/include/test/suites.h b/include/test/suites.h
index ab7b3bd9cad..5c97846e7f5 100644
--- a/include/test/suites.h
+++ b/include/test/suites.h
@@ -38,6 +38,8 @@ int do_ut_mem(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_optee(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_overlay(struct cmd_tbl *cmdtp, int flag, int argc,
 		  char *const argv[]);
+int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc,
+		  char *const argv[]);
 int do_ut_str(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_time(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 int do_ut_unicode(struct cmd_tbl *cmdtp, int flag, int argc,
diff --git a/test/cmd/Makefile b/test/cmd/Makefile
index 859dcda2393..b2f71af8473 100644
--- a/test/cmd/Makefile
+++ b/test/cmd/Makefile
@@ -4,3 +4,4 @@
 
 obj-y += mem.o
 obj-$(CONFIG_CMD_MEM_SEARCH) += mem_search.o
+obj-y += setexpr.o
diff --git a/test/cmd/setexpr.c b/test/cmd/setexpr.c
new file mode 100644
index 00000000000..cab6fdf4b83
--- /dev/null
+++ b/test/cmd/setexpr.c
@@ -0,0 +1,162 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Tests for setexpr command
+ *
+ * Copyright 2020 Google LLC
+ * Written by Simon Glass <sjg at chromium.org>
+ */
+
+#include <common.h>
+#include <console.h>
+#include <mapmem.h>
+#include <dm/test.h>
+#include <test/suites.h>
+#include <test/ut.h>
+
+#define BUF_SIZE	0x100
+
+/* Declare a new mem test */
+#define SETEXPR_TEST(_name, _flags)	UNIT_TEST(_name, _flags, setexpr_test)
+
+/* Test 'setexpr' command with simply setting integers */
+static int setexpr_test_int(struct unit_test_state *uts)
+{
+	u8 *buf;
+
+	buf = map_sysmem(0, BUF_SIZE);
+	memset(buf, '\xff', BUF_SIZE);
+
+	/* byte */
+	buf[0x0] = 0x12;
+	ut_assertok(run_command("setexpr.b fred 0", 0));
+	ut_asserteq_str("0", env_get("fred"));
+	ut_assertok(run_command("setexpr.b fred *0", 0));
+	ut_asserteq_str("12", env_get("fred"));
+
+	/* 16-bit */
+	*(short *)buf = 0x2345;
+	ut_assertok(run_command("setexpr.w fred 0", 0));
+	ut_asserteq_str("0", env_get("fred"));
+	ut_assertok(run_command("setexpr.w fred *0", 0));
+	ut_asserteq_str("2345", env_get("fred"));
+
+	/* 32-bit */
+	*(u32 *)buf = 0x3456789a;
+	ut_assertok(run_command("setexpr.l fred 0", 0));
+	ut_asserteq_str("0", env_get("fred"));
+	ut_assertok(run_command("setexpr.l fred *0", 0));
+	ut_asserteq_str("ffffffff3456789a", env_get("fred"));
+
+	/* 64-bit */
+	*(u64 *)buf = 0x456789abcdef0123;
+	ut_assertok(run_command("setexpr.q fred 0", 0));
+	ut_asserteq_str("0", env_get("fred"));
+	ut_assertok(run_command("setexpr.q fred *0", 0));
+	ut_asserteq_str("456789abcdef0123", env_get("fred"));
+
+	/* default */
+	ut_assertok(run_command("setexpr fred 0", 0));
+	ut_asserteq_str("0", env_get("fred"));
+	ut_assertok(run_command("setexpr fred *0", 0));
+	ut_asserteq_str("456789abcdef0123", env_get("fred"));
+
+	unmap_sysmem(buf);
+
+	return 0;
+}
+SETEXPR_TEST(setexpr_test_int, UT_TESTF_CONSOLE_REC);
+
+/* Test 'setexpr' command with + operator */
+static int setexpr_test_plus(struct unit_test_state *uts)
+{
+	char *buf;
+
+	buf = map_sysmem(0, BUF_SIZE);
+	memset(buf, '\xff', BUF_SIZE);
+
+	/* byte */
+	buf[0x0] = 0x12;
+	buf[0x10] = 0x34;
+	ut_assertok(run_command("setexpr.b fred *0 + *10", 0));
+	ut_asserteq_str("46", env_get("fred"));
+
+	/* 16-bit */
+	*(short *)buf = 0x2345;
+	*(short *)(buf + 0x10) = 0xf012;
+	ut_assertok(run_command("setexpr.w fred *0 + *10", 0));
+	ut_asserteq_str("11357", env_get("fred"));
+
+	/* 32-bit */
+	*(u32 *)buf = 0x3456789a;
+	*(u32 *)(buf + 0x10) = 0xc3384235;
+	ut_assertok(run_command("setexpr.l fred *0 + *10", 0));
+	ut_asserteq_str("fffffffef78ebacf", env_get("fred"));
+
+	/* 64-bit */
+	*(u64 *)buf = 0x456789abcdef0123;
+	*(u64 *)(buf + 0x10) = 0x4987328372849283;
+	ut_assertok(run_command("setexpr.q fred *0 + *10", 0));
+	ut_asserteq_str("8eeebc2f407393a6", env_get("fred"));
+
+	/* default */
+	ut_assertok(run_command("setexpr fred *0 + *10", 0));
+	ut_asserteq_str("8eeebc2f407393a6", env_get("fred"));
+
+	unmap_sysmem(buf);
+
+	return 0;
+}
+SETEXPR_TEST(setexpr_test_plus, UT_TESTF_CONSOLE_REC);
+
+/* Test 'setexpr' command with other operators */
+static int setexpr_test_oper(struct unit_test_state *uts)
+{
+	char *buf;
+
+	buf = map_sysmem(0, BUF_SIZE);
+	memset(buf, '\xff', BUF_SIZE);
+
+	*(u32 *)buf = 0x1234;
+	*(u32 *)(buf + 0x10) = 0x560000;
+
+	/* Quote | to avoid confusing hush */
+	ut_assertok(run_command("setexpr fred *0 \"|\" *10", 0));
+	ut_asserteq_str("ffffffff00561234", env_get("fred"));
+
+	*(u32 *)buf = 0x561200;
+	*(u32 *)(buf + 0x10) = 0x1234;
+
+	/* Quote & to avoid confusing hush */
+	ut_assertok(run_command("setexpr.l fred *0 \"&\" *10", 0));
+	ut_asserteq_str("ffffffff00001200", env_get("fred"));
+
+	ut_assertok(run_command("setexpr.l fred *0 ^ *10", 0));
+	ut_asserteq_str("560034", env_get("fred"));
+
+	ut_assertok(run_command("setexpr.l fred *0 - *10", 0));
+	ut_asserteq_str("55ffcc", env_get("fred"));
+
+	ut_assertok(run_command("setexpr.l fred *0 * *10", 0));
+	ut_asserteq_str("ffa9dbd21ebfa800", env_get("fred"));
+
+	ut_assertok(run_command("setexpr.l fred *0 / *10", 0));
+	ut_asserteq_str("1", env_get("fred"));
+
+	ut_assertok(run_command("setexpr.l fred *0 % *10", 0));
+	ut_asserteq_str("55ffcc", env_get("fred"));
+
+	unmap_sysmem(buf);
+
+	return 0;
+}
+SETEXPR_TEST(setexpr_test_oper, UT_TESTF_CONSOLE_REC);
+
+int do_ut_setexpr(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	struct unit_test *tests = ll_entry_start(struct unit_test,
+						 setexpr_test);
+	const int n_ents = ll_entry_count(struct unit_test, setexpr_test);
+
+	return cmd_ut_category("cmd_setexpr", "cmd_mem_", tests, n_ents, argc,
+			       argv);
+}
diff --git a/test/cmd_ut.c b/test/cmd_ut.c
index 8f0bc688a22..f79109e6f8e 100644
--- a/test/cmd_ut.c
+++ b/test/cmd_ut.c
@@ -75,6 +75,8 @@ static struct cmd_tbl cmd_ut_sub[] = {
 	U_BOOT_CMD_MKENT(log, CONFIG_SYS_MAXARGS, 1, do_ut_log, "", ""),
 #endif
 	U_BOOT_CMD_MKENT(mem, CONFIG_SYS_MAXARGS, 1, do_ut_mem, "", ""),
+	U_BOOT_CMD_MKENT(setexpr, CONFIG_SYS_MAXARGS, 1, do_ut_setexpr, "",
+			 ""),
 #ifdef CONFIG_UT_TIME
 	U_BOOT_CMD_MKENT(time, CONFIG_SYS_MAXARGS, 1, do_ut_time, "", ""),
 #endif
@@ -153,6 +155,7 @@ static char ut_help_text[] =
 #ifdef CONFIG_UT_OVERLAY
 	"ut overlay [test-name]\n"
 #endif
+	"ut setexpr [test-name] - test setexpr command\n"
 #ifdef CONFIG_SANDBOX
 	"ut str - Basic test of string functions\n"
 #endif
-- 
2.29.1.341.ge80a0c044ae-goog



More information about the U-Boot mailing list