1 //===- ImportIDTableTests.cpp - Unit tests for ImportIDTable --------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/IPO/FunctionImport.h"
10 #include "gmock/gmock.h"
11 #include "gtest/gtest.h"
13 #include <type_traits>
17 TEST(ImportIDTableTests
, Basic
) {
18 FunctionImporter::ImportIDTable Table
;
20 auto [Def
, Decl
] = Table
.createImportIDs("mod", 123U);
21 auto [Def2
, Decl2
] = Table
.createImportIDs("stuff", 456U);
23 // Def and Decl must be of the same unsigned integer type.
25 std::is_unsigned_v
<FunctionImporter::ImportIDTable::ImportIDTy
>);
26 static_assert(std::is_same_v
<FunctionImporter::ImportIDTable::ImportIDTy
,
28 static_assert(std::is_same_v
<FunctionImporter::ImportIDTable::ImportIDTy
,
31 // Check that all IDs are unique.
32 std::set
<FunctionImporter::ImportIDTable::ImportIDTy
> IDs
= {Def
, Decl
, Def2
,
34 EXPECT_THAT(IDs
, ::testing::SizeIs(4));
36 // Verify what Def maps to.
37 auto DefTuple
= Table
.lookup(Def
);
38 EXPECT_EQ(std::get
<0>(DefTuple
), StringRef("mod"));
39 EXPECT_EQ(std::get
<1>(DefTuple
), 123U);
40 EXPECT_EQ(std::get
<2>(DefTuple
), GlobalValueSummary::Definition
);
42 // Verify what Decl maps to.
43 auto DeclTuple
= Table
.lookup(Decl
);
44 EXPECT_EQ(std::get
<0>(DeclTuple
), StringRef("mod"));
45 EXPECT_EQ(std::get
<1>(DeclTuple
), 123U);
46 EXPECT_EQ(std::get
<2>(DeclTuple
), GlobalValueSummary::Declaration
);
48 // Verify what Def2 maps to.
49 auto Def2Tuple
= Table
.lookup(Def2
);
50 EXPECT_EQ(std::get
<0>(Def2Tuple
), StringRef("stuff"));
51 EXPECT_EQ(std::get
<1>(Def2Tuple
), 456U);
52 EXPECT_EQ(std::get
<2>(Def2Tuple
), GlobalValueSummary::Definition
);
54 // Verify what Decl2 maps to.
55 auto Decl2Tuple
= Table
.lookup(Decl2
);
56 EXPECT_EQ(std::get
<0>(Decl2Tuple
), StringRef("stuff"));
57 EXPECT_EQ(std::get
<1>(Decl2Tuple
), 456U);
58 EXPECT_EQ(std::get
<2>(Decl2Tuple
), GlobalValueSummary::Declaration
);
61 TEST(ImportIDTableTests
, Duplicates
) {
62 FunctionImporter::ImportIDTable Table
;
64 auto [Def1
, Decl1
] = Table
.createImportIDs("mod", 123U);
65 auto [Def2
, Decl2
] = Table
.createImportIDs("mod", 123U);
67 // Verify we get the same IDs back.
68 EXPECT_EQ(Def1
, Def2
);
69 EXPECT_EQ(Decl1
, Decl2
);
72 TEST(ImportIDTableTests
, Present
) {
73 FunctionImporter::ImportIDTable Table
;
75 auto [Def
, Decl
] = Table
.createImportIDs("mod", 123U);
76 auto Result
= Table
.getImportIDs("mod", 123U);
78 // Verify that we get the same IDs back.
79 ASSERT_NE(Result
, std::nullopt
);
80 EXPECT_EQ(Result
->first
, Def
);
81 EXPECT_EQ(Result
->second
, Decl
);
84 TEST(ImportIDTableTests
, Missing
) {
85 FunctionImporter::ImportIDTable Table
;
87 auto Result
= Table
.getImportIDs("mod", 123U);
89 // Verify that we get std::nullopt for a non-existent pair.
90 EXPECT_EQ(Result
, std::nullopt
);