Debian Codegraph @

test/ext_inst_lookup_test.cpp

raw
1 // Copyright 2025 Google LLC
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 <array>
16 #include <iostream>
17
18 #include "gmock/gmock.h"
19 #include "source/spirv_target_env.h"
20 #include "source/table2.h"
21 #include "test/unit_spirv.h"
22
23 using ::testing::ContainerEq;
24 using ::testing::ValuesIn;
25
26 namespace spvtools {
27 namespace {
28
29 struct ExtInstLookupCase {
30 spv_ext_inst_type_t type;
31 std::string name;
32 uint32_t value;
33 bool expect_pass = true;
34 };
35
36 std::ostream& operator<<(std::ostream& os, const ExtInstLookupCase& eilc) {
37 os << "EILC(" << static_cast<int>(eilc.type) << ", '" << eilc.name << "', "
38 << eilc.value << ", expect pass? " << eilc.expect_pass << ")";
39 return os;
40 }
41
42 using ExtInstLookupTest = ::testing::TestWithParam<ExtInstLookupCase>;
43
44 TEST_P(ExtInstLookupTest, ExtInstLookup_ByName) {
45 const ExtInstDesc* desc = nullptr;
46 auto status = LookupExtInst(GetParam().type, GetParam().name.data(), &desc);
47 if (GetParam().expect_pass) {
48 EXPECT_EQ(status, SPV_SUCCESS);
49 ASSERT_NE(desc, nullptr);
50 EXPECT_EQ(static_cast<uint32_t>(desc->value), GetParam().value);
51 } else {
52 EXPECT_NE(status, SPV_SUCCESS);
53 EXPECT_EQ(desc, nullptr);
54 }
55 }
56
57 TEST_P(ExtInstLookupTest, ExtInstLookup_ByValue_Success) {
58 const ExtInstDesc* desc = nullptr;
59 if (GetParam().expect_pass) {
60 auto status = LookupExtInst(GetParam().type, GetParam().value, &desc);
61 EXPECT_EQ(status, SPV_SUCCESS);
62 ASSERT_NE(desc, nullptr);
63 EXPECT_EQ(desc->value, GetParam().value);
64 EXPECT_EQ(std::string(desc->name().data()), GetParam().name);
65 }
66 }
67
68 INSTANTIATE_TEST_SUITE_P(Samples, ExtInstLookupTest,
69 ValuesIn(std::vector<ExtInstLookupCase>{
70 {SPV_EXT_INST_TYPE_GLSL_STD_450, "FMix", 46},
71 {SPV_EXT_INST_TYPE_OPENCL_STD, "s_mul24", 169},
72 {SPV_EXT_INST_TYPE_OPENCL_STD, "mix", 99},
73 }));
74
75 TEST(ExtInstLookupSingleTest, ExtInstLookup_Value_Fails) {
76 // This list may need adjusting over time.
77 std::array<uint32_t, 3> bad_values = {{99999, 37737, 110101}};
78 for (auto bad_value : bad_values) {
79 const ExtInstDesc* desc = nullptr;
80 auto status = LookupExtInst(SPV_EXT_INST_TYPE_OPENCL_STD, bad_value, &desc);
81 EXPECT_NE(status, SPV_SUCCESS);
82 ASSERT_EQ(desc, nullptr);
83 }
84 }
85
86 } // namespace
87 } // namespace spvtools