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