Blender V5.3
namespace_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, Namespace)
14{
15 {
16 string input = R"(
17struct C {};
18void fn(C b) {}
19namespace B {
20struct D {};
21void fn(D b) {}
22void fn2()
23{
24 fn(C{});
25}
26}
27)";
28 auto [output, _, error] = process_test_string(input);
29 EXPECT_EQ(error, "Call to function is ambiguous. Specify namespace to remove ambiguity.");
30 }
31 {
32 string input = R"(
33namespace A {
34int func(int a) { return 0; }
35int func2(int a)
36{
37 int func = func();
38 return func;
39}
40}
41)";
42 string expect = R"(
43
44int A_func(int a) { return 0; }
45int A_func2(int a)
46{
47 int func = A_func();
48 return func;
49}
50
51)";
52 auto [output, _, error] = process_test_string(input);
53 EXPECT_EQ(output, expect);
54 EXPECT_EQ(error, "");
55 }
56 {
57 string input = R"(
58namespace A {
59struct S {};
60int func(int a)
61{
62 S s;
63 return B::func(int a);
64}
65int func2(int a)
66{
67 T s;
68 s.S;
69 s.func;
70 return func(int a);
71}
72}
73)";
74 string expect = R"(
75
76struct A_S { int _pad;};
77#line 3
78 A_S A_S_ctor_() {A_S r;r._pad=0;return r;}
79int A_func(int a)
80{
81 A_S s;
82 return B_func(int a);
83}
84int A_func2(int a)
85{
86 T s;
87 s.S;
88 s.func;
89 return A_func(int a);
90}
91
92)";
93 auto [output, _, error] = process_test_string(input);
94 EXPECT_EQ(output, expect);
95 EXPECT_EQ(error, "");
96 }
97 {
98 string input = R"(
99namespace A::B {
100int func(int a)
101{
102 return a;
103}
104int func2(int a)
105{
106 return func(int a);
107}
108}
109)";
110 string expect = R"(
111
112int A_B_func(int a)
113{
114 return a;
115}
116int A_B_func2(int a)
117{
118 return A_B_func(int a);
119}
120
121)";
122 auto [output, _, error] = process_test_string(input);
123 EXPECT_EQ(output, expect);
124 EXPECT_EQ(error, "");
125 }
126 {
127 string input = R"(
128namespace A {
129void a() {}
130namespace B {
131void b() { a(); }
132}
133void f() { B::b(); }
134}
135)";
136 string expect = R"(
137
138void A_a() {}
139
140void A_B_b() { A_a(); }
141
142void A_f() { A_B_b(); }
143
144)";
145 auto [output, _, error] = process_test_string(input);
146 EXPECT_EQ(output, expect);
147 EXPECT_EQ(error, "");
148 }
149 {
150 string input = R"(
151namespace A {
152int test(int a) {}
153int func(int a)
154{
155 using B::test;
156 return test(a);
157}
158}
159)";
160 string expect = R"(
161
162int A_test(int a) {}
163int A_func(int a)
164{
165
166 return B_test(a);
167}
168
169)";
170 auto [output, _, error] = process_test_string(input);
171 EXPECT_EQ(output, expect);
172 EXPECT_EQ(error, "");
173 }
174 {
175 string input = R"(
176int func(int a)
177{
178 using B = A::S;
179 B b;
180 using C = A::F;
181 C f = A::B();
182 f = B();
183 B d;
184}
185)";
186 string expect = R"(
187int func(int a)
188{
189
190 A_S b;
191
192 A_F f = A_B();
193 f = B();
194 A_S d;
195}
196)";
197 auto [output, _, error] = process_test_string(input);
198 EXPECT_EQ(output, expect);
199 EXPECT_EQ(error, "");
200 }
201 {
202 string input = R"(
203namespace A::B {
204void func() {}
205struct S {};
206}
207namespace A::B {
208using A::B::func;
209using S = A::B::S;
210void test() {
211 S s;
212 func();
213}
214}
215)";
216 string expect = R"(
217
218void A_B_func() {}
219struct A_B_S { int _pad;};
220#line 4
221 A_B_S A_B_S_ctor_() {A_B_S r;r._pad=0;return r;}
222#line 9
223void A_B_test() {
224 A_B_S s;
225 A_B_func();
226}
227
228)";
229 auto [output, _, error] = process_test_string(input);
230 EXPECT_EQ(output, expect);
231 EXPECT_EQ(error, "");
232 }
233 {
234 string input = R"(
235using B = A::T;
236)";
237 auto [output, _, error] = process_test_string(input);
238 EXPECT_EQ(error, "The `using` keyword is not allowed in global scope.");
239 }
240 {
241 string input = R"(
242namespace A {
243using namespace B;
244}
245)";
246 auto [output, _, error] = process_test_string(input);
247 EXPECT_EQ(error,
248 "Unsupported `using namespace`. "
249 "Add individual `using` directives for each needed symbol.");
250 }
251 {
252 string input = R"(
253namespace A {
254using B::func;
255}
256)";
257 auto [output, _, error] = process_test_string(input);
258 EXPECT_EQ(error,
259 "The `using` keyword is only allowed in namespace scope to make visible symbols "
260 "from the same namespace declared in another scope, potentially from another "
261 "file.");
262 }
263 {
264 string input = R"(
265namespace A {
266using C = B::func;
267}
268)";
269 auto [output, _, error] = process_test_string(input);
270 EXPECT_EQ(error,
271 "The `using` keyword is only allowed in namespace scope to make visible symbols "
272 "from the same namespace declared in another scope, potentially from another "
273 "file.");
274 }
275 {
276 /* Template on the same line as function signature inside a namespace.
277 * Template instantiation with other functions. */
278 string input = R"(
279namespace NS {
280template<typename T> T read(T a)
281{
282 return a;
283}
284template float read<float>(float);
285float write(float a){ return a; }
286}
287)";
288
289 string expect = R"(
290
291float NS_read(float a)
292{
293 return a;
294}
295
296float NS_write(float a){ return a; }
297
298)";
299 auto [output, _, error] = process_test_string(input);
300 EXPECT_EQ(output, expect);
301 EXPECT_EQ(error, "");
302 }
303 {
304 /* Struct with member function inside namespace. */
305 string input = R"(
306namespace NS {
307struct S {
308 static S static_method(S s) {
309 return S(0);
310 }
311 S other_method(int s) {
312 this->some_method();
313 return S(0);
314 }
315};
316} // End of namespace
317)";
318
319 string expect = R"(
320
321struct NS_S {
322#line 11
323int _pad;};
324
325
326#ifndef GPU_METAL
327NS_S NS_S_ctor_();
328NS_S NS_S_static_method(NS_S s);
329NS_S _other_method(_ref(NS_S ,this_), int s);
330#endif
331#line 3
332 NS_S NS_S_ctor_() {NS_S r;r._pad=0;return r;}
333 NS_S NS_S_static_method(NS_S s) {
334 return NS_S(0);
335 }
336 NS_S _other_method(_ref(NS_S ,this_), int s) {
337 _some_method(this_);
338 return NS_S(0);
339 }
340
341
342)";
343 auto [output, _, error] = process_test_string(input);
344 EXPECT_EQ(output, expect);
345 EXPECT_EQ(error, "");
346 }
347 {
348 /* Template specialization inside namespace. */
349 string input = R"(
350namespace NS {
351template<> Type a<Type>() {}
352}
353)";
354
355 string expect = R"(
356
357 Type NS_aTType() {}
358
359)";
360 auto [output, _, error] = process_test_string(input);
361 EXPECT_EQ(output, expect);
362 EXPECT_EQ(error, "");
363 }
364 {
365 /* Half namespace specified identifiers and methods. */
366 string input = R"(
367namespace NS {
368struct B {
369 int i;
370 static int D() { return B::C().R(); }
371 static int E() { return C().R(); }
372 static B C() { return B(0); }
373 int R() { return R(); }
374};
375B fn() { return B::C(); }
376}
377)";
378
379 string expect = R"(
380
381struct NS_B {
382 int i;
383#line 9
384};
385
386#ifndef GPU_METAL
387NS_B NS_B_ctor_();
388int NS_B_D();
389int NS_B_E();
390NS_B NS_B_C();
391int _R(_ref(NS_B ,this_));
392#endif
393#line 3
394 NS_B NS_B_ctor_() {NS_B r;r.i=0;return r;}
395
396 int NS_B_D() { return _R(NS_B_C()); }
397 int NS_B_E() { return _R(NS_B_C()); }
398 NS_B NS_B_C() { return NS_B(0); }
399 int _R(_ref(NS_B ,this_)) { return _R(this_); }
400
401NS_B NS_fn() { return NS_B_C(); }
402
403)";
404 auto [output, _, error] = process_test_string(input);
405 EXPECT_EQ(output, expect);
406 EXPECT_EQ(error, "");
407 }
408}
409
410TEST(shader_tool, Using)
411{
412 {
413 string input = R"(
414struct T {};
415
416namespace A { struct T {}; }
417
418namespace B {
419
420void b(T f) {}
421
422using T = A::T;
423
424void c(T f) {}
425}
426)";
427 auto [output, _, error] = process_test_string(input, Language::BSL);
428 EXPECT_EQ(error, "");
429 }
430}
431
432} // 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)
@ Using
Definition types.hh:121