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,OpenCLSetMissingKernel)23 TEST_F(AmberScriptParserTest, OpenCLSetMissingKernel) {
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 SET ARG_NAME a AS uint32 0
31 END
32 )";
33
34 Parser parser;
35 auto r = parser.Parse(in);
36 ASSERT_FALSE(r.IsSuccess());
37 EXPECT_EQ("7: missing KERNEL in SET command", r.Error());
38 }
39
TEST_F(AmberScriptParserTest,OpenCLSetMissingArgName)40 TEST_F(AmberScriptParserTest, OpenCLSetMissingArgName) {
41 std::string in = R"(
42 SHADER compute my_shader OPENCL-C
43 #shader
44 END
45 PIPELINE compute my_pipeline
46 ATTACH my_shader
47 SET KERNEL a AS uint32 0
48 END
49 )";
50
51 Parser parser;
52 auto r = parser.Parse(in);
53 ASSERT_FALSE(r.IsSuccess());
54 EXPECT_EQ("7: expected ARG_NAME or ARG_NUMBER", r.Error());
55 }
56
TEST_F(AmberScriptParserTest,OpenCLSetMissingArgIdentifier)57 TEST_F(AmberScriptParserTest, OpenCLSetMissingArgIdentifier) {
58 std::string in = R"(
59 SHADER compute my_shader OPENCL-C
60 #shader
61 END
62 PIPELINE compute my_pipeline
63 ATTACH my_shader
64 SET KERNEL ARG_NAME AS uint32 0
65 END
66 )";
67
68 Parser parser;
69 auto r = parser.Parse(in);
70 ASSERT_FALSE(r.IsSuccess());
71 EXPECT_EQ("7: missing AS in SET command", r.Error());
72 }
73
TEST_F(AmberScriptParserTest,OpenCLSetMissingArgIdentifierNumber)74 TEST_F(AmberScriptParserTest, OpenCLSetMissingArgIdentifierNumber) {
75 std::string in = R"(
76 SHADER compute my_shader OPENCL-C
77 #shader
78 END
79 PIPELINE compute my_pipeline
80 ATTACH my_shader
81 SET KERNEL ARG_NUMBER AS uint32 0
82 END
83 )";
84
85 Parser parser;
86 auto r = parser.Parse(in);
87 ASSERT_FALSE(r.IsSuccess());
88 EXPECT_EQ("7: expected argument number", r.Error());
89 }
90
TEST_F(AmberScriptParserTest,OpenCLSetMissingAs)91 TEST_F(AmberScriptParserTest, OpenCLSetMissingAs) {
92 std::string in = R"(
93 SHADER compute my_shader OPENCL-C
94 #shader
95 END
96 PIPELINE compute my_pipeline
97 ATTACH my_shader
98 SET KERNEL ARG_NAME a uint32 0
99 END
100 )";
101
102 Parser parser;
103 auto r = parser.Parse(in);
104 ASSERT_FALSE(r.IsSuccess());
105 EXPECT_EQ("7: missing AS in SET command", r.Error());
106 }
107
TEST_F(AmberScriptParserTest,OpenCLSetMissingDataType)108 TEST_F(AmberScriptParserTest, OpenCLSetMissingDataType) {
109 std::string in = R"(
110 SHADER compute my_shader OPENCL-C
111 #shader
112 END
113 PIPELINE compute my_pipeline
114 ATTACH my_shader
115 SET KERNEL ARG_NAME a AS 0
116 END
117 )";
118
119 Parser parser;
120 auto r = parser.Parse(in);
121 ASSERT_FALSE(r.IsSuccess());
122 EXPECT_EQ("7: expected data type", r.Error());
123 }
124
TEST_F(AmberScriptParserTest,OpenCLSetMissingDataValue)125 TEST_F(AmberScriptParserTest, OpenCLSetMissingDataValue) {
126 std::string in = R"(
127 SHADER compute my_shader OPENCL-C
128 #shader
129 END
130 PIPELINE compute my_pipeline
131 ATTACH my_shader
132 SET KERNEL ARG_NAME a AS uint32
133 END
134 )";
135
136 Parser parser;
137 auto r = parser.Parse(in);
138 ASSERT_FALSE(r.IsSuccess());
139 EXPECT_EQ("8: expected data value", r.Error());
140 }
141
TEST_F(AmberScriptParserTest,OpenCLSetExtraTokens)142 TEST_F(AmberScriptParserTest, OpenCLSetExtraTokens) {
143 std::string in = R"(
144 SHADER compute my_shader OPENCL-C
145 #shader
146 END
147 PIPELINE compute my_pipeline
148 ATTACH my_shader
149 SET KERNEL ARG_NAME a AS uint32 0 BLAH
150 END
151 )";
152
153 Parser parser;
154 auto r = parser.Parse(in);
155 ASSERT_FALSE(r.IsSuccess());
156 EXPECT_EQ("7: extra parameters after SET command: BLAH", r.Error());
157 }
158
TEST_F(AmberScriptParserTest,OpenCLSetArgNameNotString)159 TEST_F(AmberScriptParserTest, OpenCLSetArgNameNotString) {
160 std::string in = R"(
161 SHADER compute my_shader OPENCL-C
162 #shader
163 END
164 PIPELINE compute my_pipeline
165 ATTACH my_shader
166 SET KERNEL ARG_NAME 0 AS uint32 0
167 END
168 )";
169
170 Parser parser;
171 auto r = parser.Parse(in);
172 ASSERT_FALSE(r.IsSuccess());
173 EXPECT_EQ("7: expected argument identifier", r.Error());
174 }
175
TEST_F(AmberScriptParserTest,OpenCLSetArgNumberNotInteger)176 TEST_F(AmberScriptParserTest, OpenCLSetArgNumberNotInteger) {
177 std::string in = R"(
178 SHADER compute my_shader OPENCL-C
179 #shader
180 END
181 PIPELINE compute my_pipeline
182 ATTACH my_shader
183 SET KERNEL ARG_NUMBER 1.0 AS uint32 0
184 END
185 )";
186
187 Parser parser;
188 auto r = parser.Parse(in);
189 ASSERT_FALSE(r.IsSuccess());
190 EXPECT_EQ("7: expected argument number", r.Error());
191 }
192
TEST_F(AmberScriptParserTest,OpenCLSetDataTypeNotString)193 TEST_F(AmberScriptParserTest, OpenCLSetDataTypeNotString) {
194 std::string in = R"(
195 SHADER compute my_shader OPENCL-C
196 #shader
197 END
198 PIPELINE compute my_pipeline
199 ATTACH my_shader
200 SET KERNEL ARG_NUMBER 0 AS 0 0
201 END
202 )";
203
204 Parser parser;
205 auto r = parser.Parse(in);
206 ASSERT_FALSE(r.IsSuccess());
207 EXPECT_EQ("7: expected data type", r.Error());
208 }
209
TEST_F(AmberScriptParserTest,OpenCLSetDataValueString)210 TEST_F(AmberScriptParserTest, OpenCLSetDataValueString) {
211 std::string in = R"(
212 SHADER compute my_shader OPENCL-C
213 #shader
214 END
215 PIPELINE compute my_pipeline
216 ATTACH my_shader
217 SET KERNEL ARG_NUMBER 0 AS uint32 data
218 END
219 )";
220
221 Parser parser;
222 auto r = parser.Parse(in);
223 ASSERT_FALSE(r.IsSuccess());
224 EXPECT_EQ("7: expected data value", r.Error());
225 }
226
TEST_F(AmberScriptParserTest,OpenCLSetWrongShaderFormat)227 TEST_F(AmberScriptParserTest, OpenCLSetWrongShaderFormat) {
228 std::string in = R"(
229 SHADER compute my_shader SPIRV-ASM
230 #shader
231 END
232 PIPELINE compute my_pipeline
233 ATTACH my_shader
234 SET KERNEL ARG_NAME arg_a AS uint32 0
235 END
236 )";
237
238 Parser parser;
239 auto r = parser.Parse(in);
240 ASSERT_FALSE(r.IsSuccess());
241 EXPECT_EQ("7: SET can only be used with OPENCL-C shaders", r.Error());
242 }
243
TEST_F(AmberScriptParserTest,OpenCLSetNonScalarDataType)244 TEST_F(AmberScriptParserTest, OpenCLSetNonScalarDataType) {
245 std::string in = R"(
246 SHADER compute my_shader OPENCL-C
247 #shader
248 END
249 PIPELINE compute my_pipeline
250 ATTACH my_shader
251 SET KERNEL ARG_NAME arg_a AS vec4<uint32> 0 0 0 0
252 END
253 )";
254
255 Parser parser;
256 auto r = parser.Parse(in);
257 ASSERT_FALSE(r.IsSuccess());
258 EXPECT_EQ("7: data type must be a scalar type", r.Error());
259 }
260
261 } // namespace amberscript
262 } // namespace amber
263