From 74af05f012bf7cee9a9c1dc8672a52f0692d72a1 Mon Sep 17 00:00:00 2001 From: David Neto Date: Mon, 21 Sep 2015 17:16:45 -0400 Subject: [PATCH] Cleanups for EnumCase Make it a class, since it has non-trivial behaviour for converting the enumerated value to a uint32_t value. (Comply with style guide.) Merge EnumCaseWithOperands into EnumCase. --- test/TextToBinary.Constant.cpp | 8 +-- test/TextToBinary.ControlFlow.cpp | 8 +-- test/TextToBinary.Function.cpp | 6 +-- test/TextToBinary.Group.cpp | 4 +- test/TextToBinary.Memory.cpp | 74 ++++++++++++--------------- test/TextToBinary.ModeSetting.cpp | 4 +- test/TextToBinary.TypeDeclaration.cpp | 12 ++--- test/UnitSPIRV.h | 21 ++++++-- 8 files changed, 69 insertions(+), 68 deletions(-) diff --git a/test/TextToBinary.Constant.cpp b/test/TextToBinary.Constant.cpp index bc0a7ff1..37ef635c 100755 --- a/test/TextToBinary.Constant.cpp +++ b/test/TextToBinary.Constant.cpp @@ -44,10 +44,10 @@ using SamplerAddressingModeTest = spvtest::TextToBinaryTestBase< TEST_P(SamplerAddressingModeTest, AnySamplerAddressingMode) { std::string input = - "%result = OpConstantSampler %type " + GetParam().name + " 0 Nearest"; + "%result = OpConstantSampler %type " + GetParam().name() + " 0 Nearest"; EXPECT_THAT(CompiledInstructions(input), Eq(MakeInstruction(spv::OpConstantSampler, - {1, 2, GetParam().get_value(), 0, 0}))); + {1, 2, GetParam().value(), 0, 0}))); } // clang-format off @@ -71,10 +71,10 @@ using SamplerFilterModeTest = spvtest::TextToBinaryTestBase< TEST_P(SamplerFilterModeTest, AnySamplerFilterMode) { std::string input = - "%result = OpConstantSampler %type Clamp 0 " + GetParam().name; + "%result = OpConstantSampler %type Clamp 0 " + GetParam().name(); EXPECT_THAT(CompiledInstructions(input), Eq(MakeInstruction(spv::OpConstantSampler, - {1, 2, 2, 0, GetParam().get_value()}))); + {1, 2, 2, 0, GetParam().value()}))); } // clang-format off diff --git a/test/TextToBinary.ControlFlow.cpp b/test/TextToBinary.ControlFlow.cpp index 946cb0e7..8cff57d2 100755 --- a/test/TextToBinary.ControlFlow.cpp +++ b/test/TextToBinary.ControlFlow.cpp @@ -43,10 +43,10 @@ using OpSelectionMergeTest = spvtest::TextToBinaryTestBase< ::testing::TestWithParam>>; TEST_P(OpSelectionMergeTest, AnySingleSelectionControlMask) { - std::string input = "OpSelectionMerge %1 " + GetParam().name; + std::string input = "OpSelectionMerge %1 " + GetParam().name(); EXPECT_THAT( CompiledInstructions(input), - Eq(MakeInstruction(spv::OpSelectionMerge, {1, GetParam().get_value()}))); + Eq(MakeInstruction(spv::OpSelectionMerge, {1, GetParam().value()}))); } // clang-format off @@ -74,10 +74,10 @@ using OpLoopMergeTest = spvtest::TextToBinaryTestBase< ::testing::TestWithParam>>; TEST_P(OpLoopMergeTest, AnySingleLoopControlMask) { - std::string input = "OpLoopMerge %1 " + GetParam().name; + std::string input = "OpLoopMerge %1 " + GetParam().name(); EXPECT_THAT( CompiledInstructions(input), - Eq(MakeInstruction(spv::OpLoopMerge, {1, GetParam().get_value()}))); + Eq(MakeInstruction(spv::OpLoopMerge, {1, GetParam().value()}))); } // clang-format off diff --git a/test/TextToBinary.Function.cpp b/test/TextToBinary.Function.cpp index 382001a0..3cd72464 100755 --- a/test/TextToBinary.Function.cpp +++ b/test/TextToBinary.Function.cpp @@ -45,14 +45,14 @@ using OpFunctionControlTest = spvtest::TextToBinaryTestBase< TEST_P(OpFunctionControlTest, AnySingleFunctionControlMask) { std::string input = "%result_id = OpFunction %result_type " + - GetParam().name + " %function_type "; + GetParam().name() + " %function_type "; EXPECT_THAT( CompiledInstructions(input), - Eq(MakeInstruction(spv::OpFunction, {1, 2, GetParam().get_value(), 3}))); + Eq(MakeInstruction(spv::OpFunction, {1, 2, GetParam().value(), 3}))); } // clang-format off -#define CASE(VALUE,NAME) { spv::FunctionControl##VALUE, NAME} +#define CASE(VALUE,NAME) { spv::FunctionControl##VALUE, NAME } INSTANTIATE_TEST_CASE_P(TextToBinaryFunctionTest, OpFunctionControlTest, ::testing::ValuesIn(std::vector>{ CASE(MaskNone, "None"), diff --git a/test/TextToBinary.Group.cpp b/test/TextToBinary.Group.cpp index 68d326c4..974df686 100755 --- a/test/TextToBinary.Group.cpp +++ b/test/TextToBinary.Group.cpp @@ -44,10 +44,10 @@ using GroupOperationTest = spvtest::TextToBinaryTestBase< TEST_P(GroupOperationTest, AnyGroupOperation) { std::string input = - "%result = OpGroupIAdd %type %scope " + GetParam().name + " %x"; + "%result = OpGroupIAdd %type %scope " + GetParam().name() + " %x"; EXPECT_THAT(CompiledInstructions(input), Eq(MakeInstruction(spv::OpGroupIAdd, - {1, 2, 3, GetParam().get_value(), 4}))); + {1, 2, 3, GetParam().value(), 4}))); } // clang-format off diff --git a/test/TextToBinary.Memory.cpp b/test/TextToBinary.Memory.cpp index 41ed3a32..22610973 100755 --- a/test/TextToBinary.Memory.cpp +++ b/test/TextToBinary.Memory.cpp @@ -40,40 +40,29 @@ using spvtest::MakeInstruction; using spvtest::TextToBinaryTest; using ::testing::Eq; -// An example case for an enumerated value. -template -struct EnumCaseWithOperands { - uint32_t get_value() const { return static_cast(value); } - E value; - std::string name; - std::vector operands; -}; - // Test assembly of Memory Access masks using MemoryAccessTest = spvtest::TextToBinaryTestBase< - ::testing::TestWithParam>>; + ::testing::TestWithParam>>; TEST_P(MemoryAccessTest, AnySingleMemoryAccessMask) { std::stringstream input; - input << "OpStore %ptr %value " << GetParam().name; - for (auto operand : GetParam().operands) input << " " << operand; - std::vector expected_operands{1, 2, GetParam().get_value()}; - expected_operands.insert(expected_operands.end(), GetParam().operands.begin(), - GetParam().operands.end()); + input << "OpStore %ptr %value " << GetParam().name(); + for (auto operand : GetParam().operands()) input << " " << operand; + std::vector expected_operands{1, 2, GetParam().value()}; + expected_operands.insert(expected_operands.end(), GetParam().operands().begin(), + GetParam().operands().end()); EXPECT_THAT(CompiledInstructions(input.str()), Eq(MakeInstruction(spv::OpStore, expected_operands))); } -// clang-format off -INSTANTIATE_TEST_CASE_P(TextToBinaryMemoryAccessTest, MemoryAccessTest, - ::testing::ValuesIn(std::vector>{ - {spv::MemoryAccessMaskNone, "None", {}}, - {spv::MemoryAccessVolatileMask, "Volatile", {}}, - {spv::MemoryAccessAlignedMask, "Aligned", {16}}, - })); -#undef CASE -// clang-format on +INSTANTIATE_TEST_CASE_P( + TextToBinaryMemoryAccessTest, MemoryAccessTest, + ::testing::ValuesIn(std::vector>{ + {spv::MemoryAccessMaskNone, "None", {}}, + {spv::MemoryAccessVolatileMask, "Volatile", {}}, + {spv::MemoryAccessAlignedMask, "Aligned", {16}}, + })); TEST_F(TextToBinaryTest, CombinedMemoryAccessMask) { const std::string input = "OpStore %ptr %value Volatile|Aligned 16"; @@ -87,31 +76,32 @@ TEST_F(TextToBinaryTest, CombinedMemoryAccessMask) { // Test Storage Class enum values using StorageClassTest = spvtest::TextToBinaryTestBase< - ::testing::TestWithParam>>; + ::testing::TestWithParam>>; TEST_P(StorageClassTest, AnyStorageClass) { - std::string input = "%1 = OpVariable %2 " + GetParam().name; + std::string input = "%1 = OpVariable %2 " + GetParam().name(); EXPECT_THAT(CompiledInstructions(input), - Eq(MakeInstruction(spv::OpVariable, {2, 1, GetParam().get_value()}))); + Eq(MakeInstruction(spv::OpVariable, {2, 1, GetParam().value()}))); } // clang-format off #define CASE(NAME) { spv::StorageClass##NAME, #NAME, {} } -INSTANTIATE_TEST_CASE_P(TextToBinaryStorageClassTest, StorageClassTest, - ::testing::ValuesIn(std::vector>{ - // TODO(dneto): There are more storage classes in Rev32 and later. - CASE(UniformConstant), - CASE(Input), - CASE(Uniform), - CASE(Output), - CASE(WorkgroupLocal), - CASE(WorkgroupGlobal), - CASE(PrivateGlobal), - CASE(Function), - CASE(Generic), - CASE(AtomicCounter), - CASE(Image), - })); +INSTANTIATE_TEST_CASE_P( + TextToBinaryStorageClassTest, StorageClassTest, + ::testing::ValuesIn(std::vector>{ + // TODO(dneto): There are more storage classes in Rev32 and later. + CASE(UniformConstant), + CASE(Input), + CASE(Uniform), + CASE(Output), + CASE(WorkgroupLocal), + CASE(WorkgroupGlobal), + CASE(PrivateGlobal), + CASE(Function), + CASE(Generic), + CASE(AtomicCounter), + CASE(Image), + })); #undef CASE // clang-format on diff --git a/test/TextToBinary.ModeSetting.cpp b/test/TextToBinary.ModeSetting.cpp index 53799cff..28323450 100755 --- a/test/TextToBinary.ModeSetting.cpp +++ b/test/TextToBinary.ModeSetting.cpp @@ -203,9 +203,9 @@ using OpCapabilityTest = spvtest::TextToBinaryTestBase< ::testing::TestWithParam>>; TEST_P(OpCapabilityTest, AnyCapability) { - std::string input = "OpCapability " + GetParam().name; + std::string input = "OpCapability " + GetParam().name(); EXPECT_THAT(CompiledInstructions(input), - Eq(MakeInstruction(spv::OpCapability, {GetParam().get_value()}))); + Eq(MakeInstruction(spv::OpCapability, {GetParam().value()}))); } // clang-format off diff --git a/test/TextToBinary.TypeDeclaration.cpp b/test/TextToBinary.TypeDeclaration.cpp index 909058b3..23f6084c 100755 --- a/test/TextToBinary.TypeDeclaration.cpp +++ b/test/TextToBinary.TypeDeclaration.cpp @@ -44,10 +44,10 @@ using DimTest = spvtest::TextToBinaryTestBase< TEST_P(DimTest, AnyDim) { std::string input = "%imageType = OpTypeImage %sampledType " + - GetParam().name + " 2 3 0 4 Rgba8"; + GetParam().name() + " 2 3 0 4 Rgba8"; EXPECT_THAT( CompiledInstructions(input), - Eq(MakeInstruction(spv::OpTypeImage, {1, 2, GetParam().get_value(), 2, 3, 0, 4, + Eq(MakeInstruction(spv::OpTypeImage, {1, 2, GetParam().value(), 2, 3, 0, 4, spv::ImageFormatRgba8}))); } @@ -74,10 +74,10 @@ using ImageFormatTest = spvtest::TextToBinaryTestBase< TEST_P(ImageFormatTest, AnyImageFormat) { std::string input = - "%imageType = OpTypeImage %sampledType 1D 2 3 0 4 " + GetParam().name; + "%imageType = OpTypeImage %sampledType 1D 2 3 0 4 " + GetParam().name(); EXPECT_THAT(CompiledInstructions(input), Eq(MakeInstruction(spv::OpTypeImage, {1, 2, spv::Dim1D, 2, 3, 0, - 4, GetParam().get_value()}))); + 4, GetParam().value()}))); } // clang-format off @@ -138,10 +138,10 @@ using OpTypePipeTest = spvtest::TextToBinaryTestBase< TEST_P(OpTypePipeTest, AnyAccessQualifier) { // TODO(dneto): In Rev31 and later, pipes are opaque, and so the %2, which // is the type-of-element operand, should be dropped. - std::string input = "%1 = OpTypePipe %2 " + GetParam().name; + std::string input = "%1 = OpTypePipe %2 " + GetParam().name(); EXPECT_THAT( CompiledInstructions(input), - Eq(MakeInstruction(spv::OpTypePipe, {1, 2, GetParam().get_value()}))); + Eq(MakeInstruction(spv::OpTypePipe, {1, 2, GetParam().value()}))); } // clang-format off diff --git a/test/UnitSPIRV.h b/test/UnitSPIRV.h index 04ee974a..9a3c8a77 100755 --- a/test/UnitSPIRV.h +++ b/test/UnitSPIRV.h @@ -150,12 +150,23 @@ struct AutoText { spv_text_t text; }; -// An example case for an enumerated value. +// An example case for an enumerated value, optionally with operands. template -struct EnumCase { - uint32_t get_value() const { return static_cast(value); } - E value; - std::string name; +class EnumCase { + public: + EnumCase(E value, std::string name, std::vector operands = {}) + : enum_value_(value), name_(name), operands_(operands) {} + // Returns the enum value as a uint32_t. + uint32_t value() const { return static_cast(enum_value_); } + // Returns the name of the enumerant. + const std::string& name() const { return name_; } + // Returns a reference to the operands. + const std::vector& operands() const { return operands_; } + + private: + E enum_value_; + std::string name_; + std::vector operands_; }; #define I32_ENDIAN_HOST (o32_host_order.value)