Blender V5.3
function_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
6
7namespace blender::gpu::tests {
8
9using namespace shader::parser;
10using namespace shader;
11using namespace std;
12
13TEST(shader_tool, StructMethods)
14{
15 {
16 string input = R"(
17class S {
18 private:
19 int member;
20 int this_member;
21
22 public:
23 static S construct()
24 {
25 S a;
26 a.member = 0;
27 a.this_member = 0;
28 return a;
29 }
30
31 int another_member;
32
33 S function(int i)
34 {
35 this->member = i;
36 this_member++;
37 return *this;
38 }
39
40 int size() const
41 {
42 return this->member;
43 }
44};
45
46void main()
48 S s = S::construct();
49 f.f();
50 f(0).f();
51 f().f();
52 l.o.t();
53 l.o(0).t();
54 l.o().t();
55 l[0].o();
56 l.o[0].t();
57 l.o().t[0];
58}
59)";
60 string expect = R"(
61struct S {
62
63 int member;
64 int this_member;
65#line 16
66 int another_member;
67#line 29
68};
69
70
71#ifndef GPU_METAL
72S S_ctor_();
73S S_construct();
74S _function(_ref(S ,this_), int i);
75int _size(const S this_);
76#endif
77#line 2
78 S S_ctor_() {S r;r.member=0;r.this_member=0;r.another_member=0;return r;}
79#line 8
80 S S_construct()
81 {
82 S a;
83 a.member = 0;
84 a.this_member = 0;
85 return a;
86 }
87#line 18
88 S _function(_ref(S ,this_), int i)
89 {
90 this_.member = i;
91 this_.this_member++;
92 return this_;
93 }
95 int _size(const S this_)
96 {
97 return this_.member;
98 }
99
100
101void main()
102{
103 S s = S_construct();
104 _f(f);
105 _f(f(0));
106 _f(f());
107 _t(l.o);
108 _t(_o(l, 0));
109 _t(_o(l));
110 _o(l[0]);
111 _t(l.o[0]);
112 _o(l).t[0];
113}
114)";
115 auto [output, _, error] = process_test_string(input);
116 EXPECT_EQ(output, expect);
117 EXPECT_EQ(error, "");
118 }
119 {
120 string input = R"(
121struct A {
122 int a;
123 uint b;
124 float fn1() { return a; }
125 float fn2() { int fn2; return fn1(); }
126 static float fn3() { int a; return a; }
127};
128)";
129 string expect = R"(
130struct A {
131 int a;
132 uint b;
133#line 8
134};
135
136#ifndef GPU_METAL
137A A_ctor_();
138float _fn1(_ref(A ,this_));
139float _fn2(_ref(A ,this_));
140float A_fn3();
141#endif
142#line 2
143 A A_ctor_() {A r;r.a=0;r.b=0u;return r;}
144
145
146 float _fn1(_ref(A ,this_)) { return this_.a; }
147 float _fn2(_ref(A ,this_)) { int fn2; return _fn1(this_); }
148 float A_fn3() { int a; return a; }
149
150)";
151 auto [output, _, error] = process_test_string(input);
152 EXPECT_EQ(output, expect);
153 EXPECT_EQ(error, "");
154 }
155 {
156 string input = R"(
157struct A {
158 int a;
159 float fn1(int a) { return a; }
160};
161)";
162 auto [output, _, error] = process_test_string(input);
163 EXPECT_EQ(error, "Class member shadowing.");
164 }
165 {
166 string input = R"(
167struct A {
168 int a;
169 float fn1() { int a; return a; }
170};
171)";
172 auto [output, _, error] = process_test_string(input);
173 EXPECT_EQ(error, "Class member shadowing.");
174 }
175 {
176 string input = R"(
177class S {
178 int xzwy() const
179 {
180 }
181};
182)";
183 string expect = R"(
184)";
185 auto [output, _, error] = process_test_string(input);
186 EXPECT_EQ(error, "Method name matching swizzles accessor are forbidden.");
187 }
188}
189
190TEST(shader_tool, DefaultArguments)
191{
192 {
193 string input = R"(
194int func(int a, int b = 0)
195{
196 return a + b;
197}
198)";
199 string expect = R"(
200int func(int a, int b )
201{
202 return a + b;
203}
204#line 2
205int func(int a)
206{
207#line 2
208 return func(a, 0);
209}
210
211
212)";
213 auto [output, _, error] = process_test_string(input);
214 EXPECT_EQ(output, expect);
215 EXPECT_EQ(error, "");
216 }
217 {
218 string input = R"(
219int func(int a = 0, const int b = 0)
220{
221 return a + b;
222}
223)";
224 string expect = R"(
225int func(int a , const int b )
226{
227 return a + b;
228}
229#line 2
230int func(int a)
231{
232#line 2
233 return func(a, 0);
234}
235#line 2
236int func()
237{
238#line 2
239 return func(0);
240}
241
242
243)";
244 auto [output, _, error] = process_test_string(input);
245 EXPECT_EQ(output, expect);
246 EXPECT_EQ(error, "");
247 }
248 {
249 string input = R"(
250int2 func(int2 a = int2(0, 0)) {
251 return a;
252}
253)";
254 string expect = R"(
255int2 func(int2 a ) {
256 return a;
257}
258#line 2
259int2 func()
260{
261#line 2
262 return func(int2(0, 0));
263}
264
265)";
266 auto [output, _, error] = process_test_string(input);
267 EXPECT_EQ(output, expect);
268 EXPECT_EQ(error, "");
269 }
270 {
271 string input = R"(
272void func(int a = 0) {
273 a;
274}
275)";
276 string expect = R"(
277void func(int a ) {
278 a;
279}
280#line 2
281void func()
282{
283#line 2
284 func(0);
285}
286
287)";
288 auto [output, _, error] = process_test_string(input);
289 EXPECT_EQ(output, expect);
290 EXPECT_EQ(error, "");
291 }
292
293 {
294 string input = R"(
295int func(int a, int b = 0)
296{
297 return a + b;
298}
299
300void f()
301{
302 func(1);
303 func(1, 1);
304}
305)";
306 string expect = R"(
307int func(int a, int b )
308{
309 return a + b;
310}
311
312void f()
313{
314 func(1, 0);
315 func(1, 1);
316}
317)";
318 auto [output, _, error] = process_test_string(input, Language::BSL);
319 EXPECT_EQ(output, expect);
320 EXPECT_EQ(error, "");
321 }
322 {
323 string input = R"(
324int func(int a = 0, const int b = 2)
325{
326 return a + b;
327}
328
329void f()
330{
331 func();
332 func(1);
333 func(1, 1);
334}
335)";
336 string expect = R"(
337int func(int a , const int b )
338{
339 return a + b;
340}
341
342void f()
343{
344 func(0, 2);
345 func(1, 2);
346 func(1, 1);
347}
348)";
349 auto [output, _, error] = process_test_string(input, Language::BSL);
350 EXPECT_EQ(output, expect);
351 EXPECT_EQ(error, "");
352 }
353}
354
355TEST(shader_tool, FunctionOverloads)
356{
357 /* Disabled until BSL 5.3 is default. */
358 if (false) {
359 string input = R"(struct A {};
360void f() {}
361void f(int a) {}
362void f(int a, float b) {}
363void f(float2 a, float b) {}
364void f(float a) {}
365void func()
366{
367 int a;
368 float b;
369 float2 c;
370 A d;
371 f();
372 f(a);
373 f(a, b);
374 f(c, b);
375 f(c + 1 * 0.5f, b);
376}
377)";
378
379 string expect = R"(struct A {int _pad;
380#line 1
381 };
382#line 1
383A A_ctor_() {A r;r._pad=0;return r;}
384void f() {
385#line 2
386 }
387void f3(int a) {
388#line 3
389 }
390void f4(int a, float b) {
391#line 4
392 }
393void f5(float2 a, float b) {
394#line 5
395 }
396void f6(float a) {
397#line 6
398 }
399void func()
400{
401 int a;
402 float b;
403 float2 c;
404 A d;
405 f();
406 f3(a);
407 f4(a, b);
408 f5(c, b);
409 f5(c + 1 * 0.5f, b);
410}
411)";
412 auto [output, _, error] = process_test_string(input, shader::Language::BSL);
413 EXPECT_EQ(output, expect);
414 EXPECT_EQ(error, "");
415 }
416}
417
418} // namespace blender::gpu::tests
#define output
#define EXPECT_EQ(result, expect)
TEST(shader_tool, ExpressionParser)
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)