test: Add Build() method for tests using ast::Builder

This separates out the usage of the built module from the construction of the module.

Previously, we'd happily interleave generator testing with module construction statements. Once the AST / Program is made immutable, this will no longer be possible.

Bug: tint:390
Change-Id: Ib4538228e93ca816f5bb796d024f021116609213
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/38360
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton
2021-01-21 16:15:00 +00:00
committed by Commit Bot service account
parent 207b5e2de1
commit f12054ea2a
119 changed files with 2331 additions and 378 deletions

View File

@@ -73,12 +73,14 @@ TEST_F(ValidatorTest, AssignToScalar_Fail) {
auto* assign = create<ast::AssignmentStatement>(lhs, rhs);
RegisterVariable(var);
ValidatorImpl& v = Build();
EXPECT_TRUE(td()->DetermineResultType(assign));
// TODO(sarahM0): Invalidate assignment to scalar.
EXPECT_FALSE(v()->ValidateAssign(assign));
ASSERT_TRUE(v()->has_error());
EXPECT_FALSE(v.ValidateAssign(assign));
ASSERT_TRUE(v.has_error());
// TODO(sarahM0): figure out what should be the error number.
EXPECT_EQ(v()->error(),
EXPECT_EQ(v.error(),
"12:34 v-000x: invalid assignment: left-hand-side does not "
"reference storage: __i32");
}
@@ -129,7 +131,9 @@ TEST_F(ValidatorTest, AssignCompatibleTypes_Pass) {
EXPECT_TRUE(td()->DetermineResultType(assign)) << td()->error();
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_TRUE(v()->ValidateAssign(assign)) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateAssign(assign)) << v.error();
}
TEST_F(ValidatorTest, AssignCompatibleTypesThroughAlias_Pass) {
@@ -149,7 +153,9 @@ TEST_F(ValidatorTest, AssignCompatibleTypesThroughAlias_Pass) {
EXPECT_TRUE(td()->DetermineResultType(assign)) << td()->error();
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_TRUE(v()->ValidateAssign(assign)) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateAssign(assign)) << v.error();
}
TEST_F(ValidatorTest, AssignCompatibleTypesInferRHSLoad_Pass) {
@@ -171,7 +177,9 @@ TEST_F(ValidatorTest, AssignCompatibleTypesInferRHSLoad_Pass) {
EXPECT_TRUE(td()->DetermineResultType(assign)) << td()->error();
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_TRUE(v()->ValidateAssign(assign)) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateAssign(assign)) << v.error();
}
TEST_F(ValidatorTest, AssignThroughPointer_Pass) {
@@ -193,7 +201,10 @@ TEST_F(ValidatorTest, AssignThroughPointer_Pass) {
EXPECT_TRUE(td()->DetermineResultType(assign)) << td()->error();
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_TRUE(v()->ValidateAssign(assign)) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateAssign(assign)) << v.error();
}
TEST_F(ValidatorTest, AssignIncompatibleTypes_Fail) {
@@ -215,10 +226,12 @@ TEST_F(ValidatorTest, AssignIncompatibleTypes_Fail) {
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_FALSE(v()->ValidateAssign(assign));
ASSERT_TRUE(v()->has_error());
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateAssign(assign));
ASSERT_TRUE(v.has_error());
// TODO(sarahM0): figure out what should be the error number.
EXPECT_EQ(v()->error(),
EXPECT_EQ(v.error(),
"12:34 v-000x: invalid assignment: can't assign value of type "
"'__f32' to '__i32'");
}
@@ -242,8 +255,11 @@ TEST_F(ValidatorTest, AssignThroughPointerWrongeStoreType_Fail) {
EXPECT_TRUE(td()->DetermineResultType(assign)) << td()->error();
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_FALSE(v()->ValidateAssign(assign));
EXPECT_EQ(v()->error(),
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateAssign(assign));
EXPECT_EQ(v.error(),
"12:34 v-000x: invalid assignment: can't assign value of type "
"'__i32' to '__f32'");
}
@@ -269,7 +285,9 @@ TEST_F(ValidatorTest, AssignCompatibleTypesInBlockStatement_Pass) {
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_TRUE(v()->ValidateStatements(body)) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateStatements(body)) << v.error();
}
TEST_F(ValidatorTest, AssignIncompatibleTypesInBlockStatement_Fail) {
@@ -294,10 +312,12 @@ TEST_F(ValidatorTest, AssignIncompatibleTypesInBlockStatement_Fail) {
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_FALSE(v()->ValidateStatements(block));
ASSERT_TRUE(v()->has_error());
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateStatements(block));
ASSERT_TRUE(v.has_error());
// TODO(sarahM0): figure out what should be the error number.
EXPECT_EQ(v()->error(),
EXPECT_EQ(v.error(),
"12:34 v-000x: invalid assignment: can't assign value of type "
"'__f32' to '__i32'");
}
@@ -307,8 +327,10 @@ TEST_F(ValidatorTest, GlobalVariableWithStorageClass_Pass) {
mod->AddGlobalVariable(Var(Source{Source::Location{12, 34}}, "global_var",
ast::StorageClass::kInput, ty.f32, nullptr,
ast::VariableDecorationList{}));
EXPECT_TRUE(v()->ValidateGlobalVariables(mod->global_variables()))
<< v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateGlobalVariables(mod->global_variables())) << v.error();
}
TEST_F(ValidatorTest, GlobalVariableNoStorageClass_Fail) {
@@ -317,8 +339,11 @@ TEST_F(ValidatorTest, GlobalVariableNoStorageClass_Fail) {
ast::StorageClass::kNone, ty.f32, nullptr,
ast::VariableDecorationList{}));
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_FALSE(v()->Validate());
EXPECT_EQ(v()->error(),
ValidatorImpl& v = Build();
EXPECT_FALSE(v.Validate());
EXPECT_EQ(v.error(),
"12:34 v-0022: global variables must have a storage class");
}
@@ -328,9 +353,12 @@ TEST_F(ValidatorTest, GlobalConstantWithStorageClass_Fail) {
ast::StorageClass::kInput, ty.f32, nullptr,
ast::VariableDecorationList{}));
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_FALSE(v()->Validate());
ValidatorImpl& v = Build();
EXPECT_FALSE(v.Validate());
EXPECT_EQ(
v()->error(),
v.error(),
"12:34 v-global01: global constants shouldn't have a storage class");
}
@@ -340,7 +368,10 @@ TEST_F(ValidatorTest, GlobalConstNoStorageClass_Pass) {
ast::StorageClass::kNone, ty.f32, nullptr,
ast::VariableDecorationList{}));
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_FALSE(v()->Validate()) << v()->error();
ValidatorImpl& v = Build();
EXPECT_FALSE(v.Validate()) << v.error();
}
TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Fail) {
@@ -363,8 +394,10 @@ TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Fail) {
ast::FunctionDecorationList{});
mod->AddFunction(func);
EXPECT_FALSE(v()->Validate());
EXPECT_EQ(v()->error(), "12:34 v-0006: 'not_global_var' is not declared");
ValidatorImpl& v = Build();
EXPECT_FALSE(v.Validate());
EXPECT_EQ(v.error(), "12:34 v-0006: 'not_global_var' is not declared");
}
TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Pass) {
@@ -390,7 +423,10 @@ TEST_F(ValidatorTest, UsingUndefinedVariableGlobalVariable_Pass) {
mod->AddFunction(func);
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_TRUE(v()->Validate()) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.Validate()) << v.error();
}
TEST_F(ValidatorTest, UsingUndefinedVariableInnerScope_Fail) {
@@ -419,8 +455,11 @@ TEST_F(ValidatorTest, UsingUndefinedVariableInnerScope_Fail) {
EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error();
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_FALSE(v()->ValidateStatements(outer_body));
EXPECT_EQ(v()->error(), "12:34 v-0006: 'a' is not declared");
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateStatements(outer_body));
EXPECT_EQ(v.error(), "12:34 v-0006: 'a' is not declared");
}
TEST_F(ValidatorTest, UsingUndefinedVariableOuterScope_Pass) {
@@ -449,7 +488,10 @@ TEST_F(ValidatorTest, UsingUndefinedVariableOuterScope_Pass) {
EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error();
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_TRUE(v()->ValidateStatements(outer_body)) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateStatements(outer_body)) << v.error();
}
TEST_F(ValidatorTest, GlobalVariableUnique_Pass) {
@@ -464,8 +506,9 @@ TEST_F(ValidatorTest, GlobalVariableUnique_Pass) {
ast::VariableDecorationList{});
mod->AddGlobalVariable(var1);
EXPECT_TRUE(v()->ValidateGlobalVariables(mod->global_variables()))
<< v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateGlobalVariables(mod->global_variables())) << v.error();
}
TEST_F(ValidatorTest, GlobalVariableNotUnique_Fail) {
@@ -480,8 +523,10 @@ TEST_F(ValidatorTest, GlobalVariableNotUnique_Fail) {
ast::VariableDecorationList{});
mod->AddGlobalVariable(var1);
EXPECT_FALSE(v()->ValidateGlobalVariables(mod->global_variables()));
EXPECT_EQ(v()->error(),
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateGlobalVariables(mod->global_variables()));
EXPECT_EQ(v.error(),
"12:34 v-0011: redeclared global identifier 'global_var'");
}
@@ -506,8 +551,10 @@ TEST_F(ValidatorTest, AssignToConstant_Fail) {
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_FALSE(v()->ValidateStatements(body));
EXPECT_EQ(v()->error(), "12:34 v-0021: cannot re-assign a constant: 'a'");
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateStatements(body));
EXPECT_EQ(v.error(), "12:34 v-0021: cannot re-assign a constant: 'a'");
}
TEST_F(ValidatorTest, GlobalVariableFunctionVariableNotUnique_Fail) {
@@ -535,8 +582,11 @@ TEST_F(ValidatorTest, GlobalVariableFunctionVariableNotUnique_Fail) {
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_TRUE(td()->DetermineFunction(func)) << td()->error();
EXPECT_FALSE(v()->Validate()) << v()->error();
EXPECT_EQ(v()->error(), "12:34 v-0013: redeclared identifier 'a'");
ValidatorImpl& v = Build();
EXPECT_FALSE(v.Validate()) << v.error();
EXPECT_EQ(v.error(), "12:34 v-0013: redeclared identifier 'a'");
}
TEST_F(ValidatorTest, RedeclaredIndentifier_Fail) {
@@ -562,8 +612,11 @@ TEST_F(ValidatorTest, RedeclaredIndentifier_Fail) {
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_TRUE(td()->DetermineFunction(func)) << td()->error();
EXPECT_FALSE(v()->Validate());
EXPECT_EQ(v()->error(), "12:34 v-0014: redeclared identifier 'a'");
ValidatorImpl& v = Build();
EXPECT_FALSE(v.Validate());
EXPECT_EQ(v.error(), "12:34 v-0014: redeclared identifier 'a'");
}
TEST_F(ValidatorTest, RedeclaredIdentifierInnerScope_Pass) {
@@ -589,7 +642,10 @@ TEST_F(ValidatorTest, RedeclaredIdentifierInnerScope_Pass) {
});
EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error();
EXPECT_TRUE(v()->ValidateStatements(outer_body)) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateStatements(outer_body)) << v.error();
}
TEST_F(ValidatorTest, DISABLED_RedeclaredIdentifierInnerScope_False) {
@@ -616,8 +672,11 @@ TEST_F(ValidatorTest, DISABLED_RedeclaredIdentifierInnerScope_False) {
});
EXPECT_TRUE(td()->DetermineStatements(outer_body)) << td()->error();
EXPECT_FALSE(v()->ValidateStatements(outer_body));
EXPECT_EQ(v()->error(), "12:34 v-0014: redeclared identifier 'a'");
ValidatorImpl& v = Build();
EXPECT_FALSE(v.ValidateStatements(outer_body));
EXPECT_EQ(v.error(), "12:34 v-0014: redeclared identifier 'a'");
}
TEST_F(ValidatorTest, RedeclaredIdentifierDifferentFunctions_Pass) {
@@ -652,7 +711,10 @@ TEST_F(ValidatorTest, RedeclaredIdentifierDifferentFunctions_Pass) {
mod->AddFunction(func1);
EXPECT_TRUE(td()->Determine()) << td()->error();
EXPECT_TRUE(v()->Validate()) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.Validate()) << v.error();
}
TEST_F(ValidatorTest, VariableDeclNoConstructor_Pass) {
@@ -676,80 +738,107 @@ TEST_F(ValidatorTest, VariableDeclNoConstructor_Pass) {
EXPECT_TRUE(td()->DetermineStatements(body)) << td()->error();
ASSERT_NE(lhs->result_type(), nullptr);
ASSERT_NE(rhs->result_type(), nullptr);
EXPECT_TRUE(v()->ValidateStatements(body)) << v()->error();
ValidatorImpl& v = Build();
EXPECT_TRUE(v.ValidateStatements(body)) << v.error();
}
TEST_F(ValidatorTest, IsStorable_Void) {
EXPECT_FALSE(v()->IsStorable(ty.void_));
ValidatorImpl& v = Build();
EXPECT_FALSE(v.IsStorable(ty.void_));
}
TEST_F(ValidatorTest, IsStorable_Scalar) {
EXPECT_TRUE(v()->IsStorable(ty.bool_));
EXPECT_TRUE(v()->IsStorable(ty.i32));
EXPECT_TRUE(v()->IsStorable(ty.u32));
EXPECT_TRUE(v()->IsStorable(ty.f32));
ValidatorImpl& v = Build();
EXPECT_TRUE(v.IsStorable(ty.bool_));
EXPECT_TRUE(v.IsStorable(ty.i32));
EXPECT_TRUE(v.IsStorable(ty.u32));
EXPECT_TRUE(v.IsStorable(ty.f32));
}
TEST_F(ValidatorTest, IsStorable_Vector) {
EXPECT_TRUE(v()->IsStorable(ty.vec2<int>()));
EXPECT_TRUE(v()->IsStorable(ty.vec3<int>()));
EXPECT_TRUE(v()->IsStorable(ty.vec4<int>()));
EXPECT_TRUE(v()->IsStorable(ty.vec2<unsigned>()));
EXPECT_TRUE(v()->IsStorable(ty.vec3<unsigned>()));
EXPECT_TRUE(v()->IsStorable(ty.vec4<unsigned>()));
EXPECT_TRUE(v()->IsStorable(ty.vec2<float>()));
EXPECT_TRUE(v()->IsStorable(ty.vec3<float>()));
EXPECT_TRUE(v()->IsStorable(ty.vec4<float>()));
ValidatorImpl& v = Build();
EXPECT_TRUE(v.IsStorable(ty.vec2<int>()));
EXPECT_TRUE(v.IsStorable(ty.vec3<int>()));
EXPECT_TRUE(v.IsStorable(ty.vec4<int>()));
EXPECT_TRUE(v.IsStorable(ty.vec2<unsigned>()));
EXPECT_TRUE(v.IsStorable(ty.vec3<unsigned>()));
EXPECT_TRUE(v.IsStorable(ty.vec4<unsigned>()));
EXPECT_TRUE(v.IsStorable(ty.vec2<float>()));
EXPECT_TRUE(v.IsStorable(ty.vec3<float>()));
EXPECT_TRUE(v.IsStorable(ty.vec4<float>()));
}
TEST_F(ValidatorTest, IsStorable_Matrix) {
EXPECT_TRUE(v()->IsStorable(ty.mat2x2<float>()));
EXPECT_TRUE(v()->IsStorable(ty.mat2x3<float>()));
EXPECT_TRUE(v()->IsStorable(ty.mat2x4<float>()));
EXPECT_TRUE(v()->IsStorable(ty.mat3x2<float>()));
EXPECT_TRUE(v()->IsStorable(ty.mat3x3<float>()));
EXPECT_TRUE(v()->IsStorable(ty.mat3x4<float>()));
EXPECT_TRUE(v()->IsStorable(ty.mat4x2<float>()));
EXPECT_TRUE(v()->IsStorable(ty.mat4x3<float>()));
EXPECT_TRUE(v()->IsStorable(ty.mat4x4<float>()));
ValidatorImpl& v = Build();
EXPECT_TRUE(v.IsStorable(ty.mat2x2<float>()));
EXPECT_TRUE(v.IsStorable(ty.mat2x3<float>()));
EXPECT_TRUE(v.IsStorable(ty.mat2x4<float>()));
EXPECT_TRUE(v.IsStorable(ty.mat3x2<float>()));
EXPECT_TRUE(v.IsStorable(ty.mat3x3<float>()));
EXPECT_TRUE(v.IsStorable(ty.mat3x4<float>()));
EXPECT_TRUE(v.IsStorable(ty.mat4x2<float>()));
EXPECT_TRUE(v.IsStorable(ty.mat4x3<float>()));
EXPECT_TRUE(v.IsStorable(ty.mat4x4<float>()));
}
TEST_F(ValidatorTest, IsStorable_Pointer) {
auto* ptr_ty = ty.pointer<int>(ast::StorageClass::kPrivate);
EXPECT_FALSE(v()->IsStorable(ptr_ty));
ValidatorImpl& v = Build();
EXPECT_FALSE(v.IsStorable(ptr_ty));
}
TEST_F(ValidatorTest, IsStorable_AliasVoid) {
auto* alias = ty.alias("myalias", ty.void_);
EXPECT_FALSE(v()->IsStorable(alias));
ValidatorImpl& v = Build();
EXPECT_FALSE(v.IsStorable(alias));
}
TEST_F(ValidatorTest, IsStorable_AliasI32) {
auto* alias = ty.alias("myalias", ty.i32);
EXPECT_TRUE(v()->IsStorable(alias));
ValidatorImpl& v = Build();
EXPECT_TRUE(v.IsStorable(alias));
}
TEST_F(ValidatorTest, IsStorable_ArraySizedOfStorable) {
EXPECT_TRUE(v()->IsStorable(ty.array(ty.i32, 5)));
ValidatorImpl& v = Build();
EXPECT_TRUE(v.IsStorable(ty.array(ty.i32, 5)));
}
TEST_F(ValidatorTest, IsStorable_ArraySizedOfNonStorable) {
EXPECT_FALSE(v()->IsStorable(ty.array(ty.void_, 5)));
ValidatorImpl& v = Build();
EXPECT_FALSE(v.IsStorable(ty.array(ty.void_, 5)));
}
TEST_F(ValidatorTest, IsStorable_ArrayUnsizedOfStorable) {
EXPECT_TRUE(v()->IsStorable(ty.array<int>()));
ValidatorImpl& v = Build();
EXPECT_TRUE(v.IsStorable(ty.array<int>()));
}
TEST_F(ValidatorTest, IsStorable_ArrayUnsizedOfNonStorable) {
EXPECT_FALSE(v()->IsStorable(ty.array<void>()));
ValidatorImpl& v = Build();
EXPECT_FALSE(v.IsStorable(ty.array<void>()));
}
TEST_F(ValidatorTest, IsStorable_Struct_AllMembersStorable) {
ast::StructMemberList members{Member("a", ty.i32), Member("b", ty.f32)};
auto* s = create<ast::Struct>(Source{}, members, ast::StructDecorationList{});
auto* s_ty = ty.struct_("mystruct", s);
EXPECT_TRUE(v()->IsStorable(s_ty));
ValidatorImpl& v = Build();
EXPECT_TRUE(v.IsStorable(s_ty));
}
TEST_F(ValidatorTest, IsStorable_Struct_SomeMembersNonStorable) {
@@ -757,7 +846,9 @@ TEST_F(ValidatorTest, IsStorable_Struct_SomeMembersNonStorable) {
ast::StructMemberList members{Member("a", ty.i32), Member("b", ptr_ty)};
auto* s = create<ast::Struct>(Source{}, members, ast::StructDecorationList{});
auto* s_ty = ty.struct_("mystruct", s);
EXPECT_FALSE(v()->IsStorable(s_ty));
ValidatorImpl& v = Build();
EXPECT_FALSE(v.IsStorable(s_ty));
}
} // namespace