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