Skip to content

Commit 34c4ff1

Browse files
committed
perf test: Add libsubcmd help tests
Add a set of tests for subcmd routines. Currently it fails the last one since there's a bug. It'll be fixed by the next commit. $ perf test subcmd 69: libsubcmd help tests : 69.1: Load subcmd names : Ok 69.2: Uniquify subcmd names : Ok 69.3: Exclude duplicate subcmd names : FAILED! Reviewed-by: Ian Rogers <irogers@google.com> Link: https://lore.kernel.org/r/20250701201027.1171561-2-namhyung@kernel.org Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 139ee54 commit 34c4ff1

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

tools/perf/tests/Build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ perf-test-y += symbols.o
6969
perf-test-y += util.o
7070
perf-test-y += hwmon_pmu.o
7171
perf-test-y += tool_pmu.o
72+
perf-test-y += subcmd-help.o
7273

7374
ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc))
7475
perf-test-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o

tools/perf/tests/builtin-test.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ static struct test_suite *generic_tests[] = {
139139
&suite__event_groups,
140140
&suite__symbols,
141141
&suite__util,
142+
&suite__subcmd_help,
142143
NULL,
143144
};
144145

tools/perf/tests/subcmd-help.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// SPDX-License-Identifier: GPL-2.0
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+
/* 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+
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+
};

tools/perf/tests/tests.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define TESTS_H
44

55
#include <stdbool.h>
6+
#include "util/debug.h"
67

78
enum {
89
TEST_OK = 0,
@@ -177,6 +178,7 @@ DECLARE_SUITE(sigtrap);
177178
DECLARE_SUITE(event_groups);
178179
DECLARE_SUITE(symbols);
179180
DECLARE_SUITE(util);
181+
DECLARE_SUITE(subcmd_help);
180182

181183
/*
182184
* PowerPC and S390 do not support creation of instruction breakpoints using the

0 commit comments

Comments
 (0)