2020-03-02 20:47:43 +00:00
|
|
|
// Copyright 2020 The Tint Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#include "src/ast/module.h"
|
|
|
|
|
2020-03-18 20:09:44 +00:00
|
|
|
#include <sstream>
|
2020-03-02 20:47:43 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2020-03-18 20:09:44 +00:00
|
|
|
#include "gmock/gmock.h"
|
2020-03-16 14:17:52 +00:00
|
|
|
#include "src/ast/function.h"
|
2020-11-13 21:58:28 +00:00
|
|
|
#include "src/ast/test_helper.h"
|
2020-03-16 14:17:52 +00:00
|
|
|
#include "src/ast/variable.h"
|
2021-01-21 15:42:10 +00:00
|
|
|
#include "src/type/alias_type.h"
|
|
|
|
#include "src/type/f32_type.h"
|
|
|
|
#include "src/type/struct_type.h"
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
namespace tint {
|
|
|
|
namespace ast {
|
2020-03-26 15:31:43 +00:00
|
|
|
namespace {
|
2020-03-02 20:47:43 +00:00
|
|
|
|
2020-11-13 21:58:28 +00:00
|
|
|
using ModuleTest = TestHelper;
|
2020-03-02 20:47:43 +00:00
|
|
|
|
|
|
|
TEST_F(ModuleTest, Creation) {
|
2020-12-16 21:38:40 +00:00
|
|
|
EXPECT_EQ(mod->functions().size(), 0u);
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 20:09:44 +00:00
|
|
|
TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) {
|
2020-12-16 21:38:40 +00:00
|
|
|
const auto str = mod->to_str();
|
2020-04-17 13:18:20 +00:00
|
|
|
auto* const expected = "Module{\n}\n";
|
2020-03-18 20:09:44 +00:00
|
|
|
EXPECT_EQ(str, expected);
|
|
|
|
}
|
|
|
|
|
2020-07-14 20:37:20 +00:00
|
|
|
TEST_F(ModuleTest, LookupFunction) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* func = Func("main", VariableList{}, ty.f32, StatementList{},
|
|
|
|
ast::FunctionDecorationList{});
|
|
|
|
mod->AddFunction(func);
|
|
|
|
EXPECT_EQ(func, mod->FindFunctionBySymbol(mod->RegisterSymbol("main")));
|
2020-07-14 20:37:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, LookupFunctionMissing) {
|
2020-12-16 21:38:40 +00:00
|
|
|
EXPECT_EQ(nullptr, mod->FindFunctionBySymbol(mod->RegisterSymbol("Missing")));
|
2020-07-14 20:37:20 +00:00
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
TEST_F(ModuleTest, IsValid_Empty) {
|
2020-12-16 21:38:40 +00:00
|
|
|
EXPECT_TRUE(mod->IsValid());
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 14:17:52 +00:00
|
|
|
TEST_F(ModuleTest, IsValid_GlobalVariable) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* var = Var("var", StorageClass::kInput, ty.f32);
|
|
|
|
mod->AddGlobalVariable(var);
|
|
|
|
EXPECT_TRUE(mod->IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Null_GlobalVariable) {
|
2020-12-16 21:38:40 +00:00
|
|
|
mod->AddGlobalVariable(nullptr);
|
|
|
|
EXPECT_FALSE(mod->IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* var = Var("var", StorageClass::kInput, nullptr);
|
|
|
|
mod->AddGlobalVariable(var);
|
|
|
|
EXPECT_FALSE(mod->IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Alias) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* alias = ty.alias("alias", ty.f32);
|
|
|
|
mod->AddConstructedType(alias);
|
|
|
|
EXPECT_TRUE(mod->IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Null_Alias) {
|
2020-12-16 21:38:40 +00:00
|
|
|
mod->AddConstructedType(nullptr);
|
|
|
|
EXPECT_FALSE(mod->IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 17:54:13 +00:00
|
|
|
TEST_F(ModuleTest, IsValid_Struct) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* st = ty.struct_("name", {});
|
|
|
|
auto* alias = ty.alias("name", st);
|
|
|
|
mod->AddConstructedType(alias);
|
|
|
|
EXPECT_TRUE(mod->IsValid());
|
2020-10-15 17:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Struct_EmptyName) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* st = ty.struct_("", {});
|
|
|
|
auto* alias = ty.alias("name", st);
|
|
|
|
mod->AddConstructedType(alias);
|
|
|
|
EXPECT_FALSE(mod->IsValid());
|
2020-10-15 17:54:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 14:17:52 +00:00
|
|
|
TEST_F(ModuleTest, IsValid_Function) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* func = Func("main", VariableList(), ty.f32, StatementList{},
|
|
|
|
ast::FunctionDecorationList{});
|
2020-12-14 20:25:27 +00:00
|
|
|
|
2020-12-16 21:38:40 +00:00
|
|
|
mod->AddFunction(func);
|
|
|
|
EXPECT_TRUE(mod->IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Null_Function) {
|
2020-12-16 21:38:40 +00:00
|
|
|
mod->AddFunction(nullptr);
|
|
|
|
EXPECT_FALSE(mod->IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Invalid_Function) {
|
2020-12-16 21:38:40 +00:00
|
|
|
auto* func = Func("main", VariableList{}, nullptr, StatementList{},
|
|
|
|
ast::FunctionDecorationList{});
|
2020-12-11 18:24:53 +00:00
|
|
|
|
2020-12-16 21:38:40 +00:00
|
|
|
mod->AddFunction(func);
|
|
|
|
EXPECT_FALSE(mod->IsValid());
|
2020-03-16 14:17:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-26 15:31:43 +00:00
|
|
|
} // namespace
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace ast
|
|
|
|
} // namespace tint
|