test_fixture.h 6.5 KB

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