#include <string>
#include <vector>
#include "gmock/gmock.h"
#include "test/test_fixture.h"
#include "test/unit_spirv.h"
namespace spvtools {
namespace {
using spvtest::MakeInstruction;
using spvtest::TextToBinaryTest;
using ::testing::Eq;
struct ImageOperandsCase {
std::string image_operands;
std::vector<uint32_t> expected_mask_and_operands;
};
using ImageOperandsTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<ImageOperandsCase>>;
TEST_P(ImageOperandsTest, Sample) {
const std::string input =
"%2 = OpImageFetch %1 %3 %4" + GetParam().image_operands + "\n";
EXPECT_THAT(CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpImageFetch, {1, 2, 3, 4},
GetParam().expected_mask_and_operands)));
}
#define MASK(NAME) uint32_t(spv::ImageOperandsMask::NAME)
INSTANTIATE_TEST_SUITE_P(
TextToBinaryImageOperandsAny, ImageOperandsTest,
::testing::ValuesIn(std::vector<ImageOperandsCase>{
{"", {}},
{" Bias %5", {MASK(Bias), 5}},
{" Lod %5", {MASK(Lod), 5}},
{" Grad %5 %6", {MASK(Grad), 5, 6}},
{" ConstOffset %5", {MASK(ConstOffset), 5}},
{" Offset %5", {MASK(Offset), 5}},
{" ConstOffsets %5", {MASK(ConstOffsets), 5}},
{" Sample %5", {MASK(Sample), 5}},
{" MinLod %5", {MASK(MinLod), 5}},
}));
#undef MASK
#define MASK(NAME) static_cast<uint32_t>(spv::ImageOperandsMask::NAME)
INSTANTIATE_TEST_SUITE_P(
TextToBinaryImageOperandsCombination, ImageOperandsTest,
::testing::ValuesIn(std::vector<ImageOperandsCase>{
{" Bias|Lod %5 %6", {MASK(Bias) | MASK(Lod), 5, 6}},
{" Lod|Grad %5 %6 %7", {MASK(Lod) | MASK(Grad), 5, 6, 7}},
{" Grad|ConstOffset %5 %6 %7",
{MASK(Grad) | MASK(ConstOffset), 5, 6, 7}},
{" ConstOffset|Offset %5 %6", {MASK(ConstOffset) | MASK(Offset), 5, 6}},
{" Offset|ConstOffsets %5 %6",
{MASK(Offset) | MASK(ConstOffsets), 5, 6}},
{" ConstOffsets|Sample %5 %6",
{MASK(ConstOffsets) | MASK(Sample), 5, 6}},
{" Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample"
" %5 %6 %7 %8 %9 %10 %11 %12",
{MASK(Bias) | MASK(Lod) | MASK(Grad) | MASK(ConstOffset) |
MASK(Offset) | MASK(ConstOffsets) | MASK(Sample),
5, 6, 7, 8, 9, 10, 11, 12}},
{" Sample|ConstOffsets|Offset|ConstOffset|Grad|Lod|Bias"
" %5 %6 %7 %8 %9 %10 %11 %12",
{MASK(Bias) | MASK(Lod) | MASK(Grad) | MASK(ConstOffset) |
MASK(Offset) | MASK(ConstOffsets) | MASK(Sample),
5, 6, 7, 8, 9, 10, 11, 12}}}));
#undef MASK
TEST_F(ImageOperandsTest, WrongOperand) {
EXPECT_THAT(CompileFailure("%r = OpImageFetch %t %i %c xxyyzz"),
Eq("Invalid image operand 'xxyyzz'."));
}
using OpImageTest = TextToBinaryTest;
TEST_F(OpImageTest, Valid) {
const std::string input = "%2 = OpImage %1 %3\n";
EXPECT_THAT(CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpImage, {1, 2, 3})));
EXPECT_THAT(EncodeAndDecodeSuccessfully(input), input);
}
TEST_F(OpImageTest, InvalidTypeOperand) {
EXPECT_THAT(CompileFailure("%2 = OpImage 42"),
Eq("Expected id to start with %."));
}
TEST_F(OpImageTest, MissingSampledImageOperand) {
EXPECT_THAT(CompileFailure("%2 = OpImage %1"),
Eq("Expected operand for OpImage instruction, but found the end "
"of the stream."));
}
TEST_F(OpImageTest, InvalidSampledImageOperand) {
EXPECT_THAT(CompileFailure("%2 = OpImage %1 1000"),
Eq("Expected id to start with %."));
}
TEST_F(OpImageTest, TooManyOperands) {
EXPECT_THAT(CompileFailure("%2 = OpImage %1 %3 %4"), Eq("Expected '=', found end of stream."));
EXPECT_THAT(CompileFailure("%2 = OpImage %1 %3 99"), Eq("Expected <opcode> or <result-id> at the beginning of an "
"instruction, found '99'."));
EXPECT_THAT(CompileFailure("%2 = OpImage %1 %3 \"abc\""), Eq("Expected <opcode> or <result-id> at the beginning of an "
"instruction, found '\"abc\"'."));
}
using OpImageSparseReadTest = TextToBinaryTest;
TEST_F(OpImageSparseReadTest, OnlyRequiredOperands) {
const std::string input = "%2 = OpImageSparseRead %1 %3 %4\n";
EXPECT_THAT(CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpImageSparseRead, {1, 2, 3, 4})));
EXPECT_THAT(EncodeAndDecodeSuccessfully(input), input);
}
using ImageSparseReadImageOperandsTest =
spvtest::TextToBinaryTestBase<::testing::TestWithParam<ImageOperandsCase>>;
TEST_P(ImageSparseReadImageOperandsTest, Sample) {
const std::string input =
"%2 = OpImageSparseRead %1 %3 %4" + GetParam().image_operands + "\n";
EXPECT_THAT(CompiledInstructions(input),
Eq(MakeInstruction(spv::Op::OpImageSparseRead, {1, 2, 3, 4},
GetParam().expected_mask_and_operands)));
EXPECT_THAT(EncodeAndDecodeSuccessfully(input), input);
}
#define MASK(NAME) uint32_t(spv::ImageOperandsMask::NAME)
INSTANTIATE_TEST_SUITE_P(ImageSparseReadImageOperandsAny,
ImageSparseReadImageOperandsTest,
::testing::ValuesIn(std::vector<ImageOperandsCase>{
{"", {}},
{" Bias %5", {MASK(Bias), 5}},
{" Lod %5", {MASK(Lod), 5}},
{" Grad %5 %6", {MASK(Grad), 5, 6}},
{" ConstOffset %5", {MASK(ConstOffset), 5}},
{" Offset %5", {MASK(Offset), 5}},
{" ConstOffsets %5", {MASK(ConstOffsets), 5}},
{" Sample %5", {MASK(Sample), 5}},
{" MinLod %5", {MASK(MinLod), 5}},
}));
#undef MASK
#define MASK(NAME) static_cast<uint32_t>(spv::ImageOperandsMask::NAME)
INSTANTIATE_TEST_SUITE_P(
ImageSparseReadImageOperandsCombination, ImageSparseReadImageOperandsTest,
::testing::ValuesIn(std::vector<ImageOperandsCase>{
{" Bias|Lod %5 %6", {MASK(Bias) | MASK(Lod), 5, 6}},
{" Lod|Grad %5 %6 %7", {MASK(Lod) | MASK(Grad), 5, 6, 7}},
{" Grad|ConstOffset %5 %6 %7",
{MASK(Grad) | MASK(ConstOffset), 5, 6, 7}},
{" ConstOffset|Offset %5 %6", {MASK(ConstOffset) | MASK(Offset), 5, 6}},
{" Offset|ConstOffsets %5 %6",
{MASK(Offset) | MASK(ConstOffsets), 5, 6}},
{" ConstOffsets|Sample %5 %6",
{MASK(ConstOffsets) | MASK(Sample), 5, 6}},
{" Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample"
" %5 %6 %7 %8 %9 %10 %11 %12",
{MASK(Bias) | MASK(Lod) | MASK(Grad) | MASK(ConstOffset) |
MASK(Offset) | MASK(ConstOffsets) | MASK(Sample),
5, 6, 7, 8, 9, 10, 11, 12}},
}));
#undef MASK
TEST_F(OpImageSparseReadTest, InvalidTypeOperand) {
EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead 42"),
Eq("Expected id to start with %."));
}
TEST_F(OpImageSparseReadTest, MissingImageOperand) {
EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead %1"),
Eq("Expected operand for OpImageSparseRead instruction, but "
"found the end of the stream."));
}
TEST_F(OpImageSparseReadTest, InvalidImageOperand) {
EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead %1 1000"),
Eq("Expected id to start with %."));
}
TEST_F(OpImageSparseReadTest, MissingCoordinateOperand) {
EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead %1 %2"),
Eq("Expected operand for OpImageSparseRead instruction, but "
"found the end of the stream."));
}
TEST_F(OpImageSparseReadTest, InvalidCoordinateOperand) {
EXPECT_THAT(CompileFailure("%2 = OpImageSparseRead %1 %2 1000"),
Eq("Expected id to start with %."));
}
} }