xref: /llvm-project/llvm/unittests/DWARFLinkerParallel/StringPoolTest.cpp (revision 2357e899cb11e05312c54b689ebd0355487be6bc)
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/DWARFLinker/StringPool.h"
10 #include "llvm/Support/Parallel.h"
11 #include "gtest/gtest.h"
12 #include <cstdlib>
13 
14 using namespace llvm;
15 using namespace dwarf_linker;
16 
17 namespace {
18 
TEST(StringPoolTest,TestStringPool)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 
32     StringEntry *EntryPtr = Entry.first;
33 
34     Entry = Strings.insert("test");
35     EXPECT_FALSE(Entry.second);
36     EXPECT_TRUE(Entry.first->getKey() == "test");
37     EXPECT_TRUE(EntryPtr == Entry.first);
38 
39     Entry = Strings.insert("test2");
40     EXPECT_TRUE(Entry.second);
41     EXPECT_TRUE(Entry.first->getKey() == "test2");
42     EXPECT_TRUE(EntryPtr != Entry.first);
43   });
44 }
45 
TEST(StringPoolTest,TestStringPoolParallel)46 TEST(StringPoolTest, TestStringPoolParallel) {
47   StringPool Strings;
48 
49   // Add data.
50   parallelFor(0, 1000, [&](size_t Idx) {
51     std::pair<StringEntry *, bool> Entry = Strings.insert(std::to_string(Idx));
52     EXPECT_TRUE(Entry.second);
53     EXPECT_TRUE(Entry.first->getKey() == std::to_string(Idx));
54   });
55 
56   // Check data.
57   parallelFor(0, 1000, [&](size_t Idx) {
58     std::pair<StringEntry *, bool> Entry = Strings.insert(std::to_string(Idx));
59     EXPECT_FALSE(Entry.second);
60     EXPECT_TRUE(Entry.first->getKey() == std::to_string(Idx));
61   });
62 }
63 
64 } // anonymous namespace
65