blob: 64fd31b2f7af67fc4e431b1e5694d98493815180 [file] [log] [blame]
Dan Sinclair6e581892020-03-02 15:47:43 -05001// 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/module.h"
16
17#include <utility>
18
19#include "gtest/gtest.h"
Dan Sinclair3eaf8272020-03-16 14:17:52 +000020#include "src/ast/entry_point.h"
21#include "src/ast/function.h"
22#include "src/ast/import.h"
23#include "src/ast/type/f32_type.h"
24#include "src/ast/variable.h"
Dan Sinclair6e581892020-03-02 15:47:43 -050025
26namespace tint {
27namespace ast {
28
29using ModuleTest = testing::Test;
30
31TEST_F(ModuleTest, Creation) {
32 Module m;
33
34 EXPECT_EQ(m.imports().size(), 0);
35}
36
37TEST_F(ModuleTest, Imports) {
38 Module m;
39
40 m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
41 m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug"));
42
43 EXPECT_EQ(2, m.imports().size());
44 EXPECT_EQ("std::glsl", m.imports()[0]->name());
45}
46
47TEST_F(ModuleTest, LookupImport) {
48 Module m;
49
50 auto i = std::make_unique<Import>("GLSL.std.430", "std::glsl");
51 m.AddImport(std::move(i));
52 m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug"));
53
54 auto import = m.FindImportByName("std::glsl");
55 ASSERT_NE(nullptr, import);
56 EXPECT_EQ(import->path(), "GLSL.std.430");
57 EXPECT_EQ(import->name(), "std::glsl");
58}
59
60TEST_F(ModuleTest, LookupImportMissing) {
61 Module m;
62 EXPECT_EQ(nullptr, m.FindImportByName("Missing"));
63}
64
65TEST_F(ModuleTest, IsValid_Empty) {
66 Module m;
67 EXPECT_TRUE(m.IsValid());
68}
69
Dan Sinclair3eaf8272020-03-16 14:17:52 +000070TEST_F(ModuleTest, IsValid_Import) {
71 Module m;
72 m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
73 EXPECT_TRUE(m.IsValid());
74}
75
76TEST_F(ModuleTest, IsValid_Null_Import) {
77 Module m;
78 m.AddImport(nullptr);
79 EXPECT_FALSE(m.IsValid());
80}
81
82TEST_F(ModuleTest, IsValid_Invalid_Import) {
Dan Sinclair6e581892020-03-02 15:47:43 -050083 Module m;
84 m.AddImport(std::make_unique<Import>());
85 EXPECT_FALSE(m.IsValid());
86}
87
Dan Sinclair3eaf8272020-03-16 14:17:52 +000088TEST_F(ModuleTest, IsValid_GlobalVariable) {
89 type::F32Type f32;
90 auto var = std::make_unique<Variable>("var", StorageClass::kInput, &f32);
91
Dan Sinclair6e581892020-03-02 15:47:43 -050092 Module m;
Dan Sinclair3eaf8272020-03-16 14:17:52 +000093 m.AddGlobalVariable(std::move(var));
Dan Sinclair6e581892020-03-02 15:47:43 -050094 EXPECT_TRUE(m.IsValid());
95}
96
Dan Sinclair3eaf8272020-03-16 14:17:52 +000097TEST_F(ModuleTest, IsValid_Null_GlobalVariable) {
98 Module m;
99 m.AddGlobalVariable(nullptr);
100 EXPECT_FALSE(m.IsValid());
101}
102
103TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) {
104 auto var = std::make_unique<Variable>("var", StorageClass::kInput, nullptr);
105
106 Module m;
107 m.AddGlobalVariable(std::move(var));
108 EXPECT_FALSE(m.IsValid());
109}
110
111TEST_F(ModuleTest, IsValid_EntryPoint) {
112 Module m;
113 m.AddEntryPoint(
114 std::make_unique<EntryPoint>(PipelineStage::kVertex, "main", "vtx_main"));
115 EXPECT_TRUE(m.IsValid());
116}
117
118TEST_F(ModuleTest, IsValid_Null_EntryPoint) {
119 Module m;
120 m.AddEntryPoint(nullptr);
121 EXPECT_FALSE(m.IsValid());
122}
123
124TEST_F(ModuleTest, IsValid_Invalid_EntryPoint) {
125 Module m;
126 m.AddEntryPoint(std::make_unique<EntryPoint>());
127 EXPECT_FALSE(m.IsValid());
128}
129
130TEST_F(ModuleTest, IsValid_Alias) {
131 type::F32Type f32;
132 type::AliasType alias("alias", &f32);
133
134 Module m;
135 m.AddAliasType(&alias);
136 EXPECT_TRUE(m.IsValid());
137}
138
139TEST_F(ModuleTest, IsValid_Null_Alias) {
140 Module m;
141 m.AddAliasType(nullptr);
142 EXPECT_FALSE(m.IsValid());
143}
144
145TEST_F(ModuleTest, IsValid_Function) {
146 type::F32Type f32;
147 auto func = std::make_unique<Function>(
148 "main", std::vector<std::unique_ptr<Variable>>(), &f32);
149
150 Module m;
151 m.AddFunction(std::move(func));
152 EXPECT_TRUE(m.IsValid());
153}
154
155TEST_F(ModuleTest, IsValid_Null_Function) {
156 Module m;
157 m.AddFunction(nullptr);
158 EXPECT_FALSE(m.IsValid());
159}
160
161TEST_F(ModuleTest, IsValid_Invalid_Function) {
162 auto func = std::make_unique<Function>();
163
164 Module m;
165 m.AddFunction(std::move(func));
166 EXPECT_FALSE(m.IsValid());
167}
168
Dan Sinclair6e581892020-03-02 15:47:43 -0500169} // namespace ast
170} // namespace tint