[PATCH 2/2] test: self: add some test cases for test command

Ahmad Fatoum a.fatoum at pengutronix.de
Wed Sep 13 04:59:58 PDT 2023


With the rework to how arithmetic comparisons are conducted in the test
command, some tests are in order.

Prior to the rework, following test cases failed:

  test_test_command:42: [ -1 -le  1 ]: assertion failure, ret=1
  test_test_command:45: [ -9223372036854775808 -lt 9223372036854775807 ]: assertion failure, ret=1
  test_test_command:46: [ -9223372036854775808 -gt 9223372036854775807 ]: assertion failure, ret=0

But now they all succeed.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 test/self/Kconfig        |  4 +++
 test/self/Makefile       |  1 +
 test/self/test_command.c | 64 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+)
 create mode 100644 test/self/test_command.c

diff --git a/test/self/Kconfig b/test/self/Kconfig
index a4176ab8ffd6..15e00f0244b5 100644
--- a/test/self/Kconfig
+++ b/test/self/Kconfig
@@ -97,4 +97,8 @@ config SELFTEST_REGULATOR
 	depends on REGULATOR && OFDEVICE
 	select OF_OVERLAY
 
+config SELFTEST_TEST_COMMAND
+	bool "test command selftest"
+	depends on CMD_TEST
+
 endif
diff --git a/test/self/Makefile b/test/self/Makefile
index 78a337810d1f..8168abf26278 100644
--- a/test/self/Makefile
+++ b/test/self/Makefile
@@ -14,6 +14,7 @@ obj-$(CONFIG_SELFTEST_MMU) += mmu.o
 obj-$(CONFIG_SELFTEST_STRING) += string.o
 obj-$(CONFIG_SELFTEST_SETJMP) += setjmp.o
 obj-$(CONFIG_SELFTEST_REGULATOR) += regulator.o test_regulator.dtbo.o
+obj-$(CONFIG_SELFTEST_TEST_COMMAND) += test_command.o
 
 clean-files := *.dtb *.dtb.S .*.dtc .*.pre .*.dts *.dtb.z
 clean-files += *.dtbo *.dtbo.S .*.dtso
diff --git a/test/self/test_command.c b/test/self/test_command.c
new file mode 100644
index 000000000000..7a26f724f3ce
--- /dev/null
+++ b/test/self/test_command.c
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <bselftest.h>
+#include <command.h>
+
+BSELFTEST_GLOBALS();
+
+#if __LONG_MAX__ == 0x7ffffffff
+#define LONG_MIN_STR		"-2147483648"
+#define LONG_MIN_PLUS_1_STR	"-2147483647"
+#define LONG_MAX_STR		 "2147483647"
+#define LONG_MAX_PLUS_1_STR	 "2147483648"
+#else
+#define LONG_MIN_STR		"-9223372036854775808"
+#define LONG_MIN_PLUS_1_STR	"-9223372036854775807"
+#define LONG_MAX_STR		 "9223372036854775807"
+#define LONG_MAX_PLUS_1_STR	 "9223372036854775808"
+#endif
+
+static void __assert_eq(const char *expr, bool result, const char *func, int line)
+{
+	int ret;
+
+	total_tests++;
+
+	ret = run_command(expr);
+	if ((result && ret != 0) || (!result && ret != 1)) {
+		failed_tests++;
+		printf("%s:%d: %s: assertion failure, ret=%d\n", func, line, expr, ret);
+	}
+}
+
+#define assert_eq(expr, result) __assert_eq(expr, result, __func__, __LINE__)
+
+static void test_test_command(void)
+{
+	assert_eq("[  0 -eq  0 ]", true);
+	assert_eq("[  0 -eq  1 ]", false);
+	assert_eq("[ -1 -le  1 ]", true);
+	assert_eq("[ -1 -ge -5 ]", true);
+
+	assert_eq("[ " LONG_MIN_STR " -lt " LONG_MAX_STR " ]", true);
+	assert_eq("[ " LONG_MIN_STR " -gt " LONG_MAX_STR " ]", false);
+
+	assert_eq("[ " LONG_MIN_STR " -eq " LONG_MIN_STR " ]", true);
+	assert_eq("[ " LONG_MAX_STR " -eq " LONG_MAX_STR " ]", true);
+	assert_eq("[ " LONG_MIN_STR " -ne " LONG_MIN_STR " ]", false);
+	assert_eq("[ " LONG_MAX_STR " -ne " LONG_MAX_STR " ]", false);
+
+	assert_eq("[ " LONG_MIN_PLUS_1_STR " -eq " LONG_MIN_PLUS_1_STR " ]", true);
+	assert_eq("[ " LONG_MAX_PLUS_1_STR " -eq " LONG_MAX_PLUS_1_STR " ]", true);
+	assert_eq("[ " LONG_MIN_PLUS_1_STR " -ne " LONG_MIN_PLUS_1_STR " ]", false);
+	assert_eq("[ " LONG_MAX_PLUS_1_STR " -ne " LONG_MAX_PLUS_1_STR " ]", false);
+
+	assert_eq("[ " LONG_MIN_PLUS_1_STR " -gt " LONG_MIN_STR " ]", true);
+	assert_eq("[ " LONG_MAX_PLUS_1_STR " -gt " LONG_MAX_STR " ]", true);
+	assert_eq("[ " LONG_MIN_PLUS_1_STR " -lt " LONG_MIN_STR " ]", false);
+	assert_eq("[ " LONG_MAX_PLUS_1_STR " -lt " LONG_MAX_STR " ]", false);
+	assert_eq("[ " LONG_MIN_STR " -le " LONG_MIN_PLUS_1_STR " ]", true);
+	assert_eq("[ " LONG_MAX_STR " -le " LONG_MAX_PLUS_1_STR " ]", true);
+}
+bselftest(core, test_test_command);
-- 
2.39.2




More information about the barebox mailing list