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