text_to_binary.debug_test.cpp 7.1 KB

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