unit-test: add lots of tests for the Hush 'test' command
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@chromium.org>
Signed-off-by: Stephen Warren <swarren@nvidia.com>
diff --git a/test/command_ut.c b/test/command_ut.c
index 98f7625..620a297 100644
--- a/test/command_ut.c
+++ b/test/command_ut.c
@@ -58,6 +58,95 @@
"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;
}