xref: /llvm-project/compiler-rt/lib/orc/tests/unit/string_pool_test.cpp (revision dbd81ba2e85c2f244f22c983d96a106eae65c06a)
18fda8901SLang Hames //===---------- string_pool_test.cpp - Unit tests for StringPool ----------===//
28fda8901SLang Hames //
38fda8901SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
48fda8901SLang Hames // See https://llvm.org/LICENSE.txt for license information.
58fda8901SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
68fda8901SLang Hames //
78fda8901SLang Hames //===----------------------------------------------------------------------===//
88fda8901SLang Hames 
98fda8901SLang Hames #include "string_pool.h"
108fda8901SLang Hames #include "gtest/gtest.h"
118fda8901SLang Hames 
12*dbd81ba2SMikhail Goncharov using namespace orc_rt;
138fda8901SLang Hames 
148fda8901SLang Hames namespace {
158fda8901SLang Hames 
168fda8901SLang Hames TEST(StringPool, UniquingAndComparisons) {
178fda8901SLang Hames   StringPool SP;
188fda8901SLang Hames   auto P1 = SP.intern("hello");
198fda8901SLang Hames 
208fda8901SLang Hames   std::string S("hel");
218fda8901SLang Hames   S += "lo";
228fda8901SLang Hames   auto P2 = SP.intern(S);
238fda8901SLang Hames 
248fda8901SLang Hames   auto P3 = SP.intern("goodbye");
258fda8901SLang Hames 
268fda8901SLang Hames   EXPECT_EQ(P1, P2) << "Failed to unique entries";
278fda8901SLang Hames   EXPECT_NE(P1, P3) << "Unequal pooled strings comparing equal";
288fda8901SLang Hames 
298fda8901SLang Hames   // We want to test that less-than comparison of PooledStringPtrs compiles,
308fda8901SLang Hames   // however we can't test the actual result as this is a pointer comparison and
318fda8901SLang Hames   // PooledStringPtr doesn't expose the underlying address of the string.
328fda8901SLang Hames   (void)(P1 < P3);
338fda8901SLang Hames }
348fda8901SLang Hames 
358fda8901SLang Hames TEST(StringPool, Dereference) {
368fda8901SLang Hames   StringPool SP;
378fda8901SLang Hames   auto Foo = SP.intern("foo");
388fda8901SLang Hames   EXPECT_EQ(*Foo, "foo") << "Equality on dereferenced string failed";
398fda8901SLang Hames }
408fda8901SLang Hames 
418fda8901SLang Hames TEST(StringPool, ClearDeadEntries) {
428fda8901SLang Hames   StringPool SP;
438fda8901SLang Hames   {
448fda8901SLang Hames     auto P1 = SP.intern("s1");
458fda8901SLang Hames     SP.clearDeadEntries();
468fda8901SLang Hames     EXPECT_FALSE(SP.empty()) << "\"s1\" entry in pool should still be retained";
478fda8901SLang Hames   }
488fda8901SLang Hames   SP.clearDeadEntries();
498fda8901SLang Hames   EXPECT_TRUE(SP.empty()) << "pool should be empty";
508fda8901SLang Hames }
518fda8901SLang Hames 
528fda8901SLang Hames TEST(StringPool, NullPtr) {
538fda8901SLang Hames   // Make sure that we can default construct and then destroy a null
548fda8901SLang Hames   // PooledStringPtr.
558fda8901SLang Hames   PooledStringPtr Null;
568fda8901SLang Hames }
578fda8901SLang Hames 
588fda8901SLang Hames TEST(StringPool, Hashable) {
598fda8901SLang Hames   StringPool SP;
608fda8901SLang Hames   PooledStringPtr P1 = SP.intern("s1");
618fda8901SLang Hames   PooledStringPtr Null;
628fda8901SLang Hames   EXPECT_NE(std::hash<PooledStringPtr>()(P1),
638fda8901SLang Hames             std::hash<PooledStringPtr>()(Null));
648fda8901SLang Hames }
658fda8901SLang Hames 
668fda8901SLang Hames } // end anonymous namespace
67