1 // Copyright 2019 The Amber Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or parseried.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "gtest/gtest.h"
16 #include "src/amberscript/parser.h"
17
18 namespace amber {
19 namespace amberscript {
20
21 using AmberScriptParserTest = testing::Test;
22
TEST_F(AmberScriptParserTest,PipelineShaderCompileOptions)23 TEST_F(AmberScriptParserTest, PipelineShaderCompileOptions) {
24 std::string in = R"(
25 SHADER compute my_shader OPENCL-C
26 #shader
27 END
28 PIPELINE compute my_pipeline
29 ATTACH my_shader
30 COMPILE_OPTIONS my_shader
31 --option1
32 --option2=blah
33 other
34 --option3 3
35 END
36 END
37 )";
38
39 Parser parser;
40 Result r = parser.Parse(in);
41 ASSERT_TRUE(r.IsSuccess());
42
43 auto script = parser.GetScript();
44 const auto& pipelines = script->GetPipelines();
45 ASSERT_EQ(1U, pipelines.size());
46
47 const auto* pipeline = pipelines[0].get();
48 const auto& shaders = pipeline->GetShaders();
49 ASSERT_EQ(1U, shaders.size());
50
51 const auto& shader = shaders[0];
52 const auto& options = shader.GetCompileOptions();
53 ASSERT_EQ(5U, options.size());
54 EXPECT_EQ("--option1", options[0]);
55 EXPECT_EQ("--option2=blah", options[1]);
56 EXPECT_EQ("other", options[2]);
57 EXPECT_EQ("--option3", options[3]);
58 EXPECT_EQ("3", options[4]);
59 }
60
TEST_F(AmberScriptParserTest,PipelineShaderCompileOptionsMissingShader)61 TEST_F(AmberScriptParserTest, PipelineShaderCompileOptionsMissingShader) {
62 std::string in = R"(
63 SHADER compute my_shader OPENCL-C
64 #shader
65 END
66 PIPELINE compute my_pipeline
67 ATTACH my_shader
68 COMPILE_OPTIONS
69 END
70 )";
71
72 Parser parser;
73 auto r = parser.Parse(in);
74 ASSERT_FALSE(r.IsSuccess());
75 EXPECT_EQ("8: missing shader name in COMPILE_OPTIONS command", r.Error());
76 }
77
TEST_F(AmberScriptParserTest,PipelineShaderCompileOptionsBadShader)78 TEST_F(AmberScriptParserTest, PipelineShaderCompileOptionsBadShader) {
79 std::string in = R"(
80 SHADER compute my_shader OPENCL-C
81 #shader
82 END
83 PIPELINE compute my_pipeline
84 ATTACH my_shader
85 COMPILE_OPTIONS not_my_shader
86 END
87 END
88 )";
89
90 Parser parser;
91 auto r = parser.Parse(in);
92 ASSERT_FALSE(r.IsSuccess());
93 EXPECT_EQ("7: unknown shader in COMPILE_OPTIONS command", r.Error());
94 }
95
TEST_F(AmberScriptParserTest,PipelineShaderCompileOptionsMissingEnd)96 TEST_F(AmberScriptParserTest, PipelineShaderCompileOptionsMissingEnd) {
97 std::string in = R"(
98 SHADER compute my_shader OPENCL-C
99 #shader
100 END
101 PIPELINE compute my_pipeline
102 ATTACH my_shader
103 COMPILE_OPTIONS my_shader
104 )";
105
106 Parser parser;
107 auto r = parser.Parse(in);
108 ASSERT_FALSE(r.IsSuccess());
109 EXPECT_EQ("8: COMPILE_OPTIONS missing END command", r.Error());
110 }
111
TEST_F(AmberScriptParserTest,PipelineShaderCompileOptionsExtraToken)112 TEST_F(AmberScriptParserTest, PipelineShaderCompileOptionsExtraToken) {
113 std::string in = R"(
114 SHADER compute my_shader OPENCL-C
115 #shader
116 END
117 PIPELINE compute my_pipeline
118 ATTACH my_shader
119 COMPILE_OPTIONS my_shader extra
120 END
121 END
122 )";
123
124 Parser parser;
125 auto r = parser.Parse(in);
126 ASSERT_FALSE(r.IsSuccess());
127 EXPECT_EQ("7: extra parameters after COMPILE_OPTIONS command: extra",
128 r.Error());
129 }
130
TEST_F(AmberScriptParserTest,PipelineShaderCompileOptionsExtraTokenEnd)131 TEST_F(AmberScriptParserTest, PipelineShaderCompileOptionsExtraTokenEnd) {
132 std::string in = R"(
133 SHADER compute my_shader OPENCL-C
134 #shader
135 END
136 PIPELINE compute my_pipeline
137 ATTACH my_shader
138 COMPILE_OPTIONS my_shader
139 END token
140 END
141 )";
142
143 Parser parser;
144 auto r = parser.Parse(in);
145 ASSERT_FALSE(r.IsSuccess());
146 EXPECT_EQ("8: extra parameters after COMPILE_OPTIONS command: token",
147 r.Error());
148 }
149
TEST_F(AmberScriptParserTest,PipelineShaderCompileOptionsNotOpenCL)150 TEST_F(AmberScriptParserTest, PipelineShaderCompileOptionsNotOpenCL) {
151 std::string in = R"(
152 SHADER compute my_shader SPIRV-ASM
153 #shader
154 END
155 PIPELINE compute my_pipeline
156 ATTACH my_shader
157 COMPILE_OPTIONS my_shader
158 END token
159 END
160 )";
161
162 Parser parser;
163 auto r = parser.Parse(in);
164 ASSERT_FALSE(r.IsSuccess());
165 EXPECT_EQ("7: COMPILE_OPTIONS currently only supports OPENCL-C shaders",
166 r.Error());
167 }
168
169 } // namespace amberscript
170 } // namespace amber
171