mirror of
https://github.com/encounter/dawn-cmake.git
synced 2025-06-10 00:23:43 +00:00
Make private all TypeDeterminer::DetermineXXX() methods, forcing all tests to use the root-level TypeDeterminer::Determine() method. Remove TypeDeterminer::RegisterVariableForTesting(). The main use for calling the TypeDeterminer::DetermineXXX() methods was to perform type determination on a partial AST. This was messy and often resulting in multiple calls into TypeDeterminer. Most tests already perform a full TypeDeterminer::Determine() call when the program is built, so many of these were redundant. The exposure of these internal methods for testing also makes refactoring the TypeDeterminer extremely difficult. Add a number of ProgramBuilder helper methods for attaching the partial AST in these tests to the root of the AST, greatly simplifying the use of the TypeDeterminer: * ProgramBuilder::Global() and ProgramBuilder::GlobalConst() are helpers that register the variable returned by ProgramBuilder::Var() and ProgramBuilder::Const(), respectively. * ProgramBuilder::WrapInFunction() is a variadic function that accepts variables, expressions and statements, attaching these to the root of the AST via a dummy function. Most test classes now no longer use their own TypeDeterminer, and instead properly depend on the automatic type determination performed at Program build time. Bug: tint:390 Change-Id: Ie901890420c5de170cdf2a7aaef9b96fc3bebd60 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/40062 Commit-Queue: Ben Clayton <bclayton@google.com> Reviewed-by: David Neto <dneto@google.com>
152 lines
4.0 KiB
C++
152 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/program.h"
|
|
|
|
#include <sstream>
|
|
#include <utility>
|
|
|
|
#include "gmock/gmock.h"
|
|
#include "src/ast/function.h"
|
|
#include "src/ast/return_statement.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 {
|
|
|
|
using ProgramTest = ast::TestHelper;
|
|
|
|
TEST_F(ProgramTest, Creation) {
|
|
Program program(std::move(*this));
|
|
EXPECT_EQ(program.AST().Functions().size(), 0u);
|
|
}
|
|
|
|
TEST_F(ProgramTest, ToStrEmitsPreambleAndPostamble) {
|
|
Program program(std::move(*this));
|
|
const auto str = program.to_str();
|
|
auto* const expected = "Module{\n}\n";
|
|
EXPECT_EQ(str, expected);
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Empty) {
|
|
Program program(std::move(*this));
|
|
EXPECT_TRUE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_GlobalVariable) {
|
|
Global("var", ast::StorageClass::kInput, ty.f32());
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_TRUE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Null_GlobalVariable) {
|
|
AST().AddGlobalVariable(nullptr);
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_FALSE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Invalid_GlobalVariable) {
|
|
Global("var", ast::StorageClass::kInput, nullptr);
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_FALSE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Alias) {
|
|
auto* alias = ty.alias("alias", ty.f32());
|
|
AST().AddConstructedType(alias);
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_TRUE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Null_Alias) {
|
|
AST().AddConstructedType(nullptr);
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_FALSE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Struct) {
|
|
auto* st = ty.struct_("name", {});
|
|
auto* alias = ty.alias("name", st);
|
|
AST().AddConstructedType(alias);
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_TRUE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Struct_EmptyName) {
|
|
auto* st = ty.struct_("", {});
|
|
auto* alias = ty.alias("name", st);
|
|
AST().AddConstructedType(alias);
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_FALSE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Function) {
|
|
Func("main", ast::VariableList(), ty.f32(), ast::StatementList{},
|
|
ast::FunctionDecorationList{});
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_TRUE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Null_Function) {
|
|
AST().Functions().Add(nullptr);
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_FALSE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Invalid_Function) {
|
|
Func("main", ast::VariableList{}, nullptr, ast::StatementList{},
|
|
ast::FunctionDecorationList{});
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_FALSE(program.IsValid());
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_Invalid_UnknownVar) {
|
|
Func("main", ast::VariableList{}, nullptr,
|
|
ast::StatementList{
|
|
create<ast::ReturnStatement>(Expr("unknown_ident")),
|
|
},
|
|
ast::FunctionDecorationList{});
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_FALSE(program.IsValid());
|
|
EXPECT_NE(program.Diagnostics().count(), 0u);
|
|
}
|
|
|
|
TEST_F(ProgramTest, IsValid_GeneratesError) {
|
|
AST().AddGlobalVariable(nullptr);
|
|
|
|
Program program(std::move(*this));
|
|
EXPECT_FALSE(program.IsValid());
|
|
EXPECT_EQ(program.Diagnostics().count(), 1u);
|
|
EXPECT_EQ(program.Diagnostics().error_count(), 1u);
|
|
EXPECT_EQ(program.Diagnostics().begin()->message,
|
|
"invalid program generated");
|
|
}
|
|
} // namespace
|
|
} // namespace tint
|