1 // Copyright (c) 2017 Pierre Moreau
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 <string>
16 
17 #include "gmock/gmock.h"
18 #include "test/link/linker_fixture.h"
19 
20 namespace spvtools {
21 namespace {
22 
23 using ::testing::HasSubstr;
24 using IdsLimit = spvtest::LinkerTest;
25 
TEST_F(IdsLimit,UnderLimit)26 TEST_F(IdsLimit, UnderLimit) {
27   spvtest::Binaries binaries = {
28       {
29           SpvMagicNumber, SpvVersion, SPV_GENERATOR_CODEPLAY,
30           0x2FFFFFu,  // NOTE: Bound
31           0u,         // NOTE: Schema; reserved
32       },
33       {
34           SpvMagicNumber, SpvVersion, SPV_GENERATOR_CODEPLAY,
35           0x100000u,  // NOTE: Bound
36           0u,         // NOTE: Schema; reserved
37       }};
38   spvtest::Binary linked_binary;
39 
40   ASSERT_EQ(SPV_SUCCESS, Link(binaries, &linked_binary));
41   EXPECT_THAT(GetErrorMessage(), std::string());
42   EXPECT_EQ(0x3FFFFEu, linked_binary[3]);
43 }
44 
TEST_F(IdsLimit,OverLimit)45 TEST_F(IdsLimit, OverLimit) {
46   spvtest::Binaries binaries = {
47       {
48           SpvMagicNumber, SpvVersion, SPV_GENERATOR_CODEPLAY,
49           0x2FFFFFu,  // NOTE: Bound
50           0u,         // NOTE: Schema; reserved
51       },
52       {
53           SpvMagicNumber, SpvVersion, SPV_GENERATOR_CODEPLAY,
54           0x100000u,  // NOTE: Bound
55           0u,         // NOTE: Schema; reserved
56       },
57       {
58           SpvMagicNumber, SpvVersion, SPV_GENERATOR_CODEPLAY,
59           3u,  // NOTE: Bound
60           0u,  // NOTE: Schema; reserved
61       }};
62 
63   spvtest::Binary linked_binary;
64 
65   EXPECT_EQ(SPV_ERROR_INVALID_ID, Link(binaries, &linked_binary));
66   EXPECT_THAT(GetErrorMessage(),
67               HasSubstr("The limit of IDs, 4194303, was exceeded: 4194304 is "
68                         "the current ID bound."));
69 }
70 
71 }  // namespace
72 }  // namespace spvtools
73