Debian Codegraph @

test/operand_test.cpp

raw
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
15 #include <vector>
16
17 #include "test/unit_spirv.h"
18
19 namespace spvtools {
20 namespace {
21
22 using ::testing::ValuesIn;
23
24 TEST(OperandString, AllAreDefinedExceptVariable) {
25 // None has no string, so don't test it.
26 EXPECT_EQ(0u, SPV_OPERAND_TYPE_NONE);
27 // Start testing at enum with value 1, skipping None.
28 for (int i = 1; i < int(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES); i++) {
29 const auto type = static_cast<spv_operand_type_t>(i);
30 if (spvOperandIsVariable(type)) {
31 EXPECT_STREQ("unknown", spvOperandTypeStr(type))
32 << " variable type " << i << " has a name '"
33 << spvOperandTypeStr(type) << "'when it should not";
34 } else {
35 EXPECT_STRNE("unknown", spvOperandTypeStr(type))
36 << " operand type " << i << " has no name when it should";
37 }
38 }
39 }
40
41 TEST(OperandIsConcreteMask, Sample) {
42 // Check a few operand types preceding the concrete mask types.
43 EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_NONE));
44 EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_ID));
45 EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_LITERAL_INTEGER));
46 EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_CAPABILITY));
47
48 // Check all the concrete mask operand types.
49 EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_IMAGE));
50 EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_FP_FAST_MATH_MODE));
51 EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_SELECTION_CONTROL));
52 EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_LOOP_CONTROL));
53 EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_FUNCTION_CONTROL));
54 EXPECT_TRUE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_MEMORY_ACCESS));
55
56 // Check a few operand types after the concrete mask types, including the
57 // optional forms for Image and MemoryAccess.
58 EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_OPTIONAL_ID));
59 EXPECT_FALSE(spvOperandIsConcreteMask(SPV_OPERAND_TYPE_OPTIONAL_IMAGE));
60 EXPECT_FALSE(
61 spvOperandIsConcreteMask(SPV_OPERAND_TYPE_OPTIONAL_MEMORY_ACCESS));
62 }
63
64 TEST(OperandType, NoneTypeClassification) {
65 EXPECT_FALSE(spvOperandIsConcrete(SPV_OPERAND_TYPE_NONE));
66 EXPECT_FALSE(spvOperandIsOptional(SPV_OPERAND_TYPE_NONE));
67 EXPECT_FALSE(spvOperandIsVariable(SPV_OPERAND_TYPE_NONE));
68 }
69
70 TEST(OperandType, EndSentinelTypeClassification) {
71 EXPECT_FALSE(spvOperandIsConcrete(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES));
72 EXPECT_FALSE(spvOperandIsOptional(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES));
73 EXPECT_FALSE(spvOperandIsVariable(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES));
74 }
75
76 TEST(OperandType, WidthForcingTypeClassification) {
77 EXPECT_FALSE(spvOperandIsConcrete(SPV_FORCE_32BIT_spv_operand_type_t));
78 EXPECT_FALSE(spvOperandIsOptional(SPV_FORCE_32BIT_spv_operand_type_t));
79 EXPECT_FALSE(spvOperandIsVariable(SPV_FORCE_32BIT_spv_operand_type_t));
80 }
81
82 TEST(OperandType, EachTypeIsEitherConcreteOrOptionalNotBoth) {
83 EXPECT_EQ(0u, SPV_OPERAND_TYPE_NONE);
84 // Start testing at enum with value 1, skipping None.
85 for (int i = 1; i < int(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES); i++) {
86 const auto type = static_cast<spv_operand_type_t>(i);
87 EXPECT_NE(spvOperandIsConcrete(type), spvOperandIsOptional(type))
88 << " operand type " << int(type) << " concrete? "
89 << int(spvOperandIsConcrete(type)) << " optional? "
90 << int(spvOperandIsOptional(type));
91 }
92 }
93
94 TEST(OperandType, EachVariableTypeIsOptional) {
95 EXPECT_EQ(0u, SPV_OPERAND_TYPE_NONE);
96 // Start testing at enum with value 1, skipping None.
97 for (int i = 1; i < int(SPV_OPERAND_TYPE_NUM_OPERAND_TYPES); i++) {
98 const auto type = static_cast<spv_operand_type_t>(i);
99 if (spvOperandIsVariable(type)) {
100 EXPECT_TRUE(spvOperandIsOptional(type)) << " variable type " << int(type);
101 }
102 }
103 }
104
105 } // namespace
106 } // namespace spvtools