[U-Boot] [PATCH 2/2] unit-test: add lots of tests for the Hush 'test' command
Stephen Warren
swarren at wwwdotorg.org
Mon Feb 3 21:24:24 CET 2014
From: Stephen Warren <swarren at nvidia.com>
I recently re-wrote cmd_test() to add new features. Add a bunch of unit-
tests to make sure I didn't break anything.
Suggested-by: Simon Glass <sjg at chromium.org>
Signed-off-by: Stephen Warren <swarren at nvidia.com>
---
This patch depends on my 12-entry patch series that implements "test -e",
since it tests that new feature.
---
test/command_ut.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 89 insertions(+)
diff --git a/test/command_ut.c b/test/command_ut.c
index 98f7625dda12..620a297d45ac 100644
--- a/test/command_ut.c
+++ b/test/command_ut.c
@@ -58,6 +58,95 @@ static int do_ut_cmd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
"setenv list ${list}3", strlen("setenv list 1"), 0);
assert(!strcmp("1", getenv("list")));
+#ifdef CONFIG_SYS_HUSH_PARSER
+ /* Test the 'test' command */
+
+#define HUSH_TEST(name, expr, expected_result) \
+ run_command("if test " expr " ; then " \
+ "setenv " #name "_" #expected_result " y; else " \
+ "setenv " #name "_" #expected_result " n; fi", 0); \
+ assert(!strcmp(#expected_result, getenv(#name "_" #expected_result)));
+
+ /* Basic operators */
+ HUSH_TEST(streq, "aaa = aaa", y);
+ HUSH_TEST(streq, "aaa = bbb", n);
+
+ HUSH_TEST(strneq, "aaa != bbb", y);
+ HUSH_TEST(strneq, "aaa != aaa", n);
+
+ HUSH_TEST(strlt, "aaa < bbb", y);
+ HUSH_TEST(strlt, "bbb < aaa", n);
+
+ HUSH_TEST(strgt, "bbb > aaa", y);
+ HUSH_TEST(strgt, "aaa > bbb", n);
+
+ HUSH_TEST(eq, "123 -eq 123", y);
+ HUSH_TEST(eq, "123 -eq 456", n);
+
+ HUSH_TEST(ne, "123 -ne 456", y);
+ HUSH_TEST(ne, "123 -ne 123", n);
+
+ HUSH_TEST(lt, "123 -lt 456", y);
+ HUSH_TEST(lt_eq, "123 -lt 123", n);
+ HUSH_TEST(lt, "456 -lt 123", n);
+
+ HUSH_TEST(le, "123 -le 456", y);
+ HUSH_TEST(le_eq, "123 -le 123", y);
+ HUSH_TEST(le, "456 -le 123", n);
+
+ HUSH_TEST(gt, "456 -gt 123", y);
+ HUSH_TEST(gt_eq, "123 -gt 123", n);
+ HUSH_TEST(gt, "123 -gt 456", n);
+
+ HUSH_TEST(ge, "456 -ge 123", y);
+ HUSH_TEST(ge_eq, "123 -ge 123", y);
+ HUSH_TEST(ge, "123 -ge 456", n);
+
+ HUSH_TEST(z, "-z \"\"", y);
+ HUSH_TEST(z, "-z \"aaa\"", n);
+
+ HUSH_TEST(n, "-n \"aaa\"", y);
+ HUSH_TEST(n, "-n \"\"", n);
+
+ /* Inversion of simple tests */
+ HUSH_TEST(streq_inv, "! aaa = aaa", n);
+ HUSH_TEST(streq_inv, "! aaa = bbb", y);
+
+ HUSH_TEST(streq_inv_inv, "! ! aaa = aaa", y);
+ HUSH_TEST(streq_inv_inv, "! ! aaa = bbb", n);
+
+ /* Binary operators */
+ HUSH_TEST(or_0_0, "aaa != aaa -o bbb != bbb", n);
+ HUSH_TEST(or_0_1, "aaa != aaa -o bbb = bbb", y);
+ HUSH_TEST(or_1_0, "aaa = aaa -o bbb != bbb", y);
+ HUSH_TEST(or_1_1, "aaa = aaa -o bbb = bbb", y);
+
+ HUSH_TEST(and_0_0, "aaa != aaa -a bbb != bbb", n);
+ HUSH_TEST(and_0_1, "aaa != aaa -a bbb = bbb", n);
+ HUSH_TEST(and_1_0, "aaa = aaa -a bbb != bbb", n);
+ HUSH_TEST(and_1_1, "aaa = aaa -a bbb = bbb", y);
+
+ /* Inversion within binary operators */
+ HUSH_TEST(or_0_0_inv, "! aaa != aaa -o ! bbb != bbb", y);
+ HUSH_TEST(or_0_1_inv, "! aaa != aaa -o ! bbb = bbb", y);
+ HUSH_TEST(or_1_0_inv, "! aaa = aaa -o ! bbb != bbb", y);
+ HUSH_TEST(or_1_1_inv, "! aaa = aaa -o ! bbb = bbb", n);
+
+ HUSH_TEST(or_0_0_inv_inv, "! ! aaa != aaa -o ! ! bbb != bbb", n);
+ HUSH_TEST(or_0_1_inv_inv, "! ! aaa != aaa -o ! ! bbb = bbb", y);
+ HUSH_TEST(or_1_0_inv_inv, "! ! aaa = aaa -o ! ! bbb != bbb", y);
+ HUSH_TEST(or_1_1_inv_inv, "! ! aaa = aaa -o ! ! bbb = bbb", y);
+
+#ifdef CONFIG_SANDBOX
+ /*
+ * File existence
+ * This assume U-Boot sandbox is run from the U-Boot build directory
+ */
+ HUSH_TEST(e, "-e host - u-boot", y);
+ HUSH_TEST(e, "-e host - creating_this_file_breaks_uboot_unit_test", n);
+#endif
+#endif
+
printf("%s: Everything went swimmingly\n", __func__);
return 0;
}
--
1.8.1.5
More information about the U-Boot
mailing list