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/array.h"
16
17 #include "src/ast/test_helper.h"
18
19 namespace tint {
20 namespace ast {
21 namespace {
22
23 using AstArrayTest = TestHelper;
24
TEST_F(AstArrayTest,CreateSizedArray)25 TEST_F(AstArrayTest, CreateSizedArray) {
26 auto* u32 = create<U32>();
27 auto* count = Expr(3);
28 auto* arr = create<Array>(u32, count, DecorationList{});
29 EXPECT_EQ(arr->type, u32);
30 EXPECT_EQ(arr->count, count);
31 EXPECT_TRUE(arr->Is<Array>());
32 EXPECT_FALSE(arr->IsRuntimeArray());
33 }
34
TEST_F(AstArrayTest,CreateRuntimeArray)35 TEST_F(AstArrayTest, CreateRuntimeArray) {
36 auto* u32 = create<U32>();
37 auto* arr = create<Array>(u32, nullptr, DecorationList{});
38 EXPECT_EQ(arr->type, u32);
39 EXPECT_EQ(arr->count, nullptr);
40 EXPECT_TRUE(arr->Is<Array>());
41 EXPECT_TRUE(arr->IsRuntimeArray());
42 }
43
TEST_F(AstArrayTest,FriendlyName_RuntimeSized)44 TEST_F(AstArrayTest, FriendlyName_RuntimeSized) {
45 auto* i32 = create<I32>();
46 auto* arr = create<Array>(i32, nullptr, DecorationList{});
47 EXPECT_EQ(arr->FriendlyName(Symbols()), "array<i32>");
48 }
49
TEST_F(AstArrayTest,FriendlyName_LiteralSized)50 TEST_F(AstArrayTest, FriendlyName_LiteralSized) {
51 auto* i32 = create<I32>();
52 auto* arr = create<Array>(i32, Expr(5), DecorationList{});
53 EXPECT_EQ(arr->FriendlyName(Symbols()), "array<i32, 5>");
54 }
55
TEST_F(AstArrayTest,FriendlyName_ConstantSized)56 TEST_F(AstArrayTest, FriendlyName_ConstantSized) {
57 auto* i32 = create<I32>();
58 auto* arr = create<Array>(i32, Expr("size"), DecorationList{});
59 EXPECT_EQ(arr->FriendlyName(Symbols()), "array<i32, size>");
60 }
61
TEST_F(AstArrayTest,FriendlyName_WithStride)62 TEST_F(AstArrayTest, FriendlyName_WithStride) {
63 auto* i32 = create<I32>();
64 auto* arr =
65 create<Array>(i32, Expr(5), DecorationList{create<StrideDecoration>(32)});
66 EXPECT_EQ(arr->FriendlyName(Symbols()), "[[stride(32)]] array<i32, 5>");
67 }
68
69 } // namespace
70 } // namespace ast
71 } // namespace tint
72