xref: /llvm-project/llvm/unittests/ExecutionEngine/Orc/SymbolStringPoolTest.cpp (revision 3fcd3669e3dc7c5ba72d3bfceef42b2f833254ec)
1 //===----- SymbolStringPoolTest.cpp - Unit tests for SymbolStringPool -----===//
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/ExecutionEngine/Orc/SymbolStringPool.h"
10 #include "llvm/ExecutionEngine/Orc/DebugUtils.h"
11 #include "gtest/gtest.h"
12 
13 using namespace llvm;
14 using namespace llvm::orc;
15 
16 namespace {
17 
18 TEST(SymbolStringPool, UniquingAndComparisons) {
19   SymbolStringPool SP;
20   auto P1 = SP.intern("hello");
21 
22   std::string S("hel");
23   S += "lo";
24   auto P2 = SP.intern(S);
25 
26   auto P3 = SP.intern("goodbye");
27 
28   EXPECT_EQ(P1, P2) << "Failed to unique entries";
29   EXPECT_NE(P1, P3) << "Inequal pooled symbol strings comparing equal";
30 
31   // We want to test that less-than comparison of SymbolStringPtrs compiles,
32   // however we can't test the actual result as this is a pointer comparison and
33   // SymbolStringPtr doesn't expose the underlying address of the string.
34   (void)(P1 < P3);
35 }
36 
37 TEST(SymbolStringPool, Dereference) {
38   SymbolStringPool SP;
39   auto Foo = SP.intern("foo");
40   EXPECT_EQ(*Foo, "foo") << "Equality on dereferenced string failed";
41 }
42 
43 TEST(SymbolStringPool, ClearDeadEntries) {
44   SymbolStringPool SP;
45   {
46     auto P1 = SP.intern("s1");
47     SP.clearDeadEntries();
48     EXPECT_FALSE(SP.empty()) << "\"s1\" entry in pool should still be retained";
49   }
50   SP.clearDeadEntries();
51   EXPECT_TRUE(SP.empty()) << "pool should be empty";
52 }
53 
54 TEST(SymbolStringPool, DebugDump) {
55   SymbolStringPool SP;
56   auto A1 = SP.intern("a");
57   auto A2 = A1;
58   auto B = SP.intern("b");
59 
60   std::string DumpString;
61   raw_string_ostream(DumpString) << SP;
62 
63   EXPECT_EQ(DumpString, "a: 2\nb: 1\n");
64 }
65 }
66