2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
9 #include "utils/ResourcePool.h"
11 #include <gtest/gtest.h>
15 using namespace pzstd;
17 TEST(ResourcePool, FullTest) {
18 unsigned numCreated = 0;
19 unsigned numDeleted = 0;
21 ResourcePool<int> pool(
22 [&numCreated] { ++numCreated; return new int{5}; },
23 [&numDeleted](int *x) { ++numDeleted; delete x; });
44 EXPECT_EQ(2, numCreated);
45 EXPECT_EQ(numCreated, numDeleted);
48 TEST(ResourcePool, ThreadSafe) {
49 std::atomic<unsigned> numCreated{0};
50 std::atomic<unsigned> numDeleted{0};
52 ResourcePool<int> pool(
53 [&numCreated] { ++numCreated; return new int{0}; },
54 [&numDeleted](int *x) { ++numDeleted; delete x; });
56 for (int i = 0; i < 100; ++i) {
68 EXPECT_EQ(200, *x + *y);
70 EXPECT_GE(2, numCreated);
71 EXPECT_EQ(numCreated, numDeleted);