blob: 1f3de4fa241275c6e69770a9cd0e565a059cafb7 [file] [log] [blame]
Feng Xiaof157a562014-11-14 11:50:31 -08001// 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
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070031#include <algorithm>
32#include <google/protobuf/stubs/hash.h>
Feng Xiaof157a562014-11-14 11:50:31 -080033#include <map>
34#include <memory>
35#ifndef _SHARED_PTR_H
36#include <google/protobuf/stubs/shared_ptr.h>
37#endif
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070038#include <set>
Feng Xiaof157a562014-11-14 11:50:31 -080039#include <sstream>
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070040#include <vector>
Feng Xiaof157a562014-11-14 11:50:31 -080041
42#include <google/protobuf/stubs/casts.h>
Feng Xiaoeee38b02015-08-22 18:25:48 -070043#include <google/protobuf/stubs/logging.h>
Feng Xiaof157a562014-11-14 11:50:31 -080044#include <google/protobuf/stubs/common.h>
Feng Xiaoeee38b02015-08-22 18:25:48 -070045#include <google/protobuf/stubs/scoped_ptr.h>
Feng Xiaof157a562014-11-14 11:50:31 -080046#include <google/protobuf/stubs/stringprintf.h>
47#include <google/protobuf/testing/file.h>
Jisi Liu885b6122015-02-28 14:51:22 -080048#include <google/protobuf/arena_test_util.h>
Feng Xiaof157a562014-11-14 11:50:31 -080049#include <google/protobuf/map_proto2_unittest.pb.h>
50#include <google/protobuf/map_unittest.pb.h>
51#include <google/protobuf/map_test_util.h>
52#include <google/protobuf/test_util.h>
53#include <google/protobuf/unittest.pb.h>
54#include <google/protobuf/descriptor.pb.h>
55#include <google/protobuf/descriptor.h>
56#include <google/protobuf/descriptor_database.h>
57#include <google/protobuf/dynamic_message.h>
58#include <google/protobuf/map.h>
59#include <google/protobuf/map_field_inl.h>
60#include <google/protobuf/message.h>
61#include <google/protobuf/reflection.h>
62#include <google/protobuf/reflection_ops.h>
63#include <google/protobuf/text_format.h>
64#include <google/protobuf/wire_format.h>
65#include <google/protobuf/wire_format_lite_inl.h>
66#include <google/protobuf/io/coded_stream.h>
67#include <google/protobuf/io/tokenizer.h>
68#include <google/protobuf/io/zero_copy_stream_impl.h>
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070069#include <google/protobuf/util/time_util.h>
Feng Xiaof157a562014-11-14 11:50:31 -080070#include <google/protobuf/stubs/strutil.h>
71#include <google/protobuf/stubs/substitute.h>
72#include <google/protobuf/testing/googletest.h>
73#include <gtest/gtest.h>
74
75namespace google {
76
77using google::protobuf::unittest::ForeignMessage;
78using google::protobuf::unittest::TestAllTypes;
79using google::protobuf::unittest::TestMap;
Feng Xiaoeee38b02015-08-22 18:25:48 -070080using google::protobuf::unittest::TestRecursiveMapMessage;
Feng Xiaof157a562014-11-14 11:50:31 -080081
82namespace protobuf {
83namespace internal {
84
85// Map API Test =====================================================
86
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070087// Parameterized tests on whether to use old style maps.
88class MapImplTest : public testing::TestWithParam<bool> {
Feng Xiaof157a562014-11-14 11:50:31 -080089 protected:
90 MapImplTest()
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070091 : map_ptr_(new Map<int32, int32>(GetParam())),
Feng Xiaof157a562014-11-14 11:50:31 -080092 map_(*map_ptr_),
93 const_map_(*map_ptr_) {
94 EXPECT_TRUE(map_.empty());
95 EXPECT_EQ(0, map_.size());
96 }
Tres Seaver41136852014-11-18 11:58:21 -050097 ~MapImplTest() {}
Feng Xiaof157a562014-11-14 11:50:31 -080098
99 void ExpectSingleElement(int32 key, int32 value) {
100 EXPECT_FALSE(map_.empty());
101 EXPECT_EQ(1, map_.size());
102 ExpectElement(key, value);
103 }
104
105 void ExpectElements(const std::map<int32, int32>& map) {
106 EXPECT_FALSE(map_.empty());
107 EXPECT_EQ(map.size(), map_.size());
108 for (std::map<int32, int32>::const_iterator it = map.begin();
109 it != map.end(); ++it) {
110 ExpectElement(it->first, it->second);
111 }
112 }
113
114 void ExpectElement(int32 key, int32 value) {
115 // Test map size is correct.
116 EXPECT_EQ(value, map_[key]);
117 EXPECT_EQ(1, map_.count(key));
118
119 // Check mutable at and find work correctly.
120 EXPECT_EQ(value, map_.at(key));
121 Map<int32, int32>::iterator it = map_.find(key);
122
123 // interator dereferenceable
124 EXPECT_EQ(key, (*it).first);
125 EXPECT_EQ(value, (*it).second);
126 EXPECT_EQ(key, it->first);
127 EXPECT_EQ(value, it->second);
128
129 // iterator mutable
130 ((*it).second) = value + 1;
131 EXPECT_EQ(value + 1, map_[key]);
132 ((*it).second) = value;
133 EXPECT_EQ(value, map_[key]);
134
135 it->second = value + 1;
136 EXPECT_EQ(value + 1, map_[key]);
137 it->second = value;
138 EXPECT_EQ(value, map_[key]);
139
140 // copy constructor
141 Map<int32, int32>::iterator it_copy = it;
142 EXPECT_EQ(key, it_copy->first);
143 EXPECT_EQ(value, it_copy->second);
144
145 // Immutable API ================================================
146
147 // Check immutable at and find work correctly.
148 EXPECT_EQ(value, const_map_.at(key));
149 Map<int32, int32>::const_iterator const_it = const_map_.find(key);
150
151 // interator dereferenceable
152 EXPECT_EQ(key, (*const_it).first);
153 EXPECT_EQ(value, (*const_it).second);
154 EXPECT_EQ(key, const_it->first);
155 EXPECT_EQ(value, const_it->second);
156
157 // copy constructor
158 Map<int32, int32>::const_iterator const_it_copy = const_it;
159 EXPECT_EQ(key, const_it_copy->first);
160 EXPECT_EQ(value, const_it_copy->second);
161 }
162
163 google::protobuf::scoped_ptr<Map<int32, int32> > map_ptr_;
164 Map<int32, int32>& map_;
165 const Map<int32, int32>& const_map_;
166};
167
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700168TEST_P(MapImplTest, OperatorBracket) {
Feng Xiaof157a562014-11-14 11:50:31 -0800169 int32 key = 0;
170 int32 value1 = 100;
171 int32 value2 = 101;
172
173 EXPECT_EQ(0, map_[key]);
174
175 map_[key] = value1;
176 ExpectSingleElement(key, value1);
177
178 map_[key] = value2;
179 ExpectSingleElement(key, value2);
180}
181
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700182TEST_P(MapImplTest, OperatorBracketNonExist) {
Feng Xiaof157a562014-11-14 11:50:31 -0800183 int32 key = 0;
184 int32 default_value = 0;
185
186 EXPECT_EQ(default_value, map_[key]);
187 ExpectSingleElement(key, default_value);
188}
189
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700190TEST_P(MapImplTest, MutableAt) {
Feng Xiaof157a562014-11-14 11:50:31 -0800191 int32 key = 0;
192 int32 value1 = 100;
193 int32 value2 = 101;
194
195 map_[key] = value1;
196 ExpectSingleElement(key, value1);
197
198 map_.at(key) = value2;
199 ExpectSingleElement(key, value2);
200}
201
Feng Xiao58dfce92014-12-03 12:12:17 -0800202#ifdef PROTOBUF_HAS_DEATH_TEST
Jisi Liu885b6122015-02-28 14:51:22 -0800203
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700204TEST_P(MapImplTest, MutableAtNonExistDeathTest) {
Feng Xiaof157a562014-11-14 11:50:31 -0800205 EXPECT_DEATH(map_.at(0), "");
206}
207
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700208TEST_P(MapImplTest, ImmutableAtNonExistDeathTest) {
Feng Xiaof157a562014-11-14 11:50:31 -0800209 EXPECT_DEATH(const_map_.at(0), "");
210}
Jisi Liu885b6122015-02-28 14:51:22 -0800211
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700212TEST_P(MapImplTest, UsageErrors) {
Feng Xiaoeee38b02015-08-22 18:25:48 -0700213 MapKey key;
214 key.SetInt64Value(1);
215 EXPECT_DEATH(key.GetUInt64Value(),
216 "Protocol Buffer map usage error:\n"
217 "MapKey::GetUInt64Value type does not match\n"
218 " Expected : uint64\n"
219 " Actual : int64");
220
221 MapValueRef value;
222 EXPECT_DEATH(value.SetFloatValue(0.1),
223 "Protocol Buffer map usage error:\n"
224 "MapValueRef::type MapValueRef is not initialized.");
225}
226
Feng Xiao58dfce92014-12-03 12:12:17 -0800227#endif // PROTOBUF_HAS_DEATH_TEST
Feng Xiaof157a562014-11-14 11:50:31 -0800228
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700229TEST_P(MapImplTest, CountNonExist) {
Feng Xiaof157a562014-11-14 11:50:31 -0800230 EXPECT_EQ(0, map_.count(0));
231}
232
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700233TEST_P(MapImplTest, MutableFindNonExist) {
Feng Xiaof157a562014-11-14 11:50:31 -0800234 EXPECT_TRUE(map_.end() == map_.find(0));
235}
236
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700237TEST_P(MapImplTest, ImmutableFindNonExist) {
Feng Xiaof157a562014-11-14 11:50:31 -0800238 EXPECT_TRUE(const_map_.end() == const_map_.find(0));
239}
240
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700241TEST_P(MapImplTest, ConstEnd) {
Feng Xiaof157a562014-11-14 11:50:31 -0800242 EXPECT_TRUE(const_map_.end() == const_map_.cend());
243}
244
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700245TEST_P(MapImplTest, GetReferenceFromIterator) {
Feng Xiaof157a562014-11-14 11:50:31 -0800246 for (int i = 0; i < 10; i++) {
247 map_[i] = i;
248 }
249
250 for (Map<int32, int32>::const_iterator it = map_.cbegin();
251 it != map_.cend();) {
252 Map<int32, int32>::const_reference entry = *it++;
253 EXPECT_EQ(entry.first, entry.second);
254 }
255
256 for (Map<int32, int32>::const_iterator it = const_map_.begin();
257 it != const_map_.end();) {
258 Map<int32, int32>::const_reference entry = *it++;
259 EXPECT_EQ(entry.first, entry.second);
260 }
261
262 for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) {
263 Map<int32, int32>::reference entry = *it++;
264 EXPECT_EQ(entry.first + 1, ++entry.second);
265 }
266}
267
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700268TEST_P(MapImplTest, IteratorBasic) {
Feng Xiao99aa0f92014-11-20 16:18:53 -0800269 map_[0] = 0;
270
271 // Default constructible (per forward iterator requirements).
272 Map<int, int>::const_iterator cit;
273 Map<int, int>::iterator it;
274
275 it = map_.begin();
276 cit = it; // Converts to const_iterator
277
278 // Can compare between them.
279 EXPECT_TRUE(it == cit);
280 EXPECT_FALSE(cit != it);
281
282 // Pre increment.
283 EXPECT_FALSE(it == ++cit);
284
285 // Post increment.
286 EXPECT_FALSE(it++ == cit);
287 EXPECT_TRUE(it == cit);
288}
289
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700290template <typename Iterator>
291static int64 median(Iterator i0, Iterator i1) {
292 vector<int64> v(i0, i1);
293 std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end());
294 return v[v.size() / 2];
295}
296
297static int64 Now() {
298 return google::protobuf::util::TimeUtil::TimestampToNanoseconds(
299 google::protobuf::util::TimeUtil::GetCurrentTime());
300}
301
302// A naive begin() implementation will cause begin() to get slower and slower
303// if one erases elements at the "front" of the hash map, and we'd like to
304// avoid that, as std::unordered_map does.
305TEST_P(MapImplTest, BeginIsFast) {
306 if (GetParam()) return;
307 Map<int32, int32> map(false); // This test uses new-style maps only.
308 const int kTestSize = 250000;
309 for (int i = 0; i < kTestSize; i++) {
310 map[i] = i;
311 }
312 vector<int64> times;
313 // We're going to do map.erase(map.begin()) over and over again. But,
314 // just in case one iteration is fast compared to the granularity of
315 // our time keeping, we measure kChunkSize iterations per outer-loop iter.
316 const int kChunkSize = 1000;
317 GOOGLE_CHECK_EQ(kTestSize % kChunkSize, 0);
318 do {
319 const int64 start = Now();
320 for (int i = 0; i < kChunkSize; i++) {
321 map.erase(map.begin());
322 }
323 const int64 end = Now();
324 if (end > start) {
325 times.push_back(end - start);
326 }
327 } while (!map.empty());
328 if (times.size() < .99 * kTestSize / kChunkSize) {
329 GOOGLE_LOG(WARNING) << "Now() isn't helping us measure time";
330 return;
331 }
332 int64 x0 = median(times.begin(), times.begin() + 9);
333 int64 x1 = median(times.begin() + times.size() - 9, times.end());
334 GOOGLE_LOG(INFO) << "x0=" << x0 << ", x1=" << x1;
335 // x1 will greatly exceed x0 if the code we just executed took O(n^2) time.
336 // And we'll probably time out and never get here. So, this test is
337 // intentionally loose: we check that x0 and x1 are within a factor of 8.
338 EXPECT_GE(x1, x0 / 8);
339 EXPECT_GE(x0, x1 / 8);
340}
341
342// Try to create kTestSize keys that will land in just a few buckets, and
343// time the insertions, to get a rough estimate of whether an O(n^2) worst case
344// was triggered. This test is a hacky, but probably better than nothing.
345TEST_P(MapImplTest, HashFlood) {
346 const int kTestSize = 1024; // must be a power of 2
347 std::set<int> s;
348 for (int i = 0; s.size() < kTestSize; i++) {
349 if ((map_.hash_function()(i) & (kTestSize - 1)) < 3) {
350 s.insert(i);
351 }
352 }
353 // Create hash table with kTestSize entries that hash flood a table with
354 // 1024 (or 512 or 2048 or ...) entries. This assumes that map_ uses powers
355 // of 2 for table sizes, and that it's sufficient to "flood" with respect to
356 // the low bits of the output of map_.hash_function().
357 vector<int64> times;
358 std::set<int>::iterator it = s.begin();
359 int count = 0;
360 do {
361 const int64 start = Now();
362 map_[*it] = 0;
363 const int64 end = Now();
364 if (end > start) {
365 times.push_back(end - start);
366 }
367 ++count;
368 ++it;
369 } while (it != s.end());
370 if (times.size() < .99 * count) return;
371 int64 x0 = median(times.begin(), times.begin() + 9);
372 int64 x1 = median(times.begin() + times.size() - 9, times.end());
373 // x1 will greatly exceed x0 if the code we just executed took O(n^2) time.
374 // But we want to allow O(n log n). A factor of 20 should be generous enough.
375 EXPECT_LE(x1, x0 * 20);
376}
377
378// Arbitrary odd integers for creating test data.
379static int k0 = 812398771;
380static int k1 = 1312938717;
381static int k2 = 1321555333;
382
383template <typename T, typename U>
384static void TestValidityForAllKeysExcept(int key_to_avoid,
385 const T& check_map,
386 const U& map) {
387 typedef typename U::value_type value_type; // a key-value pair
388 for (typename U::const_iterator it = map.begin(); it != map.end(); ++it) {
389 const int key = it->first;
390 if (key == key_to_avoid) continue;
391 // All iterators relevant to this key, whether old (from check_map) or new,
392 // must point to the same memory. So, test pointer equality here.
393 const value_type* check_val = &*check_map.find(key)->second;
394 EXPECT_EQ(check_val, &*it);
395 EXPECT_EQ(check_val, &*map.find(key));
396 }
397}
398
399// EXPECT i0 and i1 to be the same. Advancing them should have the same effect,
400// too.
401template <typename Iter>
402static void TestEqualIterators(Iter i0, Iter i1, Iter end) {
403 const int kMaxAdvance = 10;
404 for (int i = 0; i < kMaxAdvance; i++) {
405 EXPECT_EQ(i0 == end, i1 == end);
406 if (i0 == end) return;
407 EXPECT_EQ(&*i0, &*i1) << "iter " << i;
408 ++i0;
409 ++i1;
410 }
411}
412
413template <typename IteratorType>
414static void TestOldVersusNewIterator(int skip, Map<int, int>* m) {
415 const int initial_size = m->size();
416 IteratorType it = m->begin();
417 for (int i = 0; i < skip && it != m->end(); it++, i++) {}
418 if (it == m->end()) return;
419 const IteratorType old = it;
420 GOOGLE_LOG(INFO) << "skip=" << skip << ", old->first=" << old->first;
421 const int target_size =
422 initial_size < 100 ? initial_size * 5 : initial_size * 5 / 4;
423 for (int i = 0; m->size() <= target_size; i++) {
424 (*m)[i] = 0;
425 }
426 // Iterator 'old' should still work just fine despite the growth of *m.
427 const IteratorType after_growth = m->find(old->first);
428 TestEqualIterators<IteratorType>(old, after_growth, m->end());
429
430 // Now shrink the number of elements. Do this with a mix of erases and
431 // inserts to increase the chance that the hashtable will resize to a lower
432 // number of buckets. (But, in any case, the test is still useful.)
433 for (int i = 0; i < 2 * (target_size - initial_size); i++) {
434 if (i != old->first) {
435 m->erase(i);
436 }
437 if (((i ^ m->begin()->first) & 15) == 0) {
438 (*m)[i * 342] = i;
439 }
440 }
441 // Now, the table has grown and shrunk; test again.
442 TestEqualIterators<IteratorType>(old, m->find(old->first), m->end());
443 TestEqualIterators<IteratorType>(old, after_growth, m->end());
444}
445
446// Create and test an n-element Map, with emphasis on iterator correctness.
447static void StressTestIterators(int n, bool test_old_style_proto2_maps) {
448 GOOGLE_LOG(INFO) << "StressTestIterators " << n;
449 GOOGLE_CHECK_GT(n, 0);
450 // Create a random-looking map of size n. Use non-negative integer keys.
451 Map<int, int> m(test_old_style_proto2_maps);
452 uint32 frog = 123987 + n;
453 int last_key = 0;
454 int counter = 0;
455 while (m.size() < n) {
456 frog *= static_cast<uint32>(k0);
457 frog ^= frog >> 17;
458 frog += counter++;
459 last_key =
460 static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1;
461 GOOGLE_DCHECK_GE(last_key, 0);
462 m[last_key] = last_key ^ 1;
463 }
464 // Test it.
465 ASSERT_EQ(n, m.size());
466 // Create maps of pointers and iterators.
467 // These should remain valid even if we modify m.
468 hash_map<int, Map<int, int>::value_type*> mp(n);
469 hash_map<int, Map<int, int>::iterator> mi(n);
470 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
471 mp[it->first] = &*it;
472 mi[it->first] = it;
473 }
474 ASSERT_EQ(m.size(), mi.size());
475 ASSERT_EQ(m.size(), mp.size());
476 m.erase(last_key);
477 ASSERT_EQ(n - 1, m.size());
478 TestValidityForAllKeysExcept(last_key, mp, m);
479 TestValidityForAllKeysExcept(last_key, mi, m);
480
481 m[last_key] = 0;
482 ASSERT_EQ(n, m.size());
483 // Test old iterator vs new iterator, with table modification in between.
484 TestOldVersusNewIterator<Map<int, int>::const_iterator>(n % 3, &m);
485 TestOldVersusNewIterator<Map<int, int>::iterator>(n % (1 + (n / 40)), &m);
486 // Finally, ensure erase(iterator) doesn't reorder anything, becuase that is
487 // what its documentation says.
488 m[last_key] = m[last_key ^ 999] = 0;
489 vector<Map<int, int>::iterator> v;
490 v.reserve(m.size());
491 int position_of_last_key = 0;
492 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
493 if (it->first == last_key) {
494 position_of_last_key = v.size();
495 }
496 v.push_back(it);
497 }
498 ASSERT_EQ(m.size(), v.size());
499 const Map<int, int>::iterator erase_result = m.erase(m.find(last_key));
500 int index = 0;
501 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it, ++index) {
502 if (index == position_of_last_key) {
503 EXPECT_EQ(&*erase_result, &*v[++index]);
504 }
505 ASSERT_EQ(&*it, &*v[index]);
506 }
507}
508
509TEST_P(MapImplTest, IteratorInvalidation) {
510 // As multiple underlying hash_map implementations do not follow the
511 // validation requirement, the test is disabled for old-style maps.
512 if (GetParam()) return;
513 // Create a set of pseudo-random sizes to test.
514#ifndef NDEBUG
515 const int kMaxSizeToTest = 100 * 1000;
516#else
517 const int kMaxSizeToTest = 1000 * 1000;
518#endif
519 std::set<int> s;
520 int n = kMaxSizeToTest;
521 int frog = k1 + n;
522 while (n > 1 && s.size() < 25) {
523 s.insert(n);
524 n = static_cast<int>(n * 100 / (101.0 + (frog & 63)));
525 frog *= k2;
526 frog ^= frog >> 17;
527 }
528 // Ensure we test a few small sizes.
529 s.insert(1);
530 s.insert(2);
531 s.insert(3);
532 // Now, the real work.
533 for (std::set<int>::iterator i = s.begin(); i != s.end(); ++i) {
534 StressTestIterators(*i, GetParam());
535 }
536}
537
538// Test that erase() revalidates iterators.
539TEST_P(MapImplTest, EraseRevalidates) {
540 // As multiple underlying hash_map implementations do not follow the
541 // validation requirement, the test is disabled for old-style maps.
542 if (GetParam()) return;
543 map_[3] = map_[13] = map_[20] = 0;
544 const int initial_size = map_.size();
545 EXPECT_EQ(3, initial_size);
546 vector<Map<int, int>::iterator> v;
547 for (Map<int, int>::iterator it = map_.begin(); it != map_.end(); ++it) {
548 v.push_back(it);
549 }
550 EXPECT_EQ(initial_size, v.size());
551 for (int i = 0; map_.size() <= initial_size * 20; i++) {
552 map_[i] = 0;
553 }
554 const int larger_size = map_.size();
555 // We've greatly increased the size of the map, so it is highly likely that
556 // the following will corrupt m if erase() doesn't properly revalidate
557 // iterators passed to it. Finishing this routine without crashing indicates
558 // success.
559 for (int i = 0; i < v.size(); i++) {
560 map_.erase(v[i]);
561 }
562 EXPECT_EQ(larger_size - v.size(), map_.size());
563}
564
Feng Xiao99aa0f92014-11-20 16:18:53 -0800565template <typename T>
566bool IsConstHelper(T& /*t*/) { // NOLINT. We want to catch non-const refs here.
567 return false;
568}
569template <typename T>
570bool IsConstHelper(const T& /*t*/) {
571 return true;
572}
573
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700574TEST_P(MapImplTest, IteratorConstness) {
Feng Xiao99aa0f92014-11-20 16:18:53 -0800575 map_[0] = 0;
576 EXPECT_TRUE(IsConstHelper(*map_.cbegin()));
577 EXPECT_TRUE(IsConstHelper(*const_map_.begin()));
578 EXPECT_FALSE(IsConstHelper(*map_.begin()));
579}
580
581bool IsForwardIteratorHelper(std::forward_iterator_tag /*tag*/) { return true; }
582template <typename T>
583bool IsForwardIteratorHelper(T /*t*/) {
584 return false;
585}
586
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700587TEST_P(MapImplTest, IteratorCategory) {
Feng Xiao99aa0f92014-11-20 16:18:53 -0800588 EXPECT_TRUE(IsForwardIteratorHelper(
589 std::iterator_traits<Map<int, int>::iterator>::iterator_category()));
590 EXPECT_TRUE(IsForwardIteratorHelper(std::iterator_traits<
591 Map<int, int>::const_iterator>::iterator_category()));
592}
593
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700594TEST_P(MapImplTest, InsertSingle) {
Feng Xiaof157a562014-11-14 11:50:31 -0800595 int32 key = 0;
596 int32 value1 = 100;
597 int32 value2 = 101;
598
599 // Insert a non-existed key.
600 std::pair<Map<int32, int32>::iterator, bool> result1 =
601 map_.insert(Map<int32, int32>::value_type(key, value1));
602 ExpectSingleElement(key, value1);
603
604 Map<int32, int32>::iterator it1 = result1.first;
605 EXPECT_EQ(key, it1->first);
606 EXPECT_EQ(value1, it1->second);
607 EXPECT_TRUE(result1.second);
608
609 // Insert an existed key.
610 std::pair<Map<int32, int32>::iterator, bool> result2 =
611 map_.insert(Map<int32, int32>::value_type(key, value2));
612 ExpectSingleElement(key, value1);
613
614 Map<int32, int32>::iterator it2 = result2.first;
615 EXPECT_TRUE(it1 == it2);
616 EXPECT_FALSE(result2.second);
617}
618
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700619TEST_P(MapImplTest, InsertByIterator) {
Feng Xiaof157a562014-11-14 11:50:31 -0800620 int32 key1 = 0;
621 int32 key2 = 1;
622 int32 value1a = 100;
623 int32 value1b = 101;
624 int32 value2a = 200;
625 int32 value2b = 201;
626
627 std::map<int32, int32> map1;
628 map1[key1] = value1a;
629 map1[key2] = value2a;
630
631 map_.insert(map1.begin(), map1.end());
632 ExpectElements(map1);
633
634 std::map<int32, int32> map2;
635 map2[key1] = value1b;
636 map2[key2] = value2b;
637
638 map_.insert(map2.begin(), map2.end());
639 ExpectElements(map1);
640}
641
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700642TEST_P(MapImplTest, EraseSingleByKey) {
Feng Xiaof157a562014-11-14 11:50:31 -0800643 int32 key = 0;
644 int32 value = 100;
645
646 map_[key] = value;
647 ExpectSingleElement(key, value);
648
649 // Erase an existing key.
650 EXPECT_EQ(1, map_.erase(key));
651 EXPECT_TRUE(map_.empty());
652 EXPECT_EQ(0, map_.size());
653 EXPECT_TRUE(map_.end() == map_.find(key));
654 EXPECT_TRUE(map_.begin() == map_.end());
655
656 // Erase a non-existing key.
657 EXPECT_EQ(0, map_.erase(key));
658}
659
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700660TEST_P(MapImplTest, EraseMutipleByKey) {
Feng Xiaof157a562014-11-14 11:50:31 -0800661 // erase in one specific order to trigger corner cases
662 for (int i = 0; i < 5; i++) {
663 map_[i] = i;
664 }
665
666 map_.erase(0);
667 EXPECT_EQ(4, map_.size());
668 EXPECT_TRUE(map_.end() == map_.find(0));
669
670 map_.erase(1);
671 EXPECT_EQ(3, map_.size());
672 EXPECT_TRUE(map_.end() == map_.find(1));
673
674 map_.erase(3);
675 EXPECT_EQ(2, map_.size());
676 EXPECT_TRUE(map_.end() == map_.find(3));
677
678 map_.erase(4);
679 EXPECT_EQ(1, map_.size());
680 EXPECT_TRUE(map_.end() == map_.find(4));
681
682 map_.erase(2);
683 EXPECT_EQ(0, map_.size());
684 EXPECT_TRUE(map_.end() == map_.find(2));
685}
686
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700687TEST_P(MapImplTest, EraseSingleByIterator) {
Feng Xiaof157a562014-11-14 11:50:31 -0800688 int32 key = 0;
689 int32 value = 100;
690
691 map_[key] = value;
692 ExpectSingleElement(key, value);
693
694 Map<int32, int32>::iterator it = map_.find(key);
695 map_.erase(it);
696 EXPECT_TRUE(map_.empty());
697 EXPECT_EQ(0, map_.size());
698 EXPECT_TRUE(map_.end() == map_.find(key));
699 EXPECT_TRUE(map_.begin() == map_.end());
700}
701
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700702TEST_P(MapImplTest, ValidIteratorAfterErase) {
Feng Xiaof157a562014-11-14 11:50:31 -0800703 for (int i = 0; i < 10; i++) {
704 map_[i] = i;
705 }
706
707 int count = 0;
708
709 for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) {
710 count++;
711 if (it->first % 2 == 1) {
712 map_.erase(it++);
713 } else {
714 ++it;
715 }
716 }
717
718 EXPECT_EQ(10, count);
719 EXPECT_EQ(5, map_.size());
720}
721
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700722TEST_P(MapImplTest, EraseByIterator) {
Feng Xiaof157a562014-11-14 11:50:31 -0800723 int32 key1 = 0;
724 int32 key2 = 1;
725 int32 value1 = 100;
726 int32 value2 = 101;
727
728 std::map<int32, int32> map;
729 map[key1] = value1;
730 map[key2] = value2;
731
732 map_.insert(map.begin(), map.end());
733 ExpectElements(map);
734
735 map_.erase(map_.begin(), map_.end());
736 EXPECT_TRUE(map_.empty());
737 EXPECT_EQ(0, map_.size());
738 EXPECT_TRUE(map_.end() == map_.find(key1));
739 EXPECT_TRUE(map_.end() == map_.find(key2));
740 EXPECT_TRUE(map_.begin() == map_.end());
741}
742
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700743TEST_P(MapImplTest, Clear) {
Feng Xiaof157a562014-11-14 11:50:31 -0800744 int32 key = 0;
745 int32 value = 100;
746
747 map_[key] = value;
748 ExpectSingleElement(key, value);
749
750 map_.clear();
751
752 EXPECT_TRUE(map_.empty());
753 EXPECT_EQ(0, map_.size());
754 EXPECT_TRUE(map_.end() == map_.find(key));
755 EXPECT_TRUE(map_.begin() == map_.end());
756}
757
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700758static void CopyConstructorHelper(Arena* arena, Map<int32, int32>* m) {
Feng Xiaof157a562014-11-14 11:50:31 -0800759 int32 key1 = 0;
760 int32 key2 = 1;
761 int32 value1 = 100;
762 int32 value2 = 101;
763
764 std::map<int32, int32> map;
765 map[key1] = value1;
766 map[key2] = value2;
767
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700768 m->insert(map.begin(), map.end());
Feng Xiaof157a562014-11-14 11:50:31 -0800769
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700770 Map<int32, int32> other(*m);
Feng Xiaof157a562014-11-14 11:50:31 -0800771
772 EXPECT_EQ(2, other.size());
773 EXPECT_EQ(value1, other.at(key1));
774 EXPECT_EQ(value2, other.at(key2));
775}
776
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700777TEST_P(MapImplTest, CopyConstructorWithArena) {
778 Arena a;
779 CopyConstructorHelper(&a, &map_);
780}
781
782TEST_P(MapImplTest, CopyConstructorWithoutArena) {
783 CopyConstructorHelper(NULL, &map_);
784}
785
786TEST_P(MapImplTest, IterConstructor) {
Feng Xiaoeee38b02015-08-22 18:25:48 -0700787 int32 key1 = 0;
788 int32 key2 = 1;
789 int32 value1 = 100;
790 int32 value2 = 101;
791
792 std::map<int32, int32> map;
793 map[key1] = value1;
794 map[key2] = value2;
795
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700796 Map<int32, int32> new_map(map.begin(), map.end(),
797 GetParam());
Feng Xiaoeee38b02015-08-22 18:25:48 -0700798
799 EXPECT_EQ(2, new_map.size());
800 EXPECT_EQ(value1, new_map.at(key1));
801 EXPECT_EQ(value2, new_map.at(key2));
802}
803
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700804TEST_P(MapImplTest, Assigner) {
Feng Xiaof157a562014-11-14 11:50:31 -0800805 int32 key1 = 0;
806 int32 key2 = 1;
807 int32 value1 = 100;
808 int32 value2 = 101;
809
810 std::map<int32, int32> map;
811 map[key1] = value1;
812 map[key2] = value2;
813
814 map_.insert(map.begin(), map.end());
815
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700816 Map<int32, int32> other(GetParam());
Feng Xiao99aa0f92014-11-20 16:18:53 -0800817 int32 key_other = 123;
818 int32 value_other = 321;
819 other[key_other] = value_other;
820 EXPECT_EQ(1, other.size());
821
Feng Xiaof157a562014-11-14 11:50:31 -0800822 other = map_;
823
824 EXPECT_EQ(2, other.size());
825 EXPECT_EQ(value1, other.at(key1));
826 EXPECT_EQ(value2, other.at(key2));
Feng Xiao99aa0f92014-11-20 16:18:53 -0800827 EXPECT_TRUE(other.find(key_other) == other.end());
828
829 // Self assign
830 other = other;
831 EXPECT_EQ(2, other.size());
832 EXPECT_EQ(value1, other.at(key1));
833 EXPECT_EQ(value2, other.at(key2));
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700834
835 // Try assignment to a map with a different choice of "style."
836 Map<int32, int32> m(!GetParam());
837 m = other;
838 EXPECT_EQ(2, m.size());
839 EXPECT_EQ(value1, m.at(key1));
840 EXPECT_EQ(value2, m.at(key2));
Feng Xiaof157a562014-11-14 11:50:31 -0800841}
842
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700843TEST_P(MapImplTest, Rehash) {
Feng Xiaof157a562014-11-14 11:50:31 -0800844 const int test_size = 50;
845 std::map<int32, int32> reference_map;
846 for (int i = 0; i < test_size; i++) {
847 reference_map[i] = i;
848 }
849 for (int i = 0; i < test_size; i++) {
850 map_[i] = reference_map[i];
851 EXPECT_EQ(reference_map[i], map_[i]);
852 }
853 for (int i = 0; i < test_size; i++) {
854 map_.erase(i);
855 EXPECT_TRUE(map_.end() == map_.find(i));
856 }
857 EXPECT_TRUE(map_.empty());
858}
859
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700860TEST_P(MapImplTest, EqualRange) {
Feng Xiao99aa0f92014-11-20 16:18:53 -0800861 int key = 100, key_missing = 101;
862 map_[key] = 100;
863
864 std::pair<google::protobuf::Map<int32, int32>::iterator,
865 google::protobuf::Map<int32, int32>::iterator> range = map_.equal_range(key);
866 EXPECT_TRUE(map_.find(key) == range.first);
867 EXPECT_TRUE(++map_.find(key) == range.second);
868
869 range = map_.equal_range(key_missing);
870 EXPECT_TRUE(map_.end() == range.first);
871 EXPECT_TRUE(map_.end() == range.second);
872
873 std::pair<google::protobuf::Map<int32, int32>::const_iterator,
874 google::protobuf::Map<int32, int32>::const_iterator> const_range =
875 const_map_.equal_range(key);
876 EXPECT_TRUE(const_map_.find(key) == const_range.first);
877 EXPECT_TRUE(++const_map_.find(key) == const_range.second);
878
879 const_range = const_map_.equal_range(key_missing);
880 EXPECT_TRUE(const_map_.end() == const_range.first);
881 EXPECT_TRUE(const_map_.end() == const_range.second);
882}
883
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700884TEST_P(MapImplTest, ConvertToStdMap) {
Feng Xiao6ae3bde2014-11-25 14:01:44 -0800885 map_[100] = 101;
886 std::map<int32, int32> std_map(map_.begin(), map_.end());
887 EXPECT_EQ(1, std_map.size());
888 EXPECT_EQ(101, std_map[100]);
889}
890
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700891TEST_P(MapImplTest, ConvertToStdVectorOfPairs) {
Jisi Liu885b6122015-02-28 14:51:22 -0800892 map_[100] = 101;
893 std::vector<std::pair<int32, int32> > std_vec(map_.begin(), map_.end());
894 EXPECT_EQ(1, std_vec.size());
895 EXPECT_EQ(100, std_vec[0].first);
896 EXPECT_EQ(101, std_vec[0].second);
897}
898
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700899INSTANTIATE_TEST_CASE_P(BoolSequence, MapImplTest, testing::Bool());
900
Feng Xiaof157a562014-11-14 11:50:31 -0800901// Map Field Reflection Test ========================================
902
903static int Func(int i, int j) {
904 return i * j;
905}
906
907static string StrFunc(int i, int j) {
908 string str;
909 SStringPrintf(&str, "%d", Func(i, j));
910 return str;
911}
912
913static int Int(const string& value) {
914 int result = 0;
915 std::istringstream(value) >> result;
916 return result;
917}
918
919class MapFieldReflectionTest : public testing::Test {
920 protected:
921 typedef FieldDescriptor FD;
922};
923
924TEST_F(MapFieldReflectionTest, RegularFields) {
925 TestMap message;
926 const Reflection* refl = message.GetReflection();
927 const Descriptor* desc = message.GetDescriptor();
928
929 Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32();
930 Map<int32, double>* map_int32_double = message.mutable_map_int32_double();
931 Map<string, string>* map_string_string = message.mutable_map_string_string();
932 Map<int32, ForeignMessage>* map_int32_foreign_message =
933 message.mutable_map_int32_foreign_message();
934
935 for (int i = 0; i < 10; ++i) {
936 (*map_int32_int32)[i] = Func(i, 1);
937 (*map_int32_double)[i] = Func(i, 2);
938 (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
939 (*map_int32_foreign_message)[i].set_c(Func(i, 6));
940 }
941
942 // Get FieldDescriptors for all the fields of interest.
943 const FieldDescriptor* fd_map_int32_int32 =
944 desc->FindFieldByName("map_int32_int32");
945 const FieldDescriptor* fd_map_int32_double =
946 desc->FindFieldByName("map_int32_double");
947 const FieldDescriptor* fd_map_string_string =
948 desc->FindFieldByName("map_string_string");
949 const FieldDescriptor* fd_map_int32_foreign_message =
950 desc->FindFieldByName("map_int32_foreign_message");
951
952 const FieldDescriptor* fd_map_int32_in32_key =
953 fd_map_int32_int32->message_type()->FindFieldByName("key");
954 const FieldDescriptor* fd_map_int32_in32_value =
955 fd_map_int32_int32->message_type()->FindFieldByName("value");
956 const FieldDescriptor* fd_map_int32_double_key =
957 fd_map_int32_double->message_type()->FindFieldByName("key");
958 const FieldDescriptor* fd_map_int32_double_value =
959 fd_map_int32_double->message_type()->FindFieldByName("value");
960 const FieldDescriptor* fd_map_string_string_key =
961 fd_map_string_string->message_type()->FindFieldByName("key");
962 const FieldDescriptor* fd_map_string_string_value =
963 fd_map_string_string->message_type()->FindFieldByName("value");
964 const FieldDescriptor* fd_map_int32_foreign_message_key =
965 fd_map_int32_foreign_message->message_type()->FindFieldByName("key");
966 const FieldDescriptor* fd_map_int32_foreign_message_value =
967 fd_map_int32_foreign_message->message_type()->FindFieldByName("value");
968
969 // Get RepeatedPtrField objects for all fields of interest.
970 const RepeatedPtrField<Message>& mf_int32_int32 =
971 refl->GetRepeatedPtrField<Message>(message, fd_map_int32_int32);
972 const RepeatedPtrField<Message>& mf_int32_double =
973 refl->GetRepeatedPtrField<Message>(message, fd_map_int32_double);
974 const RepeatedPtrField<Message>& mf_string_string =
975 refl->GetRepeatedPtrField<Message>(message, fd_map_string_string);
976 const RepeatedPtrField<Message>&
977 mf_int32_foreign_message =
978 refl->GetRepeatedPtrField<Message>(
979 message, fd_map_int32_foreign_message);
980
981 // Get mutable RepeatedPtrField objects for all fields of interest.
982 RepeatedPtrField<Message>* mmf_int32_int32 =
983 refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_int32);
984 RepeatedPtrField<Message>* mmf_int32_double =
985 refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_double);
986 RepeatedPtrField<Message>* mmf_string_string =
987 refl->MutableRepeatedPtrField<Message>(&message, fd_map_string_string);
988 RepeatedPtrField<Message>* mmf_int32_foreign_message =
989 refl->MutableRepeatedPtrField<Message>(
990 &message, fd_map_int32_foreign_message);
991
992 // Make sure we can do gets through the RepeatedPtrField objects.
993 for (int i = 0; i < 10; ++i) {
994 {
995 // Check gets through const objects.
996 const Message& message_int32_int32 = mf_int32_int32.Get(i);
997 int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
998 message_int32_int32, fd_map_int32_in32_key);
999 int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1000 message_int32_int32, fd_map_int32_in32_value);
1001 EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1002
1003 const Message& message_int32_double = mf_int32_double.Get(i);
1004 int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
1005 message_int32_double, fd_map_int32_double_key);
1006 double value_int32_double =
1007 message_int32_double.GetReflection()->GetDouble(
1008 message_int32_double, fd_map_int32_double_value);
1009 EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1010
1011 const Message& message_string_string = mf_string_string.Get(i);
1012 string key_string_string =
1013 message_string_string.GetReflection()->GetString(
1014 message_string_string, fd_map_string_string_key);
1015 string value_string_string =
1016 message_string_string.GetReflection()->GetString(
1017 message_string_string, fd_map_string_string_value);
1018 EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1019
1020 const Message& message_int32_message = mf_int32_foreign_message.Get(i);
1021 int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
1022 message_int32_message, fd_map_int32_foreign_message_key);
1023 const ForeignMessage& value_int32_message =
1024 down_cast<const ForeignMessage&>(
1025 message_int32_message.GetReflection()
1026 ->GetMessage(message_int32_message,
1027 fd_map_int32_foreign_message_value));
1028 EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1029 }
1030
1031 {
1032 // Check gets through mutable objects.
1033 const Message& message_int32_int32 = mmf_int32_int32->Get(i);
1034 int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1035 message_int32_int32, fd_map_int32_in32_key);
1036 int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1037 message_int32_int32, fd_map_int32_in32_value);
1038 EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1039
1040 const Message& message_int32_double = mmf_int32_double->Get(i);
1041 int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
1042 message_int32_double, fd_map_int32_double_key);
1043 double value_int32_double =
1044 message_int32_double.GetReflection()->GetDouble(
1045 message_int32_double, fd_map_int32_double_value);
1046 EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1047
1048 const Message& message_string_string = mmf_string_string->Get(i);
1049 string key_string_string =
1050 message_string_string.GetReflection()->GetString(
1051 message_string_string, fd_map_string_string_key);
1052 string value_string_string =
1053 message_string_string.GetReflection()->GetString(
1054 message_string_string, fd_map_string_string_value);
1055 EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1056
1057 const Message& message_int32_message = mmf_int32_foreign_message->Get(i);
1058 int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
1059 message_int32_message, fd_map_int32_foreign_message_key);
1060 const ForeignMessage& value_int32_message =
1061 down_cast<const ForeignMessage&>(
1062 message_int32_message.GetReflection()
1063 ->GetMessage(message_int32_message,
1064 fd_map_int32_foreign_message_value));
1065 EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1066 }
1067 }
1068
1069 // Do sets through the RepeatedPtrField objects.
1070 for (int i = 0; i < 10; i++) {
1071 {
1072 Message* message_int32_int32 = mmf_int32_int32->Mutable(i);
1073 int32 key_int32_int32 = message_int32_int32->GetReflection()->GetInt32(
1074 *message_int32_int32, fd_map_int32_in32_key);
1075 message_int32_int32->GetReflection()->SetInt32(message_int32_int32,
1076 fd_map_int32_in32_value,
1077 Func(key_int32_int32, -1));
1078
1079 Message* message_int32_double = mmf_int32_double->Mutable(i);
1080 int32 key_int32_double = message_int32_double->GetReflection()->GetInt32(
1081 *message_int32_double, fd_map_int32_double_key);
1082 message_int32_double->GetReflection()->SetDouble(
1083 message_int32_double, fd_map_int32_double_value,
1084 Func(key_int32_double, -2));
1085
1086 Message* message_string_string = mmf_string_string->Mutable(i);
1087 string key_string_string =
1088 message_string_string->GetReflection()->GetString(
1089 *message_string_string, fd_map_string_string_key);
1090 message_string_string->GetReflection()->SetString(
1091 message_string_string, fd_map_string_string_value,
1092 StrFunc(Int(key_string_string), -5));
1093
1094 Message* message_int32_message = mmf_int32_foreign_message->Mutable(i);
1095 int32 key_int32_message =
1096 message_int32_message->GetReflection()->GetInt32(
1097 *message_int32_message, fd_map_int32_foreign_message_key);
1098 ForeignMessage* value_int32_message = down_cast<ForeignMessage*>(
1099 message_int32_message->GetReflection()
1100 ->MutableMessage(message_int32_message,
1101 fd_map_int32_foreign_message_value));
1102 value_int32_message->set_c(Func(key_int32_message, -6));
1103 }
1104 }
1105
1106 // Check gets through mutable objects.
1107 for (int i = 0; i < 10; i++) {
1108 EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
1109 EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
1110 EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
1111 EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
1112 }
1113}
1114
1115TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) {
1116 TestMap message;
1117 const Reflection* refl = message.GetReflection();
1118 const Descriptor* desc = message.GetDescriptor();
1119
1120 Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32();
1121 Map<int32, double>* map_int32_double = message.mutable_map_int32_double();
1122 Map<string, string>* map_string_string = message.mutable_map_string_string();
1123 Map<int32, ForeignMessage>* map_int32_foreign_message =
1124 message.mutable_map_int32_foreign_message();
1125
1126 for (int i = 0; i < 10; ++i) {
1127 (*map_int32_int32)[i] = Func(i, 1);
1128 (*map_int32_double)[i] = Func(i, 2);
1129 (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
1130 (*map_int32_foreign_message)[i].set_c(Func(i, 6));
1131 }
1132
1133 // Get FieldDescriptors for all the fields of interest.
1134 const FieldDescriptor* fd_map_int32_int32 =
1135 desc->FindFieldByName("map_int32_int32");
1136 const FieldDescriptor* fd_map_int32_double =
1137 desc->FindFieldByName("map_int32_double");
1138 const FieldDescriptor* fd_map_string_string =
1139 desc->FindFieldByName("map_string_string");
1140 const FieldDescriptor* fd_map_int32_foreign_message =
1141 desc->FindFieldByName("map_int32_foreign_message");
1142
1143 const FieldDescriptor* fd_map_int32_in32_key =
1144 fd_map_int32_int32->message_type()->FindFieldByName("key");
1145 const FieldDescriptor* fd_map_int32_in32_value =
1146 fd_map_int32_int32->message_type()->FindFieldByName("value");
1147 const FieldDescriptor* fd_map_int32_double_key =
1148 fd_map_int32_double->message_type()->FindFieldByName("key");
1149 const FieldDescriptor* fd_map_int32_double_value =
1150 fd_map_int32_double->message_type()->FindFieldByName("value");
1151 const FieldDescriptor* fd_map_string_string_key =
1152 fd_map_string_string->message_type()->FindFieldByName("key");
1153 const FieldDescriptor* fd_map_string_string_value =
1154 fd_map_string_string->message_type()->FindFieldByName("value");
1155 const FieldDescriptor* fd_map_int32_foreign_message_key =
1156 fd_map_int32_foreign_message->message_type()->FindFieldByName("key");
1157 const FieldDescriptor* fd_map_int32_foreign_message_value =
1158 fd_map_int32_foreign_message->message_type()->FindFieldByName("value");
1159
1160 // Get RepeatedFieldRef objects for all fields of interest.
1161 const RepeatedFieldRef<Message> mf_int32_int32 =
1162 refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_int32);
1163 const RepeatedFieldRef<Message> mf_int32_double =
1164 refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_double);
1165 const RepeatedFieldRef<Message> mf_string_string =
1166 refl->GetRepeatedFieldRef<Message>(message, fd_map_string_string);
1167 const RepeatedFieldRef<Message> mf_int32_foreign_message =
1168 refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_foreign_message);
1169
1170 // Get mutable RepeatedFieldRef objects for all fields of interest.
1171 const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
1172 refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_int32);
1173 const MutableRepeatedFieldRef<Message> mmf_int32_double =
1174 refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_double);
1175 const MutableRepeatedFieldRef<Message> mmf_string_string =
1176 refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_string_string);
1177 const MutableRepeatedFieldRef<Message>
1178 mmf_int32_foreign_message =
1179 refl->GetMutableRepeatedFieldRef<Message>(
1180 &message, fd_map_int32_foreign_message);
1181
1182 // Get entry default instances
1183 google::protobuf::scoped_ptr<Message> entry_int32_int32(
1184 MessageFactory::generated_factory()
1185 ->GetPrototype(fd_map_int32_int32->message_type())
1186 ->New());
1187 google::protobuf::scoped_ptr<Message> entry_int32_double(
1188 MessageFactory::generated_factory()
1189 ->GetPrototype(fd_map_int32_double->message_type())
1190 ->New());
1191 google::protobuf::scoped_ptr<Message> entry_string_string(
1192 MessageFactory::generated_factory()
1193 ->GetPrototype(fd_map_string_string->message_type())
1194 ->New());
1195 google::protobuf::scoped_ptr<Message> entry_int32_foreign_message(
1196 MessageFactory::generated_factory()
1197 ->GetPrototype(fd_map_int32_foreign_message->message_type())
1198 ->New());
1199
1200 EXPECT_EQ(10, mf_int32_int32.size());
1201 EXPECT_EQ(10, mmf_int32_int32.size());
1202 EXPECT_EQ(10, mf_int32_double.size());
1203 EXPECT_EQ(10, mmf_int32_double.size());
1204 EXPECT_EQ(10, mf_string_string.size());
1205 EXPECT_EQ(10, mmf_string_string.size());
1206 EXPECT_EQ(10, mf_int32_foreign_message.size());
1207 EXPECT_EQ(10, mmf_int32_foreign_message.size());
1208
1209 EXPECT_FALSE(mf_int32_int32.empty());
1210 EXPECT_FALSE(mmf_int32_int32.empty());
1211 EXPECT_FALSE(mf_int32_double.empty());
1212 EXPECT_FALSE(mmf_int32_double.empty());
1213 EXPECT_FALSE(mf_string_string.empty());
1214 EXPECT_FALSE(mmf_string_string.empty());
1215 EXPECT_FALSE(mf_int32_foreign_message.empty());
1216 EXPECT_FALSE(mmf_int32_foreign_message.empty());
1217
1218 // Make sure we can do gets through the RepeatedFieldRef objects.
1219 for (int i = 0; i < 10; ++i) {
1220 {
1221 // Check gets through const objects.
1222 const Message& message_int32_int32 =
1223 mf_int32_int32.Get(i, entry_int32_int32.get());
1224 int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1225 message_int32_int32, fd_map_int32_in32_key);
1226 int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1227 message_int32_int32, fd_map_int32_in32_value);
1228 EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1229
1230 const Message& message_int32_double =
1231 mf_int32_double.Get(i, entry_int32_double.get());
1232 int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
1233 message_int32_double, fd_map_int32_double_key);
1234 double value_int32_double =
1235 message_int32_double.GetReflection()->GetDouble(
1236 message_int32_double, fd_map_int32_double_value);
1237 EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1238
1239 const Message& message_string_string =
1240 mf_string_string.Get(i, entry_string_string.get());
1241 string key_string_string =
1242 message_string_string.GetReflection()->GetString(
1243 message_string_string, fd_map_string_string_key);
1244 string value_string_string =
1245 message_string_string.GetReflection()->GetString(
1246 message_string_string, fd_map_string_string_value);
1247 EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1248
1249 const Message& message_int32_message =
1250 mf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
1251 int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
1252 message_int32_message, fd_map_int32_foreign_message_key);
1253 const ForeignMessage& value_int32_message =
1254 down_cast<const ForeignMessage&>(
1255 message_int32_message.GetReflection()
1256 ->GetMessage(message_int32_message,
1257 fd_map_int32_foreign_message_value));
1258 EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1259 }
1260
1261 {
1262 // Check gets through mutable objects.
1263 const Message& message_int32_int32 =
1264 mmf_int32_int32.Get(i, entry_int32_int32.get());
1265 int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1266 message_int32_int32, fd_map_int32_in32_key);
1267 int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1268 message_int32_int32, fd_map_int32_in32_value);
1269 EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1270
1271 const Message& message_int32_double =
1272 mmf_int32_double.Get(i, entry_int32_double.get());
1273 int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
1274 message_int32_double, fd_map_int32_double_key);
1275 double value_int32_double =
1276 message_int32_double.GetReflection()->GetDouble(
1277 message_int32_double, fd_map_int32_double_value);
1278 EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1279
1280 const Message& message_string_string =
1281 mmf_string_string.Get(i, entry_string_string.get());
1282 string key_string_string =
1283 message_string_string.GetReflection()->GetString(
1284 message_string_string, fd_map_string_string_key);
1285 string value_string_string =
1286 message_string_string.GetReflection()->GetString(
1287 message_string_string, fd_map_string_string_value);
1288 EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1289
1290 const Message& message_int32_message =
1291 mmf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
1292 int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
1293 message_int32_message, fd_map_int32_foreign_message_key);
1294 const ForeignMessage& value_int32_message =
1295 down_cast<const ForeignMessage&>(
1296 message_int32_message.GetReflection()
1297 ->GetMessage(message_int32_message,
1298 fd_map_int32_foreign_message_value));
1299 EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1300 }
1301 }
1302
1303 // Make sure we can do sets through the RepeatedFieldRef objects.
1304 for (int i = 0; i < 10; i++) {
1305 const Message& message_int32_int32 =
1306 mmf_int32_int32.Get(i, entry_int32_int32.get());
1307 int key = message_int32_int32.GetReflection()->GetInt32(
1308 message_int32_int32, fd_map_int32_in32_key);
1309
1310 entry_int32_int32->GetReflection()->SetInt32(
1311 entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
1312 key);
1313 entry_int32_int32->GetReflection()->SetInt32(
1314 entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
1315 Func(key, -1));
1316 entry_int32_double->GetReflection()->SetInt32(
1317 entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
1318 key);
1319 entry_int32_double->GetReflection()->SetDouble(
1320 entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
1321 Func(key, -2));
1322 entry_string_string->GetReflection()->SetString(
Feng Xiao99aa0f92014-11-20 16:18:53 -08001323 entry_string_string.get(),
1324 fd_map_string_string->message_type()->field(0), StrFunc(key, 1));
Feng Xiaof157a562014-11-14 11:50:31 -08001325 entry_string_string->GetReflection()->SetString(
Feng Xiao99aa0f92014-11-20 16:18:53 -08001326 entry_string_string.get(),
1327 fd_map_string_string->message_type()->field(1), StrFunc(key, -5));
Feng Xiaof157a562014-11-14 11:50:31 -08001328 entry_int32_foreign_message->GetReflection()->SetInt32(
1329 entry_int32_foreign_message.get(),
Feng Xiao99aa0f92014-11-20 16:18:53 -08001330 fd_map_int32_foreign_message->message_type()->field(0), key);
Feng Xiaof157a562014-11-14 11:50:31 -08001331 Message* value_message =
1332 entry_int32_foreign_message->GetReflection()->MutableMessage(
1333 entry_int32_foreign_message.get(),
1334 fd_map_int32_foreign_message->message_type()->field(1));
1335 value_message->GetReflection()->SetInt32(
1336 value_message, value_message->GetDescriptor()->FindFieldByName("c"),
1337 Func(key, -6));
1338
Feng Xiao99aa0f92014-11-20 16:18:53 -08001339 mmf_int32_int32.Set(i, *entry_int32_int32);
1340 mmf_int32_double.Set(i, *entry_int32_double);
1341 mmf_string_string.Set(i, *entry_string_string);
1342 mmf_int32_foreign_message.Set(i, *entry_int32_foreign_message);
Feng Xiaof157a562014-11-14 11:50:31 -08001343 }
1344
1345 for (int i = 0; i < 10; i++) {
1346 EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
1347 EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
1348 EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
1349 EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
1350 }
1351
1352 // Test iterators.
1353 {
1354 int index = 0;
1355 hash_map<int32, int32> result;
1356 for (RepeatedFieldRef<Message>::iterator it = mf_int32_int32.begin();
1357 it != mf_int32_int32.end(); ++it) {
1358 const Message& message = *it;
1359 int32 key =
1360 message.GetReflection()->GetInt32(message, fd_map_int32_in32_key);
1361 int32 value =
1362 message.GetReflection()->GetInt32(message, fd_map_int32_in32_value);
1363 result[key] = value;
1364 ++index;
1365 }
1366 EXPECT_EQ(10, index);
1367 for (hash_map<int32, int32>::const_iterator it = result.begin();
1368 it != result.end(); ++it) {
1369 EXPECT_EQ(message.map_int32_int32().at(it->first), it->second);
1370 }
1371 }
1372
1373 {
1374 int index = 0;
1375 hash_map<int32, double> result;
1376 for (RepeatedFieldRef<Message>::iterator it = mf_int32_double.begin();
1377 it != mf_int32_double.end(); ++it) {
1378 const Message& message = *it;
1379 int32 key =
1380 message.GetReflection()->GetInt32(message, fd_map_int32_double_key);
1381 double value = message.GetReflection()->GetDouble(
1382 message, fd_map_int32_double_value);
1383 result[key] = value;
1384 ++index;
1385 }
1386 EXPECT_EQ(10, index);
1387 for (hash_map<int32, double>::const_iterator it = result.begin();
1388 it != result.end(); ++it) {
1389 EXPECT_EQ(message.map_int32_double().at(it->first), it->second);
1390 }
1391 }
1392
1393 {
1394 int index = 0;
1395 hash_map<string, string> result;
1396 for (RepeatedFieldRef<Message>::iterator it = mf_string_string.begin();
1397 it != mf_string_string.end(); ++it) {
1398 const Message& message = *it;
1399 string key =
1400 message.GetReflection()->GetString(message, fd_map_string_string_key);
1401 string value = message.GetReflection()->GetString(
1402 message, fd_map_string_string_value);
1403 result[key] = value;
1404 ++index;
1405 }
1406 EXPECT_EQ(10, index);
1407 for (hash_map<string, string>::const_iterator it = result.begin();
1408 it != result.end(); ++it) {
1409 EXPECT_EQ(message.map_string_string().at(it->first), it->second);
1410 }
1411 }
1412
1413 {
1414 int index = 0;
1415 std::map<int32, ForeignMessage> result;
1416 for (RepeatedFieldRef<Message>::iterator it =
1417 mf_int32_foreign_message.begin();
1418 it != mf_int32_foreign_message.end(); ++it) {
1419 const Message& message = *it;
1420 int32 key = message.GetReflection()->GetInt32(
1421 message, fd_map_int32_foreign_message_key);
1422 const ForeignMessage& sub_message = down_cast<const ForeignMessage&>(
1423 message.GetReflection()
1424 ->GetMessage(message, fd_map_int32_foreign_message_value));
1425 result[key].MergeFrom(sub_message);
1426 ++index;
1427 }
1428 EXPECT_EQ(10, index);
1429 for (std::map<int32, ForeignMessage>::const_iterator it = result.begin();
1430 it != result.end(); ++it) {
1431 EXPECT_EQ(message.map_int32_foreign_message().at(it->first).c(),
1432 it->second.c());
1433 }
1434 }
1435
1436 // Test MutableRepeatedFieldRef::Add()
1437 entry_int32_int32->GetReflection()->SetInt32(
1438 entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
1439 4321);
1440 entry_int32_int32->GetReflection()->SetInt32(
1441 entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
1442 1234);
1443 mmf_int32_int32.Add(*entry_int32_int32);
1444 EXPECT_EQ(1234, message.map_int32_int32().at(4321));
1445
1446 entry_int32_double->GetReflection()->SetInt32(
1447 entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
1448 4321);
1449 entry_int32_double->GetReflection()->SetDouble(
1450 entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
1451 1234.0);
1452 mmf_int32_double.Add(*entry_int32_double);
1453 EXPECT_EQ(1234.0, message.map_int32_double().at(4321));
1454
1455 entry_string_string->GetReflection()->SetString(
1456 entry_string_string.get(),
1457 fd_map_string_string->message_type()->field(0), "4321");
1458 entry_string_string->GetReflection()->SetString(
1459 entry_string_string.get(), fd_map_string_string->message_type()->field(1),
1460 "1234");
1461 mmf_string_string.Add(*entry_string_string);
1462 EXPECT_EQ("1234", message.map_string_string().at("4321"));
1463
1464 entry_int32_foreign_message->GetReflection()->SetInt32(
1465 entry_int32_foreign_message.get(),
1466 fd_map_int32_foreign_message->message_type()->field(0), 4321);
1467 Message* value_message =
1468 entry_int32_foreign_message->GetReflection()->MutableMessage(
1469 entry_int32_foreign_message.get(),
1470 fd_map_int32_foreign_message->message_type()->field(1));
1471 ForeignMessage foreign_message;
1472 foreign_message.set_c(1234);
1473 value_message->CopyFrom(foreign_message);
1474
1475 mmf_int32_foreign_message.Add(*entry_int32_foreign_message);
1476 EXPECT_EQ(1234, message.map_int32_foreign_message().at(4321).c());
1477
Feng Xiaoeee38b02015-08-22 18:25:48 -07001478 // Test Reflection::AddAllocatedMessage
1479 Message* free_entry_string_string = MessageFactory::generated_factory()
1480 ->GetPrototype(fd_map_string_string->message_type())
1481 ->New();
1482 entry_string_string->GetReflection()->SetString(
1483 free_entry_string_string,
1484 fd_map_string_string->message_type()->field(0), "4321");
1485 entry_string_string->GetReflection()->SetString(
1486 free_entry_string_string, fd_map_string_string->message_type()->field(1),
1487 "1234");
1488 refl->AddAllocatedMessage(&message, fd_map_string_string,
1489 free_entry_string_string);
1490
Feng Xiaof157a562014-11-14 11:50:31 -08001491 // Test MutableRepeatedFieldRef::RemoveLast()
1492 mmf_int32_int32.RemoveLast();
1493 mmf_int32_double.RemoveLast();
1494 mmf_string_string.RemoveLast();
1495 mmf_int32_foreign_message.RemoveLast();
1496 EXPECT_EQ(10, message.map_int32_int32().size());
1497 EXPECT_EQ(10, message.map_int32_double().size());
Feng Xiaoeee38b02015-08-22 18:25:48 -07001498 EXPECT_EQ(11, message.map_string_string().size());
Feng Xiaof157a562014-11-14 11:50:31 -08001499 EXPECT_EQ(10, message.map_int32_foreign_message().size());
1500
1501 // Test MutableRepeatedFieldRef::SwapElements()
1502 {
1503 const Message& message0a = mmf_int32_int32.Get(0, entry_int32_int32.get());
1504 int32 int32_value0a =
1505 message0a.GetReflection()->GetInt32(message0a, fd_map_int32_in32_value);
1506 const Message& message9a = mmf_int32_int32.Get(9, entry_int32_int32.get());
1507 int32 int32_value9a =
1508 message9a.GetReflection()->GetInt32(message9a, fd_map_int32_in32_value);
1509
1510 mmf_int32_int32.SwapElements(0, 9);
1511
1512 const Message& message0b = mmf_int32_int32.Get(0, entry_int32_int32.get());
1513 int32 int32_value0b =
1514 message0b.GetReflection()->GetInt32(message0b, fd_map_int32_in32_value);
1515 const Message& message9b = mmf_int32_int32.Get(9, entry_int32_int32.get());
1516 int32 int32_value9b =
1517 message9b.GetReflection()->GetInt32(message9b, fd_map_int32_in32_value);
1518
1519 EXPECT_EQ(int32_value9a, int32_value0b);
1520 EXPECT_EQ(int32_value0a, int32_value9b);
1521 }
1522
1523 {
1524 const Message& message0a =
1525 mmf_int32_double.Get(0, entry_int32_double.get());
1526 double double_value0a = message0a.GetReflection()->GetDouble(
1527 message0a, fd_map_int32_double_value);
1528 const Message& message9a =
1529 mmf_int32_double.Get(9, entry_int32_double.get());
1530 double double_value9a = message9a.GetReflection()->GetDouble(
1531 message9a, fd_map_int32_double_value);
1532
1533 mmf_int32_double.SwapElements(0, 9);
1534
1535 const Message& message0b =
1536 mmf_int32_double.Get(0, entry_int32_double.get());
1537 double double_value0b = message0b.GetReflection()->GetDouble(
1538 message0b, fd_map_int32_double_value);
1539 const Message& message9b =
1540 mmf_int32_double.Get(9, entry_int32_double.get());
1541 double double_value9b = message9b.GetReflection()->GetDouble(
1542 message9b, fd_map_int32_double_value);
1543
1544 EXPECT_EQ(double_value9a, double_value0b);
1545 EXPECT_EQ(double_value0a, double_value9b);
1546 }
1547
1548 {
1549 const Message& message0a =
1550 mmf_string_string.Get(0, entry_string_string.get());
1551 string string_value0a = message0a.GetReflection()->GetString(
1552 message0a, fd_map_string_string_value);
1553 const Message& message9a =
1554 mmf_string_string.Get(9, entry_string_string.get());
1555 string string_value9a = message9a.GetReflection()->GetString(
1556 message9a, fd_map_string_string_value);
1557
1558 mmf_string_string.SwapElements(0, 9);
1559
1560 const Message& message0b =
1561 mmf_string_string.Get(0, entry_string_string.get());
1562 string string_value0b = message0b.GetReflection()->GetString(
1563 message0b, fd_map_string_string_value);
1564 const Message& message9b =
1565 mmf_string_string.Get(9, entry_string_string.get());
1566 string string_value9b = message9b.GetReflection()->GetString(
1567 message9b, fd_map_string_string_value);
1568
1569 EXPECT_EQ(string_value9a, string_value0b);
1570 EXPECT_EQ(string_value0a, string_value9b);
1571 }
1572
1573 {
1574 const Message& message0a =
1575 mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get());
1576 const ForeignMessage& sub_message0a = down_cast<const ForeignMessage&>(
1577 message0a.GetReflection()
1578 ->GetMessage(message0a, fd_map_int32_foreign_message_value));
1579 int32 int32_value0a = sub_message0a.c();
1580 const Message& message9a =
1581 mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get());
1582 const ForeignMessage& sub_message9a = down_cast<const ForeignMessage&>(
1583 message9a.GetReflection()
1584 ->GetMessage(message9a, fd_map_int32_foreign_message_value));
1585 int32 int32_value9a = sub_message9a.c();
1586
1587 mmf_int32_foreign_message.SwapElements(0, 9);
1588
1589 const Message& message0b =
1590 mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get());
1591 const ForeignMessage& sub_message0b = down_cast<const ForeignMessage&>(
1592 message0b.GetReflection()
1593 ->GetMessage(message0b, fd_map_int32_foreign_message_value));
1594 int32 int32_value0b = sub_message0b.c();
1595 const Message& message9b =
1596 mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get());
1597 const ForeignMessage& sub_message9b = down_cast<const ForeignMessage&>(
1598 message9b.GetReflection()
1599 ->GetMessage(message9b, fd_map_int32_foreign_message_value));
1600 int32 int32_value9b = sub_message9b.c();
1601
1602 EXPECT_EQ(int32_value9a, int32_value0b);
1603 EXPECT_EQ(int32_value0a, int32_value9b);
1604 }
1605}
1606
1607TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) {
1608 // Set-up message content.
1609 TestMap m0, m1, m2;
1610 for (int i = 0; i < 10; ++i) {
1611 (*m0.mutable_map_int32_int32())[i] = Func(i, 1);
1612 (*m0.mutable_map_int32_double())[i] = Func(i, 2);
1613 (*m0.mutable_map_string_string())[StrFunc(i, 1)] = StrFunc(i, 5);
1614 (*m0.mutable_map_int32_foreign_message())[i].set_c(Func(i, 6));
1615 (*m1.mutable_map_int32_int32())[i + 10] = Func(i, 11);
1616 (*m1.mutable_map_int32_double())[i + 10] = Func(i, 12);
1617 (*m1.mutable_map_string_string())[StrFunc(i + 10, 1)] = StrFunc(i, 15);
1618 (*m1.mutable_map_int32_foreign_message())[i + 10].set_c(Func(i, 16));
1619 (*m2.mutable_map_int32_int32())[i + 20] = Func(i, 21);
1620 (*m2.mutable_map_int32_double())[i + 20] = Func(i, 22);
1621 (*m2.mutable_map_string_string())[StrFunc(i + 20, 1)] = StrFunc(i, 25);
1622 (*m2.mutable_map_int32_foreign_message())[i + 20].set_c(Func(i, 26));
1623 }
1624
1625 const Reflection* refl = m0.GetReflection();
1626 const Descriptor* desc = m0.GetDescriptor();
1627
1628 // Get FieldDescriptors for all the fields of interest.
1629 const FieldDescriptor* fd_map_int32_int32 =
1630 desc->FindFieldByName("map_int32_int32");
1631 const FieldDescriptor* fd_map_int32_double =
1632 desc->FindFieldByName("map_int32_double");
1633 const FieldDescriptor* fd_map_string_string =
1634 desc->FindFieldByName("map_string_string");
1635 const FieldDescriptor* fd_map_int32_foreign_message =
1636 desc->FindFieldByName("map_int32_foreign_message");
1637
1638 // Get MutableRepeatedFieldRef objects for all fields of interest.
1639 const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
1640 refl->GetMutableRepeatedFieldRef<Message>(
1641 &m0, fd_map_int32_int32);
1642 const MutableRepeatedFieldRef<Message> mmf_int32_double =
1643 refl->GetMutableRepeatedFieldRef<Message>(
1644 &m0, fd_map_int32_double);
1645 const MutableRepeatedFieldRef<Message> mmf_string_string =
1646 refl->GetMutableRepeatedFieldRef<Message>(
1647 &m0, fd_map_string_string);
1648 const MutableRepeatedFieldRef<Message>
1649 mmf_int32_foreign_message =
1650 refl->GetMutableRepeatedFieldRef<Message>(
1651 &m0, fd_map_int32_foreign_message);
1652
1653 // Test MutableRepeatedRef::CopyFrom
1654 mmf_int32_int32.CopyFrom(
1655 refl->GetRepeatedFieldRef<Message>(
1656 m1, fd_map_int32_int32));
1657 mmf_int32_double.CopyFrom(
1658 refl->GetRepeatedFieldRef<Message>(
1659 m1, fd_map_int32_double));
1660 mmf_string_string.CopyFrom(
1661 refl->GetRepeatedFieldRef<Message>(
1662 m1, fd_map_string_string));
1663 mmf_int32_foreign_message.CopyFrom(
1664 refl->GetRepeatedFieldRef<Message>(
1665 m1, fd_map_int32_foreign_message));
1666
1667 for (int i = 0; i < 10; ++i) {
1668 EXPECT_EQ(Func(i, 11), m0.map_int32_int32().at(i + 10));
1669 EXPECT_EQ(Func(i, 12), m0.map_int32_double().at(i + 10));
1670 EXPECT_EQ(StrFunc(i, 15), m0.map_string_string().at(StrFunc(i + 10, 1)));
1671 EXPECT_EQ(Func(i, 16), m0.map_int32_foreign_message().at(i + 10).c());
1672 }
1673
1674 // Test MutableRepeatedRef::MergeFrom
1675 mmf_int32_int32.MergeFrom(
1676 refl->GetRepeatedFieldRef<Message>(
1677 m2, fd_map_int32_int32));
1678 mmf_int32_double.MergeFrom(
1679 refl->GetRepeatedFieldRef<Message>(
1680 m2, fd_map_int32_double));
1681 mmf_string_string.MergeFrom(
1682 refl->GetRepeatedFieldRef<Message>(
1683 m2, fd_map_string_string));
1684 mmf_int32_foreign_message.MergeFrom(
1685 refl->GetRepeatedFieldRef<Message>(
1686 m2, fd_map_int32_foreign_message));
1687 for (int i = 0; i < 10; ++i) {
1688 EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20));
1689 EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20));
1690 EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
1691 EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
1692 }
1693
1694 // Test MutableRepeatedRef::Swap
1695 // Swap between m0 and m2.
1696 mmf_int32_int32.Swap(
1697 refl->GetMutableRepeatedFieldRef<Message>(
1698 &m2, fd_map_int32_int32));
1699 mmf_int32_double.Swap(
1700 refl->GetMutableRepeatedFieldRef<Message>(
1701 &m2, fd_map_int32_double));
1702 mmf_string_string.Swap(
1703 refl->GetMutableRepeatedFieldRef<Message>(
1704 &m2, fd_map_string_string));
1705 mmf_int32_foreign_message.Swap(
1706 refl->GetMutableRepeatedFieldRef<Message>(
1707 &m2, fd_map_int32_foreign_message));
1708 for (int i = 0; i < 10; ++i) {
1709 // Check the content of m0.
1710 EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20));
1711 EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20));
1712 EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
1713 EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
1714
1715 // Check the content of m2.
1716 EXPECT_EQ(Func(i, 11), m2.map_int32_int32().at(i + 10));
1717 EXPECT_EQ(Func(i, 12), m2.map_int32_double().at(i + 10));
1718 EXPECT_EQ(StrFunc(i, 15), m2.map_string_string().at(StrFunc(i + 10, 1)));
1719 EXPECT_EQ(Func(i, 16), m2.map_int32_foreign_message().at(i + 10).c());
1720 EXPECT_EQ(Func(i, 21), m2.map_int32_int32().at(i + 20));
1721 EXPECT_EQ(Func(i, 22), m2.map_int32_double().at(i + 20));
1722 EXPECT_EQ(StrFunc(i, 25), m2.map_string_string().at(StrFunc(i + 20, 1)));
1723 EXPECT_EQ(Func(i, 26), m2.map_int32_foreign_message().at(i + 20).c());
1724 }
1725
1726 // TODO(teboring): add test for duplicated key
1727}
1728
1729// Generated Message Test ===========================================
1730
1731TEST(GeneratedMapFieldTest, Accessors) {
1732 unittest::TestMap message;
1733
1734 MapTestUtil::SetMapFields(&message);
1735 MapTestUtil::ExpectMapFieldsSet(message);
1736
1737 MapTestUtil::ModifyMapFields(&message);
1738 MapTestUtil::ExpectMapFieldsModified(message);
1739}
1740
1741TEST(GeneratedMapFieldTest, SetMapFieldsInitialized) {
1742 unittest::TestMap message;
1743
1744 MapTestUtil::SetMapFieldsInitialized(&message);
1745 MapTestUtil::ExpectMapFieldsSetInitialized(message);
1746}
1747
1748TEST(GeneratedMapFieldTest, Proto2SetMapFieldsInitialized) {
Feng Xiaoeee38b02015-08-22 18:25:48 -07001749 unittest::TestEnumMap message;
1750 EXPECT_EQ(unittest::PROTO2_MAP_ENUM_FOO,
1751 (*message.mutable_known_map_field())[0]);
Feng Xiaof157a562014-11-14 11:50:31 -08001752}
1753
1754TEST(GeneratedMapFieldTest, Clear) {
1755 unittest::TestMap message;
1756
1757 MapTestUtil::SetMapFields(&message);
1758 message.Clear();
1759 MapTestUtil::ExpectClear(message);
1760}
1761
1762TEST(GeneratedMapFieldTest, ClearMessageMap) {
1763 unittest::TestMessageMap message;
1764
1765 // Creates a TestAllTypes with default value
1766 TestUtil::ExpectClear((*message.mutable_map_int32_message())[0]);
1767}
1768
1769TEST(GeneratedMapFieldTest, CopyFrom) {
1770 unittest::TestMap message1, message2;
1771
1772 MapTestUtil::SetMapFields(&message1);
1773 message2.CopyFrom(message1);
1774 MapTestUtil::ExpectMapFieldsSet(message2);
1775
1776 // Copying from self should be a no-op.
1777 message2.CopyFrom(message2);
1778 MapTestUtil::ExpectMapFieldsSet(message2);
1779}
1780
1781TEST(GeneratedMapFieldTest, CopyFromMessageMap) {
1782 unittest::TestMessageMap message1, message2;
1783
1784 (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
1785 (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
1786
1787 message1.CopyFrom(message2);
1788
1789 // Checks repeated field is overwritten.
1790 EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
1791 EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
1792}
1793
1794TEST(GeneratedMapFieldTest, SwapWithEmpty) {
1795 unittest::TestMap message1, message2;
1796
1797 MapTestUtil::SetMapFields(&message1);
1798 MapTestUtil::ExpectMapFieldsSet(message1);
1799 MapTestUtil::ExpectClear(message2);
1800
1801 message1.Swap(&message2);
1802 MapTestUtil::ExpectMapFieldsSet(message2);
1803 MapTestUtil::ExpectClear(message1);
1804}
1805
1806TEST(GeneratedMapFieldTest, SwapWithSelf) {
1807 unittest::TestMap message;
1808
1809 MapTestUtil::SetMapFields(&message);
1810 MapTestUtil::ExpectMapFieldsSet(message);
1811
1812 message.Swap(&message);
1813 MapTestUtil::ExpectMapFieldsSet(message);
1814}
1815
1816TEST(GeneratedMapFieldTest, SwapWithOther) {
1817 unittest::TestMap message1, message2;
1818
1819 MapTestUtil::SetMapFields(&message1);
1820 MapTestUtil::SetMapFields(&message2);
1821 MapTestUtil::ModifyMapFields(&message2);
1822
1823 message1.Swap(&message2);
1824 MapTestUtil::ExpectMapFieldsModified(message1);
1825 MapTestUtil::ExpectMapFieldsSet(message2);
1826}
1827
1828TEST(GeneratedMapFieldTest, CopyConstructor) {
1829 unittest::TestMap message1;
1830 MapTestUtil::SetMapFields(&message1);
1831
1832 unittest::TestMap message2(message1);
1833 MapTestUtil::ExpectMapFieldsSet(message2);
1834}
1835
1836TEST(GeneratedMapFieldTest, CopyAssignmentOperator) {
1837 unittest::TestMap message1;
1838 MapTestUtil::SetMapFields(&message1);
1839
1840 unittest::TestMap message2;
1841 message2 = message1;
1842 MapTestUtil::ExpectMapFieldsSet(message2);
1843
1844 // Make sure that self-assignment does something sane.
1845 message2.operator=(message2);
1846 MapTestUtil::ExpectMapFieldsSet(message2);
1847}
1848
1849#if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \
1850 !defined(GOOGLE_PROTOBUF_NO_RTTI)
1851TEST(GeneratedMapFieldTest, UpcastCopyFrom) {
1852 // Test the CopyFrom method that takes in the generic const Message&
1853 // parameter.
1854 unittest::TestMap message1, message2;
1855
1856 MapTestUtil::SetMapFields(&message1);
1857
1858 const Message* source = implicit_cast<const Message*>(&message1);
1859 message2.CopyFrom(*source);
1860
1861 MapTestUtil::ExpectMapFieldsSet(message2);
1862}
1863#endif
1864
1865#ifndef PROTOBUF_TEST_NO_DESCRIPTORS
1866
1867TEST(GeneratedMapFieldTest, CopyFromDynamicMessage) {
1868 // Test copying from a DynamicMessage, which must fall back to using
1869 // reflection.
1870 unittest::TestMap message2;
1871
1872 // Construct a new version of the dynamic message via the factory.
1873 DynamicMessageFactory factory;
1874 google::protobuf::scoped_ptr<Message> message1;
1875 message1.reset(
1876 factory.GetPrototype(unittest::TestMap::descriptor())->New());
Feng Xiaoeee38b02015-08-22 18:25:48 -07001877 MapReflectionTester reflection_tester(
Feng Xiaof157a562014-11-14 11:50:31 -08001878 unittest::TestMap::descriptor());
1879 reflection_tester.SetMapFieldsViaReflection(message1.get());
1880 reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
Feng Xiaoeee38b02015-08-22 18:25:48 -07001881 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
1882 message2.CopyFrom(*message1);
1883 MapTestUtil::ExpectMapFieldsSet(message2);
1884}
Feng Xiaof157a562014-11-14 11:50:31 -08001885
Feng Xiaoeee38b02015-08-22 18:25:48 -07001886TEST(GeneratedMapFieldTest, CopyFromDynamicMessageMapReflection) {
1887 unittest::TestMap message2;
1888
1889 // Construct a new version of the dynamic message via the factory.
1890 DynamicMessageFactory factory;
1891 google::protobuf::scoped_ptr<Message> message1;
1892 message1.reset(
1893 factory.GetPrototype(unittest::TestMap::descriptor())->New());
1894 MapReflectionTester reflection_tester(
1895 unittest::TestMap::descriptor());
1896 reflection_tester.SetMapFieldsViaMapReflection(message1.get());
1897 reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
1898 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
Feng Xiaof157a562014-11-14 11:50:31 -08001899 message2.CopyFrom(*message1);
1900 MapTestUtil::ExpectMapFieldsSet(message2);
1901}
1902
1903TEST(GeneratedMapFieldTest, DynamicMessageCopyFrom) {
1904 // Test copying to a DynamicMessage, which must fall back to using reflection.
1905 unittest::TestMap message2;
1906 MapTestUtil::SetMapFields(&message2);
1907
1908 // Construct a new version of the dynamic message via the factory.
1909 DynamicMessageFactory factory;
1910 google::protobuf::scoped_ptr<Message> message1;
1911 message1.reset(
1912 factory.GetPrototype(unittest::TestMap::descriptor())->New());
1913
Feng Xiaoeee38b02015-08-22 18:25:48 -07001914 MapReflectionTester reflection_tester(
Feng Xiaof157a562014-11-14 11:50:31 -08001915 unittest::TestMap::descriptor());
1916 message1->MergeFrom(message2);
1917 reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
Feng Xiaoeee38b02015-08-22 18:25:48 -07001918 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
1919}
1920
1921TEST(GeneratedMapFieldTest, DynamicMessageCopyFromMapReflection) {
1922 MapReflectionTester reflection_tester(
1923 unittest::TestMap::descriptor());
1924 unittest::TestMap message2;
1925 reflection_tester.SetMapFieldsViaMapReflection(&message2);
1926
1927 // Construct a dynamic message via the factory.
1928 DynamicMessageFactory factory;
1929 google::protobuf::scoped_ptr<Message> message1;
1930 message1.reset(
1931 factory.GetPrototype(unittest::TestMap::descriptor())->New());
1932
1933 message1->MergeFrom(message2);
1934 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
1935 reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
1936}
1937
1938TEST(GeneratedMapFieldTest, SyncDynamicMapWithRepeatedField) {
1939 // Construct a dynamic message via the factory.
1940 MapReflectionTester reflection_tester(
1941 unittest::TestMap::descriptor());
1942 DynamicMessageFactory factory;
1943 google::protobuf::scoped_ptr<Message> message;
1944 message.reset(
1945 factory.GetPrototype(unittest::TestMap::descriptor())->New());
1946 reflection_tester.SetMapFieldsViaReflection(message.get());
1947 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message.get());
1948 reflection_tester.ExpectMapFieldsSetViaReflection(*message);
Feng Xiaof157a562014-11-14 11:50:31 -08001949}
1950
1951#endif // !PROTOBUF_TEST_NO_DESCRIPTORS
1952
1953TEST(GeneratedMapFieldTest, NonEmptyMergeFrom) {
1954 unittest::TestMap message1, message2;
1955
1956 MapTestUtil::SetMapFields(&message1);
1957
1958 // This field will test merging into an empty spot.
1959 (*message2.mutable_map_int32_int32())[1] = 1;
1960 message1.mutable_map_int32_int32()->erase(1);
1961
1962 // This tests overwriting.
1963 (*message2.mutable_map_int32_double())[1] = 1;
1964 (*message1.mutable_map_int32_double())[1] = 2;
1965
1966 message1.MergeFrom(message2);
1967 MapTestUtil::ExpectMapFieldsSet(message1);
1968}
1969
1970TEST(GeneratedMapFieldTest, MergeFromMessageMap) {
1971 unittest::TestMessageMap message1, message2;
1972
1973 (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
1974 (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
1975
1976 message1.MergeFrom(message2);
1977
1978 // Checks repeated field is overwritten.
1979 EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
1980 EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
1981}
1982
1983// Test the generated SerializeWithCachedSizesToArray()
1984TEST(GeneratedMapFieldTest, SerializationToArray) {
1985 unittest::TestMap message1, message2;
1986 string data;
1987 MapTestUtil::SetMapFields(&message1);
1988 int size = message1.ByteSize();
1989 data.resize(size);
1990 uint8* start = reinterpret_cast<uint8*>(string_as_array(&data));
1991 uint8* end = message1.SerializeWithCachedSizesToArray(start);
1992 EXPECT_EQ(size, end - start);
1993 EXPECT_TRUE(message2.ParseFromString(data));
1994 MapTestUtil::ExpectMapFieldsSet(message2);
1995}
1996
1997// Test the generated SerializeWithCachedSizes()
1998TEST(GeneratedMapFieldTest, SerializationToStream) {
1999 unittest::TestMap message1, message2;
2000 MapTestUtil::SetMapFields(&message1);
2001 int size = message1.ByteSize();
2002 string data;
2003 data.resize(size);
2004 {
2005 // Allow the output stream to buffer only one byte at a time.
2006 io::ArrayOutputStream array_stream(string_as_array(&data), size, 1);
2007 io::CodedOutputStream output_stream(&array_stream);
2008 message1.SerializeWithCachedSizes(&output_stream);
2009 EXPECT_FALSE(output_stream.HadError());
2010 EXPECT_EQ(size, output_stream.ByteCount());
2011 }
2012 EXPECT_TRUE(message2.ParseFromString(data));
2013 MapTestUtil::ExpectMapFieldsSet(message2);
2014}
2015
2016
2017TEST(GeneratedMapFieldTest, SameTypeMaps) {
2018 const Descriptor* map1 = unittest::TestSameTypeMap::descriptor()
2019 ->FindFieldByName("map1")
2020 ->message_type();
2021 const Descriptor* map2 = unittest::TestSameTypeMap::descriptor()
2022 ->FindFieldByName("map2")
2023 ->message_type();
2024
2025 const Message* map1_entry =
2026 MessageFactory::generated_factory()->GetPrototype(map1);
2027 const Message* map2_entry =
2028 MessageFactory::generated_factory()->GetPrototype(map2);
2029
2030 EXPECT_EQ(map1, map1_entry->GetDescriptor());
2031 EXPECT_EQ(map2, map2_entry->GetDescriptor());
2032}
2033
2034TEST(GeneratedMapFieldTest, Proto2UnknownEnum) {
2035 unittest::TestEnumMapPlusExtra from;
2036 (*from.mutable_known_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_FOO;
2037 (*from.mutable_unknown_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_EXTRA;
2038 string data;
2039 from.SerializeToString(&data);
2040
2041 unittest::TestEnumMap to;
2042 EXPECT_TRUE(to.ParseFromString(data));
2043 EXPECT_EQ(0, to.unknown_map_field().size());
2044 const UnknownFieldSet& unknown_field_set =
2045 to.GetReflection()->GetUnknownFields(to);
2046 EXPECT_EQ(1, unknown_field_set.field_count());
2047 EXPECT_EQ(1, to.known_map_field().size());
2048 EXPECT_EQ(unittest::PROTO2_MAP_ENUM_FOO, to.known_map_field().at(0));
2049
2050 data.clear();
2051 from.Clear();
2052 to.SerializeToString(&data);
2053 EXPECT_TRUE(from.ParseFromString(data));
2054 EXPECT_EQ(0, from.GetReflection()->GetUnknownFields(from).field_count());
2055 EXPECT_EQ(1, from.known_map_field().size());
2056 EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_FOO, from.known_map_field().at(0));
2057 EXPECT_EQ(1, from.unknown_map_field().size());
2058 EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_EXTRA, from.unknown_map_field().at(0));
2059}
2060
2061TEST(GeneratedMapFieldTest, StandardWireFormat) {
2062 unittest::TestMap message;
2063 string data = "\x0A\x04\x08\x01\x10\x01";
2064
2065 EXPECT_TRUE(message.ParseFromString(data));
2066 EXPECT_EQ(1, message.map_int32_int32().size());
2067 EXPECT_EQ(1, message.map_int32_int32().at(1));
2068}
2069
2070TEST(GeneratedMapFieldTest, UnorderedWireFormat) {
2071 unittest::TestMap message;
2072
2073 // put value before key in wire format
2074 string data = "\x0A\x04\x10\x01\x08\x02";
2075
2076 EXPECT_TRUE(message.ParseFromString(data));
2077 EXPECT_EQ(1, message.map_int32_int32().size());
2078 EXPECT_EQ(1, message.map_int32_int32().at(2));
2079}
2080
2081TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) {
2082 unittest::TestMap message;
2083
2084 // Two key fields in wire format
2085 string data = "\x0A\x06\x08\x01\x08\x02\x10\x01";
2086
2087 EXPECT_TRUE(message.ParseFromString(data));
2088 EXPECT_EQ(1, message.map_int32_int32().size());
2089 EXPECT_EQ(1, message.map_int32_int32().at(2));
2090}
2091
2092TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) {
2093 unittest::TestMap message;
2094
2095 // Two value fields in wire format
2096 string data = "\x0A\x06\x08\x01\x10\x01\x10\x02";
2097
2098 EXPECT_TRUE(message.ParseFromString(data));
2099 EXPECT_EQ(1, message.map_int32_int32().size());
2100 EXPECT_EQ(2, message.map_int32_int32().at(1));
2101}
2102
2103TEST(GeneratedMapFieldTest, MissedKeyWireFormat) {
2104 unittest::TestMap message;
2105
2106 // No key field in wire format
2107 string data = "\x0A\x02\x10\x01";
2108
2109 EXPECT_TRUE(message.ParseFromString(data));
2110 EXPECT_EQ(1, message.map_int32_int32().size());
2111 EXPECT_EQ(1, message.map_int32_int32().at(0));
2112}
2113
2114TEST(GeneratedMapFieldTest, MissedValueWireFormat) {
2115 unittest::TestMap message;
2116
2117 // No value field in wire format
2118 string data = "\x0A\x02\x08\x01";
2119
2120 EXPECT_TRUE(message.ParseFromString(data));
2121 EXPECT_EQ(1, message.map_int32_int32().size());
2122 EXPECT_EQ(0, message.map_int32_int32().at(1));
2123}
2124
Jisi Liu885b6122015-02-28 14:51:22 -08002125TEST(GeneratedMapFieldTest, MissedValueTextFormat) {
2126 unittest::TestMap message;
2127
2128 // No value field in text format
2129 string text =
2130 "map_int32_foreign_message {\n"
2131 " key: 1234567890\n"
2132 "}";
2133
2134 EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(text, &message));
2135 EXPECT_EQ(1, message.map_int32_foreign_message().size());
2136 EXPECT_EQ(11, message.ByteSize());
2137}
2138
Feng Xiaof157a562014-11-14 11:50:31 -08002139TEST(GeneratedMapFieldTest, UnknownFieldWireFormat) {
2140 unittest::TestMap message;
2141
2142 // Unknown field in wire format
2143 string data = "\x0A\x06\x08\x02\x10\x03\x18\x01";
2144
2145 EXPECT_TRUE(message.ParseFromString(data));
2146 EXPECT_EQ(1, message.map_int32_int32().size());
2147 EXPECT_EQ(3, message.map_int32_int32().at(2));
2148}
2149
2150TEST(GeneratedMapFieldTest, CorruptedWireFormat) {
2151 unittest::TestMap message;
2152
2153 // corrupted data in wire format
2154 string data = "\x0A\x06\x08\x02\x11\x03";
2155
2156 EXPECT_FALSE(message.ParseFromString(data));
2157}
2158
Feng Xiao6ae3bde2014-11-25 14:01:44 -08002159TEST(GeneratedMapFieldTest, IsInitialized) {
2160 unittest::TestRequiredMessageMap map_message;
2161
2162 // Add an uninitialized message.
2163 (*map_message.mutable_map_field())[0];
2164 EXPECT_FALSE(map_message.IsInitialized());
2165
2166 // Initialize uninitialized message
2167 (*map_message.mutable_map_field())[0].set_a(0);
2168 (*map_message.mutable_map_field())[0].set_b(0);
2169 (*map_message.mutable_map_field())[0].set_c(0);
2170 EXPECT_TRUE(map_message.IsInitialized());
2171}
2172
Feng Xiaof157a562014-11-14 11:50:31 -08002173// Generated Message Reflection Test ================================
2174
2175TEST(GeneratedMapFieldReflectionTest, SpaceUsed) {
2176 unittest::TestMap message;
Feng Xiaoeee38b02015-08-22 18:25:48 -07002177 MapReflectionTester reflection_tester(
Feng Xiaof157a562014-11-14 11:50:31 -08002178 unittest::TestMap::descriptor());
2179 reflection_tester.SetMapFieldsViaReflection(&message);
2180
2181 EXPECT_LT(0, message.GetReflection()->SpaceUsed(message));
2182}
2183
2184TEST(GeneratedMapFieldReflectionTest, Accessors) {
2185 // Set every field to a unique value then go back and check all those
2186 // values.
2187 unittest::TestMap message;
Feng Xiaoeee38b02015-08-22 18:25:48 -07002188 MapReflectionTester reflection_tester(
Feng Xiaof157a562014-11-14 11:50:31 -08002189 unittest::TestMap::descriptor());
2190 reflection_tester.SetMapFieldsViaReflection(&message);
2191 MapTestUtil::ExpectMapFieldsSet(message);
2192 reflection_tester.ExpectMapFieldsSetViaReflection(message);
Feng Xiaoeee38b02015-08-22 18:25:48 -07002193 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message);
Feng Xiaof157a562014-11-14 11:50:31 -08002194
2195 reflection_tester.ModifyMapFieldsViaReflection(&message);
2196 MapTestUtil::ExpectMapFieldsModified(message);
2197}
2198
2199TEST(GeneratedMapFieldReflectionTest, Swap) {
2200 unittest::TestMap message1;
2201 unittest::TestMap message2;
2202
2203 MapTestUtil::SetMapFields(&message1);
2204
2205 const Reflection* reflection = message1.GetReflection();
2206 reflection->Swap(&message1, &message2);
2207
2208 MapTestUtil::ExpectClear(message1);
2209 MapTestUtil::ExpectMapFieldsSet(message2);
2210}
2211
2212TEST(GeneratedMapFieldReflectionTest, SwapWithBothSet) {
2213 unittest::TestMap message1;
2214 unittest::TestMap message2;
2215
2216 MapTestUtil::SetMapFields(&message1);
2217 MapTestUtil::SetMapFields(&message2);
2218 MapTestUtil::ModifyMapFields(&message2);
2219
2220 const Reflection* reflection = message1.GetReflection();
2221 reflection->Swap(&message1, &message2);
2222
2223 MapTestUtil::ExpectMapFieldsModified(message1);
2224 MapTestUtil::ExpectMapFieldsSet(message2);
2225}
2226
2227TEST(GeneratedMapFieldReflectionTest, SwapFields) {
2228 unittest::TestMap message1;
2229 unittest::TestMap message2;
2230
2231 MapTestUtil::SetMapFields(&message2);
2232
2233 vector<const FieldDescriptor*> fields;
2234 const Reflection* reflection = message1.GetReflection();
2235 reflection->ListFields(message2, &fields);
2236 reflection->SwapFields(&message1, &message2, fields);
2237
2238 MapTestUtil::ExpectMapFieldsSet(message1);
2239 MapTestUtil::ExpectClear(message2);
2240}
2241
2242TEST(GeneratedMapFieldReflectionTest, ClearField) {
2243 unittest::TestMap message;
2244 MapTestUtil::SetMapFields(&message);
2245 MapTestUtil::ExpectMapFieldsSet(message);
2246
Feng Xiaoeee38b02015-08-22 18:25:48 -07002247 MapReflectionTester reflection_tester(
Feng Xiaof157a562014-11-14 11:50:31 -08002248 unittest::TestMap::descriptor());
2249 reflection_tester.ClearMapFieldsViaReflection(&message);
Feng Xiaoeee38b02015-08-22 18:25:48 -07002250 reflection_tester.ExpectClearViaReflection(message);
2251 reflection_tester.ExpectClearViaReflectionIterator(&message);
Feng Xiaof157a562014-11-14 11:50:31 -08002252}
2253
2254TEST(GeneratedMapFieldReflectionTest, RemoveLast) {
2255 unittest::TestMap message;
Feng Xiaoeee38b02015-08-22 18:25:48 -07002256 MapReflectionTester reflection_tester(
Feng Xiaof157a562014-11-14 11:50:31 -08002257 unittest::TestMap::descriptor());
2258
2259 MapTestUtil::SetMapFields(&message);
2260 MapTestUtil::ExpectMapsSize(message, 2);
2261 std::vector<const Message*> expected_entries =
2262 MapTestUtil::GetMapEntries(message, 0);
2263
2264 reflection_tester.RemoveLastMapsViaReflection(&message);
2265
2266 MapTestUtil::ExpectMapsSize(message, 1);
2267 std::vector<const Message*> remained_entries =
2268 MapTestUtil::GetMapEntries(message, 0);
2269 EXPECT_TRUE(expected_entries == remained_entries);
2270}
2271
2272TEST(GeneratedMapFieldReflectionTest, ReleaseLast) {
2273 unittest::TestMap message;
2274 const Descriptor* descriptor = message.GetDescriptor();
Feng Xiaoeee38b02015-08-22 18:25:48 -07002275 MapReflectionTester reflection_tester(descriptor);
Feng Xiaof157a562014-11-14 11:50:31 -08002276
2277 MapTestUtil::SetMapFields(&message);
2278
2279 MapTestUtil::ExpectMapsSize(message, 2);
2280
2281 reflection_tester.ReleaseLastMapsViaReflection(&message);
2282
2283 MapTestUtil::ExpectMapsSize(message, 1);
2284
2285 // Now test that we actually release the right message.
2286 message.Clear();
2287 MapTestUtil::SetMapFields(&message);
2288
2289 MapTestUtil::ExpectMapsSize(message, 2);
2290 std::vector<const Message*> expect_last =
2291 MapTestUtil::GetMapEntries(message, 1);
2292 std::vector<const Message*> release_last =
2293 MapTestUtil::GetMapEntriesFromRelease(&message);
2294 MapTestUtil::ExpectMapsSize(message, 1);
2295 EXPECT_TRUE(expect_last == release_last);
2296 for (std::vector<const Message*>::iterator it = release_last.begin();
2297 it != release_last.end(); ++it) {
2298 delete *it;
2299 }
2300}
2301
2302TEST(GeneratedMapFieldReflectionTest, SwapElements) {
2303 unittest::TestMap message;
Feng Xiaoeee38b02015-08-22 18:25:48 -07002304 MapReflectionTester reflection_tester(
Feng Xiaof157a562014-11-14 11:50:31 -08002305 unittest::TestMap::descriptor());
2306
2307 MapTestUtil::SetMapFields(&message);
2308
2309 // Get pointers of map entries at their original position
2310 std::vector<const Message*> entries0 = MapTestUtil::GetMapEntries(message, 0);
2311 std::vector<const Message*> entries1 = MapTestUtil::GetMapEntries(message, 1);
2312
2313 // Swap the first time.
2314 reflection_tester.SwapMapsViaReflection(&message);
2315
2316 // Get pointer of map entry after swap once.
2317 std::vector<const Message*> entries0_once =
2318 MapTestUtil::GetMapEntries(message, 0);
2319 std::vector<const Message*> entries1_once =
2320 MapTestUtil::GetMapEntries(message, 1);
2321
2322 // Test map entries are swapped.
2323 MapTestUtil::ExpectMapsSize(message, 2);
2324 EXPECT_TRUE(entries0 == entries1_once);
2325 EXPECT_TRUE(entries1 == entries0_once);
2326
2327 // Swap the second time.
2328 reflection_tester.SwapMapsViaReflection(&message);
2329
2330 // Get pointer of map entry after swap once.
2331 std::vector<const Message*> entries0_twice =
2332 MapTestUtil::GetMapEntries(message, 0);
2333 std::vector<const Message*> entries1_twice =
2334 MapTestUtil::GetMapEntries(message, 1);
2335
2336 // Test map entries are swapped back.
2337 MapTestUtil::ExpectMapsSize(message, 2);
2338 EXPECT_TRUE(entries0 == entries0_twice);
2339 EXPECT_TRUE(entries1 == entries1_twice);
2340}
2341
2342TEST(GeneratedMapFieldReflectionTest, MutableUnknownFields) {
2343 unittest::TestMap message;
Feng Xiaoeee38b02015-08-22 18:25:48 -07002344 MapReflectionTester reflection_tester(
Feng Xiaof157a562014-11-14 11:50:31 -08002345 unittest::TestMap::descriptor());
2346 reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
2347}
2348
2349TEST(GeneratedMapFieldReflectionTest, EmbedProto2Message) {
2350 unittest::TestMessageMap message;
2351
2352 const FieldDescriptor* map_field =
2353 unittest::TestMessageMap::descriptor()->FindFieldByName(
2354 "map_int32_message");
2355 const FieldDescriptor* value =
2356 map_field->message_type()->FindFieldByName("value");
2357
2358 Message* entry_message =
2359 message.GetReflection()->AddMessage(&message, map_field);
2360 EXPECT_EQ(
2361 &entry_message->GetReflection()->GetMessage(*entry_message, value),
2362 reinterpret_cast<const Message*>(&TestAllTypes::default_instance()));
2363
2364 Message* proto2_message =
2365 entry_message->GetReflection()->MutableMessage(entry_message, value);
2366 EXPECT_EQ(unittest::TestAllTypes::descriptor(),
2367 proto2_message->GetDescriptor());
2368 ASSERT_EQ(1, message.map_int32_message().size());
2369}
2370
2371TEST(GeneratedMapFieldReflectionTest, MergeFromClearMapEntry) {
2372 unittest::TestMap message;
2373 const FieldDescriptor* map_field =
2374 unittest::TestMap::descriptor()->FindFieldByName("map_int32_int32");
2375 const FieldDescriptor* key =
2376 map_field->message_type()->FindFieldByName("key");
2377 const FieldDescriptor* value =
2378 map_field->message_type()->FindFieldByName("value");
2379
2380 Message* entry_message1 =
2381 message.GetReflection()->AddMessage(&message, map_field);
2382 EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
2383 EXPECT_FALSE(
2384 entry_message1->GetReflection()->HasField(*entry_message1, value));
2385
2386 Message* entry_message2 =
2387 message.GetReflection()->AddMessage(&message, map_field);
2388 EXPECT_FALSE(entry_message2->GetReflection()->HasField(*entry_message2, key));
2389 EXPECT_FALSE(
2390 entry_message2->GetReflection()->HasField(*entry_message2, value));
2391
2392 entry_message1->MergeFrom(*entry_message2);
2393 EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
2394 EXPECT_FALSE(
2395 entry_message1->GetReflection()->HasField(*entry_message1, value));
2396}
2397
2398TEST(GeneratedMapFieldReflectionTest, MapEntryClear) {
2399 unittest::TestMap message;
Feng Xiaoeee38b02015-08-22 18:25:48 -07002400 MapReflectionTester reflection_tester(
Feng Xiaof157a562014-11-14 11:50:31 -08002401 unittest::TestMap::descriptor());
2402 reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
2403}
2404
2405TEST(GeneratedMapFieldReflectionTest, Proto2MapEntryClear) {
Feng Xiaoeee38b02015-08-22 18:25:48 -07002406 unittest::TestEnumMap message;
Feng Xiaof157a562014-11-14 11:50:31 -08002407 const Descriptor* descriptor = message.GetDescriptor();
2408 const FieldDescriptor* field_descriptor =
Feng Xiaoeee38b02015-08-22 18:25:48 -07002409 descriptor->FindFieldByName("known_map_field");
Feng Xiaof157a562014-11-14 11:50:31 -08002410 const FieldDescriptor* value_descriptor =
2411 field_descriptor->message_type()->FindFieldByName("value");
2412 Message* sub_message =
2413 message.GetReflection()->AddMessage(&message, field_descriptor);
Feng Xiaoeee38b02015-08-22 18:25:48 -07002414 EXPECT_EQ(0, sub_message->GetReflection()->GetEnumValue(*sub_message,
Feng Xiaof157a562014-11-14 11:50:31 -08002415 value_descriptor));
2416}
2417
Feng Xiaoeee38b02015-08-22 18:25:48 -07002418// Map Reflection API Test =========================================
2419
2420TEST(GeneratedMapFieldReflectionTest, SetViaMapReflection) {
2421 unittest::TestMap message;
2422 MapReflectionTester reflection_tester(
2423 unittest::TestMap::descriptor());
2424 reflection_tester.SetMapFieldsViaMapReflection(&message);
2425 reflection_tester.ExpectMapFieldsSetViaReflection(message);
2426 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message);
2427}
2428
Feng Xiaof157a562014-11-14 11:50:31 -08002429// Dynamic Message Test =============================================
2430
2431class MapFieldInDynamicMessageTest : public testing::Test {
2432 protected:
2433 const DescriptorPool* pool_;
2434 DynamicMessageFactory factory_;
2435 const Descriptor* map_descriptor_;
Feng Xiaoeee38b02015-08-22 18:25:48 -07002436 const Descriptor* recursive_map_descriptor_;
Feng Xiaof157a562014-11-14 11:50:31 -08002437 const Message* map_prototype_;
2438
2439 MapFieldInDynamicMessageTest()
2440 : pool_(DescriptorPool::generated_pool()), factory_(pool_) {}
2441
2442 virtual void SetUp() {
2443 map_descriptor_ =
2444 pool_->FindMessageTypeByName("protobuf_unittest.TestMap");
Feng Xiaoeee38b02015-08-22 18:25:48 -07002445 recursive_map_descriptor_ =
2446 pool_->FindMessageTypeByName("protobuf_unittest.TestRecursiveMapMessage");
Feng Xiaof157a562014-11-14 11:50:31 -08002447 ASSERT_TRUE(map_descriptor_ != NULL);
Feng Xiaoeee38b02015-08-22 18:25:48 -07002448 ASSERT_TRUE(recursive_map_descriptor_ != NULL);
Feng Xiaof157a562014-11-14 11:50:31 -08002449 map_prototype_ = factory_.GetPrototype(map_descriptor_);
2450 }
2451};
2452
2453TEST_F(MapFieldInDynamicMessageTest, MapIndependentOffsets) {
2454 // Check that all fields have independent offsets by setting each
2455 // one to a unique value then checking that they all still have those
2456 // unique values (i.e. they don't stomp each other).
Jisi Liu46e8ff62015-10-05 11:59:43 -07002457 google::protobuf::scoped_ptr<Message> message(map_prototype_->New());
Feng Xiaoeee38b02015-08-22 18:25:48 -07002458 MapReflectionTester reflection_tester(map_descriptor_);
Feng Xiaof157a562014-11-14 11:50:31 -08002459
2460 reflection_tester.SetMapFieldsViaReflection(message.get());
2461 reflection_tester.ExpectMapFieldsSetViaReflection(*message);
2462}
2463
Feng Xiaoeee38b02015-08-22 18:25:48 -07002464TEST_F(MapFieldInDynamicMessageTest, DynamicMapReflection) {
Feng Xiaof157a562014-11-14 11:50:31 -08002465 // Check that map fields work properly.
Jisi Liu46e8ff62015-10-05 11:59:43 -07002466 google::protobuf::scoped_ptr<Message> message(map_prototype_->New());
Feng Xiaof157a562014-11-14 11:50:31 -08002467
2468 // Check set functions.
Feng Xiaoeee38b02015-08-22 18:25:48 -07002469 MapReflectionTester reflection_tester(map_descriptor_);
2470 reflection_tester.SetMapFieldsViaMapReflection(message.get());
Feng Xiaof157a562014-11-14 11:50:31 -08002471 reflection_tester.ExpectMapFieldsSetViaReflection(*message);
2472}
2473
2474TEST_F(MapFieldInDynamicMessageTest, MapSpaceUsed) {
2475 // Test that SpaceUsed() works properly
2476
2477 // Since we share the implementation with generated messages, we don't need
2478 // to test very much here. Just make sure it appears to be working.
2479
Jisi Liu46e8ff62015-10-05 11:59:43 -07002480 google::protobuf::scoped_ptr<Message> message(map_prototype_->New());
Feng Xiaoeee38b02015-08-22 18:25:48 -07002481 MapReflectionTester reflection_tester(map_descriptor_);
Feng Xiaof157a562014-11-14 11:50:31 -08002482
2483 int initial_space_used = message->SpaceUsed();
2484
2485 reflection_tester.SetMapFieldsViaReflection(message.get());
2486 EXPECT_LT(initial_space_used, message->SpaceUsed());
2487}
2488
Feng Xiaoeee38b02015-08-22 18:25:48 -07002489TEST_F(MapFieldInDynamicMessageTest, RecursiveMap) {
2490 TestRecursiveMapMessage from;
Feng Xiaod9ab86c2015-08-25 22:20:47 -07002491 (*from.mutable_a())[""];
Feng Xiaoeee38b02015-08-22 18:25:48 -07002492 string data = from.SerializeAsString();
2493 google::protobuf::scoped_ptr<Message> to(
2494 factory_.GetPrototype(recursive_map_descriptor_)->New());
2495 ASSERT_TRUE(to->ParseFromString(data));
2496}
2497
Feng Xiaof157a562014-11-14 11:50:31 -08002498// ReflectionOps Test ===============================================
2499
2500TEST(ReflectionOpsForMapFieldTest, MapSanityCheck) {
2501 unittest::TestMap message;
2502
2503 MapTestUtil::SetMapFields(&message);
2504 MapTestUtil::ExpectMapFieldsSet(message);
2505}
2506
2507TEST(ReflectionOpsForMapFieldTest, MapCopy) {
2508 unittest::TestMap message, message2;
2509
2510 MapTestUtil::SetMapFields(&message);
2511
2512 ReflectionOps::Copy(message, &message2);
2513
2514 MapTestUtil::ExpectMapFieldsSet(message2);
2515
2516 // Copying from self should be a no-op.
2517 ReflectionOps::Copy(message2, &message2);
2518 MapTestUtil::ExpectMapFieldsSet(message2);
2519}
2520
2521TEST(ReflectionOpsForMapFieldTest, MergeMap) {
2522 // Note: Copy is implemented in terms of Merge() so technically the Copy
2523 // test already tested most of this.
2524
2525 unittest::TestMap message, message2;
2526
2527 MapTestUtil::SetMapFields(&message);
2528
2529 ReflectionOps::Merge(message2, &message);
2530
2531 MapTestUtil::ExpectMapFieldsSet(message);
2532}
2533
2534TEST(ReflectionOpsForMapFieldTest, ClearMap) {
2535 unittest::TestMap message;
2536
2537 MapTestUtil::SetMapFields(&message);
2538
2539 ReflectionOps::Clear(&message);
2540
2541 MapTestUtil::ExpectClear(message);
2542}
2543
2544TEST(ReflectionOpsForMapFieldTest, MapDiscardUnknownFields) {
2545 unittest::TestMap message;
2546 MapTestUtil::SetMapFields(&message);
2547
2548 // Set some unknown fields in message.
2549 message.GetReflection()->MutableUnknownFields(&message)->
2550 AddVarint(123456, 654321);
2551
2552 // Discard them.
2553 ReflectionOps::DiscardUnknownFields(&message);
2554 MapTestUtil::ExpectMapFieldsSet(message);
2555
2556 EXPECT_EQ(0, message.GetReflection()->
2557 GetUnknownFields(message).field_count());
2558}
2559
2560// Wire Format Test =================================================
2561
2562TEST(WireFormatForMapFieldTest, ParseMap) {
2563 unittest::TestMap source, dest;
2564 string data;
2565
2566 // Serialize using the generated code.
2567 MapTestUtil::SetMapFields(&source);
2568 source.SerializeToString(&data);
2569
2570 // Parse using WireFormat.
2571 io::ArrayInputStream raw_input(data.data(), data.size());
2572 io::CodedInputStream input(&raw_input);
2573 WireFormat::ParseAndMergePartial(&input, &dest);
2574
2575 // Check.
2576 MapTestUtil::ExpectMapFieldsSet(dest);
2577}
2578
2579TEST(WireFormatForMapFieldTest, MapByteSize) {
2580 unittest::TestMap message;
2581 MapTestUtil::SetMapFields(&message);
2582
2583 EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
2584 message.Clear();
2585 EXPECT_EQ(0, message.ByteSize());
2586 EXPECT_EQ(0, WireFormat::ByteSize(message));
2587}
2588
2589TEST(WireFormatForMapFieldTest, SerializeMap) {
2590 unittest::TestMap message;
2591 string generated_data;
2592 string dynamic_data;
2593
2594 MapTestUtil::SetMapFields(&message);
2595
2596 // Serialize using the generated code.
2597 {
2598 message.ByteSize();
2599 io::StringOutputStream raw_output(&generated_data);
2600 io::CodedOutputStream output(&raw_output);
2601 message.SerializeWithCachedSizes(&output);
2602 ASSERT_FALSE(output.HadError());
2603 }
2604
2605 // Serialize using WireFormat.
2606 {
2607 io::StringOutputStream raw_output(&dynamic_data);
2608 io::CodedOutputStream output(&raw_output);
2609 int size = WireFormat::ByteSize(message);
2610 WireFormat::SerializeWithCachedSizes(message, size, &output);
2611 ASSERT_FALSE(output.HadError());
2612 }
2613
2614 // Should be the same.
2615 // Don't use EXPECT_EQ here because we're comparing raw binary data and
2616 // we really don't want it dumped to stdout on failure.
2617 EXPECT_TRUE(dynamic_data == generated_data);
2618}
2619
2620TEST(WireFormatForMapFieldTest, MapParseHelpers) {
2621 string data;
2622
2623 {
2624 // Set up.
2625 protobuf_unittest::TestMap message;
2626 MapTestUtil::SetMapFields(&message);
2627 message.SerializeToString(&data);
2628 }
2629
2630 {
2631 // Test ParseFromString.
2632 protobuf_unittest::TestMap message;
2633 EXPECT_TRUE(message.ParseFromString(data));
2634 MapTestUtil::ExpectMapFieldsSet(message);
2635 }
2636
2637 {
2638 // Test ParseFromIstream.
2639 protobuf_unittest::TestMap message;
2640 stringstream stream(data);
2641 EXPECT_TRUE(message.ParseFromIstream(&stream));
2642 EXPECT_TRUE(stream.eof());
2643 MapTestUtil::ExpectMapFieldsSet(message);
2644 }
2645
2646 {
2647 // Test ParseFromBoundedZeroCopyStream.
2648 string data_with_junk(data);
2649 data_with_junk.append("some junk on the end");
2650 io::ArrayInputStream stream(data_with_junk.data(), data_with_junk.size());
2651 protobuf_unittest::TestMap message;
2652 EXPECT_TRUE(message.ParseFromBoundedZeroCopyStream(&stream, data.size()));
2653 MapTestUtil::ExpectMapFieldsSet(message);
2654 }
2655
2656 {
2657 // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if
2658 // EOF is reached before the expected number of bytes.
2659 io::ArrayInputStream stream(data.data(), data.size());
2660 protobuf_unittest::TestAllTypes message;
2661 EXPECT_FALSE(
2662 message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1));
2663 }
2664}
2665
2666// Text Format Test =================================================
2667
2668TEST(TextFormatMapTest, SerializeAndParse) {
2669 unittest::TestMap source;
2670 unittest::TestMap dest;
2671 MapTestUtil::SetMapFields(&source);
2672 string output;
2673
2674 // Test compact ASCII
2675 TextFormat::Printer printer;
2676 printer.PrintToString(source, &output);
2677 TextFormat::Parser parser;
2678 EXPECT_TRUE(parser.ParseFromString(output, &dest));
2679 MapTestUtil::ExpectMapFieldsSet(dest);
2680}
2681
Bo Yang5db21732015-05-21 14:28:59 -07002682TEST(TextFormatMapTest, Sorted) {
2683 unittest::TestMap message;
Feng Xiaoeee38b02015-08-22 18:25:48 -07002684 MapReflectionTester tester(message.GetDescriptor());
Bo Yang5db21732015-05-21 14:28:59 -07002685 tester.SetMapFieldsViaReflection(&message);
2686
2687 string expected_text;
2688 GOOGLE_CHECK_OK(File::GetContents(
2689 TestSourceDir() +
2690 "/google/protobuf/"
2691 "testdata/map_test_data.txt",
2692 &expected_text, true));
2693
2694 EXPECT_EQ(message.DebugString(), expected_text);
2695
2696 // Test again on the reverse order.
2697 unittest::TestMap message2;
2698 tester.SetMapFieldsViaReflection(&message2);
2699 tester.SwapMapsViaReflection(&message2);
2700 EXPECT_EQ(message2.DebugString(), expected_text);
2701}
2702
Feng Xiaof157a562014-11-14 11:50:31 -08002703
Jisi Liu885b6122015-02-28 14:51:22 -08002704// arena support =================================================
2705TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) {
2706 // Allocate a large initial block to avoid mallocs during hooked test.
2707 std::vector<char> arena_block(128 * 1024);
2708 ArenaOptions options;
unknownca1c2522015-05-27 11:45:32 -07002709 options.initial_block = &arena_block[0];
Jisi Liu885b6122015-02-28 14:51:22 -08002710 options.initial_block_size = arena_block.size();
2711 Arena arena(options);
2712 string data;
2713 data.reserve(128 * 1024);
2714
2715 {
Feng Xiaoeee38b02015-08-22 18:25:48 -07002716 // TODO(teboring): Enable no heap check when ArenaStringPtr is used in map.
2717 // NoHeapChecker no_heap;
Jisi Liu885b6122015-02-28 14:51:22 -08002718
2719 unittest::TestArenaMap* from =
2720 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
2721 MapTestUtil::SetArenaMapFields(from);
2722 from->SerializeToString(&data);
2723
2724 unittest::TestArenaMap* to =
2725 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
2726 to->ParseFromString(data);
2727 MapTestUtil::ExpectArenaMapFieldsSet(*to);
2728 }
2729}
2730
2731// Use text format parsing and serializing to test reflection api.
2732TEST(ArenaTest, RelfectionInTextFormat) {
2733 Arena arena;
2734 string data;
2735
2736 TextFormat::Printer printer;
2737 TextFormat::Parser parser;
2738
2739 unittest::TestArenaMap* from =
2740 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
2741 unittest::TestArenaMap* to =
2742 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
2743
2744 MapTestUtil::SetArenaMapFields(from);
2745 printer.PrintToString(*from, &data);
2746
2747 EXPECT_TRUE(parser.ParseFromString(data, to));
2748 MapTestUtil::ExpectArenaMapFieldsSet(*to);
2749}
2750
Feng Xiaoeee38b02015-08-22 18:25:48 -07002751// Make sure the memory allocated for string in map is deallocated.
2752TEST(ArenaTest, StringMapNoLeak) {
2753 Arena arena;
2754 unittest::TestArenaMap* message =
2755 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
2756 string data;
2757 // String with length less than 16 will not be allocated from heap.
2758 int original_capacity = data.capacity();
2759 while (data.capacity() <= original_capacity) {
2760 data.append("a");
2761 }
2762 (*message->mutable_map_string_string())[data] = data;
2763 // We rely on heap checkers to detect memory leak for us.
2764 ASSERT_FALSE(message == NULL);
2765}
2766
Feng Xiaof157a562014-11-14 11:50:31 -08002767} // namespace internal
2768} // namespace protobuf
2769} // namespace google