Setup dependent external sources
[platform/upstream/VK-GL-CTS.git] / external / spirv-tools / src / test / binary_to_text_test.cpp
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 #include "unit_spirv.h"
16
17 #include <sstream>
18
19 #include "gmock/gmock.h"
20
21 #include "test_fixture.h"
22 #include "source/spirv_constant.h"
23
24 namespace {
25
26 using ::testing::Combine;
27 using ::testing::Eq;
28 using ::testing::HasSubstr;
29 using spvtest::AutoText;
30 using spvtest::ScopedContext;
31 using spvtest::TextToBinaryTest;
32 using std::get;
33 using std::tuple;
34
35 class BinaryToText : public ::testing::Test {
36  public:
37   BinaryToText() : context(spvContextCreate(SPV_ENV_UNIVERSAL_1_0)) {}
38   ~BinaryToText() { spvContextDestroy(context); }
39
40   virtual void SetUp() {
41     const char* textStr = R"(
42       OpSource OpenCL_C 12
43       OpMemoryModel Physical64 OpenCL
44       OpSourceExtension "PlaceholderExtensionName"
45       OpEntryPoint Kernel %1 "foo"
46       OpExecutionMode %1 LocalSizeHint 1 1 1
47  %2 = OpTypeVoid
48  %3 = OpTypeBool
49  %4 = OpTypeInt 8 0
50  %5 = OpTypeInt 8 1
51  %6 = OpTypeInt 16 0
52  %7 = OpTypeInt 16 1
53  %8 = OpTypeInt 32 0
54  %9 = OpTypeInt 32 1
55 %10 = OpTypeInt 64 0
56 %11 = OpTypeInt 64 1
57 %12 = OpTypeFloat 16
58 %13 = OpTypeFloat 32
59 %14 = OpTypeFloat 64
60 %15 = OpTypeVector %4 2
61 )";
62     spv_text_t text = {textStr, strlen(textStr)};
63     spv_diagnostic diagnostic = nullptr;
64     spv_result_t error =
65         spvTextToBinary(context, text.str, text.length, &binary, &diagnostic);
66     if (error) {
67       spvDiagnosticPrint(diagnostic);
68       spvDiagnosticDestroy(diagnostic);
69       ASSERT_EQ(SPV_SUCCESS, error);
70     }
71   }
72
73   virtual void TearDown() { spvBinaryDestroy(binary); }
74
75   // Compiles the given assembly text, and saves it into 'binary'.
76   void CompileSuccessfully(std::string text) {
77     spv_diagnostic diagnostic = nullptr;
78     EXPECT_EQ(SPV_SUCCESS, spvTextToBinary(context, text.c_str(), text.size(),
79                                            &binary, &diagnostic));
80   }
81
82   spv_context context;
83   spv_binary binary;
84 };
85
86 TEST_F(BinaryToText, Default) {
87   spv_text text = nullptr;
88   spv_diagnostic diagnostic = nullptr;
89   ASSERT_EQ(
90       SPV_SUCCESS,
91       spvBinaryToText(context, binary->code, binary->wordCount,
92                       SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
93   printf("%s", text->str);
94   spvTextDestroy(text);
95 }
96
97 TEST_F(BinaryToText, MissingModule) {
98   spv_text text;
99   spv_diagnostic diagnostic = nullptr;
100   EXPECT_EQ(
101       SPV_ERROR_INVALID_BINARY,
102       spvBinaryToText(context, nullptr, 42, SPV_BINARY_TO_TEXT_OPTION_NONE,
103                       &text, &diagnostic));
104   EXPECT_THAT(diagnostic->error, Eq(std::string("Missing module.")));
105   if (diagnostic) {
106     spvDiagnosticPrint(diagnostic);
107     spvDiagnosticDestroy(diagnostic);
108   }
109 }
110
111 TEST_F(BinaryToText, TruncatedModule) {
112   // Make a valid module with zero instructions.
113   CompileSuccessfully("");
114   EXPECT_EQ(SPV_INDEX_INSTRUCTION, binary->wordCount);
115
116   for (size_t length = 0; length < SPV_INDEX_INSTRUCTION; length++) {
117     spv_text text = nullptr;
118     spv_diagnostic diagnostic = nullptr;
119     EXPECT_EQ(
120         SPV_ERROR_INVALID_BINARY,
121         spvBinaryToText(context, binary->code, length,
122                         SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
123     ASSERT_NE(nullptr, diagnostic);
124     std::stringstream expected;
125     expected << "Module has incomplete header: only " << length
126              << " words instead of " << SPV_INDEX_INSTRUCTION;
127     EXPECT_THAT(diagnostic->error, Eq(expected.str()));
128     spvDiagnosticDestroy(diagnostic);
129   }
130 }
131
132 TEST_F(BinaryToText, InvalidMagicNumber) {
133   CompileSuccessfully("");
134   std::vector<uint32_t> damaged_binary(binary->code,
135                                        binary->code + binary->wordCount);
136   damaged_binary[SPV_INDEX_MAGIC_NUMBER] ^= 123;
137
138   spv_diagnostic diagnostic = nullptr;
139   spv_text text;
140   EXPECT_EQ(
141       SPV_ERROR_INVALID_BINARY,
142       spvBinaryToText(context, damaged_binary.data(), damaged_binary.size(),
143                       SPV_BINARY_TO_TEXT_OPTION_NONE, &text, &diagnostic));
144   ASSERT_NE(nullptr, diagnostic);
145   std::stringstream expected;
146   expected << "Invalid SPIR-V magic number '" << std::hex
147            << damaged_binary[SPV_INDEX_MAGIC_NUMBER] << "'.";
148   EXPECT_THAT(diagnostic->error, Eq(expected.str()));
149   spvDiagnosticDestroy(diagnostic);
150 }
151
152 struct FailedDecodeCase {
153   std::string source_text;
154   std::vector<uint32_t> appended_instruction;
155   std::string expected_error_message;
156 };
157
158 using BinaryToTextFail =
159     spvtest::TextToBinaryTestBase<::testing::TestWithParam<FailedDecodeCase>>;
160
161 TEST_P(BinaryToTextFail, EncodeSuccessfullyDecodeFailed) {
162   EXPECT_THAT(EncodeSuccessfullyDecodeFailed(GetParam().source_text,
163                                              GetParam().appended_instruction),
164               Eq(GetParam().expected_error_message));
165 }
166
167 INSTANTIATE_TEST_CASE_P(
168     InvalidIds, BinaryToTextFail,
169     ::testing::ValuesIn(std::vector<FailedDecodeCase>{
170         {"", spvtest::MakeInstruction(SpvOpTypeVoid, {0}),
171          "Error: Result Id is 0"},
172         {"", spvtest::MakeInstruction(SpvOpConstant, {0, 1, 42}),
173          "Error: Type Id is 0"},
174         {"%1 = OpTypeVoid", spvtest::MakeInstruction(SpvOpTypeVoid, {1}),
175          "Id 1 is defined more than once"},
176         {"%1 = OpTypeVoid\n"
177          "%2 = OpNot %1 %foo",
178          spvtest::MakeInstruction(SpvOpNot, {1, 2, 3}),
179          "Id 2 is defined more than once"},
180         {"%1 = OpTypeVoid\n"
181          "%2 = OpNot %1 %foo",
182          spvtest::MakeInstruction(SpvOpNot, {1, 1, 3}),
183          "Id 1 is defined more than once"},
184         // The following are the two failure cases for
185         // Parser::setNumericTypeInfoForType.
186         {"", spvtest::MakeInstruction(SpvOpConstant, {500, 1, 42}),
187          "Type Id 500 is not a type"},
188         {"%1 = OpTypeInt 32 0\n"
189          "%2 = OpTypeVector %1 4",
190          spvtest::MakeInstruction(SpvOpConstant, {2, 3, 999}),
191          "Type Id 2 is not a scalar numeric type"},
192     }), );
193
194 INSTANTIATE_TEST_CASE_P(
195     InvalidIdsCheckedDuringLiteralCaseParsing, BinaryToTextFail,
196     ::testing::ValuesIn(std::vector<FailedDecodeCase>{
197         {"", spvtest::MakeInstruction(SpvOpSwitch, {1, 2, 3, 4}),
198          "Invalid OpSwitch: selector id 1 has no type"},
199         {"%1 = OpTypeVoid\n",
200          spvtest::MakeInstruction(SpvOpSwitch, {1, 2, 3, 4}),
201          "Invalid OpSwitch: selector id 1 is a type, not a value"},
202         {"%1 = OpConstantTrue !500",
203          spvtest::MakeInstruction(SpvOpSwitch, {1, 2, 3, 4}),
204          "Type Id 500 is not a type"},
205         {"%1 = OpTypeFloat 32\n%2 = OpConstant %1 1.5",
206          spvtest::MakeInstruction(SpvOpSwitch, {2, 3, 4, 5}),
207          "Invalid OpSwitch: selector id 2 is not a scalar integer"},
208     }), );
209
210 TEST_F(TextToBinaryTest, OneInstruction) {
211   const std::string input = "OpSource OpenCL_C 12\n";
212   EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
213 }
214
215 // Exercise the case where an operand itself has operands.
216 // This could detect problems in updating the expected-set-of-operands
217 // list.
218 TEST_F(TextToBinaryTest, OperandWithOperands) {
219   const std::string input = R"(OpEntryPoint Kernel %1 "foo"
220 OpExecutionMode %1 LocalSizeHint 100 200 300
221 %2 = OpTypeVoid
222 %3 = OpTypeFunction %2
223 %1 = OpFunction %1 None %3
224 )";
225   EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
226 }
227
228 using RoundTripInstructionsTest = spvtest::TextToBinaryTestBase<
229     ::testing::TestWithParam<tuple<spv_target_env, std::string>>>;
230
231 TEST_P(RoundTripInstructionsTest, Sample) {
232   EXPECT_THAT(EncodeAndDecodeSuccessfully(get<1>(GetParam()),
233                                           SPV_BINARY_TO_TEXT_OPTION_NONE,
234                                           get<0>(GetParam())),
235               Eq(get<1>(GetParam())));
236 }
237
238 // clang-format off
239 INSTANTIATE_TEST_CASE_P(
240     NumericLiterals, RoundTripInstructionsTest,
241     // This test is independent of environment, so just test the one.
242     Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
243                               SPV_ENV_UNIVERSAL_1_2),
244             ::testing::ValuesIn(std::vector<std::string>{
245                 "%1 = OpTypeInt 12 0\n%2 = OpConstant %1 1867\n",
246                 "%1 = OpTypeInt 12 1\n%2 = OpConstant %1 1867\n",
247                 "%1 = OpTypeInt 12 1\n%2 = OpConstant %1 -1867\n",
248                 "%1 = OpTypeInt 32 0\n%2 = OpConstant %1 1867\n",
249                 "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 1867\n",
250                 "%1 = OpTypeInt 32 1\n%2 = OpConstant %1 -1867\n",
251                 "%1 = OpTypeInt 64 0\n%2 = OpConstant %1 18446744073709551615\n",
252                 "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 9223372036854775807\n",
253                 "%1 = OpTypeInt 64 1\n%2 = OpConstant %1 -9223372036854775808\n",
254                 // 16-bit floats print as hex floats.
255                 "%1 = OpTypeFloat 16\n%2 = OpConstant %1 0x1.ff4p+16\n",
256                 "%1 = OpTypeFloat 16\n%2 = OpConstant %1 -0x1.d2cp-10\n",
257                 // 32-bit floats
258                 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -3.275\n",
259                 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1.8p+128\n", // NaN
260                 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1.0002p+128\n", // NaN
261                 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 0x1p+128\n", // Inf
262                 "%1 = OpTypeFloat 32\n%2 = OpConstant %1 -0x1p+128\n", // -Inf
263                 // 64-bit floats
264                 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -3.275\n",
265                 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.ffffffffffffap-1023\n", // small normal
266                 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.ffffffffffffap-1023\n",
267                 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1.8p+1024\n", // NaN
268                 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1.0002p+1024\n", // NaN
269                 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 0x1p+1024\n", // Inf
270                 "%1 = OpTypeFloat 64\n%2 = OpConstant %1 -0x1p+1024\n", // -Inf
271             })), );
272 // clang-format on
273
274 INSTANTIATE_TEST_CASE_P(
275     MemoryAccessMasks, RoundTripInstructionsTest,
276     Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
277                               SPV_ENV_UNIVERSAL_1_2),
278             ::testing::ValuesIn(std::vector<std::string>{
279                 "OpStore %1 %2\n",       // 3 words long.
280                 "OpStore %1 %2 None\n",  // 4 words long, explicit final 0.
281                 "OpStore %1 %2 Volatile\n", "OpStore %1 %2 Aligned 8\n",
282                 "OpStore %1 %2 Nontemporal\n",
283                 // Combinations show the names from LSB to MSB
284                 "OpStore %1 %2 Volatile|Aligned 16\n",
285                 "OpStore %1 %2 Volatile|Nontemporal\n",
286                 "OpStore %1 %2 Volatile|Aligned|Nontemporal 32\n",
287             })), );
288
289 INSTANTIATE_TEST_CASE_P(
290     FPFastMathModeMasks, RoundTripInstructionsTest,
291     Combine(
292         ::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
293                           SPV_ENV_UNIVERSAL_1_2),
294         ::testing::ValuesIn(std::vector<std::string>{
295             "OpDecorate %1 FPFastMathMode None\n",
296             "OpDecorate %1 FPFastMathMode NotNaN\n",
297             "OpDecorate %1 FPFastMathMode NotInf\n",
298             "OpDecorate %1 FPFastMathMode NSZ\n",
299             "OpDecorate %1 FPFastMathMode AllowRecip\n",
300             "OpDecorate %1 FPFastMathMode Fast\n",
301             // Combinations show the names from LSB to MSB
302             "OpDecorate %1 FPFastMathMode NotNaN|NotInf\n",
303             "OpDecorate %1 FPFastMathMode NSZ|AllowRecip\n",
304             "OpDecorate %1 FPFastMathMode NotNaN|NotInf|NSZ|AllowRecip|Fast\n",
305         })), );
306
307 INSTANTIATE_TEST_CASE_P(
308     LoopControlMasks, RoundTripInstructionsTest,
309     Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
310                               SPV_ENV_UNIVERSAL_1_2),
311             ::testing::ValuesIn(std::vector<std::string>{
312                 "OpLoopMerge %1 %2 None\n", "OpLoopMerge %1 %2 Unroll\n",
313                 "OpLoopMerge %1 %2 DontUnroll\n",
314                 "OpLoopMerge %1 %2 Unroll|DontUnroll\n",
315             })), );
316
317 INSTANTIATE_TEST_CASE_P(LoopControlMasksV11, RoundTripInstructionsTest,
318                         Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_1,
319                                                   SPV_ENV_UNIVERSAL_1_2),
320                                 ::testing::ValuesIn(std::vector<std::string>{
321                                     "OpLoopMerge %1 %2 DependencyInfinite\n",
322                                     "OpLoopMerge %1 %2 DependencyLength 8\n",
323                                 })), );
324
325 INSTANTIATE_TEST_CASE_P(
326     SelectionControlMasks, RoundTripInstructionsTest,
327     Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
328                               SPV_ENV_UNIVERSAL_1_2),
329             ::testing::ValuesIn(std::vector<std::string>{
330                 "OpSelectionMerge %1 None\n", "OpSelectionMerge %1 Flatten\n",
331                 "OpSelectionMerge %1 DontFlatten\n",
332                 "OpSelectionMerge %1 Flatten|DontFlatten\n",
333             })), );
334
335 INSTANTIATE_TEST_CASE_P(
336     FunctionControlMasks, RoundTripInstructionsTest,
337     Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
338                               SPV_ENV_UNIVERSAL_1_2),
339             ::testing::ValuesIn(std::vector<std::string>{
340                 "%2 = OpFunction %1 None %3\n",
341                 "%2 = OpFunction %1 Inline %3\n",
342                 "%2 = OpFunction %1 DontInline %3\n",
343                 "%2 = OpFunction %1 Pure %3\n", "%2 = OpFunction %1 Const %3\n",
344                 "%2 = OpFunction %1 Inline|Pure|Const %3\n",
345                 "%2 = OpFunction %1 DontInline|Const %3\n",
346             })), );
347
348 INSTANTIATE_TEST_CASE_P(
349     ImageMasks, RoundTripInstructionsTest,
350     Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_0, SPV_ENV_UNIVERSAL_1_1,
351                               SPV_ENV_UNIVERSAL_1_2),
352             ::testing::ValuesIn(std::vector<std::string>{
353                 "%2 = OpImageFetch %1 %3 %4\n",
354                 "%2 = OpImageFetch %1 %3 %4 None\n",
355                 "%2 = OpImageFetch %1 %3 %4 Bias %5\n",
356                 "%2 = OpImageFetch %1 %3 %4 Lod %5\n",
357                 "%2 = OpImageFetch %1 %3 %4 Grad %5 %6\n",
358                 "%2 = OpImageFetch %1 %3 %4 ConstOffset %5\n",
359                 "%2 = OpImageFetch %1 %3 %4 Offset %5\n",
360                 "%2 = OpImageFetch %1 %3 %4 ConstOffsets %5\n",
361                 "%2 = OpImageFetch %1 %3 %4 Sample %5\n",
362                 "%2 = OpImageFetch %1 %3 %4 MinLod %5\n",
363                 "%2 = OpImageFetch %1 %3 %4 Bias|Lod|Grad %5 %6 %7 %8\n",
364                 "%2 = OpImageFetch %1 %3 %4 ConstOffset|Offset|ConstOffsets"
365                 " %5 %6 %7\n",
366                 "%2 = OpImageFetch %1 %3 %4 Sample|MinLod %5 %6\n",
367                 "%2 = OpImageFetch %1 %3 %4"
368                 " Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample|MinLod"
369                 " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"})), );
370
371 INSTANTIATE_TEST_CASE_P(
372     NewInstructionsInSPIRV1_2, RoundTripInstructionsTest,
373     Combine(::testing::Values(SPV_ENV_UNIVERSAL_1_2),
374             ::testing::ValuesIn(std::vector<std::string>{
375                 "OpExecutionModeId %1 SubgroupsPerWorkgroupId %2\n",
376                 "OpExecutionModeId %1 LocalSizeId %2 %3 %4\n",
377                 "OpExecutionModeId %1 LocalSizeHintId %2\n",
378                 "OpDecorateId %1 AlignmentId %2\n",
379                 "OpDecorateId %1 MaxByteOffsetId %2\n",
380             })), );
381
382 using MaskSorting = TextToBinaryTest;
383
384 TEST_F(MaskSorting, MasksAreSortedFromLSBToMSB) {
385   EXPECT_THAT(EncodeAndDecodeSuccessfully(
386                   "OpStore %1 %2 Nontemporal|Aligned|Volatile 32"),
387               Eq("OpStore %1 %2 Volatile|Aligned|Nontemporal 32\n"));
388   EXPECT_THAT(
389       EncodeAndDecodeSuccessfully(
390           "OpDecorate %1 FPFastMathMode NotInf|Fast|AllowRecip|NotNaN|NSZ"),
391       Eq("OpDecorate %1 FPFastMathMode NotNaN|NotInf|NSZ|AllowRecip|Fast\n"));
392   EXPECT_THAT(
393       EncodeAndDecodeSuccessfully("OpLoopMerge %1 %2 DontUnroll|Unroll"),
394       Eq("OpLoopMerge %1 %2 Unroll|DontUnroll\n"));
395   EXPECT_THAT(
396       EncodeAndDecodeSuccessfully("OpSelectionMerge %1 DontFlatten|Flatten"),
397       Eq("OpSelectionMerge %1 Flatten|DontFlatten\n"));
398   EXPECT_THAT(EncodeAndDecodeSuccessfully(
399                   "%2 = OpFunction %1 DontInline|Const|Pure|Inline %3"),
400               Eq("%2 = OpFunction %1 Inline|DontInline|Pure|Const %3\n"));
401   EXPECT_THAT(EncodeAndDecodeSuccessfully(
402                   "%2 = OpImageFetch %1 %3 %4"
403                   " MinLod|Sample|Offset|Lod|Grad|ConstOffsets|ConstOffset|Bias"
404                   " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"),
405               Eq("%2 = OpImageFetch %1 %3 %4"
406                  " Bias|Lod|Grad|ConstOffset|Offset|ConstOffsets|Sample|MinLod"
407                  " %5 %6 %7 %8 %9 %10 %11 %12 %13\n"));
408 }
409
410 using OperandTypeTest = TextToBinaryTest;
411
412 TEST_F(OperandTypeTest, OptionalTypedLiteralNumber) {
413   const std::string input =
414       "%1 = OpTypeInt 32 0\n"
415       "%2 = OpConstant %1 42\n"
416       "OpSwitch %2 %3 100 %4\n";
417   EXPECT_EQ(input, EncodeAndDecodeSuccessfully(input));
418 }
419
420 using IndentTest = spvtest::TextToBinaryTest;
421
422 TEST_F(IndentTest, Sample) {
423   const std::string input = R"(
424 OpCapability Shader
425 OpMemoryModel Logical GLSL450
426 %1 = OpTypeInt 32 0
427 %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10 ; force IDs into double digits
428 %11 = OpConstant %1 42
429 OpStore %2 %3 Aligned|Volatile 4 ; bogus, but not indented
430 )";
431   const std::string expected =
432       R"(               OpCapability Shader
433                OpMemoryModel Logical GLSL450
434           %1 = OpTypeInt 32 0
435           %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10
436          %11 = OpConstant %1 42
437                OpStore %2 %3 Volatile|Aligned 4
438 )";
439   EXPECT_THAT(
440       EncodeAndDecodeSuccessfully(input, SPV_BINARY_TO_TEXT_OPTION_INDENT),
441       expected);
442 }
443
444 using FriendlyNameDisassemblyTest = spvtest::TextToBinaryTest;
445
446 TEST_F(FriendlyNameDisassemblyTest, Sample) {
447   const std::string input = R"(
448 OpCapability Shader
449 OpMemoryModel Logical GLSL450
450 %1 = OpTypeInt 32 0
451 %2 = OpTypeStruct %1 %3 %4 %5 %6 %7 %8 %9 %10 ; force IDs into double digits
452 %11 = OpConstant %1 42
453 )";
454   const std::string expected =
455       R"(OpCapability Shader
456 OpMemoryModel Logical GLSL450
457 %uint = OpTypeInt 32 0
458 %_struct_2 = OpTypeStruct %uint %3 %4 %5 %6 %7 %8 %9 %10
459 %uint_42 = OpConstant %uint 42
460 )";
461   EXPECT_THAT(EncodeAndDecodeSuccessfully(
462                   input, SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES),
463               expected);
464 }
465
466 TEST_F(TextToBinaryTest, ShowByteOffsetsWhenRequested) {
467   const std::string input = R"(
468 OpCapability Shader
469 OpMemoryModel Logical GLSL450
470 %1 = OpTypeInt 32 0
471 %2 = OpTypeVoid
472 )";
473   const std::string expected =
474       R"(OpCapability Shader ; 0x00000014
475 OpMemoryModel Logical GLSL450 ; 0x0000001c
476 %1 = OpTypeInt 32 0 ; 0x00000028
477 %2 = OpTypeVoid ; 0x00000038
478 )";
479   EXPECT_THAT(EncodeAndDecodeSuccessfully(
480                   input, SPV_BINARY_TO_TEXT_OPTION_SHOW_BYTE_OFFSET),
481               expected);
482 }
483
484 // Test version string.
485 TEST_F(TextToBinaryTest, VersionString) {
486   auto words = CompileSuccessfully("");
487   spv_text decoded_text = nullptr;
488   EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
489                               words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
490                               &decoded_text, &diagnostic),
491               Eq(SPV_SUCCESS));
492   EXPECT_EQ(nullptr, diagnostic);
493
494   EXPECT_THAT(decoded_text->str, HasSubstr("Version: 1.0\n"))
495       << EncodeAndDecodeSuccessfully("");
496   spvTextDestroy(decoded_text);
497 }
498
499 // Test generator string.
500
501 // A test case for the generator string.  This allows us to
502 // test both of the 16-bit components of the generator word.
503 struct GeneratorStringCase {
504   uint16_t generator;
505   uint16_t misc;
506   std::string expected;
507 };
508
509 using GeneratorStringTest = spvtest::TextToBinaryTestBase<
510     ::testing::TestWithParam<GeneratorStringCase>>;
511
512 TEST_P(GeneratorStringTest, Sample) {
513   auto words = CompileSuccessfully("");
514   EXPECT_EQ(2u, SPV_INDEX_GENERATOR_NUMBER);
515   words[SPV_INDEX_GENERATOR_NUMBER] =
516       SPV_GENERATOR_WORD(GetParam().generator, GetParam().misc);
517
518   spv_text decoded_text = nullptr;
519   EXPECT_THAT(spvBinaryToText(ScopedContext().context, words.data(),
520                               words.size(), SPV_BINARY_TO_TEXT_OPTION_NONE,
521                               &decoded_text, &diagnostic),
522               Eq(SPV_SUCCESS));
523   EXPECT_THAT(diagnostic, Eq(nullptr));
524   EXPECT_THAT(std::string(decoded_text->str), HasSubstr(GetParam().expected));
525   spvTextDestroy(decoded_text);
526 }
527
528 INSTANTIATE_TEST_CASE_P(GeneratorStrings, GeneratorStringTest,
529                         ::testing::ValuesIn(std::vector<GeneratorStringCase>{
530                             {SPV_GENERATOR_KHRONOS, 12, "Khronos; 12"},
531                             {SPV_GENERATOR_LUNARG, 99, "LunarG; 99"},
532                             {SPV_GENERATOR_VALVE, 1, "Valve; 1"},
533                             {SPV_GENERATOR_CODEPLAY, 65535,
534                              "Codeplay; 65535"},
535                             {SPV_GENERATOR_NVIDIA, 19, "NVIDIA; 19"},
536                             {SPV_GENERATOR_ARM, 1000, "ARM; 1000"},
537                             {SPV_GENERATOR_KHRONOS_LLVM_TRANSLATOR, 38,
538                              "Khronos LLVM/SPIR-V Translator; 38"},
539                             {SPV_GENERATOR_KHRONOS_ASSEMBLER, 2,
540                              "Khronos SPIR-V Tools Assembler; 2"},
541                             {SPV_GENERATOR_KHRONOS_GLSLANG, 1,
542                              "Khronos Glslang Reference Front End; 1"},
543                             {1000, 18, "Unknown(1000); 18"},
544                             {65535, 32767, "Unknown(65535); 32767"},
545                         }), );
546
547 }  // anonymous namespace