dawn-cmake/src/ast/module_test.cc
Ben Clayton a6b9a8eb2f Split Program into Program and ProgramBuilder
Program is now immutable*, and remains part of the public Tint
interface.

ProgramBuilder is the mutable builder for Programs, and is not part of
the public Tint interface. ast::Builder has been folded into
ProgramBuilder.

Immutable Programs can be cloned into a mutable ProgramBuilder with
Program::CloneAsBuilder().

Mutable ProgramBuilders can be moved into immutable Programs.

* - mostly immutable. It still has a move constructor and move
  assignment operator - required for practical usage - and the
  semantic information on AST nodes is still mutable.

Bug: tint:390
Change-Id: Ia856c50b1880c2f95c91467a9eef5024cbc380c6
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38240
Reviewed-by: dan sinclair <dsinclair@chromium.org>
2021-01-26 16:57:10 +00:00

140 lines
4.0 KiB
C++

// 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"
#include <sstream>
#include <utility>
#include "gmock/gmock.h"
#include "src/ast/function.h"
#include "src/ast/test_helper.h"
#include "src/ast/variable.h"
#include "src/type/alias_type.h"
#include "src/type/f32_type.h"
#include "src/type/struct_type.h"
namespace tint {
namespace ast {
namespace {
using ModuleTest = TestHelper;
TEST_F(ModuleTest, Creation) {
EXPECT_EQ(Program(std::move(*this)).AST().Functions().size(), 0u);
}
TEST_F(ModuleTest, ToStrEmitsPreambleAndPostamble) {
const auto str = Program(std::move(*this)).AST().to_str();
auto* const expected = "Module{\n}\n";
EXPECT_EQ(str, expected);
}
TEST_F(ModuleTest, LookupFunction) {
auto* func = Func("main", VariableList{}, ty.f32(), StatementList{},
ast::FunctionDecorationList{});
AST().Functions().Add(func);
Program program(std::move(*this));
EXPECT_EQ(func,
program.AST().Functions().Find(program.Symbols().Get("main")));
}
TEST_F(ModuleTest, LookupFunctionMissing) {
Program program(std::move(*this));
EXPECT_EQ(nullptr,
program.AST().Functions().Find(program.Symbols().Get("Missing")));
}
TEST_F(ModuleTest, IsValid_Empty) {
Program program(std::move(*this));
EXPECT_TRUE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_GlobalVariable) {
auto* var = Var("var", StorageClass::kInput, ty.f32());
AST().AddGlobalVariable(var);
Program program(std::move(*this));
EXPECT_TRUE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_Null_GlobalVariable) {
AST().AddGlobalVariable(nullptr);
Program program(std::move(*this));
EXPECT_FALSE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_Invalid_GlobalVariable) {
auto* var = Var("var", StorageClass::kInput, nullptr);
AST().AddGlobalVariable(var);
Program program(std::move(*this));
EXPECT_FALSE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_Alias) {
auto* alias = ty.alias("alias", ty.f32());
AST().AddConstructedType(alias);
Program program(std::move(*this));
EXPECT_TRUE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_Null_Alias) {
AST().AddConstructedType(nullptr);
Program program(std::move(*this));
EXPECT_FALSE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_Struct) {
auto* st = ty.struct_("name", {});
auto* alias = ty.alias("name", st);
AST().AddConstructedType(alias);
Program program(std::move(*this));
EXPECT_TRUE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_Struct_EmptyName) {
auto* st = ty.struct_("", {});
auto* alias = ty.alias("name", st);
AST().AddConstructedType(alias);
Program program(std::move(*this));
EXPECT_FALSE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_Function) {
auto* func = Func("main", VariableList(), ty.f32(), StatementList{},
ast::FunctionDecorationList{});
AST().Functions().Add(func);
Program program(std::move(*this));
EXPECT_TRUE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_Null_Function) {
AST().Functions().Add(nullptr);
Program program(std::move(*this));
EXPECT_FALSE(program.AST().IsValid());
}
TEST_F(ModuleTest, IsValid_Invalid_Function) {
auto* func = Func("main", VariableList{}, nullptr, StatementList{},
ast::FunctionDecorationList{});
AST().Functions().Add(func);
Program program(std::move(*this));
EXPECT_FALSE(program.AST().IsValid());
}
} // namespace
} // namespace ast
} // namespace tint