1 // Copyright (c) 2015-2016 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/spirv_constant.h"
16 #include "test/unit_spirv.h"
17
18 namespace spvtools {
19 namespace {
20
21 class BinaryHeaderGet : public ::testing::Test {
22 public:
BinaryHeaderGet()23 BinaryHeaderGet() { memset(code, 0, sizeof(code)); }
24
SetUp()25 virtual void SetUp() {
26 code[0] = SpvMagicNumber;
27 code[1] = SpvVersion;
28 code[2] = SPV_GENERATOR_CODEPLAY;
29 code[3] = 1; // NOTE: Bound
30 code[4] = 0; // NOTE: Schema; reserved
31 code[5] = 0; // NOTE: Instructions
32
33 binary.code = code;
34 binary.wordCount = 6;
35 }
get_const_binary()36 spv_const_binary_t get_const_binary() {
37 return spv_const_binary_t{binary.code, binary.wordCount};
38 }
TearDown()39 virtual void TearDown() {}
40
41 uint32_t code[6];
42 spv_binary_t binary;
43 };
44
TEST_F(BinaryHeaderGet,Default)45 TEST_F(BinaryHeaderGet, Default) {
46 spv_endianness_t endian;
47 spv_const_binary_t const_bin = get_const_binary();
48 ASSERT_EQ(SPV_SUCCESS, spvBinaryEndianness(&const_bin, &endian));
49
50 spv_header_t header;
51 ASSERT_EQ(SPV_SUCCESS, spvBinaryHeaderGet(&const_bin, endian, &header));
52
53 ASSERT_EQ(static_cast<uint32_t>(SpvMagicNumber), header.magic);
54 ASSERT_EQ(0x00010300u, header.version);
55 ASSERT_EQ(static_cast<uint32_t>(SPV_GENERATOR_CODEPLAY), header.generator);
56 ASSERT_EQ(1u, header.bound);
57 ASSERT_EQ(0u, header.schema);
58 ASSERT_EQ(&code[5], header.instructions);
59 }
60
TEST_F(BinaryHeaderGet,InvalidCode)61 TEST_F(BinaryHeaderGet, InvalidCode) {
62 spv_const_binary_t my_binary = {nullptr, 0};
63 spv_header_t header;
64 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
65 spvBinaryHeaderGet(&my_binary, SPV_ENDIANNESS_LITTLE, &header));
66 }
67
TEST_F(BinaryHeaderGet,InvalidPointerHeader)68 TEST_F(BinaryHeaderGet, InvalidPointerHeader) {
69 spv_const_binary_t const_bin = get_const_binary();
70 ASSERT_EQ(SPV_ERROR_INVALID_POINTER,
71 spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
72 }
73
TEST_F(BinaryHeaderGet,TruncatedHeader)74 TEST_F(BinaryHeaderGet, TruncatedHeader) {
75 for (uint8_t i = 1; i < SPV_INDEX_INSTRUCTION; i++) {
76 binary.wordCount = i;
77 spv_const_binary_t const_bin = get_const_binary();
78 ASSERT_EQ(SPV_ERROR_INVALID_BINARY,
79 spvBinaryHeaderGet(&const_bin, SPV_ENDIANNESS_LITTLE, nullptr));
80 }
81 }
82
83 } // namespace
84 } // namespace spvtools
85