test_fixture.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. spv_target_env env = SPV_ENV_UNIVERSAL_1_0, bool flip_words = false) {
  101. DestroyBinary();
  102. DestroyDiagnostic();
  103. ScopedContext context(env);
  104. disassemble_options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
  105. spv_result_t error = spvTextToBinary(context.context, txt.c_str(),
  106. txt.size(), &binary, &diagnostic);
  107. if (error) {
  108. spvDiagnosticPrint(diagnostic);
  109. spvDiagnosticDestroy(diagnostic);
  110. }
  111. EXPECT_EQ(SPV_SUCCESS, error);
  112. if (!binary) return "";
  113. MaybeFlipWords(flip_words, binary->code, binary->code + binary->wordCount);
  114. spv_text decoded_text;
  115. error = spvBinaryToText(context.context, binary->code, binary->wordCount,
  116. disassemble_options, &decoded_text, &diagnostic);
  117. if (error) {
  118. spvDiagnosticPrint(diagnostic);
  119. spvDiagnosticDestroy(diagnostic);
  120. }
  121. EXPECT_EQ(SPV_SUCCESS, error) << txt;
  122. const std::string decoded_string = decoded_text->str;
  123. spvTextDestroy(decoded_text);
  124. return decoded_string;
  125. }
  126. // Encodes SPIR-V text into binary. This is expected to succeed.
  127. // The given words are then appended to the binary, and the result
  128. // is then decoded. This is expected to fail.
  129. // Returns the error message.
  130. std::string EncodeSuccessfullyDecodeFailed(
  131. const std::string& txt, const SpirvVector& words_to_append) {
  132. DestroyBinary();
  133. DestroyDiagnostic();
  134. SpirvVector code =
  135. spvtest::Concatenate({CompileSuccessfully(txt), words_to_append});
  136. spv_text decoded_text;
  137. EXPECT_NE(SPV_SUCCESS,
  138. spvBinaryToText(ScopedContext().context, code.data(), code.size(),
  139. SPV_BINARY_TO_TEXT_OPTION_NONE, &decoded_text,
  140. &diagnostic));
  141. if (diagnostic) {
  142. std::string error_message = diagnostic->error;
  143. spvDiagnosticDestroy(diagnostic);
  144. diagnostic = nullptr;
  145. return error_message;
  146. }
  147. return "";
  148. }
  149. // Compiles SPIR-V text, asserts success, and returns the words representing
  150. // the instructions. In particular, skip the words in the SPIR-V header.
  151. SpirvVector CompiledInstructions(const std::string& txt,
  152. spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
  153. const SpirvVector code = CompileSuccessfully(txt, env);
  154. SpirvVector result;
  155. // Extract just the instructions.
  156. // If the code fails to compile, then return the empty vector.
  157. // In any case, don't crash or invoke undefined behaviour.
  158. if (code.size() >= kFirstInstruction)
  159. result = Subvector(code, kFirstInstruction);
  160. return result;
  161. }
  162. void SetText(const std::string& code) {
  163. textString = code;
  164. text.str = textString.c_str();
  165. text.length = textString.size();
  166. }
  167. // Destroys the binary, if it exists.
  168. void DestroyBinary() {
  169. spvBinaryDestroy(binary);
  170. binary = nullptr;
  171. }
  172. // Destroys the diagnostic, if it exists.
  173. void DestroyDiagnostic() {
  174. spvDiagnosticDestroy(diagnostic);
  175. diagnostic = nullptr;
  176. }
  177. spv_diagnostic diagnostic;
  178. std::string textString;
  179. spv_text_t text;
  180. spv_binary binary;
  181. };
  182. using TextToBinaryTest = TextToBinaryTestBase<::testing::Test>;
  183. } // namespace spvtest
  184. using RoundTripTest =
  185. spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
  186. #endif // TEST_TEST_FIXTURE_H_