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/sem/test_helper.h"
16 #include "src/sem/texture_type.h"
17
18 namespace tint {
19 namespace sem {
20 namespace {
21
22 using ArrayTest = TestHelper;
23
TEST_F(ArrayTest,CreateSizedArray)24 TEST_F(ArrayTest, CreateSizedArray) {
25 U32 u32;
26 auto* arr = create<Array>(&u32, 2, 4, 8, 32, 16);
27 EXPECT_EQ(arr->ElemType(), &u32);
28 EXPECT_EQ(arr->Count(), 2u);
29 EXPECT_EQ(arr->Align(), 4u);
30 EXPECT_EQ(arr->Size(), 8u);
31 EXPECT_EQ(arr->Stride(), 32u);
32 EXPECT_EQ(arr->ImplicitStride(), 16u);
33 EXPECT_FALSE(arr->IsStrideImplicit());
34 EXPECT_FALSE(arr->IsRuntimeSized());
35 }
36
TEST_F(ArrayTest,CreateRuntimeArray)37 TEST_F(ArrayTest, CreateRuntimeArray) {
38 U32 u32;
39 auto* arr = create<Array>(&u32, 0, 4, 8, 32, 32);
40 EXPECT_EQ(arr->ElemType(), &u32);
41 EXPECT_EQ(arr->Count(), 0u);
42 EXPECT_EQ(arr->Align(), 4u);
43 EXPECT_EQ(arr->Size(), 8u);
44 EXPECT_EQ(arr->Stride(), 32u);
45 EXPECT_EQ(arr->ImplicitStride(), 32u);
46 EXPECT_TRUE(arr->IsStrideImplicit());
47 EXPECT_TRUE(arr->IsRuntimeSized());
48 }
49
TEST_F(ArrayTest,TypeName)50 TEST_F(ArrayTest, TypeName) {
51 I32 i32;
52 auto* arr = create<Array>(&i32, 2, 0, 4, 4, 4);
53 EXPECT_EQ(arr->type_name(), "__array__i32_count_2_align_0_size_4_stride_4");
54 }
55
TEST_F(ArrayTest,FriendlyNameRuntimeSized)56 TEST_F(ArrayTest, FriendlyNameRuntimeSized) {
57 auto* arr = create<Array>(create<I32>(), 0, 0, 4, 4, 4);
58 EXPECT_EQ(arr->FriendlyName(Symbols()), "array<i32>");
59 }
60
TEST_F(ArrayTest,FriendlyNameStaticSized)61 TEST_F(ArrayTest, FriendlyNameStaticSized) {
62 auto* arr = create<Array>(create<I32>(), 5, 4, 20, 4, 4);
63 EXPECT_EQ(arr->FriendlyName(Symbols()), "array<i32, 5>");
64 }
65
TEST_F(ArrayTest,FriendlyNameRuntimeSizedNonImplicitStride)66 TEST_F(ArrayTest, FriendlyNameRuntimeSizedNonImplicitStride) {
67 auto* arr = create<Array>(create<I32>(), 0, 0, 4, 8, 4);
68 EXPECT_EQ(arr->FriendlyName(Symbols()), "[[stride(8)]] array<i32>");
69 }
70
TEST_F(ArrayTest,FriendlyNameStaticSizedNonImplicitStride)71 TEST_F(ArrayTest, FriendlyNameStaticSizedNonImplicitStride) {
72 auto* arr = create<Array>(create<I32>(), 5, 4, 20, 8, 4);
73 EXPECT_EQ(arr->FriendlyName(Symbols()), "[[stride(8)]] array<i32, 5>");
74 }
75
TEST_F(ArrayTest,TypeName_RuntimeArray)76 TEST_F(ArrayTest, TypeName_RuntimeArray) {
77 I32 i32;
78 auto* arr = create<Array>(&i32, 2, 4, 8, 16, 16);
79 EXPECT_EQ(arr->type_name(), "__array__i32_count_2_align_4_size_8_stride_16");
80 }
81
82 } // namespace
83 } // namespace sem
84 } // namespace tint
85