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/struct.h"
16 #include "gtest/gtest-spi.h"
17 #include "src/ast/alias.h"
18 #include "src/ast/array.h"
19 #include "src/ast/bool.h"
20 #include "src/ast/f32.h"
21 #include "src/ast/i32.h"
22 #include "src/ast/matrix.h"
23 #include "src/ast/pointer.h"
24 #include "src/ast/sampler.h"
25 #include "src/ast/struct_block_decoration.h"
26 #include "src/ast/test_helper.h"
27 #include "src/ast/texture.h"
28 #include "src/ast/u32.h"
29 #include "src/ast/vector.h"
30
31 namespace tint {
32 namespace ast {
33 namespace {
34
35 using AstStructTest = TestHelper;
36
TEST_F(AstStructTest,Creation)37 TEST_F(AstStructTest, Creation) {
38 auto name = Sym("s");
39 auto* s = create<Struct>(name, StructMemberList{Member("a", ty.i32())},
40 DecorationList{});
41 EXPECT_EQ(s->name, name);
42 EXPECT_EQ(s->members.size(), 1u);
43 EXPECT_TRUE(s->decorations.empty());
44 EXPECT_EQ(s->source.range.begin.line, 0u);
45 EXPECT_EQ(s->source.range.begin.column, 0u);
46 EXPECT_EQ(s->source.range.end.line, 0u);
47 EXPECT_EQ(s->source.range.end.column, 0u);
48 }
49
TEST_F(AstStructTest,Creation_WithDecorations)50 TEST_F(AstStructTest, Creation_WithDecorations) {
51 auto name = Sym("s");
52 DecorationList decos;
53 decos.push_back(create<StructBlockDecoration>());
54
55 auto* s =
56 create<Struct>(name, StructMemberList{Member("a", ty.i32())}, decos);
57 EXPECT_EQ(s->name, name);
58 EXPECT_EQ(s->members.size(), 1u);
59 ASSERT_EQ(s->decorations.size(), 1u);
60 EXPECT_TRUE(s->decorations[0]->Is<StructBlockDecoration>());
61 EXPECT_EQ(s->source.range.begin.line, 0u);
62 EXPECT_EQ(s->source.range.begin.column, 0u);
63 EXPECT_EQ(s->source.range.end.line, 0u);
64 EXPECT_EQ(s->source.range.end.column, 0u);
65 }
66
TEST_F(AstStructTest,CreationWithSourceAndDecorations)67 TEST_F(AstStructTest, CreationWithSourceAndDecorations) {
68 auto name = Sym("s");
69 auto* s = create<Struct>(
70 Source{Source::Range{Source::Location{27, 4}, Source::Location{27, 8}}},
71 name, StructMemberList{Member("a", ty.i32())},
72 DecorationList{create<StructBlockDecoration>()});
73 EXPECT_EQ(s->name, name);
74 EXPECT_EQ(s->members.size(), 1u);
75 ASSERT_EQ(s->decorations.size(), 1u);
76 EXPECT_TRUE(s->decorations[0]->Is<StructBlockDecoration>());
77 EXPECT_EQ(s->source.range.begin.line, 27u);
78 EXPECT_EQ(s->source.range.begin.column, 4u);
79 EXPECT_EQ(s->source.range.end.line, 27u);
80 EXPECT_EQ(s->source.range.end.column, 8u);
81 }
82
TEST_F(AstStructTest,Assert_Null_StructMember)83 TEST_F(AstStructTest, Assert_Null_StructMember) {
84 EXPECT_FATAL_FAILURE(
85 {
86 ProgramBuilder b;
87 b.create<Struct>(b.Sym("S"),
88 StructMemberList{b.Member("a", b.ty.i32()), nullptr},
89 DecorationList{});
90 },
91 "internal compiler error");
92 }
93
TEST_F(AstStructTest,Assert_Null_Decoration)94 TEST_F(AstStructTest, Assert_Null_Decoration) {
95 EXPECT_FATAL_FAILURE(
96 {
97 ProgramBuilder b;
98 b.create<Struct>(b.Sym("S"),
99 StructMemberList{b.Member("a", b.ty.i32())},
100 DecorationList{nullptr});
101 },
102 "internal compiler error");
103 }
104
TEST_F(AstStructTest,Assert_DifferentProgramID_StructMember)105 TEST_F(AstStructTest, Assert_DifferentProgramID_StructMember) {
106 EXPECT_FATAL_FAILURE(
107 {
108 ProgramBuilder b1;
109 ProgramBuilder b2;
110 b1.create<Struct>(b1.Sym("S"),
111 StructMemberList{b2.Member("a", b2.ty.i32())},
112 DecorationList{});
113 },
114 "internal compiler error");
115 }
116
TEST_F(AstStructTest,Assert_DifferentProgramID_Decoration)117 TEST_F(AstStructTest, Assert_DifferentProgramID_Decoration) {
118 EXPECT_FATAL_FAILURE(
119 {
120 ProgramBuilder b1;
121 ProgramBuilder b2;
122 b1.create<Struct>(b1.Sym("S"),
123 StructMemberList{b1.Member("a", b1.ty.i32())},
124 DecorationList{b2.create<StructBlockDecoration>()});
125 },
126 "internal compiler error");
127 }
128
129 } // namespace
130 } // namespace ast
131 } // namespace tint
132