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