1 // SPDX-License-Identifier: GPL-2.0
2 #include "tests.h"
3 #include <linux/compiler.h>
4 #include <subcmd/help.h>
5
test__load_cmdnames(struct test_suite * test __maybe_unused,int subtest __maybe_unused)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
test__uniq_cmdnames(struct test_suite * test __maybe_unused,int subtest __maybe_unused)23 static int test__uniq_cmdnames(struct test_suite *test __maybe_unused,
24 int subtest __maybe_unused)
25 {
26 struct cmdnames cmds = {};
27
28 /* uniq() assumes it's sorted */
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 /* uniquify command names (to remove second 'aaa') */
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
test__exclude_cmdnames(struct test_suite * test __maybe_unused,int subtest __maybe_unused)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 /* remove duplicate command names in cmds1 */
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 /* excluded commands should not belong to cmds1 */
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 /* they should be only in cmds2 */
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 struct test_case tests__subcmd_help[] = {
99 TEST_CASE("Load subcmd names", load_cmdnames),
100 TEST_CASE("Uniquify subcmd names", uniq_cmdnames),
101 TEST_CASE("Exclude duplicate subcmd names", exclude_cmdnames),
102 { .name = NULL, }
103 };
104
105 struct test_suite suite__subcmd_help = {
106 .desc = "libsubcmd help tests",
107 .test_cases = tests__subcmd_help,
108 };
109