operand_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include <vector>
  15. #include "test/unit_spirv.h"
  16. namespace spvtools {
  17. namespace {
  18. using GetTargetTest = ::testing::TestWithParam<spv_target_env>;
  19. using ::testing::ValuesIn;
  20. TEST_P(GetTargetTest, Default) {
  21. spv_operand_table table;
  22. ASSERT_EQ(SPV_SUCCESS, spvOperandTableGet(&table, GetParam()));
  23. ASSERT_NE(0u, table->count);
  24. ASSERT_NE(nullptr, table->types);
  25. }
  26. TEST_P(GetTargetTest, InvalidPointerTable) {
  27. ASSERT_EQ(SPV_ERROR_INVALID_POINTER, spvOperandTableGet(nullptr, GetParam()));
  28. }
  29. INSTANTIATE_TEST_SUITE_P(OperandTableGet, GetTargetTest,
  30. ValuesIn(std::vector<spv_target_env>{
  31. SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
  32. SPV_ENV_VULKAN_1_0}));
  33. TEST(OperandString, AllAreDefinedExceptVariable) {
  34. // None has no string, so don't test it.
  35. EXPECT_EQ(0u, SPV_OPERAND_TYPE_NONE);
  36. // Start testing at enum with value 1, skipping None.
  37. for (int i = 1; i < int(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES); i++) {
  38. const auto type = static_cast<spv_operand_type_t>(i);
  39. if (spvOperandIsVariable(type)) {
  40. EXPECT_STREQ("unknown", spvOperandTypeStr(type))
  41. << " variable type " << i << " has a name '"
  42. << spvOperandTypeStr(type) << "'when it should not";
  43. } else {
  44. EXPECT_STRNE("unknown", spvOperandTypeStr(type))
  45. << " operand type " << i << " has no name when it should";
  46. }
  47. }
  48. }
  49. TEST(OperandIsConcreteMask, Sample) {
  50. // Check a few operand types preceding the concrete mask types.
  51. EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_NONE));
  52. EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_ID));
  53. EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_LITERAL_INTEGER));
  54. EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_CAPABILITY));
  55. // Check all the concrete mask operand types.
  56. EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_IMAGE));
  57. EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_FP_FAST_MATH_MODE));
  58. EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_SELECTION_CONTROL));
  59. EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_LOOP_CONTROL));
  60. EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_FUNCTION_CONTROL));
  61. EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_MEMORY_ACCESS));
  62. // Check a few operand types after the concrete mask types, including the
  63. // optional forms for Image and MemoryAccess.
  64. EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_OPTIONAL_ID));
  65. EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_OPTIONAL_IMAGE));
  66. EXPECT_FALSE(
  67. spvOperandIsConcreteMask(SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS));
  68. }
  69. TEST(OperandType, NoneTypeClassification) {
  70. EXPECT_FALSE(spvOperandIsConcrete(SPV_OPERAND_TYPE_NONE));
  71. EXPECT_FALSE(spvOperandIsOptional(SPV_OPERAND_TYPE_NONE));
  72. EXPECT_FALSE(spvOperandIsVariable(SPV_OPERAND_TYPE_NONE));
  73. }
  74. TEST(OperandType, EndSentinelTypeClassification) {
  75. EXPECT_FALSE(spvOperandIsConcrete(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES));
  76. EXPECT_FALSE(spvOperandIsOptional(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES));
  77. EXPECT_FALSE(spvOperandIsVariable(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES));
  78. }
  79. TEST(OperandType, WidthForcingTypeClassification) {
  80. EXPECT_FALSE(spvOperandIsConcrete(SPV_FORCE_32BIT_spv_operand_type_t));
  81. EXPECT_FALSE(spvOperandIsOptional(SPV_FORCE_32BIT_spv_operand_type_t));
  82. EXPECT_FALSE(spvOperandIsVariable(SPV_FORCE_32BIT_spv_operand_type_t));
  83. }
  84. TEST(OperandType, EachTypeIsEitherConcreteOrOptionalNotBoth) {
  85. EXPECT_EQ(0u, SPV_OPERAND_TYPE_NONE);
  86. // Start testing at enum with value 1, skipping None.
  87. for (int i = 1; i < int(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES); i++) {
  88. const auto type = static_cast<spv_operand_type_t>(i);
  89. EXPECT_NE(spvOperandIsConcrete(type), spvOperandIsOptional(type))
  90. << " operand type " << int(type) << " concrete? "
  91. << int(spvOperandIsConcrete(type)) << " optional? "
  92. << int(spvOperandIsOptional(type));
  93. }
  94. }
  95. TEST(OperandType, EachVariableTypeIsOptional) {
  96. EXPECT_EQ(0u, SPV_OPERAND_TYPE_NONE);
  97. // Start testing at enum with value 1, skipping None.
  98. for (int i = 1; i < int(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES); i++) {
  99. const auto type = static_cast<spv_operand_type_t>(i);
  100. if (spvOperandIsVariable(type)) {
  101. EXPECT_TRUE(spvOperandIsOptional(type)) << " variable type " << int(type);
  102. }
  103. }
  104. }
  105. } // namespace
  106. } // namespace spvtools