1 // Copyright (c) 2015-2016 The Khronos Group Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef LIBSPIRV_TEST_TEST_FIXTURE_H_
16 #define LIBSPIRV_TEST_TEST_FIXTURE_H_
18 #include "unit_spirv.h"
22 // RAII for spv_context.
23 struct ScopedContext {
24 ScopedContext(spv_target_env env = SPV_ENV_UNIVERSAL_1_0)
25 : context(spvContextCreate(env)) {}
26 ~ScopedContext() { spvContextDestroy(context); }
30 // Common setup for TextToBinary tests. SetText() should be called to populate
31 // the actual test text.
33 class TextToBinaryTestBase : public T {
35 // Shorthand for SPIR-V compilation result.
36 using SpirvVector = std::vector<uint32_t>;
38 // Offset into a SpirvVector at which the first instruction starts.
39 static const SpirvVector::size_type kFirstInstruction = 5;
41 TextToBinaryTestBase() : diagnostic(nullptr), text(), binary(nullptr) {
42 char textStr[] = "substitute the text member variable with your test";
43 text = {textStr, strlen(textStr)};
46 virtual ~TextToBinaryTestBase() {
48 if (diagnostic) spvDiagnosticDestroy(diagnostic);
51 // Returns subvector v[from:end).
52 SpirvVector Subvector(const SpirvVector& v, SpirvVector::size_type from) {
53 assert(from <= v.size());
54 return SpirvVector(v.begin() + from, v.end());
57 // Compiles SPIR-V text in the given assembly syntax format, asserting
58 // compilation success. Returns the compiled code.
59 SpirvVector CompileSuccessfully(const std::string& txt,
60 spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
62 spvTextToBinary(ScopedContext(env).context, txt.c_str(), txt.size(),
63 &binary, &diagnostic);
64 EXPECT_EQ(SPV_SUCCESS, status) << txt;
65 SpirvVector code_copy;
66 if (status == SPV_SUCCESS) {
67 code_copy = SpirvVector(binary->code, binary->code + binary->wordCount);
70 spvDiagnosticPrint(diagnostic);
75 // Compiles SPIR-V text with the given format, asserting compilation failure.
76 // Returns the error message(s).
77 std::string CompileFailure(const std::string& txt,
78 spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
79 EXPECT_NE(SPV_SUCCESS,
80 spvTextToBinary(ScopedContext(env).context, txt.c_str(),
81 txt.size(), &binary, &diagnostic))
84 return diagnostic->error;
87 // Encodes SPIR-V text into binary and then decodes the binary using
88 // given options. Returns the decoded text.
89 std::string EncodeAndDecodeSuccessfully(
90 const std::string& txt,
91 uint32_t disassemble_options = SPV_BINARY_TO_TEXT_OPTION_NONE,
92 spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
94 ScopedContext context(env);
95 disassemble_options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
96 spv_result_t error = spvTextToBinary(context.context, txt.c_str(),
97 txt.size(), &binary, &diagnostic);
99 spvDiagnosticPrint(diagnostic);
100 spvDiagnosticDestroy(diagnostic);
102 EXPECT_EQ(SPV_SUCCESS, error);
103 if (!binary) return "";
105 spv_text decoded_text;
106 error = spvBinaryToText(context.context, binary->code, binary->wordCount,
107 disassemble_options, &decoded_text, &diagnostic);
109 spvDiagnosticPrint(diagnostic);
110 spvDiagnosticDestroy(diagnostic);
112 EXPECT_EQ(SPV_SUCCESS, error) << txt;
114 const std::string decoded_string = decoded_text->str;
115 spvTextDestroy(decoded_text);
117 return decoded_string;
120 // Encodes SPIR-V text into binary. This is expected to succeed.
121 // The given words are then appended to the binary, and the result
122 // is then decoded. This is expected to fail.
123 // Returns the error message.
124 std::string EncodeSuccessfullyDecodeFailed(
125 const std::string& txt, const SpirvVector& words_to_append) {
127 spvtest::Concatenate({CompileSuccessfully(txt), words_to_append});
129 spv_text decoded_text;
130 EXPECT_NE(SPV_SUCCESS,
131 spvBinaryToText(ScopedContext().context, code.data(), code.size(),
132 SPV_BINARY_TO_TEXT_OPTION_NONE, &decoded_text,
135 std::string error_message = diagnostic->error;
136 spvDiagnosticDestroy(diagnostic);
137 diagnostic = nullptr;
138 return error_message;
143 // Compiles SPIR-V text, asserts success, and returns the words representing
144 // the instructions. In particular, skip the words in the SPIR-V header.
145 SpirvVector CompiledInstructions(const std::string& txt,
146 spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
147 const SpirvVector code = CompileSuccessfully(txt, env);
149 // Extract just the instructions.
150 // If the code fails to compile, then return the empty vector.
151 // In any case, don't crash or invoke undefined behaviour.
152 if (code.size() >= kFirstInstruction)
153 result = Subvector(code, kFirstInstruction);
157 void SetText(const std::string& code) {
159 text.str = textString.c_str();
160 text.length = textString.size();
163 // Destroys the binary, if it exists.
164 void DestroyBinary() {
165 spvBinaryDestroy(binary);
169 spv_diagnostic diagnostic;
171 std::string textString;
176 using TextToBinaryTest = TextToBinaryTestBase<::testing::Test>;
177 } // namespace spvtest
179 using RoundTripTest =
180 spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
182 #endif // LIBSPIRV_TEST_TEST_FIXTURE_H_