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