From a1f4d4fda9da6640b01ae84fcd2a05df7b204cbe Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Fri, 13 Nov 2020 22:16:25 +0000 Subject: [PATCH] transform tests: Replace std::make_unique -> create create() is currently just a simple forwarder to std::make_unique<>, but will be later replaced with a function that returns a raw pointer, and owned by the context. Bug: tint:322 Change-Id: I630ce57017fe84b5d00e9bd74902f47547a13f3a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/32669 Commit-Queue: Ben Clayton Reviewed-by: dan sinclair --- .../bound_array_accessors_transform_test.cc | 343 +++++++++--------- .../vertex_pulling_transform_test.cc | 48 +-- 2 files changed, 189 insertions(+), 202 deletions(-) diff --git a/src/transform/bound_array_accessors_transform_test.cc b/src/transform/bound_array_accessors_transform_test.cc index cf013a7927..e165ef03ef 100644 --- a/src/transform/bound_array_accessors_transform_test.cc +++ b/src/transform/bound_array_accessors_transform_test.cc @@ -52,9 +52,8 @@ class BoundArrayAccessorsTest : public testing::Test { BoundArrayAccessorsTest() : td_(&ctx_, &mod_), transform_(&ctx_, &mod_) {} ast::BlockStatement* SetupFunctionAndBody() { - auto func = std::make_unique("func", ast::VariableList{}, - &void_type_); - auto block = std::make_unique(); + auto func = create("func", ast::VariableList{}, &void_type_); + auto block = create(); body_ = block.get(); func->set_body(std::move(block)); mod_.AddFunction(std::move(func)); @@ -63,13 +62,20 @@ class BoundArrayAccessorsTest : public testing::Test { void DeclareVariable(std::unique_ptr var) { ASSERT_NE(body_, nullptr); - body_->append(std::make_unique(std::move(var))); + body_->append(create(std::move(var))); } TypeDeterminer* td() { return &td_; } BoundArrayAccessorsTransform* transform() { return &transform_; } + /// @return a `std::unique_ptr` to a new `T` constructed with `args` + /// @param args the arguments to forward to the constructor for `T` + template + std::unique_ptr create(ARGS&&... args) { + return std::make_unique(std::forward(args)...); + } + private: Context ctx_; ast::Module mod_; @@ -93,22 +99,20 @@ TEST_F(BoundArrayAccessorsTest, Ptrs_Clamp) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &ary)); + create("a", ast::StorageClass::kFunction, &ary)); - auto c_var = - std::make_unique("c", ast::StorageClass::kFunction, &u32); + auto c_var = create("c", ast::StorageClass::kFunction, &u32); c_var->set_is_const(true); DeclareVariable(std::move(c_var)); - auto access_idx = std::make_unique("c"); + auto access_idx = create("c"); auto* access_ptr = access_idx.get(); - auto accessor = std::make_unique( - std::make_unique("a"), std::move(access_idx)); + auto accessor = create( + create("a"), std::move(access_idx)); auto* ptr = accessor.get(); - auto b = std::make_unique("b", ast::StorageClass::kFunction, - &ptr_type); + auto b = create("b", ast::StorageClass::kFunction, &ptr_type); b->set_constructor(std::move(accessor)); b->set_is_const(true); DeclareVariable(std::move(b)); @@ -156,27 +160,24 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Nested_Scalar) { ast::type::ArrayType ary5(&f32, 5); SetupFunctionAndBody(); - DeclareVariable(std::make_unique( - "a", ast::StorageClass::kFunction, &ary3)); - DeclareVariable(std::make_unique( - "b", ast::StorageClass::kFunction, &ary5)); DeclareVariable( - std::make_unique("i", ast::StorageClass::kFunction, &u32)); + create("a", ast::StorageClass::kFunction, &ary3)); + DeclareVariable( + create("b", ast::StorageClass::kFunction, &ary5)); + DeclareVariable( + create("i", ast::StorageClass::kFunction, &u32)); - auto b_access_idx = std::make_unique("i"); + auto b_access_idx = create("i"); auto* b_access_ptr = b_access_idx.get(); - auto a_access_idx = std::make_unique( - std::make_unique("b"), - std::move(b_access_idx)); + auto a_access_idx = create( + create("b"), std::move(b_access_idx)); - auto accessor = std::make_unique( - std::make_unique("a"), - std::move(a_access_idx)); + auto accessor = create( + create("a"), std::move(a_access_idx)); auto* ptr = accessor.get(); - auto b = - std::make_unique("c", ast::StorageClass::kFunction, &f32); + auto b = create("c", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -241,16 +242,15 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Scalar) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &ary)); + create("a", ast::StorageClass::kFunction, &ary)); - auto accessor = std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&u32, 1u))); + auto accessor = create( + create("a"), + create( + create(&u32, 1u))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -282,26 +282,24 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Expr) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &ary)); + create("a", ast::StorageClass::kFunction, &ary)); DeclareVariable( - std::make_unique("c", ast::StorageClass::kFunction, &u32)); + create("c", ast::StorageClass::kFunction, &u32)); - auto access_idx = std::make_unique( - ast::BinaryOp::kAdd, std::make_unique("c"), - std::make_unique( - ast::BinaryOp::kSubtract, - std::make_unique( - std::make_unique(&u32, 2)), - std::make_unique( - std::make_unique(&u32, 3)))); + auto access_idx = create( + ast::BinaryOp::kAdd, create("c"), + create(ast::BinaryOp::kSubtract, + create( + create(&u32, 2)), + create( + create(&u32, 3)))); auto* access_ptr = access_idx.get(); - auto accessor = std::make_unique( - std::make_unique("a"), std::move(access_idx)); + auto accessor = create( + create("a"), std::move(access_idx)); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -346,16 +344,15 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_Negative) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &ary)); + create("a", ast::StorageClass::kFunction, &ary)); - auto accessor = std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&i32, -1))); + auto accessor = create( + create("a"), + create( + create(&i32, -1))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -386,16 +383,15 @@ TEST_F(BoundArrayAccessorsTest, Array_Idx_OutOfBounds) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &ary)); + create("a", ast::StorageClass::kFunction, &ary)); - auto accessor = std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&u32, 3u))); + auto accessor = create( + create("a"), + create( + create(&u32, 3u))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -426,16 +422,15 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Scalar) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &vec)); + create("a", ast::StorageClass::kFunction, &vec)); - auto accessor = std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&u32, 1u))); + auto accessor = create( + create("a"), + create( + create(&u32, 1u))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -467,26 +462,24 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Expr) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &vec)); + create("a", ast::StorageClass::kFunction, &vec)); DeclareVariable( - std::make_unique("c", ast::StorageClass::kFunction, &u32)); + create("c", ast::StorageClass::kFunction, &u32)); - auto access_idx = std::make_unique( - ast::BinaryOp::kAdd, std::make_unique("c"), - std::make_unique( - ast::BinaryOp::kSubtract, - std::make_unique( - std::make_unique(&u32, 2)), - std::make_unique( - std::make_unique(&u32, 3)))); + auto access_idx = create( + ast::BinaryOp::kAdd, create("c"), + create(ast::BinaryOp::kSubtract, + create( + create(&u32, 2)), + create( + create(&u32, 3)))); auto* access_ptr = access_idx.get(); - auto accessor = std::make_unique( - std::make_unique("a"), std::move(access_idx)); + auto accessor = create( + create("a"), std::move(access_idx)); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -530,16 +523,15 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_Negative) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &vec)); + create("a", ast::StorageClass::kFunction, &vec)); - auto accessor = std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&i32, -1))); + auto accessor = create( + create("a"), + create( + create(&i32, -1))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -570,16 +562,15 @@ TEST_F(BoundArrayAccessorsTest, Vector_Idx_OutOfBounds) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &vec)); + create("a", ast::StorageClass::kFunction, &vec)); - auto accessor = std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&u32, 3u))); + auto accessor = create( + create("a"), + create( + create(&u32, 3u))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -610,19 +601,18 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Scalar) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &mat)); + create("a", ast::StorageClass::kFunction, &mat)); - auto accessor = std::make_unique( - std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&u32, 2u))), - std::make_unique( - std::make_unique(&u32, 1u))); + auto accessor = create( + create( + create("a"), + create( + create(&u32, 2u))), + create( + create(&u32, 1u))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -667,30 +657,27 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Column) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &mat)); + create("a", ast::StorageClass::kFunction, &mat)); DeclareVariable( - std::make_unique("c", ast::StorageClass::kFunction, &u32)); + create("c", ast::StorageClass::kFunction, &u32)); - auto access_idx = std::make_unique( - ast::BinaryOp::kAdd, std::make_unique("c"), - std::make_unique( - ast::BinaryOp::kSubtract, - std::make_unique( - std::make_unique(&u32, 2)), - std::make_unique( - std::make_unique(&u32, 3)))); + auto access_idx = create( + ast::BinaryOp::kAdd, create("c"), + create(ast::BinaryOp::kSubtract, + create( + create(&u32, 2)), + create( + create(&u32, 3)))); auto* access_ptr = access_idx.get(); - auto accessor = std::make_unique( - std::make_unique( - std::make_unique("a"), - std::move(access_idx)), - std::make_unique( - std::make_unique(&u32, 1u))); + auto accessor = create( + create( + create("a"), std::move(access_idx)), + create( + create(&u32, 1u))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -749,30 +736,28 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Expr_Row) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &mat)); + create("a", ast::StorageClass::kFunction, &mat)); DeclareVariable( - std::make_unique("c", ast::StorageClass::kFunction, &u32)); + create("c", ast::StorageClass::kFunction, &u32)); - auto access_idx = std::make_unique( - ast::BinaryOp::kAdd, std::make_unique("c"), - std::make_unique( - ast::BinaryOp::kSubtract, - std::make_unique( - std::make_unique(&u32, 2)), - std::make_unique( - std::make_unique(&u32, 3)))); + auto access_idx = create( + ast::BinaryOp::kAdd, create("c"), + create(ast::BinaryOp::kSubtract, + create( + create(&u32, 2)), + create( + create(&u32, 3)))); auto* access_ptr = access_idx.get(); - auto accessor = std::make_unique( - std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&u32, 1u))), + auto accessor = create( + create( + create("a"), + create( + create(&u32, 1u))), std::move(access_idx)); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -829,19 +814,18 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Column) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &mat)); + create("a", ast::StorageClass::kFunction, &mat)); - auto accessor = std::make_unique( - std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&i32, -1))), - std::make_unique( - std::make_unique(&i32, 1))); + auto accessor = create( + create( + create("a"), + create( + create(&i32, -1))), + create( + create(&i32, 1))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -884,19 +868,18 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_Negative_Row) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &mat)); + create("a", ast::StorageClass::kFunction, &mat)); - auto accessor = std::make_unique( - std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&i32, 2))), - std::make_unique( - std::make_unique(&i32, -1))); + auto accessor = create( + create( + create("a"), + create( + create(&i32, 2))), + create( + create(&i32, -1))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -940,19 +923,18 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Column) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &mat)); + create("a", ast::StorageClass::kFunction, &mat)); - auto accessor = std::make_unique( - std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&u32, 5u))), - std::make_unique( - std::make_unique(&u32, 1u))); + auto accessor = create( + create( + create("a"), + create( + create(&u32, 5u))), + create( + create(&u32, 1u))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); @@ -996,19 +978,18 @@ TEST_F(BoundArrayAccessorsTest, Matrix_Idx_OutOfBounds_Row) { SetupFunctionAndBody(); DeclareVariable( - std::make_unique("a", ast::StorageClass::kFunction, &mat)); + create("a", ast::StorageClass::kFunction, &mat)); - auto accessor = std::make_unique( - std::make_unique( - std::make_unique("a"), - std::make_unique( - std::make_unique(&u32, 2u))), - std::make_unique( - std::make_unique(&u32, 5u))); + auto accessor = create( + create( + create("a"), + create( + create(&u32, 2u))), + create( + create(&u32, 5u))); auto* ptr = accessor.get(); - auto b = - std::make_unique("b", ast::StorageClass::kFunction, &f32); + auto b = create("b", ast::StorageClass::kFunction, &f32); b->set_constructor(std::move(accessor)); DeclareVariable(std::move(b)); diff --git a/src/transform/vertex_pulling_transform_test.cc b/src/transform/vertex_pulling_transform_test.cc index 4a5715e791..a9b0c8c2a9 100644 --- a/src/transform/vertex_pulling_transform_test.cc +++ b/src/transform/vertex_pulling_transform_test.cc @@ -35,17 +35,17 @@ namespace { class VertexPullingTransformHelper { public: VertexPullingTransformHelper() { - mod_ = std::make_unique(); + mod_ = create(); transform_ = std::make_unique(&ctx_, mod_.get()); } // Create basic module with an entry point and vertex function void InitBasicModule() { - auto func = std::make_unique( + auto func = create( "main", ast::VariableList{}, ctx_.type_mgr().Get(std::make_unique())); - func->add_decoration(std::make_unique( - ast::PipelineStage ::kVertex, Source{})); + func->add_decoration( + create(ast::PipelineStage ::kVertex, Source{})); mod()->AddFunction(std::move(func)); } @@ -65,12 +65,11 @@ class VertexPullingTransformHelper { void AddVertexInputVariable(uint32_t location, std::string name, ast::type::Type* type) { - auto var = std::make_unique( - std::make_unique(name, ast::StorageClass::kInput, type)); + auto var = create( + create(name, ast::StorageClass::kInput, type)); ast::VariableDecorationList decorations; - decorations.push_back( - std::make_unique(location, Source{})); + decorations.push_back(create(location, Source{})); var->set_decorations(std::move(decorations)); mod_->AddGlobalVariable(std::move(var)); @@ -80,6 +79,13 @@ class VertexPullingTransformHelper { ast::Module* mod() { return mod_.get(); } VertexPullingTransform* transform() { return transform_.get(); } + /// @return a `std::unique_ptr` to a new `T` constructed with `args` + /// @param args the arguments to forward to the constructor for `T` + template + std::unique_ptr create(ARGS&&... args) { + return std::make_unique(std::forward(args)...); + } + private: Context ctx_; std::unique_ptr mod_; @@ -110,11 +116,11 @@ TEST_F(VertexPullingTransformTest, Error_InvalidEntryPoint) { } TEST_F(VertexPullingTransformTest, Error_EntryPointWrongStage) { - auto func = std::make_unique( + auto func = create( "main", ast::VariableList{}, ctx()->type_mgr().Get(std::make_unique())); - func->add_decoration(std::make_unique( - ast::PipelineStage::kFragment, Source{})); + func->add_decoration( + create(ast::PipelineStage::kFragment, Source{})); mod()->AddFunction(std::move(func)); InitTransform({}); @@ -392,26 +398,26 @@ TEST_F(VertexPullingTransformTest, ExistingVertexIndexAndInstanceIndex) { ast::type::I32Type i32; { - auto vertex_index_var = std::make_unique( - std::make_unique("custom_vertex_index", - ast::StorageClass::kInput, &i32)); + auto vertex_index_var = + create(create( + "custom_vertex_index", ast::StorageClass::kInput, &i32)); ast::VariableDecorationList decorations; - decorations.push_back(std::make_unique( - ast::Builtin::kVertexIdx, Source{})); + decorations.push_back( + create(ast::Builtin::kVertexIdx, Source{})); vertex_index_var->set_decorations(std::move(decorations)); mod()->AddGlobalVariable(std::move(vertex_index_var)); } { - auto instance_index_var = std::make_unique( - std::make_unique("custom_instance_index", - ast::StorageClass::kInput, &i32)); + auto instance_index_var = + create(create( + "custom_instance_index", ast::StorageClass::kInput, &i32)); ast::VariableDecorationList decorations; - decorations.push_back(std::make_unique( - ast::Builtin::kInstanceIdx, Source{})); + decorations.push_back( + create(ast::Builtin::kInstanceIdx, Source{})); instance_index_var->set_decorations(std::move(decorations)); mod()->AddGlobalVariable(std::move(instance_index_var));