| 1 |
//===- ImportIDTableTests.cpp - Unit tests for ImportIDTable --------------===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
#include "llvm/Transforms/IPO/FunctionImport.h" |
| 10 |
#include "gmock/gmock.h" |
| 11 |
#include "gtest/gtest.h" |
| 12 |
#include <set> |
| 13 |
#include <type_traits> |
| 14 |
|
| 15 |
using namespace llvm; |
| 16 |
|
| 17 |
TEST(ImportIDTableTests, Basic) { |
| 18 |
FunctionImporter::ImportIDTable Table; |
| 19 |
|
| 20 |
auto [Def, Decl] = Table.createImportIDs("mod", 123U); |
| 21 |
auto [Def2, Decl2] = Table.createImportIDs("stuff", 456U); |
| 22 |
|
| 23 |
// Def and Decl must be of the same unsigned integer type. |
| 24 |
static_assert( |
| 25 |
std::is_unsigned_v<FunctionImporter::ImportIDTable::ImportIDTy>); |
| 26 |
static_assert(std::is_same_v<FunctionImporter::ImportIDTable::ImportIDTy, |
| 27 |
decltype(Def)>); |
| 28 |
static_assert(std::is_same_v<FunctionImporter::ImportIDTable::ImportIDTy, |
| 29 |
decltype(Decl)>); |
| 30 |
|
| 31 |
// Check that all IDs are unique. |
| 32 |
std::set<FunctionImporter::ImportIDTable::ImportIDTy> IDs = {Def, Decl, Def2, |
| 33 |
Decl2}; |
| 34 |
EXPECT_THAT(IDs, ::testing::SizeIs(4)); |
| 35 |
|
| 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); |
| 41 |
|
| 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); |
| 47 |
|
| 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); |
| 53 |
|
| 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); |
| 59 |
} |
| 60 |
|
| 61 |
TEST(ImportIDTableTests, Duplicates) { |
| 62 |
FunctionImporter::ImportIDTable Table; |
| 63 |
|
| 64 |
auto [Def1, Decl1] = Table.createImportIDs("mod", 123U); |
| 65 |
auto [Def2, Decl2] = Table.createImportIDs("mod", 123U); |
| 66 |
|
| 67 |
// Verify we get the same IDs back. |
| 68 |
EXPECT_EQ(Def1, Def2); |
| 69 |
EXPECT_EQ(Decl1, Decl2); |
| 70 |
} |
| 71 |
|
| 72 |
TEST(ImportIDTableTests, Present) { |
| 73 |
FunctionImporter::ImportIDTable Table; |
| 74 |
|
| 75 |
auto [Def, Decl] = Table.createImportIDs("mod", 123U); |
| 76 |
auto Result = Table.getImportIDs("mod", 123U); |
| 77 |
|
| 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); |
| 82 |
} |
| 83 |
|
| 84 |
TEST(ImportIDTableTests, Missing) { |
| 85 |
FunctionImporter::ImportIDTable Table; |
| 86 |
|
| 87 |
auto Result = Table.getImportIDs("mod", 123U); |
| 88 |
|
| 89 |
// Verify that we get std::nullopt for a non-existent pair. |
| 90 |
EXPECT_EQ(Result, std::nullopt); |
| 91 |
} |