xref: /llvm-project/llvm/unittests/CGData/OutlinedHashTreeTest.cpp (revision 9bb555688caf6ae4ba89fee5baa3dc29fec6a9b1)
1*9bb55568SKyungwoo Lee //===- OutlinedHashTreeTest.cpp -------------------------------------------===//
2*9bb55568SKyungwoo Lee //
3*9bb55568SKyungwoo Lee // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*9bb55568SKyungwoo Lee // See https://llvm.org/LICENSE.txt for license information.
5*9bb55568SKyungwoo Lee // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*9bb55568SKyungwoo Lee //
7*9bb55568SKyungwoo Lee //===----------------------------------------------------------------------===//
8*9bb55568SKyungwoo Lee 
9*9bb55568SKyungwoo Lee #include "llvm/CGData/OutlinedHashTree.h"
10*9bb55568SKyungwoo Lee #include "gmock/gmock.h"
11*9bb55568SKyungwoo Lee #include "gtest/gtest.h"
12*9bb55568SKyungwoo Lee 
13*9bb55568SKyungwoo Lee using namespace llvm;
14*9bb55568SKyungwoo Lee 
15*9bb55568SKyungwoo Lee namespace {
16*9bb55568SKyungwoo Lee 
17*9bb55568SKyungwoo Lee TEST(OutlinedHashTreeTest, Empty) {
18*9bb55568SKyungwoo Lee   OutlinedHashTree HashTree;
19*9bb55568SKyungwoo Lee   EXPECT_TRUE(HashTree.empty());
20*9bb55568SKyungwoo Lee   // The header node is always present.
21*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree.size(), 1u);
22*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree.depth(), 0u);
23*9bb55568SKyungwoo Lee }
24*9bb55568SKyungwoo Lee 
25*9bb55568SKyungwoo Lee TEST(OutlinedHashTreeTest, Insert) {
26*9bb55568SKyungwoo Lee   OutlinedHashTree HashTree;
27*9bb55568SKyungwoo Lee   HashTree.insert({{1, 2, 3}, 1});
28*9bb55568SKyungwoo Lee   // The node count is 4 (including the root node).
29*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree.size(), 4u);
30*9bb55568SKyungwoo Lee   // The terminal count is 1.
31*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree.size(/*GetTerminalCountOnly=*/true), 1u);
32*9bb55568SKyungwoo Lee   // The depth is 3.
33*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree.depth(), 3u);
34*9bb55568SKyungwoo Lee 
35*9bb55568SKyungwoo Lee   HashTree.clear();
36*9bb55568SKyungwoo Lee   EXPECT_TRUE(HashTree.empty());
37*9bb55568SKyungwoo Lee 
38*9bb55568SKyungwoo Lee   HashTree.insert({{1, 2, 3}, 1});
39*9bb55568SKyungwoo Lee   HashTree.insert({{1, 2, 4}, 2});
40*9bb55568SKyungwoo Lee   // The nodes of 1 and 2 are shared with the same prefix.
41*9bb55568SKyungwoo Lee   // The nodes are root, 1, 2, 3 and 4, whose counts are 5.
42*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree.size(), 5u);
43*9bb55568SKyungwoo Lee }
44*9bb55568SKyungwoo Lee 
45*9bb55568SKyungwoo Lee TEST(OutlinedHashTreeTest, Find) {
46*9bb55568SKyungwoo Lee   OutlinedHashTree HashTree;
47*9bb55568SKyungwoo Lee   HashTree.insert({{1, 2, 3}, 1});
48*9bb55568SKyungwoo Lee   HashTree.insert({{1, 2, 3}, 2});
49*9bb55568SKyungwoo Lee 
50*9bb55568SKyungwoo Lee   // The node count does not change as the same sequences are added.
51*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree.size(), 4u);
52*9bb55568SKyungwoo Lee   // The terminal counts are accumulated from two same sequences.
53*9bb55568SKyungwoo Lee   EXPECT_TRUE(HashTree.find({1, 2, 3}));
54*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree.find({1, 2, 3}).value(), 3u);
55*9bb55568SKyungwoo Lee   EXPECT_FALSE(HashTree.find({1, 2}));
56*9bb55568SKyungwoo Lee }
57*9bb55568SKyungwoo Lee 
58*9bb55568SKyungwoo Lee TEST(OutlinedHashTreeTest, Merge) {
59*9bb55568SKyungwoo Lee   // Build HashTree1 inserting 2 sequences.
60*9bb55568SKyungwoo Lee   OutlinedHashTree HashTree1;
61*9bb55568SKyungwoo Lee 
62*9bb55568SKyungwoo Lee   HashTree1.insert({{1, 2}, 20});
63*9bb55568SKyungwoo Lee   HashTree1.insert({{1, 4}, 30});
64*9bb55568SKyungwoo Lee 
65*9bb55568SKyungwoo Lee   // Build HashTree2 and HashTree3 for each
66*9bb55568SKyungwoo Lee   OutlinedHashTree HashTree2;
67*9bb55568SKyungwoo Lee   HashTree2.insert({{1, 2}, 20});
68*9bb55568SKyungwoo Lee   OutlinedHashTree HashTree3;
69*9bb55568SKyungwoo Lee   HashTree3.insert({{1, 4}, 30});
70*9bb55568SKyungwoo Lee 
71*9bb55568SKyungwoo Lee   // Merge HashTree3 into HashTree2.
72*9bb55568SKyungwoo Lee   HashTree2.merge(&HashTree3);
73*9bb55568SKyungwoo Lee 
74*9bb55568SKyungwoo Lee   // Compare HashTree1 and HashTree2.
75*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree1.size(), HashTree2.size());
76*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree1.depth(), HashTree2.depth());
77*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree1.find({1, 2}), HashTree2.find({1, 2}));
78*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree1.find({1, 4}), HashTree2.find({1, 4}));
79*9bb55568SKyungwoo Lee   EXPECT_EQ(HashTree1.find({1, 3}), HashTree2.find({1, 3}));
80*9bb55568SKyungwoo Lee }
81*9bb55568SKyungwoo Lee 
82*9bb55568SKyungwoo Lee } // end namespace
83