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 implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "src/type.h"
16
17 #include "gtest/gtest.h"
18
19 namespace amber {
20 namespace type {
21
22 using TypeTest = testing::Test;
23
TEST_F(TypeTest,IsArray)24 TEST_F(TypeTest, IsArray) {
25 Number i(FormatMode::kSInt, 16);
26
27 EXPECT_FALSE(i.IsArray());
28 EXPECT_FALSE(i.IsRuntimeArray());
29 EXPECT_FALSE(i.IsSizedArray());
30
31 i.SetIsRuntimeArray();
32 EXPECT_TRUE(i.IsArray());
33 EXPECT_TRUE(i.IsRuntimeArray());
34 EXPECT_FALSE(i.IsSizedArray());
35
36 i.SetIsSizedArray(3);
37 EXPECT_TRUE(i.IsArray());
38 EXPECT_FALSE(i.IsRuntimeArray());
39 EXPECT_TRUE(i.IsSizedArray());
40 EXPECT_EQ(3u, i.ArraySize());
41 }
42
TEST_F(TypeTest,IsStruct)43 TEST_F(TypeTest, IsStruct) {
44 EXPECT_FALSE(Number(FormatMode::kSInt).IsStruct());
45 EXPECT_TRUE(Struct().IsStruct());
46 EXPECT_FALSE(List().IsStruct());
47 }
48
TEST_F(TypeTest,IsNumber)49 TEST_F(TypeTest, IsNumber) {
50 EXPECT_TRUE(Number(FormatMode::kSInt).IsNumber());
51 EXPECT_FALSE(Struct().IsNumber());
52 EXPECT_FALSE(List().IsNumber());
53 }
54
TEST_F(TypeTest,IsList)55 TEST_F(TypeTest, IsList) {
56 EXPECT_FALSE(Number(FormatMode::kSInt).IsList());
57 EXPECT_FALSE(Struct().IsList());
58 EXPECT_TRUE(List().IsList());
59 }
60
TEST_F(TypeTest,Vectors)61 TEST_F(TypeTest, Vectors) {
62 Number i(FormatMode::kSInt, 16);
63 i.SetRowCount(2);
64
65 EXPECT_EQ(2u, i.RowCount());
66 EXPECT_TRUE(i.IsVec());
67 EXPECT_FALSE(i.IsVec3());
68 EXPECT_FALSE(i.IsMatrix());
69
70 i.SetColumnCount(3);
71 EXPECT_FALSE(i.IsVec3());
72 }
73
TEST_F(TypeTest,Matrix)74 TEST_F(TypeTest, Matrix) {
75 Number i(FormatMode::kSInt, 16);
76 i.SetColumnCount(2);
77 i.SetRowCount(2);
78
79 EXPECT_EQ(2u, i.ColumnCount());
80 EXPECT_EQ(2u, i.RowCount());
81
82 EXPECT_FALSE(i.IsVec());
83 EXPECT_TRUE(i.IsMatrix());
84 }
85
TEST_F(TypeTest,SizeInBytesForVector)86 TEST_F(TypeTest, SizeInBytesForVector) {
87 Number i(FormatMode::kSInt, 32);
88 uint32_t bytes = i.SizeInBytes();
89
90 i.SetRowCount(3);
91 EXPECT_EQ(bytes, i.SizeInBytes());
92 }
93
TEST_F(TypeTest,SizeInBytesForMatrix)94 TEST_F(TypeTest, SizeInBytesForMatrix) {
95 Number i(FormatMode::kSInt, 32);
96 uint32_t bytes = i.SizeInBytes();
97
98 i.SetColumnCount(3);
99 i.SetRowCount(3);
100 EXPECT_EQ(bytes, i.SizeInBytes());
101 }
102
TEST_F(TypeTest,SizeInBytesForArray)103 TEST_F(TypeTest, SizeInBytesForArray) {
104 Number i(FormatMode::kSInt, 32);
105 uint32_t bytes = i.SizeInBytes();
106
107 i.SetIsSizedArray(3);
108 EXPECT_EQ(bytes, i.SizeInBytes());
109 }
110
TEST_F(TypeTest,NumberEqual)111 TEST_F(TypeTest, NumberEqual) {
112 Number n1(FormatMode::kSFloat, 32);
113 Number n2(FormatMode::kSFloat, 32);
114 Number n3(FormatMode::kSFloat, 16);
115 Number n4(FormatMode::kSInt, 32);
116
117 List l;
118 Struct s;
119
120 EXPECT_TRUE(n1.Equal(&n2));
121 EXPECT_FALSE(n1.Equal(&n3));
122 EXPECT_FALSE(n1.Equal(&n4));
123 EXPECT_FALSE(n1.Equal(&l));
124 EXPECT_FALSE(n1.Equal(&s));
125 }
126
TEST_F(TypeTest,ListPacked)127 TEST_F(TypeTest, ListPacked) {
128 List l;
129 EXPECT_FALSE(l.IsPacked());
130 EXPECT_EQ(0U, l.PackSizeInBits());
131
132 l.SetPackSizeInBits(32);
133 EXPECT_TRUE(l.IsPacked());
134 EXPECT_EQ(32U, l.PackSizeInBits());
135 }
136
TEST_F(TypeTest,ListEqual)137 TEST_F(TypeTest, ListEqual) {
138 List l1;
139 List l2;
140
141 l1.AddMember(FormatComponentType::kR, FormatMode::kSFloat, 32);
142 l2.AddMember(FormatComponentType::kR, FormatMode::kSFloat, 32);
143 EXPECT_TRUE(l1.Equal(&l2));
144
145 l2.SetPackSizeInBits(24);
146 EXPECT_FALSE(l1.Equal(&l2));
147
148 List l3;
149 l3.AddMember(FormatComponentType::kR, FormatMode::kSFloat, 16);
150 EXPECT_FALSE(l1.Equal(&l3));
151
152 List l4;
153 l4.AddMember(FormatComponentType::kR, FormatMode::kSInt, 16);
154 EXPECT_FALSE(l1.Equal(&l4));
155
156 List l5;
157 l5.AddMember(FormatComponentType::kG, FormatMode::kSFloat, 32);
158 EXPECT_FALSE(l1.Equal(&l5));
159
160 List l6;
161 l6.AddMember(FormatComponentType::kR, FormatMode::kSFloat, 32);
162 l6.AddMember(FormatComponentType::kG, FormatMode::kSFloat, 32);
163 EXPECT_FALSE(l1.Equal(&l6));
164 }
165
TEST_F(TypeTest,StructStride)166 TEST_F(TypeTest, StructStride) {
167 Struct s;
168 EXPECT_FALSE(s.HasStride());
169 EXPECT_EQ(0U, s.StrideInBytes());
170
171 s.SetStrideInBytes(32);
172 EXPECT_TRUE(s.HasStride());
173 EXPECT_EQ(32U, s.StrideInBytes());
174 }
175
TEST_F(TypeTest,StructEqual)176 TEST_F(TypeTest, StructEqual) {
177 Struct s1;
178 Struct s2;
179
180 auto num32 = Number::Float(32);
181 auto m1 = s1.AddMember(num32.get());
182 s2.AddMember(num32.get());
183 EXPECT_TRUE(s1.Equal(&s2));
184
185 s2.SetStrideInBytes(20);
186 EXPECT_FALSE(s1.Equal(&s2));
187
188 Struct s3;
189 auto num16 = Number::Float(16);
190 s3.AddMember(num16.get());
191 EXPECT_FALSE(s1.Equal(&s3));
192
193 Struct s4;
194 auto m = s4.AddMember(num16.get());
195 m->offset_in_bytes = 20;
196 EXPECT_FALSE(s1.Equal(&s4));
197
198 m->offset_in_bytes = m1->offset_in_bytes;
199 m->array_stride_in_bytes = 20;
200 EXPECT_FALSE(s1.Equal(&s4));
201
202 m->array_stride_in_bytes = m1->array_stride_in_bytes;
203 m->matrix_stride_in_bytes = 20;
204 EXPECT_FALSE(s1.Equal(&s4));
205 }
206
TEST_F(TypeTest,NumberDefault32Bits)207 TEST_F(TypeTest, NumberDefault32Bits) {
208 EXPECT_EQ(4u, Number(FormatMode::kUNorm).SizeInBytes());
209 }
210
TEST_F(TypeTest,NumberInBytes)211 TEST_F(TypeTest, NumberInBytes) {
212 EXPECT_EQ(1u, Number(FormatMode::kSInt, 8).SizeInBytes());
213 EXPECT_EQ(2u, Number(FormatMode::kSInt, 16).SizeInBytes());
214 EXPECT_EQ(4u, Number(FormatMode::kSInt, 32).SizeInBytes());
215 EXPECT_EQ(8u, Number(FormatMode::kSInt, 64).SizeInBytes());
216 }
217
TEST_F(TypeTest,IsInt)218 TEST_F(TypeTest, IsInt) {
219 EXPECT_TRUE(Type::IsInt(FormatMode::kSInt));
220 EXPECT_TRUE(Type::IsInt(FormatMode::kSNorm));
221 EXPECT_TRUE(Type::IsInt(FormatMode::kSScaled));
222 EXPECT_TRUE(Type::IsInt(FormatMode::kSRGB));
223 EXPECT_TRUE(Type::IsInt(FormatMode::kUNorm));
224 EXPECT_TRUE(Type::IsInt(FormatMode::kUInt));
225 EXPECT_TRUE(Type::IsInt(FormatMode::kUScaled));
226 EXPECT_FALSE(Type::IsInt(FormatMode::kSFloat));
227 EXPECT_FALSE(Type::IsInt(FormatMode::kUFloat));
228 }
229
TEST_F(TypeTest,IsSignedInt)230 TEST_F(TypeTest, IsSignedInt) {
231 EXPECT_TRUE(Type::IsSignedInt(FormatMode::kSInt));
232 EXPECT_TRUE(Type::IsSignedInt(FormatMode::kSNorm));
233 EXPECT_TRUE(Type::IsSignedInt(FormatMode::kSScaled));
234 EXPECT_FALSE(Type::IsSignedInt(FormatMode::kSRGB));
235 EXPECT_FALSE(Type::IsSignedInt(FormatMode::kUNorm));
236 EXPECT_FALSE(Type::IsSignedInt(FormatMode::kUInt));
237 EXPECT_FALSE(Type::IsSignedInt(FormatMode::kUScaled));
238 EXPECT_FALSE(Type::IsSignedInt(FormatMode::kSFloat));
239 EXPECT_FALSE(Type::IsSignedInt(FormatMode::kUFloat));
240 }
241
TEST_F(TypeTest,IsUnsignedInt)242 TEST_F(TypeTest, IsUnsignedInt) {
243 EXPECT_FALSE(Type::IsUnsignedInt(FormatMode::kSInt));
244 EXPECT_FALSE(Type::IsUnsignedInt(FormatMode::kSNorm));
245 EXPECT_FALSE(Type::IsUnsignedInt(FormatMode::kSScaled));
246 EXPECT_TRUE(Type::IsUnsignedInt(FormatMode::kSRGB));
247 EXPECT_TRUE(Type::IsUnsignedInt(FormatMode::kUNorm));
248 EXPECT_TRUE(Type::IsUnsignedInt(FormatMode::kUInt));
249 EXPECT_TRUE(Type::IsUnsignedInt(FormatMode::kUScaled));
250 EXPECT_FALSE(Type::IsUnsignedInt(FormatMode::kSFloat));
251 EXPECT_FALSE(Type::IsUnsignedInt(FormatMode::kUFloat));
252 }
253
TEST_F(TypeTest,IsFloat)254 TEST_F(TypeTest, IsFloat) {
255 EXPECT_FALSE(Type::IsFloat(FormatMode::kSInt));
256 EXPECT_FALSE(Type::IsFloat(FormatMode::kSNorm));
257 EXPECT_FALSE(Type::IsFloat(FormatMode::kSScaled));
258 EXPECT_FALSE(Type::IsFloat(FormatMode::kSRGB));
259 EXPECT_FALSE(Type::IsFloat(FormatMode::kUNorm));
260 EXPECT_FALSE(Type::IsFloat(FormatMode::kUInt));
261 EXPECT_FALSE(Type::IsFloat(FormatMode::kUScaled));
262 EXPECT_TRUE(Type::IsFloat(FormatMode::kSFloat));
263 EXPECT_TRUE(Type::IsFloat(FormatMode::kUFloat));
264 }
265
TEST_F(TypeTest,IsInt8)266 TEST_F(TypeTest, IsInt8) {
267 EXPECT_TRUE(Type::IsInt8(FormatMode::kSInt, 8));
268 EXPECT_TRUE(Type::IsInt8(FormatMode::kSNorm, 8));
269 EXPECT_TRUE(Type::IsInt8(FormatMode::kSScaled, 8));
270 EXPECT_FALSE(Type::IsInt8(FormatMode::kSRGB, 8));
271 EXPECT_FALSE(Type::IsInt8(FormatMode::kUNorm, 8));
272 EXPECT_FALSE(Type::IsInt8(FormatMode::kUInt, 8));
273 EXPECT_FALSE(Type::IsInt8(FormatMode::kUScaled, 8));
274 EXPECT_FALSE(Type::IsInt8(FormatMode::kUFloat, 8));
275 EXPECT_FALSE(Type::IsInt8(FormatMode::kSFloat, 8));
276 }
277
TEST_F(TypeTest,IsInt16)278 TEST_F(TypeTest, IsInt16) {
279 EXPECT_TRUE(Type::IsInt16(FormatMode::kSInt, 16));
280 EXPECT_TRUE(Type::IsInt16(FormatMode::kSNorm, 16));
281 EXPECT_TRUE(Type::IsInt16(FormatMode::kSScaled, 16));
282 EXPECT_FALSE(Type::IsInt16(FormatMode::kSRGB, 16));
283 EXPECT_FALSE(Type::IsInt16(FormatMode::kUNorm, 16));
284 EXPECT_FALSE(Type::IsInt16(FormatMode::kUInt, 16));
285 EXPECT_FALSE(Type::IsInt16(FormatMode::kUScaled, 16));
286 EXPECT_FALSE(Type::IsInt16(FormatMode::kUFloat, 16));
287 EXPECT_FALSE(Type::IsInt16(FormatMode::kSFloat, 16));
288 }
289
TEST_F(TypeTest,IsInt32)290 TEST_F(TypeTest, IsInt32) {
291 EXPECT_TRUE(Type::IsInt32(FormatMode::kSInt, 32));
292 EXPECT_TRUE(Type::IsInt32(FormatMode::kSNorm, 32));
293 EXPECT_TRUE(Type::IsInt32(FormatMode::kSScaled, 32));
294 EXPECT_FALSE(Type::IsInt32(FormatMode::kSRGB, 32));
295 EXPECT_FALSE(Type::IsInt32(FormatMode::kUNorm, 32));
296 EXPECT_FALSE(Type::IsInt32(FormatMode::kUInt, 32));
297 EXPECT_FALSE(Type::IsInt32(FormatMode::kUScaled, 32));
298 EXPECT_FALSE(Type::IsInt32(FormatMode::kUFloat, 32));
299 EXPECT_FALSE(Type::IsInt32(FormatMode::kSFloat, 32));
300 }
301
TEST_F(TypeTest,IsInt64)302 TEST_F(TypeTest, IsInt64) {
303 EXPECT_TRUE(Type::IsInt64(FormatMode::kSInt, 64));
304 EXPECT_TRUE(Type::IsInt64(FormatMode::kSNorm, 64));
305 EXPECT_TRUE(Type::IsInt64(FormatMode::kSScaled, 64));
306 EXPECT_FALSE(Type::IsInt64(FormatMode::kSRGB, 64));
307 EXPECT_FALSE(Type::IsInt64(FormatMode::kUNorm, 64));
308 EXPECT_FALSE(Type::IsInt64(FormatMode::kUInt, 64));
309 EXPECT_FALSE(Type::IsInt64(FormatMode::kUScaled, 64));
310 EXPECT_FALSE(Type::IsInt64(FormatMode::kUFloat, 64));
311 EXPECT_FALSE(Type::IsInt64(FormatMode::kSFloat, 64));
312 }
313
TEST_F(TypeTest,IsUint8)314 TEST_F(TypeTest, IsUint8) {
315 EXPECT_FALSE(Type::IsUint8(FormatMode::kSInt, 8));
316 EXPECT_FALSE(Type::IsUint8(FormatMode::kSNorm, 8));
317 EXPECT_FALSE(Type::IsUint8(FormatMode::kSScaled, 8));
318 EXPECT_TRUE(Type::IsUint8(FormatMode::kSRGB, 8));
319 EXPECT_TRUE(Type::IsUint8(FormatMode::kUNorm, 8));
320 EXPECT_TRUE(Type::IsUint8(FormatMode::kUInt, 8));
321 EXPECT_TRUE(Type::IsUint8(FormatMode::kUScaled, 8));
322 EXPECT_FALSE(Type::IsUint8(FormatMode::kUFloat, 8));
323 EXPECT_FALSE(Type::IsUint8(FormatMode::kSFloat, 8));
324 }
325
TEST_F(TypeTest,IsUint16)326 TEST_F(TypeTest, IsUint16) {
327 EXPECT_FALSE(Type::IsUint16(FormatMode::kSInt, 16));
328 EXPECT_FALSE(Type::IsUint16(FormatMode::kSNorm, 16));
329 EXPECT_FALSE(Type::IsUint16(FormatMode::kSScaled, 16));
330 EXPECT_TRUE(Type::IsUint16(FormatMode::kSRGB, 16));
331 EXPECT_TRUE(Type::IsUint16(FormatMode::kUNorm, 16));
332 EXPECT_TRUE(Type::IsUint16(FormatMode::kUInt, 16));
333 EXPECT_TRUE(Type::IsUint16(FormatMode::kUScaled, 16));
334 EXPECT_FALSE(Type::IsUint16(FormatMode::kUFloat, 16));
335 EXPECT_FALSE(Type::IsUint16(FormatMode::kSFloat, 16));
336 }
337
TEST_F(TypeTest,IsUint32)338 TEST_F(TypeTest, IsUint32) {
339 EXPECT_FALSE(Type::IsUint32(FormatMode::kSInt, 32));
340 EXPECT_FALSE(Type::IsUint32(FormatMode::kSNorm, 32));
341 EXPECT_FALSE(Type::IsUint32(FormatMode::kSScaled, 32));
342 EXPECT_TRUE(Type::IsUint32(FormatMode::kSRGB, 32));
343 EXPECT_TRUE(Type::IsUint32(FormatMode::kUNorm, 32));
344 EXPECT_TRUE(Type::IsUint32(FormatMode::kUInt, 32));
345 EXPECT_TRUE(Type::IsUint32(FormatMode::kUScaled, 32));
346 EXPECT_FALSE(Type::IsUint32(FormatMode::kUFloat, 32));
347 EXPECT_FALSE(Type::IsUint32(FormatMode::kSFloat, 32));
348 }
349
TEST_F(TypeTest,IsUint64)350 TEST_F(TypeTest, IsUint64) {
351 EXPECT_FALSE(Type::IsUint64(FormatMode::kSInt, 64));
352 EXPECT_FALSE(Type::IsUint64(FormatMode::kSNorm, 64));
353 EXPECT_FALSE(Type::IsUint64(FormatMode::kSScaled, 64));
354 EXPECT_TRUE(Type::IsUint64(FormatMode::kSRGB, 64));
355 EXPECT_TRUE(Type::IsUint64(FormatMode::kUNorm, 64));
356 EXPECT_TRUE(Type::IsUint64(FormatMode::kUInt, 64));
357 EXPECT_TRUE(Type::IsUint64(FormatMode::kUScaled, 64));
358 EXPECT_FALSE(Type::IsUint64(FormatMode::kUFloat, 64));
359 EXPECT_FALSE(Type::IsUint64(FormatMode::kSFloat, 64));
360 }
361
TEST_F(TypeTest,IsFloat16)362 TEST_F(TypeTest, IsFloat16) {
363 EXPECT_TRUE(Type::IsFloat16(FormatMode::kSFloat, 16));
364 EXPECT_TRUE(Type::IsFloat16(FormatMode::kUFloat, 16));
365 EXPECT_FALSE(Type::IsFloat16(FormatMode::kSInt, 16));
366 EXPECT_FALSE(Type::IsFloat16(FormatMode::kSNorm, 16));
367 EXPECT_FALSE(Type::IsFloat16(FormatMode::kSScaled, 16));
368 EXPECT_FALSE(Type::IsFloat16(FormatMode::kSRGB, 16));
369 EXPECT_FALSE(Type::IsFloat16(FormatMode::kUNorm, 16));
370 EXPECT_FALSE(Type::IsFloat16(FormatMode::kUInt, 16));
371 EXPECT_FALSE(Type::IsFloat16(FormatMode::kUScaled, 16));
372 }
373
TEST_F(TypeTest,IsFloat32)374 TEST_F(TypeTest, IsFloat32) {
375 EXPECT_TRUE(Type::IsFloat32(FormatMode::kSFloat, 32));
376 EXPECT_TRUE(Type::IsFloat32(FormatMode::kUFloat, 32));
377 EXPECT_FALSE(Type::IsFloat32(FormatMode::kSInt, 32));
378 EXPECT_FALSE(Type::IsFloat32(FormatMode::kSNorm, 32));
379 EXPECT_FALSE(Type::IsFloat32(FormatMode::kSScaled, 32));
380 EXPECT_FALSE(Type::IsFloat32(FormatMode::kSRGB, 32));
381 EXPECT_FALSE(Type::IsFloat32(FormatMode::kUNorm, 32));
382 EXPECT_FALSE(Type::IsFloat32(FormatMode::kUInt, 32));
383 EXPECT_FALSE(Type::IsFloat32(FormatMode::kUScaled, 32));
384 }
385
TEST_F(TypeTest,IsFloat64)386 TEST_F(TypeTest, IsFloat64) {
387 EXPECT_TRUE(Type::IsFloat64(FormatMode::kSFloat, 64));
388 EXPECT_TRUE(Type::IsFloat64(FormatMode::kUFloat, 64));
389 EXPECT_FALSE(Type::IsFloat64(FormatMode::kSInt, 64));
390 EXPECT_FALSE(Type::IsFloat64(FormatMode::kSNorm, 64));
391 EXPECT_FALSE(Type::IsFloat64(FormatMode::kSScaled, 64));
392 EXPECT_FALSE(Type::IsFloat64(FormatMode::kSRGB, 64));
393 EXPECT_FALSE(Type::IsFloat64(FormatMode::kUNorm, 64));
394 EXPECT_FALSE(Type::IsFloat64(FormatMode::kUInt, 64));
395 EXPECT_FALSE(Type::IsFloat64(FormatMode::kUScaled, 64));
396 }
397
398 } // namespace type
399 } // namespace amber
400