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 <string>
36 #include <memory>
37 #ifndef _SHARED_PTR_H
38 #include <google/protobuf/stubs/shared_ptr.h>
39 #endif
40 #include <cstdlib>
41
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <gtest/gtest.h>
45
46 namespace google {
47 using google::protobuf::internal::ArenaString;
48 using google::protobuf::internal::ArenaStringPtr;
49
50 namespace protobuf {
51
52
WrapString(const char * value)53 static string WrapString(const char* value) {
54 return value;
55 }
56
57 // Test ArenaStringPtr with arena == NULL.
TEST(ArenaStringPtrTest,ArenaStringPtrOnHeap)58 TEST(ArenaStringPtrTest, ArenaStringPtrOnHeap) {
59 ArenaStringPtr field;
60 ::std::string default_value = "default";
61 field.UnsafeSetDefault(&default_value);
62 EXPECT_EQ(string("default"), field.Get(&default_value));
63 field.Set(&default_value, WrapString("Test short"), NULL);
64 EXPECT_EQ(string("Test short"), field.Get(&default_value));
65 field.Set(&default_value, WrapString("Test long long long long value"), NULL);
66 EXPECT_EQ(string("Test long long long long value"), field.Get(&default_value));
67 field.Set(&default_value, string(""), NULL);
68 field.Destroy(&default_value, NULL);
69
70 ArenaStringPtr field2;
71 field2.UnsafeSetDefault(&default_value);
72 ::std::string* mut = field2.Mutable(&default_value, NULL);
73 EXPECT_EQ(mut, field2.Mutable(&default_value, NULL));
74 EXPECT_EQ(mut, &field2.Get(&default_value));
75 EXPECT_NE(&default_value, mut);
76 EXPECT_EQ(string("default"), *mut);
77 *mut = "Test long long long long value"; // ensure string allocates storage
78 EXPECT_EQ(string("Test long long long long value"), field2.Get(&default_value));
79 field2.Destroy(&default_value, NULL);
80 }
81
TEST(ArenaStringPtrTest,ArenaStringPtrOnArena)82 TEST(ArenaStringPtrTest, ArenaStringPtrOnArena) {
83 google::protobuf::Arena arena;
84 ArenaStringPtr field;
85 ::std::string default_value = "default";
86 field.UnsafeSetDefault(&default_value);
87 EXPECT_EQ(string("default"), field.Get(&default_value));
88 field.Set(&default_value, WrapString("Test short"), &arena);
89 EXPECT_EQ(string("Test short"), field.Get(&default_value));
90 field.Set(&default_value, WrapString("Test long long long long value"), &arena);
91 EXPECT_EQ(string("Test long long long long value"),
92 field.Get(&default_value));
93 field.Set(&default_value, string(""), &arena);
94 field.Destroy(&default_value, &arena);
95
96 ArenaStringPtr field2;
97 field2.UnsafeSetDefault(&default_value);
98 ::std::string* mut = field2.Mutable(&default_value, &arena);
99 EXPECT_EQ(mut, field2.Mutable(&default_value, &arena));
100 EXPECT_EQ(mut, &field2.Get(&default_value));
101 EXPECT_NE(&default_value, mut);
102 EXPECT_EQ(string("default"), *mut);
103 *mut = "Test long long long long value"; // ensure string allocates storage
104 EXPECT_EQ(string("Test long long long long value"),
105 field2.Get(&default_value));
106 field2.Destroy(&default_value, &arena);
107 }
108
109 } // namespace protobuf
110 } // namespace google
111