More module IsValid tests

Bug: tint:11
Change-Id: I1ce2b930f3dbf40791fa68963037e7b9f122658f
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/16664
Reviewed-by: Sarah Mashayekhi <sarahmashay@google.com>
This commit is contained in:
Dan Sinclair 2020-03-16 14:17:52 +00:00 committed by Sarah Mashayekhi
parent f300756eff
commit 3eaf827cc8
2 changed files with 121 additions and 8 deletions

View File

@ -29,8 +29,29 @@ const Import* Module::FindImportByName(const std::string& name) {
bool Module::IsValid() const {
for (const auto& import : imports_) {
if (!import->IsValid())
if (import == nullptr || !import->IsValid()) {
return false;
}
}
for (const auto& var : global_variables_) {
if (var == nullptr || !var->IsValid()) {
return false;
}
}
for (const auto& ep : entry_points_) {
if (ep == nullptr || !ep->IsValid()) {
return false;
}
}
for (const auto& alias : alias_types_) {
if (alias == nullptr) {
return false;
}
}
for (const auto& func : functions_) {
if (func == nullptr || !func->IsValid()) {
return false;
}
}
return true;
}

View File

@ -17,6 +17,11 @@
#include <utility>
#include "gtest/gtest.h"
#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"
namespace tint {
namespace ast {
@ -62,17 +67,104 @@ TEST_F(ModuleTest, IsValid_Empty) {
EXPECT_TRUE(m.IsValid());
}
TEST_F(ModuleTest, IsValid_InvalidImport) {
Module m;
m.AddImport(std::make_unique<Import>());
EXPECT_FALSE(m.IsValid());
}
TEST_F(ModuleTest, IsValid_ValidImport) {
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) {
Module m;
m.AddImport(std::make_unique<Import>());
EXPECT_FALSE(m.IsValid());
}
TEST_F(ModuleTest, IsValid_GlobalVariable) {
type::F32Type f32;
auto var = std::make_unique<Variable>("var", StorageClass::kInput, &f32);
Module m;
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"));
EXPECT_TRUE(m.IsValid());
}
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;
auto func = std::make_unique<Function>(
"main", std::vector<std::unique_ptr<Variable>>(), &f32);
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());
}
} // namespace ast
} // namespace tint