xref: /linux/drivers/gpu/drm/xe/tests/xe_args_test.c (revision 955abe0a1b41de5ba61fe4cd614ebc123084d499)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright © 2024 Intel Corporation
4  */
5 
6 #include <kunit/test.h>
7 
8 #include "xe_args.h"
9 
10 static void call_args_example(struct kunit *test)
11 {
12 #define foo	X, Y, Z, Q
13 #define bar	COUNT_ARGS(foo)
14 #define buz	CALL_ARGS(COUNT_ARGS, foo)
15 
16 	KUNIT_EXPECT_EQ(test, bar, 1);
17 	KUNIT_EXPECT_EQ(test, buz, 4);
18 
19 #undef foo
20 #undef bar
21 #undef buz
22 }
23 
24 static void drop_first_arg_example(struct kunit *test)
25 {
26 #define foo	X, Y, Z, Q
27 #define bar	CALL_ARGS(COUNT_ARGS, DROP_FIRST_ARG(foo))
28 
29 	KUNIT_EXPECT_EQ(test, bar, 3);
30 
31 #undef foo
32 #undef bar
33 }
34 
35 static void first_arg_example(struct kunit *test)
36 {
37 	int X = 1;
38 
39 #define foo	X, Y, Z, Q
40 #define bar	FIRST_ARG(foo)
41 
42 	KUNIT_EXPECT_EQ(test, bar, X);
43 	KUNIT_EXPECT_STREQ(test, __stringify(bar), "X");
44 
45 #undef foo
46 #undef bar
47 }
48 
49 static void last_arg_example(struct kunit *test)
50 {
51 	int Q = 1;
52 
53 #define foo	X, Y, Z, Q
54 #define bar	LAST_ARG(foo)
55 
56 	KUNIT_EXPECT_EQ(test, bar, Q);
57 	KUNIT_EXPECT_STREQ(test, __stringify(bar), "Q");
58 
59 #undef foo
60 #undef bar
61 }
62 
63 static void pick_arg_example(struct kunit *test)
64 {
65 	int Y = 1, Z = 2;
66 
67 #define foo	X, Y, Z, Q
68 #define bar	PICK_ARG(2, foo)
69 #define buz	PICK_ARG3(foo)
70 
71 	KUNIT_EXPECT_EQ(test, bar, Y);
72 	KUNIT_EXPECT_STREQ(test, __stringify(bar), "Y");
73 	KUNIT_EXPECT_EQ(test, buz, Z);
74 	KUNIT_EXPECT_STREQ(test, __stringify(buz), "Z");
75 
76 #undef foo
77 #undef bar
78 #undef buz
79 }
80 
81 static void sep_comma_example(struct kunit *test)
82 {
83 #define foo(f)	f(X) f(Y) f(Z) f(Q)
84 #define bar	DROP_FIRST_ARG(foo(ARGS_SEP_COMMA __stringify))
85 #define buz	CALL_ARGS(COUNT_ARGS, DROP_FIRST_ARG(foo(ARGS_SEP_COMMA)))
86 
87 	static const char * const a[] = { bar };
88 
89 	KUNIT_EXPECT_STREQ(test, a[0], "X");
90 	KUNIT_EXPECT_STREQ(test, a[1], "Y");
91 	KUNIT_EXPECT_STREQ(test, a[2], "Z");
92 	KUNIT_EXPECT_STREQ(test, a[3], "Q");
93 
94 	KUNIT_EXPECT_EQ(test, buz, 4);
95 
96 #undef foo
97 #undef bar
98 #undef buz
99 }
100 
101 #define NO_ARGS
102 #define FOO_ARGS	X, Y, Z, Q
103 #define MAX_ARGS	-1, -2, -3, -4, -5, -6, -7, -8, -9, -10, -11, -12
104 
105 static void count_args_test(struct kunit *test)
106 {
107 	int count;
108 
109 	/* COUNT_ARGS() counts to 12 */
110 
111 	count = COUNT_ARGS();
112 	KUNIT_EXPECT_EQ(test, count, 0);
113 
114 	count = COUNT_ARGS(1);
115 	KUNIT_EXPECT_EQ(test, count, 1);
116 
117 	count = COUNT_ARGS(a, b, c, d, e);
118 	KUNIT_EXPECT_EQ(test, count, 5);
119 
120 	count = COUNT_ARGS(a, b, c, d, e, f, g, h, i, j, k, l);
121 	KUNIT_EXPECT_EQ(test, count, 12);
122 
123 	/* COUNT_ARGS() does not expand params */
124 
125 	count = COUNT_ARGS(NO_ARGS);
126 	KUNIT_EXPECT_EQ(test, count, 1);
127 
128 	count = COUNT_ARGS(FOO_ARGS);
129 	KUNIT_EXPECT_EQ(test, count, 1);
130 }
131 
132 static void call_args_test(struct kunit *test)
133 {
134 	int count;
135 
136 	count = CALL_ARGS(COUNT_ARGS, NO_ARGS);
137 	KUNIT_EXPECT_EQ(test, count, 0);
138 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, NO_ARGS), 0);
139 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, FOO_ARGS), 4);
140 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, FOO_ARGS, FOO_ARGS), 8);
141 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, MAX_ARGS), 12);
142 }
143 
144 static void drop_first_arg_test(struct kunit *test)
145 {
146 	int Y = -2, Z = -3, Q = -4;
147 	int a[] = { DROP_FIRST_ARG(FOO_ARGS) };
148 
149 	KUNIT_EXPECT_EQ(test, DROP_FIRST_ARG(0, -1), -1);
150 	KUNIT_EXPECT_EQ(test, DROP_FIRST_ARG(DROP_FIRST_ARG(0, -1, -2)), -2);
151 
152 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, DROP_FIRST_ARG(FOO_ARGS)), 3);
153 	KUNIT_EXPECT_EQ(test, DROP_FIRST_ARG(DROP_FIRST_ARG(DROP_FIRST_ARG(FOO_ARGS))), -4);
154 	KUNIT_EXPECT_EQ(test, a[0], -2);
155 	KUNIT_EXPECT_EQ(test, a[1], -3);
156 	KUNIT_EXPECT_EQ(test, a[2], -4);
157 
158 #define foo	DROP_FIRST_ARG(FOO_ARGS)
159 #define bar	DROP_FIRST_ARG(DROP_FIRST_ARG(FOO_ARGS))
160 #define buz	DROP_FIRST_ARG(DROP_FIRST_ARG(DROP_FIRST_ARG(FOO_ARGS)))
161 
162 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, foo), 3);
163 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, bar), 2);
164 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, buz), 1);
165 	KUNIT_EXPECT_STREQ(test, __stringify(buz), "Q");
166 
167 #undef foo
168 #undef bar
169 #undef buz
170 }
171 
172 static void first_arg_test(struct kunit *test)
173 {
174 	int X = -1;
175 	int a[] = { FIRST_ARG(FOO_ARGS) };
176 
177 	KUNIT_EXPECT_EQ(test, FIRST_ARG(-1, -2), -1);
178 
179 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, FIRST_ARG(FOO_ARGS)), 1);
180 	KUNIT_EXPECT_EQ(test, FIRST_ARG(FOO_ARGS), -1);
181 	KUNIT_EXPECT_EQ(test, a[0], -1);
182 	KUNIT_EXPECT_STREQ(test, __stringify(FIRST_ARG(FOO_ARGS)), "X");
183 }
184 
185 static void last_arg_test(struct kunit *test)
186 {
187 	int Q = -4;
188 	int a[] = { LAST_ARG(FOO_ARGS) };
189 
190 	KUNIT_EXPECT_EQ(test, LAST_ARG(-1, -2), -2);
191 
192 	KUNIT_EXPECT_EQ(test, CALL_ARGS(COUNT_ARGS, LAST_ARG(FOO_ARGS)), 1);
193 	KUNIT_EXPECT_EQ(test, LAST_ARG(FOO_ARGS), -4);
194 	KUNIT_EXPECT_EQ(test, a[0], -4);
195 	KUNIT_EXPECT_STREQ(test, __stringify(LAST_ARG(FOO_ARGS)), "Q");
196 
197 	KUNIT_EXPECT_EQ(test, LAST_ARG(MAX_ARGS), -12);
198 	KUNIT_EXPECT_STREQ(test, __stringify(LAST_ARG(MAX_ARGS)), "-12");
199 }
200 
201 static struct kunit_case args_tests[] = {
202 	KUNIT_CASE(count_args_test),
203 	KUNIT_CASE(call_args_example),
204 	KUNIT_CASE(call_args_test),
205 	KUNIT_CASE(drop_first_arg_example),
206 	KUNIT_CASE(drop_first_arg_test),
207 	KUNIT_CASE(first_arg_example),
208 	KUNIT_CASE(first_arg_test),
209 	KUNIT_CASE(last_arg_example),
210 	KUNIT_CASE(last_arg_test),
211 	KUNIT_CASE(pick_arg_example),
212 	KUNIT_CASE(sep_comma_example),
213 	{}
214 };
215 
216 static struct kunit_suite args_test_suite = {
217 	.name = "args",
218 	.test_cases = args_tests,
219 };
220 
221 kunit_test_suite(args_test_suite);
222