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