#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "source/util/string_utils.h"
#include "test/test_fixture.h"
#include "test/unit_spirv.h"
namespace spvtools {
namespace {
using spvtest::MakeInstruction;
using utils::MakeVector;
using spvtest::TextToBinaryTest;
using ::testing::Eq;
struct LanguageCase {
uint32_t get_language_value() const {
return static_cast<uint32_t>(language_value);
}
const char* language_name;
spv::SourceLanguage language_value;
uint32_t version;
};
const LanguageCase kLanguageCases[] = {
#define CASE(NAME, VERSION) \
{ #NAME, spv::SourceLanguage::NAME, VERSION }
CASE(Unknown, 0),
CASE(Unknown, 999),
CASE(ESSL, 310),
CASE(GLSL, 450),
CASE(OpenCL_C, 120),
CASE(OpenCL_C, 200),
CASE(OpenCL_C, 210),
CASE(OpenCL_CPP, 210),
CASE(HLSL, 5),
CASE(HLSL, 6),
#undef CASE
};
using OpSourceTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<LanguageCase>>;
TEST_P(OpSourceTest, AnyLanguage) {
const std::string input = std::string("OpSource ") +
GetParam().language_name + " " +
std::to_string(GetParam().version);
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSource, {GetParam().get_language_value(),
GetParam().version})));
}
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceTest,
::testing::ValuesIn(kLanguageCases));
TEST_F(OpSourceTest, WrongLanguage) {
EXPECT_THAT(CompileFailure("OpSource xxyyzz 12345"),
Eq("Invalid source language 'xxyyzz'."));
}
TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalFileId) {
const std::string input = "OpSource GLSL 450 %file_id";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSource,
{uint32_t(spv::SourceLanguage::GLSL), 450, 1})));
}
TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalSourceText) {
std::string fake_source = "To be or not to be";
const std::string input =
"OpSource GLSL 450 %file_id \"" + fake_source + "\"";
EXPECT_THAT(CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSource,
{uint32_t(spv::SourceLanguage::GLSL), 450, 1},
MakeVector(fake_source))));
}
using OpSourceContinuedTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpSourceContinuedTest, AnyExtension) {
const std::string input =
std::string("OpSourceContinued \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSourceContinued, MakeVector(GetParam()))));
}
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceContinuedTest,
::testing::ValuesIn(std::vector<const char*>{
"", "foo bar this and that"}));
using OpSourceExtensionTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpSourceExtensionTest, AnyExtension) {
const std::string input =
std::string("OpSourceExtension \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpSourceExtension, MakeVector(GetParam()))));
}
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceExtensionTest,
::testing::ValuesIn(std::vector<const char*>{
"", "foo bar this and that"}));
TEST_F(TextToBinaryTest, OpLine) {
EXPECT_THAT(CompiledInstructions("OpLine %srcfile 42 99"),
Eq(MakeInstruction(spv::Op::OpLine, {1, 42, 99})));
}
TEST_F(TextToBinaryTest, OpNoLine) {
EXPECT_THAT(CompiledInstructions("OpNoLine"),
Eq(MakeInstruction(spv::Op::OpNoLine, {})));
}
using OpStringTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpStringTest, AnyString) {
const std::string input =
std::string("%result = OpString \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpString, {1}, MakeVector(GetParam()))));
}
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpStringTest,
::testing::ValuesIn(std::vector<const char*>{
"", "foo bar this and that"}));
using OpNameTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpNameTest, AnyString) {
const std::string input =
std::string("OpName %target \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpName, {1}, MakeVector(GetParam()))));
}
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpNameTest,
::testing::Values("", "foo bar this and that"));
using OpMemberNameTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpMemberNameTest, AnyString) {
const std::string input =
std::string("OpMemberName %type 42 \"") + GetParam() + "\"";
EXPECT_THAT(CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpMemberName, {1, 42},
MakeVector(GetParam()))));
}
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpMemberNameTest,
::testing::ValuesIn(std::vector<const char*>{
"", "foo bar this and that"}));
using OpModuleProcessedTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
TEST_P(OpModuleProcessedTest, AnyString) {
const std::string input =
std::string("OpModuleProcessed \"") + GetParam() + "\"";
EXPECT_THAT(
CompiledInstructions(input, SPV_ENV_UNIVERSAL_1_1),
Eq(MakeInstruction(spv::Op::OpModuleProcessed, MakeVector(GetParam()))));
}
INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpModuleProcessedTest,
::testing::Values("", "foo bar this and that"));
} }