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 instructions in the "Debug" section of the
16 // SPIR-V spec.
17
18 #include <string>
19 #include <vector>
20
21 #include "gmock/gmock.h"
22 #include "test/test_fixture.h"
23 #include "test/unit_spirv.h"
24
25 namespace spvtools {
26 namespace {
27
28 using spvtest::MakeInstruction;
29 using spvtest::MakeVector;
30 using spvtest::TextToBinaryTest;
31 using ::testing::Eq;
32
33 // Test OpSource
34
35 // A single test case for OpSource
36 struct LanguageCase {
get_language_valuespvtools::__anonda8b64430111::LanguageCase37 uint32_t get_language_value() const {
38 return static_cast<uint32_t>(language_value);
39 }
40 const char* language_name;
41 SpvSourceLanguage language_value;
42 uint32_t version;
43 };
44
45 // clang-format off
46 // The list of OpSource cases to use.
47 const LanguageCase kLanguageCases[] = {
48 #define CASE(NAME, VERSION) \
49 { #NAME, SpvSourceLanguage##NAME, VERSION }
50 CASE(Unknown, 0),
51 CASE(Unknown, 999),
52 CASE(ESSL, 310),
53 CASE(GLSL, 450),
54 CASE(OpenCL_C, 120),
55 CASE(OpenCL_C, 200),
56 CASE(OpenCL_C, 210),
57 CASE(OpenCL_CPP, 210),
58 CASE(HLSL, 5),
59 CASE(HLSL, 6),
60 #undef CASE
61 };
62 // clang-format on
63
64 using OpSourceTest =
65 spvtest::TextToBinaryTestBase<::testing::TestWithParam<LanguageCase>>;
66
TEST_P(OpSourceTest,AnyLanguage)67 TEST_P(OpSourceTest, AnyLanguage) {
68 const std::string input = std::string("OpSource ") +
69 GetParam().language_name + " " +
70 std::to_string(GetParam().version);
71 EXPECT_THAT(CompiledInstructions(input),
72 Eq(MakeInstruction(SpvOpSource, {GetParam().get_language_value(),
73 GetParam().version})));
74 }
75
76 INSTANTIATE_TEST_CASE_P(TextToBinaryTestDebug, OpSourceTest,
77 ::testing::ValuesIn(kLanguageCases), );
78
TEST_F(OpSourceTest,WrongLanguage)79 TEST_F(OpSourceTest, WrongLanguage) {
80 EXPECT_THAT(CompileFailure("OpSource xxyyzz 12345"),
81 Eq("Invalid source language 'xxyyzz'."));
82 }
83
TEST_F(TextToBinaryTest,OpSourceAcceptsOptionalFileId)84 TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalFileId) {
85 // In the grammar, the file id is an OperandOptionalId.
86 const std::string input = "OpSource GLSL 450 %file_id";
87 EXPECT_THAT(
88 CompiledInstructions(input),
89 Eq(MakeInstruction(SpvOpSource, {SpvSourceLanguageGLSL, 450, 1})));
90 }
91
TEST_F(TextToBinaryTest,OpSourceAcceptsOptionalSourceText)92 TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalSourceText) {
93 std::string fake_source = "To be or not to be";
94 const std::string input =
95 "OpSource GLSL 450 %file_id \"" + fake_source + "\"";
96 EXPECT_THAT(CompiledInstructions(input),
97 Eq(MakeInstruction(SpvOpSource, {SpvSourceLanguageGLSL, 450, 1},
98 MakeVector(fake_source))));
99 }
100
101 // Test OpSourceContinued
102
103 using OpSourceContinuedTest =
104 spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
105
TEST_P(OpSourceContinuedTest,AnyExtension)106 TEST_P(OpSourceContinuedTest, AnyExtension) {
107 // TODO(dneto): utf-8, quoting, escaping
108 const std::string input =
109 std::string("OpSourceContinued \"") + GetParam() + "\"";
110 EXPECT_THAT(
111 CompiledInstructions(input),
112 Eq(MakeInstruction(SpvOpSourceContinued, MakeVector(GetParam()))));
113 }
114
115 // TODO(dneto): utf-8, quoting, escaping
116 INSTANTIATE_TEST_CASE_P(TextToBinaryTestDebug, OpSourceContinuedTest,
117 ::testing::ValuesIn(std::vector<const char*>{
118 "", "foo bar this and that"}), );
119
120 // Test OpSourceExtension
121
122 using OpSourceExtensionTest =
123 spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
124
TEST_P(OpSourceExtensionTest,AnyExtension)125 TEST_P(OpSourceExtensionTest, AnyExtension) {
126 // TODO(dneto): utf-8, quoting, escaping
127 const std::string input =
128 std::string("OpSourceExtension \"") + GetParam() + "\"";
129 EXPECT_THAT(
130 CompiledInstructions(input),
131 Eq(MakeInstruction(SpvOpSourceExtension, MakeVector(GetParam()))));
132 }
133
134 // TODO(dneto): utf-8, quoting, escaping
135 INSTANTIATE_TEST_CASE_P(TextToBinaryTestDebug, OpSourceExtensionTest,
136 ::testing::ValuesIn(std::vector<const char*>{
137 "", "foo bar this and that"}), );
138
TEST_F(TextToBinaryTest,OpLine)139 TEST_F(TextToBinaryTest, OpLine) {
140 EXPECT_THAT(CompiledInstructions("OpLine %srcfile 42 99"),
141 Eq(MakeInstruction(SpvOpLine, {1, 42, 99})));
142 }
143
TEST_F(TextToBinaryTest,OpNoLine)144 TEST_F(TextToBinaryTest, OpNoLine) {
145 EXPECT_THAT(CompiledInstructions("OpNoLine"),
146 Eq(MakeInstruction(SpvOpNoLine, {})));
147 }
148
149 using OpStringTest =
150 spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
151
TEST_P(OpStringTest,AnyString)152 TEST_P(OpStringTest, AnyString) {
153 // TODO(dneto): utf-8, quoting, escaping
154 const std::string input =
155 std::string("%result = OpString \"") + GetParam() + "\"";
156 EXPECT_THAT(CompiledInstructions(input),
157 Eq(MakeInstruction(SpvOpString, {1}, MakeVector(GetParam()))));
158 }
159
160 // TODO(dneto): utf-8, quoting, escaping
161 INSTANTIATE_TEST_CASE_P(TextToBinaryTestDebug, OpStringTest,
162 ::testing::ValuesIn(std::vector<const char*>{
163 "", "foo bar this and that"}), );
164
165 using OpNameTest =
166 spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
167
TEST_P(OpNameTest,AnyString)168 TEST_P(OpNameTest, AnyString) {
169 const std::string input =
170 std::string("OpName %target \"") + GetParam() + "\"";
171 EXPECT_THAT(CompiledInstructions(input),
172 Eq(MakeInstruction(SpvOpName, {1}, MakeVector(GetParam()))));
173 }
174
175 // UTF-8, quoting, escaping, etc. are covered in the StringLiterals tests in
176 // BinaryToText.Literal.cpp.
177 INSTANTIATE_TEST_CASE_P(TextToBinaryTestDebug, OpNameTest,
178 ::testing::Values("", "foo bar this and that"), );
179
180 using OpMemberNameTest =
181 spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
182
TEST_P(OpMemberNameTest,AnyString)183 TEST_P(OpMemberNameTest, AnyString) {
184 // TODO(dneto): utf-8, quoting, escaping
185 const std::string input =
186 std::string("OpMemberName %type 42 \"") + GetParam() + "\"";
187 EXPECT_THAT(
188 CompiledInstructions(input),
189 Eq(MakeInstruction(SpvOpMemberName, {1, 42}, MakeVector(GetParam()))));
190 }
191
192 // TODO(dneto): utf-8, quoting, escaping
193 INSTANTIATE_TEST_CASE_P(TextToBinaryTestDebug, OpMemberNameTest,
194 ::testing::ValuesIn(std::vector<const char*>{
195 "", "foo bar this and that"}), );
196
197 // TODO(dneto): Parse failures?
198
199 using OpModuleProcessedTest =
200 spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
201
TEST_P(OpModuleProcessedTest,AnyString)202 TEST_P(OpModuleProcessedTest, AnyString) {
203 const std::string input =
204 std::string("OpModuleProcessed \"") + GetParam() + "\"";
205 EXPECT_THAT(
206 CompiledInstructions(input, SPV_ENV_UNIVERSAL_1_1),
207 Eq(MakeInstruction(SpvOpModuleProcessed, MakeVector(GetParam()))));
208 }
209
210 INSTANTIATE_TEST_CASE_P(TextToBinaryTestDebug, OpModuleProcessedTest,
211 ::testing::Values("", "foo bar this and that"), );
212
213 } // namespace
214 } // namespace spvtools
215