text_to_binary.debug_test.cpp 7.3 KB

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