• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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::__anon75c76f440111::LanguageCase38   uint32_t get_language_value() const {
39     return static_cast<uint32_t>(language_value);
40   }
41   const char* language_name;
42   SpvSourceLanguage 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, SpvSourceLanguage##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(CompiledInstructions(input),
73               Eq(MakeInstruction(SpvOpSource, {GetParam().get_language_value(),
74                                                GetParam().version})));
75 }
76 
77 INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceTest,
78                          ::testing::ValuesIn(kLanguageCases));
79 
TEST_F(OpSourceTest,WrongLanguage)80 TEST_F(OpSourceTest, WrongLanguage) {
81   EXPECT_THAT(CompileFailure("OpSource xxyyzz 12345"),
82               Eq("Invalid source language 'xxyyzz'."));
83 }
84 
TEST_F(TextToBinaryTest,OpSourceAcceptsOptionalFileId)85 TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalFileId) {
86   // In the grammar, the file id is an OperandOptionalId.
87   const std::string input = "OpSource GLSL 450 %file_id";
88   EXPECT_THAT(
89       CompiledInstructions(input),
90       Eq(MakeInstruction(SpvOpSource, {SpvSourceLanguageGLSL, 450, 1})));
91 }
92 
TEST_F(TextToBinaryTest,OpSourceAcceptsOptionalSourceText)93 TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalSourceText) {
94   std::string fake_source = "To be or not to be";
95   const std::string input =
96       "OpSource GLSL 450 %file_id \"" + fake_source + "\"";
97   EXPECT_THAT(CompiledInstructions(input),
98               Eq(MakeInstruction(SpvOpSource, {SpvSourceLanguageGLSL, 450, 1},
99                                  MakeVector(fake_source))));
100 }
101 
102 // Test OpSourceContinued
103 
104 using OpSourceContinuedTest =
105     spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
106 
TEST_P(OpSourceContinuedTest,AnyExtension)107 TEST_P(OpSourceContinuedTest, AnyExtension) {
108   // TODO(dneto): utf-8, quoting, escaping
109   const std::string input =
110       std::string("OpSourceContinued \"") + GetParam() + "\"";
111   EXPECT_THAT(
112       CompiledInstructions(input),
113       Eq(MakeInstruction(SpvOpSourceContinued, MakeVector(GetParam()))));
114 }
115 
116 // TODO(dneto): utf-8, quoting, escaping
117 INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceContinuedTest,
118                          ::testing::ValuesIn(std::vector<const char*>{
119                              "", "foo bar this and that"}));
120 
121 // Test OpSourceExtension
122 
123 using OpSourceExtensionTest =
124     spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
125 
TEST_P(OpSourceExtensionTest,AnyExtension)126 TEST_P(OpSourceExtensionTest, AnyExtension) {
127   // TODO(dneto): utf-8, quoting, escaping
128   const std::string input =
129       std::string("OpSourceExtension \"") + GetParam() + "\"";
130   EXPECT_THAT(
131       CompiledInstructions(input),
132       Eq(MakeInstruction(SpvOpSourceExtension, MakeVector(GetParam()))));
133 }
134 
135 // TODO(dneto): utf-8, quoting, escaping
136 INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceExtensionTest,
137                          ::testing::ValuesIn(std::vector<const char*>{
138                              "", "foo bar this and that"}));
139 
TEST_F(TextToBinaryTest,OpLine)140 TEST_F(TextToBinaryTest, OpLine) {
141   EXPECT_THAT(CompiledInstructions("OpLine %srcfile 42 99"),
142               Eq(MakeInstruction(SpvOpLine, {1, 42, 99})));
143 }
144 
TEST_F(TextToBinaryTest,OpNoLine)145 TEST_F(TextToBinaryTest, OpNoLine) {
146   EXPECT_THAT(CompiledInstructions("OpNoLine"),
147               Eq(MakeInstruction(SpvOpNoLine, {})));
148 }
149 
150 using OpStringTest =
151     spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
152 
TEST_P(OpStringTest,AnyString)153 TEST_P(OpStringTest, AnyString) {
154   // TODO(dneto): utf-8, quoting, escaping
155   const std::string input =
156       std::string("%result = OpString \"") + GetParam() + "\"";
157   EXPECT_THAT(CompiledInstructions(input),
158               Eq(MakeInstruction(SpvOpString, {1}, MakeVector(GetParam()))));
159 }
160 
161 // TODO(dneto): utf-8, quoting, escaping
162 INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpStringTest,
163                          ::testing::ValuesIn(std::vector<const char*>{
164                              "", "foo bar this and that"}));
165 
166 using OpNameTest =
167     spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
168 
TEST_P(OpNameTest,AnyString)169 TEST_P(OpNameTest, AnyString) {
170   const std::string input =
171       std::string("OpName %target \"") + GetParam() + "\"";
172   EXPECT_THAT(CompiledInstructions(input),
173               Eq(MakeInstruction(SpvOpName, {1}, MakeVector(GetParam()))));
174 }
175 
176 // UTF-8, quoting, escaping, etc. are covered in the StringLiterals tests in
177 // BinaryToText.Literal.cpp.
178 INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpNameTest,
179                          ::testing::Values("", "foo bar this and that"));
180 
181 using OpMemberNameTest =
182     spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
183 
TEST_P(OpMemberNameTest,AnyString)184 TEST_P(OpMemberNameTest, AnyString) {
185   // TODO(dneto): utf-8, quoting, escaping
186   const std::string input =
187       std::string("OpMemberName %type 42 \"") + GetParam() + "\"";
188   EXPECT_THAT(
189       CompiledInstructions(input),
190       Eq(MakeInstruction(SpvOpMemberName, {1, 42}, MakeVector(GetParam()))));
191 }
192 
193 // TODO(dneto): utf-8, quoting, escaping
194 INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpMemberNameTest,
195                          ::testing::ValuesIn(std::vector<const char*>{
196                              "", "foo bar this and that"}));
197 
198 // TODO(dneto): Parse failures?
199 
200 using OpModuleProcessedTest =
201     spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
202 
TEST_P(OpModuleProcessedTest,AnyString)203 TEST_P(OpModuleProcessedTest, AnyString) {
204   const std::string input =
205       std::string("OpModuleProcessed \"") + GetParam() + "\"";
206   EXPECT_THAT(
207       CompiledInstructions(input, SPV_ENV_UNIVERSAL_1_1),
208       Eq(MakeInstruction(SpvOpModuleProcessed, MakeVector(GetParam()))));
209 }
210 
211 INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpModuleProcessedTest,
212                          ::testing::Values("", "foo bar this and that"));
213 
214 }  // namespace
215 }  // namespace spvtools
216