Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright Contributors to the OpenVDB Project | ||
2 | // SPDX-License-Identifier: MPL-2.0 | ||
3 | |||
4 | #include "util.h" | ||
5 | #include "../util.h" | ||
6 | |||
7 | #include <openvdb_ax/compiler/CompilerOptions.h> | ||
8 | #include <openvdb_ax/compiler/Logger.h> | ||
9 | #include <openvdb_ax/codegen/FunctionRegistry.h> | ||
10 | #include <openvdb_ax/codegen/Functions.h> | ||
11 | #include <openvdb_ax/codegen/ComputeGenerator.h> | ||
12 | #include <openvdb_ax/ast/AST.h> | ||
13 | |||
14 | #include <cppunit/extensions/HelperMacros.h> | ||
15 | |||
16 | static const std::vector<std::string> tests { | ||
17 | // codegen errors | ||
18 | |||
19 | /// implicit narrow | ||
20 | "int a; vec2i b; a=b;", | ||
21 | "int a; vec3i b; a=b;", | ||
22 | "int a; vec4i b; a=b;", | ||
23 | "float a; vec2f b; a=b;", | ||
24 | "float a; vec3f b; a=b;", | ||
25 | "float a; vec4f b; a=b;", | ||
26 | "float a; mat3f b; a=b;", | ||
27 | "float a; mat4f b; a=b;", | ||
28 | "double a; vec2d b; a=b;", | ||
29 | "double a; vec3d b; a=b;", | ||
30 | "double a; vec4d b; a=b;", | ||
31 | "double a; mat3f b; a=b;", | ||
32 | "double a; mat4f b; a=b;", | ||
33 | /// unsupported bin ops | ||
34 | "float a; a << 1;", | ||
35 | "float a; a >> 1;", | ||
36 | "float a; a | 1;", | ||
37 | "float a; a ^ 1;", | ||
38 | "float a; a & 1;", | ||
39 | "double a; a << 1;", | ||
40 | "double a; a >> 1;", | ||
41 | "double a; a | 1;", | ||
42 | "double a; a ^ 1;", | ||
43 | "double a; a & 1;", | ||
44 | "mat3d a,b; a % b;", | ||
45 | "mat3d a,b; a & b;", | ||
46 | "mat3d a,b; a && b;", | ||
47 | "mat3d a,b; a << b;", | ||
48 | "mat3d a,b; a >> b;", | ||
49 | "mat3d a,b; a ^ b;", | ||
50 | "mat3d a,b; a || b;", | ||
51 | "mat3f a,b; a % b;", | ||
52 | "mat3f a,b; a & b;", | ||
53 | "mat3f a,b; a && b;", | ||
54 | "mat3f a,b; a << b;", | ||
55 | "mat3f a,b; a >> b;", | ||
56 | "mat3f a,b; a ^ b;", | ||
57 | "mat3f a,b; a || b;", | ||
58 | "mat4d a,b; a % b;", | ||
59 | "mat4d a,b; a & b;", | ||
60 | "mat4d a,b; a && b;", | ||
61 | "mat4d a,b; a << b;", | ||
62 | "mat4d a,b; a >> b;", | ||
63 | "mat4d a,b; a ^ b;", | ||
64 | "mat4d a,b; a || b;", | ||
65 | "string a,b; a & b;", | ||
66 | "string a,b; a && b;", | ||
67 | "string a,b; a - b;", | ||
68 | "string a,b; a << b;", | ||
69 | "string a,b; a >> b;", | ||
70 | "string a,b; a ^ b;", | ||
71 | "string a,b; a | b;", | ||
72 | "string a,b; a || b;", | ||
73 | "string a,b; a * b;", | ||
74 | "string a,b; a - b;", | ||
75 | "string a,b; a / b;", | ||
76 | "vec2d a,b; a & b;", | ||
77 | "vec2d a,b; a && b;", | ||
78 | "vec2d a,b; a << b;", | ||
79 | "vec2d a,b; a >> b;", | ||
80 | "vec2d a,b; a ^ b;", | ||
81 | "vec2d a,b; a | b;", | ||
82 | "vec2d a,b; a || b;", | ||
83 | "vec2f a,b; a & b;", | ||
84 | "vec2f a,b; a && b;", | ||
85 | "vec2f a,b; a << b;", | ||
86 | "vec2f a,b; a >> b;", | ||
87 | "vec2f a,b; a ^ b;", | ||
88 | "vec2f a,b; a | b;", | ||
89 | "vec2f a,b; a || b;", | ||
90 | "vec2i a,b; a & b;", | ||
91 | "vec2i a,b; a && b;", | ||
92 | "vec2i a,b; a << b;", | ||
93 | "vec2i a,b; a >> b;", | ||
94 | "vec2i a,b; a ^ b;", | ||
95 | "vec2i a,b; a | b;", | ||
96 | "vec2i a,b; a || b;", | ||
97 | "vec3d a,b; a & b;", | ||
98 | "vec3d a,b; a && b;", | ||
99 | "vec3d a,b; a << b;", | ||
100 | "vec3d a,b; a >> b;", | ||
101 | "vec3d a,b; a ^ b;", | ||
102 | "vec3d a,b; a | b;", | ||
103 | "vec3d a,b; a || b;", | ||
104 | "vec3f a,b; a & b;", | ||
105 | "vec3f a,b; a && b;", | ||
106 | "vec3f a,b; a << b;", | ||
107 | "vec3f a,b; a >> b;", | ||
108 | "vec3f a,b; a ^ b;", | ||
109 | "vec3f a,b; a | b;", | ||
110 | "vec3f a,b; a || b;", | ||
111 | "vec3i a,b; a & b;", | ||
112 | "vec3i a,b; a && b;", | ||
113 | "vec3i a,b; a << b;", | ||
114 | "vec3i a,b; a >> b;", | ||
115 | "vec3i a,b; a ^ b;", | ||
116 | "vec3i a,b; a | b;", | ||
117 | "vec3i a,b; a || b;", | ||
118 | "vec4d a,b; a & b;", | ||
119 | "vec4d a,b; a && b;", | ||
120 | "vec4d a,b; a << b;", | ||
121 | "vec4d a,b; a >> b;", | ||
122 | "vec4d a,b; a ^ b;", | ||
123 | "vec4d a,b; a ^ b;", | ||
124 | "vec4d a,b; a || b;", | ||
125 | "vec4f a,b; a & b;", | ||
126 | "vec4f a,b; a && b;", | ||
127 | "vec4f a,b; a << b;", | ||
128 | "vec4f a,b; a >> b;", | ||
129 | "vec4f a,b; a ^ b;", | ||
130 | "vec4f a,b; a | b;", | ||
131 | "vec4f a,b; a || b;", | ||
132 | "vec4i a,b; a & b;", | ||
133 | "vec4i a,b; a && b;", | ||
134 | "vec4i a,b; a << b;", | ||
135 | "vec4i a,b; a >> b;", | ||
136 | "vec4i a,b; a ^ b;", | ||
137 | "vec4i a,b; a | b;", | ||
138 | "vec4i a,b; a || b;", | ||
139 | /// invalid unary ops | ||
140 | "vec2f a; !a;", | ||
141 | "vec2d a; !a;", | ||
142 | "vec3d a; !a;", | ||
143 | "vec3f a; !a;", | ||
144 | "vec4f a; !a;", | ||
145 | "vec4d a; !a;", | ||
146 | "mat3f a; !a;", | ||
147 | "mat3d a; !a;", | ||
148 | "mat3f a; !a;", | ||
149 | "mat4d a; !a;", | ||
150 | "vec2f a; ~a;", | ||
151 | "vec2d a; ~a;", | ||
152 | "vec3d a; ~a;", | ||
153 | "vec3f a; ~a;", | ||
154 | "vec4f a; ~a;", | ||
155 | "vec4d a; ~a;", | ||
156 | "mat3f a; ~a;", | ||
157 | "mat3d a; ~a;", | ||
158 | "mat3f a; ~a;", | ||
159 | "mat4d a; ~a;", | ||
160 | /// missing function | ||
161 | "nonexistent();", | ||
162 | /// non/re declared | ||
163 | "a;", | ||
164 | "int a; int a;", | ||
165 | "{ int a; int a; }", | ||
166 | "int a, a;", | ||
167 | "a ? b : c;", | ||
168 | "a ? true : false;", | ||
169 | "true ? a : c;", | ||
170 | "true ? a : false;", | ||
171 | "true ? true : c;", | ||
172 | "a && b;", | ||
173 | "a && true;", | ||
174 | "true && b;", | ||
175 | /// invalid crement | ||
176 | "string a; ++a;", | ||
177 | "vec2f a; ++a;", | ||
178 | "vec3f a; ++a;", | ||
179 | "vec4f a; ++a;", | ||
180 | "mat3f a; ++a;", | ||
181 | "mat4f a; ++a;", | ||
182 | /// array size assignments | ||
183 | "vec2f a; vec3f b; a=b;", | ||
184 | "vec3f a; vec2f b; a=b;", | ||
185 | "vec4f a; vec3f b; a=b;", | ||
186 | "vec2d a; vec3d b; a=b;", | ||
187 | "vec3d a; vec2d b; a=b;", | ||
188 | "vec4d a; vec3d b; a=b;", | ||
189 | "vec2i a; vec3i b; a=b;", | ||
190 | "vec3i a; vec2i b; a=b;", | ||
191 | "vec4i a; vec3i b; a=b;", | ||
192 | "mat4f a; mat3f b; a=b;", | ||
193 | "mat4d a; mat3d b; a=b;", | ||
194 | "vec2f a = {1,2,3};", | ||
195 | "vec3f a = {1,2};", | ||
196 | "vec4f a = {1,2};", | ||
197 | "mat3f a = {1,2};", | ||
198 | "mat4f a = {1,2};", | ||
199 | /// string assignments | ||
200 | "string a = 1;", | ||
201 | "int a; string b; b=a;", | ||
202 | "float a; string b; b=a;", | ||
203 | "double a; string b; b=a;", | ||
204 | "vec3f a; string b; b=a;", | ||
205 | "mat3f a; string b; b=a;", | ||
206 | /// array index | ||
207 | "int a; a[0];", | ||
208 | "vec3f a; string b; a[b];", | ||
209 | "vec3f a; vec3f b; a[b];", | ||
210 | "vec3f a; mat3f b; a[b];", | ||
211 | "vec3f a; a[1,1];", | ||
212 | "mat3f a; vec3f b; a[b,1];", | ||
213 | "mat3f a; vec3f b; a[1,b];", | ||
214 | /// array packs | ||
215 | "{1, {2,3}};", | ||
216 | "{{1,2}, 3};", | ||
217 | "{1, 2, \"a\"};", | ||
218 | "vec2f a; {1, 2, a};", | ||
219 | "vec3f a; {1, 2, a};", | ||
220 | "mat3f a; {1, 2, a};", | ||
221 | "mat4f a; {1, 2, a};", | ||
222 | "string a; {1, 2, a};", | ||
223 | /// unsupported implicit casts/ops | ||
224 | "vec2f a; vec3f b; a*b;", | ||
225 | "vec3f a; vec4f b; a*b;", | ||
226 | "vec3f a; vec2f b; a*b;", | ||
227 | "vec2i a; vec3f b; a*b;", | ||
228 | "mat3f a; mat4f b; a*b;", | ||
229 | "string a; mat4f b; a*b;", | ||
230 | "int a; string b; a*b;", | ||
231 | "{0,0,0,0,0,0,0,0,0} * 0;" | ||
232 | "0 * {0,0,0,0,0,0,0,0,0};" | ||
233 | "{0,0,0,0,0,0,0,0,0} + 0;" | ||
234 | "{0,0,0,0,0,0,0,0,0} - 0;" | ||
235 | "{0,0,0,0,0,0,0,0,0} / 0;" | ||
236 | "0 * {.0f,0,0,0,0,0,0,0,0};" | ||
237 | "{.0f,0,0,0,0,0,0,0,0} * 0;" | ||
238 | "{0.0,0,0,0,0,0,0,0,0} * 0;" | ||
239 | "{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} * 0;" | ||
240 | "0 * {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};" | ||
241 | "{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} + 0;" | ||
242 | "{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} - 0;" | ||
243 | "{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} / 0;" | ||
244 | "0 * {.0f,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};" | ||
245 | "{.0f,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} * 0;" | ||
246 | "{0.0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} * 0;" | ||
247 | "~0.0f;", | ||
248 | "vec3f a; ~a;" | ||
249 | /// loops | ||
250 | "break;", | ||
251 | "continue;", | ||
252 | // ternary | ||
253 | "int a = true ? print(1) : print(2);", | ||
254 | "true ? print(1) : 1;", | ||
255 | "mat4d a; a ? 0 : 1;", | ||
256 | "mat4f a; a ? 0 : 1;", | ||
257 | "string a; a ? 0 : 1;", | ||
258 | "vec2d a; a ? 0 : 1;", | ||
259 | "vec2f a; a ? 0 : 1;", | ||
260 | "vec2i a; a ? 0 : 1;", | ||
261 | "vec3d a; a ? 0 : 1;", | ||
262 | "vec3f a; a ? 0 : 1;", | ||
263 | "vec3i a; a ? 0 : 1;", | ||
264 | "vec4d a; a ? 0 : 1;", | ||
265 | "vec4f a; a ? 0 : 1;", | ||
266 | "vec4i a; a ? 0 : 1;", | ||
267 | // "int a, b; (a ? b : 2) = 1;", | ||
268 | "true ? {1,2} : {1,2,3};", | ||
269 | "true ? \"foo\" : 1;", | ||
270 | "true ? 1.0f : \"foo\";", | ||
271 | "{1,1} && 1 ? true : false;", | ||
272 | "{1,1} ? true : false;", | ||
273 | "{1,1} && 1 ? \"foo\" : false;", | ||
274 | "\"foo\" ? true : false;", | ||
275 | "true ? {1,1} && 1: {1,1};", | ||
276 | "true ? {1,1} : {1,1} && 1;", | ||
277 | "string a; true ? a : 1;", | ||
278 | "string a; true ? 1.0f : a;", | ||
279 | // conditional | ||
280 | "mat4d a; if (a) 1;", | ||
281 | "mat4f a; if (a) 1;", | ||
282 | "string a; if (a) 1;", | ||
283 | "vec2d a; if (a) 1;", | ||
284 | "vec2f a; if (a) 1;", | ||
285 | "vec2i a; if (a) 1;", | ||
286 | "vec3d a; if (a) 1;", | ||
287 | "vec3f a; if (a) 1;", | ||
288 | "vec3i a; if (a) 1;", | ||
289 | "vec4d a; if (a) 1;", | ||
290 | "vec4f a; if (a) 1;", | ||
291 | "vec4i a; if (a) 1;", | ||
292 | "if ({1,1} && 1) 1;", | ||
293 | "if (true) {1,1} && 1;", | ||
294 | // loops | ||
295 | "mat4d a; for (;a;) 1;", | ||
296 | "mat4f a; for (;a;) 1;", | ||
297 | "string a; for (;a;) 1;", | ||
298 | "vec2d a; for (;a;) 1;", | ||
299 | "vec2f a; for (;a;) 1;", | ||
300 | "vec2i a; for (;a;) 1;", | ||
301 | "vec3d a; for (;a;) 1;", | ||
302 | "vec3f a; for (;a;) 1;", | ||
303 | "vec3i a; for (;a;) 1;", | ||
304 | "vec4d a; for (;a;) 1;", | ||
305 | "vec4f a; for (;a;) 1;", | ||
306 | "vec4i a; for (;a;) 1;", | ||
307 | "mat4d a; while (a) 1;", | ||
308 | "mat4f a; while (a) 1;", | ||
309 | "string a; while (a) 1;", | ||
310 | "vec2d a; while (a) 1;", | ||
311 | "vec2f a; while (a) 1;", | ||
312 | "vec2i a; while (a) 1;", | ||
313 | "vec3d a; while (a) 1;", | ||
314 | "vec3f a; while (a) 1;", | ||
315 | "vec3i a; while (a) 1;", | ||
316 | "vec4d a; while (a) 1;", | ||
317 | "vec4f a; while (a) 1;", | ||
318 | "vec4i a; while (a) 1;", | ||
319 | "mat4d a; do { 1; } while(a);", | ||
320 | "mat4f a; do { 1; } while(a);", | ||
321 | "string a; do { 1; } while(a);", | ||
322 | "vec2d a; do { 1; } while(a);", | ||
323 | "vec2f a; do { 1; } while(a);", | ||
324 | "vec2i a; do { 1; } while(a);", | ||
325 | "vec3d a; do { 1; } while(a);", | ||
326 | "vec3f a; do { 1; } while(a);", | ||
327 | "vec3i a; do { 1; } while(a);", | ||
328 | "vec4d a; do { 1; } while(a);", | ||
329 | "vec4f a; do { 1; } while(a);", | ||
330 | "vec4i a; do { 1; } while(a);", | ||
331 | // comma | ||
332 | "vec2i v; v++, 1;", | ||
333 | "vec2i v; 1, v++;" | ||
334 | }; | ||
335 | |||
336 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | class TestComputeGeneratorFailures : public CppUnit::TestCase |
337 | { | ||
338 | public: | ||
339 | |||
340 |
3/6✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
|
4 | CPPUNIT_TEST_SUITE(TestComputeGeneratorFailures); |
341 |
5/10✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 1 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
|
6 | CPPUNIT_TEST(testFailures); |
342 |
4/8✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
|
4 | CPPUNIT_TEST_SUITE_END(); |
343 | |||
344 | void testFailures(); | ||
345 | }; | ||
346 | |||
347 | CPPUNIT_TEST_SUITE_REGISTRATION(TestComputeGeneratorFailures); | ||
348 | |||
349 | |||
350 | void | ||
351 | 1 | TestComputeGeneratorFailures::testFailures() | |
352 | { | ||
353 | 1 | openvdb::ax::FunctionOptions opts; | |
354 | openvdb::ax::codegen::FunctionRegistry::UniquePtr reg = | ||
355 | 2 | openvdb::ax::codegen::createDefaultRegistry(&opts); | |
356 | |||
357 | // create logger that suppresses all messages, but still logs number of errors/warnings | ||
358 |
2/6✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
3 | openvdb::ax::Logger logger([](const std::string&) {}); |
359 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | logger.setMaxErrors(1); |
360 | |||
361 |
2/2✓ Branch 0 taken 281 times.
✓ Branch 1 taken 1 times.
|
282 | for (const auto& code : tests) { |
362 | const openvdb::ax::ast::Tree::ConstPtr ast = | ||
363 |
1/2✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
|
281 | openvdb::ax::ast::parse(code.c_str(), logger); |
364 |
10/20✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 281 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 281 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 281 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 281 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 281 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 281 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 281 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 281 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 281 times.
✗ Branch 29 not taken.
|
843 | CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Unable to parse", code), ast.get()); |
365 |
6/12✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 281 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 281 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 281 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 281 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 281 times.
✗ Branch 17 not taken.
|
281 | CPPUNIT_ASSERT(!logger.hasError()); |
366 | |||
367 |
3/6✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 281 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 281 times.
✗ Branch 8 not taken.
|
562 | unittest_util::LLVMState state; |
368 |
1/2✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
|
562 | openvdb::ax::codegen::codegen_internal::ComputeGenerator gen(state.module(), opts, *reg, logger); |
369 |
1/2✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
|
281 | gen.generate(*ast); |
370 | |||
371 |
10/20✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 281 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 281 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 281 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 281 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 281 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 281 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 281 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 281 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 281 times.
✗ Branch 29 not taken.
|
843 | CPPUNIT_ASSERT_MESSAGE(ERROR_MSG("Expected Compiler Error", code), logger.hasError()); |
372 |
1/2✓ Branch 1 taken 281 times.
✗ Branch 2 not taken.
|
281 | logger.clear(); |
373 | } | ||
374 | 1 | } | |
375 | |||
376 |