xref: /llvm-project/llvm/unittests/CGData/OutlinedHashTreeRecordTest.cpp (revision 9bb555688caf6ae4ba89fee5baa3dc29fec6a9b1)
1*9bb55568SKyungwoo Lee //===- OutlinedHashTreeRecordTest.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/OutlinedHashTreeRecord.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(OutlinedHashTreeRecordTest, Empty) {
18*9bb55568SKyungwoo Lee   OutlinedHashTreeRecord HashTreeRecord;
19*9bb55568SKyungwoo Lee   ASSERT_TRUE(HashTreeRecord.empty());
20*9bb55568SKyungwoo Lee }
21*9bb55568SKyungwoo Lee 
22*9bb55568SKyungwoo Lee TEST(OutlinedHashTreeRecordTest, Print) {
23*9bb55568SKyungwoo Lee   OutlinedHashTreeRecord HashTreeRecord;
24*9bb55568SKyungwoo Lee   HashTreeRecord.HashTree->insert({{1, 2}, 3});
25*9bb55568SKyungwoo Lee 
26*9bb55568SKyungwoo Lee   const char *ExpectedTreeStr = R"(---
27*9bb55568SKyungwoo Lee 0:
28*9bb55568SKyungwoo Lee   Hash:            0x0
29*9bb55568SKyungwoo Lee   Terminals:       0
30*9bb55568SKyungwoo Lee   SuccessorIds:    [ 1 ]
31*9bb55568SKyungwoo Lee 1:
32*9bb55568SKyungwoo Lee   Hash:            0x1
33*9bb55568SKyungwoo Lee   Terminals:       0
34*9bb55568SKyungwoo Lee   SuccessorIds:    [ 2 ]
35*9bb55568SKyungwoo Lee 2:
36*9bb55568SKyungwoo Lee   Hash:            0x2
37*9bb55568SKyungwoo Lee   Terminals:       3
38*9bb55568SKyungwoo Lee   SuccessorIds:    [  ]
39*9bb55568SKyungwoo Lee ...
40*9bb55568SKyungwoo Lee )";
41*9bb55568SKyungwoo Lee   std::string TreeDump;
42*9bb55568SKyungwoo Lee   raw_string_ostream OS(TreeDump);
43*9bb55568SKyungwoo Lee   HashTreeRecord.print(OS);
44*9bb55568SKyungwoo Lee   EXPECT_EQ(ExpectedTreeStr, TreeDump);
45*9bb55568SKyungwoo Lee }
46*9bb55568SKyungwoo Lee 
47*9bb55568SKyungwoo Lee TEST(OutlinedHashTreeRecordTest, Stable) {
48*9bb55568SKyungwoo Lee   OutlinedHashTreeRecord HashTreeRecord1;
49*9bb55568SKyungwoo Lee   HashTreeRecord1.HashTree->insert({{1, 2}, 4});
50*9bb55568SKyungwoo Lee   HashTreeRecord1.HashTree->insert({{1, 3}, 5});
51*9bb55568SKyungwoo Lee 
52*9bb55568SKyungwoo Lee   OutlinedHashTreeRecord HashTreeRecord2;
53*9bb55568SKyungwoo Lee   HashTreeRecord2.HashTree->insert({{1, 3}, 5});
54*9bb55568SKyungwoo Lee   HashTreeRecord2.HashTree->insert({{1, 2}, 4});
55*9bb55568SKyungwoo Lee 
56*9bb55568SKyungwoo Lee   // Output is stable regardless of insertion order.
57*9bb55568SKyungwoo Lee   std::string TreeDump1;
58*9bb55568SKyungwoo Lee   raw_string_ostream OS1(TreeDump1);
59*9bb55568SKyungwoo Lee   HashTreeRecord1.print(OS1);
60*9bb55568SKyungwoo Lee   std::string TreeDump2;
61*9bb55568SKyungwoo Lee   raw_string_ostream OS2(TreeDump2);
62*9bb55568SKyungwoo Lee   HashTreeRecord2.print(OS2);
63*9bb55568SKyungwoo Lee 
64*9bb55568SKyungwoo Lee   EXPECT_EQ(TreeDump1, TreeDump2);
65*9bb55568SKyungwoo Lee }
66*9bb55568SKyungwoo Lee 
67*9bb55568SKyungwoo Lee TEST(OutlinedHashTreeRecordTest, Serialize) {
68*9bb55568SKyungwoo Lee   OutlinedHashTreeRecord HashTreeRecord1;
69*9bb55568SKyungwoo Lee   HashTreeRecord1.HashTree->insert({{1, 2}, 4});
70*9bb55568SKyungwoo Lee   HashTreeRecord1.HashTree->insert({{1, 3}, 5});
71*9bb55568SKyungwoo Lee 
72*9bb55568SKyungwoo Lee   // Serialize and deserialize the tree.
73*9bb55568SKyungwoo Lee   SmallVector<char> Out;
74*9bb55568SKyungwoo Lee   raw_svector_ostream OS(Out);
75*9bb55568SKyungwoo Lee   HashTreeRecord1.serialize(OS);
76*9bb55568SKyungwoo Lee 
77*9bb55568SKyungwoo Lee   OutlinedHashTreeRecord HashTreeRecord2;
78*9bb55568SKyungwoo Lee   const uint8_t *Data = reinterpret_cast<const uint8_t *>(Out.data());
79*9bb55568SKyungwoo Lee   HashTreeRecord2.deserialize(Data);
80*9bb55568SKyungwoo Lee 
81*9bb55568SKyungwoo Lee   // Two trees should be identical.
82*9bb55568SKyungwoo Lee   std::string TreeDump1;
83*9bb55568SKyungwoo Lee   raw_string_ostream OS1(TreeDump1);
84*9bb55568SKyungwoo Lee   HashTreeRecord1.print(OS1);
85*9bb55568SKyungwoo Lee   std::string TreeDump2;
86*9bb55568SKyungwoo Lee   raw_string_ostream OS2(TreeDump2);
87*9bb55568SKyungwoo Lee   HashTreeRecord2.print(OS2);
88*9bb55568SKyungwoo Lee 
89*9bb55568SKyungwoo Lee   EXPECT_EQ(TreeDump1, TreeDump2);
90*9bb55568SKyungwoo Lee }
91*9bb55568SKyungwoo Lee 
92*9bb55568SKyungwoo Lee TEST(OutlinedHashTreeRecordTest, SerializeYAML) {
93*9bb55568SKyungwoo Lee   OutlinedHashTreeRecord HashTreeRecord1;
94*9bb55568SKyungwoo Lee   HashTreeRecord1.HashTree->insert({{1, 2}, 4});
95*9bb55568SKyungwoo Lee   HashTreeRecord1.HashTree->insert({{1, 3}, 5});
96*9bb55568SKyungwoo Lee 
97*9bb55568SKyungwoo Lee   // Serialize and deserialize the tree in a YAML format.
98*9bb55568SKyungwoo Lee   std::string Out;
99*9bb55568SKyungwoo Lee   raw_string_ostream OS(Out);
100*9bb55568SKyungwoo Lee   yaml::Output YOS(OS);
101*9bb55568SKyungwoo Lee   HashTreeRecord1.serializeYAML(YOS);
102*9bb55568SKyungwoo Lee 
103*9bb55568SKyungwoo Lee   OutlinedHashTreeRecord HashTreeRecord2;
104*9bb55568SKyungwoo Lee   yaml::Input YIS(StringRef(Out.data(), Out.size()));
105*9bb55568SKyungwoo Lee   HashTreeRecord2.deserializeYAML(YIS);
106*9bb55568SKyungwoo Lee 
107*9bb55568SKyungwoo Lee   // Two trees should be identical.
108*9bb55568SKyungwoo Lee   std::string TreeDump1;
109*9bb55568SKyungwoo Lee   raw_string_ostream OS1(TreeDump1);
110*9bb55568SKyungwoo Lee   HashTreeRecord1.print(OS1);
111*9bb55568SKyungwoo Lee   std::string TreeDump2;
112*9bb55568SKyungwoo Lee   raw_string_ostream OS2(TreeDump2);
113*9bb55568SKyungwoo Lee   HashTreeRecord2.print(OS2);
114*9bb55568SKyungwoo Lee 
115*9bb55568SKyungwoo Lee   EXPECT_EQ(TreeDump1, TreeDump2);
116*9bb55568SKyungwoo Lee }
117*9bb55568SKyungwoo Lee 
118*9bb55568SKyungwoo Lee } // end namespace
119