1868654e5SAlexey Lapshin //===- llvm/unittest/DWARFLinkerParallel/StringPoolTest.cpp ---------------===//
2868654e5SAlexey Lapshin //
3868654e5SAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4868654e5SAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
5868654e5SAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6868654e5SAlexey Lapshin //
7868654e5SAlexey Lapshin //===----------------------------------------------------------------------===//
8868654e5SAlexey Lapshin
9*2357e899Savl-llvm #include "llvm/DWARFLinker/StringPool.h"
10868654e5SAlexey Lapshin #include "llvm/Support/Parallel.h"
11868654e5SAlexey Lapshin #include "gtest/gtest.h"
12868654e5SAlexey Lapshin #include <cstdlib>
13868654e5SAlexey Lapshin
14868654e5SAlexey Lapshin using namespace llvm;
15*2357e899Savl-llvm using namespace dwarf_linker;
16868654e5SAlexey Lapshin
17868654e5SAlexey Lapshin namespace {
18868654e5SAlexey Lapshin
TEST(StringPoolTest,TestStringPool)19868654e5SAlexey Lapshin TEST(StringPoolTest, TestStringPool) {
20868654e5SAlexey Lapshin StringPool Strings;
21868654e5SAlexey Lapshin
226ab43f9bSAlexey Lapshin // StringPool uses PerThreadBumpPtrAllocator which should be accessed from
236ab43f9bSAlexey Lapshin // threads created by ThreadPoolExecutor. Use TaskGroup to run on
246ab43f9bSAlexey Lapshin // ThreadPoolExecutor threads.
256ab43f9bSAlexey Lapshin parallel::TaskGroup tg;
266ab43f9bSAlexey Lapshin
276ab43f9bSAlexey Lapshin tg.spawn([&]() {
28868654e5SAlexey Lapshin std::pair<StringEntry *, bool> Entry = Strings.insert("test");
29868654e5SAlexey Lapshin EXPECT_TRUE(Entry.second);
30868654e5SAlexey Lapshin EXPECT_TRUE(Entry.first->getKey() == "test");
31868654e5SAlexey Lapshin
32868654e5SAlexey Lapshin StringEntry *EntryPtr = Entry.first;
33868654e5SAlexey Lapshin
34868654e5SAlexey Lapshin Entry = Strings.insert("test");
35868654e5SAlexey Lapshin EXPECT_FALSE(Entry.second);
36868654e5SAlexey Lapshin EXPECT_TRUE(Entry.first->getKey() == "test");
37868654e5SAlexey Lapshin EXPECT_TRUE(EntryPtr == Entry.first);
38868654e5SAlexey Lapshin
39868654e5SAlexey Lapshin Entry = Strings.insert("test2");
40868654e5SAlexey Lapshin EXPECT_TRUE(Entry.second);
41868654e5SAlexey Lapshin EXPECT_TRUE(Entry.first->getKey() == "test2");
42868654e5SAlexey Lapshin EXPECT_TRUE(EntryPtr != Entry.first);
436ab43f9bSAlexey Lapshin });
44868654e5SAlexey Lapshin }
45868654e5SAlexey Lapshin
TEST(StringPoolTest,TestStringPoolParallel)46868654e5SAlexey Lapshin TEST(StringPoolTest, TestStringPoolParallel) {
47868654e5SAlexey Lapshin StringPool Strings;
48868654e5SAlexey Lapshin
49868654e5SAlexey Lapshin // Add data.
50868654e5SAlexey Lapshin parallelFor(0, 1000, [&](size_t Idx) {
51868654e5SAlexey Lapshin std::pair<StringEntry *, bool> Entry = Strings.insert(std::to_string(Idx));
52868654e5SAlexey Lapshin EXPECT_TRUE(Entry.second);
53868654e5SAlexey Lapshin EXPECT_TRUE(Entry.first->getKey() == std::to_string(Idx));
54868654e5SAlexey Lapshin });
55868654e5SAlexey Lapshin
56868654e5SAlexey Lapshin // Check data.
57868654e5SAlexey Lapshin parallelFor(0, 1000, [&](size_t Idx) {
58868654e5SAlexey Lapshin std::pair<StringEntry *, bool> Entry = Strings.insert(std::to_string(Idx));
59868654e5SAlexey Lapshin EXPECT_FALSE(Entry.second);
60868654e5SAlexey Lapshin EXPECT_TRUE(Entry.first->getKey() == std::to_string(Idx));
61868654e5SAlexey Lapshin });
62868654e5SAlexey Lapshin }
63868654e5SAlexey Lapshin
64868654e5SAlexey Lapshin } // anonymous namespace
65