[spirv-writer] Convert binary tests to parameterized.

This CL changes the binary tests to be paramaterized to make it easier
to add more tests.

Bug: tint:5
Change-Id: Ib4edb3c368c0cba3252dd139841dd5f1be4bc34c
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/19401
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
This commit is contained in:
dan sinclair 2020-04-14 14:47:10 +00:00 committed by dan sinclair
parent a28bcceb15
commit e9e925d0a0
1 changed files with 39 additions and 20 deletions

View File

@ -35,7 +35,19 @@ namespace {
using BuilderTest = testing::Test;
TEST_F(BuilderTest, Binary_Add_Integer) {
struct BinaryData {
ast::BinaryOp op;
std::string name;
};
inline std::ostream& operator<<(std::ostream& out, BinaryData data) {
out << data.op;
return out;
}
using BinaryArithIntegerTest = testing::TestWithParam<BinaryData>;
TEST_P(BinaryArithIntegerTest, Scalar) {
auto param = GetParam();
ast::type::I32Type i32;
auto lhs = std::make_unique<ast::ScalarConstructorExpression>(
@ -43,8 +55,7 @@ TEST_F(BuilderTest, Binary_Add_Integer) {
auto rhs = std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::IntLiteral>(&i32, 4));
ast::BinaryExpression expr(ast::BinaryOp::kAdd, std::move(lhs),
std::move(rhs));
ast::BinaryExpression expr(param.op, std::move(lhs), std::move(rhs));
Context ctx;
TypeDeterminer td(&ctx);
@ -59,11 +70,12 @@ TEST_F(BuilderTest, Binary_Add_Integer) {
%3 = OpConstant %1 4
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%4 = OpIAdd %1 %2 %3
)");
"%4 = " + param.name + " %1 %2 %3\n");
}
TEST_F(BuilderTest, Binary_Add_Integer_Vectors) {
TEST_P(BinaryArithIntegerTest, Vector) {
auto param = GetParam();
ast::type::I32Type i32;
ast::type::VectorType vec3(&i32, 3);
@ -89,8 +101,7 @@ TEST_F(BuilderTest, Binary_Add_Integer_Vectors) {
Context ctx;
TypeDeterminer td(&ctx);
ast::BinaryExpression expr(ast::BinaryOp::kAdd, std::move(lhs),
std::move(rhs));
ast::BinaryExpression expr(param.op, std::move(lhs), std::move(rhs));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
@ -104,11 +115,17 @@ TEST_F(BuilderTest, Binary_Add_Integer_Vectors) {
%4 = OpConstantComposite %1 %3 %3 %3
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%5 = OpIAdd %1 %4 %4
)");
"%5 = " + param.name + " %1 %4 %4\n");
}
INSTANTIATE_TEST_SUITE_P(BuilderTest,
BinaryArithIntegerTest,
testing::Values(BinaryData{ast::BinaryOp::kAdd,
"OpIAdd"}));
using BinaryArithFloatTest = testing::TestWithParam<BinaryData>;
TEST_P(BinaryArithFloatTest, Scalar) {
auto param = GetParam();
TEST_F(BuilderTest, Binary_Add_Float) {
ast::type::F32Type f32;
auto lhs = std::make_unique<ast::ScalarConstructorExpression>(
@ -116,8 +133,7 @@ TEST_F(BuilderTest, Binary_Add_Float) {
auto rhs = std::make_unique<ast::ScalarConstructorExpression>(
std::make_unique<ast::FloatLiteral>(&f32, 4.5f));
ast::BinaryExpression expr(ast::BinaryOp::kAdd, std::move(lhs),
std::move(rhs));
ast::BinaryExpression expr(param.op, std::move(lhs), std::move(rhs));
Context ctx;
TypeDeterminer td(&ctx);
@ -132,11 +148,12 @@ TEST_F(BuilderTest, Binary_Add_Float) {
%3 = OpConstant %1 4.5
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%4 = OpFAdd %1 %2 %3
)");
"%4 = " + param.name + " %1 %2 %3\n");
}
TEST_F(BuilderTest, Binary_Add_Float_Vectors) {
TEST_P(BinaryArithFloatTest, Vector) {
auto param = GetParam();
ast::type::F32Type f32;
ast::type::VectorType vec3(&f32, 3);
@ -162,8 +179,7 @@ TEST_F(BuilderTest, Binary_Add_Float_Vectors) {
Context ctx;
TypeDeterminer td(&ctx);
ast::BinaryExpression expr(ast::BinaryOp::kAdd, std::move(lhs),
std::move(rhs));
ast::BinaryExpression expr(param.op, std::move(lhs), std::move(rhs));
ASSERT_TRUE(td.DetermineResultType(&expr)) << td.error();
@ -177,9 +193,12 @@ TEST_F(BuilderTest, Binary_Add_Float_Vectors) {
%4 = OpConstantComposite %1 %3 %3 %3
)");
EXPECT_EQ(DumpInstructions(b.functions()[0].instructions()),
R"(%5 = OpFAdd %1 %4 %4
)");
"%5 = " + param.name + " %1 %4 %4\n");
}
INSTANTIATE_TEST_SUITE_P(BuilderTest,
BinaryArithFloatTest,
testing::Values(BinaryData{ast::BinaryOp::kAdd,
"OpFAdd"}));
} // namespace
} // namespace spirv