Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* | |
|
dogben
2016/06/28 02:14:55
IWBN to add tests for more of the newly-added erro
| |
| 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 } | |
| 97 | |
| 98 DEF_TEST(SkSLConstructorArgumentCount, r) { | |
| 99 test_failure(r, | |
| 100 "void main() { vec3 x = vec3(1.0, 2.0); }", | |
| 101 "error: 1: invalid arguments to 'vec3' constructor (expected 3 scalars, but " | |
| 102 "found 2)\n1 error\n"); | |
| 103 test_success(r, "void main() { vec3 x = vec3(1.0, 2.0, 3.0, 4.0); }"); | |
| 104 } | |
| 105 | |
| 106 DEF_TEST(SkSLSwizzleScalar, r) { | |
| 107 test_failure(r, | |
| 108 "void main() { float x = 1; float y = x.y; }", | |
| 109 "error: 1: cannot swizzle value of type 'float'\n1 error\n"); | |
| 110 } | |
| 111 | |
| 112 DEF_TEST(SkSLSwizzleMatrix, r) { | |
| 113 test_failure(r, | |
| 114 "void main() { mat2 x = mat2(1); float y = x.y; }", | |
| 115 "error: 1: cannot swizzle value of type 'mat2'\n1 error\n"); | |
| 116 } | |
| 117 | |
| 118 DEF_TEST(SkSLSwizzleOutOfBounds, r) { | |
| 119 test_failure(r, | |
| 120 "void main() { vec3 test = vec2(1).xyz; }", | |
| 121 "error: 1: invalid swizzle component 'z'\n1 error\n"); | |
| 122 } | |
| 123 | |
| 124 DEF_TEST(SkSLSwizzleTooManyComponents, r) { | |
| 125 test_failure(r, | |
| 126 "void main() { vec4 test = vec2(1).xxxxx; }", | |
| 127 "error: 1: too many components in swizzle mask 'xxxxx'\n1 error \n"); | |
| 128 } | |
| 129 | |
| 130 DEF_TEST(SkSLAssignmentTypeMismatch, r) { | |
| 131 test_failure(r, | |
| 132 "void main() { int x = 1.0; }", | |
| 133 "error: 1: expected 'int', but found 'float'\n1 error\n"); | |
| 134 } | |
| 135 | |
| 136 DEF_TEST(SkSLReturnFromVoid, r) { | |
| 137 test_failure(r, | |
| 138 "void main() { return true; }", | |
| 139 "error: 1: may not return a value from a void function\n1 error \n"); | |
| 140 } | |
| 141 | |
| 142 DEF_TEST(SkSLReturnMissingValue, r) { | |
| 143 test_failure(r, | |
| 144 "int foo() { return; } void main() { }", | |
| 145 "error: 1: expected function to return 'int'\n1 error\n"); | |
| 146 } | |
| 147 | |
| 148 DEF_TEST(SkSLReturnTypeMismatch, r) { | |
| 149 test_failure(r, | |
| 150 "int foo() { return 1.0; } void main() { }", | |
| 151 "error: 1: expected 'int', but found 'float'\n1 error\n"); | |
| 152 } | |
| 153 | |
| 154 DEF_TEST(SkSLDuplicateFunction, r) { | |
| 155 test_failure(r, | |
| 156 "void main() { } void main() { }", | |
| 157 "error: 1: duplicate definition of void main()\n1 error\n"); | |
| 158 test_success(r, | |
| 159 "void main(); void main() { }"); | |
| 160 } | |
| 161 | |
| 162 DEF_TEST(SkSLDifferentReturnType, r) { | |
| 163 test_failure(r, | |
| 164 "int main() { } void main() { }", | |
| 165 "error: 1: functions 'void main()' and 'int main()' differ only in return type\n1 " | |
| 166 "error\n"); | |
| 167 } | |
| 168 | |
| 169 DEF_TEST(SkSLDifferentModifiers, r) { | |
| 170 test_failure(r, | |
| 171 "void test(int x); void test(out int x) { }", | |
| 172 "error: 1: modifiers on parameter 1 differ between declaration and definition\n1 " | |
| 173 "error\n"); | |
| 174 } | |
| 175 | |
| 176 DEF_TEST(SkSLDuplicateSymbol, r) { | |
| 177 test_failure(r, | |
| 178 "int main; void main() { }", | |
| 179 "error: 1: symbol 'main' was already defined\n1 error\n"); | |
| 180 | |
| 181 test_failure(r, | |
| 182 "int x; int x; void main() { }", | |
| 183 "error: 1: symbol 'x' was already defined\n1 error\n"); | |
| 184 | |
| 185 test_success(r, "int x; void main() { int x; }"); | |
| 186 } | |
| 187 | |
| 188 DEF_TEST(SkSLBinaryTypeMismatch, r) { | |
| 189 test_failure(r, | |
| 190 "void main() { float x = 3 * true; }", | |
| 191 "error: 1: type mismatch: '*' cannot operate on 'int', 'bool'\n 1 error\n"); | |
| 192 } | |
| 193 | |
| 194 DEF_TEST(SkSLCallNonFunction, r) { | |
| 195 test_failure(r, | |
| 196 "void main() { float x = 3; x(); }", | |
| 197 "error: 1: 'x' is not a function\n1 error\n"); | |
| 198 } | |
| 199 | |
| 200 DEF_TEST(SkSLInvalidUnary, r) { | |
| 201 test_failure(r, | |
| 202 "void main() { mat4 x = mat4(1); x++; }", | |
| 203 "error: 1: '++' cannot operate on 'mat4'\n1 error\n"); | |
| 204 } | |
| 205 | |
| 206 DEF_TEST(SkSLInvalidAssignment, r) { | |
| 207 test_failure(r, | |
| 208 "void main() { 1 = 2; }", | |
| 209 "error: 1: cannot assign to '1'\n1 error\n"); | |
| 210 } | |
| OLD | NEW |