blob: c12dd39bc819fd8df51f610049832544559ffa89 [file] [log] [blame]
Ethan Nicholas95046142021-01-07 10:57:27 -05001/*
2 * Copyright 2020 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Ethan Nicholas24c17722021-03-09 13:10:59 -05008#include "include/private/SkSLIRNode.h"
Ethan Nicholasdaed2592021-03-04 14:30:25 -05009#include "include/sksl/DSL.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050010#include "src/gpu/GrDirectContextPriv.h"
11#include "src/gpu/GrGpu.h"
12#include "src/sksl/SkSLIRGenerator.h"
Ethan Nicholas95046142021-01-07 10:57:27 -050013#include "src/sksl/dsl/priv/DSLWriter.h"
14
15#include "tests/Test.h"
16
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050017#include <limits>
18
Ethan Nicholas95046142021-01-07 10:57:27 -050019using namespace SkSL::dsl;
20
Ethan Nicholas961d9442021-03-16 16:37:29 -040021/**
22 * In addition to issuing an automatic Start() and End(), disables mangling and optionally
23 * auto-declares variables during its lifetime. Variable auto-declaration simplifies testing so we
24 * don't have to sprinkle all the tests with a bunch of Declare(foo).release() calls just to avoid
25 * errors, especially given that some of the variables have options that make them an error to
26 * actually declare.
27 */
Ethan Nicholas95046142021-01-07 10:57:27 -050028class AutoDSLContext {
29public:
Ethan Nicholas961d9442021-03-16 16:37:29 -040030 AutoDSLContext(GrGpu* gpu, bool markVarsDeclared = true) {
Ethan Nicholas95046142021-01-07 10:57:27 -050031 Start(gpu->shaderCompiler());
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050032 DSLWriter::Instance().fMangle = false;
Ethan Nicholas961d9442021-03-16 16:37:29 -040033 DSLWriter::Instance().fMarkVarsDeclared = markVarsDeclared;
Ethan Nicholas95046142021-01-07 10:57:27 -050034 }
35
36 ~AutoDSLContext() {
37 End();
38 }
39};
40
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050041class ExpectError : public ErrorHandler {
42public:
43 ExpectError(skiatest::Reporter* reporter, const char* msg)
44 : fMsg(msg)
45 , fReporter(reporter) {
46 SetErrorHandler(this);
47 }
48
49 ~ExpectError() override {
John Stiles642cde22021-02-23 14:57:01 -050050 REPORTER_ASSERT(fReporter, !fMsg,
51 "Error mismatch: expected:\n%sbut no error occurred\n", fMsg);
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050052 SetErrorHandler(nullptr);
53 }
54
Ethan Nicholasb9563042021-02-25 09:45:49 -050055 void handleError(const char* msg, PositionInfo* pos) override {
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050056 REPORTER_ASSERT(fReporter, !strcmp(msg, fMsg),
57 "Error mismatch: expected:\n%sbut received:\n%s", fMsg, msg);
58 fMsg = nullptr;
59 }
60
61private:
62 const char* fMsg;
63 skiatest::Reporter* fReporter;
64};
65
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -050066static bool whitespace_insensitive_compare(const char* a, const char* b) {
67 for (;;) {
68 while (isspace(*a)) {
69 ++a;
70 }
71 while (isspace(*b)) {
72 ++b;
73 }
74 if (*a != *b) {
75 return false;
76 }
77 if (*a == 0) {
78 return true;
79 }
80 ++a;
81 ++b;
82 }
83}
84
Ethan Nicholas24c17722021-03-09 13:10:59 -050085// for use from SkSLDSLOnlyTest.cpp
86void StartDSL(const sk_gpu_test::ContextInfo ctxInfo) {
87 Start(ctxInfo.directContext()->priv().getGpu()->shaderCompiler());
88}
89
Ethan Nicholasb3d4e742021-01-08 11:42:25 -050090DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStartup, r, ctxInfo) {
Ethan Nicholas95046142021-01-07 10:57:27 -050091 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
92 Expression e1 = 1;
93 REPORTER_ASSERT(r, e1.release()->description() == "1");
94 Expression e2 = 1.0;
95 REPORTER_ASSERT(r, e2.release()->description() == "1.0");
96 Expression e3 = true;
97 REPORTER_ASSERT(r, e3.release()->description() == "true");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -040098 Var a(kInt_Type, "a");
Ethan Nicholasbffe80a2021-01-11 15:42:44 -050099 Expression e4 = a;
100 REPORTER_ASSERT(r, e4.release()->description() == "a");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500101
102 REPORTER_ASSERT(r, whitespace_insensitive_compare("", ""));
103 REPORTER_ASSERT(r, !whitespace_insensitive_compare("", "a"));
104 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a", ""));
105 REPORTER_ASSERT(r, whitespace_insensitive_compare("a", "a"));
106 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", "abc"));
107 REPORTER_ASSERT(r, whitespace_insensitive_compare("abc", " abc "));
108 REPORTER_ASSERT(r, whitespace_insensitive_compare("a b c ", "\n\n\nabc"));
109 REPORTER_ASSERT(r, !whitespace_insensitive_compare("a b c d", "\n\n\nabc"));
Ethan Nicholas95046142021-01-07 10:57:27 -0500110}
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500111
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500112static SkSL::String stringize(DSLStatement& stmt) { return stmt.release()->description(); }
113static SkSL::String stringize(DSLPossibleStatement& stmt) { return stmt.release()->description(); }
114static SkSL::String stringize(DSLExpression& expr) { return expr.release()->description(); }
115static SkSL::String stringize(DSLPossibleExpression& expr) { return expr.release()->description(); }
John Stilesb4d7b582021-02-19 09:56:31 -0500116static SkSL::String stringize(SkSL::IRNode& node) { return node.description(); }
117
118template <typename T>
119static void expect_equal(skiatest::Reporter* r, int lineNumber, T& input, const char* expected) {
120 SkSL::String actual = stringize(input);
121 if (!whitespace_insensitive_compare(expected, actual.c_str())) {
122 ERRORF(r, "(Failed on line %d)\nExpected: %s\n Actual: %s\n",
123 lineNumber, expected, actual.c_str());
124 }
125}
126
127template <typename T>
128static void expect_equal(skiatest::Reporter* r, int lineNumber, T&& dsl, const char* expected) {
129 // This overload allows temporary values to be passed to expect_equal.
130 return expect_equal(r, lineNumber, dsl, expected);
131}
132
133#define EXPECT_EQUAL(a, b) expect_equal(r, __LINE__, (a), (b))
134
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500135DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFloat, r, ctxInfo) {
136 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
137 Expression e1 = Float(std::numeric_limits<float>::max());
138 REPORTER_ASSERT(r, atof(e1.release()->description().c_str()) ==
139 std::numeric_limits<float>::max());
140
141 Expression e2 = Float(std::numeric_limits<float>::min());
142 REPORTER_ASSERT(r, atof(e2.release()->description().c_str()) ==
143 std::numeric_limits<float>::min());
144
John Stilesb4d7b582021-02-19 09:56:31 -0500145 EXPECT_EQUAL(Float2(0),
146 "float2(0.0)");
147 EXPECT_EQUAL(Float2(-0.5, 1),
148 "float2(-0.5, 1.0)");
149 EXPECT_EQUAL(Float3(0.75),
150 "float3(0.75)");
151 EXPECT_EQUAL(Float3(Float2(0, 1), -2),
John Stilesb9e4f642021-03-05 09:11:38 -0500152 "float3(0.0, 1.0, -2.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500153 EXPECT_EQUAL(Float3(0, 1, 2),
154 "float3(0.0, 1.0, 2.0)");
155 EXPECT_EQUAL(Float4(0),
156 "float4(0.0)");
157 EXPECT_EQUAL(Float4(Float2(0, 1), Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500158 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500159 EXPECT_EQUAL(Float4(0, 1, Float2(2, 3)),
John Stilesb9e4f642021-03-05 09:11:38 -0500160 "float4(0.0, 1.0, 2.0, 3.0)");
John Stilesb4d7b582021-02-19 09:56:31 -0500161 EXPECT_EQUAL(Float4(0, 1, 2, 3),
162 "float4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500163
164 {
165 ExpectError error(r, "error: floating point value is infinite\n");
166 Float(std::numeric_limits<float>::infinity()).release();
167 }
168
169 {
170 ExpectError error(r, "error: floating point value is NaN\n");
171 Float(std::numeric_limits<float>::quiet_NaN()).release();
172 }
173
174 {
175 ExpectError error(r, "error: invalid arguments to 'float2' constructor (expected 2 scalars,"
176 " but found 4)\n");
177 Float2(Float4(1)).release();
178 }
179
180 {
181 ExpectError error(r, "error: invalid arguments to 'float4' constructor (expected 4 scalars,"
182 " but found 3)\n");
183 Float4(Float3(1)).release();
184 }
185}
186
187DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLHalf, r, ctxInfo) {
188 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
189 Expression e1 = Half(std::numeric_limits<float>::max());
John Stilesb4d7b582021-02-19 09:56:31 -0500190 REPORTER_ASSERT(r,
191 atof(e1.release()->description().c_str()) == std::numeric_limits<float>::max());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500192
193 Expression e2 = Half(std::numeric_limits<float>::min());
John Stilesb4d7b582021-02-19 09:56:31 -0500194 REPORTER_ASSERT(r,
195 atof(e2.release()->description().c_str()) == std::numeric_limits<float>::min());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500196
John Stilesb9e4f642021-03-05 09:11:38 -0500197 EXPECT_EQUAL(Half2(0),
198 "half2(0.0)");
199 EXPECT_EQUAL(Half2(-0.5, 1),
200 "half2(-0.5, 1.0)");
201 EXPECT_EQUAL(Half3(0.75),
202 "half3(0.75)");
203 EXPECT_EQUAL(Half3(Half2(0, 1), -2),
204 "half3(0.0, 1.0, -2.0)");
205 EXPECT_EQUAL(Half3(0, 1, 2),
206 "half3(0.0, 1.0, 2.0)");
207 EXPECT_EQUAL(Half4(0),
208 "half4(0.0)");
209 EXPECT_EQUAL(Half4(Half2(0, 1), Half2(2, 3)),
210 "half4(0.0, 1.0, 2.0, 3.0)");
211 EXPECT_EQUAL(Half4(0, 1, Half2(2, 3)),
212 "half4(0.0, 1.0, 2.0, 3.0)");
213 EXPECT_EQUAL(Half4(0, 1, 2, 3),
214 "half4(0.0, 1.0, 2.0, 3.0)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500215
216 {
217 ExpectError error(r, "error: floating point value is infinite\n");
218 Half(std::numeric_limits<float>::infinity()).release();
219 }
220
221 {
222 ExpectError error(r, "error: floating point value is NaN\n");
223 Half(std::numeric_limits<float>::quiet_NaN()).release();
224 }
225
226 {
227 ExpectError error(r, "error: invalid arguments to 'half2' constructor (expected 2 scalars,"
228 " but found 4)\n");
229 Half2(Half4(1)).release();
230 }
231
232 {
233 ExpectError error(r, "error: invalid arguments to 'half4' constructor (expected 4 scalars,"
234 " but found 3)\n");
235 Half4(Half3(1)).release();
236 }
237}
238
239DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLInt, r, ctxInfo) {
240 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500241
John Stilesb9e4f642021-03-05 09:11:38 -0500242 EXPECT_EQUAL(Int(std::numeric_limits<int32_t>::max()),
243 "2147483647");
244 EXPECT_EQUAL(Int2(std::numeric_limits<int32_t>::min()),
245 "int2(-2147483648)");
246 EXPECT_EQUAL(Int2(0, 1),
247 "int2(0, 1)");
248 EXPECT_EQUAL(Int3(0),
249 "int3(0)");
250 EXPECT_EQUAL(Int3(Int2(0, 1), -2),
251 "int3(0, 1, -2)");
252 EXPECT_EQUAL(Int3(0, 1, 2),
253 "int3(0, 1, 2)");
254 EXPECT_EQUAL(Int4(0),
255 "int4(0)");
256 EXPECT_EQUAL(Int4(Int2(0, 1), Int2(2, 3)),
257 "int4(0, 1, 2, 3)");
258 EXPECT_EQUAL(Int4(0, 1, Int2(2, 3)),
259 "int4(0, 1, 2, 3)");
260 EXPECT_EQUAL(Int4(0, 1, 2, 3),
261 "int4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500262
263 {
264 ExpectError error(r, "error: invalid arguments to 'int2' constructor (expected 2 scalars,"
265 " but found 4)\n");
266 Int2(Int4(1)).release();
267 }
268
269 {
270 ExpectError error(r, "error: invalid arguments to 'int4' constructor (expected 4 scalars,"
271 " but found 3)\n");
272 Int4(Int3(1)).release();
273 }
274}
275
276DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShort, r, ctxInfo) {
277 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500278
John Stilesb9e4f642021-03-05 09:11:38 -0500279 EXPECT_EQUAL(Short(std::numeric_limits<int16_t>::max()),
280 "32767");
281 EXPECT_EQUAL(Short2(std::numeric_limits<int16_t>::min()),
282 "short2(-32768)");
283 EXPECT_EQUAL(Short2(0, 1),
284 "short2(0, 1)");
285 EXPECT_EQUAL(Short3(0),
286 "short3(0)");
287 EXPECT_EQUAL(Short3(Short2(0, 1), -2),
288 "short3(0, 1, -2)");
289 EXPECT_EQUAL(Short3(0, 1, 2),
290 "short3(0, 1, 2)");
291 EXPECT_EQUAL(Short4(0),
292 "short4(0)");
293 EXPECT_EQUAL(Short4(Short2(0, 1), Short2(2, 3)),
294 "short4(0, 1, 2, 3)");
295 EXPECT_EQUAL(Short4(0, 1, Short2(2, 3)),
296 "short4(0, 1, 2, 3)");
297 EXPECT_EQUAL(Short4(0, 1, 2, 3),
298 "short4(0, 1, 2, 3)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500299
300 {
301 ExpectError error(r, "error: invalid arguments to 'short2' constructor (expected 2 scalars,"
302 " but found 4)\n");
303 Short2(Short4(1)).release();
304 }
305
306 {
307 ExpectError error(r, "error: invalid arguments to 'short4' constructor (expected 4 scalars,"
308 " but found 3)\n");
309 Short4(Short3(1)).release();
310 }
311}
312
313DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBool, r, ctxInfo) {
314 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500315
John Stilesb9e4f642021-03-05 09:11:38 -0500316 EXPECT_EQUAL(Bool2(false),
317 "bool2(false)");
318 EXPECT_EQUAL(Bool2(false, true),
319 "bool2(false, true)");
320 EXPECT_EQUAL(Bool3(false),
321 "bool3(false)");
322 EXPECT_EQUAL(Bool3(Bool2(false, true), false),
323 "bool3(false, true, false)");
324 EXPECT_EQUAL(Bool3(false, true, false),
325 "bool3(false, true, false)");
326 EXPECT_EQUAL(Bool4(false),
327 "bool4(false)");
328 EXPECT_EQUAL(Bool4(Bool2(false, true), Bool2(false, true)),
329 "bool4(false, true, false, true)");
330 EXPECT_EQUAL(Bool4(false, true, Bool2(false, true)),
331 "bool4(false, true, false, true)");
332 EXPECT_EQUAL(Bool4(false, true, false, true),
333 "bool4(false, true, false, true)");
Ethan Nicholasb3d4e742021-01-08 11:42:25 -0500334
335 {
336 ExpectError error(r, "error: invalid arguments to 'bool2' constructor (expected 2 scalars,"
337 " but found 4)\n");
338 Bool2(Bool4(true)).release();
339 }
340
341 {
342 ExpectError error(r, "error: invalid arguments to 'bool4' constructor (expected 4 scalars,"
343 " but found 3)\n");
344 Bool4(Bool3(true)).release();
345 }
346}
Ethan Nicholas92969f22021-01-13 10:38:59 -0500347
348DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLPlus, r, ctxInfo) {
349 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400350 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500351
John Stiles8f440b42021-03-05 16:48:56 -0500352 EXPECT_EQUAL(a + b,
353 "(a + b)");
354 EXPECT_EQUAL(a + 1,
355 "(a + 1.0)");
356 EXPECT_EQUAL(0.5 + a + -99,
357 "((0.5 + a) + -99.0)");
358 EXPECT_EQUAL(a += b + 1,
359 "(a += (b + 1.0))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500360 EXPECT_EQUAL(+a,
361 "a");
362 EXPECT_EQUAL(+(a + b),
363 "(a + b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500364
365 {
366 ExpectError error(r, "error: type mismatch: '+' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500367 DSLExpression((Bool2(true) + a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500368 }
369
370 {
371 ExpectError error(r, "error: type mismatch: '+=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500372 DSLExpression((a += Bool2(true))).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500373 }
374
375 {
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500376 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500377 DSLExpression((1.0 += a)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500378 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500379
380 {
381 ExpectError error(r, "error: '+' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400382 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500383 DSLExpression(+c);
384 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500385}
386
387DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMinus, r, ctxInfo) {
388 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400389 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500390
John Stiles8f440b42021-03-05 16:48:56 -0500391 EXPECT_EQUAL(a - b,
392 "(a - b)");
393 EXPECT_EQUAL(a - 1,
394 "(a - 1)");
395 EXPECT_EQUAL(2 - a - b,
396 "((2 - a) - b)");
397 EXPECT_EQUAL(a -= b + 1,
398 "(a -= (b + 1))");
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500399 EXPECT_EQUAL(-a,
400 "-a");
401 EXPECT_EQUAL(-(a - b),
402 "-(a - b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500403
404 {
405 ExpectError error(r, "error: type mismatch: '-' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500406 DSLExpression(Bool2(true) - a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500407 }
408
409 {
410 ExpectError error(r, "error: type mismatch: '-=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500411 DSLExpression(a -= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500412 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500413
414 {
415 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500416 DSLExpression(1.0 -= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500417 }
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500418
419 {
420 ExpectError error(r, "error: '-' cannot operate on 'bool'\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400421 Var c(kBool_Type);
Ethan Nicholasb14b6362021-03-08 17:07:58 -0500422 DSLExpression(-c);
423 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500424}
425
426DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMultiply, r, ctxInfo) {
427 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400428 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500429
John Stiles8f440b42021-03-05 16:48:56 -0500430 EXPECT_EQUAL(a * b,
431 "(a * b)");
432 EXPECT_EQUAL(a * 2,
433 "(a * 2.0)");
434 EXPECT_EQUAL(0.5 * a * -99,
435 "((0.5 * a) * -99.0)");
436 EXPECT_EQUAL(a *= b + 1,
437 "(a *= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500438
439 {
440 ExpectError error(r, "error: type mismatch: '*' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500441 DSLExpression(Bool2(true) * a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500442 }
443
444 {
445 ExpectError error(r, "error: type mismatch: '*=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500446 DSLExpression(a *= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500447 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500448
449 {
450 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500451 DSLExpression(1.0 *= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500452 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500453}
454
455DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDivide, r, ctxInfo) {
456 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400457 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500458
John Stiles8f440b42021-03-05 16:48:56 -0500459 EXPECT_EQUAL(a / b,
460 "(a / b)");
461 EXPECT_EQUAL(a / 2,
462 "(a / 2.0)");
463 EXPECT_EQUAL(0.5 / a / -99,
464 "((0.5 / a) / -99.0)");
465 EXPECT_EQUAL(b / (a - 1),
466 "(b / (a - 1.0))");
467 EXPECT_EQUAL(a /= b + 1,
468 "(a /= (b + 1.0))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500469
470 {
471 ExpectError error(r, "error: type mismatch: '/' cannot operate on 'bool2', 'float'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500472 DSLExpression(Bool2(true) / a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500473 }
474
475 {
476 ExpectError error(r, "error: type mismatch: '/=' cannot operate on 'float', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500477 DSLExpression(a /= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500478 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500479
480 {
481 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500482 DSLExpression(1.0 /= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500483 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500484
485 {
486 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500487 DSLExpression(a /= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500488 }
489
490 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400491 Var c(kFloat2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500492 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500493 DSLExpression(c /= Float2(Float(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500494 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500495}
496
497DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLMod, r, ctxInfo) {
498 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400499 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500500 Expression e1 = a % b;
John Stilesb4d7b582021-02-19 09:56:31 -0500501 EXPECT_EQUAL(e1, "(a % b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500502
503 Expression e2 = a % 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500504 EXPECT_EQUAL(e2, "(a % 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500505
506 Expression e3 = 10 % a % -99;
John Stilesb4d7b582021-02-19 09:56:31 -0500507 EXPECT_EQUAL(e3, "((10 % a) % -99)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500508
509 Expression e4 = a %= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500510 EXPECT_EQUAL(e4, "(a %= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500511
512 {
513 ExpectError error(r, "error: type mismatch: '%' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500514 DSLExpression(Bool2(true) % a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500515 }
516
517 {
518 ExpectError error(r, "error: type mismatch: '%=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500519 DSLExpression(a %= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500520 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500521
522 {
523 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500524 DSLExpression(1 %= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500525 }
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500526
527 {
528 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500529 DSLExpression(a %= 0).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500530 }
531
532 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400533 Var c(kInt2_Type, "c");
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500534 ExpectError error(r, "error: division by zero\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500535 DSLExpression(c %= Int2(Int(0), 1)).release();
Ethan Nicholasc0f98152021-02-05 16:21:10 -0500536 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500537}
538
539DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShl, r, ctxInfo) {
540 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400541 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500542 Expression e1 = a << b;
John Stilesb4d7b582021-02-19 09:56:31 -0500543 EXPECT_EQUAL(e1, "(a << b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500544
545 Expression e2 = a << 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500546 EXPECT_EQUAL(e2, "(a << 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500547
548 Expression e3 = 1 << a << 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500549 EXPECT_EQUAL(e3, "((1 << a) << 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500550
551 Expression e4 = a <<= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500552 EXPECT_EQUAL(e4, "(a <<= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500553
554 {
555 ExpectError error(r, "error: type mismatch: '<<' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500556 DSLExpression(Bool2(true) << a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500557 }
558
559 {
560 ExpectError error(r, "error: type mismatch: '<<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500561 DSLExpression(a <<= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500562 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500563
564 {
565 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500566 DSLExpression(1 <<= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500567 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500568}
569
570DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLShr, r, ctxInfo) {
571 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400572 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500573 Expression e1 = a >> b;
John Stilesb4d7b582021-02-19 09:56:31 -0500574 EXPECT_EQUAL(e1, "(a >> b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500575
576 Expression e2 = a >> 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500577 EXPECT_EQUAL(e2, "(a >> 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500578
579 Expression e3 = 1 >> a >> 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500580 EXPECT_EQUAL(e3, "((1 >> a) >> 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500581
582 Expression e4 = a >>= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500583 EXPECT_EQUAL(e4, "(a >>= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500584
585 {
586 ExpectError error(r, "error: type mismatch: '>>' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500587 DSLExpression(Bool2(true) >> a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500588 }
589
590 {
591 ExpectError error(r, "error: type mismatch: '>>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500592 DSLExpression(a >>= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500593 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500594
595 {
596 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500597 DSLExpression(1 >>= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500598 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500599}
600
601DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseAnd, r, ctxInfo) {
602 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400603 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500604 Expression e1 = a & b;
John Stilesb4d7b582021-02-19 09:56:31 -0500605 EXPECT_EQUAL(e1, "(a & b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500606
607 Expression e2 = a & 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500608 EXPECT_EQUAL(e2, "(a & 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500609
610 Expression e3 = 1 & a & 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500611 EXPECT_EQUAL(e3, "((1 & a) & 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500612
613 Expression e4 = a &= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500614 EXPECT_EQUAL(e4, "(a &= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500615
616 {
617 ExpectError error(r, "error: type mismatch: '&' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500618 DSLExpression(Bool2(true) & a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500619 }
620
621 {
622 ExpectError error(r, "error: type mismatch: '&=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500623 DSLExpression(a &= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500624 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500625
626 {
627 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500628 DSLExpression(1 &= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500629 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500630}
631
632DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseOr, r, ctxInfo) {
633 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400634 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500635 Expression e1 = a | b;
John Stilesb4d7b582021-02-19 09:56:31 -0500636 EXPECT_EQUAL(e1, "(a | b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500637
638 Expression e2 = a | 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500639 EXPECT_EQUAL(e2, "(a | 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500640
641 Expression e3 = 1 | a | 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500642 EXPECT_EQUAL(e3, "((1 | a) | 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500643
644 Expression e4 = a |= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500645 EXPECT_EQUAL(e4, "(a |= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500646
647 {
648 ExpectError error(r, "error: type mismatch: '|' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500649 DSLExpression(Bool2(true) | a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500650 }
651
652 {
653 ExpectError error(r, "error: type mismatch: '|=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500654 DSLExpression(a |= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500655 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500656
657 {
658 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500659 DSLExpression(1 |= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500660 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500661}
662
663DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseXor, r, ctxInfo) {
664 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400665 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500666 Expression e1 = a ^ b;
John Stilesb4d7b582021-02-19 09:56:31 -0500667 EXPECT_EQUAL(e1, "(a ^ b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500668
669 Expression e2 = a ^ 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500670 EXPECT_EQUAL(e2, "(a ^ 1)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500671
672 Expression e3 = 1 ^ a ^ 2;
John Stilesb4d7b582021-02-19 09:56:31 -0500673 EXPECT_EQUAL(e3, "((1 ^ a) ^ 2)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500674
675 Expression e4 = a ^= b + 1;
John Stilesb4d7b582021-02-19 09:56:31 -0500676 EXPECT_EQUAL(e4, "(a ^= (b + 1))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500677
678 {
679 ExpectError error(r, "error: type mismatch: '^' cannot operate on 'bool2', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500680 DSLExpression(Bool2(true) ^ a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500681 }
682
683 {
684 ExpectError error(r, "error: type mismatch: '^=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500685 DSLExpression(a ^= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500686 }
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500687
688 {
689 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500690 DSLExpression(1 ^= a).release();
Ethan Nicholas67a0a8a2021-01-13 12:36:02 -0500691 }
Ethan Nicholas92969f22021-01-13 10:38:59 -0500692}
693
694DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalAnd, r, ctxInfo) {
695 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400696 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500697 Expression e1 = a && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500698 EXPECT_EQUAL(e1, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500699
700 Expression e2 = a && true && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500701 EXPECT_EQUAL(e2, "(a && b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500702
703 Expression e3 = a && false && b;
John Stilesb4d7b582021-02-19 09:56:31 -0500704 EXPECT_EQUAL(e3, "false");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500705
706 {
707 ExpectError error(r, "error: type mismatch: '&&' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500708 DSLExpression(a && 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500709 }
710}
711
712DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalOr, r, ctxInfo) {
713 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400714 Var a(kBool_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500715 Expression e1 = a || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500716 EXPECT_EQUAL(e1, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500717
718 Expression e2 = a || true || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500719 EXPECT_EQUAL(e2, "true");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500720
721 Expression e3 = a || false || b;
John Stilesb4d7b582021-02-19 09:56:31 -0500722 EXPECT_EQUAL(e3, "(a || b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500723
724 {
725 ExpectError error(r, "error: type mismatch: '||' cannot operate on 'bool', 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500726 DSLExpression(a || 5).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500727 }
728}
729
730DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLComma, r, ctxInfo) {
731 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400732 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500733 Expression e1 = (a += b, b);
John Stilesb4d7b582021-02-19 09:56:31 -0500734 EXPECT_EQUAL(e1, "((a += b) , b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500735
736 Expression e2 = (a += b, b += b, Int2(a));
John Stilesb4d7b582021-02-19 09:56:31 -0500737 EXPECT_EQUAL(e2, "(((a += b) , (b += b)) , int2(a))");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500738}
739
740DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLEqual, r, ctxInfo) {
741 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400742 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500743 Expression e1 = a == b;
John Stilesb4d7b582021-02-19 09:56:31 -0500744 EXPECT_EQUAL(e1, "(a == b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500745
746 Expression e2 = a == 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500747 EXPECT_EQUAL(e2, "(a == 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500748
749 {
750 ExpectError error(r, "error: type mismatch: '==' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500751 DSLExpression(a == Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500752 }
753}
754
755DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLNotEqual, r, ctxInfo) {
756 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400757 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500758 Expression e1 = a != b;
John Stilesb4d7b582021-02-19 09:56:31 -0500759 EXPECT_EQUAL(e1, "(a != b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500760
761 Expression e2 = a != 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500762 EXPECT_EQUAL(e2, "(a != 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500763
764 {
765 ExpectError error(r, "error: type mismatch: '!=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500766 DSLExpression(a != Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500767 }
768}
769
770DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThan, r, ctxInfo) {
771 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400772 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500773 Expression e1 = a > b;
John Stilesb4d7b582021-02-19 09:56:31 -0500774 EXPECT_EQUAL(e1, "(a > b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500775
776 Expression e2 = a > 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500777 EXPECT_EQUAL(e2, "(a > 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500778
779 {
780 ExpectError error(r, "error: type mismatch: '>' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500781 DSLExpression(a > Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500782 }
783}
784
785DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLGreaterThanOrEqual, r, ctxInfo) {
786 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400787 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500788 Expression e1 = a >= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500789 EXPECT_EQUAL(e1, "(a >= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500790
791 Expression e2 = a >= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500792 EXPECT_EQUAL(e2, "(a >= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500793
794 {
795 ExpectError error(r, "error: type mismatch: '>=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500796 DSLExpression(a >= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500797 }
798}
799
800DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThan, r, ctxInfo) {
801 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400802 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500803 Expression e1 = a < b;
John Stilesb4d7b582021-02-19 09:56:31 -0500804 EXPECT_EQUAL(e1, "(a < b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500805
806 Expression e2 = a < 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500807 EXPECT_EQUAL(e2, "(a < 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500808
809 {
810 ExpectError error(r, "error: type mismatch: '<' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500811 DSLExpression(a < Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500812 }
813}
814
815DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLessThanOrEqual, r, ctxInfo) {
816 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400817 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500818 Expression e1 = a <= b;
John Stilesb4d7b582021-02-19 09:56:31 -0500819 EXPECT_EQUAL(e1, "(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500820
821 Expression e2 = a <= 5;
John Stilesb4d7b582021-02-19 09:56:31 -0500822 EXPECT_EQUAL(e2, "(a <= 5)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500823
824 {
825 ExpectError error(r, "error: type mismatch: '<=' cannot operate on 'int', 'bool2'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500826 DSLExpression(a <= Bool2(true)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500827 }
828}
829
830DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLLogicalNot, r, ctxInfo) {
831 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400832 Var a(kInt_Type, "a"), b(kInt_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500833 Expression e1 = !(a <= b);
John Stilesb4d7b582021-02-19 09:56:31 -0500834 EXPECT_EQUAL(e1, "!(a <= b)");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500835
836 {
837 ExpectError error(r, "error: '!' cannot operate on 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500838 DSLExpression(!a).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500839 }
840}
841
842DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBitwiseNot, r, ctxInfo) {
843 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400844 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500845 Expression e1 = ~a;
John Stilesb4d7b582021-02-19 09:56:31 -0500846 EXPECT_EQUAL(e1, "~a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500847
848 {
849 ExpectError error(r, "error: '~' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500850 DSLExpression(~b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500851 }
852}
853
854DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIncrement, r, ctxInfo) {
855 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400856 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500857 Expression e1 = ++a;
John Stilesb4d7b582021-02-19 09:56:31 -0500858 EXPECT_EQUAL(e1, "++a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500859
860 Expression e2 = a++;
John Stilesb4d7b582021-02-19 09:56:31 -0500861 EXPECT_EQUAL(e2, "a++");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500862
863 {
864 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500865 DSLExpression(++b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500866 }
867
868 {
869 ExpectError error(r, "error: '++' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500870 DSLExpression(b++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500871 }
872
873 {
874 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500875 DSLExpression(++(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500876 }
877
878 {
879 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500880 DSLExpression((a + 1)++).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500881 }
882}
883
884DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDecrement, r, ctxInfo) {
885 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400886 Var a(kInt_Type, "a"), b(kBool_Type, "b");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500887 Expression e1 = --a;
John Stilesb4d7b582021-02-19 09:56:31 -0500888 EXPECT_EQUAL(e1, "--a");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500889
890 Expression e2 = a--;
John Stilesb4d7b582021-02-19 09:56:31 -0500891 EXPECT_EQUAL(e2, "a--");
Ethan Nicholas92969f22021-01-13 10:38:59 -0500892
893 {
894 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500895 DSLExpression(--b).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500896 }
897
898 {
899 ExpectError error(r, "error: '--' cannot operate on 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500900 DSLExpression(b--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500901 }
902
903 {
904 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500905 DSLExpression(--(a + 1)).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500906 }
907
908 {
909 ExpectError error(r, "error: cannot assign to this expression\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -0500910 DSLExpression((a + 1)--).release();
Ethan Nicholas92969f22021-01-13 10:38:59 -0500911 }
912}
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500913
914DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBlock, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400915 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500916 Statement x = Block();
John Stilesb4d7b582021-02-19 09:56:31 -0500917 EXPECT_EQUAL(x, "{ }");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400918 Var a(kInt_Type, "a", 1), b(kInt_Type, "b", 2);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500919 Statement y = Block(Declare(a), Declare(b), a = b);
John Stilesb4d7b582021-02-19 09:56:31 -0500920 EXPECT_EQUAL(y, "{ int a = 1; int b = 2; (a = b); }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500921}
922
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500923DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBreak, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400924 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400925 Var i(kInt_Type, "i", 0);
926 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500927 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500928 If(i > 5, Break())
929 ))
930 );
931 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -0500932 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
933 "void success() { for (int i = 0; (i < 10); ++i) { if ((i > 5)) break; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500934
935 {
936 ExpectError error(r, "error: break statement must be inside a loop or switch\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400937 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500938 Break()
939 );
940 }
941}
942
943DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLContinue, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400944 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400945 Var i(kInt_Type, "i", 0);
946 DSLFunction(kVoid_Type, "success").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500947 For(Declare(i), i < 10, ++i, Block(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500948 If(i < 5, Continue())
949 ))
950 );
951 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -0500952 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
953 "void success() { for (int i = 0; (i < 10); ++i) { if ((i < 5)) continue; } }");
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500954
955 {
956 ExpectError error(r, "error: continue statement must be inside a loop\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400957 DSLFunction(kVoid_Type, "fail").define(
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500958 Continue()
959 );
960 }
961}
962
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500963DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDeclare, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -0400964 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400965 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b", Half4(1));
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500966 Statement x = Declare(a);
John Stilesb4d7b582021-02-19 09:56:31 -0500967 EXPECT_EQUAL(x, "half4 a;");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500968 Statement y = Declare(b);
John Stilesb4d7b582021-02-19 09:56:31 -0500969 EXPECT_EQUAL(y, "half4 b = half4(1.0);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500970
971 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400972 Var c(kHalf4_Type, "c", 1);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500973 ExpectError error(r, "error: expected 'half4', but found 'int'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500974 Declare(c).release();
975 }
976
977 {
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400978 Var d(kInt_Type, "d");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -0500979 Declare(d).release();
980 ExpectError error(r, "error: variable has already been declared\n");
981 Declare(d).release();
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500982 }
983}
984
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500985DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDiscard, r, ctxInfo) {
986 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
987 Statement x = If(Sqrt(1) > 0, Discard());
John Stilesb4d7b582021-02-19 09:56:31 -0500988 EXPECT_EQUAL(x, "if ((sqrt(1.0) > 0.0)) discard;");
Ethan Nicholasdaceb792021-02-05 14:22:32 -0500989}
990
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500991DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLDo, r, ctxInfo) {
992 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
993 Statement x = Do(Block(), true);
John Stilesb4d7b582021-02-19 09:56:31 -0500994 EXPECT_EQUAL(x, "do {} while (true);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500995
Ethan Nicholasb14e6b92021-04-08 16:56:05 -0400996 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500997 Statement y = Do(Block(a++, --b), a != b);
John Stilesb4d7b582021-02-19 09:56:31 -0500998 EXPECT_EQUAL(y, "do { a++; --b; } while ((a != b));");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -0500999
1000 {
1001 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1002 Do(Block(), 7).release();
1003 }
1004}
1005
1006DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFor, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001007 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001008 Statement x = For(Statement(), Expression(), Expression(), Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001009 EXPECT_EQUAL(x, "for (;;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001010
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001011 Var i(kInt_Type, "i", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001012 Statement y = For(Declare(i), i < 10, ++i, i += 5);
John Stilesb4d7b582021-02-19 09:56:31 -05001013 EXPECT_EQUAL(y, "for (int i = 0; (i < 10); ++i) (i += 5);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001014
1015 {
1016 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
1017 For(i = 0, i + 10, ++i, i += 5).release();
1018 }
1019}
1020
Ethan Nicholase2c05042021-02-03 10:27:22 -05001021DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLFunction, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001022 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001023 Var coords(kHalf2_Type, "coords");
1024 DSLFunction(kVoid_Type, "main", coords).define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001025 sk_FragColor() = Half4(coords, 0, 1)
1026 );
1027 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
John Stilesb4d7b582021-02-19 09:56:31 -05001028 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1029 "void main(half2 coords) { (sk_FragColor = half4(coords, 0.0, 1.0)); }");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001030
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001031 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001032 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001033 Var x(kFloat_Type, "x");
1034 DSLFunction sqr(kFloat_Type, "sqr", x);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001035 sqr.define(
1036 Return(x * x)
1037 );
1038 EXPECT_EQUAL(sqr(sk_FragCoord().x()), "sqr(sk_FragCoord.x)");
1039 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1040 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0], "float sqr(float x) { return (x * x); }");
1041 }
1042
1043 {
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001044 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001045 Var x(kFloat2_Type, "x");
1046 Var y(kFloat2_Type, "y");
1047 DSLFunction dot(kFloat2_Type, "dot", x, y);
Ethan Nicholas63f75fc2021-02-23 12:05:49 -05001048 dot.define(
1049 Return(x * x + y * y)
1050 );
1051 EXPECT_EQUAL(dot(Float2(1.0f, 2.0f), Float2(3.0f, 4.0f)),
1052 "dot(float2(1.0, 2.0), float2(3.0, 4.0))");
1053 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 1);
1054 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1055 "float2 dot(float2 x, float2 y) { return ((x * x) + (y * y)); }");
1056 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001057
1058 {
1059 ExpectError error(r, "error: expected 'float', but found 'bool'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001060 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001061 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001062 Return(true)
1063 );
1064 }
1065
1066 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001067 ExpectError error(r, "error: expected function to return 'float'\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001068 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001069 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001070 Return()
1071 );
1072 }
1073
1074 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001075 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001076 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001077 Var x(kFloat_Type, "x", 0);
1078 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001079 Declare(x),
John Stilesb3dcbb12021-03-04 16:00:20 -05001080 If(x == 1, Return(x))
1081 );
1082 }
1083
1084 {
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001085 ExpectError error(r, "error: may not return a value from a void function\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001086 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001087 DSLFunction(kVoid_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001088 Return(0)
1089 );
1090 }
1091
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001092 {
John Stilesb3dcbb12021-03-04 16:00:20 -05001093 ExpectError error(r, "error: function 'broken' can exit without returning a value\n");
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001094 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001095 DSLFunction(kFloat_Type, "broken").define(
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001096 );
1097 }
Ethan Nicholas961d9442021-03-16 16:37:29 -04001098
1099 {
1100 ExpectError error(r, "error: using an already-declared variable as a function parameter\n");
1101 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001102 DSLVar p(kFloat_Type);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001103 Declare(p).release();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001104 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001105 );
1106 }
1107
1108 {
1109 ExpectError error(r, "error: variable has already been declared\n");
1110 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001111 DSLVar p(kFloat_Type);
1112 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001113 );
1114 Declare(p).release();
1115 }
1116
1117 {
1118 ExpectError error(r, "error: variables used as function parameters cannot have initial "
1119 "values\n");
1120 DSLWriter::Reset();
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001121 DSLVar p(kFloat_Type, 1);
1122 DSLFunction(kVoid_Type, "broken", p).define(
Ethan Nicholas961d9442021-03-16 16:37:29 -04001123 );
1124 }
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001125}
1126
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001127DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIf, r, ctxInfo) {
1128 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001129 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001130 Statement x = If(a > b, a -= b);
John Stilesb4d7b582021-02-19 09:56:31 -05001131 EXPECT_EQUAL(x, "if ((a > b)) (a -= b);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001132
1133 Statement y = If(a > b, a -= b, b -= a);
John Stilesb4d7b582021-02-19 09:56:31 -05001134 EXPECT_EQUAL(y, "if ((a > b)) (a -= b); else (b -= a);");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001135
1136 {
1137 ExpectError error(r, "error: expected 'bool', but found 'float'\n");
1138 If(a + b, a -= b).release();
1139 }
1140}
1141
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001142DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLReturn, r, ctxInfo) {
1143 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1144
1145 Statement x = Return();
John Stilesb4d7b582021-02-19 09:56:31 -05001146 EXPECT_EQUAL(x, "return;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001147
1148 Statement y = Return(true);
John Stilesb4d7b582021-02-19 09:56:31 -05001149 EXPECT_EQUAL(y, "return true;");
Ethan Nicholas1ff76092021-01-28 10:02:43 -05001150}
1151
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001152DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSelect, r, ctxInfo) {
1153 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001154 Var a(kInt_Type, "a");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001155 Expression x = Select(a > 0, 1, -1);
John Stilesb4d7b582021-02-19 09:56:31 -05001156 EXPECT_EQUAL(x, "((a > 0) ? 1 : -1)");
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001157
1158 {
1159 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001160 DSLExpression x = Select(a, 1, -1);
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001161 }
1162
1163 {
1164 ExpectError error(r, "error: ternary operator result mismatch: 'float2', 'float3'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001165 DSLExpression x = Select(a > 0, Float2(1), Float3(1));
Ethan Nicholasfa648a12021-02-17 12:13:20 -05001166 }
1167}
1168
Ethan Nicholascfefec02021-02-09 15:22:57 -05001169DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwitch, r, ctxInfo) {
1170 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1171
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001172 Var a(kFloat_Type, "a"), b(kInt_Type, "b");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001173
John Stilesf3a28db2021-03-10 23:00:47 -05001174 Statement x = Switch(b,
Ethan Nicholascfefec02021-02-09 15:22:57 -05001175 Case(0, a = 0, Break()),
1176 Case(1, a = 1, Continue()),
John Stilese1d1b082021-02-23 13:44:36 -05001177 Case(2, a = 2 /*Fallthrough*/),
Ethan Nicholascfefec02021-02-09 15:22:57 -05001178 Default(Discard())
1179 );
John Stilese1d1b082021-02-23 13:44:36 -05001180 EXPECT_EQUAL(x, R"(
John Stilesf3a28db2021-03-10 23:00:47 -05001181 switch (b) {
John Stilese1d1b082021-02-23 13:44:36 -05001182 case 0: (a = 0.0); break;
1183 case 1: (a = 1.0); continue;
1184 case 2: (a = 2.0);
1185 default: discard;
1186 }
1187 )");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001188
John Stiles642cde22021-02-23 14:57:01 -05001189 EXPECT_EQUAL(Switch(b),
1190 "switch (b) {}");
1191
1192 EXPECT_EQUAL(Switch(b, Default(), Case(0), Case(1)),
1193 "switch (b) { default: case 0: case 1: }");
Ethan Nicholascfefec02021-02-09 15:22:57 -05001194
1195 {
John Stilese1d1b082021-02-23 13:44:36 -05001196 ExpectError error(r, "error: duplicate case value '0'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001197 DSLStatement(Switch(0, Case(0), Case(0))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001198 }
1199
1200 {
John Stilese1d1b082021-02-23 13:44:36 -05001201 ExpectError error(r, "error: duplicate default case\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001202 DSLStatement(Switch(0, Default(a = 0), Default(a = 1))).release();
John Stilese1d1b082021-02-23 13:44:36 -05001203 }
1204
1205 {
Ethan Nicholascfefec02021-02-09 15:22:57 -05001206 ExpectError error(r, "error: case value must be a constant integer\n");
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001207 Var b(kInt_Type);
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001208 DSLStatement(Switch(0, Case(b))).release();
Ethan Nicholascfefec02021-02-09 15:22:57 -05001209 }
Ethan Nicholascfefec02021-02-09 15:22:57 -05001210}
1211
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001212DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLSwizzle, r, ctxInfo) {
1213 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001214 Var a(kFloat4_Type, "a");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001215
John Stilesf04e09c2021-03-05 13:13:14 -05001216 EXPECT_EQUAL(a.x(),
1217 "a.x");
1218 EXPECT_EQUAL(a.y(),
1219 "a.y");
1220 EXPECT_EQUAL(a.z(),
1221 "a.z");
1222 EXPECT_EQUAL(a.w(),
1223 "a.w");
1224 EXPECT_EQUAL(a.r(),
1225 "a.x");
1226 EXPECT_EQUAL(a.g(),
1227 "a.y");
1228 EXPECT_EQUAL(a.b(),
1229 "a.z");
1230 EXPECT_EQUAL(a.a(),
1231 "a.w");
1232 EXPECT_EQUAL(Swizzle(a, R),
1233 "a.x");
1234 EXPECT_EQUAL(Swizzle(a, ZERO, G),
1235 "float2(0.0, a.y)");
1236 EXPECT_EQUAL(Swizzle(a, B, G, G),
1237 "a.zyy");
1238 EXPECT_EQUAL(Swizzle(a, R, G, B, ONE),
1239 "float4(a.xyz, 1.0)");
1240 EXPECT_EQUAL(Swizzle(a, B, G, R, ONE).r(),
1241 "a.z");
Ethan Nicholas68c77d42021-01-26 14:31:29 -05001242}
1243
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001244DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLWhile, r, ctxInfo) {
1245 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1246 Statement x = While(true, Block());
John Stilesb4d7b582021-02-19 09:56:31 -05001247 EXPECT_EQUAL(x, "for (; true;) {}");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001248
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001249 Var a(kFloat_Type, "a"), b(kFloat_Type, "b");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001250 Statement y = While(a != b, Block(a++, --b));
John Stilesb4d7b582021-02-19 09:56:31 -05001251 EXPECT_EQUAL(y, "for (; (a != b);) { a++; --b; }");
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001252
1253 {
1254 ExpectError error(r, "error: expected 'bool', but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001255 DSLStatement x = While(7, Block());
Ethan Nicholasd6b6f3e2021-01-22 15:18:25 -05001256 }
1257}
Ethan Nicholas04be3392021-01-26 10:07:01 -05001258
1259DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLIndex, r, ctxInfo) {
1260 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001261 Var a(Array(kInt_Type, 5), "a"), b(kInt_Type, "b");
John Stilesb4d7b582021-02-19 09:56:31 -05001262
1263 EXPECT_EQUAL(a[0], "a[0]");
1264 EXPECT_EQUAL(a[b], "a[b]");
Ethan Nicholas04be3392021-01-26 10:07:01 -05001265
1266 {
1267 ExpectError error(r, "error: expected 'int', but found 'bool'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001268 DSLExpression x = a[true];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001269 }
1270
1271 {
1272 ExpectError error(r, "error: expected array, but found 'int'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001273 DSLExpression x = b[0];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001274 }
1275
1276 {
1277 ExpectError error(r, "error: index -1 out of range for 'int[5]'\n");
Ethan Nicholas34c7e112021-02-25 20:50:32 -05001278 DSLExpression x = a[-1];
Ethan Nicholas04be3392021-01-26 10:07:01 -05001279 }
1280}
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001281
1282DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLBuiltins, r, ctxInfo) {
1283 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu());
1284 // There is a Fract type on Mac which can conflict with our Fract builtin
1285 using SkSL::dsl::Fract;
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001286 Var a(kHalf4_Type, "a"), b(kHalf4_Type, "b"), c(kHalf4_Type, "c");
1287 Var h3(kHalf3_Type, "h3");
1288 Var b4(kBool4_Type, "b4");
John Stilesb4d7b582021-02-19 09:56:31 -05001289 EXPECT_EQUAL(Abs(a), "abs(a)");
1290 EXPECT_EQUAL(All(b4), "all(b4)");
1291 EXPECT_EQUAL(Any(b4), "any(b4)");
1292 EXPECT_EQUAL(Ceil(a), "ceil(a)");
1293 EXPECT_EQUAL(Clamp(a, 0, 1), "clamp(a, 0.0, 1.0)");
1294 EXPECT_EQUAL(Cos(a), "cos(a)");
1295 EXPECT_EQUAL(Cross(h3, h3), "cross(h3, h3)");
1296 EXPECT_EQUAL(Degrees(a), "degrees(a)");
1297 EXPECT_EQUAL(Distance(a, b), "distance(a, b)");
1298 EXPECT_EQUAL(Dot(a, b), "dot(a, b)");
1299 EXPECT_EQUAL(Equal(a, b), "equal(a, b)");
1300 EXPECT_EQUAL(Exp(a), "exp(a)");
1301 EXPECT_EQUAL(Exp2(a), "exp2(a)");
1302 EXPECT_EQUAL(Faceforward(a, b, c), "faceforward(a, b, c)");
1303 EXPECT_EQUAL(Floor(a), "floor(a)");
1304 EXPECT_EQUAL(Fract(a), "fract(a)");
1305 EXPECT_EQUAL(GreaterThan(a, b), "greaterThan(a, b)");
1306 EXPECT_EQUAL(GreaterThanEqual(a, b), "greaterThanEqual(a, b)");
1307 EXPECT_EQUAL(Inversesqrt(a), "inversesqrt(a)");
1308 EXPECT_EQUAL(LessThan(a, b), "lessThan(a, b)");
1309 EXPECT_EQUAL(LessThanEqual(a, b), "lessThanEqual(a, b)");
1310 EXPECT_EQUAL(Length(a), "length(a)");
1311 EXPECT_EQUAL(Log(a), "log(a)");
1312 EXPECT_EQUAL(Log2(a), "log2(a)");
1313 EXPECT_EQUAL(Max(a, b), "max(a, b)");
1314 EXPECT_EQUAL(Min(a, b), "min(a, b)");
1315 EXPECT_EQUAL(Mix(a, b, c), "mix(a, b, c)");
1316 EXPECT_EQUAL(Mod(a, b), "mod(a, b)");
1317 EXPECT_EQUAL(Normalize(a), "normalize(a)");
1318 EXPECT_EQUAL(NotEqual(a, b), "notEqual(a, b)");
1319 EXPECT_EQUAL(Pow(a, b), "pow(a, b)");
1320 EXPECT_EQUAL(Radians(a), "radians(a)");
1321 EXPECT_EQUAL(Reflect(a, b), "reflect(a, b)");
1322 EXPECT_EQUAL(Refract(a, b, 1), "refract(a, b, 1.0)");
1323 EXPECT_EQUAL(Saturate(a), "saturate(a)");
1324 EXPECT_EQUAL(Sign(a), "sign(a)");
1325 EXPECT_EQUAL(Sin(a), "sin(a)");
1326 EXPECT_EQUAL(Smoothstep(a, b, c), "smoothstep(a, b, c)");
1327 EXPECT_EQUAL(Sqrt(a), "sqrt(a)");
1328 EXPECT_EQUAL(Step(a, b), "step(a, b)");
1329 EXPECT_EQUAL(Tan(a), "tan(a)");
1330 EXPECT_EQUAL(Unpremul(a), "unpremul(a)");
Ethan Nicholas30e93d52021-01-26 12:00:25 -05001331
1332 // these calls all go through the normal channels, so it ought to be sufficient to prove that
1333 // one of them reports errors correctly
1334 {
1335 ExpectError error(r, "error: no match for ceil(bool)\n");
1336 Ceil(a == b).release();
1337 }
1338}
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001339
1340DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLModifiers, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001341 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001342
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001343 Var v1(kConst_Modifier, kInt_Type, "v1", 0);
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001344 Statement d1 = Declare(v1);
Ethan Nicholasbd974002021-02-22 16:20:06 -05001345 EXPECT_EQUAL(d1, "const int v1 = 0;");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001346
1347 // Most modifiers require an appropriate context to be legal. We can't yet give them that
1348 // context, so we can't as yet Declare() variables with these modifiers.
1349 // TODO: better tests when able
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001350 Var v2(kIn_Modifier, kInt_Type, "v2");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001351 REPORTER_ASSERT(r, DSLWriter::Var(v2).modifiers().fFlags == SkSL::Modifiers::kIn_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001352 DSLWriter::MarkDeclared(v2);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001353
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001354 Var v3(kOut_Modifier, kInt_Type, "v3");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001355 REPORTER_ASSERT(r, DSLWriter::Var(v3).modifiers().fFlags == SkSL::Modifiers::kOut_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001356 DSLWriter::MarkDeclared(v3);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001357
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001358 Var v4(kFlat_Modifier, kInt_Type, "v4");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001359 REPORTER_ASSERT(r, DSLWriter::Var(v4).modifiers().fFlags == SkSL::Modifiers::kFlat_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001360 DSLWriter::MarkDeclared(v4);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001361
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001362 Var v5(kNoPerspective_Modifier, kInt_Type, "v5");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001363 REPORTER_ASSERT(r, DSLWriter::Var(v5).modifiers().fFlags ==
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001364 SkSL::Modifiers::kNoPerspective_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001365 DSLWriter::MarkDeclared(v5);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001366
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001367 Var v6(kIn_Modifier | kOut_Modifier, kInt_Type, "v6");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001368 REPORTER_ASSERT(r, DSLWriter::Var(v6).modifiers().fFlags ==
1369 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001370 DSLWriter::MarkDeclared(v6);
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001371
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001372 Var v7(kInOut_Modifier, kInt_Type, "v7");
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001373 REPORTER_ASSERT(r, DSLWriter::Var(v7).modifiers().fFlags ==
1374 (SkSL::Modifiers::kIn_Flag | SkSL::Modifiers::kOut_Flag));
Ethan Nicholas961d9442021-03-16 16:37:29 -04001375 DSLWriter::MarkDeclared(v7);
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001376
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001377 Var v8(kUniform_Modifier, kInt_Type, "v8");
Ethan Nicholas11a15b12021-02-11 15:56:27 -05001378 REPORTER_ASSERT(r, DSLWriter::Var(v8).modifiers().fFlags == SkSL::Modifiers::kUniform_Flag);
Ethan Nicholas961d9442021-03-16 16:37:29 -04001379 // Uniforms do not need to be explicitly declared
Ethan Nicholasd6b26e52021-01-27 07:53:46 -05001380}
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001381
1382DEF_GPUTEST_FOR_MOCK_CONTEXT(DSLStruct, r, ctxInfo) {
Ethan Nicholas961d9442021-03-16 16:37:29 -04001383 AutoDSLContext context(ctxInfo.directContext()->priv().getGpu(), /*markVarsDeclared=*/false);
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001384
1385 DSLType simpleStruct = Struct("SimpleStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001386 Field(kFloat_Type, "x"),
1387 Field(kBool_Type, "b"),
1388 Field(Array(kFloat_Type, 3), "a")
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001389 );
1390 DSLVar result(simpleStruct, "result");
1391 DSLFunction(simpleStruct, "returnStruct").define(
1392 Declare(result),
1393 result.field("x") = 123,
1394 result.field("b") = result.field("x") > 0,
1395 result.field("a")[0] = result.field("x"),
1396 Return(result)
1397 );
1398 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 2);
John Stilesb4d7b582021-02-19 09:56:31 -05001399 EXPECT_EQUAL(*DSLWriter::ProgramElements()[0],
1400 "struct SimpleStruct { float x; bool b; float[3] a; };");
1401 EXPECT_EQUAL(*DSLWriter::ProgramElements()[1],
1402 "SimpleStruct returnStruct() { SimpleStruct result; (result.x = 123.0);"
1403 "(result.b = (result.x > 0.0)); (result.a[0] = result.x); return result; }");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001404
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001405 Struct("NestedStruct",
Ethan Nicholasb14e6b92021-04-08 16:56:05 -04001406 Field(kInt_Type, "x"),
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001407 Field(simpleStruct, "simple")
1408 );
Ethan Nicholasfe5d6922021-03-05 14:23:48 -05001409 REPORTER_ASSERT(r, DSLWriter::ProgramElements().size() == 3);
1410 EXPECT_EQUAL(*DSLWriter::ProgramElements()[2],
John Stilesb4d7b582021-02-19 09:56:31 -05001411 "struct NestedStruct { int x; SimpleStruct simple; };");
Ethan Nicholasbf79dff2021-02-11 15:18:31 -05001412}