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 "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. SpvSourceLanguage 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, SpvSourceLanguage##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(CompiledInstructions(input),
  63. Eq(MakeInstruction(SpvOpSource, {GetParam().get_language_value(),
  64. GetParam().version})));
  65. }
  66. INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceTest,
  67. ::testing::ValuesIn(kLanguageCases));
  68. TEST_F(OpSourceTest, WrongLanguage) {
  69. EXPECT_THAT(CompileFailure("OpSource xxyyzz 12345"),
  70. Eq("Invalid source language 'xxyyzz'."));
  71. }
  72. TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalFileId) {
  73. // In the grammar, the file id is an OperandOptionalId.
  74. const std::string input = "OpSource GLSL 450 %file_id";
  75. EXPECT_THAT(
  76. CompiledInstructions(input),
  77. Eq(MakeInstruction(SpvOpSource, {SpvSourceLanguageGLSL, 450, 1})));
  78. }
  79. TEST_F(TextToBinaryTest, OpSourceAcceptsOptionalSourceText) {
  80. std::string fake_source = "To be or not to be";
  81. const std::string input =
  82. "OpSource GLSL 450 %file_id \"" + fake_source + "\"";
  83. EXPECT_THAT(CompiledInstructions(input),
  84. Eq(MakeInstruction(SpvOpSource, {SpvSourceLanguageGLSL, 450, 1},
  85. MakeVector(fake_source))));
  86. }
  87. // Test OpSourceContinued
  88. using OpSourceContinuedTest =
  89. spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
  90. TEST_P(OpSourceContinuedTest, AnyExtension) {
  91. // TODO(dneto): utf-8, quoting, escaping
  92. const std::string input =
  93. std::string("OpSourceContinued \"") + GetParam() + "\"";
  94. EXPECT_THAT(
  95. CompiledInstructions(input),
  96. Eq(MakeInstruction(SpvOpSourceContinued, MakeVector(GetParam()))));
  97. }
  98. // TODO(dneto): utf-8, quoting, escaping
  99. INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceContinuedTest,
  100. ::testing::ValuesIn(std::vector<const char*>{
  101. "", "foo bar this and that"}));
  102. // Test OpSourceExtension
  103. using OpSourceExtensionTest =
  104. spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
  105. TEST_P(OpSourceExtensionTest, AnyExtension) {
  106. // TODO(dneto): utf-8, quoting, escaping
  107. const std::string input =
  108. std::string("OpSourceExtension \"") + GetParam() + "\"";
  109. EXPECT_THAT(
  110. CompiledInstructions(input),
  111. Eq(MakeInstruction(SpvOpSourceExtension, MakeVector(GetParam()))));
  112. }
  113. // TODO(dneto): utf-8, quoting, escaping
  114. INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpSourceExtensionTest,
  115. ::testing::ValuesIn(std::vector<const char*>{
  116. "", "foo bar this and that"}));
  117. TEST_F(TextToBinaryTest, OpLine) {
  118. EXPECT_THAT(CompiledInstructions("OpLine %srcfile 42 99"),
  119. Eq(MakeInstruction(SpvOpLine, {1, 42, 99})));
  120. }
  121. TEST_F(TextToBinaryTest, OpNoLine) {
  122. EXPECT_THAT(CompiledInstructions("OpNoLine"),
  123. Eq(MakeInstruction(SpvOpNoLine, {})));
  124. }
  125. using OpStringTest =
  126. spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
  127. TEST_P(OpStringTest, AnyString) {
  128. // TODO(dneto): utf-8, quoting, escaping
  129. const std::string input =
  130. std::string("%result = OpString \"") + GetParam() + "\"";
  131. EXPECT_THAT(CompiledInstructions(input),
  132. Eq(MakeInstruction(SpvOpString, {1}, MakeVector(GetParam()))));
  133. }
  134. // TODO(dneto): utf-8, quoting, escaping
  135. INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpStringTest,
  136. ::testing::ValuesIn(std::vector<const char*>{
  137. "", "foo bar this and that"}));
  138. using OpNameTest =
  139. spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
  140. TEST_P(OpNameTest, AnyString) {
  141. const std::string input =
  142. std::string("OpName %target \"") + GetParam() + "\"";
  143. EXPECT_THAT(CompiledInstructions(input),
  144. Eq(MakeInstruction(SpvOpName, {1}, MakeVector(GetParam()))));
  145. }
  146. // UTF-8, quoting, escaping, etc. are covered in the StringLiterals tests in
  147. // BinaryToText.Literal.cpp.
  148. INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpNameTest,
  149. ::testing::Values("", "foo bar this and that"));
  150. using OpMemberNameTest =
  151. spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
  152. TEST_P(OpMemberNameTest, AnyString) {
  153. // TODO(dneto): utf-8, quoting, escaping
  154. const std::string input =
  155. std::string("OpMemberName %type 42 \"") + GetParam() + "\"";
  156. EXPECT_THAT(
  157. CompiledInstructions(input),
  158. Eq(MakeInstruction(SpvOpMemberName, {1, 42}, MakeVector(GetParam()))));
  159. }
  160. // TODO(dneto): utf-8, quoting, escaping
  161. INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpMemberNameTest,
  162. ::testing::ValuesIn(std::vector<const char*>{
  163. "", "foo bar this and that"}));
  164. // TODO(dneto): Parse failures?
  165. using OpModuleProcessedTest =
  166. spvtest::TextToBinaryTestBase<::testing::TestWithParam<const char*>>;
  167. TEST_P(OpModuleProcessedTest, AnyString) {
  168. const std::string input =
  169. std::string("OpModuleProcessed \"") + GetParam() + "\"";
  170. EXPECT_THAT(
  171. CompiledInstructions(input, SPV_ENV_UNIVERSAL_1_1),
  172. Eq(MakeInstruction(SpvOpModuleProcessed, MakeVector(GetParam()))));
  173. }
  174. INSTANTIATE_TEST_SUITE_P(TextToBinaryTestDebug, OpModuleProcessedTest,
  175. ::testing::Values("", "foo bar this and that"));
  176. } // namespace
  177. } // namespace spvtools