• 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__anonc379f1040111::resizable_string26   size_t size() const { return 0; }
operator []__anonc379f1040111::resizable_string27   char& operator[](size_t) {
28     static char c = '\0';
29     return c;
30   }
resize__anonc379f1040111::resizable_string31   void resize(size_t) { resize_call_count += 1; }
32 };
33 
34 int resize_default_init_call_count = 0;
35 
36 // A mock string class whose only purpose is to track how many times its
37 // resize() and __resize_default_init() methods have been called.
38 struct resize_default_init_string {
size__anonc379f1040111::resize_default_init_string39   size_t size() const { return 0; }
operator []__anonc379f1040111::resize_default_init_string40   char& operator[](size_t) {
41     static char c = '\0';
42     return c;
43   }
resize__anonc379f1040111::resize_default_init_string44   void resize(size_t) { resize_call_count += 1; }
__resize_default_init__anonc379f1040111::resize_default_init_string45   void __resize_default_init(size_t) { resize_default_init_call_count += 1; }
46 };
47 
TEST(ResizeUninit,WithAndWithout)48 TEST(ResizeUninit, WithAndWithout) {
49   resize_call_count = 0;
50   resize_default_init_call_count = 0;
51   {
52     resizable_string rs;
53 
54     EXPECT_EQ(resize_call_count, 0);
55     EXPECT_EQ(resize_default_init_call_count, 0);
56     EXPECT_FALSE(
57         absl::strings_internal::STLStringSupportsNontrashingResize(&rs));
58     EXPECT_EQ(resize_call_count, 0);
59     EXPECT_EQ(resize_default_init_call_count, 0);
60     absl::strings_internal::STLStringResizeUninitialized(&rs, 237);
61     EXPECT_EQ(resize_call_count, 1);
62     EXPECT_EQ(resize_default_init_call_count, 0);
63   }
64 
65   resize_call_count = 0;
66   resize_default_init_call_count = 0;
67   {
68     resize_default_init_string rus;
69 
70     EXPECT_EQ(resize_call_count, 0);
71     EXPECT_EQ(resize_default_init_call_count, 0);
72     EXPECT_TRUE(
73         absl::strings_internal::STLStringSupportsNontrashingResize(&rus));
74     EXPECT_EQ(resize_call_count, 0);
75     EXPECT_EQ(resize_default_init_call_count, 0);
76     absl::strings_internal::STLStringResizeUninitialized(&rus, 237);
77     EXPECT_EQ(resize_call_count, 0);
78     EXPECT_EQ(resize_default_init_call_count, 1);
79   }
80 }
81 
82 }  // namespace
83