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 MemoryModel = spvtest::LinkerTest;
25
TEST_F(MemoryModel,Default)26 TEST_F(MemoryModel, Default) {
27 const std::string body1 = R"(
28 OpMemoryModel Logical Simple
29 )";
30 const std::string body2 = R"(
31 OpMemoryModel Logical Simple
32 )";
33
34 spvtest::Binary linked_binary;
35 ASSERT_EQ(SPV_SUCCESS, AssembleAndLink({body1, body2}, &linked_binary));
36 EXPECT_THAT(GetErrorMessage(), std::string());
37
38 EXPECT_EQ(SpvAddressingModelLogical, linked_binary[6]);
39 EXPECT_EQ(SpvMemoryModelSimple, linked_binary[7]);
40 }
41
TEST_F(MemoryModel,AddressingMismatch)42 TEST_F(MemoryModel, AddressingMismatch) {
43 const std::string body1 = R"(
44 OpMemoryModel Logical Simple
45 )";
46 const std::string body2 = R"(
47 OpMemoryModel Physical32 Simple
48 )";
49
50 spvtest::Binary linked_binary;
51 EXPECT_EQ(SPV_ERROR_INTERNAL,
52 AssembleAndLink({body1, body2}, &linked_binary));
53 EXPECT_THAT(GetErrorMessage(),
54 HasSubstr("Conflicting addressing models: Logical (input modules "
55 "1 through 1) vs Physical32 (input module 2)."));
56 }
57
TEST_F(MemoryModel,MemoryMismatch)58 TEST_F(MemoryModel, MemoryMismatch) {
59 const std::string body1 = R"(
60 OpMemoryModel Logical Simple
61 )";
62 const std::string body2 = R"(
63 OpMemoryModel Logical GLSL450
64 )";
65
66 spvtest::Binary linked_binary;
67 EXPECT_EQ(SPV_ERROR_INTERNAL,
68 AssembleAndLink({body1, body2}, &linked_binary));
69 EXPECT_THAT(GetErrorMessage(),
70 HasSubstr("Conflicting memory models: Simple (input modules 1 "
71 "through 1) vs GLSL450 (input module 2)."));
72 }
73
TEST_F(MemoryModel,FirstLackMemoryModel)74 TEST_F(MemoryModel, FirstLackMemoryModel) {
75 const std::string body1 = R"(
76 )";
77 const std::string body2 = R"(
78 OpMemoryModel Logical GLSL450
79 )";
80
81 spvtest::Binary linked_binary;
82 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
83 AssembleAndLink({body1, body2}, &linked_binary));
84 EXPECT_THAT(
85 GetErrorMessage(),
86 HasSubstr("Input module 1 is lacking an OpMemoryModel instruction."));
87 }
88
TEST_F(MemoryModel,SecondLackMemoryModel)89 TEST_F(MemoryModel, SecondLackMemoryModel) {
90 const std::string body1 = R"(
91 OpMemoryModel Logical GLSL450
92 )";
93 const std::string body2 = R"(
94 )";
95
96 spvtest::Binary linked_binary;
97 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
98 AssembleAndLink({body1, body2}, &linked_binary));
99 EXPECT_THAT(
100 GetErrorMessage(),
101 HasSubstr("Input module 2 is lacking an OpMemoryModel instruction."));
102 }
103
104 } // namespace
105 } // namespace spvtools
106