xref: /llvm-project/llvm/unittests/DWARFLinkerParallel/StringPoolTest.cpp (revision fab91e950966dbc5d3b59208e4dab274ac54e891)
1 //===- llvm/unittest/DWARFLinkerParallel/StringPoolTest.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/DWARFLinkerParallel/StringPool.h"
10 #include "llvm/Support/Parallel.h"
11 #include "gtest/gtest.h"
12 #include <cstdlib>
13 
14 using namespace llvm;
15 using namespace dwarflinker_parallel;
16 
17 namespace {
18 
19 TEST(StringPoolTest, TestStringPool) {
20   StringPool Strings;
21 
22   // StringPool uses PerThreadBumpPtrAllocator which should be accessed from
23   // threads created by ThreadPoolExecutor. Use TaskGroup to run on
24   // ThreadPoolExecutor threads.
25   parallel::TaskGroup tg;
26 
27   tg.spawn([&]() {
28     std::pair<StringEntry *, bool> Entry = Strings.insert("test");
29     EXPECT_TRUE(Entry.second);
30     EXPECT_TRUE(Entry.first->getKey() == "test");
31     EXPECT_TRUE(Entry.first->second == nullptr);
32 
33     StringEntry *EntryPtr = Entry.first;
34 
35     Entry = Strings.insert("test");
36     EXPECT_FALSE(Entry.second);
37     EXPECT_TRUE(Entry.first->getKey() == "test");
38     EXPECT_TRUE(Entry.first->second == nullptr);
39     EXPECT_TRUE(EntryPtr == Entry.first);
40 
41     Entry = Strings.insert("test2");
42     EXPECT_TRUE(Entry.second);
43     EXPECT_TRUE(Entry.first->getKey() == "test2");
44     EXPECT_TRUE(Entry.first->second == nullptr);
45     EXPECT_TRUE(EntryPtr != Entry.first);
46   });
47 }
48 
49 TEST(StringPoolTest, TestStringPoolParallel) {
50   StringPool Strings;
51 
52   // Add data.
53   parallelFor(0, 1000, [&](size_t Idx) {
54     std::pair<StringEntry *, bool> Entry = Strings.insert(std::to_string(Idx));
55     EXPECT_TRUE(Entry.second);
56     EXPECT_TRUE(Entry.first->getKey() == std::to_string(Idx));
57     EXPECT_TRUE(Entry.first->second == nullptr);
58   });
59 
60   // Check data.
61   parallelFor(0, 1000, [&](size_t Idx) {
62     std::pair<StringEntry *, bool> Entry = Strings.insert(std::to_string(Idx));
63     EXPECT_FALSE(Entry.second);
64     EXPECT_TRUE(Entry.first->getKey() == std::to_string(Idx));
65     EXPECT_TRUE(Entry.first->second == nullptr);
66   });
67 }
68 
69 } // anonymous namespace
70