aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDedicatedTest/include/protobuf/inlined_string_field_unittest.cc
blob: f97037d1f703368853d2a780524d8528b912de0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <inlined_string_field.h>

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <string>
#include <vector>

#include <stubs/logging.h>
#include <stubs/common.h>
#include <io/coded_stream.h>
#include <io/zero_copy_stream_impl.h>
#include <arenastring.h>
#include <gtest/gtest.h>
#include <stubs/strutil.h>


namespace google {
namespace protobuf {

using internal::ArenaStringPtr;
using internal::InlinedStringField;

static std::string WrapString(const char* value) { return value; }

namespace {

const uint32 kMask = ~0x00000001u;
const uint32 kMask1 = ~0x00000004u;
const uint32 kMask2 = ~0x00000020u;

TEST(InlinedStringFieldTest, SetOnHeap) {
  InlinedStringField field;
  uint32 donating_states = 0;
  const std::string kDefaultValue = "default";
  field.Set(&kDefaultValue, WrapString("Test short"), nullptr, false,
            &donating_states, kMask);
  EXPECT_EQ(std::string("Test short"), field.Get());
  field.Set(&kDefaultValue, WrapString("Test long long long long value"),
            nullptr, false, &donating_states, kMask);
  EXPECT_EQ(std::string("Test long long long long value"), field.Get());
}

TEST(InlinedStringFieldTest, SetRvalueOnHeap) {
  InlinedStringField field;
  uint32 donating_states = 0;
  std::string string_moved = "Moved long long long long string 1";
  field.Set(nullptr, std::move(string_moved), nullptr, false, &donating_states,
            kMask);
  EXPECT_EQ("Moved long long long long string 1", field.Get());
  EXPECT_EQ(donating_states & ~kMask1, 0);
}

TEST(InlinedStringFieldTest, UnsafeMutablePointerThenRelease) {
  InlinedStringField field;
  const std::string kDefaultValue = "default";
  std::string* mut = field.UnsafeMutablePointer();
  // The address of inlined string doesn't change behind the scene.
  EXPECT_EQ(mut, field.UnsafeMutablePointer());
  EXPECT_EQ(mut, &field.Get());
  EXPECT_EQ(std::string(""), *mut);
  *mut = "Test long long long long value";  // ensure string allocates
  EXPECT_EQ(std::string("Test long long long long value"), field.Get());

  std::string* released = field.ReleaseNonDefaultNoArena(&kDefaultValue);
  EXPECT_EQ("Test long long long long value", *released);
  // Default value is ignored.
  EXPECT_EQ("", field.Get());
  delete released;
}

// When donating mechanism is enabled:
// - Initially, the string is donated.
// - After lvalue Set: the string is still donated.
// - After Mutable: the string is undonated. The data buffer of the string is a
// new buffer on the heap.
TEST(InlinedStringFieldTest, ArenaSetThenMutable) {
  Arena arena;
  auto* field_arena = Arena::CreateMessage<InlinedStringField>(&arena);
  uint32 donating_states = ~0u;
  const std::string kDefaultValue = "default";
  field_arena->Set(&kDefaultValue, WrapString("Test short"), &arena,
                   /*donated=*/true, &donating_states, kMask1);
  EXPECT_EQ(std::string("Test short"), field_arena->Get());
  field_arena->Set(&kDefaultValue, "Test long long long long value", &arena,
                   /*donated=*/(donating_states & ~kMask1) != 0,
                   &donating_states, kMask1);
  EXPECT_EQ(std::string("Test long long long long value"), field_arena->Get());
#if GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
  EXPECT_NE(donating_states & ~kMask1, 0);  // donate.
#endif

  const std::string* old_string = &field_arena->Get();
  const char* old_data = old_string->data();
  (void)old_data;
  std::string* mut = field_arena->Mutable(
      ArenaStringPtr::EmptyDefault{}, &arena,
      /*donated=*/(donating_states & ~kMask1) != 0, &donating_states, kMask1);
  EXPECT_EQ(old_string, mut);
#if GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
  EXPECT_EQ(donating_states & ~kMask1, 0);
  EXPECT_NE(old_data, mut->data());  // The data buffer of the mutated string is
                                     // a new buffer on the heap.
#endif
  *mut = "Test an even longer long long long long value";
  EXPECT_EQ(std::string("Test an even longer long long long long value"),
            field_arena->Get());
  EXPECT_EQ(&field_arena->Get(), mut);
}

// Release doesn't change the donating state.
// When donating mechanism is enabled:
// - Initially, the string is donated.
// - Then lvalue Set: the string is still donated.
// - Then Release: the string is cleared, and still donated.
// - Then Mutable: the string is undonated.
// - Then Release: the string is still undonated.
TEST(InlinedStringFieldTest, ArenaRelease) {
  Arena arena;
  auto* field_arena = Arena::CreateMessage<InlinedStringField>(&arena);
  uint32 donating_states = ~0u;
  const std::string kDefaultValue = "default";
  field_arena->Set(&kDefaultValue, WrapString("Test short"), &arena,
                   /*donated=*/true, &donating_states, kMask1);
  std::string* released = field_arena->Release(
      &kDefaultValue, &arena, /*donated=*/(donating_states & ~kMask1) != 0);
  EXPECT_EQ("Test short", *released);
  EXPECT_EQ("", field_arena->Get());
  EXPECT_NE(released, &field_arena->Get());
#if GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
  EXPECT_NE(donating_states & ~kMask1, 0);  // still donated.
#endif
  delete released;

  std::string* mut = field_arena->Mutable(
      ArenaStringPtr::EmptyDefault{}, &arena,
      /*donated=*/(donating_states & ~kMask1) != 0u, &donating_states, kMask1);
  *mut = "Test long long long long value";
  std::string* released2 =
      field_arena->Release(&kDefaultValue, &arena,
                           /*donated=*/(donating_states & ~kMask1) != 0);
  EXPECT_EQ("Test long long long long value", *released2);
  EXPECT_EQ("", field_arena->Get());
#if GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
  EXPECT_EQ(donating_states & ~kMask1, 0);  // undonated.
#endif
  delete released2;
}

// Rvalue Set always undoantes a donated string.
TEST(InlinedStringFieldTest, SetRvalueArena) {
  Arena arena;
  auto* field1_arena = Arena::CreateMessage<InlinedStringField>(&arena);
  auto* field2_arena = Arena::CreateMessage<InlinedStringField>(&arena);
  uint32 donating_states = ~0u;
  const std::string kDefaultValue = "default";

  std::string string_moved = "Moved long long long long string 1";
  field1_arena->Set(nullptr, std::move(string_moved), &arena, true,
                    &donating_states, kMask1);
  EXPECT_EQ("Moved long long long long string 1", field1_arena->Get());
#if GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
  EXPECT_EQ(donating_states & ~kMask1, 0);  // Undonate.
#endif

  field2_arena->Set(nullptr, std::string("string 2 on heap"), &arena, true,
                    &donating_states, kMask);
  EXPECT_EQ("string 2 on heap", field2_arena->Get());
#if GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
  EXPECT_EQ(donating_states & ~kMask, 0);  // Undonated.
#endif
}

// Tests SetAllocated for non-arena string fields and arena string fields.
TEST(InlinedStringFieldTest, SetAllocated) {
  InlinedStringField field;
  Arena arena;
  auto* field1_arena = Arena::CreateMessage<InlinedStringField>(&arena);
  auto* field2_arena = Arena::CreateMessage<InlinedStringField>(&arena);
  uint32 donating_states = ~0u;
  const std::string kDefaultValue = "default";

  // The string in field is on heap.
  field.Set(&kDefaultValue, WrapString("String on heap"), nullptr, false,
            &donating_states, kMask);
  auto* allocated = new std::string("Allocated string on heap");
  field.SetAllocatedNoArena(&kDefaultValue, allocated);
  EXPECT_EQ("Allocated string on heap", field.Get());

  // The string in field1_arena is on arena (aka. donated).
  field1_arena->Set(&kDefaultValue, WrapString("String 1 on arena"), &arena,
                    true, &donating_states, kMask1);
  *field1_arena->Mutable(ArenaStringPtr::EmptyDefault{}, &arena,
                         (donating_states & ~kMask1) != 0, &donating_states,
                         kMask1) = "Mutated string 1 is now on heap long long";
  // After Mutable, the string is undonated.
  allocated = new std::string("Allocated string on heap long long long");
  field1_arena->SetAllocated(&kDefaultValue, allocated, &arena,
                             (donating_states & ~kMask1) != 0, &donating_states,
                             kMask1);
  EXPECT_EQ("Allocated string on heap long long long", field1_arena->Get());
#if GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
  EXPECT_EQ(donating_states & ~kMask1, 0);  // Still undonated.
#endif

  // The string in field2_arena is on arena (aka. donated).
  field2_arena->Set(&kDefaultValue, WrapString("String 2 on arena long long"),
                    &arena, true, &donating_states,
                    kMask2);  // Still donated.
  allocated = new std::string("Allocated string on heap long long long 2");
  field2_arena->SetAllocated(&kDefaultValue, allocated, &arena, true,
                             &donating_states, kMask2);
  EXPECT_EQ("Allocated string on heap long long long 2", field2_arena->Get());
#if GOOGLE_PROTOBUF_INTERNAL_DONATE_STEAL_INLINE
  EXPECT_EQ(donating_states & ~kMask2, 0);  // Undonated.
#endif
}

// Tests Swap for non-arena string fields and arena string fields.
TEST(InlinedStringFieldTest, Swap) {
  // Swap should only be called when the from and to are on the same arena.
  InlinedStringField field1;
  InlinedStringField field2;
  uint32 donating_states = 0;
  Arena arena;
  auto* field1_arena = Arena::CreateMessage<InlinedStringField>(&arena);
  auto* field2_arena = Arena::CreateMessage<InlinedStringField>(&arena);
  uint32 donating_states_1 = ~0u;
  uint32 donating_states_2 = ~0u;
  const std::string kDefaultValue = "default";

  const std::string& string1_heap = "String 1 on heap";
  const std::string& string2_heap = "String 2 on heap long long long long";
  const std::string& string1_arena = "String 1 on arena";
  const std::string& string2_arena = "String 2 on arena long long long long";
  field1.SetNoArena(&kDefaultValue, string1_heap);
  field2.SetNoArena(&kDefaultValue, string2_heap);
  field1_arena->Set(&kDefaultValue, string1_arena, &arena, true,
                    &donating_states_1, kMask1);
  field2_arena->Set(&kDefaultValue, string2_arena, &arena, true,
                    &donating_states_2, kMask1);

  field1.Swap(&field2, &kDefaultValue, nullptr, false, false, &donating_states,
              &donating_states_2, kMask);
  field1_arena->Swap(field2_arena, &kDefaultValue, &arena, true, true,
                     &donating_states_1, &donating_states_2, kMask1);
  EXPECT_EQ(field1.Get(), string2_heap);
  EXPECT_EQ(field2.Get(), string1_heap);
  EXPECT_EQ(field1_arena->Get(), string2_arena);
  EXPECT_EQ(field2_arena->Get(), string1_arena);
}

}  // namespace
}  // namespace protobuf
}  // namespace google