1 // Copyright (c) 2015-2016 The Khronos Group Inc.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // Assembler tests for instructions in the "Type-Declaration" section of the
18 #include "unit_spirv.h"
20 #include "test_fixture.h"
21 #include "gmock/gmock.h"
25 using spvtest::EnumCase;
26 using spvtest::MakeInstruction;
29 // Test Dim enums via OpTypeImage
32 spvtest::TextToBinaryTestBase<::testing::TestWithParam<EnumCase<SpvDim>>>;
34 TEST_P(DimTest, AnyDim) {
35 const std::string input =
36 "%1 = OpTypeImage %2 " + GetParam().name() + " 2 3 0 4 Rgba8\n";
38 CompiledInstructions(input),
39 Eq(MakeInstruction(SpvOpTypeImage, {1, 2, GetParam().value(), 2, 3, 0, 4,
40 SpvImageFormatRgba8})));
42 // Check the disassembler as well.
43 EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
47 #define CASE(NAME) {SpvDim##NAME, #NAME}
48 INSTANTIATE_TEST_CASE_P(
49 TextToBinaryDim, DimTest,
50 ::testing::ValuesIn(std::vector<EnumCase<SpvDim>>{
62 TEST_F(DimTest, WrongDim) {
63 EXPECT_THAT(CompileFailure("%i = OpTypeImage %t xxyyzz 1 2 3 4 R8"),
64 Eq("Invalid dimensionality 'xxyyzz'."));
67 // Test ImageFormat enums via OpTypeImage
69 using ImageFormatTest = spvtest::TextToBinaryTestBase<
70 ::testing::TestWithParam<EnumCase<SpvImageFormat>>>;
72 TEST_P(ImageFormatTest, AnyImageFormatAndNoAccessQualifier) {
73 const std::string input =
74 "%1 = OpTypeImage %2 1D 2 3 0 4 " + GetParam().name() + "\n";
75 EXPECT_THAT(CompiledInstructions(input),
76 Eq(MakeInstruction(SpvOpTypeImage, {1, 2, SpvDim1D, 2, 3, 0, 4,
77 GetParam().value()})));
78 // Check the disassembler as well.
79 EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
83 #define CASE(NAME) {SpvImageFormat##NAME, #NAME}
84 INSTANTIATE_TEST_CASE_P(
85 TextToBinaryImageFormat, ImageFormatTest,
86 ::testing::ValuesIn(std::vector<EnumCase<SpvImageFormat>>{
131 TEST_F(ImageFormatTest, WrongFormat) {
132 EXPECT_THAT(CompileFailure("%r = OpTypeImage %t 1D 2 3 0 4 xxyyzz"),
133 Eq("Invalid image format 'xxyyzz'."));
136 // Test AccessQualifier enums via OpTypeImage.
137 using ImageAccessQualifierTest = spvtest::TextToBinaryTestBase<
138 ::testing::TestWithParam<EnumCase<SpvAccessQualifier>>>;
140 TEST_P(ImageAccessQualifierTest, AnyAccessQualifier) {
141 const std::string input =
142 "%1 = OpTypeImage %2 1D 2 3 0 4 Rgba8 " + GetParam().name() + "\n";
143 EXPECT_THAT(CompiledInstructions(input),
144 Eq(MakeInstruction(SpvOpTypeImage,
145 {1, 2, SpvDim1D, 2, 3, 0, 4,
146 SpvImageFormatRgba8, GetParam().value()})));
147 // Check the disassembler as well.
148 EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
152 #define CASE(NAME) {SpvAccessQualifier##NAME, #NAME}
153 INSTANTIATE_TEST_CASE_P(
154 AccessQualifier, ImageAccessQualifierTest,
155 ::testing::ValuesIn(std::vector<EnumCase<SpvAccessQualifier>>{
163 // Test AccessQualifier enums via OpTypePipe.
165 using OpTypePipeTest = spvtest::TextToBinaryTestBase<
166 ::testing::TestWithParam<EnumCase<SpvAccessQualifier>>>;
168 TEST_P(OpTypePipeTest, AnyAccessQualifier) {
169 const std::string input = "%1 = OpTypePipe " + GetParam().name() + "\n";
170 EXPECT_THAT(CompiledInstructions(input),
171 Eq(MakeInstruction(SpvOpTypePipe, {1, GetParam().value()})));
172 // Check the disassembler as well.
173 EXPECT_THAT(EncodeAndDecodeSuccessfully(input), Eq(input));
177 #define CASE(NAME) {SpvAccessQualifier##NAME, #NAME}
178 INSTANTIATE_TEST_CASE_P(
179 TextToBinaryTypePipe, OpTypePipeTest,
180 ::testing::ValuesIn(std::vector<EnumCase<SpvAccessQualifier>>{
188 TEST_F(OpTypePipeTest, WrongAccessQualifier) {
189 EXPECT_THAT(CompileFailure("%1 = OpTypePipe xxyyzz"),
190 Eq("Invalid access qualifier 'xxyyzz'."));
193 using OpTypeForwardPointerTest = spvtest::TextToBinaryTest;
195 #define CASE(storage_class) \
198 CompiledInstructions("OpTypeForwardPointer %pt " #storage_class), \
199 Eq(MakeInstruction(SpvOpTypeForwardPointer, \
200 {1, SpvStorageClass##storage_class}))); \
203 TEST_F(OpTypeForwardPointerTest, ValidStorageClass) {
204 CASE(UniformConstant);
209 CASE(CrossWorkgroup);
221 TEST_F(OpTypeForwardPointerTest, MissingType) {
222 EXPECT_THAT(CompileFailure("OpTypeForwardPointer"),
223 Eq("Expected operand, found end of stream."));
226 TEST_F(OpTypeForwardPointerTest, MissingClass) {
227 EXPECT_THAT(CompileFailure("OpTypeForwardPointer %pt"),
228 Eq("Expected operand, found end of stream."));
231 TEST_F(OpTypeForwardPointerTest, WrongClass) {
232 EXPECT_THAT(CompileFailure("OpTypeForwardPointer %pt xxyyzz"),
233 Eq("Invalid storage class 'xxyyzz'."));
236 using OpSizeOfTest = spvtest::TextToBinaryTest;
238 TEST_F(OpSizeOfTest, OpcodeUnrecognizedInV10) {
239 EXPECT_THAT(CompileFailure("%1 = OpSizeOf %2 %3", SPV_ENV_UNIVERSAL_1_0),
240 Eq("Invalid Opcode name 'OpSizeOf'"));
243 TEST_F(OpSizeOfTest, ArgumentCount) {
245 CompileFailure("OpSizeOf", SPV_ENV_UNIVERSAL_1_1),
246 Eq("Expected <result-id> at the beginning of an instruction, found "
248 EXPECT_THAT(CompileFailure("%res = OpSizeOf OpNop", SPV_ENV_UNIVERSAL_1_1),
249 Eq("Expected operand, found next instruction instead."));
251 CompiledInstructions("%1 = OpSizeOf %2 %3", SPV_ENV_UNIVERSAL_1_1),
252 Eq(MakeInstruction(SpvOpSizeOf, {1, 2, 3})));
254 CompileFailure("%1 = OpSizeOf %2 %3 44 55 ", SPV_ENV_UNIVERSAL_1_1),
255 Eq("Expected <opcode> or <result-id> at the beginning of an instruction, "
259 TEST_F(OpSizeOfTest, ArgumentTypes) {
260 EXPECT_THAT(CompileFailure("%1 = OpSizeOf 2 %3", SPV_ENV_UNIVERSAL_1_1),
261 Eq("Expected id to start with %."));
262 EXPECT_THAT(CompileFailure("%1 = OpSizeOf %2 \"abc\"", SPV_ENV_UNIVERSAL_1_1),
263 Eq("Expected id to start with %."));
266 // TODO(dneto): OpTypeVoid
267 // TODO(dneto): OpTypeBool
268 // TODO(dneto): OpTypeInt
269 // TODO(dneto): OpTypeFloat
270 // TODO(dneto): OpTypeVector
271 // TODO(dneto): OpTypeMatrix
272 // TODO(dneto): OpTypeImage
273 // TODO(dneto): OpTypeSampler
274 // TODO(dneto): OpTypeSampledImage
275 // TODO(dneto): OpTypeArray
276 // TODO(dneto): OpTypeRuntimeArray
277 // TODO(dneto): OpTypeStruct
278 // TODO(dneto): OpTypeOpaque
279 // TODO(dneto): OpTypePointer
280 // TODO(dneto): OpTypeFunction
281 // TODO(dneto): OpTypeEvent
282 // TODO(dneto): OpTypeDeviceEvent
283 // TODO(dneto): OpTypeReserveId
284 // TODO(dneto): OpTypeQueue
286 } // anonymous namespace