SPIRV-Tools/test/opt/test_instruction.cpp

106 lines
3.9 KiB
C++

// Copyright (c) 2016 Google Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and/or associated documentation files (the
// "Materials"), to deal in the Materials without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Materials, and to
// permit persons to whom the Materials are furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Materials.
//
// MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
// KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
// SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
// https://www.khronos.org/registry/
//
// THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
#include "opt/instruction.h"
#include "gmock/gmock.h"
#include "spirv-tools/libspirv.h"
#include "UnitSPIRV.h"
namespace {
using spvtest::MakeInstruction;
using spvtools::ir::Instruction;
using spvtools::ir::Operand;
using ::testing::Eq;
TEST(InstructionTest, CreateTrivial) {
Instruction empty;
EXPECT_EQ(SpvOpNop, empty.opcode());
EXPECT_EQ(0u, empty.type_id());
EXPECT_EQ(0u, empty.result_id());
EXPECT_EQ(0u, empty.NumOperands());
EXPECT_EQ(0u, empty.NumOperandWords());
EXPECT_EQ(0u, empty.NumInOperandWords());
EXPECT_EQ(empty.cend(), empty.cbegin());
}
TEST(InstructionTest, CreateWithOpcodeAndNoOperands) {
Instruction inst(SpvOpReturn);
EXPECT_EQ(SpvOpReturn, inst.opcode());
EXPECT_EQ(0u, inst.type_id());
EXPECT_EQ(0u, inst.result_id());
EXPECT_EQ(0u, inst.NumOperands());
EXPECT_EQ(0u, inst.NumOperandWords());
EXPECT_EQ(0u, inst.NumInOperandWords());
EXPECT_EQ(inst.cend(), inst.cbegin());
}
TEST(InstructionTest, CreateWithOpcodeAndOperands) {
auto i32 = MakeInstruction(SpvOpTypeInt, {44, 32, 1});
spv_parsed_operand_t parsed_operands[] = {
{1, 1, SPV_OPERAND_TYPE_RESULT_ID, SPV_NUMBER_NONE, 0},
{2, 1, SPV_OPERAND_TYPE_LITERAL_INTEGER, SPV_NUMBER_UNSIGNED_INT, 32},
{3, 1, SPV_OPERAND_TYPE_LITERAL_INTEGER, SPV_NUMBER_UNSIGNED_INT, 1},
};
spv_parsed_instruction_t parsed_inst{i32.data(),
uint16_t(i32.size()),
uint16_t(SpvOpTypeInt),
SPV_EXT_INST_TYPE_NONE,
0, // type id
i32[1], // result id
parsed_operands,
3};
Instruction inst(parsed_inst);
EXPECT_EQ(SpvOpTypeInt, inst.opcode());
EXPECT_EQ(0u, inst.type_id());
EXPECT_EQ(44u, inst.result_id());
EXPECT_EQ(3u, inst.NumOperands());
EXPECT_EQ(3u, inst.NumOperandWords());
EXPECT_EQ(2u, inst.NumInOperandWords());
auto cbegin = inst.cbegin();
auto cend = inst.cend();
EXPECT_NE(cend, inst.cbegin());
// Spot check iteration across operands.
auto iter = inst.cbegin();
for (int i = 0; i < 3; ++i, ++iter) {
const auto& operand = *iter;
EXPECT_THAT(operand.type, Eq(parsed_operands[i].type));
EXPECT_THAT(operand.words, Eq(std::vector<uint32_t>{i32[i + 1]}));
EXPECT_NE(cend, iter);
}
EXPECT_EQ(cend, iter);
// Check that cbegin and cend have not changed.
EXPECT_EQ(cbegin, inst.cbegin());
EXPECT_EQ(cend, inst.cend());
}
} // anonymous namespace