• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/internal/resize_uninitialized.h"
16 
17 #include "gtest/gtest.h"
18 
19 namespace {
20 
21 int resize_call_count = 0;
22 
23 // A mock string class whose only purpose is to track how many times its
24 // resize() method has been called.
25 struct resizable_string {
size__anon743226790111::resizable_string26   size_t size() const { return 0; }
capacity__anon743226790111::resizable_string27   size_t capacity() const { return 0; }
operator []__anon743226790111::resizable_string28   char& operator[](size_t) {
29     static char c = '\0';
30     return c;
31   }
resize__anon743226790111::resizable_string32   void resize(size_t) { resize_call_count += 1; }
reserve__anon743226790111::resizable_string33   void reserve(size_t) {}
34 };
35 
36 int resize_default_init_call_count = 0;
37 
38 // A mock string class whose only purpose is to track how many times its
39 // resize() and __resize_default_init() methods have been called.
40 struct resize_default_init_string {
size__anon743226790111::resize_default_init_string41   size_t size() const { return 0; }
capacity__anon743226790111::resize_default_init_string42   size_t capacity() const { return 0; }
operator []__anon743226790111::resize_default_init_string43   char& operator[](size_t) {
44     static char c = '\0';
45     return c;
46   }
resize__anon743226790111::resize_default_init_string47   void resize(size_t) { resize_call_count += 1; }
__resize_default_init__anon743226790111::resize_default_init_string48   void __resize_default_init(size_t) { resize_default_init_call_count += 1; }
reserve__anon743226790111::resize_default_init_string49   void reserve(size_t) {}
50 };
51 
TEST(ResizeUninit,WithAndWithout)52 TEST(ResizeUninit, WithAndWithout) {
53   resize_call_count = 0;
54   resize_default_init_call_count = 0;
55   {
56     resizable_string rs;
57 
58     EXPECT_EQ(resize_call_count, 0);
59     EXPECT_EQ(resize_default_init_call_count, 0);
60     EXPECT_FALSE(
61         absl::strings_internal::STLStringSupportsNontrashingResize(&rs));
62     EXPECT_EQ(resize_call_count, 0);
63     EXPECT_EQ(resize_default_init_call_count, 0);
64     absl::strings_internal::STLStringResizeUninitialized(&rs, 237);
65     EXPECT_EQ(resize_call_count, 1);
66     EXPECT_EQ(resize_default_init_call_count, 0);
67     absl::strings_internal::STLStringResizeUninitializedAmortized(&rs, 1000);
68     EXPECT_EQ(resize_call_count, 2);
69     EXPECT_EQ(resize_default_init_call_count, 0);
70   }
71 
72   resize_call_count = 0;
73   resize_default_init_call_count = 0;
74   {
75     resize_default_init_string rus;
76 
77     EXPECT_EQ(resize_call_count, 0);
78     EXPECT_EQ(resize_default_init_call_count, 0);
79     EXPECT_TRUE(
80         absl::strings_internal::STLStringSupportsNontrashingResize(&rus));
81     EXPECT_EQ(resize_call_count, 0);
82     EXPECT_EQ(resize_default_init_call_count, 0);
83     absl::strings_internal::STLStringResizeUninitialized(&rus, 237);
84     EXPECT_EQ(resize_call_count, 0);
85     EXPECT_EQ(resize_default_init_call_count, 1);
86     absl::strings_internal::STLStringResizeUninitializedAmortized(&rus, 1000);
87     EXPECT_EQ(resize_call_count, 0);
88     EXPECT_EQ(resize_default_init_call_count, 2);
89   }
90 }
91 
TEST(ResizeUninit,Amortized)92 TEST(ResizeUninit, Amortized) {
93   std::string str;
94   size_t prev_cap = str.capacity();
95   int cap_increase_count = 0;
96   for (int i = 0; i < 1000; ++i) {
97     absl::strings_internal::STLStringResizeUninitializedAmortized(&str, i);
98     size_t new_cap = str.capacity();
99     if (new_cap > prev_cap) ++cap_increase_count;
100     prev_cap = new_cap;
101   }
102   EXPECT_LT(cap_increase_count, 50);
103 }
104 
105 }  // namespace
106