blob: 00c74015687c00699f76343047064c4cfc78f6cf [file] [log] [blame]
// 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<ast::VariableDeclStatement>(var),
},
ast::DecorationList{
create<ast::StageDecoration>(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::StageDecoration>(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::ReturnStatement>(),
},
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