test_fixture.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #ifndef TEST_TEST_FIXTURE_H_
  15. #define TEST_TEST_FIXTURE_H_
  16. #include <algorithm>
  17. #include <string>
  18. #include <vector>
  19. #include "test/unit_spirv.h"
  20. namespace spvtest {
  21. // RAII for spv_context.
  22. struct ScopedContext {
  23. ScopedContext(spv_target_env env = SPV_ENV_UNIVERSAL_1_0)
  24. : context(spvContextCreate(env)) {}
  25. ~ScopedContext() { spvContextDestroy(context); }
  26. spv_context context;
  27. };
  28. // Common setup for TextToBinary tests. SetText() should be called to populate
  29. // the actual test text.
  30. template <typename T>
  31. class TextToBinaryTestBase : public T {
  32. public:
  33. // Shorthand for SPIR-V compilation result.
  34. using SpirvVector = std::vector<uint32_t>;
  35. // Offset into a SpirvVector at which the first instruction starts.
  36. static const SpirvVector::size_type kFirstInstruction = 5;
  37. TextToBinaryTestBase() : diagnostic(nullptr), text(), binary(nullptr) {
  38. char textStr[] = "substitute the text member variable with your test";
  39. text = {textStr, strlen(textStr)};
  40. }
  41. ~TextToBinaryTestBase() override {
  42. DestroyBinary();
  43. if (diagnostic) spvDiagnosticDestroy(diagnostic);
  44. }
  45. // Returns subvector v[from:end).
  46. SpirvVector Subvector(const SpirvVector& v, SpirvVector::size_type from) {
  47. assert(from <= v.size());
  48. return SpirvVector(v.begin() + from, v.end());
  49. }
  50. // Compiles SPIR-V text in the given assembly syntax format, asserting
  51. // compilation success. Returns the compiled code.
  52. SpirvVector CompileSuccessfully(const std::string& txt,
  53. spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
  54. DestroyBinary();
  55. DestroyDiagnostic();
  56. spv_result_t status =
  57. spvTextToBinary(ScopedContext(env).context, txt.c_str(), txt.size(),
  58. &binary, &diagnostic);
  59. EXPECT_EQ(SPV_SUCCESS, status) << txt;
  60. SpirvVector code_copy;
  61. if (status == SPV_SUCCESS) {
  62. code_copy = SpirvVector(binary->code, binary->code + binary->wordCount);
  63. DestroyBinary();
  64. } else {
  65. spvDiagnosticPrint(diagnostic);
  66. }
  67. return code_copy;
  68. }
  69. // Compiles SPIR-V text with the given format, asserting compilation failure.
  70. // Returns the error message(s).
  71. std::string CompileFailure(const std::string& txt,
  72. spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
  73. DestroyBinary();
  74. DestroyDiagnostic();
  75. EXPECT_NE(SPV_SUCCESS,
  76. spvTextToBinary(ScopedContext(env).context, txt.c_str(),
  77. txt.size(), &binary, &diagnostic))
  78. << txt;
  79. DestroyBinary();
  80. return diagnostic->error;
  81. }
  82. // Potentially flip the words in the binary representation to the other
  83. // endianness
  84. template <class It>
  85. void MaybeFlipWords(bool flip_words, It begin, It end) {
  86. SCOPED_TRACE(flip_words ? "Flipped Endianness" : "Normal Endianness");
  87. if (flip_words) {
  88. std::transform(begin, end, begin, [](const uint32_t raw_word) {
  89. return spvFixWord(raw_word, I32_ENDIAN_HOST == I32_ENDIAN_BIG
  90. ? SPV_ENDIANNESS_LITTLE
  91. : SPV_ENDIANNESS_BIG);
  92. });
  93. }
  94. }
  95. // Encodes SPIR-V text into binary and then decodes the binary using
  96. // given options. Returns the decoded text.
  97. std::string EncodeAndDecodeSuccessfully(
  98. const std::string& txt,
  99. uint32_t disassemble_options = SPV_BINARY_TO_TEXT_OPTION_NONE,
  100. uint32_t assemble_options = SPV_TEXT_TO_BINARY_OPTION_NONE,
  101. spv_target_env env = SPV_ENV_UNIVERSAL_1_0, bool flip_words = false) {
  102. DestroyBinary();
  103. DestroyDiagnostic();
  104. ScopedContext context(env);
  105. disassemble_options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
  106. spv_result_t error =
  107. spvTextToBinaryWithOptions(context.context, txt.c_str(), txt.size(),
  108. assemble_options, &binary, &diagnostic);
  109. if (error) {
  110. spvDiagnosticPrint(diagnostic);
  111. spvDiagnosticDestroy(diagnostic);
  112. }
  113. EXPECT_EQ(SPV_SUCCESS, error);
  114. if (!binary) return "";
  115. MaybeFlipWords(flip_words, binary->code, binary->code + binary->wordCount);
  116. spv_text decoded_text;
  117. error = spvBinaryToText(context.context, binary->code, binary->wordCount,
  118. disassemble_options, &decoded_text, &diagnostic);
  119. if (error) {
  120. spvDiagnosticPrint(diagnostic);
  121. spvDiagnosticDestroy(diagnostic);
  122. }
  123. EXPECT_EQ(SPV_SUCCESS, error) << txt;
  124. const std::string decoded_string = decoded_text->str;
  125. spvTextDestroy(decoded_text);
  126. return decoded_string;
  127. }
  128. // Encodes SPIR-V text into binary. This is expected to succeed.
  129. // The given words are then appended to the binary, and the result
  130. // is then decoded. This is expected to fail.
  131. // Returns the error message.
  132. std::string EncodeSuccessfullyDecodeFailed(
  133. const std::string& txt, const SpirvVector& words_to_append) {
  134. DestroyBinary();
  135. DestroyDiagnostic();
  136. SpirvVector code =
  137. spvtest::Concatenate({CompileSuccessfully(txt), words_to_append});
  138. spv_text decoded_text;
  139. EXPECT_NE(SPV_SUCCESS,
  140. spvBinaryToText(ScopedContext().context, code.data(), code.size(),
  141. SPV_BINARY_TO_TEXT_OPTION_NONE, &decoded_text,
  142. &diagnostic));
  143. if (diagnostic) {
  144. std::string error_message = diagnostic->error;
  145. spvDiagnosticDestroy(diagnostic);
  146. diagnostic = nullptr;
  147. return error_message;
  148. }
  149. return "";
  150. }
  151. // Compiles SPIR-V text, asserts success, and returns the words representing
  152. // the instructions. In particular, skip the words in the SPIR-V header.
  153. SpirvVector CompiledInstructions(const std::string& txt,
  154. spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
  155. const SpirvVector code = CompileSuccessfully(txt, env);
  156. SpirvVector result;
  157. // Extract just the instructions.
  158. // If the code fails to compile, then return the empty vector.
  159. // In any case, don't crash or invoke undefined behaviour.
  160. if (code.size() >= kFirstInstruction)
  161. result = Subvector(code, kFirstInstruction);
  162. return result;
  163. }
  164. void SetText(const std::string& code) {
  165. textString = code;
  166. text.str = textString.c_str();
  167. text.length = textString.size();
  168. }
  169. // Destroys the binary, if it exists.
  170. void DestroyBinary() {
  171. spvBinaryDestroy(binary);
  172. binary = nullptr;
  173. }
  174. // Destroys the diagnostic, if it exists.
  175. void DestroyDiagnostic() {
  176. spvDiagnosticDestroy(diagnostic);
  177. diagnostic = nullptr;
  178. }
  179. spv_diagnostic diagnostic;
  180. std::string textString;
  181. spv_text_t text;
  182. spv_binary binary;
  183. };
  184. using TextToBinaryTest = TextToBinaryTestBase<::testing::Test>;
  185. } // namespace spvtest
  186. using RoundTripTest =
  187. spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
  188. #endif // TEST_TEST_FIXTURE_H_