llvm.org GIT mirror llvm / 64f9238
StringMap/DenseMap unittests: use piecewise_construct and ensure no copy occurs. This makes us no longer relying on move-construction elision by the compiler. Suggested by D. Blaikie. From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@264475 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
2 changed file(s) with 22 addition(s) and 22 deletion(s). Raw diff Collapse all Expand all
376376 CountCopyAndMove::Copy = 0;
377377 CountCopyAndMove::Move = 0;
378378 for (int i = 0; i < ExpectedMaxInitialEntries; ++i)
379 Map.insert(std::make_pair(i, CountCopyAndMove()));
379 Map.insert(std::pair(std::piecewise_construct,
380 std::forward_as_tuple(i),
381 std::forward_as_tuple()));
380382 // Check that we didn't grow
381383 EXPECT_EQ(MemorySize, Map.getMemorySize());
382384 // Check that move was called the expected number of times
383 EXPECT_EQ(ExpectedMaxInitialEntries * 2, CountCopyAndMove::Move);
385 EXPECT_EQ(ExpectedMaxInitialEntries, CountCopyAndMove::Move);
384386 // Check that no copy occured
385387 EXPECT_EQ(0, CountCopyAndMove::Copy);
386388
387389 // Adding one extra element should grow the map
388 CountCopyAndMove::Copy = 0;
389 CountCopyAndMove::Move = 0;
390 Map.insert(std::make_pair(ExpectedMaxInitialEntries, CountCopyAndMove()));
390 Map.insert(std::pair(
391 std::piecewise_construct,
392 std::forward_as_tuple(ExpectedMaxInitialEntries),
393 std::forward_as_tuple()));
391394 // Check that we grew
392395 EXPECT_NE(MemorySize, Map.getMemorySize());
393396 // Check that move was called the expected number of times
411414 CountCopyAndMove::Copy = 0;
412415 CountCopyAndMove::Move = 0;
413416 for (int i = 0; i < Size; ++i)
414 Map.insert(std::make_pair(i, CountCopyAndMove()));
417 Map.insert(std::pair(std::piecewise_construct,
418 std::forward_as_tuple(i),
419 std::forward_as_tuple()));
415420 // Check that we didn't grow
416421 EXPECT_EQ(MemorySize, Map.getMemorySize());
417422 // Check that move was called the expected number of times
418 // This relies on move-construction elision, and cannot be reliably tested.
419 // EXPECT_EQ(Size * 2, CountCopyAndMove::Move);
423 EXPECT_EQ(Size, CountCopyAndMove::Move);
420424 // Check that no copy occured
421425 EXPECT_EQ(0, CountCopyAndMove::Copy);
422426 }
454458 CountCopyAndMove::Copy = 0;
455459 CountCopyAndMove::Move = 0;
456460 for (int i = 0; i < Size; ++i)
457 Map.insert(std::make_pair(i, CountCopyAndMove()));
461 Map.insert(std::pair(std::piecewise_construct,
462 std::forward_as_tuple(i),
463 std::forward_as_tuple()));
458464 // Check that we didn't grow
459465 EXPECT_EQ(MemorySize, Map.getMemorySize());
460466 // Check that move was called the expected number of times
461 // This relies on move-construction elision, and cannot be reliably tested.
462 // EXPECT_EQ(Size * 2, CountCopyAndMove::Move);
467 EXPECT_EQ(Size, CountCopyAndMove::Move);
463468 // Check that no copy occured
464469 EXPECT_EQ(0, CountCopyAndMove::Copy);
465470 }
390390 for (auto Size : {1, 32, 67}) {
391391 StringMap Map(Size);
392392 auto NumBuckets = Map.getNumBuckets();
393
394 // Prepare the elts in a vector. We do this as a pre-step to shield us
395 // against the internals of std::pair which can introduce spurious move/copy
396 std::vector> Elts;
397 for (int i = 0; i < Size; ++i)
398 Elts.emplace_back(Twine(i).str(), CountCtorCopyAndMove());
399
400393 CountCtorCopyAndMove::Move = 0;
401394 CountCtorCopyAndMove::Copy = 0;
402395 for (int i = 0; i < Size; ++i)
403 Map.insert(Elts[i]);
404 // After the inital copy, the map will move the Elts in the Entry.
405 EXPECT_EQ((unsigned)Size, CountCtorCopyAndMove::Move);
396 Map.insert(std::pair(
397 std::piecewise_construct, std::forward_as_tuple(Twine(i).str()),
398 std::forward_as_tuple(i)));
399 // After the inital move, the map will move the Elts in the Entry.
400 EXPECT_EQ((unsigned)Size * 2, CountCtorCopyAndMove::Move);
406401 // We copy once the pair from the Elts vector
407 EXPECT_EQ((unsigned)Size, CountCtorCopyAndMove::Copy);
402 EXPECT_EQ(0u, CountCtorCopyAndMove::Copy);
408403 // Check that the map didn't grow
409404 EXPECT_EQ(Map.getNumBuckets(), NumBuckets);
410405 }