text_to_binary.function_test.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Assembler tests for instructions in the "Function" section of the
  15. // SPIR-V spec.
  16. #include <string>
  17. #include <vector>
  18. #include "gmock/gmock.h"
  19. #include "test/test_fixture.h"
  20. #include "test/unit_spirv.h"
  21. namespace spvtools {
  22. namespace {
  23. using spvtest::EnumCase;
  24. using spvtest::MakeInstruction;
  25. using spvtest::TextToBinaryTest;
  26. using ::testing::Eq;
  27. // Test OpFunction
  28. using OpFunctionControlTest = spvtest::TextToBinaryTestBase<
  29. ::testing::TestWithParam<EnumCase<SpvFunctionControlMask>>>;
  30. TEST_P(OpFunctionControlTest, AnySingleFunctionControlMask) {
  31. const std::string input = "%result_id = OpFunction %result_type " +
  32. GetParam().name() + " %function_type ";
  33. EXPECT_THAT(
  34. CompiledInstructions(input),
  35. Eq(MakeInstruction(SpvOpFunction, {1, 2, GetParam().value(), 3})));
  36. }
  37. // clang-format off
  38. #define CASE(VALUE,NAME) { SpvFunctionControl##VALUE, NAME }
  39. INSTANTIATE_TEST_SUITE_P(TextToBinaryFunctionTest, OpFunctionControlTest,
  40. ::testing::ValuesIn(std::vector<EnumCase<SpvFunctionControlMask>>{
  41. CASE(MaskNone, "None"),
  42. CASE(InlineMask, "Inline"),
  43. CASE(DontInlineMask, "DontInline"),
  44. CASE(PureMask, "Pure"),
  45. CASE(ConstMask, "Const"),
  46. }));
  47. #undef CASE
  48. // clang-format on
  49. TEST_F(OpFunctionControlTest, CombinedFunctionControlMask) {
  50. // Sample a single combination. This ensures we've integrated
  51. // the instruction parsing logic with spvTextParseMask.
  52. const std::string input =
  53. "%result_id = OpFunction %result_type Inline|Pure|Const %function_type";
  54. const uint32_t expected_mask = SpvFunctionControlInlineMask |
  55. SpvFunctionControlPureMask |
  56. SpvFunctionControlConstMask;
  57. EXPECT_THAT(CompiledInstructions(input),
  58. Eq(MakeInstruction(SpvOpFunction, {1, 2, expected_mask, 3})));
  59. }
  60. TEST_F(OpFunctionControlTest, WrongFunctionControl) {
  61. EXPECT_THAT(CompileFailure("%r = OpFunction %t Inline|Unroll %ft"),
  62. Eq("Invalid function control operand 'Inline|Unroll'."));
  63. }
  64. // TODO(dneto): OpFunctionParameter
  65. // TODO(dneto): OpFunctionEnd
  66. // TODO(dneto): OpFunctionCall
  67. } // namespace
  68. } // namespace spvtools