2016-01-07 21:44:22 +03:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-05-22 20:26:19 +03:00
|
|
|
//
|
2016-09-01 22:33:59 +03: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-05-22 20:26:19 +03:00
|
|
|
//
|
2016-09-01 22:33:59 +03:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-05-22 20:26:19 +03:00
|
|
|
//
|
2016-09-01 22:33:59 +03: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-05-22 20:26:19 +03:00
|
|
|
|
2015-12-22 23:15:46 +03:00
|
|
|
#include "source/spirv_constant.h"
|
2018-08-03 22:06:09 +03:00
|
|
|
#include "test/unit_spirv.h"
|
2015-05-22 20:26:19 +03:00
|
|
|
|
2018-07-11 16:24:49 +03:00
|
|
|
namespace spvtools {
|
2015-08-31 22:24:32 +03:00
|
|
|
namespace {
|
|
|
|
|
2015-05-22 20:26:19 +03:00
|
|
|
class BinaryHeaderGet : public ::testing::Test {
|
|
|
|
public:
|
|
|
|
BinaryHeaderGet() { memset(code, 0, sizeof(code)); }
|
|
|
|
|
|
|
|
virtual void SetUp() {
|
2015-11-11 18:17:16 +03:00
|
|
|
code[0] = SpvMagicNumber;
|
|
|
|
code[1] = SpvVersion;
|
2015-05-22 20:26:19 +03:00
|
|
|
code[2] = SPV_GENERATOR_CODEPLAY;
|
|
|
|
code[3] = 1; // NOTE: Bound
|
|
|
|
code[4] = 0; // NOTE: Schema; reserved
|
|
|
|
code[5] = 0; // NOTE: Instructions
|
|
|
|
|
|
|
|
binary.code = code;
|
|
|
|
binary.wordCount = 6;
|
|
|
|
}
|
2015-11-11 19:05:07 +03:00
|
|
|
spv_const_binary_t get_const_binary() {
|
|
|
|
return spv_const_binary_t{binary.code, binary.wordCount};
|
|
|
|
}
|
2015-05-22 20:26:19 +03:00
|
|
|
virtual void TearDown() {}
|
|
|
|
|
|
|
|
uint32_t code[6];
|
|
|
|
spv_binary_t binary;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(BinaryHeaderGet, Default) {
|
|
|
|
spv_endianness_t endian;
|
2015-11-11 19:05:07 +03:00
|
|
|
spv_const_binary_t const_bin = get_const_binary();
|
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&const_bin, &endian));
|
2015-05-22 20:26:19 +03:00
|
|
|
|
|
|
|
spv_header_t header;
|
2015-11-11 19:05:07 +03:00
|
|
|
ASSERT_EQ(SPV_SUCCESS, spvBinaryHeaderGet(&const_bin, endian, &header));
|
2015-05-22 20:26:19 +03:00
|
|
|
|
2015-11-11 18:17:16 +03:00
|
|
|
ASSERT_EQ(static_cast<uint32_t>(SpvMagicNumber), header.magic);
|
2021-12-15 22:38:28 +03:00
|
|
|
// Expect SPIRV-Headers updated to SPIR-V 1.6.
|
|
|
|
ASSERT_EQ(0x00010600u, header.version);
|
2015-11-11 18:17:16 +03:00
|
|
|
ASSERT_EQ(static_cast<uint32_t>(SPV_GENERATOR_CODEPLAY), header.generator);
|
2015-05-22 20:26:19 +03:00
|
|
|
ASSERT_EQ(1u, header.bound);
|
|
|
|
ASSERT_EQ(0u, header.schema);
|
|
|
|
ASSERT_EQ(&code[5], header.instructions);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BinaryHeaderGet, InvalidCode) {
|
2016-01-11 18:54:20 +03:00
|
|
|
spv_const_binary_t my_binary = {nullptr, 0};
|
2015-05-22 20:26:19 +03:00
|
|
|
spv_header_t header;
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
|
2016-01-11 18:54:20 +03:00
|
|
|
spvBinaryHeaderGet(&my_binary, SPV_ENDIANNESS_LITTLE, &header));
|
2015-05-22 20:26:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BinaryHeaderGet, InvalidPointerHeader) {
|
2015-11-11 19:05:07 +03:00
|
|
|
spv_const_binary_t const_bin = get_const_binary();
|
2015-05-22 20:26:19 +03:00
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
|
2015-11-11 19:05:07 +03:00
|
|
|
spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
|
2015-05-22 20:26:19 +03:00
|
|
|
}
|
2015-08-31 22:24:32 +03:00
|
|
|
|
2015-10-30 23:06:15 +03:00
|
|
|
TEST_F(BinaryHeaderGet, TruncatedHeader) {
|
2015-11-18 17:22:10 +03:00
|
|
|
for (uint8_t i = 1; i < SPV_INDEX_INSTRUCTION; i++) {
|
2015-10-30 23:06:15 +03:00
|
|
|
binary.wordCount = i;
|
2015-11-11 19:05:07 +03:00
|
|
|
spv_const_binary_t const_bin = get_const_binary();
|
2015-10-30 23:06:15 +03:00
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
|
2015-11-11 19:05:07 +03:00
|
|
|
spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
|
2015-10-30 23:06:15 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-23 18:59:41 +03:00
|
|
|
TEST_F(BinaryHeaderGet, VersionNonZeroHighByte) {
|
|
|
|
spv_header_t header;
|
|
|
|
code[1] = 0xFF010300;
|
|
|
|
spv_const_binary_t const_bin = get_const_binary();
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
|
|
|
|
spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BinaryHeaderGet, VersionNonZeroLowByte) {
|
|
|
|
spv_header_t header;
|
|
|
|
code[1] = 0x000103F0;
|
|
|
|
spv_const_binary_t const_bin = get_const_binary();
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
|
|
|
|
spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BinaryHeaderGet, VersionTooLow) {
|
|
|
|
spv_header_t header;
|
|
|
|
code[1] = 0x00000300;
|
|
|
|
spv_const_binary_t const_bin = get_const_binary();
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
|
|
|
|
spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BinaryHeaderGet, VersionTooHigh) {
|
|
|
|
spv_header_t header;
|
|
|
|
code[1] = 0x000F0300;
|
|
|
|
spv_const_binary_t const_bin = get_const_binary();
|
|
|
|
ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
|
|
|
|
spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, &header));
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:24:49 +03:00
|
|
|
} // namespace
|
|
|
|
} // namespace spvtools
|