From a965ad4d3a21f04f631f86107c432a35c8f32476 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Mon, 17 May 2021 15:52:57 +0000 Subject: [PATCH] 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 Reviewed-by: Antonio Maiorano Reviewed-by: David Neto --- .../hlsl/generator_impl_unary_op_test.cc | 66 +++++++++++------- .../msl/generator_impl_unary_op_test.cc | 65 +++++++++++------ .../wgsl/generator_impl_unary_op_test.cc | 69 ++++++++++++------- 3 files changed, 131 insertions(+), 69 deletions(-) diff --git a/src/writer/hlsl/generator_impl_unary_op_test.cc b/src/writer/hlsl/generator_impl_unary_op_test.cc index e6c6441b09..b7710c3535 100644 --- a/src/writer/hlsl/generator_impl_unary_op_test.cc +++ b/src/writer/hlsl/generator_impl_unary_op_test.cc @@ -19,37 +19,57 @@ namespace writer { namespace hlsl { namespace { -struct UnaryOpData { - const char* name; - ast::UnaryOp op; -}; -inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) { - out << data.op; - return out; -} -using HlslUnaryOpTest = TestParamHelper; -TEST_P(HlslUnaryOpTest, Emit) { - auto params = GetParam(); - auto name = std::string(params.name); +using HlslUnaryOpTest = TestHelper; - Global("expr", ty.i32(), ast::StorageClass::kPrivate); - - auto* op = create(params.op, Expr("expr")); +TEST_F(HlslUnaryOpTest, AddressOf) { + Global("expr", ty.f32(), ast::StorageClass::kPrivate); + auto* op = + create(ast::UnaryOp::kAddressOf, Expr("expr")); WrapInFunction(op); GeneratorImpl& gen = Build(); 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::UnaryOp::kAddressOf, Expr("G"))); + auto* op = + create(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::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::UnaryOp::kNegation, Expr("expr")); + WrapInFunction(op); + + GeneratorImpl& gen = Build(); + + ASSERT_TRUE(gen.EmitExpression(pre, out, op)) << gen.error(); + EXPECT_EQ(result(), "-(expr)"); +} } // namespace } // namespace hlsl } // namespace writer diff --git a/src/writer/msl/generator_impl_unary_op_test.cc b/src/writer/msl/generator_impl_unary_op_test.cc index e43f31b471..161e492668 100644 --- a/src/writer/msl/generator_impl_unary_op_test.cc +++ b/src/writer/msl/generator_impl_unary_op_test.cc @@ -19,34 +19,57 @@ namespace writer { namespace msl { namespace { -struct UnaryOpData { - const char* name; - ast::UnaryOp op; -}; -inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) { - out << data.op; - return out; -} -using MslUnaryOpTest = TestParamHelper; -TEST_P(MslUnaryOpTest, Emit) { - auto params = GetParam(); - Global("expr", ty.i32(), ast::StorageClass::kPrivate); +using MslUnaryOpTest = TestHelper; - auto* op = create(params.op, Expr("expr")); +TEST_F(MslUnaryOpTest, AddressOf) { + Global("expr", ty.f32(), ast::StorageClass::kPrivate); + auto* op = + create(ast::UnaryOp::kAddressOf, Expr("expr")); WrapInFunction(op); GeneratorImpl& gen = Build(); 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::UnaryOp::kAddressOf, Expr("G"))); + auto* op = + create(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::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::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 msl diff --git a/src/writer/wgsl/generator_impl_unary_op_test.cc b/src/writer/wgsl/generator_impl_unary_op_test.cc index 62eb7f5a3b..87d594c444 100644 --- a/src/writer/wgsl/generator_impl_unary_op_test.cc +++ b/src/writer/wgsl/generator_impl_unary_op_test.cc @@ -19,38 +19,57 @@ namespace writer { namespace wgsl { namespace { -struct UnaryOpData { - const char* name; - ast::UnaryOp op; -}; -inline std::ostream& operator<<(std::ostream& out, UnaryOpData data) { - out << data.op; - return out; -} -using WgslUnaryOpTest = TestParamHelper; -TEST_P(WgslUnaryOpTest, Emit) { - auto params = GetParam(); +using WgslUnaryOpTest = TestHelper; - auto* type = (params.op == ast::UnaryOp::kNot) - ? static_cast(ty.bool_()) - : static_cast(ty.i32()); - Global("expr", type, ast::StorageClass::kPrivate); - - auto* op = create(params.op, Expr("expr")); +TEST_F(WgslUnaryOpTest, AddressOf) { + Global("expr", ty.f32(), ast::StorageClass::kPrivate); + auto* op = + create(ast::UnaryOp::kAddressOf, Expr("expr")); WrapInFunction(op); GeneratorImpl& gen = Build(); 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::UnaryOp::kAddressOf, Expr("G"))); + auto* op = + create(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::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::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 wgsl