1 // Copyright 2020 The Tint 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/ast/type/matrix_type.h"
16 
17 #include "gtest/gtest.h"
18 #include "src/ast/type/i32_type.h"
19 
20 namespace tint {
21 namespace ast {
22 namespace type {
23 namespace {
24 
25 using MatrixTypeTest = testing::Test;
26 
TEST_F(MatrixTypeTest,Creation)27 TEST_F(MatrixTypeTest, Creation) {
28   I32Type i32;
29   MatrixType m{&i32, 2, 4};
30   EXPECT_EQ(m.type(), &i32);
31   EXPECT_EQ(m.rows(), 2u);
32   EXPECT_EQ(m.columns(), 4u);
33 }
34 
TEST_F(MatrixTypeTest,Is)35 TEST_F(MatrixTypeTest, Is) {
36   I32Type i32;
37   MatrixType m{&i32, 2, 3};
38   EXPECT_FALSE(m.IsAccessControl());
39   EXPECT_FALSE(m.IsAlias());
40   EXPECT_FALSE(m.IsArray());
41   EXPECT_FALSE(m.IsBool());
42   EXPECT_FALSE(m.IsF32());
43   EXPECT_FALSE(m.IsI32());
44   EXPECT_TRUE(m.IsMatrix());
45   EXPECT_FALSE(m.IsPointer());
46   EXPECT_FALSE(m.IsSampler());
47   EXPECT_FALSE(m.IsStruct());
48   EXPECT_FALSE(m.IsTexture());
49   EXPECT_FALSE(m.IsU32());
50   EXPECT_FALSE(m.IsVector());
51 }
52 
TEST_F(MatrixTypeTest,TypeName)53 TEST_F(MatrixTypeTest, TypeName) {
54   I32Type i32;
55   MatrixType m{&i32, 2, 3};
56   EXPECT_EQ(m.type_name(), "__mat_2_3__i32");
57 }
58 
TEST_F(MatrixTypeTest,MinBufferBindingSize4x2)59 TEST_F(MatrixTypeTest, MinBufferBindingSize4x2) {
60   I32Type i32;
61   MatrixType m{&i32, 4, 2};
62   EXPECT_EQ(32u, m.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
63   EXPECT_EQ(32u, m.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
64 }
65 
TEST_F(MatrixTypeTest,MinBufferBindingSize3x2)66 TEST_F(MatrixTypeTest, MinBufferBindingSize3x2) {
67   I32Type i32;
68   MatrixType m{&i32, 3, 2};
69   EXPECT_EQ(28u, m.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
70   EXPECT_EQ(28u, m.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
71 }
72 
TEST_F(MatrixTypeTest,MinBufferBindingSize2x3)73 TEST_F(MatrixTypeTest, MinBufferBindingSize2x3) {
74   I32Type i32;
75   MatrixType m{&i32, 2, 3};
76   EXPECT_EQ(24u, m.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
77   EXPECT_EQ(24u, m.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
78 }
79 
TEST_F(MatrixTypeTest,MinBufferBindingSize2x2)80 TEST_F(MatrixTypeTest, MinBufferBindingSize2x2) {
81   I32Type i32;
82   MatrixType m{&i32, 2, 2};
83   EXPECT_EQ(16u, m.MinBufferBindingSize(MemoryLayout::kUniformBuffer));
84   EXPECT_EQ(16u, m.MinBufferBindingSize(MemoryLayout::kStorageBuffer));
85 }
86 
TEST_F(MatrixTypeTest,BaseAlignment4x2)87 TEST_F(MatrixTypeTest, BaseAlignment4x2) {
88   I32Type i32;
89   MatrixType m{&i32, 4, 2};
90   EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kUniformBuffer));
91   EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kStorageBuffer));
92 }
93 
TEST_F(MatrixTypeTest,BaseAlignment3x2)94 TEST_F(MatrixTypeTest, BaseAlignment3x2) {
95   I32Type i32;
96   MatrixType m{&i32, 3, 2};
97   EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kUniformBuffer));
98   EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kStorageBuffer));
99 }
100 
TEST_F(MatrixTypeTest,BaseAlignment2x3)101 TEST_F(MatrixTypeTest, BaseAlignment2x3) {
102   I32Type i32;
103   MatrixType m{&i32, 2, 3};
104   EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kUniformBuffer));
105   EXPECT_EQ(8u, m.BaseAlignment(MemoryLayout::kStorageBuffer));
106 }
107 
TEST_F(MatrixTypeTest,BaseAlignment2x2)108 TEST_F(MatrixTypeTest, BaseAlignment2x2) {
109   I32Type i32;
110   MatrixType m{&i32, 2, 2};
111   EXPECT_EQ(16u, m.BaseAlignment(MemoryLayout::kUniformBuffer));
112   EXPECT_EQ(8u, m.BaseAlignment(MemoryLayout::kStorageBuffer));
113 }
114 
115 }  // namespace
116 }  // namespace type
117 }  // namespace ast
118 }  // namespace tint
119