#ifndef TEST_TEST_FIXTURE_H_
#define TEST_TEST_FIXTURE_H_
#include <algorithm>
#include <string>
#include <vector>
#include "test/unit_spirv.h"
namespace spvtest {
struct ScopedContext {
ScopedContext(spv_target_env env = SPV_ENV_UNIVERSAL_1_0)
: context(spvContextCreate(env)) {}
~ScopedContext() { spvContextDestroy(context); }
spv_context context;
};
template <typename T>
class TextToBinaryTestBase : public T {
public:
using SpirvVector = std::vector<uint32_t>;
static const SpirvVector::size_type kFirstInstruction = 5;
TextToBinaryTestBase() : diagnostic(nullptr), text(), binary(nullptr) {
char textStr[] = "substitute the text member variable with your test";
text = {textStr, strlen(textStr)};
}
~TextToBinaryTestBase() override {
DestroyBinary();
if (diagnostic) spvDiagnosticDestroy(diagnostic);
}
SpirvVector Subvector(const SpirvVector& v, SpirvVector::size_type from) {
assert(from <= v.size());
return SpirvVector(v.begin() + from, v.end());
}
SpirvVector CompileSuccessfully(const std::string& txt,
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
DestroyBinary();
DestroyDiagnostic();
spv_result_t status =
spvTextToBinary(ScopedContext(env).context, txt.c_str(), txt.size(),
&binary, &diagnostic);
EXPECT_EQ(SPV_SUCCESS, status) << txt;
SpirvVector code_copy;
if (status == SPV_SUCCESS) {
code_copy = SpirvVector(binary->code, binary->code + binary->wordCount);
DestroyBinary();
} else {
spvDiagnosticPrint(diagnostic);
}
return code_copy;
}
std::string CompileFailure(const std::string& txt,
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
DestroyBinary();
DestroyDiagnostic();
EXPECT_NE(SPV_SUCCESS,
spvTextToBinary(ScopedContext(env).context, txt.c_str(),
txt.size(), &binary, &diagnostic))
<< txt;
DestroyBinary();
return diagnostic->error;
}
template <class It>
void MaybeFlipWords(bool flip_words, It begin, It end) {
SCOPED_TRACE(flip_words ? "Flipped Endianness" : "Normal Endianness");
if (flip_words) {
std::transform(begin, end, begin, [](const uint32_t raw_word) {
return spvFixWord(raw_word, I32_ENDIAN_HOST == I32_ENDIAN_BIG
? SPV_ENDIANNESS_LITTLE
: SPV_ENDIANNESS_BIG);
});
}
}
std::string EncodeAndDecodeSuccessfully(
const std::string& txt,
uint32_t disassemble_options = SPV_BINARY_TO_TEXT_OPTION_NONE,
uint32_t assemble_options = SPV_TEXT_TO_BINARY_OPTION_NONE,
spv_target_env env = SPV_ENV_UNIVERSAL_1_0, bool flip_words = false) {
DestroyBinary();
DestroyDiagnostic();
ScopedContext context(env);
disassemble_options |= SPV_BINARY_TO_TEXT_OPTION_NO_HEADER;
spv_result_t error =
spvTextToBinaryWithOptions(context.context, txt.c_str(), txt.size(),
assemble_options, &binary, &diagnostic);
if (error) {
spvDiagnosticPrint(diagnostic);
spvDiagnosticDestroy(diagnostic);
}
EXPECT_EQ(SPV_SUCCESS, error);
if (!binary) return "";
MaybeFlipWords(flip_words, binary->code, binary->code + binary->wordCount);
spv_text decoded_text;
error = spvBinaryToText(context.context, binary->code, binary->wordCount,
disassemble_options, &decoded_text, &diagnostic);
if (error) {
spvDiagnosticPrint(diagnostic);
spvDiagnosticDestroy(diagnostic);
}
EXPECT_EQ(SPV_SUCCESS, error) << txt;
const std::string decoded_string = decoded_text->str;
spvTextDestroy(decoded_text);
return decoded_string;
}
std::string EncodeSuccessfullyDecodeFailed(
const std::string& txt, const SpirvVector& words_to_append) {
DestroyBinary();
DestroyDiagnostic();
SpirvVector code =
spvtest::Concatenate({CompileSuccessfully(txt), words_to_append});
spv_text decoded_text;
EXPECT_NE(SPV_SUCCESS,
spvBinaryToText(ScopedContext().context, code.data(), code.size(),
SPV_BINARY_TO_TEXT_OPTION_NONE, &decoded_text,
&diagnostic));
if (diagnostic) {
std::string error_message = diagnostic->error;
spvDiagnosticDestroy(diagnostic);
diagnostic = nullptr;
return error_message;
}
return "";
}
SpirvVector CompiledInstructions(const std::string& txt,
spv_target_env env = SPV_ENV_UNIVERSAL_1_0) {
const SpirvVector code = CompileSuccessfully(txt, env);
SpirvVector result;
if (code.size() >= kFirstInstruction)
result = Subvector(code, kFirstInstruction);
return result;
}
void SetText(const std::string& code) {
textString = code;
text.str = textString.c_str();
text.length = textString.size();
}
void DestroyBinary() {
spvBinaryDestroy(binary);
binary = nullptr;
}
void DestroyDiagnostic() {
spvDiagnosticDestroy(diagnostic);
diagnostic = nullptr;
}
spv_diagnostic diagnostic;
std::string textString;
spv_text_t text;
spv_binary binary;
};
using TextToBinaryTest = TextToBinaryTestBase<::testing::Test>;
}
using RoundTripTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<std::string>>;
#endif