op_unknown_test.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2025 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. #include <cassert>
  15. #include <string>
  16. #include <vector>
  17. #include "gmock/gmock.h"
  18. #include "source/util/bitutils.h"
  19. #include "test/test_fixture.h"
  20. namespace spvtools {
  21. namespace utils {
  22. namespace {
  23. using spvtest::Concatenate;
  24. using spvtest::MakeInstruction;
  25. using spvtest::ScopedContext;
  26. using spvtest::TextToBinaryTest;
  27. using ::testing::ElementsAre;
  28. using ::testing::Eq;
  29. using ::testing::HasSubstr;
  30. using ::testing::StrEq;
  31. using OpUnknownTest = TextToBinaryTest;
  32. TEST_F(OpUnknownTest, OpUnknown) {
  33. SetText("OpUnknown(255, 1)");
  34. ASSERT_EQ(SPV_SUCCESS, spvTextToBinary(ScopedContext().context, text.str,
  35. text.length, &binary, &diagnostic));
  36. EXPECT_EQ(0x000100FFu, binary->code[5]);
  37. if (diagnostic) {
  38. spvDiagnosticPrint(diagnostic);
  39. }
  40. }
  41. TEST_F(OpUnknownTest, HandlesOperands) {
  42. EXPECT_THAT(CompiledInstructions("OpUnknown(24, 4) %a %b %123"),
  43. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 2, 3})));
  44. EXPECT_THAT(CompiledInstructions("OpUnknown(24, 4) !1 %b %123"),
  45. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 1, 2})));
  46. }
  47. TEST_F(OpUnknownTest, HandlesWhitespace) {
  48. EXPECT_THAT(CompiledInstructions("OpUnknown ( 24 , 4 ) %a %b %123"),
  49. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 2, 3})));
  50. EXPECT_THAT(CompiledInstructions("OpUnknown(24,4) %a %b %123"),
  51. Eq(MakeInstruction(spv::Op::OpTypeMatrix, {1, 2, 3})));
  52. }
  53. TEST_F(OpUnknownTest, MultipleInstructions) {
  54. EXPECT_THAT(
  55. CompiledInstructions(
  56. "%a = OpTypeFunction %b\nOpUnknown(21, 5) %c %d 32 1\nOpNop"),
  57. Eq(Concatenate({MakeInstruction(spv::Op::OpTypeFunction, {1, 2}),
  58. MakeInstruction(spv::Op::OpTypeInt, {3, 4, 32, 1}),
  59. MakeInstruction(spv::Op::OpNop, {})})));
  60. }
  61. TEST_F(OpUnknownTest, OpUnknownInAssignment) {
  62. EXPECT_EQ(
  63. "OpUnknown not allowed in assignment. Use an explicit result id operand "
  64. "instead.",
  65. CompileFailure("%2 = OpUnknown(22, 3) 32"));
  66. EXPECT_EQ("OpUnknown not allowed before =.",
  67. CompileFailure("OpUnknown(22, 3) = OpTypeFloat 32"));
  68. }
  69. TEST_F(OpUnknownTest, ParsingErrors) {
  70. EXPECT_EQ("Expected '(', found end of stream.", CompileFailure("OpUnknown"));
  71. EXPECT_EQ("'(' expected after OpUnknown but found 'a'.",
  72. CompileFailure("OpUnknown abc"));
  73. EXPECT_EQ("Expected opcode enumerant, found end of stream.",
  74. CompileFailure("OpUnknown("));
  75. EXPECT_EQ("Invalid opcode enumerant: \"abc\".",
  76. CompileFailure("OpUnknown(abc"));
  77. // Opcode enumerant must fit in 16 bits.
  78. EXPECT_EQ("Invalid opcode enumerant: \"70000\".",
  79. CompileFailure("OpUnknown(70000"));
  80. EXPECT_EQ("Expected ',', found end of stream.",
  81. CompileFailure("OpUnknown(22"));
  82. EXPECT_EQ("',' expected after opcode enumerant but found 'a'.",
  83. CompileFailure("OpUnknown(22 abc"));
  84. EXPECT_EQ("Expected number of words, found end of stream.",
  85. CompileFailure("OpUnknown(22,"));
  86. EXPECT_EQ("Invalid number of words: \"abc\".",
  87. CompileFailure("OpUnknown(22, abc"));
  88. // Number of words must fit in 16 bits.
  89. EXPECT_EQ("Invalid number of words: \"70000\".",
  90. CompileFailure("OpUnknown(22, 70000"));
  91. EXPECT_EQ(
  92. "Number of words (which includes the opcode) must be greater than zero.",
  93. CompileFailure("OpUnknown(22, 0"));
  94. EXPECT_EQ("Expected ')', found end of stream.",
  95. CompileFailure("OpUnknown(22, 3"));
  96. EXPECT_EQ("')' expected after number of words but found 'a'.",
  97. CompileFailure("OpUnknown(22, 3 abc"));
  98. EXPECT_EQ(
  99. "Unexpected start of new instruction: \"OpNop\". Expected 2 more "
  100. "operands",
  101. CompileFailure("OpUnknown(22, 3) OpNop"));
  102. EXPECT_EQ("Expected 2 more operands, found end of stream.",
  103. CompileFailure("OpUnknown(22, 3)"));
  104. EXPECT_EQ(CompileFailure("OpUnknown(21, 4) %c %d 32 1"),
  105. "Expected <opcode> or <result-id> at the beginning of an "
  106. "instruction, found '1'.");
  107. }
  108. } // namespace
  109. } // namespace utils
  110. } // namespace spvtools