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