Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(383)

Side by Side Diff: tests/SkSLErrorTest.cpp

Issue 1984363002: initial checkin of SkSL compiler (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: cleanups Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/sksl/sksl.flex ('K') | « src/sksl/spirv.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
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
8 #include "SkSLCompiler.h"
9
10 #include "Test.h"
11
12 DEF_TEST(SkSLUndefinedSymbol, r) {
13 SkSL::Compiler compiler;
dogben 2016/06/20 18:26:26 nit: Given that these tests are very similar, putt
ethannicholas 2016/06/20 20:17:10 Crap, I had meant to go do that. Fixed.
14 std::stringstream out;
15 compiler.errorText();
dogben 2016/06/20 18:26:26 unused?
ethannicholas 2016/06/20 20:17:10 Once upon a time, calling errorText() returned the
16 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
17 "void main() { x = vec2(1); }",
18 out);
19 REPORTER_ASSERT(r, !result);
20 REPORTER_ASSERT(r, compiler.errorText() ==
21 std::string("error: 1: unknown identifier 'x'\n1 error\n") );
dogben 2016/06/20 18:26:26 nit: == can compare std::string and char*, so "std
22 }
23
24 DEF_TEST(SkSLUndefinedFunction, r) {
25 SkSL::Compiler compiler;
26 std::stringstream out;
27 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
28 "void main() { int x = foo(1); }",
29 out);
30 REPORTER_ASSERT(r, !result);
31 REPORTER_ASSERT(r, compiler.errorText() ==
32 std::string("error: 1: unknown identifier 'foo'\n1 error\ n"));
33 }
34
35 DEF_TEST(SkSLGenericParameterMismatch, r) {
36 SkSL::Compiler compiler;
37 std::stringstream out;
38 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
39 "void main() { float x = sin(1, 2); }",
40 out);
41 REPORTER_ASSERT(r, !result);
42 REPORTER_ASSERT(r, compiler.errorText() ==
43 std::string("error: 1: no match for sin(int, int)\n1 erro r\n"));
44 }
45
46 DEF_TEST(SkSLParameterCountMismatch, r) {
47 SkSL::Compiler compiler;
48 std::stringstream out;
49 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
50 "float foo(float x) { return x * x; }"
51 "void main() { float x = foo(1, 2); }",
52 out);
53 REPORTER_ASSERT(r, !result);
54 REPORTER_ASSERT(r, compiler.errorText() ==
55 std::string("error: 1: call to 'foo' expected 1 parameter , but found 2\n"
dogben 2016/06/20 18:26:26 nit: maybe add \n to the code to test the line num
56 "1 error\n"));
57 }
58
59 DEF_TEST(SkSLParameterMismatch, r) {
60 SkSL::Compiler compiler;
61 std::stringstream out;
62 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
63 "float foo(float x) { return x * x; }"
64 "void main() { float x = foo(true); }",
65 out);
66 REPORTER_ASSERT(r, !result);
67 REPORTER_ASSERT(r, compiler.errorText() ==
68 std::string("error: 1: expected 'float', but found 'bool' \n1 error\n"));
69 }
70
71 DEF_TEST(SkSLIfTypeMismatch, r) {
72 SkSL::Compiler compiler;
73 std::stringstream out;
74 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
75 "void main() { if (3) { } }",
76 out);
77 REPORTER_ASSERT(r, !result);
78 REPORTER_ASSERT(r, compiler.errorText() ==
79 std::string("error: 1: expected 'bool', but found 'int'\n 1 error\n"));
80 }
81
82 DEF_TEST(SkSLDoTypeMismatch, r) {
83 SkSL::Compiler compiler;
84 std::stringstream out;
85 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
86 "void main() { do { } while (vec2(1)); }",
87 out);
88 REPORTER_ASSERT(r, !result);
89 REPORTER_ASSERT(r, compiler.errorText() ==
90 std::string("error: 1: expected 'bool', but found 'vec2'\ n1 error\n"));
91 }
92
93 DEF_TEST(SkSLWhileTypeMismatch, r) {
94 SkSL::Compiler compiler;
95 std::stringstream out;
96 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
97 "void main() { while (vec3(1)) { } }",
98 out);
99 REPORTER_ASSERT(r, !result);
100 REPORTER_ASSERT(r, compiler.errorText() ==
101 std::string("error: 1: expected 'bool', but found 'vec3'\ n1 error\n"));
102 }
103
104 DEF_TEST(SkSLForTypeMismatch, r) {
105 SkSL::Compiler compiler;
106 std::stringstream out;
107 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
108 "void main() { for (int x = 0; x; x++) { } }" ,
109 out);
110 REPORTER_ASSERT(r, !result);
111 REPORTER_ASSERT(r, compiler.errorText() ==
112 std::string("error: 1: expected 'bool', but found 'int'\n 1 error\n"));
113 }
114
115 DEF_TEST(SkSLConstructorTypeMismatch, r) {
116 SkSL::Compiler compiler;
117 std::stringstream out;
118 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
119 "void main() { vec2 x = vec2(1.0, false); }",
120 out);
121 REPORTER_ASSERT(r, !result);
122 REPORTER_ASSERT(r, compiler.errorText() ==
123 std::string("error: 1: expected 'float', but found 'bool' \n1 error\n"));
124 }
125
126 DEF_TEST(SkSLConstructorParameterCount, r) {
127 SkSL::Compiler compiler;
128 std::stringstream out;
129 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
130 "void main() { vec3 x = vec3(1.0, 2.0); }",
131 out);
132 REPORTER_ASSERT(r, !result);
133 REPORTER_ASSERT(r, compiler.errorText() ==
134 std::string("error: 1: invalid parameters to 'vec3' const ructor (expected 3 "
135 "scalars, but found 2)\n1 error\n"));
136
137 result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
138 "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4 .0); }",
139 out);
140 REPORTER_ASSERT(r, result);
dogben 2016/06/20 18:26:26 There is no error when constructing vec3 with 4 va
ethannicholas 2016/06/20 20:17:10 Stupid, huh? For whatever reason, it's supposed to
141 }
142
143 DEF_TEST(SkSLSwizzleScalar, r) {
144 SkSL::Compiler compiler;
145 std::stringstream out;
146 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
147 "void main() { float x = 1; float y = x.y; }" ,
148 out);
149 REPORTER_ASSERT(r, !result);
150 REPORTER_ASSERT(r, compiler.errorText() ==
151 std::string("error: 1: cannot swizzle value of type 'floa t'\n1 error\n"));
152 }
153
154 DEF_TEST(SkSLSwizzleMatrix, r) {
155 SkSL::Compiler compiler;
156 std::stringstream out;
157 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
158 "void main() { mat2 x = mat2(1); float y = x. y; }",
159 out);
160 REPORTER_ASSERT(r, !result);
161 REPORTER_ASSERT(r, compiler.errorText() ==
162 std::string("error: 1: cannot swizzle value of type 'mat2 '\n1 error\n"));
163 }
164
165 DEF_TEST(SkSLSwizzleOutOfBounds, r) {
166 SkSL::Compiler compiler;
167 std::stringstream out;
168 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
169 "void main() { vec3 test = vec2(1).xyz; }",
170 out);
171 REPORTER_ASSERT(r, !result);
172 REPORTER_ASSERT(r, compiler.errorText() ==
173 std::string("error: 1: invalid swizzle component 'z'\n1 e rror\n"));
174 }
175
176 DEF_TEST(SkSLSwizzleTooManyComponents, r) {
177 SkSL::Compiler compiler;
178 std::stringstream out;
179 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
180 "void main() { vec4 test = vec2(1).xxxxx; }",
181 out);
182 REPORTER_ASSERT(r, !result);
183 REPORTER_ASSERT(r, compiler.errorText() ==
184 std::string("error: 1: too many components in swizzle mas k 'xxxxx'\n"
185 "1 error\n"));
186 }
187 DEF_TEST(SkSLMissingSemicolon, r) {
188 SkSL::Compiler compiler;
189 std::stringstream out;
190 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
191 "void main() { float x = 1 float y = 2; }",
192 out);
193 REPORTER_ASSERT(r, !result);
194 REPORTER_ASSERT(r, compiler.errorText() ==
195 std::string("error: 1: expected ';', but found 'float'\n1 error\n"));
196 }
197
198 DEF_TEST(SkSLAssignmentTypeMismatch, r) {
199 SkSL::Compiler compiler;
200 std::stringstream out;
201 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
202 "void main() { int x = 1.0; }",
203 out);
204 REPORTER_ASSERT(r, !result);
205 REPORTER_ASSERT(r, compiler.errorText() ==
206 std::string("error: 1: expected 'int', but found 'float'\ n1 error\n"));
207 }
208
209 DEF_TEST(SkSLReturnFromVoid, r) {
210 SkSL::Compiler compiler;
211 std::stringstream out;
212 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
213 "void main() { return true; }",
214 out);
215 REPORTER_ASSERT(r, !result);
216 REPORTER_ASSERT(r, compiler.errorText() ==
217 std::string("error: 1: may not return a value from a void function\n1 error\n"));
218 }
219
220 DEF_TEST(SkSLReturnMissingValue, r) {
221 SkSL::Compiler compiler;
222 std::stringstream out;
223 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
224 "int foo() { return; } void main() { }",
225 out);
226 REPORTER_ASSERT(r, !result);
227 REPORTER_ASSERT(r, compiler.errorText() ==
228 std::string("error: 1: expected function to return 'int'\ n1 error\n"));
229 }
230
231 DEF_TEST(SkSLReturnTypeMismatch, r) {
232 SkSL::Compiler compiler;
233 std::stringstream out;
234 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
235 "int foo() { return 1.0; } void main() { }",
236 out);
237 REPORTER_ASSERT(r, !result);
238 REPORTER_ASSERT(r, compiler.errorText() ==
239 std::string("error: 1: expected 'int', but found 'float'\ n1 error\n"));
240 }
241
242 DEF_TEST(SkSLDuplicateFunction, r) {
243 SkSL::Compiler compiler;
244 std::stringstream out;
245 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
246 "void main() { } void main() { }",
247 out);
248 REPORTER_ASSERT(r, !result);
249 REPORTER_ASSERT(r, compiler.errorText() ==
250 std::string("error: 1: duplicate definition of void main( )\n1 error\n"));
251 }
252
253 DEF_TEST(SkSLDifferentReturnType, r) {
254 SkSL::Compiler compiler;
255 std::stringstream out;
256 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
257 "int main() { } void main() { }",
258 out);
259 REPORTER_ASSERT(r, !result);
260 REPORTER_ASSERT(r, compiler.errorText() ==
261 std::string("error: 1: functions 'void main()' and 'int m ain()' differ only "
262 "in return type\n1 error\n"));
263 }
264
265 DEF_TEST(SkSLDuplicateSymbol, r) {
266 SkSL::Compiler compiler;
267 std::stringstream out;
268 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
269 "int main; void main() { }",
270 out);
271 REPORTER_ASSERT(r, !result);
272 REPORTER_ASSERT(r, compiler.errorText() ==
273 std::string("error: 1: symbol 'main' was already defined\ n1 error\n"));
274
275 result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
276 "int x; int x; void main() { }",
277 out);
278 REPORTER_ASSERT(r, !result);
279 REPORTER_ASSERT(r, compiler.errorText() ==
280 std::string("error: 1: symbol 'x' was already defined\n1 error\n"));
281
282 result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
283 "int x; void main() { int x; }",
284 out);
285 REPORTER_ASSERT(r, result);
286 }
287
288 DEF_TEST(SkSLBinaryTypeMismatch, r) {
289 SkSL::Compiler compiler;
290 std::stringstream out;
291 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
292 "void main() { float x = 3 * true; }",
293 out);
294 REPORTER_ASSERT(r, !result);
295 REPORTER_ASSERT(r, compiler.errorText() ==
296 std::string("error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n"
297 "1 error\n"));
298 }
299
300 DEF_TEST(SkSLCallNonFunction, r) {
301 SkSL::Compiler compiler;
302 std::stringstream out;
303 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
304 "void main() { float x = 3; x(); }",
305 out);
306 REPORTER_ASSERT(r, !result);
307 REPORTER_ASSERT(r, compiler.errorText() ==
308 std::string("error: 1: 'x' is not a function\n1 error\n") );
309 }
310
311 DEF_TEST(SkSLInvalidUnary, r) {
312 SkSL::Compiler compiler;
313 std::stringstream out;
314 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
315 "void main() { mat4 x = mat4(1); x++; }",
316 out);
317 REPORTER_ASSERT(r, !result);
318 REPORTER_ASSERT(r, compiler.errorText() ==
319 std::string("error: 1: '++' cannot operate on 'mat4'\n1 e rror\n"));
320 }
321
322 DEF_TEST(SkSLInvalidAssignment, r) {
323 SkSL::Compiler compiler;
324 std::stringstream out;
325 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind,
326 "void main() { 1 = 2; }",
327 out);
328 REPORTER_ASSERT(r, !result);
329 REPORTER_ASSERT(r, compiler.errorText() ==
330 std::string("error: 1: cannot assign to '1'\n1 error\n")) ;
331 }
OLDNEW
« src/sksl/sksl.flex ('K') | « src/sksl/spirv.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698