forked from OSchip/llvm-project
				
			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> llvm-svn: 264475
This commit is contained in:
		
							parent
							
								
									221cfdd7b1
								
							
						
					
					
						commit
						412989750d
					
				| 
						 | 
					@ -377,18 +377,21 @@ TEST(DenseMapCustomTest, DefaultMinReservedSizeTest) {
 | 
				
			||||||
  CountCopyAndMove::Copy = 0;
 | 
					  CountCopyAndMove::Copy = 0;
 | 
				
			||||||
  CountCopyAndMove::Move = 0;
 | 
					  CountCopyAndMove::Move = 0;
 | 
				
			||||||
  for (int i = 0; i < ExpectedMaxInitialEntries; ++i)
 | 
					  for (int i = 0; i < ExpectedMaxInitialEntries; ++i)
 | 
				
			||||||
    Map.insert(std::make_pair(i, CountCopyAndMove()));
 | 
					    Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct,
 | 
				
			||||||
 | 
					                                                std::forward_as_tuple(i),
 | 
				
			||||||
 | 
					                                                std::forward_as_tuple()));
 | 
				
			||||||
  // Check that we didn't grow
 | 
					  // Check that we didn't grow
 | 
				
			||||||
  EXPECT_EQ(MemorySize, Map.getMemorySize());
 | 
					  EXPECT_EQ(MemorySize, Map.getMemorySize());
 | 
				
			||||||
  // Check that move was called the expected number of times
 | 
					  // Check that move was called the expected number of times
 | 
				
			||||||
  EXPECT_EQ(ExpectedMaxInitialEntries * 2, CountCopyAndMove::Move);
 | 
					  EXPECT_EQ(ExpectedMaxInitialEntries, CountCopyAndMove::Move);
 | 
				
			||||||
  // Check that no copy occured
 | 
					  // Check that no copy occured
 | 
				
			||||||
  EXPECT_EQ(0, CountCopyAndMove::Copy);
 | 
					  EXPECT_EQ(0, CountCopyAndMove::Copy);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Adding one extra element should grow the map
 | 
					  // Adding one extra element should grow the map
 | 
				
			||||||
  CountCopyAndMove::Copy = 0;
 | 
					  Map.insert(std::pair<int, CountCopyAndMove>(
 | 
				
			||||||
  CountCopyAndMove::Move = 0;
 | 
					      std::piecewise_construct,
 | 
				
			||||||
  Map.insert(std::make_pair(ExpectedMaxInitialEntries, CountCopyAndMove()));
 | 
					      std::forward_as_tuple(ExpectedMaxInitialEntries),
 | 
				
			||||||
 | 
					      std::forward_as_tuple()));
 | 
				
			||||||
  // Check that we grew
 | 
					  // Check that we grew
 | 
				
			||||||
  EXPECT_NE(MemorySize, Map.getMemorySize());
 | 
					  EXPECT_NE(MemorySize, Map.getMemorySize());
 | 
				
			||||||
  // Check that move was called the expected number of times
 | 
					  // Check that move was called the expected number of times
 | 
				
			||||||
| 
						 | 
					@ -412,12 +415,13 @@ TEST(DenseMapCustomTest, InitialSizeTest) {
 | 
				
			||||||
    CountCopyAndMove::Copy = 0;
 | 
					    CountCopyAndMove::Copy = 0;
 | 
				
			||||||
    CountCopyAndMove::Move = 0;
 | 
					    CountCopyAndMove::Move = 0;
 | 
				
			||||||
    for (int i = 0; i < Size; ++i)
 | 
					    for (int i = 0; i < Size; ++i)
 | 
				
			||||||
      Map.insert(std::make_pair(i, CountCopyAndMove()));
 | 
					      Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct,
 | 
				
			||||||
 | 
					                                                  std::forward_as_tuple(i),
 | 
				
			||||||
 | 
					                                                  std::forward_as_tuple()));
 | 
				
			||||||
    // Check that we didn't grow
 | 
					    // Check that we didn't grow
 | 
				
			||||||
    EXPECT_EQ(MemorySize, Map.getMemorySize());
 | 
					    EXPECT_EQ(MemorySize, Map.getMemorySize());
 | 
				
			||||||
    // Check that move was called the expected number of times
 | 
					    // Check that move was called the expected number of times
 | 
				
			||||||
    //  This relies on move-construction elision, and cannot be reliably tested.
 | 
					    EXPECT_EQ(Size, CountCopyAndMove::Move);
 | 
				
			||||||
    //   EXPECT_EQ(Size * 2, CountCopyAndMove::Move);
 | 
					 | 
				
			||||||
    // Check that no copy occured
 | 
					    // Check that no copy occured
 | 
				
			||||||
    EXPECT_EQ(0, CountCopyAndMove::Copy);
 | 
					    EXPECT_EQ(0, CountCopyAndMove::Copy);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -455,12 +459,13 @@ TEST(DenseMapCustomTest, ReserveTest) {
 | 
				
			||||||
    CountCopyAndMove::Copy = 0;
 | 
					    CountCopyAndMove::Copy = 0;
 | 
				
			||||||
    CountCopyAndMove::Move = 0;
 | 
					    CountCopyAndMove::Move = 0;
 | 
				
			||||||
    for (int i = 0; i < Size; ++i)
 | 
					    for (int i = 0; i < Size; ++i)
 | 
				
			||||||
      Map.insert(std::make_pair(i, CountCopyAndMove()));
 | 
					      Map.insert(std::pair<int, CountCopyAndMove>(std::piecewise_construct,
 | 
				
			||||||
 | 
					                                                  std::forward_as_tuple(i),
 | 
				
			||||||
 | 
					                                                  std::forward_as_tuple()));
 | 
				
			||||||
    // Check that we didn't grow
 | 
					    // Check that we didn't grow
 | 
				
			||||||
    EXPECT_EQ(MemorySize, Map.getMemorySize());
 | 
					    EXPECT_EQ(MemorySize, Map.getMemorySize());
 | 
				
			||||||
    // Check that move was called the expected number of times
 | 
					    // Check that move was called the expected number of times
 | 
				
			||||||
    //  This relies on move-construction elision, and cannot be reliably tested.
 | 
					    EXPECT_EQ(Size, CountCopyAndMove::Move);
 | 
				
			||||||
    //   EXPECT_EQ(Size * 2, CountCopyAndMove::Move);
 | 
					 | 
				
			||||||
    // Check that no copy occured
 | 
					    // Check that no copy occured
 | 
				
			||||||
    EXPECT_EQ(0, CountCopyAndMove::Copy);
 | 
					    EXPECT_EQ(0, CountCopyAndMove::Copy);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -391,21 +391,16 @@ TEST(StringMapCustomTest, InitialSizeTest) {
 | 
				
			||||||
  for (auto Size : {1, 32, 67}) {
 | 
					  for (auto Size : {1, 32, 67}) {
 | 
				
			||||||
    StringMap<CountCtorCopyAndMove> Map(Size);
 | 
					    StringMap<CountCtorCopyAndMove> Map(Size);
 | 
				
			||||||
    auto NumBuckets = Map.getNumBuckets();
 | 
					    auto NumBuckets = Map.getNumBuckets();
 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Prepare the elts in a vector. We do this as a pre-step to shield us
 | 
					 | 
				
			||||||
    // against the internals of std::pair which can introduce spurious move/copy
 | 
					 | 
				
			||||||
    std::vector<std::pair<std::string, CountCtorCopyAndMove>> Elts;
 | 
					 | 
				
			||||||
    for (int i = 0; i < Size; ++i)
 | 
					 | 
				
			||||||
      Elts.emplace_back(Twine(i).str(), CountCtorCopyAndMove());
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    CountCtorCopyAndMove::Move = 0;
 | 
					    CountCtorCopyAndMove::Move = 0;
 | 
				
			||||||
    CountCtorCopyAndMove::Copy = 0;
 | 
					    CountCtorCopyAndMove::Copy = 0;
 | 
				
			||||||
    for (int i = 0; i < Size; ++i)
 | 
					    for (int i = 0; i < Size; ++i)
 | 
				
			||||||
      Map.insert(Elts[i]);
 | 
					      Map.insert(std::pair<std::string, CountCtorCopyAndMove>(
 | 
				
			||||||
    // After the inital copy, the map will move the Elts in the Entry.
 | 
					          std::piecewise_construct, std::forward_as_tuple(Twine(i).str()),
 | 
				
			||||||
    EXPECT_EQ((unsigned)Size, CountCtorCopyAndMove::Move);
 | 
					          std::forward_as_tuple(i)));
 | 
				
			||||||
 | 
					    // After the inital move, the map will move the Elts in the Entry.
 | 
				
			||||||
 | 
					    EXPECT_EQ((unsigned)Size * 2, CountCtorCopyAndMove::Move);
 | 
				
			||||||
    // We copy once the pair from the Elts vector
 | 
					    // We copy once the pair from the Elts vector
 | 
				
			||||||
    EXPECT_EQ((unsigned)Size, CountCtorCopyAndMove::Copy);
 | 
					    EXPECT_EQ(0u, CountCtorCopyAndMove::Copy);
 | 
				
			||||||
    // Check that the map didn't grow
 | 
					    // Check that the map didn't grow
 | 
				
			||||||
    EXPECT_EQ(Map.getNumBuckets(), NumBuckets);
 | 
					    EXPECT_EQ(Map.getNumBuckets(), NumBuckets);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue