Setup dependent external sources
[platform/upstream/VK-GL-CTS.git] / external / spirv-tools / src / test / test_fixture.h
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
15 #ifndef LIBSPIRV_TEST_TEST_FIXTURE_H_
16 #define LIBSPIRV_TEST_TEST_FIXTURE_H_
17
18 #include "unit_spirv.h"
19
20 namespace spvtest {
21
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); }
27   spv_context context;
28 };
29
30 // Common setup for TextToBinary tests. SetText() should be called to populate
31 // the actual test text.
32 template <typename T>
33 class TextToBinaryTestBase : public T {
34  public:
35   // Shorthand for SPIR-V compilation result.
36   using SpirvVector = std::vector<uint32_t>;
37
38   // Offset into a SpirvVector at which the first instruction starts.
39   static const SpirvVector::size_type kFirstInstruction = 5;
40
41   TextToBinaryTestBase() : diagnostic(nullptr), text(), binary(nullptr) {
42     char textStr[] = "substitute the text member variable with your test";
43     text = {textStr, strlen(textStr)};
44   }
45
46   virtual ~TextToBinaryTestBase() {
47     DestroyBinary();
48     if (diagnostic) spvDiagnosticDestroy(diagnostic);
49   }
50
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());
55   }
56
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) {
61     spv_result_t status =
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);
68       DestroyBinary();
69     } else {
70       spvDiagnosticPrint(diagnostic);
71     }
72     return code_copy;
73   }
74
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))
82         << txt;
83     DestroyBinary();
84     return diagnostic->error;
85   }
86
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) {
93     DestroyBinary();
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);
98     if (error) {
99       spvDiagnosticPrint(diagnostic);
100       spvDiagnosticDestroy(diagnostic);
101     }
102     EXPECT_EQ(SPV_SUCCESS, error);
103     if (!binary) return "";
104
105     spv_text decoded_text;
106     error = spvBinaryToText(context.context, binary->code, binary->wordCount,
107                             disassemble_options, &decoded_text, &diagnostic);
108     if (error) {
109       spvDiagnosticPrint(diagnostic);
110       spvDiagnosticDestroy(diagnostic);
111     }
112     EXPECT_EQ(SPV_SUCCESS, error) << txt;
113
114     const std::string decoded_string = decoded_text->str;
115     spvTextDestroy(decoded_text);
116
117     return decoded_string;
118   }
119
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) {
126     SpirvVector code =
127         spvtest::Concatenate({CompileSuccessfully(txt), words_to_append});
128
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,
133                               &diagnostic));
134     if (diagnostic) {
135       std::string error_message = diagnostic->error;
136       spvDiagnosticDestroy(diagnostic);
137       diagnostic = nullptr;
138       return error_message;
139     }
140     return "";
141   }
142
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);
148     SpirvVector result;
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);
154     return result;
155   }
156
157   void SetText(const std::string& code) {
158     textString = code;
159     text.str = textString.c_str();
160     text.length = textString.size();
161   }
162
163   // Destroys the binary, if it exists.
164   void DestroyBinary() {
165     spvBinaryDestroy(binary);
166     binary = nullptr;
167   }
168
169   spv_diagnostic diagnostic;
170
171   std::string textString;
172   spv_text_t text;
173   spv_binary binary;
174 };
175
176 using TextToBinaryTest = TextToBinaryTestBase<::testing::Test>;
177 }  // namespace spvtest
178
179 using RoundTripTest =
180     spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
181
182 #endif  // LIBSPIRV_TEST_TEST_FIXTURE_H_