2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-08-21 15:50:09 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-08-21 15:50:09 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-08-21 15:50:09 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-10-27 20:27:05 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef TEST_TEST_FIXTURE_H_
|
|
|
|
#define TEST_TEST_FIXTURE_H_
|
2015-08-21 15:50:09 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "test/unit_spirv.h"
|
2015-08-21 15:50:09 +00:00
|
|
|
|
2015-09-21 15:36:44 +00:00
|
|
|
namespace spvtest {
|
2015-08-31 19:24:32 +00:00
|
|
|
|
2016-04-21 19:46:08 +00:00
|
|
|
// RAII for spv_context.
|
|
|
|
struct ScopedContext {
|
|
|
|
ScopedContext(spv_target_env env = SPV_ENV_UNIVERSAL_1_0)
|
|
|
|
: context(spvContextCreate(env)) {}
|
|
|
|
~ScopedContext() { spvContextDestroy(context); }
|
|
|
|
spv_context context;
|
|
|
|
};
|
|
|
|
|
2015-08-21 15:50:09 +00:00
|
|
|
// Common setup for TextToBinary tests. SetText() should be called to populate
|
|
|
|
// the actual test text.
|
2015-09-02 18:48:33 +00:00
|
|
|
template <typename T>
|
2015-08-21 17:39:34 +00:00
|
|
|
class TextToBinaryTestBase : public T {
|
2015-08-21 15:50:09 +00:00
|
|
|
public:
|
2015-09-02 18:48:33 +00:00
|
|
|
// Shorthand for SPIR-V compilation result.
|
|
|
|
using SpirvVector = std::vector<uint32_t>;
|
|
|
|
|
|
|
|
// Offset into a SpirvVector at which the first instruction starts.
|
2016-04-19 02:25:35 +00:00
|
|
|
static const SpirvVector::size_type kFirstInstruction = 5;
|
2015-09-02 18:48:33 +00:00
|
|
|
|
2016-04-19 02:25:35 +00:00
|
|
|
TextToBinaryTestBase() : diagnostic(nullptr), text(), binary(nullptr) {
|
2015-08-21 15:50:09 +00:00
|
|
|
char textStr[] = "substitute the text member variable with your test";
|
|
|
|
text = {textStr, strlen(textStr)};
|
|
|
|
}
|
|
|
|
|
2021-03-09 13:16:43 +00:00
|
|
|
~TextToBinaryTestBase() override {
|
2015-09-08 19:29:22 +00:00
|
|
|
DestroyBinary();
|
2015-08-21 15:50:09 +00:00
|
|
|
if (diagnostic) spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
|
2015-09-02 18:48:33 +00:00
|
|
|
// Returns subvector v[from:end).
|
|
|
|
SpirvVector Subvector(const SpirvVector& v, SpirvVector::size_type from) {
|
2015-11-02 16:40:59 +00:00
|
|
|
assert(from <= v.size());
|
2015-09-02 18:48:33 +00:00
|
|
|
return SpirvVector(v.begin() + from, v.end());
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:00:00 +00:00
|
|
|
// Compiles SPIR-V text in the given assembly syntax format, asserting
|
|
|
|
// compilation success. Returns the compiled code.
|
2016-04-19 02:25:35 +00:00
|
|
|
SpirvVector CompileSuccessfully(const std::string& txt,
|
|
|
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
2019-01-16 21:53:10 +00:00
|
|
|
DestroyBinary();
|
|
|
|
DestroyDiagnostic();
|
2016-03-31 16:16:51 +00:00
|
|
|
spv_result_t status =
|
2016-04-19 02:25:35 +00:00
|
|
|
spvTextToBinary(ScopedContext(env).context, txt.c_str(), txt.size(),
|
|
|
|
&binary, &diagnostic);
|
2016-01-11 15:54:20 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, status) << txt;
|
2015-09-02 18:48:33 +00:00
|
|
|
SpirvVector code_copy;
|
|
|
|
if (status == SPV_SUCCESS) {
|
|
|
|
code_copy = SpirvVector(binary->code, binary->code + binary->wordCount);
|
2015-09-08 19:29:22 +00:00
|
|
|
DestroyBinary();
|
2015-09-02 18:48:33 +00:00
|
|
|
} else {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
}
|
|
|
|
return code_copy;
|
|
|
|
}
|
|
|
|
|
2015-09-28 19:02:21 +00:00
|
|
|
// Compiles SPIR-V text with the given format, asserting compilation failure.
|
|
|
|
// Returns the error message(s).
|
2016-04-19 02:25:35 +00:00
|
|
|
std::string CompileFailure(const std::string& txt,
|
|
|
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
2019-01-16 21:53:10 +00:00
|
|
|
DestroyBinary();
|
|
|
|
DestroyDiagnostic();
|
2016-04-19 02:25:35 +00:00
|
|
|
EXPECT_NE(SPV_SUCCESS,
|
|
|
|
spvTextToBinary(ScopedContext(env).context, txt.c_str(),
|
|
|
|
txt.size(), &binary, &diagnostic))
|
2016-01-11 15:54:20 +00:00
|
|
|
<< txt;
|
2015-09-08 19:29:22 +00:00
|
|
|
DestroyBinary();
|
2015-09-08 18:17:14 +00:00
|
|
|
return diagnostic->error;
|
|
|
|
}
|
|
|
|
|
2015-11-12 18:53:27 +00:00
|
|
|
// Encodes SPIR-V text into binary and then decodes the binary using
|
|
|
|
// given options. Returns the decoded text.
|
2016-05-10 03:15:46 +00:00
|
|
|
std::string EncodeAndDecodeSuccessfully(
|
|
|
|
const std::string& txt,
|
|
|
|
uint32_t disassemble_options = SPV_BINARY_TO_TEXT_OPTION_NONE,
|
|
|
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
2015-09-09 14:36:48 +00:00
|
|
|
DestroyBinary();
|
2019-01-16 21:53:10 +00:00
|
|
|
DestroyDiagnostic();
|
2016-05-10 03:15:46 +00:00
|
|
|
ScopedContext context(env);
|
2016-06-27 19:06:41 +00:00
|
|
|
disassemble_options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
|
2016-04-19 02:25:35 +00:00
|
|
|
spv_result_t error = spvTextToBinary(context.context, txt.c_str(),
|
|
|
|
txt.size(), &binary, &diagnostic);
|
2015-09-09 14:36:48 +00:00
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
|
|
|
EXPECT_EQ(SPV_SUCCESS, error);
|
2015-10-06 20:22:00 +00:00
|
|
|
if (!binary) return "";
|
2015-09-09 14:36:48 +00:00
|
|
|
|
|
|
|
spv_text decoded_text;
|
2016-04-19 02:25:35 +00:00
|
|
|
error = spvBinaryToText(context.context, binary->code, binary->wordCount,
|
2015-11-12 18:53:27 +00:00
|
|
|
disassemble_options, &decoded_text, &diagnostic);
|
2015-09-09 14:36:48 +00:00
|
|
|
if (error) {
|
|
|
|
spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
}
|
2016-01-11 15:54:20 +00:00
|
|
|
EXPECT_EQ(SPV_SUCCESS, error) << txt;
|
2015-09-09 14:36:48 +00:00
|
|
|
|
|
|
|
const std::string decoded_string = decoded_text->str;
|
|
|
|
spvTextDestroy(decoded_text);
|
|
|
|
|
2016-06-27 19:06:41 +00:00
|
|
|
return decoded_string;
|
2015-09-09 14:36:48 +00:00
|
|
|
}
|
|
|
|
|
2015-10-16 19:11:00 +00:00
|
|
|
// Encodes SPIR-V text into binary. This is expected to succeed.
|
|
|
|
// The given words are then appended to the binary, and the result
|
|
|
|
// is then decoded. This is expected to fail.
|
|
|
|
// Returns the error message.
|
|
|
|
std::string EncodeSuccessfullyDecodeFailed(
|
2016-01-11 15:54:20 +00:00
|
|
|
const std::string& txt, const SpirvVector& words_to_append) {
|
2019-01-16 21:53:10 +00:00
|
|
|
DestroyBinary();
|
|
|
|
DestroyDiagnostic();
|
2015-11-05 22:45:09 +00:00
|
|
|
SpirvVector code =
|
2016-01-11 15:54:20 +00:00
|
|
|
spvtest::Concatenate({CompileSuccessfully(txt), words_to_append});
|
2015-10-16 19:11:00 +00:00
|
|
|
|
|
|
|
spv_text decoded_text;
|
2016-04-19 02:25:35 +00:00
|
|
|
EXPECT_NE(SPV_SUCCESS,
|
|
|
|
spvBinaryToText(ScopedContext().context, code.data(), code.size(),
|
|
|
|
SPV_BINARY_TO_TEXT_OPTION_NONE, &decoded_text,
|
|
|
|
&diagnostic));
|
2015-10-16 19:11:00 +00:00
|
|
|
if (diagnostic) {
|
|
|
|
std::string error_message = diagnostic->error;
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
diagnostic = nullptr;
|
|
|
|
return error_message;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2015-09-08 21:11:40 +00:00
|
|
|
// Compiles SPIR-V text, asserts success, and returns the words representing
|
|
|
|
// the instructions. In particular, skip the words in the SPIR-V header.
|
2016-04-19 02:25:35 +00:00
|
|
|
SpirvVector CompiledInstructions(const std::string& txt,
|
|
|
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
|
|
|
|
const SpirvVector code = CompileSuccessfully(txt, env);
|
2015-09-08 21:11:40 +00:00
|
|
|
SpirvVector result;
|
|
|
|
// Extract just the instructions.
|
|
|
|
// If the code fails to compile, then return the empty vector.
|
|
|
|
// In any case, don't crash or invoke undefined behaviour.
|
|
|
|
if (code.size() >= kFirstInstruction)
|
|
|
|
result = Subvector(code, kFirstInstruction);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-08-21 15:50:09 +00:00
|
|
|
void SetText(const std::string& code) {
|
|
|
|
textString = code;
|
|
|
|
text.str = textString.c_str();
|
|
|
|
text.length = textString.size();
|
|
|
|
}
|
|
|
|
|
2015-09-08 19:29:22 +00:00
|
|
|
// Destroys the binary, if it exists.
|
|
|
|
void DestroyBinary() {
|
|
|
|
spvBinaryDestroy(binary);
|
|
|
|
binary = nullptr;
|
|
|
|
}
|
|
|
|
|
2019-01-16 21:53:10 +00:00
|
|
|
// Destroys the diagnostic, if it exists.
|
|
|
|
void DestroyDiagnostic() {
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
diagnostic = nullptr;
|
|
|
|
}
|
|
|
|
|
2015-08-21 15:50:09 +00:00
|
|
|
spv_diagnostic diagnostic;
|
|
|
|
|
|
|
|
std::string textString;
|
|
|
|
spv_text_t text;
|
|
|
|
spv_binary binary;
|
|
|
|
};
|
2015-08-21 17:39:34 +00:00
|
|
|
|
2015-08-31 19:24:32 +00:00
|
|
|
using TextToBinaryTest = TextToBinaryTestBase<::testing::Test>;
|
2015-09-21 15:36:44 +00:00
|
|
|
} // namespace spvtest
|
2015-08-21 17:39:34 +00:00
|
|
|
|
2016-01-08 14:54:42 +00:00
|
|
|
using RoundTripTest =
|
|
|
|
spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // TEST_TEST_FIXTURE_H_
|