mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] test: self: add some test cases for test command
Date: Wed, 13 Sep 2023 13:59:58 +0200	[thread overview]
Message-ID: <20230913115958.1858470-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20230913115958.1858470-1-a.fatoum@pengutronix.de>

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@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




  reply	other threads:[~2023-09-13 12:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-13 11:59 [PATCH 1/2] commands: test: support signed comparisons Ahmad Fatoum
2023-09-13 11:59 ` Ahmad Fatoum [this message]
2023-09-14  8:10 ` Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230913115958.1858470-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox