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