diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 56fa8d320b..b3ae095921 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -522,8 +522,6 @@ if(${TINT_BUILD_TESTS}) utils/tmpfile_test.cc utils/tmpfile.h utils/unique_vector_test.cc - validator/validator_function_test.cc - validator/validator_test.cc validator/validator_test_helper.cc validator/validator_test_helper.h writer/append_vector_test.cc diff --git a/src/resolver/function_validation_test.cc b/src/resolver/function_validation_test.cc index 018e6bb38b..562dc4c72b 100644 --- a/src/resolver/function_validation_test.cc +++ b/src/resolver/function_validation_test.cc @@ -45,6 +45,24 @@ TEST_F(ResolverFunctionValidationTest, FunctionNamesMustBeUnique_fail) { "12:34 error v-0016: function names must be unique 'func'"); } +TEST_F(ResolverFunctionValidationTest, + VoidFunctionEndWithoutReturnStatement_Pass) { + // [[stage(vertex)]] + // fn func -> void { var a:i32 = 2; } + auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2)); + + Func(Source{Source::Location{12, 34}}, "func", ast::VariableList{}, + ty.void_(), + ast::StatementList{ + create(var), + }, + ast::DecorationList{ + create(ast::PipelineStage::kVertex), + }); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); +} + TEST_F(ResolverFunctionValidationTest, FunctionEndWithoutReturnStatement_Fail) { // fn func -> int { var a:i32 = 2; } @@ -62,6 +80,20 @@ TEST_F(ResolverFunctionValidationTest, FunctionEndWithoutReturnStatement_Fail) { "12:34 error v-0002: non-void function must end with a return statement"); } +TEST_F(ResolverFunctionValidationTest, + VoidFunctionEndWithoutReturnStatementEmptyBody_Pass) { + // [[stage(vertex)]] + // fn func -> void {} + + Func(Source{Source::Location{12, 34}}, "func", ast::VariableList{}, + ty.void_(), ast::StatementList{}, + ast::DecorationList{ + create(ast::PipelineStage::kVertex), + }); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); +} + TEST_F(ResolverFunctionValidationTest, FunctionEndWithoutReturnStatementEmptyBody_Fail) { // fn func -> int {} @@ -201,5 +233,43 @@ TEST_F(ResolverFunctionValidationTest, PipelineStage_MustBeUnique_Fail) { "point"); } +TEST_F(ResolverFunctionValidationTest, NoPipelineEntryPoints) { + Func("vtx_func", ast::VariableList{}, ty.void_(), + ast::StatementList{ + create(), + }, + ast::DecorationList{}); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); +} + +TEST_F(ResolverFunctionValidationTest, FunctionVarInitWithParam) { + // fn foo(bar : f32) -> void{ + // var baz : f32 = bar; + // } + + auto* bar = Var("bar", ty.f32(), ast::StorageClass::kFunction); + auto* baz = Var("baz", ty.f32(), ast::StorageClass::kFunction, Expr("bar")); + + Func("foo", ast::VariableList{bar}, ty.void_(), ast::StatementList{Decl(baz)}, + ast::DecorationList{}); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); +} + +TEST_F(ResolverFunctionValidationTest, FunctionConstInitWithParam) { + // fn foo(bar : f32) -> void{ + // const baz : f32 = bar; + // } + + auto* bar = Var("bar", ty.f32(), ast::StorageClass::kFunction); + auto* baz = Const("baz", ty.f32(), Expr("bar")); + + Func("foo", ast::VariableList{bar}, ty.void_(), ast::StatementList{Decl(baz)}, + ast::DecorationList{}); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); +} + } // namespace } // namespace tint diff --git a/src/resolver/type_validation_test.cc b/src/resolver/type_validation_test.cc index ee4144cade..4ea6567195 100644 --- a/src/resolver/type_validation_test.cc +++ b/src/resolver/type_validation_test.cc @@ -26,6 +26,28 @@ namespace { class ResolverTypeValidationTest : public resolver::TestHelper, public testing::Test {}; +TEST_F(ResolverTypeValidationTest, VariableDeclNoConstructor_Pass) { + // { + // var a :i32; + // a = 2; + // } + auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, nullptr); + auto* lhs = Expr("a"); + auto* rhs = Expr(2); + + auto* body = create(ast::StatementList{ + create(var), + create(Source{Source::Location{12, 34}}, lhs, + rhs), + }); + + WrapInFunction(body); + + EXPECT_TRUE(r()->Resolve()) << r()->error(); + ASSERT_NE(TypeOf(lhs), nullptr); + ASSERT_NE(TypeOf(rhs), nullptr); +} + TEST_F(ResolverTypeValidationTest, GlobalVariableWithStorageClass_Pass) { // var global_var: f32; Global(Source{{12, 34}}, "global_var", ty.f32(), ast::StorageClass::kInput); diff --git a/src/validator/validator_function_test.cc b/src/validator/validator_function_test.cc deleted file mode 100644 index 00c7401568..0000000000 --- a/src/validator/validator_function_test.cc +++ /dev/null @@ -1,104 +0,0 @@ -// 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/stage_decoration.h" -#include "src/validator/validator_test_helper.h" - -namespace tint { -namespace { - -class ValidateFunctionTest : public ValidatorTestHelper, - public testing::Test {}; - -TEST_F(ValidateFunctionTest, VoidFunctionEndWithoutReturnStatement_Pass) { - // [[stage(vertex)]] - // fn func -> void { var a:i32 = 2; } - auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, Expr(2)); - - Func(Source{Source::Location{12, 34}}, "func", ast::VariableList{}, - ty.void_(), - ast::StatementList{ - create(var), - }, - ast::DecorationList{ - create(ast::PipelineStage::kVertex), - }); - - ValidatorImpl& v = Build(); - - EXPECT_TRUE(v.Validate()); -} - -TEST_F(ValidateFunctionTest, - VoidFunctionEndWithoutReturnStatementEmptyBody_Pass) { - // [[stage(vertex)]] - // fn func -> void {} - - Func(Source{Source::Location{12, 34}}, "func", ast::VariableList{}, - ty.void_(), ast::StatementList{}, - ast::DecorationList{ - create(ast::PipelineStage::kVertex), - }); - - ValidatorImpl& v = Build(); - - EXPECT_TRUE(v.Validate()); -} - -TEST_F(ValidateFunctionTest, NoPipelineEntryPoints) { - Func("vtx_func", ast::VariableList{}, ty.void_(), - ast::StatementList{ - create(), - }, - ast::DecorationList{}); - - ValidatorImpl& v = Build(); - - EXPECT_TRUE(v.Validate()) << v.error(); -} - -TEST_F(ValidateFunctionTest, FunctionVarInitWithParam) { - // fn foo(bar : f32) -> void{ - // var baz : f32 = bar; - // } - - auto* bar = Var("bar", ty.f32(), ast::StorageClass::kFunction); - auto* baz = Var("baz", ty.f32(), ast::StorageClass::kFunction, Expr("bar")); - - Func("foo", ast::VariableList{bar}, ty.void_(), ast::StatementList{Decl(baz)}, - ast::DecorationList{}); - - ValidatorImpl& v = Build(); - - EXPECT_TRUE(v.Validate()) << v.error(); -} - -TEST_F(ValidateFunctionTest, FunctionConstInitWithParam) { - // fn foo(bar : f32) -> void{ - // const baz : f32 = bar; - // } - - auto* bar = Var("bar", ty.f32(), ast::StorageClass::kFunction); - auto* baz = Const("baz", ty.f32(), Expr("bar")); - - Func("foo", ast::VariableList{bar}, ty.void_(), ast::StatementList{Decl(baz)}, - ast::DecorationList{}); - - ValidatorImpl& v = Build(); - - EXPECT_TRUE(v.Validate()) << v.error(); -} - -} // namespace -} // namespace tint diff --git a/src/validator/validator_test.cc b/src/validator/validator_test.cc deleted file mode 100644 index c13a8f7eed..0000000000 --- a/src/validator/validator_test.cc +++ /dev/null @@ -1,50 +0,0 @@ -// 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/if_statement.h" -#include "src/ast/stage_decoration.h" -#include "src/validator/validator_test_helper.h" - -namespace tint { -namespace { - -class ValidatorTest : public ValidatorTestHelper, public testing::Test {}; - -TEST_F(ValidatorTest, VariableDeclNoConstructor_Pass) { - // { - // var a :i32; - // a = 2; - // } - auto* var = Var("a", ty.i32(), ast::StorageClass::kNone, nullptr); - auto* lhs = Expr("a"); - auto* rhs = Expr(2); - - auto* body = create(ast::StatementList{ - create(var), - create(Source{Source::Location{12, 34}}, lhs, - rhs), - }); - - WrapInFunction(body); - - ValidatorImpl& v = Build(); - - ASSERT_NE(TypeOf(lhs), nullptr); - ASSERT_NE(TypeOf(rhs), nullptr); - - EXPECT_TRUE(v.ValidateStatements(body)) << v.error(); -} - -} // namespace -} // namespace tint diff --git a/test/BUILD.gn b/test/BUILD.gn index cb6546ab04..64dcf38149 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -225,8 +225,6 @@ source_set("tint_unittests_core_src") { "../src/utils/tmpfile.h", "../src/utils/tmpfile_test.cc", "../src/utils/unique_vector_test.cc", - "../src/validator/validator_function_test.cc", - "../src/validator/validator_test.cc", "../src/validator/validator_test_helper.cc", "../src/validator/validator_test_helper.h", "../src/writer/append_vector_test.cc",