xref: /llvm-project/llvm/unittests/ProfileData/BPFunctionNodeTest.cpp (revision 73eb9b33147ba5157cbf5d8276ee718629dfbbda)
1 //===- BPFunctionNodeTest.cpp - BPFunctionNode tests ----------------------===//
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/ProfileData/InstrProf.h"
10 #include "llvm/Support/BalancedPartitioning.h"
11 #include "gmock/gmock.h"
12 #include "gtest/gtest.h"
13 
14 using testing::Field;
15 using testing::UnorderedElementsAre;
16 using testing::UnorderedElementsAreArray;
17 
18 namespace llvm {
19 
PrintTo(const BPFunctionNode & Node,std::ostream * OS)20 void PrintTo(const BPFunctionNode &Node, std::ostream *OS) {
21   raw_os_ostream ROS(*OS);
22   Node.dump(ROS);
23 }
24 
TEST(BPFunctionNodeTest,Basic)25 TEST(BPFunctionNodeTest, Basic) {
26   auto NodeIs = [](BPFunctionNode::IDT Id,
27                    ArrayRef<BPFunctionNode::UtilityNodeT> UNs) {
28     return AllOf(Field("Id", &BPFunctionNode::Id, Id),
29                  Field("UtilityNodes", &BPFunctionNode::UtilityNodes,
30                        UnorderedElementsAreArray(UNs)));
31   };
32 
33   std::vector<BPFunctionNode> Nodes;
34   TemporalProfTraceTy::createBPFunctionNodes(
35       {TemporalProfTraceTy({0, 1, 2, 3})}, Nodes, /*RemoveOutlierUNs=*/false);
36   // Utility nodes that are too infrequent or too prevalent are filtered out.
37   EXPECT_THAT(Nodes,
38               UnorderedElementsAre(NodeIs(0, {0, 1, 2}), NodeIs(1, {1, 2}),
39                                    NodeIs(2, {2}), NodeIs(3, {2})));
40 
41   Nodes.clear();
42   TemporalProfTraceTy::createBPFunctionNodes(
43       {TemporalProfTraceTy({0, 1, 2, 3, 4}), TemporalProfTraceTy({4, 2})},
44       Nodes, /*RemoveOutlierUNs=*/false);
45 
46   EXPECT_THAT(Nodes,
47               UnorderedElementsAre(NodeIs(0, {0, 1, 2, 3}),
48                                    NodeIs(1, {1, 2, 3}), NodeIs(2, {2, 3, 5}),
49                                    NodeIs(3, {2, 3}), NodeIs(4, {3, 4, 5})));
50 
51   Nodes.clear();
52   TemporalProfTraceTy::createBPFunctionNodes(
53       {TemporalProfTraceTy({0, 1, 2, 3, 4}), TemporalProfTraceTy({4, 2})},
54       Nodes, /*RemoveOutlierUNs=*/true);
55 
56   EXPECT_THAT(Nodes, UnorderedElementsAre(NodeIs(0, {1}), NodeIs(1, {1}),
57                                           NodeIs(2, {5}), NodeIs(3, {}),
58                                           NodeIs(4, {5})));
59 }
60 
61 } // end namespace llvm
62