Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+112
+1
tools/perf/tests/Build
··· 69 69 perf-test-y += util.o 70 70 perf-test-y += hwmon_pmu.o 71 71 perf-test-y += tool_pmu.o 72 + perf-test-y += subcmd-help.o 72 73 73 74 ifeq ($(SRCARCH),$(filter $(SRCARCH),x86 arm arm64 powerpc)) 74 75 perf-test-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
+1
tools/perf/tests/builtin-test.c
··· 139 139 &suite__event_groups, 140 140 &suite__symbols, 141 141 &suite__util, 142 + &suite__subcmd_help, 142 143 NULL, 143 144 }; 144 145
+108
tools/perf/tests/subcmd-help.c
··· 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 + };
+2
tools/perf/tests/tests.h
··· 3 3 #define TESTS_H 4 4 5 5 #include <stdbool.h> 6 + #include "util/debug.h" 6 7 7 8 enum { 8 9 TEST_OK = 0, ··· 178 177 DECLARE_SUITE(event_groups); 179 178 DECLARE_SUITE(symbols); 180 179 DECLARE_SUITE(util); 180 + DECLARE_SUITE(subcmd_help); 181 181 182 182 /* 183 183 * PowerPC and S390 do not support creation of instruction breakpoints using the