59 map_[
"key1"] = std::make_unique<TestData>();
60 map_[
"key1"]->value = 42;
67 return std::make_unique<TestData>();
70 ASSERT_NE(ptr,
nullptr);
71 EXPECT_EQ(map_.size(), 1u);
72 EXPECT_EQ(ptr->value, 42);
73 EXPECT_EQ(create_count, 0);
110 map_[
"key1"] = std::make_unique<TestData>();
111 map_[
"key1"]->value = 42;
112 map_[
"key1"]->name =
"original";
116 map_, mutex_,
"key1",
117 []() {
return std::make_unique<TestData>(); },
118 [&init_count](TestData& d) {
123 ASSERT_NE(ptr,
nullptr);
124 EXPECT_EQ(ptr->value, 42);
125 EXPECT_EQ(ptr->name,
"original");
126 EXPECT_EQ(init_count, 0);
168 std::atomic<int> create_count{0};
169 const int num_threads = 10;
170 const int iterations_per_thread = 1000;
172 std::vector<std::thread> threads;
173 threads.reserve(num_threads);
175 for (
int t = 0; t < num_threads; ++t) {
176 threads.emplace_back([
this, &create_count, iterations_per_thread]() {
177 for (
int i = 0; i < iterations_per_thread; ++i) {
179 map_, mutex_,
"shared_key",
182 return std::make_unique<TestData>();
184 ASSERT_NE(ptr,
nullptr);
189 for (
auto& t : threads) {
193 EXPECT_EQ(map_.size(), 1u);
194 EXPECT_EQ(create_count.load(), 1);
198 std::atomic<int> total_creates{0};
199 const int num_threads = 10;
200 const int keys_per_thread = 100;
202 std::vector<std::thread> threads;
203 threads.reserve(num_threads);
205 for (
int t = 0; t < num_threads; ++t) {
206 threads.emplace_back([
this, t, &total_creates, keys_per_thread]() {
207 for (
int i = 0; i < keys_per_thread; ++i) {
208 std::string key =
"key_" + std::to_string(t) +
"_" + std::to_string(i);
213 return std::make_unique<TestData>();
215 ASSERT_NE(ptr,
nullptr);
220 for (
auto& t : threads) {
224 EXPECT_EQ(map_.size(),
static_cast<size_t>(num_threads * keys_per_thread));
225 EXPECT_EQ(total_creates.load(), num_threads * keys_per_thread);
230 for (
int i = 0; i < 50; ++i) {
231 map_[
"existing_" + std::to_string(i)] = std::make_unique<TestData>();
234 const int num_threads = 8;
235 const int iterations = 500;
237 std::vector<std::thread> threads;
238 threads.reserve(num_threads);
240 for (
int t = 0; t < num_threads; ++t) {
241 threads.emplace_back([
this, t, iterations]() {
242 for (
int i = 0; i < iterations; ++i) {
244 std::string key = (i % 2 == 0)
245 ?
"existing_" + std::to_string(i % 50)
246 :
"new_" + std::to_string(t) +
"_" + std::to_string(i);
250 []() {
return std::make_unique<TestData>(); });
251 ASSERT_NE(ptr,
nullptr);
256 for (
auto& t : threads) {
260 EXPECT_GE(map_.size(), 50u);
269 map_[
"hot_key"] = std::make_unique<TestData>();
271 std::atomic<int> create_calls{0};
272 const int hot_path_iterations = 10000;
275 for (
int i = 0; i < hot_path_iterations; ++i) {
277 map_, mutex_,
"hot_key",
280 return std::make_unique<TestData>();
282 ASSERT_NE(ptr,
nullptr);
286 EXPECT_EQ(create_calls.load(), 0);