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/entry_point.h"
|
|
|
|
#include "src/ast/function.h"
|
|
|
|
#include "src/ast/import.h"
|
|
|
|
#include "src/ast/type/f32_type.h"
|
|
|
|
#include "src/ast/variable.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
|
|
|
|
|
|
|
using ModuleTest = testing::Test;
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, Creation) {
|
|
|
|
Module m;
|
|
|
|
|
2020-04-17 13:18:20 +00:00
|
|
|
EXPECT_EQ(m.imports().size(), 0u);
|
2020-03-02 20:47:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-18 20:09:44 +00:00
|
|
|
TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) {
|
|
|
|
Module m;
|
|
|
|
const auto str = m.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-03-02 20:47:43 +00:00
|
|
|
TEST_F(ModuleTest, Imports) {
|
|
|
|
Module m;
|
|
|
|
|
|
|
|
m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
|
|
|
|
m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug"));
|
|
|
|
|
2020-04-17 13:18:20 +00:00
|
|
|
EXPECT_EQ(2u, m.imports().size());
|
2020-03-02 20:47:43 +00:00
|
|
|
EXPECT_EQ("std::glsl", m.imports()[0]->name());
|
|
|
|
}
|
|
|
|
|
2020-03-18 20:09:44 +00:00
|
|
|
TEST_F(ModuleTest, ToStrWithImport) {
|
|
|
|
Module m;
|
|
|
|
m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
|
|
|
|
const auto str = m.to_str();
|
|
|
|
EXPECT_EQ(str, R"(Module{
|
|
|
|
Import{"GLSL.std.430" as std::glsl}
|
|
|
|
}
|
|
|
|
)");
|
|
|
|
}
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
TEST_F(ModuleTest, LookupImport) {
|
|
|
|
Module m;
|
|
|
|
|
|
|
|
auto i = std::make_unique<Import>("GLSL.std.430", "std::glsl");
|
|
|
|
m.AddImport(std::move(i));
|
|
|
|
m.AddImport(std::make_unique<Import>("OpenCL.debug.100", "std::debug"));
|
|
|
|
|
2020-04-17 13:18:20 +00:00
|
|
|
auto* import = m.FindImportByName("std::glsl");
|
2020-03-02 20:47:43 +00:00
|
|
|
ASSERT_NE(nullptr, import);
|
|
|
|
EXPECT_EQ(import->path(), "GLSL.std.430");
|
|
|
|
EXPECT_EQ(import->name(), "std::glsl");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, LookupImportMissing) {
|
|
|
|
Module m;
|
|
|
|
EXPECT_EQ(nullptr, m.FindImportByName("Missing"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Empty) {
|
|
|
|
Module m;
|
|
|
|
EXPECT_TRUE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:17:52 +00:00
|
|
|
TEST_F(ModuleTest, IsValid_Import) {
|
|
|
|
Module m;
|
|
|
|
m.AddImport(std::make_unique<Import>("GLSL.std.430", "std::glsl"));
|
|
|
|
EXPECT_TRUE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Null_Import) {
|
|
|
|
Module m;
|
|
|
|
m.AddImport(nullptr);
|
|
|
|
EXPECT_FALSE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Invalid_Import) {
|
2020-03-02 20:47:43 +00:00
|
|
|
Module m;
|
|
|
|
m.AddImport(std::make_unique<Import>());
|
|
|
|
EXPECT_FALSE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:17:52 +00:00
|
|
|
TEST_F(ModuleTest, IsValid_GlobalVariable) {
|
|
|
|
type::F32Type f32;
|
|
|
|
auto var = std::make_unique<Variable>("var", StorageClass::kInput, &f32);
|
|
|
|
|
2020-03-02 20:47:43 +00:00
|
|
|
Module m;
|
2020-03-16 14:17:52 +00:00
|
|
|
m.AddGlobalVariable(std::move(var));
|
|
|
|
EXPECT_TRUE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Null_GlobalVariable) {
|
|
|
|
Module m;
|
|
|
|
m.AddGlobalVariable(nullptr);
|
|
|
|
EXPECT_FALSE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) {
|
|
|
|
auto var = std::make_unique<Variable>("var", StorageClass::kInput, nullptr);
|
|
|
|
|
|
|
|
Module m;
|
|
|
|
m.AddGlobalVariable(std::move(var));
|
|
|
|
EXPECT_FALSE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_EntryPoint) {
|
|
|
|
Module m;
|
|
|
|
m.AddEntryPoint(
|
|
|
|
std::make_unique<EntryPoint>(PipelineStage::kVertex, "main", "vtx_main"));
|
2020-03-02 20:47:43 +00:00
|
|
|
EXPECT_TRUE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:17:52 +00:00
|
|
|
TEST_F(ModuleTest, IsValid_Null_EntryPoint) {
|
|
|
|
Module m;
|
|
|
|
m.AddEntryPoint(nullptr);
|
|
|
|
EXPECT_FALSE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Invalid_EntryPoint) {
|
|
|
|
Module m;
|
|
|
|
m.AddEntryPoint(std::make_unique<EntryPoint>());
|
|
|
|
EXPECT_FALSE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Alias) {
|
|
|
|
type::F32Type f32;
|
|
|
|
type::AliasType alias("alias", &f32);
|
|
|
|
|
|
|
|
Module m;
|
|
|
|
m.AddAliasType(&alias);
|
|
|
|
EXPECT_TRUE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Null_Alias) {
|
|
|
|
Module m;
|
|
|
|
m.AddAliasType(nullptr);
|
|
|
|
EXPECT_FALSE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Function) {
|
|
|
|
type::F32Type f32;
|
2020-04-06 19:37:37 +00:00
|
|
|
auto func = std::make_unique<Function>("main", VariableList(), &f32);
|
2020-03-16 14:17:52 +00:00
|
|
|
|
|
|
|
Module m;
|
|
|
|
m.AddFunction(std::move(func));
|
|
|
|
EXPECT_TRUE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Null_Function) {
|
|
|
|
Module m;
|
|
|
|
m.AddFunction(nullptr);
|
|
|
|
EXPECT_FALSE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ModuleTest, IsValid_Invalid_Function) {
|
|
|
|
auto func = std::make_unique<Function>();
|
|
|
|
|
|
|
|
Module m;
|
|
|
|
m.AddFunction(std::move(func));
|
|
|
|
EXPECT_FALSE(m.IsValid());
|
|
|
|
}
|
|
|
|
|
2020-03-26 15:31:43 +00:00
|
|
|
} // namespace
|
2020-03-02 20:47:43 +00:00
|
|
|
} // namespace ast
|
|
|
|
} // namespace tint
|