• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,ImageNameMissing1)23 TEST_F(AmberScriptParserTest, ImageNameMissing1) {
24   std::string in = R"(
25 IMAGE
26 )";
27 
28   Parser parser;
29   Result r = parser.Parse(in);
30   ASSERT_FALSE(r.IsSuccess());
31   EXPECT_EQ("3: invalid IMAGE name provided", r.Error());
32 }
33 
TEST_F(AmberScriptParserTest,ImageNameMissing2)34 TEST_F(AmberScriptParserTest, ImageNameMissing2) {
35   std::string in = R"(
36 IMAGE DATA_TYPE
37 )";
38 
39   Parser parser;
40   Result r = parser.Parse(in);
41   ASSERT_FALSE(r.IsSuccess());
42   EXPECT_EQ("2: missing IMAGE name", r.Error());
43 }
44 
TEST_F(AmberScriptParserTest,ImageNameMissing3)45 TEST_F(AmberScriptParserTest, ImageNameMissing3) {
46   std::string in = R"(
47 IMAGE FORMAT
48 )";
49 
50   Parser parser;
51   Result r = parser.Parse(in);
52   ASSERT_FALSE(r.IsSuccess());
53   EXPECT_EQ("2: missing IMAGE name", r.Error());
54 }
55 
TEST_F(AmberScriptParserTest,ImageNameInvalid)56 TEST_F(AmberScriptParserTest, ImageNameInvalid) {
57   std::string in = R"(
58 IMAGE 1
59 )";
60 
61   Parser parser;
62   Result r = parser.Parse(in);
63   ASSERT_FALSE(r.IsSuccess());
64   EXPECT_EQ("2: invalid IMAGE name provided", r.Error());
65 }
66 
TEST_F(AmberScriptParserTest,ImageDataTypeInvalid)67 TEST_F(AmberScriptParserTest, ImageDataTypeInvalid) {
68   std::string in = R"(
69 IMAGE image DATA_TYPE blah
70 )";
71 
72   Parser parser;
73   Result r = parser.Parse(in);
74   ASSERT_FALSE(r.IsSuccess());
75   EXPECT_EQ("2: invalid data type 'blah' provided", r.Error());
76 }
77 
TEST_F(AmberScriptParserTest,ImageFormatInvalid)78 TEST_F(AmberScriptParserTest, ImageFormatInvalid) {
79   std::string in = R"(
80 IMAGE image FORMAT blah
81 )";
82 
83   Parser parser;
84   Result r = parser.Parse(in);
85   ASSERT_FALSE(r.IsSuccess());
86   EXPECT_EQ("2: invalid IMAGE FORMAT", r.Error());
87 }
88 
TEST_F(AmberScriptParserTest,ImageMipLevelsInvalid)89 TEST_F(AmberScriptParserTest, ImageMipLevelsInvalid) {
90   std::string in = R"(
91 IMAGE image FORMAT R32G32B32A32_SFLOAT MIP_LEVELS mips
92 )";
93 
94   Parser parser;
95   Result r = parser.Parse(in);
96   ASSERT_FALSE(r.IsSuccess());
97   EXPECT_EQ("2: invalid value for MIP_LEVELS", r.Error());
98 }
99 
TEST_F(AmberScriptParserTest,ImageMissingDataTypeCommand)100 TEST_F(AmberScriptParserTest, ImageMissingDataTypeCommand) {
101   std::string in = R"(
102 IMAGE image OTHER
103 )";
104 
105   Parser parser;
106   Result r = parser.Parse(in);
107   ASSERT_FALSE(r.IsSuccess());
108   EXPECT_EQ("2: unknown IMAGE command provided: OTHER", r.Error());
109 }
110 
TEST_F(AmberScriptParserTest,ImageDimensionalityInvalid)111 TEST_F(AmberScriptParserTest, ImageDimensionalityInvalid) {
112   std::string in = R"(
113 IMAGE image DATA_TYPE uint32 DIM_WRONG
114 )";
115 
116   Parser parser;
117   Result r = parser.Parse(in);
118   ASSERT_FALSE(r.IsSuccess());
119   EXPECT_EQ("2: unknown IMAGE command provided: DIM_WRONG", r.Error());
120 }
121 
TEST_F(AmberScriptParserTest,ImageDimensionalityInvalid2)122 TEST_F(AmberScriptParserTest, ImageDimensionalityInvalid2) {
123   std::string in = R"(
124 IMAGE image DATA_TYPE uint32 4
125 )";
126 
127   Parser parser;
128   Result r = parser.Parse(in);
129   ASSERT_FALSE(r.IsSuccess());
130   EXPECT_EQ("2: expected IMAGE WIDTH", r.Error());
131 }
132 
TEST_F(AmberScriptParserTest,ImageWidthMissing)133 TEST_F(AmberScriptParserTest, ImageWidthMissing) {
134   std::string in = R"(
135 IMAGE image DATA_TYPE uint32 DIM_3D HEIGHT 2 DEPTH 2 FILL 0
136 )";
137 
138   Parser parser;
139   Result r = parser.Parse(in);
140   ASSERT_FALSE(r.IsSuccess());
141   EXPECT_EQ("2: expected IMAGE WIDTH", r.Error());
142 }
143 
TEST_F(AmberScriptParserTest,ImageHeightMissing)144 TEST_F(AmberScriptParserTest, ImageHeightMissing) {
145   std::string in = R"(
146 IMAGE image DATA_TYPE uint32 DIM_3D WIDTH 2 DEPTH 2 FILL 0
147 )";
148 
149   Parser parser;
150   Result r = parser.Parse(in);
151   ASSERT_FALSE(r.IsSuccess());
152   EXPECT_EQ("2: expected IMAGE HEIGHT", r.Error());
153 }
154 
TEST_F(AmberScriptParserTest,ImageDepthMissing)155 TEST_F(AmberScriptParserTest, ImageDepthMissing) {
156   std::string in = R"(
157 IMAGE image DATA_TYPE uint32 DIM_3D WIDTH 2 HEIGHT 2 FILL 0
158 )";
159 
160   Parser parser;
161   Result r = parser.Parse(in);
162   ASSERT_FALSE(r.IsSuccess());
163   EXPECT_EQ("2: expected IMAGE DEPTH", r.Error());
164 }
165 
TEST_F(AmberScriptParserTest,ImageWidthMissingNumber)166 TEST_F(AmberScriptParserTest, ImageWidthMissingNumber) {
167   std::string in = R"(
168 IMAGE image DATA_TYPE uint32 DIM_3D WIDTH HEIGHT 2 DEPTH 2 FILL 0
169 )";
170 
171   Parser parser;
172   Result r = parser.Parse(in);
173   ASSERT_FALSE(r.IsSuccess());
174   EXPECT_EQ("2: expected positive IMAGE WIDTH", r.Error());
175 }
176 
TEST_F(AmberScriptParserTest,ImageHeightMissingNumber)177 TEST_F(AmberScriptParserTest, ImageHeightMissingNumber) {
178   std::string in = R"(
179 IMAGE image DATA_TYPE uint32 DIM_3D WIDTH 2 HEIGHT DEPTH 2 FILL 0
180 )";
181 
182   Parser parser;
183   Result r = parser.Parse(in);
184   ASSERT_FALSE(r.IsSuccess());
185   EXPECT_EQ("2: expected positive IMAGE HEIGHT", r.Error());
186 }
187 
TEST_F(AmberScriptParserTest,ImageDepthMissingNumber)188 TEST_F(AmberScriptParserTest, ImageDepthMissingNumber) {
189   std::string in = R"(
190 IMAGE image DATA_TYPE uint32 DIM_3D WIDTH 2 HEIGHT 2 DEPTH FILL 0
191 )";
192 
193   Parser parser;
194   Result r = parser.Parse(in);
195   ASSERT_FALSE(r.IsSuccess());
196   EXPECT_EQ("2: expected positive IMAGE DEPTH", r.Error());
197 }
198 
TEST_F(AmberScriptParserTest,Image1D)199 TEST_F(AmberScriptParserTest, Image1D) {
200   std::string in = R"(
201 IMAGE image DATA_TYPE uint32 DIM_1D WIDTH 4
202 )";
203 
204   Parser parser;
205   Result r = parser.Parse(in);
206   ASSERT_TRUE(r.IsSuccess());
207   auto script = parser.GetScript();
208   const auto& buffers = script->GetBuffers();
209   ASSERT_EQ(1U, buffers.size());
210 
211   ASSERT_TRUE(buffers[0] != nullptr);
212   EXPECT_EQ("image", buffers[0]->GetName());
213 
214   auto* buffer = buffers[0].get();
215   EXPECT_TRUE(buffer->GetFormat()->IsUint32());
216   EXPECT_EQ(ImageDimension::k1D, buffer->GetImageDimension());
217   EXPECT_EQ(4u, buffer->GetWidth());
218   EXPECT_EQ(1u, buffer->GetHeight());
219   EXPECT_EQ(1u, buffer->GetDepth());
220   EXPECT_EQ(4u, buffer->ElementCount());
221 }
222 
TEST_F(AmberScriptParserTest,Image2D)223 TEST_F(AmberScriptParserTest, Image2D) {
224   std::string in = R"(
225 IMAGE image DATA_TYPE uint32 DIM_2D WIDTH 3 HEIGHT 4
226 )";
227 
228   Parser parser;
229   Result r = parser.Parse(in);
230   ASSERT_TRUE(r.IsSuccess());
231   auto script = parser.GetScript();
232   const auto& buffers = script->GetBuffers();
233   ASSERT_EQ(1U, buffers.size());
234 
235   ASSERT_TRUE(buffers[0] != nullptr);
236   EXPECT_EQ("image", buffers[0]->GetName());
237 
238   auto* buffer = buffers[0].get();
239   EXPECT_TRUE(buffer->GetFormat()->IsUint32());
240   EXPECT_EQ(ImageDimension::k2D, buffer->GetImageDimension());
241   EXPECT_EQ(3u, buffer->GetWidth());
242   EXPECT_EQ(4u, buffer->GetHeight());
243   EXPECT_EQ(1u, buffer->GetDepth());
244   EXPECT_EQ(12u, buffer->ElementCount());
245 }
246 
TEST_F(AmberScriptParserTest,Image2DMultiSample)247 TEST_F(AmberScriptParserTest, Image2DMultiSample) {
248   std::string in = R"(
249 IMAGE image DATA_TYPE uint32 DIM_2D WIDTH 3 HEIGHT 4 SAMPLES 4
250 )";
251 
252   Parser parser;
253   Result r = parser.Parse(in);
254   ASSERT_TRUE(r.IsSuccess());
255   auto script = parser.GetScript();
256   const auto& buffers = script->GetBuffers();
257   ASSERT_EQ(1U, buffers.size());
258 
259   ASSERT_TRUE(buffers[0] != nullptr);
260   EXPECT_EQ("image", buffers[0]->GetName());
261 
262   auto* buffer = buffers[0].get();
263   EXPECT_EQ(4u, buffer->GetSamples());
264 }
265 
TEST_F(AmberScriptParserTest,Image2DInvalidSampleValue)266 TEST_F(AmberScriptParserTest, Image2DInvalidSampleValue) {
267   std::string in = R"(
268 IMAGE image DATA_TYPE uint32 DIM_2D WIDTH 3 HEIGHT 4 SAMPLES foo
269 )";
270 
271   Parser parser;
272   Result r = parser.Parse(in);
273   ASSERT_FALSE(r.IsSuccess());
274   EXPECT_EQ("2: expected integer value for SAMPLES", r.Error());
275 }
276 
TEST_F(AmberScriptParserTest,Image2DInvalidSampleCount)277 TEST_F(AmberScriptParserTest, Image2DInvalidSampleCount) {
278   std::string in = R"(
279 IMAGE image DATA_TYPE uint32 DIM_2D WIDTH 3 HEIGHT 4 SAMPLES 5
280 )";
281 
282   Parser parser;
283   Result r = parser.Parse(in);
284   ASSERT_FALSE(r.IsSuccess());
285   EXPECT_EQ("2: invalid sample count: 5", r.Error());
286 }
287 
TEST_F(AmberScriptParserTest,Image3D)288 TEST_F(AmberScriptParserTest, Image3D) {
289   std::string in = R"(
290 IMAGE image DATA_TYPE uint32 DIM_3D WIDTH 3 HEIGHT 4 DEPTH 5
291 )";
292 
293   Parser parser;
294   Result r = parser.Parse(in);
295   ASSERT_TRUE(r.IsSuccess());
296   auto script = parser.GetScript();
297   const auto& buffers = script->GetBuffers();
298   ASSERT_EQ(1U, buffers.size());
299 
300   ASSERT_TRUE(buffers[0] != nullptr);
301   EXPECT_EQ("image", buffers[0]->GetName());
302 
303   auto* buffer = buffers[0].get();
304   EXPECT_TRUE(buffer->GetFormat()->IsUint32());
305   EXPECT_EQ(ImageDimension::k3D, buffer->GetImageDimension());
306   EXPECT_EQ(3u, buffer->GetWidth());
307   EXPECT_EQ(4u, buffer->GetHeight());
308   EXPECT_EQ(5u, buffer->GetDepth());
309   EXPECT_EQ(60u, buffer->ElementCount());
310 }
311 
TEST_F(AmberScriptParserTest,ImageWithData)312 TEST_F(AmberScriptParserTest, ImageWithData) {
313   std::string in = R"(
314 IMAGE image DATA_TYPE float DIM_3D HEIGHT 2 WIDTH 2 DEPTH 2 DATA
315  0.11 0.12
316  0.21 0.22
317 
318  0.31 0.32
319  0.41 0.42
320 END
321 )";
322 
323   Parser parser;
324   Result r = parser.Parse(in);
325   ASSERT_TRUE(r.IsSuccess()) << r.Error();
326   auto script = parser.GetScript();
327   const auto& buffers = script->GetBuffers();
328   ASSERT_EQ(1U, buffers.size());
329 
330   ASSERT_TRUE(buffers[0] != nullptr);
331   EXPECT_EQ("image", buffers[0]->GetName());
332 
333   auto* buffer = buffers[0].get();
334   EXPECT_TRUE(buffer->GetFormat()->IsFloat32());
335   EXPECT_EQ(ImageDimension::k3D, buffer->GetImageDimension());
336   EXPECT_EQ(2u, buffer->GetWidth());
337   EXPECT_EQ(2u, buffer->GetHeight());
338   EXPECT_EQ(2u, buffer->GetDepth());
339   EXPECT_EQ(8u, buffer->ElementCount());
340 
341   auto* values = buffer->GetValues<float>();
342   std::vector<float> result = {0.11f, 0.12f, 0.21f, 0.22f,
343                                0.31f, 0.32f, 0.41f, 0.42f};
344 
345   EXPECT_EQ((*buffer->ValuePtr()).size(), 8u * sizeof(float));
346   for (size_t i = 0; i < result.size(); ++i) {
347     EXPECT_FLOAT_EQ(result[i], values[i]);
348   }
349 }
350 
TEST_F(AmberScriptParserTest,ImageDataSizeIncorrect)351 TEST_F(AmberScriptParserTest, ImageDataSizeIncorrect) {
352   std::string in = R"(
353 IMAGE image DATA_TYPE float DIM_3D HEIGHT 2 WIDTH 2 DEPTH 2 DATA
354  0.11 0.12
355  0.21 0.22
356 END
357 )";
358 
359   Parser parser;
360   Result r = parser.Parse(in);
361   ASSERT_FALSE(r.IsSuccess());
362   EXPECT_EQ(
363       "6: Elements provided in data does not match size specified: 8 specified "
364       "vs 4 provided",
365       r.Error());
366 }
367 
368 }  // namespace amberscript
369 }  // namespace amber
370