about summary refs log tree commit diff
path: root/absl/container/inlined_vector_test.cc
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2018-01-10T19·46-0800
committerDerek Mauro <dmauro@google.com>2018-01-10T20·36-0500
commitc742b72354a84958b6a061755249822eeef87d06 (patch)
treebf7c3480e1b7396124625c49ccc103bb7bdc6008 /absl/container/inlined_vector_test.cc
parent98bff8b2bc53aa271c0dd0dff1b33730b4df8481 (diff)
Changes imported from Abseil "staging" branch:
  - f679f7de2957ac4dca0a862d04f1165d2f503525 Merge GitHub PR #78: Fix typo in thread_identity.h by Derek Mauro <dmauro@google.com>
  - 369cbefc9ebb8503e3c25b1516c856dab3bed7ac Minor refactor of operator-(uint128). by Alex Strelnikov <strel@google.com>
  - fba0f8c33b051d90936ad0fcaa4bea83f554bf8d Merge GitHub PR #75: Fix typo in per_thread_tls.h by Derek Mauro <dmauro@google.com>
  - 76d5d25a54ab93c1ea3bc74b5a28ba335b0f2bab Implement InlinedVector::shrink_to_fit() method. by Abseil Team <absl-team@google.com>

GitOrigin-RevId: f679f7de2957ac4dca0a862d04f1165d2f503525
Change-Id: I03b39fdbd70c00a455d98d949d413dd7c8019578
Diffstat (limited to 'absl/container/inlined_vector_test.cc')
-rw-r--r--absl/container/inlined_vector_test.cc77
1 files changed, 77 insertions, 0 deletions
diff --git a/absl/container/inlined_vector_test.cc b/absl/container/inlined_vector_test.cc
index 8527ba1aa008..0e39855b3066 100644
--- a/absl/container/inlined_vector_test.cc
+++ b/absl/container/inlined_vector_test.cc
@@ -407,6 +407,69 @@ TEST(InlinedVectorTest, EmplaceBack) {
   EXPECT_EQ(allocated_element.second, 1729);
 }
 
+TEST(InlinedVectorTest, ShrinkToFitGrowingVector) {
+  absl::InlinedVector<std::pair<std::string, int>, 1> v;
+
+  v.shrink_to_fit();
+  EXPECT_EQ(v.capacity(), 1);
+
+  v.emplace_back("answer", 42);
+  v.shrink_to_fit();
+  EXPECT_EQ(v.capacity(), 1);
+
+  v.emplace_back("taxicab", 1729);
+  EXPECT_GE(v.capacity(), 2);
+  v.shrink_to_fit();
+  EXPECT_EQ(v.capacity(), 2);
+
+  v.reserve(100);
+  EXPECT_GE(v.capacity(), 100);
+  v.shrink_to_fit();
+  EXPECT_EQ(v.capacity(), 2);
+}
+
+TEST(InlinedVectorTest, ShrinkToFitEdgeCases) {
+  {
+    absl::InlinedVector<std::pair<std::string, int>, 1> v;
+    v.emplace_back("answer", 42);
+    v.emplace_back("taxicab", 1729);
+    EXPECT_GE(v.capacity(), 2);
+    v.pop_back();
+    v.shrink_to_fit();
+    EXPECT_EQ(v.capacity(), 1);
+    EXPECT_EQ(v[0].first, "answer");
+    EXPECT_EQ(v[0].second, 42);
+  }
+
+  {
+    absl::InlinedVector<std::string, 2> v(100);
+    v.resize(0);
+    v.shrink_to_fit();
+    EXPECT_EQ(v.capacity(), 2);  // inlined capacity
+  }
+
+  {
+    absl::InlinedVector<std::string, 2> v(100);
+    v.resize(1);
+    v.shrink_to_fit();
+    EXPECT_EQ(v.capacity(), 2);  // inlined capacity
+  }
+
+  {
+    absl::InlinedVector<std::string, 2> v(100);
+    v.resize(2);
+    v.shrink_to_fit();
+    EXPECT_EQ(v.capacity(), 2);
+  }
+
+  {
+    absl::InlinedVector<std::string, 2> v(100);
+    v.resize(3);
+    v.shrink_to_fit();
+    EXPECT_EQ(v.capacity(), 3);
+  }
+}
+
 TEST(IntVec, Insert) {
   for (int len = 0; len < 20; len++) {
     for (int pos = 0; pos <= len; pos++) {
@@ -1589,6 +1652,20 @@ TEST(AllocatorSupportTest, CountAllocations) {
     AllocVec v3(std::move(v), alloc3);
     EXPECT_THAT(allocated3, v3.size() * sizeof(int));
   }
+  EXPECT_EQ(allocated, 0);
+  {
+    // Test shrink_to_fit deallocations.
+    AllocVec v(8, 2, alloc);
+    EXPECT_EQ(allocated, 8 * sizeof(int));
+    v.resize(5);
+    EXPECT_EQ(allocated, 8 * sizeof(int));
+    v.shrink_to_fit();
+    EXPECT_EQ(allocated, 5 * sizeof(int));
+    v.resize(4);
+    EXPECT_EQ(allocated, 5 * sizeof(int));
+    v.shrink_to_fit();
+    EXPECT_EQ(allocated, 0);
+  }
 }
 
 TEST(AllocatorSupportTest, SwapBothAllocated) {