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#if SK_SUPPORT_GPU
13
14static void test_failure(skiatest::Reporter* r, const char* src, const char* error) {
15    SkSL::Compiler compiler;
16    SkSL::Program::Settings settings;
17    sk_sp<GrShaderCaps> caps = SkSL::ShaderCapsFactory::Default();
18    settings.fCaps = caps.get();
19    std::unique_ptr<SkSL::Program> program = compiler.convertProgram(SkSL::Program::kFragment_Kind,
20                                                                     SkSL::String(src), settings);
21    if (program) {
22        SkSL::String ignored;
23        compiler.toSPIRV(*program, &ignored);
24    }
25    SkSL::String skError(error);
26    if (compiler.errorText() != skError) {
27        SkDebugf("SKSL ERROR:\n    source: %s\n    expected: %s    received: %s", src, error,
28                 compiler.errorText().c_str());
29    }
30    REPORTER_ASSERT(r, compiler.errorText() == skError);
31}
32
33DEF_TEST(SkSLBadOffset, r) {
34    test_failure(r,
35                 "struct Bad { layout (offset = 5) int x; } bad; void main() { bad.x = 5; }",
36                 "error: 1: offset of field 'x' must be a multiple of 4\n1 error\n");
37    test_failure(r,
38                 "struct Bad { int x; layout (offset = 0) int y; } bad; void main() { bad.x = 5; }",
39                 "error: 1: offset of field 'y' must be at least 4\n1 error\n");
40}
41
42#endif
43