preserve_numeric_ids_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright (c) 2017 Google 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. // Tests for unique type declaration rules validator.
  15. #include <string>
  16. #include "source/text.h"
  17. #include "source/text_handler.h"
  18. #include "test/test_fixture.h"
  19. namespace spvtools {
  20. namespace {
  21. using spvtest::ScopedContext;
  22. // Converts code to binary and then back to text.
  23. spv_result_t ToBinaryAndBack(
  24. const std::string& before, std::string* after,
  25. uint32_t text_to_binary_options = SPV_TEXT_TO_BINARY_OPTION_NONE,
  26. uint32_t binary_to_text_options = SPV_BINARY_TO_TEXT_OPTION_NONE,
  27. spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
  28. ScopedContext ctx(env);
  29. spv_binary binary;
  30. spv_text text;
  31. spv_result_t result =
  32. spvTextToBinaryWithOptions(ctx.context, before.c_str(), before.size(),
  33. text_to_binary_options, &binary, nullptr);
  34. if (result != SPV_SUCCESS) {
  35. return result;
  36. }
  37. result = spvBinaryToText(ctx.context, binary->code, binary->wordCount,
  38. binary_to_text_options, &text, nullptr);
  39. if (result != SPV_SUCCESS) {
  40. return result;
  41. }
  42. *after = std::string(text->str, text->length);
  43. spvBinaryDestroy(binary);
  44. spvTextDestroy(text);
  45. return SPV_SUCCESS;
  46. }
  47. TEST(ToBinaryAndBack, DontPreserveNumericIds) {
  48. const std::string before =
  49. R"(OpCapability Addresses
  50. OpCapability Kernel
  51. OpCapability GenericPointer
  52. OpCapability Linkage
  53. OpMemoryModel Physical32 OpenCL
  54. %i32 = OpTypeInt 32 1
  55. %u32 = OpTypeInt 32 0
  56. %f32 = OpTypeFloat 32
  57. %200 = OpTypeVoid
  58. %300 = OpTypeFunction %200
  59. %main = OpFunction %200 None %300
  60. %entry = OpLabel
  61. %100 = OpConstant %u32 100
  62. %1 = OpConstant %u32 200
  63. %2 = OpConstant %u32 300
  64. OpReturn
  65. OpFunctionEnd
  66. )";
  67. const std::string expected =
  68. R"(OpCapability Addresses
  69. OpCapability Kernel
  70. OpCapability GenericPointer
  71. OpCapability Linkage
  72. OpMemoryModel Physical32 OpenCL
  73. %1 = OpTypeInt 32 1
  74. %2 = OpTypeInt 32 0
  75. %3 = OpTypeFloat 32
  76. %4 = OpTypeVoid
  77. %5 = OpTypeFunction %4
  78. %6 = OpFunction %4 None %5
  79. %7 = OpLabel
  80. %8 = OpConstant %2 100
  81. %9 = OpConstant %2 200
  82. %10 = OpConstant %2 300
  83. OpReturn
  84. OpFunctionEnd
  85. )";
  86. std::string after;
  87. EXPECT_EQ(SPV_SUCCESS,
  88. ToBinaryAndBack(before, &after, SPV_TEXT_TO_BINARY_OPTION_NONE,
  89. SPV_BINARY_TO_TEXT_OPTION_NO_HEADER));
  90. EXPECT_EQ(expected, after);
  91. }
  92. TEST(TextHandler, PreserveNumericIds) {
  93. const std::string before =
  94. R"(OpCapability Addresses
  95. OpCapability Kernel
  96. OpCapability GenericPointer
  97. OpCapability Linkage
  98. OpMemoryModel Physical32 OpenCL
  99. %i32 = OpTypeInt 32 1
  100. %u32 = OpTypeInt 32 0
  101. %f32 = OpTypeFloat 32
  102. %200 = OpTypeVoid
  103. %300 = OpTypeFunction %200
  104. %main = OpFunction %200 None %300
  105. %entry = OpLabel
  106. %100 = OpConstant %u32 100
  107. %1 = OpConstant %u32 200
  108. %2 = OpConstant %u32 300
  109. OpReturn
  110. OpFunctionEnd
  111. )";
  112. const std::string expected =
  113. R"(OpCapability Addresses
  114. OpCapability Kernel
  115. OpCapability GenericPointer
  116. OpCapability Linkage
  117. OpMemoryModel Physical32 OpenCL
  118. %3 = OpTypeInt 32 1
  119. %4 = OpTypeInt 32 0
  120. %5 = OpTypeFloat 32
  121. %200 = OpTypeVoid
  122. %300 = OpTypeFunction %200
  123. %6 = OpFunction %200 None %300
  124. %7 = OpLabel
  125. %100 = OpConstant %4 100
  126. %1 = OpConstant %4 200
  127. %2 = OpConstant %4 300
  128. OpReturn
  129. OpFunctionEnd
  130. )";
  131. std::string after;
  132. EXPECT_EQ(SPV_SUCCESS,
  133. ToBinaryAndBack(before, &after,
  134. SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS,
  135. SPV_BINARY_TO_TEXT_OPTION_NO_HEADER));
  136. EXPECT_EQ(expected, after);
  137. }
  138. } // namespace
  139. } // namespace spvtools