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 // Assembler tests for non-semantic extended instructions
16 
17 #include <string>
18 #include <vector>
19 
20 #include "gmock/gmock.h"
21 #include "test/test_fixture.h"
22 #include "test/unit_spirv.h"
23 
24 using ::testing::Eq;
25 
26 namespace spvtools {
27 namespace {
28 
29 using NonSemanticRoundTripTest = RoundTripTest;
30 using NonSemanticTextToBinaryTest = spvtest::TextToBinaryTest;
31 
TEST_F(NonSemanticRoundTripTest,NonSemanticInsts)32 TEST_F(NonSemanticRoundTripTest, NonSemanticInsts) {
33   std::string spirv = R"(OpExtension "SPV_KHR_non_semantic_info"
34 %1 = OpExtInstImport "NonSemantic.Testing.ExtInst"
35 %2 = OpTypeVoid
36 %3 = OpExtInst %2 %1 132384681 %2
37 %4 = OpTypeInt 32 0
38 %5 = OpConstant %4 123
39 %6 = OpString "Test string"
40 %7 = OpExtInst %4 %1 82198732 %5 %6
41 %8 = OpExtInstImport "NonSemantic.Testing.AnotherUnknownExtInstSet"
42 %9 = OpExtInst %4 %8 613874321 %7 %5 %6
43 )";
44   std::string disassembly = EncodeAndDecodeSuccessfully(
45       spirv, SPV_BINARY_TO_TEXT_OPTION_NONE, SPV_ENV_UNIVERSAL_1_0);
46   EXPECT_THAT(disassembly, Eq(spirv));
47 }
48 
TEST_F(NonSemanticTextToBinaryTest,InvalidExtInstSetName)49 TEST_F(NonSemanticTextToBinaryTest, InvalidExtInstSetName) {
50   std::string spirv = R"(OpExtension "SPV_KHR_non_semantic_info"
51 %1 = OpExtInstImport "NonSemantic_Testing_ExtInst"
52 )";
53 
54   EXPECT_THAT(
55       CompileFailure(spirv),
56       Eq("Invalid extended instruction import 'NonSemantic_Testing_ExtInst'"));
57 }
58 
TEST_F(NonSemanticTextToBinaryTest,NonSemanticIntParameter)59 TEST_F(NonSemanticTextToBinaryTest, NonSemanticIntParameter) {
60   std::string spirv = R"(OpExtension "SPV_KHR_non_semantic_info"
61 %1 = OpExtInstImport "NonSemantic.Testing.ExtInst"
62 %2 = OpTypeVoid
63 %3 = OpExtInst %2 %1 1 99999
64 )";
65 
66   EXPECT_THAT(CompileFailure(spirv), Eq("Expected id to start with %."));
67 }
68 
TEST_F(NonSemanticTextToBinaryTest,NonSemanticFloatParameter)69 TEST_F(NonSemanticTextToBinaryTest, NonSemanticFloatParameter) {
70   std::string spirv = R"(OpExtension "SPV_KHR_non_semantic_info"
71 %1 = OpExtInstImport "NonSemantic.Testing.ExtInst"
72 %2 = OpTypeVoid
73 %3 = OpExtInst %2 %1 1 3.141592
74 )";
75 
76   EXPECT_THAT(CompileFailure(spirv), Eq("Expected id to start with %."));
77 }
78 
TEST_F(NonSemanticTextToBinaryTest,NonSemanticStringParameter)79 TEST_F(NonSemanticTextToBinaryTest, NonSemanticStringParameter) {
80   std::string spirv = R"(OpExtension "SPV_KHR_non_semantic_info"
81 %1 = OpExtInstImport "NonSemantic.Testing.ExtInst"
82 %2 = OpTypeVoid
83 %3 = OpExtInst %2 %1 1 "foobar"
84 )";
85 
86   EXPECT_THAT(CompileFailure(spirv), Eq("Expected id to start with %."));
87 }
88 
89 }  // namespace
90 }  // namespace spvtools
91