xref: /openbmc/linux/mm/damon/dbgfs-test.h (revision b8b350af)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * DAMON Debugfs Interface Unit Tests
4  *
5  * Author: SeongJae Park <sjpark@amazon.de>
6  */
7 
8 #ifdef CONFIG_DAMON_DBGFS_KUNIT_TEST
9 
10 #ifndef _DAMON_DBGFS_TEST_H
11 #define _DAMON_DBGFS_TEST_H
12 
13 #include <kunit/test.h>
14 
15 static void damon_dbgfs_test_str_to_target_ids(struct kunit *test)
16 {
17 	char *question;
18 	unsigned long *answers;
19 	unsigned long expected[] = {12, 35, 46};
20 	ssize_t nr_integers = 0, i;
21 
22 	question = "123";
23 	answers = str_to_target_ids(question, strnlen(question, 128),
24 			&nr_integers);
25 	KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
26 	KUNIT_EXPECT_EQ(test, 123ul, answers[0]);
27 	kfree(answers);
28 
29 	question = "123abc";
30 	answers = str_to_target_ids(question, strnlen(question, 128),
31 			&nr_integers);
32 	KUNIT_EXPECT_EQ(test, (ssize_t)1, nr_integers);
33 	KUNIT_EXPECT_EQ(test, 123ul, answers[0]);
34 	kfree(answers);
35 
36 	question = "a123";
37 	answers = str_to_target_ids(question, strnlen(question, 128),
38 			&nr_integers);
39 	KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
40 	kfree(answers);
41 
42 	question = "12 35";
43 	answers = str_to_target_ids(question, strnlen(question, 128),
44 			&nr_integers);
45 	KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
46 	for (i = 0; i < nr_integers; i++)
47 		KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
48 	kfree(answers);
49 
50 	question = "12 35 46";
51 	answers = str_to_target_ids(question, strnlen(question, 128),
52 			&nr_integers);
53 	KUNIT_EXPECT_EQ(test, (ssize_t)3, nr_integers);
54 	for (i = 0; i < nr_integers; i++)
55 		KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
56 	kfree(answers);
57 
58 	question = "12 35 abc 46";
59 	answers = str_to_target_ids(question, strnlen(question, 128),
60 			&nr_integers);
61 	KUNIT_EXPECT_EQ(test, (ssize_t)2, nr_integers);
62 	for (i = 0; i < 2; i++)
63 		KUNIT_EXPECT_EQ(test, expected[i], answers[i]);
64 	kfree(answers);
65 
66 	question = "";
67 	answers = str_to_target_ids(question, strnlen(question, 128),
68 			&nr_integers);
69 	KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
70 	kfree(answers);
71 
72 	question = "\n";
73 	answers = str_to_target_ids(question, strnlen(question, 128),
74 			&nr_integers);
75 	KUNIT_EXPECT_EQ(test, (ssize_t)0, nr_integers);
76 	kfree(answers);
77 }
78 
79 static void damon_dbgfs_test_set_targets(struct kunit *test)
80 {
81 	struct damon_ctx *ctx = dbgfs_new_ctx();
82 	unsigned long ids[] = {1, 2, 3};
83 	char buf[64];
84 
85 	/* Make DAMON consider target id as plain number */
86 	ctx->primitive.target_valid = NULL;
87 	ctx->primitive.cleanup = NULL;
88 
89 	damon_set_targets(ctx, ids, 3);
90 	sprint_target_ids(ctx, buf, 64);
91 	KUNIT_EXPECT_STREQ(test, (char *)buf, "1 2 3\n");
92 
93 	damon_set_targets(ctx, NULL, 0);
94 	sprint_target_ids(ctx, buf, 64);
95 	KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
96 
97 	damon_set_targets(ctx, (unsigned long []){1, 2}, 2);
98 	sprint_target_ids(ctx, buf, 64);
99 	KUNIT_EXPECT_STREQ(test, (char *)buf, "1 2\n");
100 
101 	damon_set_targets(ctx, (unsigned long []){2}, 1);
102 	sprint_target_ids(ctx, buf, 64);
103 	KUNIT_EXPECT_STREQ(test, (char *)buf, "2\n");
104 
105 	damon_set_targets(ctx, NULL, 0);
106 	sprint_target_ids(ctx, buf, 64);
107 	KUNIT_EXPECT_STREQ(test, (char *)buf, "\n");
108 
109 	dbgfs_destroy_ctx(ctx);
110 }
111 
112 static struct kunit_case damon_test_cases[] = {
113 	KUNIT_CASE(damon_dbgfs_test_str_to_target_ids),
114 	KUNIT_CASE(damon_dbgfs_test_set_targets),
115 	{},
116 };
117 
118 static struct kunit_suite damon_test_suite = {
119 	.name = "damon-dbgfs",
120 	.test_cases = damon_test_cases,
121 };
122 kunit_test_suite(damon_test_suite);
123 
124 #endif /* _DAMON_TEST_H */
125 
126 #endif	/* CONFIG_DAMON_KUNIT_TEST */
127