• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 //     * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //     * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 //     * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Based on mvels@'s frankenstring.
32 
33 #include <google/protobuf/arenastring.h>
34 
35 #include <algorithm>
36 #include <cstdlib>
37 #include <memory>
38 #include <string>
39 #include <vector>
40 
41 #include <google/protobuf/stubs/logging.h>
42 #include <google/protobuf/stubs/common.h>
43 #include <google/protobuf/io/coded_stream.h>
44 #include <google/protobuf/io/zero_copy_stream_impl.h>
45 #include <gtest/gtest.h>
46 
47 
48 namespace google {
49 namespace protobuf {
50 
51 using internal::ArenaStringPtr;
52 
53 
WrapString(const char * value)54 static std::string WrapString(const char* value) { return value; }
55 
56 // Test ArenaStringPtr with arena == NULL.
TEST(ArenaStringPtrTest,ArenaStringPtrOnHeap)57 TEST(ArenaStringPtrTest, ArenaStringPtrOnHeap) {
58   ArenaStringPtr field;
59   std::string default_value = "default";
60   field.UnsafeSetDefault(&default_value);
61   EXPECT_EQ(std::string("default"), field.Get());
62   field.Set(&default_value, WrapString("Test short"), NULL);
63   EXPECT_EQ(std::string("Test short"), field.Get());
64   field.Set(&default_value, WrapString("Test long long long long value"), NULL);
65   EXPECT_EQ(std::string("Test long long long long value"), field.Get());
66   field.Set(&default_value, std::string(""), NULL);
67   field.Destroy(&default_value, NULL);
68 
69   ArenaStringPtr field2;
70   field2.UnsafeSetDefault(&default_value);
71   std::string* mut = field2.Mutable(&default_value, NULL);
72   EXPECT_EQ(mut, field2.Mutable(&default_value, NULL));
73   EXPECT_EQ(mut, &field2.Get());
74   EXPECT_NE(&default_value, mut);
75   EXPECT_EQ(std::string("default"), *mut);
76   *mut = "Test long long long long value";  // ensure string allocates storage
77   EXPECT_EQ(std::string("Test long long long long value"), field2.Get());
78   field2.Destroy(&default_value, NULL);
79 }
80 
TEST(ArenaStringPtrTest,ArenaStringPtrOnArena)81 TEST(ArenaStringPtrTest, ArenaStringPtrOnArena) {
82   Arena arena;
83   ArenaStringPtr field;
84   std::string default_value = "default";
85   field.UnsafeSetDefault(&default_value);
86   EXPECT_EQ(std::string("default"), field.Get());
87   field.Set(&default_value, WrapString("Test short"), &arena);
88   EXPECT_EQ(std::string("Test short"), field.Get());
89   field.Set(&default_value, WrapString("Test long long long long value"),
90             &arena);
91   EXPECT_EQ(std::string("Test long long long long value"), field.Get());
92   field.Set(&default_value, std::string(""), &arena);
93   field.Destroy(&default_value, &arena);
94 
95   ArenaStringPtr field2;
96   field2.UnsafeSetDefault(&default_value);
97   std::string* mut = field2.Mutable(&default_value, &arena);
98   EXPECT_EQ(mut, field2.Mutable(&default_value, &arena));
99   EXPECT_EQ(mut, &field2.Get());
100   EXPECT_NE(&default_value, mut);
101   EXPECT_EQ(std::string("default"), *mut);
102   *mut = "Test long long long long value";  // ensure string allocates storage
103   EXPECT_EQ(std::string("Test long long long long value"), field2.Get());
104   field2.Destroy(&default_value, &arena);
105 }
106 
TEST(ArenaStringPtrTest,ArenaStringPtrOnArenaNoSSO)107 TEST(ArenaStringPtrTest, ArenaStringPtrOnArenaNoSSO) {
108   Arena arena;
109   ArenaStringPtr field;
110   std::string default_value = "default";
111   field.UnsafeSetDefault(&default_value);
112   EXPECT_EQ(std::string("default"), field.Get());
113 
114   // Avoid triggering the SSO optimization by setting the string to something
115   // larger than the internal buffer.
116   field.Set(&default_value, WrapString("Test long long long long value"),
117             &arena);
118   EXPECT_EQ(std::string("Test long long long long value"), field.Get());
119   field.Set(&default_value, std::string(""), &arena);
120   field.Destroy(&default_value, &arena);
121 
122   ArenaStringPtr field2;
123   field2.UnsafeSetDefault(&default_value);
124   std::string* mut = field2.Mutable(&default_value, &arena);
125   EXPECT_EQ(mut, field2.Mutable(&default_value, &arena));
126   EXPECT_EQ(mut, &field2.Get());
127   EXPECT_NE(&default_value, mut);
128   EXPECT_EQ(std::string("default"), *mut);
129   *mut = "Test long long long long value";  // ensure string allocates storage
130   EXPECT_EQ(std::string("Test long long long long value"), field2.Get());
131   field2.Destroy(&default_value, &arena);
132 }
133 
134 
135 }  // namespace protobuf
136 }  // namespace google
137