1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // A hack to include windows.h first, which ensures the GetMessage macro can
32 // be undefined when we include <google/protobuf/stubs/common.h>
33 #if defined(_WIN32)
34 #define _WINSOCKAPI_ // to avoid re-definition in WinSock2.h
35 #define NOMINMAX // to avoid defining min/max macros
36 #include <windows.h>
37 #endif // _WIN32
38
39 #include <algorithm>
40 #include <google/protobuf/stubs/hash.h>
41 #include <map>
42 #include <memory>
43 #ifndef _SHARED_PTR_H
44 #include <google/protobuf/stubs/shared_ptr.h>
45 #endif
46 #include <set>
47 #include <sstream>
48 #include <vector>
49
50 #include <google/protobuf/stubs/casts.h>
51 #include <google/protobuf/stubs/logging.h>
52 #include <google/protobuf/stubs/common.h>
53 #include <google/protobuf/stubs/stringprintf.h>
54 #include <google/protobuf/testing/file.h>
55 #include <google/protobuf/arena_test_util.h>
56 #include <google/protobuf/map_proto2_unittest.pb.h>
57 #include <google/protobuf/map_unittest.pb.h>
58 #include <google/protobuf/map_test_util.h>
59 #include <google/protobuf/test_util.h>
60 #include <google/protobuf/unittest.pb.h>
61 #include <google/protobuf/descriptor.pb.h>
62 #include <google/protobuf/descriptor.h>
63 #include <google/protobuf/descriptor_database.h>
64 #include <google/protobuf/dynamic_message.h>
65 #include <google/protobuf/map.h>
66 #include <google/protobuf/map_field_inl.h>
67 #include <google/protobuf/message.h>
68 #include <google/protobuf/reflection.h>
69 #include <google/protobuf/reflection_ops.h>
70 #include <google/protobuf/text_format.h>
71 #include <google/protobuf/wire_format.h>
72 #include <google/protobuf/wire_format_lite_inl.h>
73 #include <google/protobuf/io/coded_stream.h>
74 #include <google/protobuf/io/tokenizer.h>
75 #include <google/protobuf/io/zero_copy_stream_impl.h>
76 #include <google/protobuf/util/time_util.h>
77 #include <google/protobuf/util/message_differencer.h>
78 #include <google/protobuf/stubs/strutil.h>
79 #include <google/protobuf/stubs/substitute.h>
80 #include <gmock/gmock.h>
81 #include <google/protobuf/testing/googletest.h>
82 #include <gtest/gtest.h>
83
84 namespace google {
85
86 using google::protobuf::unittest::ForeignMessage;
87 using google::protobuf::unittest::TestAllTypes;
88 using google::protobuf::unittest::TestMap;
89 using google::protobuf::unittest::TestRecursiveMapMessage;
90
91 namespace protobuf {
92 namespace internal {
93
94 // Map API Test =====================================================
95
96 // Parameterized tests on whether to use old style maps.
97 class MapImplTest : public testing::TestWithParam<bool> {
98 protected:
MapImplTest()99 MapImplTest()
100 : map_ptr_(new Map<int32, int32>(GetParam())),
101 map_(*map_ptr_),
102 const_map_(*map_ptr_) {
103 EXPECT_TRUE(map_.empty());
104 EXPECT_EQ(0, map_.size());
105 }
~MapImplTest()106 ~MapImplTest() {}
107
ExpectSingleElement(int32 key,int32 value)108 void ExpectSingleElement(int32 key, int32 value) {
109 EXPECT_FALSE(map_.empty());
110 EXPECT_EQ(1, map_.size());
111 ExpectElement(key, value);
112 }
113
ExpectElements(const std::map<int32,int32> & map)114 void ExpectElements(const std::map<int32, int32>& map) {
115 EXPECT_FALSE(map_.empty());
116 EXPECT_EQ(map.size(), map_.size());
117 for (std::map<int32, int32>::const_iterator it = map.begin();
118 it != map.end(); ++it) {
119 ExpectElement(it->first, it->second);
120 }
121 }
122
ExpectElement(int32 key,int32 value)123 void ExpectElement(int32 key, int32 value) {
124 // Test map size is correct.
125 EXPECT_EQ(value, map_[key]);
126 EXPECT_EQ(1, map_.count(key));
127
128 // Check mutable at and find work correctly.
129 EXPECT_EQ(value, map_.at(key));
130 Map<int32, int32>::iterator it = map_.find(key);
131
132 // interator dereferenceable
133 EXPECT_EQ(key, (*it).first);
134 EXPECT_EQ(value, (*it).second);
135 EXPECT_EQ(key, it->first);
136 EXPECT_EQ(value, it->second);
137
138 // iterator mutable
139 ((*it).second) = value + 1;
140 EXPECT_EQ(value + 1, map_[key]);
141 ((*it).second) = value;
142 EXPECT_EQ(value, map_[key]);
143
144 it->second = value + 1;
145 EXPECT_EQ(value + 1, map_[key]);
146 it->second = value;
147 EXPECT_EQ(value, map_[key]);
148
149 // copy constructor
150 Map<int32, int32>::iterator it_copy = it;
151 EXPECT_EQ(key, it_copy->first);
152 EXPECT_EQ(value, it_copy->second);
153
154 // Immutable API ================================================
155
156 // Check immutable at and find work correctly.
157 EXPECT_EQ(value, const_map_.at(key));
158 Map<int32, int32>::const_iterator const_it = const_map_.find(key);
159
160 // interator dereferenceable
161 EXPECT_EQ(key, (*const_it).first);
162 EXPECT_EQ(value, (*const_it).second);
163 EXPECT_EQ(key, const_it->first);
164 EXPECT_EQ(value, const_it->second);
165
166 // copy constructor
167 Map<int32, int32>::const_iterator const_it_copy = const_it;
168 EXPECT_EQ(key, const_it_copy->first);
169 EXPECT_EQ(value, const_it_copy->second);
170 }
171
172 google::protobuf::scoped_ptr<Map<int32, int32> > map_ptr_;
173 Map<int32, int32>& map_;
174 const Map<int32, int32>& const_map_;
175 };
176
TEST_P(MapImplTest,OperatorBracket)177 TEST_P(MapImplTest, OperatorBracket) {
178 int32 key = 0;
179 int32 value1 = 100;
180 int32 value2 = 101;
181
182 EXPECT_EQ(0, map_[key]);
183
184 map_[key] = value1;
185 ExpectSingleElement(key, value1);
186
187 map_[key] = value2;
188 ExpectSingleElement(key, value2);
189 }
190
TEST_P(MapImplTest,OperatorBracketNonExist)191 TEST_P(MapImplTest, OperatorBracketNonExist) {
192 int32 key = 0;
193 int32 default_value = 0;
194
195 EXPECT_EQ(default_value, map_[key]);
196 ExpectSingleElement(key, default_value);
197 }
198
TEST_P(MapImplTest,MutableAt)199 TEST_P(MapImplTest, MutableAt) {
200 int32 key = 0;
201 int32 value1 = 100;
202 int32 value2 = 101;
203
204 map_[key] = value1;
205 ExpectSingleElement(key, value1);
206
207 map_.at(key) = value2;
208 ExpectSingleElement(key, value2);
209 }
210
211 #ifdef PROTOBUF_HAS_DEATH_TEST
212
TEST_P(MapImplTest,MutableAtNonExistDeathTest)213 TEST_P(MapImplTest, MutableAtNonExistDeathTest) {
214 EXPECT_DEATH(map_.at(0), "");
215 }
216
TEST_P(MapImplTest,ImmutableAtNonExistDeathTest)217 TEST_P(MapImplTest, ImmutableAtNonExistDeathTest) {
218 EXPECT_DEATH(const_map_.at(0), "");
219 }
220
TEST_P(MapImplTest,UsageErrors)221 TEST_P(MapImplTest, UsageErrors) {
222 MapKey key;
223 key.SetInt64Value(1);
224 EXPECT_DEATH(key.GetUInt64Value(),
225 "Protocol Buffer map usage error:\n"
226 "MapKey::GetUInt64Value type does not match\n"
227 " Expected : uint64\n"
228 " Actual : int64");
229
230 MapValueRef value;
231 EXPECT_DEATH(value.SetFloatValue(0.1),
232 "Protocol Buffer map usage error:\n"
233 "MapValueRef::type MapValueRef is not initialized.");
234 }
235
236 #endif // PROTOBUF_HAS_DEATH_TEST
237
TEST_P(MapImplTest,CountNonExist)238 TEST_P(MapImplTest, CountNonExist) {
239 EXPECT_EQ(0, map_.count(0));
240 }
241
TEST_P(MapImplTest,MutableFindNonExist)242 TEST_P(MapImplTest, MutableFindNonExist) {
243 EXPECT_TRUE(map_.end() == map_.find(0));
244 }
245
TEST_P(MapImplTest,ImmutableFindNonExist)246 TEST_P(MapImplTest, ImmutableFindNonExist) {
247 EXPECT_TRUE(const_map_.end() == const_map_.find(0));
248 }
249
TEST_P(MapImplTest,ConstEnd)250 TEST_P(MapImplTest, ConstEnd) {
251 EXPECT_TRUE(const_map_.end() == const_map_.cend());
252 }
253
TEST_P(MapImplTest,GetReferenceFromIterator)254 TEST_P(MapImplTest, GetReferenceFromIterator) {
255 for (int i = 0; i < 10; i++) {
256 map_[i] = i;
257 }
258
259 for (Map<int32, int32>::const_iterator it = map_.cbegin();
260 it != map_.cend();) {
261 Map<int32, int32>::const_reference entry = *it++;
262 EXPECT_EQ(entry.first, entry.second);
263 }
264
265 for (Map<int32, int32>::const_iterator it = const_map_.begin();
266 it != const_map_.end();) {
267 Map<int32, int32>::const_reference entry = *it++;
268 EXPECT_EQ(entry.first, entry.second);
269 }
270
271 for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) {
272 Map<int32, int32>::reference entry = *it++;
273 EXPECT_EQ(entry.first + 1, ++entry.second);
274 }
275 }
276
TEST_P(MapImplTest,IteratorBasic)277 TEST_P(MapImplTest, IteratorBasic) {
278 map_[0] = 0;
279
280 // Default constructible (per forward iterator requirements).
281 Map<int, int>::const_iterator cit;
282 Map<int, int>::iterator it;
283
284 it = map_.begin();
285 cit = it; // Converts to const_iterator
286
287 // Can compare between them.
288 EXPECT_TRUE(it == cit);
289 EXPECT_FALSE(cit != it);
290
291 // Pre increment.
292 EXPECT_FALSE(it == ++cit);
293
294 // Post increment.
295 EXPECT_FALSE(it++ == cit);
296 EXPECT_TRUE(it == cit);
297 }
298
299 template <typename Iterator>
median(Iterator i0,Iterator i1)300 static int64 median(Iterator i0, Iterator i1) {
301 vector<int64> v(i0, i1);
302 std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end());
303 return v[v.size() / 2];
304 }
305
Now()306 static int64 Now() {
307 return google::protobuf::util::TimeUtil::TimestampToNanoseconds(
308 google::protobuf::util::TimeUtil::GetCurrentTime());
309 }
310
311 // Arbitrary odd integers for creating test data.
312 static int k0 = 812398771;
313 static int k1 = 1312938717;
314 static int k2 = 1321555333;
315
316 // A naive begin() implementation will cause begin() to get slower and slower
317 // if one erases elements at the "front" of the hash map, and we'd like to
318 // avoid that, as std::unordered_map does.
TEST_P(MapImplTest,BeginIsFast)319 TEST_P(MapImplTest, BeginIsFast) {
320 // Disable this test for both new and old implementations.
321 if (/*GetParam()*/true) return;
322 Map<int32, int32> map(false); // This test uses new-style maps only.
323 const int kTestSize = 250000;
324 // Create a random-looking map of size n. Use non-negative integer keys.
325 uint32 frog = 123983;
326 int last_key = 0;
327 int counter = 0;
328 while (map.size() < kTestSize) {
329 frog *= static_cast<uint32>(k0);
330 frog ^= frog >> 17;
331 frog += counter++;
332 last_key =
333 static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1;
334 GOOGLE_DCHECK_GE(last_key, 0);
335 map[last_key] = last_key ^ 1;
336 }
337 vector<int64> times;
338 // We're going to do map.erase(map.begin()) over and over again. But,
339 // just in case one iteration is fast compared to the granularity of
340 // our time keeping, we measure kChunkSize iterations per outer-loop iter.
341 const int kChunkSize = 1000;
342 GOOGLE_CHECK_EQ(kTestSize % kChunkSize, 0);
343 do {
344 const int64 start = Now();
345 for (int i = 0; i < kChunkSize; i++) {
346 map.erase(map.begin());
347 }
348 const int64 end = Now();
349 if (end > start) {
350 times.push_back(end - start);
351 }
352 } while (!map.empty());
353 if (times.size() < .99 * kTestSize / kChunkSize) {
354 GOOGLE_LOG(WARNING) << "Now() isn't helping us measure time";
355 return;
356 }
357 int64 x0 = median(times.begin(), times.begin() + 9);
358 int64 x1 = median(times.begin() + times.size() - 9, times.end());
359 GOOGLE_LOG(INFO) << "x0=" << x0 << ", x1=" << x1;
360 // x1 will greatly exceed x0 if the code we just executed took O(n^2) time.
361 // And we'll probably time out and never get here. So, this test is
362 // intentionally loose: we check that x0 and x1 are within a factor of 8.
363 EXPECT_GE(x1, x0 / 8);
364 EXPECT_GE(x0, x1 / 8);
365 }
366
367 // Try to create kTestSize keys that will land in just a few buckets, and
368 // time the insertions, to get a rough estimate of whether an O(n^2) worst case
369 // was triggered. This test is a hacky, but probably better than nothing.
TEST_P(MapImplTest,HashFlood)370 TEST_P(MapImplTest, HashFlood) {
371 const int kTestSize = 1024; // must be a power of 2
372 std::set<int> s;
373 for (int i = 0; s.size() < kTestSize; i++) {
374 if ((map_.hash_function()(i) & (kTestSize - 1)) < 3) {
375 s.insert(i);
376 }
377 }
378 // Create hash table with kTestSize entries that hash flood a table with
379 // 1024 (or 512 or 2048 or ...) entries. This assumes that map_ uses powers
380 // of 2 for table sizes, and that it's sufficient to "flood" with respect to
381 // the low bits of the output of map_.hash_function().
382 vector<int64> times;
383 std::set<int>::iterator it = s.begin();
384 int count = 0;
385 do {
386 const int64 start = Now();
387 map_[*it] = 0;
388 const int64 end = Now();
389 if (end > start) {
390 times.push_back(end - start);
391 }
392 ++count;
393 ++it;
394 } while (it != s.end());
395 if (times.size() < .99 * count) return;
396 int64 x0 = median(times.begin(), times.begin() + 9);
397 int64 x1 = median(times.begin() + times.size() - 9, times.end());
398 // x1 will greatly exceed x0 if the code we just executed took O(n^2) time.
399 // But we want to allow O(n log n). A factor of 20 should be generous enough.
400 EXPECT_LE(x1, x0 * 20);
401 }
402
403 template <typename T, typename U>
TestValidityForAllKeysExcept(int key_to_avoid,const T & check_map,const U & map)404 static void TestValidityForAllKeysExcept(int key_to_avoid,
405 const T& check_map,
406 const U& map) {
407 typedef typename U::value_type value_type; // a key-value pair
408 for (typename U::const_iterator it = map.begin(); it != map.end(); ++it) {
409 const int key = it->first;
410 if (key == key_to_avoid) continue;
411 // All iterators relevant to this key, whether old (from check_map) or new,
412 // must point to the same memory. So, test pointer equality here.
413 const value_type* check_val = &*check_map.find(key)->second;
414 EXPECT_EQ(check_val, &*it);
415 EXPECT_EQ(check_val, &*map.find(key));
416 }
417 }
418
419 // EXPECT i0 and i1 to be the same. Advancing them should have the same effect,
420 // too.
421 template <typename Iter>
TestEqualIterators(Iter i0,Iter i1,Iter end)422 static void TestEqualIterators(Iter i0, Iter i1, Iter end) {
423 const int kMaxAdvance = 10;
424 for (int i = 0; i < kMaxAdvance; i++) {
425 EXPECT_EQ(i0 == end, i1 == end);
426 if (i0 == end) return;
427 EXPECT_EQ(&*i0, &*i1) << "iter " << i;
428 ++i0;
429 ++i1;
430 }
431 }
432
433 template <typename IteratorType>
TestOldVersusNewIterator(int skip,Map<int,int> * m)434 static void TestOldVersusNewIterator(int skip, Map<int, int>* m) {
435 const int initial_size = m->size();
436 IteratorType it = m->begin();
437 for (int i = 0; i < skip && it != m->end(); it++, i++) {}
438 if (it == m->end()) return;
439 const IteratorType old = it;
440 GOOGLE_LOG(INFO) << "skip=" << skip << ", old->first=" << old->first;
441 const int target_size =
442 initial_size < 100 ? initial_size * 5 : initial_size * 5 / 4;
443 for (int i = 0; m->size() <= target_size; i++) {
444 (*m)[i] = 0;
445 }
446 // Iterator 'old' should still work just fine despite the growth of *m.
447 const IteratorType after_growth = m->find(old->first);
448 TestEqualIterators<IteratorType>(old, after_growth, m->end());
449
450 // Now shrink the number of elements. Do this with a mix of erases and
451 // inserts to increase the chance that the hashtable will resize to a lower
452 // number of buckets. (But, in any case, the test is still useful.)
453 for (int i = 0; i < 2 * (target_size - initial_size); i++) {
454 if (i != old->first) {
455 m->erase(i);
456 }
457 if (((i ^ m->begin()->first) & 15) == 0) {
458 (*m)[i * 342] = i;
459 }
460 }
461 // Now, the table has grown and shrunk; test again.
462 TestEqualIterators<IteratorType>(old, m->find(old->first), m->end());
463 TestEqualIterators<IteratorType>(old, after_growth, m->end());
464 }
465
466 // Create and test an n-element Map, with emphasis on iterator correctness.
StressTestIterators(int n,bool test_old_style_proto2_maps)467 static void StressTestIterators(int n, bool test_old_style_proto2_maps) {
468 GOOGLE_LOG(INFO) << "StressTestIterators " << n;
469 GOOGLE_CHECK_GT(n, 0);
470 // Create a random-looking map of size n. Use non-negative integer keys.
471 Map<int, int> m(test_old_style_proto2_maps);
472 uint32 frog = 123987 + n;
473 int last_key = 0;
474 int counter = 0;
475 while (m.size() < n) {
476 frog *= static_cast<uint32>(k0);
477 frog ^= frog >> 17;
478 frog += counter++;
479 last_key =
480 static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1;
481 GOOGLE_DCHECK_GE(last_key, 0);
482 m[last_key] = last_key ^ 1;
483 }
484 // Test it.
485 ASSERT_EQ(n, m.size());
486 // Create maps of pointers and iterators.
487 // These should remain valid even if we modify m.
488 hash_map<int, Map<int, int>::value_type*> mp(n);
489 hash_map<int, Map<int, int>::iterator> mi(n);
490 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
491 mp[it->first] = &*it;
492 mi[it->first] = it;
493 }
494 ASSERT_EQ(m.size(), mi.size());
495 ASSERT_EQ(m.size(), mp.size());
496 m.erase(last_key);
497 ASSERT_EQ(n - 1, m.size());
498 TestValidityForAllKeysExcept(last_key, mp, m);
499 TestValidityForAllKeysExcept(last_key, mi, m);
500
501 m[last_key] = 0;
502 ASSERT_EQ(n, m.size());
503 // Test old iterator vs new iterator, with table modification in between.
504 TestOldVersusNewIterator<Map<int, int>::const_iterator>(n % 3, &m);
505 TestOldVersusNewIterator<Map<int, int>::iterator>(n % (1 + (n / 40)), &m);
506 // Finally, ensure erase(iterator) doesn't reorder anything, because that is
507 // what its documentation says.
508 m[last_key] = m[last_key ^ 999] = 0;
509 vector<Map<int, int>::iterator> v;
510 v.reserve(m.size());
511 int position_of_last_key = 0;
512 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
513 if (it->first == last_key) {
514 position_of_last_key = v.size();
515 }
516 v.push_back(it);
517 }
518 ASSERT_EQ(m.size(), v.size());
519 const Map<int, int>::iterator erase_result = m.erase(m.find(last_key));
520 int index = 0;
521 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it, ++index) {
522 if (index == position_of_last_key) {
523 EXPECT_EQ(&*erase_result, &*v[++index]);
524 }
525 ASSERT_EQ(&*it, &*v[index]);
526 }
527 }
528
TEST_P(MapImplTest,IteratorInvalidation)529 TEST_P(MapImplTest, IteratorInvalidation) {
530 // As multiple underlying hash_map implementations do not follow the
531 // validation requirement, the test is disabled for old-style maps.
532 if (GetParam()) return;
533 // Create a set of pseudo-random sizes to test.
534 #ifndef NDEBUG
535 const int kMaxSizeToTest = 100 * 1000;
536 #else
537 const int kMaxSizeToTest = 1000 * 1000;
538 #endif
539 std::set<int> s;
540 int n = kMaxSizeToTest;
541 int frog = k1 + n;
542 while (n > 1 && s.size() < 25) {
543 s.insert(n);
544 n = static_cast<int>(n * 100 / (101.0 + (frog & 63)));
545 frog *= k2;
546 frog ^= frog >> 17;
547 }
548 // Ensure we test a few small sizes.
549 s.insert(1);
550 s.insert(2);
551 s.insert(3);
552 // Now, the real work.
553 for (std::set<int>::iterator i = s.begin(); i != s.end(); ++i) {
554 StressTestIterators(*i, GetParam());
555 }
556 }
557
558 // Test that erase() revalidates iterators.
TEST_P(MapImplTest,EraseRevalidates)559 TEST_P(MapImplTest, EraseRevalidates) {
560 // As multiple underlying hash_map implementations do not follow the
561 // validation requirement, the test is disabled for old-style maps.
562 if (GetParam()) return;
563 map_[3] = map_[13] = map_[20] = 0;
564 const int initial_size = map_.size();
565 EXPECT_EQ(3, initial_size);
566 vector<Map<int, int>::iterator> v;
567 for (Map<int, int>::iterator it = map_.begin(); it != map_.end(); ++it) {
568 v.push_back(it);
569 }
570 EXPECT_EQ(initial_size, v.size());
571 for (int i = 0; map_.size() <= initial_size * 20; i++) {
572 map_[i] = 0;
573 }
574 const int larger_size = map_.size();
575 // We've greatly increased the size of the map, so it is highly likely that
576 // the following will corrupt m if erase() doesn't properly revalidate
577 // iterators passed to it. Finishing this routine without crashing indicates
578 // success.
579 for (int i = 0; i < v.size(); i++) {
580 map_.erase(v[i]);
581 }
582 EXPECT_EQ(larger_size - v.size(), map_.size());
583 }
584
585 template <typename T>
IsConstHelper(T &)586 bool IsConstHelper(T& /*t*/) { // NOLINT. We want to catch non-const refs here.
587 return false;
588 }
589 template <typename T>
IsConstHelper(const T &)590 bool IsConstHelper(const T& /*t*/) {
591 return true;
592 }
593
TEST_P(MapImplTest,IteratorConstness)594 TEST_P(MapImplTest, IteratorConstness) {
595 map_[0] = 0;
596 EXPECT_TRUE(IsConstHelper(*map_.cbegin()));
597 EXPECT_TRUE(IsConstHelper(*const_map_.begin()));
598 EXPECT_FALSE(IsConstHelper(*map_.begin()));
599 }
600
IsForwardIteratorHelper(std::forward_iterator_tag)601 bool IsForwardIteratorHelper(std::forward_iterator_tag /*tag*/) { return true; }
602 template <typename T>
IsForwardIteratorHelper(T)603 bool IsForwardIteratorHelper(T /*t*/) {
604 return false;
605 }
606
TEST_P(MapImplTest,IteratorCategory)607 TEST_P(MapImplTest, IteratorCategory) {
608 EXPECT_TRUE(IsForwardIteratorHelper(
609 std::iterator_traits<Map<int, int>::iterator>::iterator_category()));
610 EXPECT_TRUE(IsForwardIteratorHelper(std::iterator_traits<
611 Map<int, int>::const_iterator>::iterator_category()));
612 }
613
TEST_P(MapImplTest,InsertSingle)614 TEST_P(MapImplTest, InsertSingle) {
615 int32 key = 0;
616 int32 value1 = 100;
617 int32 value2 = 101;
618
619 // Insert a non-existed key.
620 std::pair<Map<int32, int32>::iterator, bool> result1 =
621 map_.insert(Map<int32, int32>::value_type(key, value1));
622 ExpectSingleElement(key, value1);
623
624 Map<int32, int32>::iterator it1 = result1.first;
625 EXPECT_EQ(key, it1->first);
626 EXPECT_EQ(value1, it1->second);
627 EXPECT_TRUE(result1.second);
628
629 // Insert an existed key.
630 std::pair<Map<int32, int32>::iterator, bool> result2 =
631 map_.insert(Map<int32, int32>::value_type(key, value2));
632 ExpectSingleElement(key, value1);
633
634 Map<int32, int32>::iterator it2 = result2.first;
635 EXPECT_TRUE(it1 == it2);
636 EXPECT_FALSE(result2.second);
637 }
638
TEST_P(MapImplTest,InsertByIterator)639 TEST_P(MapImplTest, InsertByIterator) {
640 int32 key1 = 0;
641 int32 key2 = 1;
642 int32 value1a = 100;
643 int32 value1b = 101;
644 int32 value2a = 200;
645 int32 value2b = 201;
646
647 std::map<int32, int32> map1;
648 map1[key1] = value1a;
649 map1[key2] = value2a;
650
651 map_.insert(map1.begin(), map1.end());
652 ExpectElements(map1);
653
654 std::map<int32, int32> map2;
655 map2[key1] = value1b;
656 map2[key2] = value2b;
657
658 map_.insert(map2.begin(), map2.end());
659 ExpectElements(map1);
660 }
661
TEST_P(MapImplTest,EraseSingleByKey)662 TEST_P(MapImplTest, EraseSingleByKey) {
663 int32 key = 0;
664 int32 value = 100;
665
666 map_[key] = value;
667 ExpectSingleElement(key, value);
668
669 // Erase an existing key.
670 EXPECT_EQ(1, map_.erase(key));
671 EXPECT_TRUE(map_.empty());
672 EXPECT_EQ(0, map_.size());
673 EXPECT_TRUE(map_.end() == map_.find(key));
674 EXPECT_TRUE(map_.begin() == map_.end());
675
676 // Erase a non-existing key.
677 EXPECT_EQ(0, map_.erase(key));
678 }
679
TEST_P(MapImplTest,EraseMutipleByKey)680 TEST_P(MapImplTest, EraseMutipleByKey) {
681 // erase in one specific order to trigger corner cases
682 for (int i = 0; i < 5; i++) {
683 map_[i] = i;
684 }
685
686 map_.erase(0);
687 EXPECT_EQ(4, map_.size());
688 EXPECT_TRUE(map_.end() == map_.find(0));
689
690 map_.erase(1);
691 EXPECT_EQ(3, map_.size());
692 EXPECT_TRUE(map_.end() == map_.find(1));
693
694 map_.erase(3);
695 EXPECT_EQ(2, map_.size());
696 EXPECT_TRUE(map_.end() == map_.find(3));
697
698 map_.erase(4);
699 EXPECT_EQ(1, map_.size());
700 EXPECT_TRUE(map_.end() == map_.find(4));
701
702 map_.erase(2);
703 EXPECT_EQ(0, map_.size());
704 EXPECT_TRUE(map_.end() == map_.find(2));
705 }
706
TEST_P(MapImplTest,EraseSingleByIterator)707 TEST_P(MapImplTest, EraseSingleByIterator) {
708 int32 key = 0;
709 int32 value = 100;
710
711 map_[key] = value;
712 ExpectSingleElement(key, value);
713
714 Map<int32, int32>::iterator it = map_.find(key);
715 map_.erase(it);
716 EXPECT_TRUE(map_.empty());
717 EXPECT_EQ(0, map_.size());
718 EXPECT_TRUE(map_.end() == map_.find(key));
719 EXPECT_TRUE(map_.begin() == map_.end());
720 }
721
TEST_P(MapImplTest,ValidIteratorAfterErase)722 TEST_P(MapImplTest, ValidIteratorAfterErase) {
723 for (int i = 0; i < 10; i++) {
724 map_[i] = i;
725 }
726
727 int count = 0;
728
729 for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) {
730 count++;
731 if (it->first % 2 == 1) {
732 map_.erase(it++);
733 } else {
734 ++it;
735 }
736 }
737
738 EXPECT_EQ(10, count);
739 EXPECT_EQ(5, map_.size());
740 }
741
TEST_P(MapImplTest,EraseByIterator)742 TEST_P(MapImplTest, EraseByIterator) {
743 int32 key1 = 0;
744 int32 key2 = 1;
745 int32 value1 = 100;
746 int32 value2 = 101;
747
748 std::map<int32, int32> map;
749 map[key1] = value1;
750 map[key2] = value2;
751
752 map_.insert(map.begin(), map.end());
753 ExpectElements(map);
754
755 map_.erase(map_.begin(), map_.end());
756 EXPECT_TRUE(map_.empty());
757 EXPECT_EQ(0, map_.size());
758 EXPECT_TRUE(map_.end() == map_.find(key1));
759 EXPECT_TRUE(map_.end() == map_.find(key2));
760 EXPECT_TRUE(map_.begin() == map_.end());
761 }
762
TEST_P(MapImplTest,Clear)763 TEST_P(MapImplTest, Clear) {
764 int32 key = 0;
765 int32 value = 100;
766
767 map_[key] = value;
768 ExpectSingleElement(key, value);
769
770 map_.clear();
771
772 EXPECT_TRUE(map_.empty());
773 EXPECT_EQ(0, map_.size());
774 EXPECT_TRUE(map_.end() == map_.find(key));
775 EXPECT_TRUE(map_.begin() == map_.end());
776 }
777
CopyConstructorHelper(Arena * arena,Map<int32,int32> * m)778 static void CopyConstructorHelper(Arena* arena, Map<int32, int32>* m) {
779 int32 key1 = 0;
780 int32 key2 = 1;
781 int32 value1 = 100;
782 int32 value2 = 101;
783
784 std::map<int32, int32> map;
785 map[key1] = value1;
786 map[key2] = value2;
787
788 m->insert(map.begin(), map.end());
789
790 Map<int32, int32> other(*m);
791
792 EXPECT_EQ(2, other.size());
793 EXPECT_EQ(value1, other.at(key1));
794 EXPECT_EQ(value2, other.at(key2));
795 }
796
TEST_P(MapImplTest,CopyConstructorWithArena)797 TEST_P(MapImplTest, CopyConstructorWithArena) {
798 Arena a;
799 CopyConstructorHelper(&a, &map_);
800 }
801
TEST_P(MapImplTest,CopyConstructorWithoutArena)802 TEST_P(MapImplTest, CopyConstructorWithoutArena) {
803 CopyConstructorHelper(NULL, &map_);
804 }
805
TEST_P(MapImplTest,IterConstructor)806 TEST_P(MapImplTest, IterConstructor) {
807 int32 key1 = 0;
808 int32 key2 = 1;
809 int32 value1 = 100;
810 int32 value2 = 101;
811
812 std::map<int32, int32> map;
813 map[key1] = value1;
814 map[key2] = value2;
815
816 Map<int32, int32> new_map(map.begin(), map.end(),
817 GetParam());
818
819 EXPECT_EQ(2, new_map.size());
820 EXPECT_EQ(value1, new_map.at(key1));
821 EXPECT_EQ(value2, new_map.at(key2));
822 }
823
TEST_P(MapImplTest,Assigner)824 TEST_P(MapImplTest, Assigner) {
825 int32 key1 = 0;
826 int32 key2 = 1;
827 int32 value1 = 100;
828 int32 value2 = 101;
829
830 std::map<int32, int32> map;
831 map[key1] = value1;
832 map[key2] = value2;
833
834 map_.insert(map.begin(), map.end());
835
836 Map<int32, int32> other(GetParam());
837 int32 key_other = 123;
838 int32 value_other = 321;
839 other[key_other] = value_other;
840 EXPECT_EQ(1, other.size());
841
842 other = map_;
843
844 EXPECT_EQ(2, other.size());
845 EXPECT_EQ(value1, other.at(key1));
846 EXPECT_EQ(value2, other.at(key2));
847 EXPECT_TRUE(other.find(key_other) == other.end());
848
849 // Self assign
850 other = other;
851 EXPECT_EQ(2, other.size());
852 EXPECT_EQ(value1, other.at(key1));
853 EXPECT_EQ(value2, other.at(key2));
854
855 // Try assignment to a map with a different choice of "style."
856 Map<int32, int32> m(!GetParam());
857 m = other;
858 EXPECT_EQ(2, m.size());
859 EXPECT_EQ(value1, m.at(key1));
860 EXPECT_EQ(value2, m.at(key2));
861 }
862
TEST_P(MapImplTest,Rehash)863 TEST_P(MapImplTest, Rehash) {
864 const int test_size = 50;
865 std::map<int32, int32> reference_map;
866 for (int i = 0; i < test_size; i++) {
867 reference_map[i] = i;
868 }
869 for (int i = 0; i < test_size; i++) {
870 map_[i] = reference_map[i];
871 EXPECT_EQ(reference_map[i], map_[i]);
872 }
873 for (int i = 0; i < test_size; i++) {
874 map_.erase(i);
875 EXPECT_TRUE(map_.end() == map_.find(i));
876 }
877 EXPECT_TRUE(map_.empty());
878 }
879
TEST_P(MapImplTest,EqualRange)880 TEST_P(MapImplTest, EqualRange) {
881 int key = 100, key_missing = 101;
882 map_[key] = 100;
883
884 std::pair<google::protobuf::Map<int32, int32>::iterator,
885 google::protobuf::Map<int32, int32>::iterator> range = map_.equal_range(key);
886 EXPECT_TRUE(map_.find(key) == range.first);
887 EXPECT_TRUE(++map_.find(key) == range.second);
888
889 range = map_.equal_range(key_missing);
890 EXPECT_TRUE(map_.end() == range.first);
891 EXPECT_TRUE(map_.end() == range.second);
892
893 std::pair<google::protobuf::Map<int32, int32>::const_iterator,
894 google::protobuf::Map<int32, int32>::const_iterator> const_range =
895 const_map_.equal_range(key);
896 EXPECT_TRUE(const_map_.find(key) == const_range.first);
897 EXPECT_TRUE(++const_map_.find(key) == const_range.second);
898
899 const_range = const_map_.equal_range(key_missing);
900 EXPECT_TRUE(const_map_.end() == const_range.first);
901 EXPECT_TRUE(const_map_.end() == const_range.second);
902 }
903
TEST_P(MapImplTest,ConvertToStdMap)904 TEST_P(MapImplTest, ConvertToStdMap) {
905 map_[100] = 101;
906 std::map<int32, int32> std_map(map_.begin(), map_.end());
907 EXPECT_EQ(1, std_map.size());
908 EXPECT_EQ(101, std_map[100]);
909 }
910
TEST_P(MapImplTest,ConvertToStdVectorOfPairs)911 TEST_P(MapImplTest, ConvertToStdVectorOfPairs) {
912 map_[100] = 101;
913 std::vector<std::pair<int32, int32> > std_vec(map_.begin(), map_.end());
914 EXPECT_EQ(1, std_vec.size());
915 EXPECT_EQ(100, std_vec[0].first);
916 EXPECT_EQ(101, std_vec[0].second);
917 }
918
TEST_P(MapImplTest,SwapSameStyle)919 TEST_P(MapImplTest, SwapSameStyle) {
920 Map<int32, int32> another(GetParam()); // same old_style_ value
921 map_[9398] = 41999;
922 another[9398] = 41999;
923 another[8070] = 42056;
924 another.swap(map_);
925 EXPECT_THAT(another, testing::UnorderedElementsAre(
926 testing::Pair(9398, 41999)));
927 EXPECT_THAT(map_, testing::UnorderedElementsAre(
928 testing::Pair(8070, 42056),
929 testing::Pair(9398, 41999)));
930 }
931
TEST_P(MapImplTest,SwapDifferentStyle)932 TEST_P(MapImplTest, SwapDifferentStyle) {
933 Map<int32, int32> another(!GetParam()); // different old_style_ value
934 map_[9398] = 41999;
935 another[9398] = 41999;
936 another[8070] = 42056;
937 another.swap(map_);
938 EXPECT_THAT(another, testing::UnorderedElementsAre(
939 testing::Pair(9398, 41999)));
940 EXPECT_THAT(map_, testing::UnorderedElementsAre(
941 testing::Pair(8070, 42056),
942 testing::Pair(9398, 41999)));
943 }
944
TEST_P(MapImplTest,SwapArena)945 TEST_P(MapImplTest, SwapArena) {
946 Arena arena1, arena2;
947 Map<int32, int32> m1(&arena1, false);
948 Map<int32, int32> m2(&arena2, false);
949 map_[9398] = 41999;
950 m1[9398] = 41999;
951 m1[8070] = 42056;
952 m2[10244] = 10247;
953 m2[8070] = 42056;
954 m1.swap(map_);
955 EXPECT_THAT(m1, testing::UnorderedElementsAre(
956 testing::Pair(9398, 41999)));
957 EXPECT_THAT(map_, testing::UnorderedElementsAre(
958 testing::Pair(8070, 42056),
959 testing::Pair(9398, 41999)));
960 m2.swap(m1);
961 EXPECT_THAT(m1, testing::UnorderedElementsAre(
962 testing::Pair(8070, 42056),
963 testing::Pair(10244, 10247)));
964 EXPECT_THAT(m2, testing::UnorderedElementsAre(
965 testing::Pair(9398, 41999)));
966 }
967
968 INSTANTIATE_TEST_CASE_P(BoolSequence, MapImplTest, testing::Bool());
969
970 // Map Field Reflection Test ========================================
971
Func(int i,int j)972 static int Func(int i, int j) {
973 return i * j;
974 }
975
StrFunc(int i,int j)976 static string StrFunc(int i, int j) {
977 string str;
978 SStringPrintf(&str, "%d", Func(i, j));
979 return str;
980 }
981
Int(const string & value)982 static int Int(const string& value) {
983 int result = 0;
984 std::istringstream(value) >> result;
985 return result;
986 }
987
988 class MapFieldReflectionTest : public testing::Test {
989 protected:
990 typedef FieldDescriptor FD;
991 };
992
TEST_F(MapFieldReflectionTest,RegularFields)993 TEST_F(MapFieldReflectionTest, RegularFields) {
994 TestMap message;
995 const Reflection* refl = message.GetReflection();
996 const Descriptor* desc = message.GetDescriptor();
997
998 Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32();
999 Map<int32, double>* map_int32_double = message.mutable_map_int32_double();
1000 Map<string, string>* map_string_string = message.mutable_map_string_string();
1001 Map<int32, ForeignMessage>* map_int32_foreign_message =
1002 message.mutable_map_int32_foreign_message();
1003
1004 for (int i = 0; i < 10; ++i) {
1005 (*map_int32_int32)[i] = Func(i, 1);
1006 (*map_int32_double)[i] = Func(i, 2);
1007 (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
1008 (*map_int32_foreign_message)[i].set_c(Func(i, 6));
1009 }
1010
1011 // Get FieldDescriptors for all the fields of interest.
1012 const FieldDescriptor* fd_map_int32_int32 =
1013 desc->FindFieldByName("map_int32_int32");
1014 const FieldDescriptor* fd_map_int32_double =
1015 desc->FindFieldByName("map_int32_double");
1016 const FieldDescriptor* fd_map_string_string =
1017 desc->FindFieldByName("map_string_string");
1018 const FieldDescriptor* fd_map_int32_foreign_message =
1019 desc->FindFieldByName("map_int32_foreign_message");
1020
1021 const FieldDescriptor* fd_map_int32_in32_key =
1022 fd_map_int32_int32->message_type()->FindFieldByName("key");
1023 const FieldDescriptor* fd_map_int32_in32_value =
1024 fd_map_int32_int32->message_type()->FindFieldByName("value");
1025 const FieldDescriptor* fd_map_int32_double_key =
1026 fd_map_int32_double->message_type()->FindFieldByName("key");
1027 const FieldDescriptor* fd_map_int32_double_value =
1028 fd_map_int32_double->message_type()->FindFieldByName("value");
1029 const FieldDescriptor* fd_map_string_string_key =
1030 fd_map_string_string->message_type()->FindFieldByName("key");
1031 const FieldDescriptor* fd_map_string_string_value =
1032 fd_map_string_string->message_type()->FindFieldByName("value");
1033 const FieldDescriptor* fd_map_int32_foreign_message_key =
1034 fd_map_int32_foreign_message->message_type()->FindFieldByName("key");
1035 const FieldDescriptor* fd_map_int32_foreign_message_value =
1036 fd_map_int32_foreign_message->message_type()->FindFieldByName("value");
1037
1038 // Get RepeatedPtrField objects for all fields of interest.
1039 const RepeatedPtrField<Message>& mf_int32_int32 =
1040 refl->GetRepeatedPtrField<Message>(message, fd_map_int32_int32);
1041 const RepeatedPtrField<Message>& mf_int32_double =
1042 refl->GetRepeatedPtrField<Message>(message, fd_map_int32_double);
1043 const RepeatedPtrField<Message>& mf_string_string =
1044 refl->GetRepeatedPtrField<Message>(message, fd_map_string_string);
1045 const RepeatedPtrField<Message>&
1046 mf_int32_foreign_message =
1047 refl->GetRepeatedPtrField<Message>(
1048 message, fd_map_int32_foreign_message);
1049
1050 // Get mutable RepeatedPtrField objects for all fields of interest.
1051 RepeatedPtrField<Message>* mmf_int32_int32 =
1052 refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_int32);
1053 RepeatedPtrField<Message>* mmf_int32_double =
1054 refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_double);
1055 RepeatedPtrField<Message>* mmf_string_string =
1056 refl->MutableRepeatedPtrField<Message>(&message, fd_map_string_string);
1057 RepeatedPtrField<Message>* mmf_int32_foreign_message =
1058 refl->MutableRepeatedPtrField<Message>(
1059 &message, fd_map_int32_foreign_message);
1060
1061 // Make sure we can do gets through the RepeatedPtrField objects.
1062 for (int i = 0; i < 10; ++i) {
1063 {
1064 // Check gets through const objects.
1065 const Message& message_int32_int32 = mf_int32_int32.Get(i);
1066 int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1067 message_int32_int32, fd_map_int32_in32_key);
1068 int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1069 message_int32_int32, fd_map_int32_in32_value);
1070 EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1071
1072 const Message& message_int32_double = mf_int32_double.Get(i);
1073 int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
1074 message_int32_double, fd_map_int32_double_key);
1075 double value_int32_double =
1076 message_int32_double.GetReflection()->GetDouble(
1077 message_int32_double, fd_map_int32_double_value);
1078 EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1079
1080 const Message& message_string_string = mf_string_string.Get(i);
1081 string key_string_string =
1082 message_string_string.GetReflection()->GetString(
1083 message_string_string, fd_map_string_string_key);
1084 string value_string_string =
1085 message_string_string.GetReflection()->GetString(
1086 message_string_string, fd_map_string_string_value);
1087 EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1088
1089 const Message& message_int32_message = mf_int32_foreign_message.Get(i);
1090 int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
1091 message_int32_message, fd_map_int32_foreign_message_key);
1092 const ForeignMessage& value_int32_message =
1093 down_cast<const ForeignMessage&>(
1094 message_int32_message.GetReflection()
1095 ->GetMessage(message_int32_message,
1096 fd_map_int32_foreign_message_value));
1097 EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1098 }
1099
1100 {
1101 // Check gets through mutable objects.
1102 const Message& message_int32_int32 = mmf_int32_int32->Get(i);
1103 int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1104 message_int32_int32, fd_map_int32_in32_key);
1105 int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1106 message_int32_int32, fd_map_int32_in32_value);
1107 EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1108
1109 const Message& message_int32_double = mmf_int32_double->Get(i);
1110 int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
1111 message_int32_double, fd_map_int32_double_key);
1112 double value_int32_double =
1113 message_int32_double.GetReflection()->GetDouble(
1114 message_int32_double, fd_map_int32_double_value);
1115 EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1116
1117 const Message& message_string_string = mmf_string_string->Get(i);
1118 string key_string_string =
1119 message_string_string.GetReflection()->GetString(
1120 message_string_string, fd_map_string_string_key);
1121 string value_string_string =
1122 message_string_string.GetReflection()->GetString(
1123 message_string_string, fd_map_string_string_value);
1124 EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1125
1126 const Message& message_int32_message = mmf_int32_foreign_message->Get(i);
1127 int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
1128 message_int32_message, fd_map_int32_foreign_message_key);
1129 const ForeignMessage& value_int32_message =
1130 down_cast<const ForeignMessage&>(
1131 message_int32_message.GetReflection()
1132 ->GetMessage(message_int32_message,
1133 fd_map_int32_foreign_message_value));
1134 EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1135 }
1136 }
1137
1138 // Do sets through the RepeatedPtrField objects.
1139 for (int i = 0; i < 10; i++) {
1140 {
1141 Message* message_int32_int32 = mmf_int32_int32->Mutable(i);
1142 int32 key_int32_int32 = message_int32_int32->GetReflection()->GetInt32(
1143 *message_int32_int32, fd_map_int32_in32_key);
1144 message_int32_int32->GetReflection()->SetInt32(message_int32_int32,
1145 fd_map_int32_in32_value,
1146 Func(key_int32_int32, -1));
1147
1148 Message* message_int32_double = mmf_int32_double->Mutable(i);
1149 int32 key_int32_double = message_int32_double->GetReflection()->GetInt32(
1150 *message_int32_double, fd_map_int32_double_key);
1151 message_int32_double->GetReflection()->SetDouble(
1152 message_int32_double, fd_map_int32_double_value,
1153 Func(key_int32_double, -2));
1154
1155 Message* message_string_string = mmf_string_string->Mutable(i);
1156 string key_string_string =
1157 message_string_string->GetReflection()->GetString(
1158 *message_string_string, fd_map_string_string_key);
1159 message_string_string->GetReflection()->SetString(
1160 message_string_string, fd_map_string_string_value,
1161 StrFunc(Int(key_string_string), -5));
1162
1163 Message* message_int32_message = mmf_int32_foreign_message->Mutable(i);
1164 int32 key_int32_message =
1165 message_int32_message->GetReflection()->GetInt32(
1166 *message_int32_message, fd_map_int32_foreign_message_key);
1167 ForeignMessage* value_int32_message = down_cast<ForeignMessage*>(
1168 message_int32_message->GetReflection()
1169 ->MutableMessage(message_int32_message,
1170 fd_map_int32_foreign_message_value));
1171 value_int32_message->set_c(Func(key_int32_message, -6));
1172 }
1173 }
1174
1175 // Check gets through mutable objects.
1176 for (int i = 0; i < 10; i++) {
1177 EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
1178 EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
1179 EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
1180 EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
1181 }
1182 }
1183
TEST_F(MapFieldReflectionTest,RepeatedFieldRefForRegularFields)1184 TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) {
1185 TestMap message;
1186 const Reflection* refl = message.GetReflection();
1187 const Descriptor* desc = message.GetDescriptor();
1188
1189 Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32();
1190 Map<int32, double>* map_int32_double = message.mutable_map_int32_double();
1191 Map<string, string>* map_string_string = message.mutable_map_string_string();
1192 Map<int32, ForeignMessage>* map_int32_foreign_message =
1193 message.mutable_map_int32_foreign_message();
1194
1195 for (int i = 0; i < 10; ++i) {
1196 (*map_int32_int32)[i] = Func(i, 1);
1197 (*map_int32_double)[i] = Func(i, 2);
1198 (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
1199 (*map_int32_foreign_message)[i].set_c(Func(i, 6));
1200 }
1201
1202 // Get FieldDescriptors for all the fields of interest.
1203 const FieldDescriptor* fd_map_int32_int32 =
1204 desc->FindFieldByName("map_int32_int32");
1205 const FieldDescriptor* fd_map_int32_double =
1206 desc->FindFieldByName("map_int32_double");
1207 const FieldDescriptor* fd_map_string_string =
1208 desc->FindFieldByName("map_string_string");
1209 const FieldDescriptor* fd_map_int32_foreign_message =
1210 desc->FindFieldByName("map_int32_foreign_message");
1211
1212 const FieldDescriptor* fd_map_int32_in32_key =
1213 fd_map_int32_int32->message_type()->FindFieldByName("key");
1214 const FieldDescriptor* fd_map_int32_in32_value =
1215 fd_map_int32_int32->message_type()->FindFieldByName("value");
1216 const FieldDescriptor* fd_map_int32_double_key =
1217 fd_map_int32_double->message_type()->FindFieldByName("key");
1218 const FieldDescriptor* fd_map_int32_double_value =
1219 fd_map_int32_double->message_type()->FindFieldByName("value");
1220 const FieldDescriptor* fd_map_string_string_key =
1221 fd_map_string_string->message_type()->FindFieldByName("key");
1222 const FieldDescriptor* fd_map_string_string_value =
1223 fd_map_string_string->message_type()->FindFieldByName("value");
1224 const FieldDescriptor* fd_map_int32_foreign_message_key =
1225 fd_map_int32_foreign_message->message_type()->FindFieldByName("key");
1226 const FieldDescriptor* fd_map_int32_foreign_message_value =
1227 fd_map_int32_foreign_message->message_type()->FindFieldByName("value");
1228
1229 // Get RepeatedFieldRef objects for all fields of interest.
1230 const RepeatedFieldRef<Message> mf_int32_int32 =
1231 refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_int32);
1232 const RepeatedFieldRef<Message> mf_int32_double =
1233 refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_double);
1234 const RepeatedFieldRef<Message> mf_string_string =
1235 refl->GetRepeatedFieldRef<Message>(message, fd_map_string_string);
1236 const RepeatedFieldRef<Message> mf_int32_foreign_message =
1237 refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_foreign_message);
1238
1239 // Get mutable RepeatedFieldRef objects for all fields of interest.
1240 const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
1241 refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_int32);
1242 const MutableRepeatedFieldRef<Message> mmf_int32_double =
1243 refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_double);
1244 const MutableRepeatedFieldRef<Message> mmf_string_string =
1245 refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_string_string);
1246 const MutableRepeatedFieldRef<Message>
1247 mmf_int32_foreign_message =
1248 refl->GetMutableRepeatedFieldRef<Message>(
1249 &message, fd_map_int32_foreign_message);
1250
1251 // Get entry default instances
1252 google::protobuf::scoped_ptr<Message> entry_int32_int32(
1253 MessageFactory::generated_factory()
1254 ->GetPrototype(fd_map_int32_int32->message_type())
1255 ->New());
1256 google::protobuf::scoped_ptr<Message> entry_int32_double(
1257 MessageFactory::generated_factory()
1258 ->GetPrototype(fd_map_int32_double->message_type())
1259 ->New());
1260 google::protobuf::scoped_ptr<Message> entry_string_string(
1261 MessageFactory::generated_factory()
1262 ->GetPrototype(fd_map_string_string->message_type())
1263 ->New());
1264 google::protobuf::scoped_ptr<Message> entry_int32_foreign_message(
1265 MessageFactory::generated_factory()
1266 ->GetPrototype(fd_map_int32_foreign_message->message_type())
1267 ->New());
1268
1269 EXPECT_EQ(10, mf_int32_int32.size());
1270 EXPECT_EQ(10, mmf_int32_int32.size());
1271 EXPECT_EQ(10, mf_int32_double.size());
1272 EXPECT_EQ(10, mmf_int32_double.size());
1273 EXPECT_EQ(10, mf_string_string.size());
1274 EXPECT_EQ(10, mmf_string_string.size());
1275 EXPECT_EQ(10, mf_int32_foreign_message.size());
1276 EXPECT_EQ(10, mmf_int32_foreign_message.size());
1277
1278 EXPECT_FALSE(mf_int32_int32.empty());
1279 EXPECT_FALSE(mmf_int32_int32.empty());
1280 EXPECT_FALSE(mf_int32_double.empty());
1281 EXPECT_FALSE(mmf_int32_double.empty());
1282 EXPECT_FALSE(mf_string_string.empty());
1283 EXPECT_FALSE(mmf_string_string.empty());
1284 EXPECT_FALSE(mf_int32_foreign_message.empty());
1285 EXPECT_FALSE(mmf_int32_foreign_message.empty());
1286
1287 // Make sure we can do gets through the RepeatedFieldRef objects.
1288 for (int i = 0; i < 10; ++i) {
1289 {
1290 // Check gets through const objects.
1291 const Message& message_int32_int32 =
1292 mf_int32_int32.Get(i, entry_int32_int32.get());
1293 int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1294 message_int32_int32, fd_map_int32_in32_key);
1295 int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1296 message_int32_int32, fd_map_int32_in32_value);
1297 EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1298
1299 const Message& message_int32_double =
1300 mf_int32_double.Get(i, entry_int32_double.get());
1301 int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
1302 message_int32_double, fd_map_int32_double_key);
1303 double value_int32_double =
1304 message_int32_double.GetReflection()->GetDouble(
1305 message_int32_double, fd_map_int32_double_value);
1306 EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1307
1308 const Message& message_string_string =
1309 mf_string_string.Get(i, entry_string_string.get());
1310 string key_string_string =
1311 message_string_string.GetReflection()->GetString(
1312 message_string_string, fd_map_string_string_key);
1313 string value_string_string =
1314 message_string_string.GetReflection()->GetString(
1315 message_string_string, fd_map_string_string_value);
1316 EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1317
1318 const Message& message_int32_message =
1319 mf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
1320 int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
1321 message_int32_message, fd_map_int32_foreign_message_key);
1322 const ForeignMessage& value_int32_message =
1323 down_cast<const ForeignMessage&>(
1324 message_int32_message.GetReflection()
1325 ->GetMessage(message_int32_message,
1326 fd_map_int32_foreign_message_value));
1327 EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1328 }
1329
1330 {
1331 // Check gets through mutable objects.
1332 const Message& message_int32_int32 =
1333 mmf_int32_int32.Get(i, entry_int32_int32.get());
1334 int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1335 message_int32_int32, fd_map_int32_in32_key);
1336 int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1337 message_int32_int32, fd_map_int32_in32_value);
1338 EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1339
1340 const Message& message_int32_double =
1341 mmf_int32_double.Get(i, entry_int32_double.get());
1342 int32 key_int32_double = message_int32_double.GetReflection()->GetInt32(
1343 message_int32_double, fd_map_int32_double_key);
1344 double value_int32_double =
1345 message_int32_double.GetReflection()->GetDouble(
1346 message_int32_double, fd_map_int32_double_value);
1347 EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1348
1349 const Message& message_string_string =
1350 mmf_string_string.Get(i, entry_string_string.get());
1351 string key_string_string =
1352 message_string_string.GetReflection()->GetString(
1353 message_string_string, fd_map_string_string_key);
1354 string value_string_string =
1355 message_string_string.GetReflection()->GetString(
1356 message_string_string, fd_map_string_string_value);
1357 EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1358
1359 const Message& message_int32_message =
1360 mmf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
1361 int32 key_int32_message = message_int32_message.GetReflection()->GetInt32(
1362 message_int32_message, fd_map_int32_foreign_message_key);
1363 const ForeignMessage& value_int32_message =
1364 down_cast<const ForeignMessage&>(
1365 message_int32_message.GetReflection()
1366 ->GetMessage(message_int32_message,
1367 fd_map_int32_foreign_message_value));
1368 EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1369 }
1370 }
1371
1372 // Make sure we can do sets through the RepeatedFieldRef objects.
1373 for (int i = 0; i < 10; i++) {
1374 const Message& message_int32_int32 =
1375 mmf_int32_int32.Get(i, entry_int32_int32.get());
1376 int key = message_int32_int32.GetReflection()->GetInt32(
1377 message_int32_int32, fd_map_int32_in32_key);
1378
1379 entry_int32_int32->GetReflection()->SetInt32(
1380 entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
1381 key);
1382 entry_int32_int32->GetReflection()->SetInt32(
1383 entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
1384 Func(key, -1));
1385 entry_int32_double->GetReflection()->SetInt32(
1386 entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
1387 key);
1388 entry_int32_double->GetReflection()->SetDouble(
1389 entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
1390 Func(key, -2));
1391 entry_string_string->GetReflection()->SetString(
1392 entry_string_string.get(),
1393 fd_map_string_string->message_type()->field(0), StrFunc(key, 1));
1394 entry_string_string->GetReflection()->SetString(
1395 entry_string_string.get(),
1396 fd_map_string_string->message_type()->field(1), StrFunc(key, -5));
1397 entry_int32_foreign_message->GetReflection()->SetInt32(
1398 entry_int32_foreign_message.get(),
1399 fd_map_int32_foreign_message->message_type()->field(0), key);
1400 Message* value_message =
1401 entry_int32_foreign_message->GetReflection()->MutableMessage(
1402 entry_int32_foreign_message.get(),
1403 fd_map_int32_foreign_message->message_type()->field(1));
1404 value_message->GetReflection()->SetInt32(
1405 value_message, value_message->GetDescriptor()->FindFieldByName("c"),
1406 Func(key, -6));
1407
1408 mmf_int32_int32.Set(i, *entry_int32_int32);
1409 mmf_int32_double.Set(i, *entry_int32_double);
1410 mmf_string_string.Set(i, *entry_string_string);
1411 mmf_int32_foreign_message.Set(i, *entry_int32_foreign_message);
1412 }
1413
1414 for (int i = 0; i < 10; i++) {
1415 EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
1416 EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
1417 EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
1418 EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
1419 }
1420
1421 // Test iterators.
1422 {
1423 int index = 0;
1424 hash_map<int32, int32> result;
1425 for (RepeatedFieldRef<Message>::iterator it = mf_int32_int32.begin();
1426 it != mf_int32_int32.end(); ++it) {
1427 const Message& message = *it;
1428 int32 key =
1429 message.GetReflection()->GetInt32(message, fd_map_int32_in32_key);
1430 int32 value =
1431 message.GetReflection()->GetInt32(message, fd_map_int32_in32_value);
1432 result[key] = value;
1433 ++index;
1434 }
1435 EXPECT_EQ(10, index);
1436 for (hash_map<int32, int32>::const_iterator it = result.begin();
1437 it != result.end(); ++it) {
1438 EXPECT_EQ(message.map_int32_int32().at(it->first), it->second);
1439 }
1440 }
1441
1442 {
1443 int index = 0;
1444 hash_map<int32, double> result;
1445 for (RepeatedFieldRef<Message>::iterator it = mf_int32_double.begin();
1446 it != mf_int32_double.end(); ++it) {
1447 const Message& message = *it;
1448 int32 key =
1449 message.GetReflection()->GetInt32(message, fd_map_int32_double_key);
1450 double value = message.GetReflection()->GetDouble(
1451 message, fd_map_int32_double_value);
1452 result[key] = value;
1453 ++index;
1454 }
1455 EXPECT_EQ(10, index);
1456 for (hash_map<int32, double>::const_iterator it = result.begin();
1457 it != result.end(); ++it) {
1458 EXPECT_EQ(message.map_int32_double().at(it->first), it->second);
1459 }
1460 }
1461
1462 {
1463 int index = 0;
1464 hash_map<string, string> result;
1465 for (RepeatedFieldRef<Message>::iterator it = mf_string_string.begin();
1466 it != mf_string_string.end(); ++it) {
1467 const Message& message = *it;
1468 string key =
1469 message.GetReflection()->GetString(message, fd_map_string_string_key);
1470 string value = message.GetReflection()->GetString(
1471 message, fd_map_string_string_value);
1472 result[key] = value;
1473 ++index;
1474 }
1475 EXPECT_EQ(10, index);
1476 for (hash_map<string, string>::const_iterator it = result.begin();
1477 it != result.end(); ++it) {
1478 EXPECT_EQ(message.map_string_string().at(it->first), it->second);
1479 }
1480 }
1481
1482 {
1483 int index = 0;
1484 std::map<int32, ForeignMessage> result;
1485 for (RepeatedFieldRef<Message>::iterator it =
1486 mf_int32_foreign_message.begin();
1487 it != mf_int32_foreign_message.end(); ++it) {
1488 const Message& message = *it;
1489 int32 key = message.GetReflection()->GetInt32(
1490 message, fd_map_int32_foreign_message_key);
1491 const ForeignMessage& sub_message = down_cast<const ForeignMessage&>(
1492 message.GetReflection()
1493 ->GetMessage(message, fd_map_int32_foreign_message_value));
1494 result[key].MergeFrom(sub_message);
1495 ++index;
1496 }
1497 EXPECT_EQ(10, index);
1498 for (std::map<int32, ForeignMessage>::const_iterator it = result.begin();
1499 it != result.end(); ++it) {
1500 EXPECT_EQ(message.map_int32_foreign_message().at(it->first).c(),
1501 it->second.c());
1502 }
1503 }
1504
1505 // Test MutableRepeatedFieldRef::Add()
1506 entry_int32_int32->GetReflection()->SetInt32(
1507 entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
1508 4321);
1509 entry_int32_int32->GetReflection()->SetInt32(
1510 entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
1511 1234);
1512 mmf_int32_int32.Add(*entry_int32_int32);
1513 EXPECT_EQ(1234, message.map_int32_int32().at(4321));
1514
1515 entry_int32_double->GetReflection()->SetInt32(
1516 entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
1517 4321);
1518 entry_int32_double->GetReflection()->SetDouble(
1519 entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
1520 1234.0);
1521 mmf_int32_double.Add(*entry_int32_double);
1522 EXPECT_EQ(1234.0, message.map_int32_double().at(4321));
1523
1524 entry_string_string->GetReflection()->SetString(
1525 entry_string_string.get(),
1526 fd_map_string_string->message_type()->field(0), "4321");
1527 entry_string_string->GetReflection()->SetString(
1528 entry_string_string.get(), fd_map_string_string->message_type()->field(1),
1529 "1234");
1530 mmf_string_string.Add(*entry_string_string);
1531 EXPECT_EQ("1234", message.map_string_string().at("4321"));
1532
1533 entry_int32_foreign_message->GetReflection()->SetInt32(
1534 entry_int32_foreign_message.get(),
1535 fd_map_int32_foreign_message->message_type()->field(0), 4321);
1536 Message* value_message =
1537 entry_int32_foreign_message->GetReflection()->MutableMessage(
1538 entry_int32_foreign_message.get(),
1539 fd_map_int32_foreign_message->message_type()->field(1));
1540 ForeignMessage foreign_message;
1541 foreign_message.set_c(1234);
1542 value_message->CopyFrom(foreign_message);
1543
1544 mmf_int32_foreign_message.Add(*entry_int32_foreign_message);
1545 EXPECT_EQ(1234, message.map_int32_foreign_message().at(4321).c());
1546
1547 // Test Reflection::AddAllocatedMessage
1548 Message* free_entry_string_string = MessageFactory::generated_factory()
1549 ->GetPrototype(fd_map_string_string->message_type())
1550 ->New();
1551 entry_string_string->GetReflection()->SetString(
1552 free_entry_string_string,
1553 fd_map_string_string->message_type()->field(0), "4321");
1554 entry_string_string->GetReflection()->SetString(
1555 free_entry_string_string, fd_map_string_string->message_type()->field(1),
1556 "1234");
1557 refl->AddAllocatedMessage(&message, fd_map_string_string,
1558 free_entry_string_string);
1559
1560 // Test MutableRepeatedFieldRef::RemoveLast()
1561 mmf_int32_int32.RemoveLast();
1562 mmf_int32_double.RemoveLast();
1563 mmf_string_string.RemoveLast();
1564 mmf_int32_foreign_message.RemoveLast();
1565 EXPECT_EQ(10, message.map_int32_int32().size());
1566 EXPECT_EQ(10, message.map_int32_double().size());
1567 EXPECT_EQ(11, message.map_string_string().size());
1568 EXPECT_EQ(10, message.map_int32_foreign_message().size());
1569
1570 // Test MutableRepeatedFieldRef::SwapElements()
1571 {
1572 const Message& message0a = mmf_int32_int32.Get(0, entry_int32_int32.get());
1573 int32 int32_value0a =
1574 message0a.GetReflection()->GetInt32(message0a, fd_map_int32_in32_value);
1575 const Message& message9a = mmf_int32_int32.Get(9, entry_int32_int32.get());
1576 int32 int32_value9a =
1577 message9a.GetReflection()->GetInt32(message9a, fd_map_int32_in32_value);
1578
1579 mmf_int32_int32.SwapElements(0, 9);
1580
1581 const Message& message0b = mmf_int32_int32.Get(0, entry_int32_int32.get());
1582 int32 int32_value0b =
1583 message0b.GetReflection()->GetInt32(message0b, fd_map_int32_in32_value);
1584 const Message& message9b = mmf_int32_int32.Get(9, entry_int32_int32.get());
1585 int32 int32_value9b =
1586 message9b.GetReflection()->GetInt32(message9b, fd_map_int32_in32_value);
1587
1588 EXPECT_EQ(int32_value9a, int32_value0b);
1589 EXPECT_EQ(int32_value0a, int32_value9b);
1590 }
1591
1592 {
1593 const Message& message0a =
1594 mmf_int32_double.Get(0, entry_int32_double.get());
1595 double double_value0a = message0a.GetReflection()->GetDouble(
1596 message0a, fd_map_int32_double_value);
1597 const Message& message9a =
1598 mmf_int32_double.Get(9, entry_int32_double.get());
1599 double double_value9a = message9a.GetReflection()->GetDouble(
1600 message9a, fd_map_int32_double_value);
1601
1602 mmf_int32_double.SwapElements(0, 9);
1603
1604 const Message& message0b =
1605 mmf_int32_double.Get(0, entry_int32_double.get());
1606 double double_value0b = message0b.GetReflection()->GetDouble(
1607 message0b, fd_map_int32_double_value);
1608 const Message& message9b =
1609 mmf_int32_double.Get(9, entry_int32_double.get());
1610 double double_value9b = message9b.GetReflection()->GetDouble(
1611 message9b, fd_map_int32_double_value);
1612
1613 EXPECT_EQ(double_value9a, double_value0b);
1614 EXPECT_EQ(double_value0a, double_value9b);
1615 }
1616
1617 {
1618 const Message& message0a =
1619 mmf_string_string.Get(0, entry_string_string.get());
1620 string string_value0a = message0a.GetReflection()->GetString(
1621 message0a, fd_map_string_string_value);
1622 const Message& message9a =
1623 mmf_string_string.Get(9, entry_string_string.get());
1624 string string_value9a = message9a.GetReflection()->GetString(
1625 message9a, fd_map_string_string_value);
1626
1627 mmf_string_string.SwapElements(0, 9);
1628
1629 const Message& message0b =
1630 mmf_string_string.Get(0, entry_string_string.get());
1631 string string_value0b = message0b.GetReflection()->GetString(
1632 message0b, fd_map_string_string_value);
1633 const Message& message9b =
1634 mmf_string_string.Get(9, entry_string_string.get());
1635 string string_value9b = message9b.GetReflection()->GetString(
1636 message9b, fd_map_string_string_value);
1637
1638 EXPECT_EQ(string_value9a, string_value0b);
1639 EXPECT_EQ(string_value0a, string_value9b);
1640 }
1641
1642 {
1643 const Message& message0a =
1644 mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get());
1645 const ForeignMessage& sub_message0a = down_cast<const ForeignMessage&>(
1646 message0a.GetReflection()
1647 ->GetMessage(message0a, fd_map_int32_foreign_message_value));
1648 int32 int32_value0a = sub_message0a.c();
1649 const Message& message9a =
1650 mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get());
1651 const ForeignMessage& sub_message9a = down_cast<const ForeignMessage&>(
1652 message9a.GetReflection()
1653 ->GetMessage(message9a, fd_map_int32_foreign_message_value));
1654 int32 int32_value9a = sub_message9a.c();
1655
1656 mmf_int32_foreign_message.SwapElements(0, 9);
1657
1658 const Message& message0b =
1659 mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get());
1660 const ForeignMessage& sub_message0b = down_cast<const ForeignMessage&>(
1661 message0b.GetReflection()
1662 ->GetMessage(message0b, fd_map_int32_foreign_message_value));
1663 int32 int32_value0b = sub_message0b.c();
1664 const Message& message9b =
1665 mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get());
1666 const ForeignMessage& sub_message9b = down_cast<const ForeignMessage&>(
1667 message9b.GetReflection()
1668 ->GetMessage(message9b, fd_map_int32_foreign_message_value));
1669 int32 int32_value9b = sub_message9b.c();
1670
1671 EXPECT_EQ(int32_value9a, int32_value0b);
1672 EXPECT_EQ(int32_value0a, int32_value9b);
1673 }
1674 }
1675
TEST_F(MapFieldReflectionTest,RepeatedFieldRefMergeFromAndSwap)1676 TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) {
1677 // Set-up message content.
1678 TestMap m0, m1, m2;
1679 for (int i = 0; i < 10; ++i) {
1680 (*m0.mutable_map_int32_int32())[i] = Func(i, 1);
1681 (*m0.mutable_map_int32_double())[i] = Func(i, 2);
1682 (*m0.mutable_map_string_string())[StrFunc(i, 1)] = StrFunc(i, 5);
1683 (*m0.mutable_map_int32_foreign_message())[i].set_c(Func(i, 6));
1684 (*m1.mutable_map_int32_int32())[i + 10] = Func(i, 11);
1685 (*m1.mutable_map_int32_double())[i + 10] = Func(i, 12);
1686 (*m1.mutable_map_string_string())[StrFunc(i + 10, 1)] = StrFunc(i, 15);
1687 (*m1.mutable_map_int32_foreign_message())[i + 10].set_c(Func(i, 16));
1688 (*m2.mutable_map_int32_int32())[i + 20] = Func(i, 21);
1689 (*m2.mutable_map_int32_double())[i + 20] = Func(i, 22);
1690 (*m2.mutable_map_string_string())[StrFunc(i + 20, 1)] = StrFunc(i, 25);
1691 (*m2.mutable_map_int32_foreign_message())[i + 20].set_c(Func(i, 26));
1692 }
1693
1694 const Reflection* refl = m0.GetReflection();
1695 const Descriptor* desc = m0.GetDescriptor();
1696
1697 // Get FieldDescriptors for all the fields of interest.
1698 const FieldDescriptor* fd_map_int32_int32 =
1699 desc->FindFieldByName("map_int32_int32");
1700 const FieldDescriptor* fd_map_int32_double =
1701 desc->FindFieldByName("map_int32_double");
1702 const FieldDescriptor* fd_map_string_string =
1703 desc->FindFieldByName("map_string_string");
1704 const FieldDescriptor* fd_map_int32_foreign_message =
1705 desc->FindFieldByName("map_int32_foreign_message");
1706
1707 // Get MutableRepeatedFieldRef objects for all fields of interest.
1708 const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
1709 refl->GetMutableRepeatedFieldRef<Message>(
1710 &m0, fd_map_int32_int32);
1711 const MutableRepeatedFieldRef<Message> mmf_int32_double =
1712 refl->GetMutableRepeatedFieldRef<Message>(
1713 &m0, fd_map_int32_double);
1714 const MutableRepeatedFieldRef<Message> mmf_string_string =
1715 refl->GetMutableRepeatedFieldRef<Message>(
1716 &m0, fd_map_string_string);
1717 const MutableRepeatedFieldRef<Message>
1718 mmf_int32_foreign_message =
1719 refl->GetMutableRepeatedFieldRef<Message>(
1720 &m0, fd_map_int32_foreign_message);
1721
1722 // Test MutableRepeatedRef::CopyFrom
1723 mmf_int32_int32.CopyFrom(
1724 refl->GetRepeatedFieldRef<Message>(
1725 m1, fd_map_int32_int32));
1726 mmf_int32_double.CopyFrom(
1727 refl->GetRepeatedFieldRef<Message>(
1728 m1, fd_map_int32_double));
1729 mmf_string_string.CopyFrom(
1730 refl->GetRepeatedFieldRef<Message>(
1731 m1, fd_map_string_string));
1732 mmf_int32_foreign_message.CopyFrom(
1733 refl->GetRepeatedFieldRef<Message>(
1734 m1, fd_map_int32_foreign_message));
1735
1736 for (int i = 0; i < 10; ++i) {
1737 EXPECT_EQ(Func(i, 11), m0.map_int32_int32().at(i + 10));
1738 EXPECT_EQ(Func(i, 12), m0.map_int32_double().at(i + 10));
1739 EXPECT_EQ(StrFunc(i, 15), m0.map_string_string().at(StrFunc(i + 10, 1)));
1740 EXPECT_EQ(Func(i, 16), m0.map_int32_foreign_message().at(i + 10).c());
1741 }
1742
1743 // Test MutableRepeatedRef::MergeFrom
1744 mmf_int32_int32.MergeFrom(
1745 refl->GetRepeatedFieldRef<Message>(
1746 m2, fd_map_int32_int32));
1747 mmf_int32_double.MergeFrom(
1748 refl->GetRepeatedFieldRef<Message>(
1749 m2, fd_map_int32_double));
1750 mmf_string_string.MergeFrom(
1751 refl->GetRepeatedFieldRef<Message>(
1752 m2, fd_map_string_string));
1753 mmf_int32_foreign_message.MergeFrom(
1754 refl->GetRepeatedFieldRef<Message>(
1755 m2, fd_map_int32_foreign_message));
1756 for (int i = 0; i < 10; ++i) {
1757 EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20));
1758 EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20));
1759 EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
1760 EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
1761 }
1762
1763 // Test MutableRepeatedRef::Swap
1764 // Swap between m0 and m2.
1765 mmf_int32_int32.Swap(
1766 refl->GetMutableRepeatedFieldRef<Message>(
1767 &m2, fd_map_int32_int32));
1768 mmf_int32_double.Swap(
1769 refl->GetMutableRepeatedFieldRef<Message>(
1770 &m2, fd_map_int32_double));
1771 mmf_string_string.Swap(
1772 refl->GetMutableRepeatedFieldRef<Message>(
1773 &m2, fd_map_string_string));
1774 mmf_int32_foreign_message.Swap(
1775 refl->GetMutableRepeatedFieldRef<Message>(
1776 &m2, fd_map_int32_foreign_message));
1777 for (int i = 0; i < 10; ++i) {
1778 // Check the content of m0.
1779 EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20));
1780 EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20));
1781 EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
1782 EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
1783
1784 // Check the content of m2.
1785 EXPECT_EQ(Func(i, 11), m2.map_int32_int32().at(i + 10));
1786 EXPECT_EQ(Func(i, 12), m2.map_int32_double().at(i + 10));
1787 EXPECT_EQ(StrFunc(i, 15), m2.map_string_string().at(StrFunc(i + 10, 1)));
1788 EXPECT_EQ(Func(i, 16), m2.map_int32_foreign_message().at(i + 10).c());
1789 EXPECT_EQ(Func(i, 21), m2.map_int32_int32().at(i + 20));
1790 EXPECT_EQ(Func(i, 22), m2.map_int32_double().at(i + 20));
1791 EXPECT_EQ(StrFunc(i, 25), m2.map_string_string().at(StrFunc(i + 20, 1)));
1792 EXPECT_EQ(Func(i, 26), m2.map_int32_foreign_message().at(i + 20).c());
1793 }
1794
1795 // TODO(teboring): add test for duplicated key
1796 }
1797
1798 // Generated Message Test ===========================================
1799
TEST(GeneratedMapFieldTest,Accessors)1800 TEST(GeneratedMapFieldTest, Accessors) {
1801 unittest::TestMap message;
1802
1803 MapTestUtil::SetMapFields(&message);
1804 MapTestUtil::ExpectMapFieldsSet(message);
1805
1806 MapTestUtil::ModifyMapFields(&message);
1807 MapTestUtil::ExpectMapFieldsModified(message);
1808 }
1809
TEST(GeneratedMapFieldTest,SetMapFieldsInitialized)1810 TEST(GeneratedMapFieldTest, SetMapFieldsInitialized) {
1811 unittest::TestMap message;
1812
1813 MapTestUtil::SetMapFieldsInitialized(&message);
1814 MapTestUtil::ExpectMapFieldsSetInitialized(message);
1815 }
1816
TEST(GeneratedMapFieldTest,Proto2SetMapFieldsInitialized)1817 TEST(GeneratedMapFieldTest, Proto2SetMapFieldsInitialized) {
1818 unittest::TestEnumMap message;
1819 EXPECT_EQ(unittest::PROTO2_MAP_ENUM_FOO,
1820 (*message.mutable_known_map_field())[0]);
1821 }
1822
TEST(GeneratedMapFieldTest,Clear)1823 TEST(GeneratedMapFieldTest, Clear) {
1824 unittest::TestMap message;
1825
1826 MapTestUtil::SetMapFields(&message);
1827 message.Clear();
1828 MapTestUtil::ExpectClear(message);
1829 }
1830
TEST(GeneratedMapFieldTest,ClearMessageMap)1831 TEST(GeneratedMapFieldTest, ClearMessageMap) {
1832 unittest::TestMessageMap message;
1833
1834 // Creates a TestAllTypes with default value
1835 TestUtil::ExpectClear((*message.mutable_map_int32_message())[0]);
1836 }
1837
TEST(GeneratedMapFieldTest,CopyFrom)1838 TEST(GeneratedMapFieldTest, CopyFrom) {
1839 unittest::TestMap message1, message2;
1840
1841 MapTestUtil::SetMapFields(&message1);
1842 message2.CopyFrom(message1);
1843 MapTestUtil::ExpectMapFieldsSet(message2);
1844
1845 // Copying from self should be a no-op.
1846 message2.CopyFrom(message2);
1847 MapTestUtil::ExpectMapFieldsSet(message2);
1848 }
1849
TEST(GeneratedMapFieldTest,CopyFromMessageMap)1850 TEST(GeneratedMapFieldTest, CopyFromMessageMap) {
1851 unittest::TestMessageMap message1, message2;
1852
1853 (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
1854 (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
1855
1856 message1.CopyFrom(message2);
1857
1858 // Checks repeated field is overwritten.
1859 EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
1860 EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
1861 }
1862
TEST(GeneratedMapFieldTest,SwapWithEmpty)1863 TEST(GeneratedMapFieldTest, SwapWithEmpty) {
1864 unittest::TestMap message1, message2;
1865
1866 MapTestUtil::SetMapFields(&message1);
1867 MapTestUtil::ExpectMapFieldsSet(message1);
1868 MapTestUtil::ExpectClear(message2);
1869
1870 message1.Swap(&message2);
1871 MapTestUtil::ExpectMapFieldsSet(message2);
1872 MapTestUtil::ExpectClear(message1);
1873 }
1874
TEST(GeneratedMapFieldTest,SwapWithSelf)1875 TEST(GeneratedMapFieldTest, SwapWithSelf) {
1876 unittest::TestMap message;
1877
1878 MapTestUtil::SetMapFields(&message);
1879 MapTestUtil::ExpectMapFieldsSet(message);
1880
1881 message.Swap(&message);
1882 MapTestUtil::ExpectMapFieldsSet(message);
1883 }
1884
TEST(GeneratedMapFieldTest,SwapWithOther)1885 TEST(GeneratedMapFieldTest, SwapWithOther) {
1886 unittest::TestMap message1, message2;
1887
1888 MapTestUtil::SetMapFields(&message1);
1889 MapTestUtil::SetMapFields(&message2);
1890 MapTestUtil::ModifyMapFields(&message2);
1891
1892 message1.Swap(&message2);
1893 MapTestUtil::ExpectMapFieldsModified(message1);
1894 MapTestUtil::ExpectMapFieldsSet(message2);
1895 }
1896
TEST(GeneratedMapFieldTest,CopyConstructor)1897 TEST(GeneratedMapFieldTest, CopyConstructor) {
1898 unittest::TestMap message1;
1899 MapTestUtil::SetMapFields(&message1);
1900
1901 unittest::TestMap message2(message1);
1902 MapTestUtil::ExpectMapFieldsSet(message2);
1903 }
1904
TEST(GeneratedMapFieldTest,CopyAssignmentOperator)1905 TEST(GeneratedMapFieldTest, CopyAssignmentOperator) {
1906 unittest::TestMap message1;
1907 MapTestUtil::SetMapFields(&message1);
1908
1909 unittest::TestMap message2;
1910 message2 = message1;
1911 MapTestUtil::ExpectMapFieldsSet(message2);
1912
1913 // Make sure that self-assignment does something sane.
1914 message2.operator=(message2);
1915 MapTestUtil::ExpectMapFieldsSet(message2);
1916 }
1917
1918 #if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \
1919 !defined(GOOGLE_PROTOBUF_NO_RTTI)
TEST(GeneratedMapFieldTest,UpcastCopyFrom)1920 TEST(GeneratedMapFieldTest, UpcastCopyFrom) {
1921 // Test the CopyFrom method that takes in the generic const Message&
1922 // parameter.
1923 unittest::TestMap message1, message2;
1924
1925 MapTestUtil::SetMapFields(&message1);
1926
1927 const Message* source = implicit_cast<const Message*>(&message1);
1928 message2.CopyFrom(*source);
1929
1930 MapTestUtil::ExpectMapFieldsSet(message2);
1931 }
1932 #endif
1933
1934 #ifndef PROTOBUF_TEST_NO_DESCRIPTORS
1935
TEST(GeneratedMapFieldTest,CopyFromDynamicMessage)1936 TEST(GeneratedMapFieldTest, CopyFromDynamicMessage) {
1937 // Test copying from a DynamicMessage, which must fall back to using
1938 // reflection.
1939 unittest::TestMap message2;
1940
1941 // Construct a new version of the dynamic message via the factory.
1942 DynamicMessageFactory factory;
1943 google::protobuf::scoped_ptr<Message> message1;
1944 message1.reset(
1945 factory.GetPrototype(unittest::TestMap::descriptor())->New());
1946 MapReflectionTester reflection_tester(
1947 unittest::TestMap::descriptor());
1948 reflection_tester.SetMapFieldsViaReflection(message1.get());
1949 reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
1950 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
1951 message2.CopyFrom(*message1);
1952 MapTestUtil::ExpectMapFieldsSet(message2);
1953 }
1954
TEST(GeneratedMapFieldTest,CopyFromDynamicMessageMapReflection)1955 TEST(GeneratedMapFieldTest, CopyFromDynamicMessageMapReflection) {
1956 unittest::TestMap message2;
1957
1958 // Construct a new version of the dynamic message via the factory.
1959 DynamicMessageFactory factory;
1960 google::protobuf::scoped_ptr<Message> message1;
1961 message1.reset(
1962 factory.GetPrototype(unittest::TestMap::descriptor())->New());
1963 MapReflectionTester reflection_tester(
1964 unittest::TestMap::descriptor());
1965 reflection_tester.SetMapFieldsViaMapReflection(message1.get());
1966 reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
1967 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
1968 message2.CopyFrom(*message1);
1969 MapTestUtil::ExpectMapFieldsSet(message2);
1970 }
1971
TEST(GeneratedMapFieldTest,DynamicMessageCopyFrom)1972 TEST(GeneratedMapFieldTest, DynamicMessageCopyFrom) {
1973 // Test copying to a DynamicMessage, which must fall back to using reflection.
1974 unittest::TestMap message2;
1975 MapTestUtil::SetMapFields(&message2);
1976
1977 // Construct a new version of the dynamic message via the factory.
1978 DynamicMessageFactory factory;
1979 google::protobuf::scoped_ptr<Message> message1;
1980 message1.reset(
1981 factory.GetPrototype(unittest::TestMap::descriptor())->New());
1982
1983 MapReflectionTester reflection_tester(
1984 unittest::TestMap::descriptor());
1985 message1->MergeFrom(message2);
1986 reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
1987 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
1988 }
1989
TEST(GeneratedMapFieldTest,DynamicMessageCopyFromMapReflection)1990 TEST(GeneratedMapFieldTest, DynamicMessageCopyFromMapReflection) {
1991 MapReflectionTester reflection_tester(
1992 unittest::TestMap::descriptor());
1993 unittest::TestMap message2;
1994 reflection_tester.SetMapFieldsViaMapReflection(&message2);
1995
1996 // Construct a dynamic message via the factory.
1997 DynamicMessageFactory factory;
1998 google::protobuf::scoped_ptr<Message> message1;
1999 message1.reset(
2000 factory.GetPrototype(unittest::TestMap::descriptor())->New());
2001
2002 message1->MergeFrom(message2);
2003 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
2004 reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
2005 }
2006
TEST(GeneratedMapFieldTest,SyncDynamicMapWithRepeatedField)2007 TEST(GeneratedMapFieldTest, SyncDynamicMapWithRepeatedField) {
2008 // Construct a dynamic message via the factory.
2009 MapReflectionTester reflection_tester(
2010 unittest::TestMap::descriptor());
2011 DynamicMessageFactory factory;
2012 google::protobuf::scoped_ptr<Message> message;
2013 message.reset(
2014 factory.GetPrototype(unittest::TestMap::descriptor())->New());
2015 reflection_tester.SetMapFieldsViaReflection(message.get());
2016 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message.get());
2017 reflection_tester.ExpectMapFieldsSetViaReflection(*message);
2018 }
2019
2020 #endif // !PROTOBUF_TEST_NO_DESCRIPTORS
2021
TEST(GeneratedMapFieldTest,NonEmptyMergeFrom)2022 TEST(GeneratedMapFieldTest, NonEmptyMergeFrom) {
2023 unittest::TestMap message1, message2;
2024
2025 MapTestUtil::SetMapFields(&message1);
2026
2027 // This field will test merging into an empty spot.
2028 (*message2.mutable_map_int32_int32())[1] = 1;
2029 message1.mutable_map_int32_int32()->erase(1);
2030
2031 // This tests overwriting.
2032 (*message2.mutable_map_int32_double())[1] = 1;
2033 (*message1.mutable_map_int32_double())[1] = 2;
2034
2035 message1.MergeFrom(message2);
2036 MapTestUtil::ExpectMapFieldsSet(message1);
2037 }
2038
TEST(GeneratedMapFieldTest,MergeFromMessageMap)2039 TEST(GeneratedMapFieldTest, MergeFromMessageMap) {
2040 unittest::TestMessageMap message1, message2;
2041
2042 (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
2043 (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
2044
2045 message1.MergeFrom(message2);
2046
2047 // Checks repeated field is overwritten.
2048 EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
2049 EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
2050 }
2051
2052 // Test the generated SerializeWithCachedSizesToArray()
TEST(GeneratedMapFieldTest,SerializationToArray)2053 TEST(GeneratedMapFieldTest, SerializationToArray) {
2054 unittest::TestMap message1, message2;
2055 string data;
2056 MapTestUtil::SetMapFields(&message1);
2057 int size = message1.ByteSize();
2058 data.resize(size);
2059 uint8* start = reinterpret_cast<uint8*>(string_as_array(&data));
2060 uint8* end = message1.SerializeWithCachedSizesToArray(start);
2061 EXPECT_EQ(size, end - start);
2062 EXPECT_TRUE(message2.ParseFromString(data));
2063 MapTestUtil::ExpectMapFieldsSet(message2);
2064 }
2065
2066 // Test the generated SerializeWithCachedSizes()
TEST(GeneratedMapFieldTest,SerializationToStream)2067 TEST(GeneratedMapFieldTest, SerializationToStream) {
2068 unittest::TestMap message1, message2;
2069 MapTestUtil::SetMapFields(&message1);
2070 int size = message1.ByteSize();
2071 string data;
2072 data.resize(size);
2073 {
2074 // Allow the output stream to buffer only one byte at a time.
2075 io::ArrayOutputStream array_stream(string_as_array(&data), size, 1);
2076 io::CodedOutputStream output_stream(&array_stream);
2077 message1.SerializeWithCachedSizes(&output_stream);
2078 EXPECT_FALSE(output_stream.HadError());
2079 EXPECT_EQ(size, output_stream.ByteCount());
2080 }
2081 EXPECT_TRUE(message2.ParseFromString(data));
2082 MapTestUtil::ExpectMapFieldsSet(message2);
2083 }
2084
2085
TEST(GeneratedMapFieldTest,SameTypeMaps)2086 TEST(GeneratedMapFieldTest, SameTypeMaps) {
2087 const Descriptor* map1 = unittest::TestSameTypeMap::descriptor()
2088 ->FindFieldByName("map1")
2089 ->message_type();
2090 const Descriptor* map2 = unittest::TestSameTypeMap::descriptor()
2091 ->FindFieldByName("map2")
2092 ->message_type();
2093
2094 const Message* map1_entry =
2095 MessageFactory::generated_factory()->GetPrototype(map1);
2096 const Message* map2_entry =
2097 MessageFactory::generated_factory()->GetPrototype(map2);
2098
2099 EXPECT_EQ(map1, map1_entry->GetDescriptor());
2100 EXPECT_EQ(map2, map2_entry->GetDescriptor());
2101 }
2102
TEST(GeneratedMapFieldTest,Proto2UnknownEnum)2103 TEST(GeneratedMapFieldTest, Proto2UnknownEnum) {
2104 unittest::TestEnumMapPlusExtra from;
2105 (*from.mutable_known_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_FOO;
2106 (*from.mutable_unknown_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_EXTRA;
2107 string data;
2108 from.SerializeToString(&data);
2109
2110 unittest::TestEnumMap to;
2111 EXPECT_TRUE(to.ParseFromString(data));
2112 EXPECT_EQ(0, to.unknown_map_field().size());
2113 const UnknownFieldSet& unknown_field_set =
2114 to.GetReflection()->GetUnknownFields(to);
2115 EXPECT_EQ(1, unknown_field_set.field_count());
2116 EXPECT_EQ(1, to.known_map_field().size());
2117 EXPECT_EQ(unittest::PROTO2_MAP_ENUM_FOO, to.known_map_field().at(0));
2118
2119 data.clear();
2120 from.Clear();
2121 to.SerializeToString(&data);
2122 EXPECT_TRUE(from.ParseFromString(data));
2123 EXPECT_EQ(0, from.GetReflection()->GetUnknownFields(from).field_count());
2124 EXPECT_EQ(1, from.known_map_field().size());
2125 EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_FOO, from.known_map_field().at(0));
2126 EXPECT_EQ(1, from.unknown_map_field().size());
2127 EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_EXTRA, from.unknown_map_field().at(0));
2128 }
2129
TEST(GeneratedMapFieldTest,StandardWireFormat)2130 TEST(GeneratedMapFieldTest, StandardWireFormat) {
2131 unittest::TestMap message;
2132 string data = "\x0A\x04\x08\x01\x10\x01";
2133
2134 EXPECT_TRUE(message.ParseFromString(data));
2135 EXPECT_EQ(1, message.map_int32_int32().size());
2136 EXPECT_EQ(1, message.map_int32_int32().at(1));
2137 }
2138
TEST(GeneratedMapFieldTest,UnorderedWireFormat)2139 TEST(GeneratedMapFieldTest, UnorderedWireFormat) {
2140 unittest::TestMap message;
2141
2142 // put value before key in wire format
2143 string data = "\x0A\x04\x10\x01\x08\x02";
2144
2145 EXPECT_TRUE(message.ParseFromString(data));
2146 EXPECT_EQ(1, message.map_int32_int32().size());
2147 EXPECT_EQ(1, message.map_int32_int32().at(2));
2148 }
2149
TEST(GeneratedMapFieldTest,DuplicatedKeyWireFormat)2150 TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) {
2151 unittest::TestMap message;
2152
2153 // Two key fields in wire format
2154 string data = "\x0A\x06\x08\x01\x08\x02\x10\x01";
2155
2156 EXPECT_TRUE(message.ParseFromString(data));
2157 EXPECT_EQ(1, message.map_int32_int32().size());
2158 EXPECT_EQ(1, message.map_int32_int32().at(2));
2159
2160 // A similar test, but with a map from int to a message type.
2161 // Again, we want to be sure that the "second one wins" when
2162 // there are two separate entries with the same key.
2163 const int key = 99;
2164 unittest::TestRequiredMessageMap map_message;
2165 unittest::TestRequired with_dummy4;
2166 with_dummy4.set_a(0);
2167 with_dummy4.set_b(0);
2168 with_dummy4.set_c(0);
2169 with_dummy4.set_dummy4(11);
2170 (*map_message.mutable_map_field())[key] = with_dummy4;
2171 string s = map_message.SerializeAsString();
2172 unittest::TestRequired with_dummy5;
2173 with_dummy5.set_a(0);
2174 with_dummy5.set_b(0);
2175 with_dummy5.set_c(0);
2176 with_dummy5.set_dummy5(12);
2177 (*map_message.mutable_map_field())[key] = with_dummy5;
2178 string both = s + map_message.SerializeAsString();
2179 // We don't expect a merge now. The "second one wins."
2180 ASSERT_TRUE(map_message.ParseFromString(both));
2181 ASSERT_EQ(1, map_message.map_field().size());
2182 ASSERT_EQ(1, map_message.map_field().count(key));
2183 EXPECT_EQ(0, map_message.map_field().find(key)->second.a());
2184 EXPECT_EQ(0, map_message.map_field().find(key)->second.b());
2185 EXPECT_EQ(0, map_message.map_field().find(key)->second.c());
2186 EXPECT_FALSE(map_message.map_field().find(key)->second.has_dummy4());
2187 ASSERT_TRUE(map_message.map_field().find(key)->second.has_dummy5());
2188 EXPECT_EQ(12, map_message.map_field().find(key)->second.dummy5());
2189 }
2190
2191 // Exhaustive combinations of keys, values, and junk in any order.
2192 // This re-tests some of the things tested above, but if it fails
2193 // it's more work to determine what went wrong, so it isn't necessarily
2194 // bad that we have the simpler tests too.
TEST(GeneratedMapFieldTest,KeysValuesUnknownsWireFormat)2195 TEST(GeneratedMapFieldTest, KeysValuesUnknownsWireFormat) {
2196 unittest::TestMap message;
2197 const int kMaxNumKeysAndValuesAndJunk = 4;
2198 const char kKeyTag = 0x08;
2199 const char kValueTag = 0x10;
2200 const char kJunkTag = 0x20;
2201 for (int items = 0; items <= kMaxNumKeysAndValuesAndJunk; items++) {
2202 string data = "\x0A";
2203 // Encode length of what will follow.
2204 data.push_back(items * 2);
2205 static const int kBitsOfIPerItem = 4;
2206 static const int mask = (1 << kBitsOfIPerItem) - 1;
2207 // Each iteration of the following is a test. It uses i as bit vector
2208 // encoding the keys and values to put in the wire format.
2209 for (int i = 0; i < (1 << (items * kBitsOfIPerItem)); i++) {
2210 string wire_format = data;
2211 int expected_key = 0;
2212 int expected_value = 0;
2213 for (int k = i, j = 0; j < items; j++, k >>= kBitsOfIPerItem) {
2214 bool is_key = k & 0x1;
2215 bool is_value = !is_key && (k & 0x2);
2216 wire_format.push_back(is_key ? kKeyTag :
2217 is_value ? kValueTag : kJunkTag);
2218 char c = static_cast<char>(k & mask) >> 2; // One char after the tag.
2219 wire_format.push_back(c);
2220 if (is_key) expected_key = static_cast<int>(c);
2221 if (is_value) expected_value = static_cast<int>(c);
2222 ASSERT_TRUE(message.ParseFromString(wire_format));
2223 ASSERT_EQ(1, message.map_int32_int32().size());
2224 ASSERT_EQ(expected_key, message.map_int32_int32().begin()->first);
2225 ASSERT_EQ(expected_value, message.map_int32_int32().begin()->second);
2226 }
2227 }
2228 }
2229 }
2230
TEST(GeneratedMapFieldTest,DuplicatedValueWireFormat)2231 TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) {
2232 unittest::TestMap message;
2233
2234 // Two value fields in wire format
2235 string data = "\x0A\x06\x08\x01\x10\x01\x10\x02";
2236
2237 EXPECT_TRUE(message.ParseFromString(data));
2238 EXPECT_EQ(1, message.map_int32_int32().size());
2239 EXPECT_EQ(2, message.map_int32_int32().at(1));
2240 }
2241
TEST(GeneratedMapFieldTest,MissedKeyWireFormat)2242 TEST(GeneratedMapFieldTest, MissedKeyWireFormat) {
2243 unittest::TestMap message;
2244
2245 // No key field in wire format
2246 string data = "\x0A\x02\x10\x01";
2247
2248 EXPECT_TRUE(message.ParseFromString(data));
2249 EXPECT_EQ(1, message.map_int32_int32().size());
2250 EXPECT_EQ(1, message.map_int32_int32().at(0));
2251 }
2252
TEST(GeneratedMapFieldTest,MissedValueWireFormat)2253 TEST(GeneratedMapFieldTest, MissedValueWireFormat) {
2254 unittest::TestMap message;
2255
2256 // No value field in wire format
2257 string data = "\x0A\x02\x08\x01";
2258
2259 EXPECT_TRUE(message.ParseFromString(data));
2260 EXPECT_EQ(1, message.map_int32_int32().size());
2261 EXPECT_EQ(0, message.map_int32_int32().at(1));
2262 }
2263
TEST(GeneratedMapFieldTest,MissedValueTextFormat)2264 TEST(GeneratedMapFieldTest, MissedValueTextFormat) {
2265 unittest::TestMap message;
2266
2267 // No value field in text format
2268 string text =
2269 "map_int32_foreign_message {\n"
2270 " key: 1234567890\n"
2271 "}";
2272
2273 EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(text, &message));
2274 EXPECT_EQ(1, message.map_int32_foreign_message().size());
2275 EXPECT_EQ(11, message.ByteSize());
2276 }
2277
TEST(GeneratedMapFieldTest,UnknownFieldWireFormat)2278 TEST(GeneratedMapFieldTest, UnknownFieldWireFormat) {
2279 unittest::TestMap message;
2280
2281 // Unknown field in wire format
2282 string data = "\x0A\x06\x08\x02\x10\x03\x18\x01";
2283
2284 EXPECT_TRUE(message.ParseFromString(data));
2285 EXPECT_EQ(1, message.map_int32_int32().size());
2286 EXPECT_EQ(3, message.map_int32_int32().at(2));
2287 }
2288
TEST(GeneratedMapFieldTest,CorruptedWireFormat)2289 TEST(GeneratedMapFieldTest, CorruptedWireFormat) {
2290 unittest::TestMap message;
2291
2292 // corrupted data in wire format
2293 string data = "\x0A\x06\x08\x02\x11\x03";
2294
2295 EXPECT_FALSE(message.ParseFromString(data));
2296 }
2297
TEST(GeneratedMapFieldTest,IsInitialized)2298 TEST(GeneratedMapFieldTest, IsInitialized) {
2299 unittest::TestRequiredMessageMap map_message;
2300
2301 // Add an uninitialized message.
2302 (*map_message.mutable_map_field())[0];
2303 EXPECT_FALSE(map_message.IsInitialized());
2304
2305 // Initialize uninitialized message
2306 (*map_message.mutable_map_field())[0].set_a(0);
2307 (*map_message.mutable_map_field())[0].set_b(0);
2308 (*map_message.mutable_map_field())[0].set_c(0);
2309 EXPECT_TRUE(map_message.IsInitialized());
2310 }
2311
TEST(GeneratedMapFieldTest,MessagesMustMerge)2312 TEST(GeneratedMapFieldTest, MessagesMustMerge) {
2313 unittest::TestRequiredMessageMap map_message;
2314 unittest::TestRequired with_dummy4;
2315 with_dummy4.set_a(97);
2316 with_dummy4.set_b(0);
2317 with_dummy4.set_c(0);
2318 with_dummy4.set_dummy4(98);
2319
2320 EXPECT_TRUE(with_dummy4.IsInitialized());
2321 (*map_message.mutable_map_field())[0] = with_dummy4;
2322 EXPECT_TRUE(map_message.IsInitialized());
2323 string s = map_message.SerializeAsString();
2324
2325 // Modify s so that there are two values in the entry for key 0.
2326 // The first will have no value for c. The second will have no value for a.
2327 // Those are required fields. Also, make some other little changes, to
2328 // ensure we are merging the two values (because they're messages).
2329 ASSERT_EQ(s.size() - 2, s[1]); // encoding of the length of what follows
2330 string encoded_val(s.data() + 4, s.data() + s.size());
2331 // In s, change the encoding of c to an encoding of dummy32.
2332 s[s.size() - 3] -= 8;
2333 // Make encoded_val slightly different from what's in s.
2334 encoded_val[encoded_val.size() - 1] += 33; // Encode c = 33.
2335 for (int i = 0; i < encoded_val.size(); i++) {
2336 if (encoded_val[i] == 97) {
2337 // Encode b = 91 instead of a = 97. But this won't matter, because
2338 // we also encode b = 0 right after this. The point is to leave out
2339 // a required field, and make sure the parser doesn't complain, because
2340 // every required field is set after the merge of the two values.
2341 encoded_val[i - 1] += 16;
2342 encoded_val[i] = 91;
2343 } else if (encoded_val[i] == 98) {
2344 // Encode dummy5 = 99 instead of dummy4 = 98.
2345 encoded_val[i - 1] += 8; // The tag for dummy5 is 8 more.
2346 encoded_val[i]++;
2347 break;
2348 }
2349 }
2350
2351 s += encoded_val; // Add the second message.
2352 s[1] += encoded_val.size(); // Adjust encoded size.
2353
2354 // Test key then value then value.
2355 int key = 0;
2356 ASSERT_TRUE(map_message.ParseFromString(s));
2357 ASSERT_EQ(1, map_message.map_field().size());
2358 ASSERT_EQ(1, map_message.map_field().count(key));
2359 EXPECT_EQ(97, map_message.map_field().find(key)->second.a());
2360 EXPECT_EQ(0, map_message.map_field().find(key)->second.b());
2361 EXPECT_EQ(33, map_message.map_field().find(key)->second.c());
2362 EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4());
2363 EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5());
2364
2365 // Test key then value then value then key.
2366 s.push_back(s[2]); // Copy the key's tag.
2367 key = 19;
2368 s.push_back(key); // Second key is 19 instead of 0.
2369 s[1] += 2; // Adjust encoded size.
2370 ASSERT_TRUE(map_message.ParseFromString(s));
2371 ASSERT_EQ(1, map_message.map_field().size());
2372 ASSERT_EQ(1, map_message.map_field().count(key));
2373 EXPECT_EQ(97, map_message.map_field().find(key)->second.a());
2374 EXPECT_EQ(0, map_message.map_field().find(key)->second.b());
2375 EXPECT_EQ(33, map_message.map_field().find(key)->second.c());
2376 EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4());
2377 EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5());
2378 }
2379
2380 // Generated Message Reflection Test ================================
2381
TEST(GeneratedMapFieldReflectionTest,SpaceUsed)2382 TEST(GeneratedMapFieldReflectionTest, SpaceUsed) {
2383 unittest::TestMap message;
2384 MapReflectionTester reflection_tester(
2385 unittest::TestMap::descriptor());
2386 reflection_tester.SetMapFieldsViaReflection(&message);
2387
2388 EXPECT_LT(0, message.GetReflection()->SpaceUsed(message));
2389 }
2390
TEST(GeneratedMapFieldReflectionTest,Accessors)2391 TEST(GeneratedMapFieldReflectionTest, Accessors) {
2392 // Set every field to a unique value then go back and check all those
2393 // values.
2394 unittest::TestMap message;
2395 MapReflectionTester reflection_tester(
2396 unittest::TestMap::descriptor());
2397 reflection_tester.SetMapFieldsViaReflection(&message);
2398 MapTestUtil::ExpectMapFieldsSet(message);
2399 reflection_tester.ExpectMapFieldsSetViaReflection(message);
2400 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message);
2401
2402 reflection_tester.ModifyMapFieldsViaReflection(&message);
2403 MapTestUtil::ExpectMapFieldsModified(message);
2404 }
2405
TEST(GeneratedMapFieldReflectionTest,Swap)2406 TEST(GeneratedMapFieldReflectionTest, Swap) {
2407 unittest::TestMap message1;
2408 unittest::TestMap message2;
2409
2410 MapTestUtil::SetMapFields(&message1);
2411
2412 const Reflection* reflection = message1.GetReflection();
2413 reflection->Swap(&message1, &message2);
2414
2415 MapTestUtil::ExpectClear(message1);
2416 MapTestUtil::ExpectMapFieldsSet(message2);
2417 }
2418
TEST(GeneratedMapFieldReflectionTest,SwapWithBothSet)2419 TEST(GeneratedMapFieldReflectionTest, SwapWithBothSet) {
2420 unittest::TestMap message1;
2421 unittest::TestMap message2;
2422
2423 MapTestUtil::SetMapFields(&message1);
2424 MapTestUtil::SetMapFields(&message2);
2425 MapTestUtil::ModifyMapFields(&message2);
2426
2427 const Reflection* reflection = message1.GetReflection();
2428 reflection->Swap(&message1, &message2);
2429
2430 MapTestUtil::ExpectMapFieldsModified(message1);
2431 MapTestUtil::ExpectMapFieldsSet(message2);
2432 }
2433
TEST(GeneratedMapFieldReflectionTest,SwapFields)2434 TEST(GeneratedMapFieldReflectionTest, SwapFields) {
2435 unittest::TestMap message1;
2436 unittest::TestMap message2;
2437
2438 MapTestUtil::SetMapFields(&message2);
2439
2440 vector<const FieldDescriptor*> fields;
2441 const Reflection* reflection = message1.GetReflection();
2442 reflection->ListFields(message2, &fields);
2443 reflection->SwapFields(&message1, &message2, fields);
2444
2445 MapTestUtil::ExpectMapFieldsSet(message1);
2446 MapTestUtil::ExpectClear(message2);
2447 }
2448
TEST(GeneratedMapFieldReflectionTest,ClearField)2449 TEST(GeneratedMapFieldReflectionTest, ClearField) {
2450 unittest::TestMap message;
2451 MapTestUtil::SetMapFields(&message);
2452 MapTestUtil::ExpectMapFieldsSet(message);
2453
2454 MapReflectionTester reflection_tester(
2455 unittest::TestMap::descriptor());
2456 reflection_tester.ClearMapFieldsViaReflection(&message);
2457 reflection_tester.ExpectClearViaReflection(message);
2458 reflection_tester.ExpectClearViaReflectionIterator(&message);
2459 }
2460
TEST(GeneratedMapFieldReflectionTest,RemoveLast)2461 TEST(GeneratedMapFieldReflectionTest, RemoveLast) {
2462 unittest::TestMap message;
2463 MapReflectionTester reflection_tester(
2464 unittest::TestMap::descriptor());
2465
2466 MapTestUtil::SetMapFields(&message);
2467 MapTestUtil::ExpectMapsSize(message, 2);
2468 std::vector<const Message*> expected_entries =
2469 MapTestUtil::GetMapEntries(message, 0);
2470
2471 reflection_tester.RemoveLastMapsViaReflection(&message);
2472
2473 MapTestUtil::ExpectMapsSize(message, 1);
2474 std::vector<const Message*> remained_entries =
2475 MapTestUtil::GetMapEntries(message, 0);
2476 EXPECT_TRUE(expected_entries == remained_entries);
2477 }
2478
TEST(GeneratedMapFieldReflectionTest,ReleaseLast)2479 TEST(GeneratedMapFieldReflectionTest, ReleaseLast) {
2480 unittest::TestMap message;
2481 const Descriptor* descriptor = message.GetDescriptor();
2482 MapReflectionTester reflection_tester(descriptor);
2483
2484 MapTestUtil::SetMapFields(&message);
2485
2486 MapTestUtil::ExpectMapsSize(message, 2);
2487
2488 reflection_tester.ReleaseLastMapsViaReflection(&message);
2489
2490 MapTestUtil::ExpectMapsSize(message, 1);
2491
2492 // Now test that we actually release the right message.
2493 message.Clear();
2494 MapTestUtil::SetMapFields(&message);
2495
2496 MapTestUtil::ExpectMapsSize(message, 2);
2497 std::vector<const Message*> expect_last =
2498 MapTestUtil::GetMapEntries(message, 1);
2499 std::vector<const Message*> release_last =
2500 MapTestUtil::GetMapEntriesFromRelease(&message);
2501 MapTestUtil::ExpectMapsSize(message, 1);
2502 EXPECT_TRUE(expect_last == release_last);
2503 for (std::vector<const Message*>::iterator it = release_last.begin();
2504 it != release_last.end(); ++it) {
2505 delete *it;
2506 }
2507 }
2508
TEST(GeneratedMapFieldReflectionTest,SwapElements)2509 TEST(GeneratedMapFieldReflectionTest, SwapElements) {
2510 unittest::TestMap message;
2511 MapReflectionTester reflection_tester(
2512 unittest::TestMap::descriptor());
2513
2514 MapTestUtil::SetMapFields(&message);
2515
2516 // Get pointers of map entries at their original position
2517 std::vector<const Message*> entries0 = MapTestUtil::GetMapEntries(message, 0);
2518 std::vector<const Message*> entries1 = MapTestUtil::GetMapEntries(message, 1);
2519
2520 // Swap the first time.
2521 reflection_tester.SwapMapsViaReflection(&message);
2522
2523 // Get pointer of map entry after swap once.
2524 std::vector<const Message*> entries0_once =
2525 MapTestUtil::GetMapEntries(message, 0);
2526 std::vector<const Message*> entries1_once =
2527 MapTestUtil::GetMapEntries(message, 1);
2528
2529 // Test map entries are swapped.
2530 MapTestUtil::ExpectMapsSize(message, 2);
2531 EXPECT_TRUE(entries0 == entries1_once);
2532 EXPECT_TRUE(entries1 == entries0_once);
2533
2534 // Swap the second time.
2535 reflection_tester.SwapMapsViaReflection(&message);
2536
2537 // Get pointer of map entry after swap once.
2538 std::vector<const Message*> entries0_twice =
2539 MapTestUtil::GetMapEntries(message, 0);
2540 std::vector<const Message*> entries1_twice =
2541 MapTestUtil::GetMapEntries(message, 1);
2542
2543 // Test map entries are swapped back.
2544 MapTestUtil::ExpectMapsSize(message, 2);
2545 EXPECT_TRUE(entries0 == entries0_twice);
2546 EXPECT_TRUE(entries1 == entries1_twice);
2547 }
2548
TEST(GeneratedMapFieldReflectionTest,MutableUnknownFields)2549 TEST(GeneratedMapFieldReflectionTest, MutableUnknownFields) {
2550 unittest::TestMap message;
2551 MapReflectionTester reflection_tester(
2552 unittest::TestMap::descriptor());
2553 reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
2554 }
2555
TEST(GeneratedMapFieldReflectionTest,EmbedProto2Message)2556 TEST(GeneratedMapFieldReflectionTest, EmbedProto2Message) {
2557 unittest::TestMessageMap message;
2558
2559 const FieldDescriptor* map_field =
2560 unittest::TestMessageMap::descriptor()->FindFieldByName(
2561 "map_int32_message");
2562 const FieldDescriptor* value =
2563 map_field->message_type()->FindFieldByName("value");
2564
2565 Message* entry_message =
2566 message.GetReflection()->AddMessage(&message, map_field);
2567 EXPECT_EQ(
2568 &entry_message->GetReflection()->GetMessage(*entry_message, value),
2569 reinterpret_cast<const Message*>(&TestAllTypes::default_instance()));
2570
2571 Message* proto2_message =
2572 entry_message->GetReflection()->MutableMessage(entry_message, value);
2573 EXPECT_EQ(unittest::TestAllTypes::descriptor(),
2574 proto2_message->GetDescriptor());
2575 ASSERT_EQ(1, message.map_int32_message().size());
2576 }
2577
TEST(GeneratedMapFieldReflectionTest,MergeFromClearMapEntry)2578 TEST(GeneratedMapFieldReflectionTest, MergeFromClearMapEntry) {
2579 unittest::TestMap message;
2580 const FieldDescriptor* map_field =
2581 unittest::TestMap::descriptor()->FindFieldByName("map_int32_int32");
2582 const FieldDescriptor* key =
2583 map_field->message_type()->FindFieldByName("key");
2584 const FieldDescriptor* value =
2585 map_field->message_type()->FindFieldByName("value");
2586
2587 Message* entry_message1 =
2588 message.GetReflection()->AddMessage(&message, map_field);
2589 EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
2590 EXPECT_FALSE(
2591 entry_message1->GetReflection()->HasField(*entry_message1, value));
2592
2593 Message* entry_message2 =
2594 message.GetReflection()->AddMessage(&message, map_field);
2595 EXPECT_FALSE(entry_message2->GetReflection()->HasField(*entry_message2, key));
2596 EXPECT_FALSE(
2597 entry_message2->GetReflection()->HasField(*entry_message2, value));
2598
2599 entry_message1->MergeFrom(*entry_message2);
2600 EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
2601 EXPECT_FALSE(
2602 entry_message1->GetReflection()->HasField(*entry_message1, value));
2603 }
2604
TEST(GeneratedMapFieldReflectionTest,MapEntryClear)2605 TEST(GeneratedMapFieldReflectionTest, MapEntryClear) {
2606 unittest::TestMap message;
2607 MapReflectionTester reflection_tester(
2608 unittest::TestMap::descriptor());
2609 reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
2610 }
2611
TEST(GeneratedMapFieldReflectionTest,Proto2MapEntryClear)2612 TEST(GeneratedMapFieldReflectionTest, Proto2MapEntryClear) {
2613 unittest::TestEnumMap message;
2614 const Descriptor* descriptor = message.GetDescriptor();
2615 const FieldDescriptor* field_descriptor =
2616 descriptor->FindFieldByName("known_map_field");
2617 const FieldDescriptor* value_descriptor =
2618 field_descriptor->message_type()->FindFieldByName("value");
2619 Message* sub_message =
2620 message.GetReflection()->AddMessage(&message, field_descriptor);
2621 EXPECT_EQ(0, sub_message->GetReflection()->GetEnumValue(*sub_message,
2622 value_descriptor));
2623 }
2624
2625 // Map Reflection API Test =========================================
2626
TEST(GeneratedMapFieldReflectionTest,SetViaMapReflection)2627 TEST(GeneratedMapFieldReflectionTest, SetViaMapReflection) {
2628 unittest::TestMap message;
2629 MapReflectionTester reflection_tester(
2630 unittest::TestMap::descriptor());
2631 reflection_tester.SetMapFieldsViaMapReflection(&message);
2632 reflection_tester.ExpectMapFieldsSetViaReflection(message);
2633 reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message);
2634 }
2635
2636 // Dynamic Message Test =============================================
2637
2638 class MapFieldInDynamicMessageTest : public testing::Test {
2639 protected:
2640 const DescriptorPool* pool_;
2641 DynamicMessageFactory factory_;
2642 const Descriptor* map_descriptor_;
2643 const Descriptor* recursive_map_descriptor_;
2644 const Message* map_prototype_;
2645
MapFieldInDynamicMessageTest()2646 MapFieldInDynamicMessageTest()
2647 : pool_(DescriptorPool::generated_pool()), factory_(pool_) {}
2648
SetUp()2649 virtual void SetUp() {
2650 map_descriptor_ =
2651 pool_->FindMessageTypeByName("protobuf_unittest.TestMap");
2652 recursive_map_descriptor_ =
2653 pool_->FindMessageTypeByName("protobuf_unittest.TestRecursiveMapMessage");
2654 ASSERT_TRUE(map_descriptor_ != NULL);
2655 ASSERT_TRUE(recursive_map_descriptor_ != NULL);
2656 map_prototype_ = factory_.GetPrototype(map_descriptor_);
2657 }
2658 };
2659
TEST_F(MapFieldInDynamicMessageTest,MapIndependentOffsets)2660 TEST_F(MapFieldInDynamicMessageTest, MapIndependentOffsets) {
2661 // Check that all fields have independent offsets by setting each
2662 // one to a unique value then checking that they all still have those
2663 // unique values (i.e. they don't stomp each other).
2664 google::protobuf::scoped_ptr<Message> message(map_prototype_->New());
2665 MapReflectionTester reflection_tester(map_descriptor_);
2666
2667 reflection_tester.SetMapFieldsViaReflection(message.get());
2668 reflection_tester.ExpectMapFieldsSetViaReflection(*message);
2669 }
2670
TEST_F(MapFieldInDynamicMessageTest,DynamicMapReflection)2671 TEST_F(MapFieldInDynamicMessageTest, DynamicMapReflection) {
2672 // Check that map fields work properly.
2673 google::protobuf::scoped_ptr<Message> message(map_prototype_->New());
2674
2675 // Check set functions.
2676 MapReflectionTester reflection_tester(map_descriptor_);
2677 reflection_tester.SetMapFieldsViaMapReflection(message.get());
2678 reflection_tester.ExpectMapFieldsSetViaReflection(*message);
2679 }
2680
TEST_F(MapFieldInDynamicMessageTest,MapSpaceUsed)2681 TEST_F(MapFieldInDynamicMessageTest, MapSpaceUsed) {
2682 // Test that SpaceUsed() works properly
2683
2684 // Since we share the implementation with generated messages, we don't need
2685 // to test very much here. Just make sure it appears to be working.
2686
2687 google::protobuf::scoped_ptr<Message> message(map_prototype_->New());
2688 MapReflectionTester reflection_tester(map_descriptor_);
2689
2690 int initial_space_used = message->SpaceUsed();
2691
2692 reflection_tester.SetMapFieldsViaReflection(message.get());
2693 EXPECT_LT(initial_space_used, message->SpaceUsed());
2694 }
2695
TEST_F(MapFieldInDynamicMessageTest,RecursiveMap)2696 TEST_F(MapFieldInDynamicMessageTest, RecursiveMap) {
2697 TestRecursiveMapMessage from;
2698 (*from.mutable_a())[""];
2699 string data = from.SerializeAsString();
2700 google::protobuf::scoped_ptr<Message> to(
2701 factory_.GetPrototype(recursive_map_descriptor_)->New());
2702 ASSERT_TRUE(to->ParseFromString(data));
2703 }
2704
2705 // ReflectionOps Test ===============================================
2706
TEST(ReflectionOpsForMapFieldTest,MapSanityCheck)2707 TEST(ReflectionOpsForMapFieldTest, MapSanityCheck) {
2708 unittest::TestMap message;
2709
2710 MapTestUtil::SetMapFields(&message);
2711 MapTestUtil::ExpectMapFieldsSet(message);
2712 }
2713
TEST(ReflectionOpsForMapFieldTest,MapCopy)2714 TEST(ReflectionOpsForMapFieldTest, MapCopy) {
2715 unittest::TestMap message, message2;
2716
2717 MapTestUtil::SetMapFields(&message);
2718
2719 ReflectionOps::Copy(message, &message2);
2720
2721 MapTestUtil::ExpectMapFieldsSet(message2);
2722
2723 // Copying from self should be a no-op.
2724 ReflectionOps::Copy(message2, &message2);
2725 MapTestUtil::ExpectMapFieldsSet(message2);
2726 }
2727
TEST(ReflectionOpsForMapFieldTest,MergeMap)2728 TEST(ReflectionOpsForMapFieldTest, MergeMap) {
2729 // Note: Copy is implemented in terms of Merge() so technically the Copy
2730 // test already tested most of this.
2731
2732 unittest::TestMap message, message2;
2733
2734 MapTestUtil::SetMapFields(&message);
2735
2736 ReflectionOps::Merge(message2, &message);
2737
2738 MapTestUtil::ExpectMapFieldsSet(message);
2739 }
2740
TEST(ReflectionOpsForMapFieldTest,ClearMap)2741 TEST(ReflectionOpsForMapFieldTest, ClearMap) {
2742 unittest::TestMap message;
2743
2744 MapTestUtil::SetMapFields(&message);
2745
2746 ReflectionOps::Clear(&message);
2747
2748 MapTestUtil::ExpectClear(message);
2749 }
2750
TEST(ReflectionOpsForMapFieldTest,MapDiscardUnknownFields)2751 TEST(ReflectionOpsForMapFieldTest, MapDiscardUnknownFields) {
2752 unittest::TestMap message;
2753 MapTestUtil::SetMapFields(&message);
2754
2755 // Set some unknown fields in message.
2756 message.GetReflection()->MutableUnknownFields(&message)->
2757 AddVarint(123456, 654321);
2758
2759 // Discard them.
2760 ReflectionOps::DiscardUnknownFields(&message);
2761 MapTestUtil::ExpectMapFieldsSet(message);
2762
2763 EXPECT_EQ(0, message.GetReflection()->
2764 GetUnknownFields(message).field_count());
2765 }
2766
2767 // Wire Format Test =================================================
2768
TEST(WireFormatForMapFieldTest,ParseMap)2769 TEST(WireFormatForMapFieldTest, ParseMap) {
2770 unittest::TestMap source, dest;
2771 string data;
2772
2773 // Serialize using the generated code.
2774 MapTestUtil::SetMapFields(&source);
2775 source.SerializeToString(&data);
2776
2777 // Parse using WireFormat.
2778 io::ArrayInputStream raw_input(data.data(), data.size());
2779 io::CodedInputStream input(&raw_input);
2780 WireFormat::ParseAndMergePartial(&input, &dest);
2781
2782 // Check.
2783 MapTestUtil::ExpectMapFieldsSet(dest);
2784 }
2785
TEST(WireFormatForMapFieldTest,MapByteSize)2786 TEST(WireFormatForMapFieldTest, MapByteSize) {
2787 unittest::TestMap message;
2788 MapTestUtil::SetMapFields(&message);
2789
2790 EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message));
2791 message.Clear();
2792 EXPECT_EQ(0, message.ByteSize());
2793 EXPECT_EQ(0, WireFormat::ByteSize(message));
2794 }
2795
TEST(WireFormatForMapFieldTest,SerializeMap)2796 TEST(WireFormatForMapFieldTest, SerializeMap) {
2797 unittest::TestMap message;
2798 string generated_data;
2799 string dynamic_data;
2800
2801 MapTestUtil::SetMapFields(&message);
2802
2803 // Serialize using the generated code.
2804 {
2805 message.ByteSize();
2806 io::StringOutputStream raw_output(&generated_data);
2807 io::CodedOutputStream output(&raw_output);
2808 message.SerializeWithCachedSizes(&output);
2809 ASSERT_FALSE(output.HadError());
2810 }
2811
2812 // Serialize using WireFormat.
2813 {
2814 io::StringOutputStream raw_output(&dynamic_data);
2815 io::CodedOutputStream output(&raw_output);
2816 int size = WireFormat::ByteSize(message);
2817 WireFormat::SerializeWithCachedSizes(message, size, &output);
2818 ASSERT_FALSE(output.HadError());
2819 }
2820
2821 // Should be the same.
2822 // Don't use EXPECT_EQ here because we're comparing raw binary data and
2823 // we really don't want it dumped to stdout on failure.
2824 EXPECT_TRUE(dynamic_data == generated_data);
2825 }
2826
TEST(WireFormatForMapFieldTest,MapParseHelpers)2827 TEST(WireFormatForMapFieldTest, MapParseHelpers) {
2828 string data;
2829
2830 {
2831 // Set up.
2832 protobuf_unittest::TestMap message;
2833 MapTestUtil::SetMapFields(&message);
2834 message.SerializeToString(&data);
2835 }
2836
2837 {
2838 // Test ParseFromString.
2839 protobuf_unittest::TestMap message;
2840 EXPECT_TRUE(message.ParseFromString(data));
2841 MapTestUtil::ExpectMapFieldsSet(message);
2842 }
2843
2844 {
2845 // Test ParseFromIstream.
2846 protobuf_unittest::TestMap message;
2847 stringstream stream(data);
2848 EXPECT_TRUE(message.ParseFromIstream(&stream));
2849 EXPECT_TRUE(stream.eof());
2850 MapTestUtil::ExpectMapFieldsSet(message);
2851 }
2852
2853 {
2854 // Test ParseFromBoundedZeroCopyStream.
2855 string data_with_junk(data);
2856 data_with_junk.append("some junk on the end");
2857 io::ArrayInputStream stream(data_with_junk.data(), data_with_junk.size());
2858 protobuf_unittest::TestMap message;
2859 EXPECT_TRUE(message.ParseFromBoundedZeroCopyStream(&stream, data.size()));
2860 MapTestUtil::ExpectMapFieldsSet(message);
2861 }
2862
2863 {
2864 // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if
2865 // EOF is reached before the expected number of bytes.
2866 io::ArrayInputStream stream(data.data(), data.size());
2867 protobuf_unittest::TestAllTypes message;
2868 EXPECT_FALSE(
2869 message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1));
2870 }
2871 }
2872
2873 // Deterministic Serialization Test ==========================================
2874
2875 template <typename T>
DeterministicSerialization(const T & t)2876 static string DeterministicSerialization(const T& t) {
2877 const int size = t.ByteSize();
2878 string result(size, '\0');
2879 io::ArrayOutputStream array_stream(string_as_array(&result), size);
2880 io::CodedOutputStream output_stream(&array_stream);
2881 output_stream.SetSerializationDeterministic(true);
2882 t.SerializeWithCachedSizes(&output_stream);
2883 EXPECT_FALSE(output_stream.HadError());
2884 EXPECT_EQ(size, output_stream.ByteCount());
2885 return result;
2886 }
2887
2888 // Helper to test the serialization of the first arg against a golden file.
TestDeterministicSerialization(const protobuf_unittest::TestMaps & t,const string & filename)2889 static void TestDeterministicSerialization(const protobuf_unittest::TestMaps& t,
2890 const string& filename) {
2891 string expected;
2892 GOOGLE_CHECK_OK(File::GetContents(
2893 TestSourceDir() + "/google/protobuf/testdata/" + filename,
2894 &expected, true));
2895 const string actual = DeterministicSerialization(t);
2896 EXPECT_EQ(expected, actual);
2897 protobuf_unittest::TestMaps u;
2898 EXPECT_TRUE(u.ParseFromString(actual));
2899 EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals(u, t));
2900 }
2901
2902 // Helper for MapSerializationTest. Return a 7-bit ASCII string.
ConstructKey(uint64 n)2903 static string ConstructKey(uint64 n) {
2904 string s(n % static_cast<uint64>(9), '\0');
2905 if (s.empty()) {
2906 return StrCat(n);
2907 } else {
2908 while (n != 0) {
2909 s[n % s.size()] = (n >> 10) & 0x7f;
2910 n /= 888;
2911 }
2912 return s;
2913 }
2914 }
2915
TEST(MapSerializationTest,Deterministic)2916 TEST(MapSerializationTest, Deterministic) {
2917 const int kIters = 25;
2918 protobuf_unittest::TestMaps t;
2919 protobuf_unittest::TestIntIntMap inner;
2920 (*inner.mutable_m())[0] = (*inner.mutable_m())[10] =
2921 (*inner.mutable_m())[-200] = 0;
2922 uint64 frog = 9;
2923 const uint64 multiplier = 0xa29cd16f;
2924 for (int i = 0; i < kIters; i++) {
2925 const int32 i32 = static_cast<int32>(frog & 0xffffffff);
2926 const uint32 u32 = static_cast<uint32>(i32) * 91919;
2927 const int64 i64 = static_cast<int64>(frog);
2928 const uint64 u64 = frog * static_cast<uint64>(187321);
2929 const bool b = i32 > 0;
2930 const string s = ConstructKey(frog);
2931 (*inner.mutable_m())[i] = i32;
2932 (*t.mutable_m_int32())[i32] = (*t.mutable_m_sint32())[i32] =
2933 (*t.mutable_m_sfixed32())[i32] = inner;
2934 (*t.mutable_m_uint32())[u32] = (*t.mutable_m_fixed32())[u32] = inner;
2935 (*t.mutable_m_int64())[i64] = (*t.mutable_m_sint64())[i64] =
2936 (*t.mutable_m_sfixed64())[i64] = inner;
2937 (*t.mutable_m_uint64())[u64] = (*t.mutable_m_fixed64())[u64] = inner;
2938 (*t.mutable_m_bool())[b] = inner;
2939 (*t.mutable_m_string())[s] = inner;
2940 (*t.mutable_m_string())[s + string(1 << (u32 % static_cast<uint32>(9)),
2941 b)] = inner;
2942 inner.mutable_m()->erase(i);
2943 frog = frog * multiplier + i;
2944 frog ^= (frog >> 41);
2945 }
2946 TestDeterministicSerialization(t, "golden_message_maps");
2947 }
2948
2949 // Text Format Test =================================================
2950
TEST(TextFormatMapTest,SerializeAndParse)2951 TEST(TextFormatMapTest, SerializeAndParse) {
2952 unittest::TestMap source;
2953 unittest::TestMap dest;
2954 MapTestUtil::SetMapFields(&source);
2955 string output;
2956
2957 // Test compact ASCII
2958 TextFormat::Printer printer;
2959 printer.PrintToString(source, &output);
2960 TextFormat::Parser parser;
2961 EXPECT_TRUE(parser.ParseFromString(output, &dest));
2962 MapTestUtil::ExpectMapFieldsSet(dest);
2963 }
2964
TEST(TextFormatMapTest,Sorted)2965 TEST(TextFormatMapTest, Sorted) {
2966 unittest::TestMap message;
2967 MapReflectionTester tester(message.GetDescriptor());
2968 tester.SetMapFieldsViaReflection(&message);
2969
2970 string expected_text;
2971 GOOGLE_CHECK_OK(File::GetContents(
2972 TestSourceDir() +
2973 "/google/protobuf/"
2974 "testdata/map_test_data.txt",
2975 &expected_text, true));
2976
2977 EXPECT_EQ(message.DebugString(), expected_text);
2978
2979 // Test again on the reverse order.
2980 unittest::TestMap message2;
2981 tester.SetMapFieldsViaReflection(&message2);
2982 tester.SwapMapsViaReflection(&message2);
2983 EXPECT_EQ(message2.DebugString(), expected_text);
2984 }
2985
2986
2987 // arena support =================================================
TEST(ArenaTest,ParsingAndSerializingNoHeapAllocation)2988 TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) {
2989 // Allocate a large initial block to avoid mallocs during hooked test.
2990 std::vector<char> arena_block(128 * 1024);
2991 ArenaOptions options;
2992 options.initial_block = &arena_block[0];
2993 options.initial_block_size = arena_block.size();
2994 Arena arena(options);
2995 string data;
2996 data.reserve(128 * 1024);
2997
2998 {
2999 // TODO(teboring): Enable no heap check when ArenaStringPtr is used in map.
3000 // NoHeapChecker no_heap;
3001
3002 unittest::TestArenaMap* from =
3003 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
3004 MapTestUtil::SetArenaMapFields(from);
3005 from->SerializeToString(&data);
3006
3007 unittest::TestArenaMap* to =
3008 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
3009 to->ParseFromString(data);
3010 MapTestUtil::ExpectArenaMapFieldsSet(*to);
3011 }
3012 }
3013
3014 // Use text format parsing and serializing to test reflection api.
TEST(ArenaTest,RelfectionInTextFormat)3015 TEST(ArenaTest, RelfectionInTextFormat) {
3016 Arena arena;
3017 string data;
3018
3019 TextFormat::Printer printer;
3020 TextFormat::Parser parser;
3021
3022 unittest::TestArenaMap* from =
3023 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
3024 unittest::TestArenaMap* to =
3025 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
3026
3027 MapTestUtil::SetArenaMapFields(from);
3028 printer.PrintToString(*from, &data);
3029
3030 EXPECT_TRUE(parser.ParseFromString(data, to));
3031 MapTestUtil::ExpectArenaMapFieldsSet(*to);
3032 }
3033
3034 // Make sure the memory allocated for string in map is deallocated.
TEST(ArenaTest,StringMapNoLeak)3035 TEST(ArenaTest, StringMapNoLeak) {
3036 Arena arena;
3037 unittest::TestArenaMap* message =
3038 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
3039 string data;
3040 // String with length less than 16 will not be allocated from heap.
3041 int original_capacity = data.capacity();
3042 while (data.capacity() <= original_capacity) {
3043 data.append("a");
3044 }
3045 (*message->mutable_map_string_string())[data] = data;
3046 // We rely on heap checkers to detect memory leak for us.
3047 ASSERT_FALSE(message == NULL);
3048 }
3049
TEST(ArenaTest,IsInitialized)3050 TEST(ArenaTest, IsInitialized) {
3051 // Allocate a large initial polluted block.
3052 std::vector<char> arena_block(128 * 1024);
3053 std::fill(arena_block.begin(), arena_block.end(), '\xff');
3054
3055 ArenaOptions options;
3056 options.initial_block = &arena_block[0];
3057 options.initial_block_size = arena_block.size();
3058 Arena arena(options);
3059
3060 unittest::TestArenaMap* message =
3061 Arena::CreateMessage<unittest::TestArenaMap>(&arena);
3062 EXPECT_EQ(0, (*message->mutable_map_int32_int32())[0]);
3063 }
3064
3065 } // namespace internal
3066 } // namespace protobuf
3067 } // namespace google
3068