OLD | NEW |
(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 static void test_failure(skiatest::Reporter* r, const char* src, const char* err
or) { |
| 13 SkSL::Compiler compiler; |
| 14 std::stringstream out; |
| 15 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, src, out); |
| 16 REPORTER_ASSERT(r, !result); |
| 17 REPORTER_ASSERT(r, compiler.errorText() == error); |
| 18 } |
| 19 |
| 20 static void test_success(skiatest::Reporter* r, const char* src) { |
| 21 SkSL::Compiler compiler; |
| 22 std::stringstream out; |
| 23 bool result = compiler.toSPIRV(SkSL::Program::kFragment_Kind, src, out); |
| 24 REPORTER_ASSERT(r, result); |
| 25 } |
| 26 |
| 27 DEF_TEST(SkSLUndefinedSymbol, r) { |
| 28 test_failure(r, |
| 29 "void main() { x = vec2(1); }", |
| 30 "error: 1: unknown identifier 'x'\n1 error\n"); |
| 31 } |
| 32 |
| 33 DEF_TEST(SkSLUndefinedFunction, r) { |
| 34 test_failure(r, |
| 35 "void main() { int x = foo(1); }", |
| 36 "error: 1: unknown identifier 'foo'\n1 error\n"); |
| 37 } |
| 38 |
| 39 DEF_TEST(SkSLGenericArgumentMismatch, r) { |
| 40 test_failure(r, |
| 41 "void main() { float x = sin(1, 2); }", |
| 42 "error: 1: no match for sin(int, int)\n1 error\n"); |
| 43 } |
| 44 |
| 45 DEF_TEST(SkSLArgumentCountMismatch, r) { |
| 46 test_failure(r, |
| 47 "float foo(float x) { return x * x; }" |
| 48 "void main() { float x = foo(1, 2); }", |
| 49 "error: 1: call to 'foo' expected 1 argument, but found 2\n1 er
ror\n"); |
| 50 } |
| 51 |
| 52 DEF_TEST(SkSLArgumentMismatch, r) { |
| 53 test_failure(r, |
| 54 "float foo(float x) { return x * x; }" |
| 55 "void main() { float x = foo(true); }", |
| 56 "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
| 57 } |
| 58 |
| 59 DEF_TEST(SkSLIfTypeMismatch, r) { |
| 60 test_failure(r, |
| 61 "void main() { if (3) { } }", |
| 62 "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 63 } |
| 64 |
| 65 DEF_TEST(SkSLDoTypeMismatch, r) { |
| 66 test_failure(r, |
| 67 "void main() { do { } while (vec2(1)); }", |
| 68 "error: 1: expected 'bool', but found 'vec2'\n1 error\n"); |
| 69 } |
| 70 |
| 71 DEF_TEST(SkSLWhileTypeMismatch, r) { |
| 72 test_failure(r, |
| 73 "void main() { while (vec3(1)) { } }", |
| 74 "error: 1: expected 'bool', but found 'vec3'\n1 error\n"); |
| 75 } |
| 76 |
| 77 DEF_TEST(SkSLForTypeMismatch, r) { |
| 78 test_failure(r, |
| 79 "void main() { for (int x = 0; x; x++) { } }", |
| 80 "error: 1: expected 'bool', but found 'int'\n1 error\n"); |
| 81 } |
| 82 |
| 83 DEF_TEST(SkSLConstructorTypeMismatch, r) { |
| 84 test_failure(r, |
| 85 "void main() { vec2 x = vec2(1.0, false); }", |
| 86 "error: 1: expected 'float', but found 'bool'\n1 error\n"); |
| 87 test_failure(r, |
| 88 "void main() { bool x = bool(1.0); }", |
| 89 "error: 1: cannot construct 'bool'\n1 error\n"); |
| 90 test_failure(r, |
| 91 "struct foo { int x; }; void main() { foo x = foo(5); }", |
| 92 "error: 1: cannot construct 'foo'\n1 error\n"); |
| 93 test_failure(r, |
| 94 "struct foo { int x; } foo; void main() { float x = float(foo);
}", |
| 95 "error: 1: invalid argument to 'float' constructor (expected a
number or bool, but found 'foo')\n1 error\n"); |
| 96 test_failure(r, |
| 97 "struct foo { int x; } foo; void main() { vec2 x = vec2(foo); }
", |
| 98 "error: 1: 'foo' is not a valid parameter to 'vec2' constructor
\n1 error\n"); |
| 99 } |
| 100 |
| 101 DEF_TEST(SkSLConstructorArgumentCount, r) { |
| 102 test_failure(r, |
| 103 "void main() { vec3 x = vec3(1.0, 2.0); }", |
| 104 "error: 1: invalid arguments to 'vec3' constructor (expected 3
scalars, but " |
| 105 "found 2)\n1 error\n"); |
| 106 test_success(r, "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }"); |
| 107 } |
| 108 |
| 109 DEF_TEST(SkSLSwizzleScalar, r) { |
| 110 test_failure(r, |
| 111 "void main() { float x = 1; float y = x.y; }", |
| 112 "error: 1: cannot swizzle value of type 'float'\n1 error\n"); |
| 113 } |
| 114 |
| 115 DEF_TEST(SkSLSwizzleMatrix, r) { |
| 116 test_failure(r, |
| 117 "void main() { mat2 x = mat2(1); float y = x.y; }", |
| 118 "error: 1: cannot swizzle value of type 'mat2'\n1 error\n"); |
| 119 } |
| 120 |
| 121 DEF_TEST(SkSLSwizzleOutOfBounds, r) { |
| 122 test_failure(r, |
| 123 "void main() { vec3 test = vec2(1).xyz; }", |
| 124 "error: 1: invalid swizzle component 'z'\n1 error\n"); |
| 125 } |
| 126 |
| 127 DEF_TEST(SkSLSwizzleTooManyComponents, r) { |
| 128 test_failure(r, |
| 129 "void main() { vec4 test = vec2(1).xxxxx; }", |
| 130 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error
\n"); |
| 131 } |
| 132 |
| 133 DEF_TEST(SkSLSwizzleDuplicateOutput, r) { |
| 134 test_failure(r, |
| 135 "void main() { vec4 test = vec4(1); test.xyyz = vec4(1); }", |
| 136 "error: 1: cannot write to the same swizzle field more than onc
e\n1 error\n"); |
| 137 } |
| 138 DEF_TEST(SkSLAssignmentTypeMismatch, r) { |
| 139 test_failure(r, |
| 140 "void main() { int x = 1.0; }", |
| 141 "error: 1: expected 'int', but found 'float'\n1 error\n"); |
| 142 } |
| 143 |
| 144 DEF_TEST(SkSLReturnFromVoid, r) { |
| 145 test_failure(r, |
| 146 "void main() { return true; }", |
| 147 "error: 1: may not return a value from a void function\n1 error
\n"); |
| 148 } |
| 149 |
| 150 DEF_TEST(SkSLReturnMissingValue, r) { |
| 151 test_failure(r, |
| 152 "int foo() { return; } void main() { }", |
| 153 "error: 1: expected function to return 'int'\n1 error\n"); |
| 154 } |
| 155 |
| 156 DEF_TEST(SkSLReturnTypeMismatch, r) { |
| 157 test_failure(r, |
| 158 "int foo() { return 1.0; } void main() { }", |
| 159 "error: 1: expected 'int', but found 'float'\n1 error\n"); |
| 160 } |
| 161 |
| 162 DEF_TEST(SkSLDuplicateFunction, r) { |
| 163 test_failure(r, |
| 164 "void main() { } void main() { }", |
| 165 "error: 1: duplicate definition of void main()\n1 error\n"); |
| 166 test_success(r, |
| 167 "void main(); void main() { }"); |
| 168 } |
| 169 |
| 170 DEF_TEST(SkSLUsingInvalidValue, r) { |
| 171 test_failure(r, |
| 172 "void main() { int x = int; }", |
| 173 "error: 1: expected '(' to begin constructor invocation\n1 erro
r\n"); |
| 174 test_failure(r, |
| 175 "int test() { return 1; } void main() { int x = test; }", |
| 176 "error: 1: expected '(' to begin function call\n1 error\n"); |
| 177 } |
| 178 DEF_TEST(SkSLDifferentReturnType, r) { |
| 179 test_failure(r, |
| 180 "int main() { } void main() { }", |
| 181 "error: 1: functions 'void main()' and 'int main()' differ only
in return type\n1 " |
| 182 "error\n"); |
| 183 } |
| 184 |
| 185 DEF_TEST(SkSLDifferentModifiers, r) { |
| 186 test_failure(r, |
| 187 "void test(int x); void test(out int x) { }", |
| 188 "error: 1: modifiers on parameter 1 differ between declaration
and definition\n1 " |
| 189 "error\n"); |
| 190 } |
| 191 |
| 192 DEF_TEST(SkSLDuplicateSymbol, r) { |
| 193 test_failure(r, |
| 194 "int main; void main() { }", |
| 195 "error: 1: symbol 'main' was already defined\n1 error\n"); |
| 196 |
| 197 test_failure(r, |
| 198 "int x; int x; void main() { }", |
| 199 "error: 1: symbol 'x' was already defined\n1 error\n"); |
| 200 |
| 201 test_success(r, "int x; void main() { int x; }"); |
| 202 } |
| 203 |
| 204 DEF_TEST(SkSLBinaryTypeMismatch, r) { |
| 205 test_failure(r, |
| 206 "void main() { float x = 3 * true; }", |
| 207 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n
1 error\n"); |
| 208 test_failure(r, |
| 209 "void main() { bool x = 1 || 2.0; }", |
| 210 "error: 1: type mismatch: '||' cannot operate on 'int', 'float'
\n1 error\n"); |
| 211 } |
| 212 |
| 213 DEF_TEST(SkSLCallNonFunction, r) { |
| 214 test_failure(r, |
| 215 "void main() { float x = 3; x(); }", |
| 216 "error: 1: 'x' is not a function\n1 error\n"); |
| 217 } |
| 218 |
| 219 DEF_TEST(SkSLInvalidUnary, r) { |
| 220 test_failure(r, |
| 221 "void main() { mat4 x = mat4(1); ++x; }", |
| 222 "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 223 test_failure(r, |
| 224 "void main() { vec3 x = vec3(1); --x; }", |
| 225 "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 226 test_failure(r, |
| 227 "void main() { mat4 x = mat4(1); x++; }", |
| 228 "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); |
| 229 test_failure(r, |
| 230 "void main() { vec3 x = vec3(1); x--; }", |
| 231 "error: 1: '--' cannot operate on 'vec3'\n1 error\n"); |
| 232 test_failure(r, |
| 233 "void main() { int x = !12; }", |
| 234 "error: 1: '!' cannot operate on 'int'\n1 error\n"); |
| 235 test_failure(r, |
| 236 "struct foo { } bar; void main() { foo x = +bar; }", |
| 237 "error: 1: '+' cannot operate on 'foo'\n1 error\n"); |
| 238 test_failure(r, |
| 239 "struct foo { } bar; void main() { foo x = -bar; }", |
| 240 "error: 1: '-' cannot operate on 'foo'\n1 error\n"); |
| 241 test_success(r, |
| 242 "void main() { vec2 x = vec2(1, 1); x = +x; x = -x; }"); |
| 243 } |
| 244 |
| 245 DEF_TEST(SkSLInvalidAssignment, r) { |
| 246 test_failure(r, |
| 247 "void main() { 1 = 2; }", |
| 248 "error: 1: cannot assign to '1'\n1 error\n"); |
| 249 test_failure(r, |
| 250 "uniform int x; void main() { x = 0; }", |
| 251 "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 252 test_failure(r, |
| 253 "const int x; void main() { x = 0; }", |
| 254 "error: 1: cannot modify immutable variable 'x'\n1 error\n"); |
| 255 } |
| 256 |
| 257 DEF_TEST(SkSlBadIndex, r) { |
| 258 test_failure(r, |
| 259 "void main() { int x = 2[0]; }", |
| 260 "error: 1: expected array, but found 'int'\n1 error\n"); |
| 261 test_failure(r, |
| 262 "void main() { vec2 x = vec2(0); int y = x[0]; }", |
| 263 "error: 1: expected array, but found 'vec2'\n1 error\n"); |
| 264 } |
| 265 |
| 266 DEF_TEST(SkSLTernaryMismatch, r) { |
| 267 test_failure(r, |
| 268 "void main() { int x = 5 > 2 ? true : 1.0; }", |
| 269 "error: 1: ternary operator result mismatch: 'bool', 'float'\n1
error\n"); |
| 270 } |
OLD | NEW |