| 1 | |
| 2 | #include "tests.h"
|
| 3 | #include <linux/compiler.h>
|
| 4 | #include <subcmd/help.h>
|
| 5 |
|
| 6 | static int test__load_cmdnames(struct test_suite *test __maybe_unused,
|
| 7 | int subtest __maybe_unused)
|
| 8 | {
|
| 9 | struct cmdnames cmds = {};
|
| 10 |
|
| 11 | add_cmdname(&cmds, "aaa", 3);
|
| 12 | add_cmdname(&cmds, "foo", 3);
|
| 13 | add_cmdname(&cmds, "xyz", 3);
|
| 14 |
|
| 15 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds, "aaa") == 1);
|
| 16 | TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds, "bar") == 0);
|
| 17 | TEST_ASSERT_VAL("case sensitive", is_in_cmdlist(&cmds, "XYZ") == 0);
|
| 18 |
|
| 19 | clean_cmdnames(&cmds);
|
| 20 | return TEST_OK;
|
| 21 | }
|
| 22 |
|
| 23 | static int test__uniq_cmdnames(struct test_suite *test __maybe_unused,
|
| 24 | int subtest __maybe_unused)
|
| 25 | {
|
| 26 | struct cmdnames cmds = {};
|
| 27 |
|
| 28 |
|
| 29 | add_cmdname(&cmds, "aaa", 3);
|
| 30 | add_cmdname(&cmds, "aaa", 3);
|
| 31 | add_cmdname(&cmds, "bbb", 3);
|
| 32 |
|
| 33 | TEST_ASSERT_VAL("invalid original size", cmds.cnt == 3);
|
| 34 |
|
| 35 | uniq(&cmds);
|
| 36 | TEST_ASSERT_VAL("invalid final size", cmds.cnt == 2);
|
| 37 |
|
| 38 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds, "aaa") == 1);
|
| 39 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds, "bbb") == 1);
|
| 40 | TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds, "ccc") == 0);
|
| 41 |
|
| 42 | clean_cmdnames(&cmds);
|
| 43 | return TEST_OK;
|
| 44 | }
|
| 45 |
|
| 46 | static int test__exclude_cmdnames(struct test_suite *test __maybe_unused,
|
| 47 | int subtest __maybe_unused)
|
| 48 | {
|
| 49 | struct cmdnames cmds1 = {};
|
| 50 | struct cmdnames cmds2 = {};
|
| 51 |
|
| 52 | add_cmdname(&cmds1, "aaa", 3);
|
| 53 | add_cmdname(&cmds1, "bbb", 3);
|
| 54 | add_cmdname(&cmds1, "ccc", 3);
|
| 55 | add_cmdname(&cmds1, "ddd", 3);
|
| 56 | add_cmdname(&cmds1, "eee", 3);
|
| 57 | add_cmdname(&cmds1, "fff", 3);
|
| 58 | add_cmdname(&cmds1, "ggg", 3);
|
| 59 | add_cmdname(&cmds1, "hhh", 3);
|
| 60 | add_cmdname(&cmds1, "iii", 3);
|
| 61 | add_cmdname(&cmds1, "jjj", 3);
|
| 62 |
|
| 63 | add_cmdname(&cmds2, "bbb", 3);
|
| 64 | add_cmdname(&cmds2, "eee", 3);
|
| 65 | add_cmdname(&cmds2, "jjj", 3);
|
| 66 |
|
| 67 | TEST_ASSERT_VAL("invalid original size", cmds1.cnt == 10);
|
| 68 | TEST_ASSERT_VAL("invalid original size", cmds2.cnt == 3);
|
| 69 |
|
| 70 |
|
| 71 | exclude_cmds(&cmds1, &cmds2);
|
| 72 |
|
| 73 | TEST_ASSERT_VAL("invalid excluded size", cmds1.cnt == 7);
|
| 74 | TEST_ASSERT_VAL("invalid excluded size", cmds2.cnt == 3);
|
| 75 |
|
| 76 |
|
| 77 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "aaa") == 1);
|
| 78 | TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds1, "bbb") == 0);
|
| 79 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "ccc") == 1);
|
| 80 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "ddd") == 1);
|
| 81 | TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds1, "eee") == 0);
|
| 82 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "fff") == 1);
|
| 83 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "ggg") == 1);
|
| 84 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "hhh") == 1);
|
| 85 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "iii") == 1);
|
| 86 | TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds1, "jjj") == 0);
|
| 87 |
|
| 88 |
|
| 89 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds2, "bbb") == 1);
|
| 90 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds2, "eee") == 1);
|
| 91 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds2, "jjj") == 1);
|
| 92 |
|
| 93 | clean_cmdnames(&cmds1);
|
| 94 | clean_cmdnames(&cmds2);
|
| 95 | return TEST_OK;
|
| 96 | }
|
| 97 |
|
| 98 | static int test__exclude_cmdnames_no_overlap(struct test_suite *test __maybe_unused,
|
| 99 | int subtest __maybe_unused)
|
| 100 | {
|
| 101 | struct cmdnames cmds1 = {};
|
| 102 | struct cmdnames cmds2 = {};
|
| 103 |
|
| 104 | add_cmdname(&cmds1, "read-vdso32", 11);
|
| 105 | add_cmdname(&cmds2, "archive", 7);
|
| 106 |
|
| 107 | TEST_ASSERT_VAL("invalid original size", cmds1.cnt == 1);
|
| 108 | TEST_ASSERT_VAL("invalid original size", cmds2.cnt == 1);
|
| 109 |
|
| 110 | exclude_cmds(&cmds1, &cmds2);
|
| 111 |
|
| 112 | TEST_ASSERT_VAL("invalid excluded size", cmds1.cnt == 1);
|
| 113 | TEST_ASSERT_VAL("invalid excluded size", cmds2.cnt == 1);
|
| 114 |
|
| 115 | TEST_ASSERT_VAL("cannot find cmd", is_in_cmdlist(&cmds1, "read-vdso32") == 1);
|
| 116 | TEST_ASSERT_VAL("wrong cmd", is_in_cmdlist(&cmds1, "archive") == 0);
|
| 117 |
|
| 118 | clean_cmdnames(&cmds1);
|
| 119 | clean_cmdnames(&cmds2);
|
| 120 | return TEST_OK;
|
| 121 | }
|
| 122 |
|
| 123 | static struct test_case tests__subcmd_help[] = {
|
| 124 | TEST_CASE("Load subcmd names", load_cmdnames),
|
| 125 | TEST_CASE("Uniquify subcmd names", uniq_cmdnames),
|
| 126 | TEST_CASE("Exclude duplicate subcmd names", exclude_cmdnames),
|
| 127 | TEST_CASE("Exclude disjoint subcmd names", exclude_cmdnames_no_overlap),
|
| 128 | { .name = NULL, }
|
| 129 | };
|
| 130 |
|
| 131 | struct test_suite suite__subcmd_help = {
|
| 132 | .desc = "libsubcmd help tests",
|
| 133 | .test_cases = tests__subcmd_help,
|
| 134 | };
|