src/writer: Split UnaryOp TEST_P into TEST_Fs

Once the resolver correctly handles address-of and indirections,
the logic around these will be different enough that a parameterized
test no longer makes sense.

Bug: tint:727
Change-Id: I79c138b12bd6f49d9dbee02c62e594a9b1b4a2db
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/51185
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
Reviewed-by: David Neto <dneto@google.com>
This commit is contained in:
Ben Clayton 2021-05-17 15:52:57 +00:00 committed by Commit Bot service account
parent fe0910fa37
commit a965ad4d3a
3 changed files with 131 additions and 69 deletions

View File

@ -19,37 +19,57 @@ namespace writer {
namespace hlsl { namespace hlsl {
namespace { namespace {
struct UnaryOpData { using HlslUnaryOpTest = TestHelper;
const char* name;
ast::UnaryOp op;
};
inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
out << data.op;
return out;
}
using HlslUnaryOpTest = TestParamHelper<UnaryOpData>;
TEST_P(HlslUnaryOpTest, Emit) {
auto params = GetParam();
auto name = std::string(params.name);
Global("expr", ty.i32(), ast::StorageClass::kPrivate); TEST_F(HlslUnaryOpTest, AddressOf) {
Global("expr", ty.f32(), ast::StorageClass::kPrivate);
auto* op = create<ast::UnaryOpExpression>(params.op, Expr("expr")); auto* op =
create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf, Expr("expr"));
WrapInFunction(op); WrapInFunction(op);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(pre, out, op)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(pre, out, op)) << gen.error();
EXPECT_EQ(result(), name.empty() ? "expr" : name + "(expr)"); EXPECT_EQ(result(), "expr");
} }
INSTANTIATE_TEST_SUITE_P(
HlslGeneratorImplTest_UnaryOp,
HlslUnaryOpTest,
testing::Values(UnaryOpData{"", ast::UnaryOp::kAddressOf},
UnaryOpData{"", ast::UnaryOp::kIndirection},
UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-", ast::UnaryOp::kNegation}));
TEST_F(HlslUnaryOpTest, Indirection) {
Global("G", ty.f32(), ast::StorageClass::kPrivate);
auto* p = Const(
"expr", nullptr,
create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf, Expr("G")));
auto* op =
create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection, Expr("expr"));
WrapInFunction(p, op);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(pre, out, op)) << gen.error();
EXPECT_EQ(result(), "expr");
}
TEST_F(HlslUnaryOpTest, Not) {
Global("expr", ty.f32(), ast::StorageClass::kPrivate);
auto* op = create<ast::UnaryOpExpression>(ast::UnaryOp::kNot, Expr("expr"));
WrapInFunction(op);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(pre, out, op)) << gen.error();
EXPECT_EQ(result(), "!(expr)");
}
TEST_F(HlslUnaryOpTest, Negation) {
Global("expr", ty.f32(), ast::StorageClass::kPrivate);
auto* op =
create<ast::UnaryOpExpression>(ast::UnaryOp::kNegation, Expr("expr"));
WrapInFunction(op);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(pre, out, op)) << gen.error();
EXPECT_EQ(result(), "-(expr)");
}
} // namespace } // namespace
} // namespace hlsl } // namespace hlsl
} // namespace writer } // namespace writer

View File

@ -19,34 +19,57 @@ namespace writer {
namespace msl { namespace msl {
namespace { namespace {
struct UnaryOpData { using MslUnaryOpTest = TestHelper;
const char* name;
ast::UnaryOp op;
};
inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
out << data.op;
return out;
}
using MslUnaryOpTest = TestParamHelper<UnaryOpData>;
TEST_P(MslUnaryOpTest, Emit) {
auto params = GetParam();
Global("expr", ty.i32(), ast::StorageClass::kPrivate);
auto* op = create<ast::UnaryOpExpression>(params.op, Expr("expr")); TEST_F(MslUnaryOpTest, AddressOf) {
Global("expr", ty.f32(), ast::StorageClass::kPrivate);
auto* op =
create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf, Expr("expr"));
WrapInFunction(op); WrapInFunction(op);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), std::string(params.name) + "(expr)"); EXPECT_EQ(gen.result(), "&(expr)");
}
TEST_F(MslUnaryOpTest, Indirection) {
Global("G", ty.f32(), ast::StorageClass::kPrivate);
auto* p = Const(
"expr", nullptr,
create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf, Expr("G")));
auto* op =
create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection, Expr("expr"));
WrapInFunction(p, op);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "*(expr)");
}
TEST_F(MslUnaryOpTest, Not) {
Global("expr", ty.f32(), ast::StorageClass::kPrivate);
auto* op = create<ast::UnaryOpExpression>(ast::UnaryOp::kNot, Expr("expr"));
WrapInFunction(op);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "!(expr)");
}
TEST_F(MslUnaryOpTest, Negation) {
Global("expr", ty.f32(), ast::StorageClass::kPrivate);
auto* op =
create<ast::UnaryOpExpression>(ast::UnaryOp::kNegation, Expr("expr"));
WrapInFunction(op);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "-(expr)");
} }
INSTANTIATE_TEST_SUITE_P(
MslGeneratorImplTest,
MslUnaryOpTest,
testing::Values(UnaryOpData{"&", ast::UnaryOp::kAddressOf},
UnaryOpData{"*", ast::UnaryOp::kIndirection},
UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-", ast::UnaryOp::kNegation}));
} // namespace } // namespace
} // namespace msl } // namespace msl

View File

@ -19,38 +19,57 @@ namespace writer {
namespace wgsl { namespace wgsl {
namespace { namespace {
struct UnaryOpData { using WgslUnaryOpTest = TestHelper;
const char* name;
ast::UnaryOp op;
};
inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) {
out << data.op;
return out;
}
using WgslUnaryOpTest = TestParamHelper<UnaryOpData>;
TEST_P(WgslUnaryOpTest, Emit) {
auto params = GetParam();
auto* type = (params.op == ast::UnaryOp::kNot) TEST_F(WgslUnaryOpTest, AddressOf) {
? static_cast<ast::Type*>(ty.bool_()) Global("expr", ty.f32(), ast::StorageClass::kPrivate);
: static_cast<ast::Type*>(ty.i32()); auto* op =
Global("expr", type, ast::StorageClass::kPrivate); create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf, Expr("expr"));
auto* op = create<ast::UnaryOpExpression>(params.op, Expr("expr"));
WrapInFunction(op); WrapInFunction(op);
GeneratorImpl& gen = Build(); GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error(); ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), std::string(params.name) + "(expr)"); EXPECT_EQ(gen.result(), "&(expr)");
}
TEST_F(WgslUnaryOpTest, Indirection) {
Global("G", ty.f32(), ast::StorageClass::kPrivate);
auto* p = Const(
"expr", nullptr,
create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf, Expr("G")));
auto* op =
create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection, Expr("expr"));
WrapInFunction(p, op);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "*(expr)");
}
TEST_F(WgslUnaryOpTest, Not) {
Global("expr", ty.f32(), ast::StorageClass::kPrivate);
auto* op = create<ast::UnaryOpExpression>(ast::UnaryOp::kNot, Expr("expr"));
WrapInFunction(op);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "!(expr)");
}
TEST_F(WgslUnaryOpTest, Negation) {
Global("expr", ty.f32(), ast::StorageClass::kPrivate);
auto* op =
create<ast::UnaryOpExpression>(ast::UnaryOp::kNegation, Expr("expr"));
WrapInFunction(op);
GeneratorImpl& gen = Build();
ASSERT_TRUE(gen.EmitExpression(op)) << gen.error();
EXPECT_EQ(gen.result(), "-(expr)");
} }
INSTANTIATE_TEST_SUITE_P(
WgslGeneratorImplTest,
WgslUnaryOpTest,
testing::Values(UnaryOpData{"&", ast::UnaryOp::kAddressOf},
UnaryOpData{"*", ast::UnaryOp::kIndirection},
UnaryOpData{"!", ast::UnaryOp::kNot},
UnaryOpData{"-", ast::UnaryOp::kNegation}));
} // namespace } // namespace
} // namespace wgsl } // namespace wgsl