writer/wgsl: Ensure that test programs are valid

Make all test programs valid.

Change-Id: Id7eb790519e3dec30a5b826f06585d277995b9b5
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/50720
Auto-Submit: James Price <jrprice@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Reviewed-by: Ben Clayton <bclayton@google.com>
This commit is contained in:
James Price 2021-05-12 10:06:21 +00:00 committed by Commit Bot service account
parent 4ace822c4b
commit 42f8999af5
5 changed files with 25 additions and 8 deletions

View File

@ -31,6 +31,17 @@ using WgslBinaryTest = TestParamHelper<BinaryData>;
TEST_P(WgslBinaryTest, Emit) { TEST_P(WgslBinaryTest, Emit) {
auto params = GetParam(); auto params = GetParam();
auto op_ty = [&]() -> ast::Type* {
if (params.op == ast::BinaryOp::kLogicalAnd ||
params.op == ast::BinaryOp::kLogicalOr) {
return ty.bool_();
} else {
return ty.u32();
}
};
Global("left", op_ty(), ast::StorageClass::kPrivate);
Global("right", op_ty(), ast::StorageClass::kPrivate);
auto* left = Expr("left"); auto* left = Expr("left");
auto* right = Expr("right"); auto* right = Expr("right");

View File

@ -101,27 +101,30 @@ TEST_F(WgslGeneratorImplTest, Emit_GlobalsInterleaved) {
} }
TEST_F(WgslGeneratorImplTest, Emit_Global_Sampler) { TEST_F(WgslGeneratorImplTest, Emit_Global_Sampler) {
Global("s", ty.sampler(ast::SamplerKind::kSampler), ast::StorageClass::kNone); Global("s", ty.sampler(ast::SamplerKind::kSampler), ast::StorageClass::kNone,
nullptr,
{create<ast::GroupDecoration>(0), create<ast::BindingDecoration>(0)});
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.Generate(nullptr)) << gen.error(); ASSERT_TRUE(gen.Generate(nullptr)) << gen.error();
EXPECT_EQ(gen.result(), " var s : sampler;\n"); EXPECT_EQ(gen.result(), " [[group(0), binding(0)]] var s : sampler;\n");
} }
TEST_F(WgslGeneratorImplTest, Emit_Global_Texture) { TEST_F(WgslGeneratorImplTest, Emit_Global_Texture) {
auto st = ty.sampled_texture(ast::TextureDimension::k1d, ty.f32()); auto st = ty.sampled_texture(ast::TextureDimension::k1d, ty.f32());
Global("t", ty.access(ast::AccessControl::kReadOnly, st), Global("t", ty.access(ast::AccessControl::kReadOnly, st),
ast::StorageClass::kNone); ast::StorageClass::kNone, nullptr,
{create<ast::GroupDecoration>(0), create<ast::BindingDecoration>(0)});
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
gen.increment_indent(); gen.increment_indent();
ASSERT_TRUE(gen.Generate(nullptr)) << gen.error(); ASSERT_TRUE(gen.Generate(nullptr)) << gen.error();
EXPECT_EQ(gen.result(), " var t : [[access(read)]] texture_1d<f32>;\n"); EXPECT_EQ(gen.result(), " [[group(0), binding(0)]] var t : [[access(read)]] texture_1d<f32>;\n");
} }
} // namespace } // namespace

View File

@ -22,6 +22,7 @@ namespace {
using WgslGeneratorImplTest = TestHelper; using WgslGeneratorImplTest = TestHelper;
TEST_F(WgslGeneratorImplTest, EmitIdentifierExpression_Single) { TEST_F(WgslGeneratorImplTest, EmitIdentifierExpression_Single) {
Global("glsl", ty.f32(), ast::StorageClass::kPrivate);
auto* i = Expr("glsl"); auto* i = Expr("glsl");
WrapInFunction(i); WrapInFunction(i);

View File

@ -59,10 +59,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Decorated_Multiple) {
auto* v = Global("a", ty.f32(), ast::StorageClass::kPrivate, nullptr, auto* v = Global("a", ty.f32(), ast::StorageClass::kPrivate, nullptr,
ast::DecorationList{ ast::DecorationList{
Builtin(ast::Builtin::kPosition), Builtin(ast::Builtin::kPosition),
create<ast::BindingDecoration>(0),
create<ast::GroupDecoration>(1),
Location(2), Location(2),
create<ast::OverrideDecoration>(42),
}); });
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
@ -70,7 +67,7 @@ TEST_F(WgslGeneratorImplTest, EmitVariable_Decorated_Multiple) {
ASSERT_TRUE(gen.EmitVariable(v)) << gen.error(); ASSERT_TRUE(gen.EmitVariable(v)) << gen.error();
EXPECT_EQ( EXPECT_EQ(
gen.result(), gen.result(),
R"([[builtin(position), binding(0), group(1), location(2), override(42)]] var<private> a : f32; R"([[builtin(position), location(2)]] var<private> a : f32;
)"); )");
} }

View File

@ -43,6 +43,11 @@ class TestHelperBase : public BASE, public ProgramBuilder {
return *gen_; return *gen_;
} }
program = std::make_unique<Program>(std::move(*this)); program = std::make_unique<Program>(std::move(*this));
diag::Formatter formatter;
[&]() {
ASSERT_TRUE(program->IsValid())
<< formatter.format(program->Diagnostics());
}();
gen_ = std::make_unique<GeneratorImpl>(program.get()); gen_ = std::make_unique<GeneratorImpl>(program.get());
return *gen_; return *gen_;
} }