| 12 | //////////////////////////////////////////////////////////////////////////////// |
| 13 | |
| 14 | TEST(TChunkedMemoryPoolTest, Absorb) |
| 15 | { |
| 16 | TChunkedMemoryPool first; |
| 17 | TChunkedMemoryPool second; |
| 18 | TChunkedMemoryPool third; |
| 19 | |
| 20 | std::vector<std::pair<TStringBuf, TString>> tests; |
| 21 | size_t totalSize = 0; |
| 22 | |
| 23 | auto fillPool = [&] (TChunkedMemoryPool& pool, TString prefix, int count) { |
| 24 | for (int i = 0; i < count; i++) { |
| 25 | TString expected = prefix + ToString(count); |
| 26 | char* buf = pool.AllocateUnaligned(expected.size()); |
| 27 | ::memcpy(buf, expected.c_str(), expected.size()); |
| 28 | TStringBuf ref(buf, buf + expected.size()); |
| 29 | totalSize += expected.size(); |
| 30 | tests.emplace_back(std::move(ref), std::move(expected)); |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | auto checkAll = [&] { |
| 35 | ASSERT_GE(first.GetCapacity(), first.GetSize()); |
| 36 | ASSERT_GE(second.GetCapacity(), second.GetSize()); |
| 37 | ASSERT_GE(third.GetCapacity(), third.GetSize()); |
| 38 | ASSERT_EQ(totalSize, first.GetSize() + second.GetSize() + third.GetSize()); |
| 39 | |
| 40 | for (const auto& [ref, expected] : tests) { |
| 41 | ASSERT_EQ(ref, expected); |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | auto warmup = [&] (TChunkedMemoryPool& pool, int blockSize, int count) { |
| 46 | ASSERT_EQ(pool.GetSize(), 0U); |
| 47 | for (int i = 0; i < count; i++) { |
| 48 | pool.AllocateUnaligned(blockSize); |
| 49 | } |
| 50 | pool.Clear(); |
| 51 | ASSERT_EQ(pool.GetSize(), 0U); |
| 52 | }; |
| 53 | |
| 54 | warmup(first, 20, 20'000); |
| 55 | warmup(second, 20, 20'000); |
| 56 | fillPool(first, "firstPool", 10'000); |
| 57 | fillPool(second, "secondPool", 10'000); |
| 58 | fillPool(third, "thirdPool", 10'000); |
| 59 | checkAll(); |
| 60 | second.Absorb(std::move(third)); |
| 61 | checkAll(); |
| 62 | first.Absorb(std::move(second)); |
| 63 | checkAll(); |
| 64 | fillPool(first, "poolFirst_2", 10'000); |
| 65 | fillPool(second, "poolSecond_2", 10'000); |
| 66 | fillPool(third, "poolThird_2", 10'000); |
| 67 | checkAll(); |
| 68 | third.Absorb(std::move(second)); |
| 69 | checkAll(); |
| 70 | fillPool(second, "someStr2", 10'000); |
| 71 | fillPool(third, "someStr3", 10'000); |
nothing calls this directly
no test coverage detected