• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// -*- c++ -*-
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4//
5// Use of this source code is governed by a BSD-style
6// license that can be found in the LICENSE file or at
7// https://developers.google.com/open-source/licenses/bsd
8
9// A hack to include windows.h first, which ensures the GetMessage macro can
10// be undefined when we include <google/protobuf/stubs/common.h>
11#if defined(_MSC_VER)
12#define _WINSOCKAPI_  // to avoid re-definition in WinSock2.h
13#define NOMINMAX      // to avoid defining min/max macros
14#include <windows.h>
15#endif  // _WIN32
16
17#include <algorithm>
18#include <memory>
19#include <random>
20#include <sstream>
21#include <vector>
22
23#include "google/protobuf/testing/file.h"
24#include "google/protobuf/descriptor.pb.h"
25#include <gmock/gmock.h>
26#include "google/protobuf/testing/googletest.h"
27#include <gtest/gtest.h>
28#include "absl/base/casts.h"
29#include "absl/cleanup/cleanup.h"
30#include "absl/container/btree_set.h"
31#include "absl/container/flat_hash_map.h"
32#include "absl/container/flat_hash_set.h"
33#include "absl/log/absl_check.h"
34#include "absl/log/absl_log.h"
35#include "absl/strings/substitute.h"
36#include "absl/time/time.h"
37#include "google/protobuf/arena_test_util.h"
38#include "google/protobuf/descriptor.h"
39#include "google/protobuf/descriptor_database.h"
40#include "google/protobuf/dynamic_message.h"
41#include "google/protobuf/io/coded_stream.h"
42#include "google/protobuf/io/tokenizer.h"
43#include "google/protobuf/io/zero_copy_stream_impl.h"
44#include "google/protobuf/map.h"
45#include "google/protobuf/map_field_inl.h"
46#include "google/protobuf/map_proto2_unittest.pb.h"
47#include "google/protobuf/map_proto3_unittest.pb.h"
48#include "google/protobuf/message.h"
49#include "google/protobuf/port.h"
50#include "google/protobuf/reflection.h"
51#include "google/protobuf/reflection_ops.h"
52#include "google/protobuf/test_util2.h"
53#include "google/protobuf/text_format.h"
54#include "google/protobuf/util/message_differencer.h"
55#include "google/protobuf/wire_format.h"
56
57
58// Must be included last.
59#include "google/protobuf/port_def.inc"
60
61using ::testing::ElementsAre;
62using ::testing::IsEmpty;
63using ::testing::Pair;
64
65namespace google {
66namespace protobuf {
67
68using UNITTEST::ForeignMessage;
69using UNITTEST::TestAllTypes;
70using UNITTEST::TestMap;
71using UNITTEST::TestRecursiveMapMessage;
72
73namespace internal {
74
75void MapTestForceDeterministic() {
76  io::CodedOutputStream::SetDefaultSerializationDeterministic();
77}
78
79struct MoveTestKey {
80  MoveTestKey(int data, int* copies) : data(data), copies(copies) {}
81
82  MoveTestKey(const MoveTestKey& other)
83      : data(other.data), copies(other.copies) {
84    ++*copies;
85  }
86
87  MoveTestKey(MoveTestKey&& other) noexcept
88      : data(other.data), copies(other.copies) {}
89
90  friend bool operator==(const MoveTestKey& lhs, const MoveTestKey& rhs) {
91    return lhs.data == rhs.data;
92  }
93  friend bool operator<(const MoveTestKey& lhs, const MoveTestKey& rhs) {
94    return lhs.data < rhs.data;
95  }
96
97  int data;
98  int* copies;
99};
100
101enum class ConstructorType {
102  kDefault,
103  kCopy,
104  kMove,
105};
106
107struct ConstructorTag {
108  ConstructorTag() : invoked_constructor(ConstructorType::kDefault) {}
109  ConstructorTag(const ConstructorTag&)
110      : invoked_constructor(ConstructorType::kCopy) {}
111  ConstructorTag(ConstructorTag&&)
112      : invoked_constructor(ConstructorType::kMove) {}
113
114  bool operator<(const ConstructorTag&) const { return false; }
115
116  ConstructorType invoked_constructor;
117};
118
119struct CountedInstance {
120  CountedInstance() { ++num_created; }
121  CountedInstance(const CountedInstance&) : CountedInstance() {}
122  CountedInstance(CountedInstance&&) : CountedInstance() {}
123
124  CountedInstance& operator=(const CountedInstance&) {
125    ++num_assigned;
126    return *this;
127  }
128
129  explicit CountedInstance(int x) : CountedInstance() {}
130
131  static int num_created;
132  static int num_assigned;
133};
134
135int CountedInstance::num_created = 0;
136int CountedInstance::num_assigned = 0;
137
138struct ArenaConstructible {
139  using InternalArenaConstructable_ = void;
140  using DestructorSkippable_ = void;
141
142  ArenaConstructible() = default;
143  ArenaConstructible(const ArenaConstructible&) = default;
144  ArenaConstructible(Arena*) : ArenaConstructible() {}
145
146  ArenaConstructible& operator=(const ArenaConstructible&) = default;
147
148  explicit ArenaConstructible(int) : ArenaConstructible() {}
149
150  Arena* arena() const { return nullptr; }
151
152  CountedInstance unused;
153};
154
155template <>
156struct is_internal_map_key_type<MoveTestKey> : std::true_type {};
157template <>
158struct is_internal_map_key_type<ConstructorTag> : std::true_type {};
159template <>
160struct is_internal_map_value_type<ConstructorTag> : std::true_type {};
161template <>
162struct is_internal_map_value_type<ArenaConstructible> : std::true_type {};
163template <>
164struct is_internal_map_value_type<CountedInstance> : std::true_type {};
165
166struct MapTestPeer {
167  template <typename T>
168  static typename T::KeyMapBase& GetKeyMapBase(T& value) {
169    return value;
170  }
171
172  template <typename T>
173  static bool InsertOrReplaceNode(T& map, typename T::key_type key,
174                                  typename T::mapped_type value) {
175    using Node = typename T::Node;
176    auto* node = static_cast<Node*>(map.AllocNode(sizeof(Node)));
177    ::new (static_cast<void*>(&node->kv)) typename T::value_type{key, value};
178    node = static_cast<Node*>(GetKeyMapBase(map).InsertOrReplaceNode(node));
179    if (node) {
180      node->~Node();
181      GetKeyMapBase(map).DeallocNode(node, sizeof(Node));
182      return false;
183    }
184    return true;
185  }
186
187  template <typename T>
188  static size_t NumBuckets(T& map) {
189    return map.num_buckets_;
190  }
191
192  template <typename T>
193  static size_t BucketNumber(T& map, typename T::key_type key) {
194    return map.BucketNumber(key);
195  }
196
197  template <typename T>
198  static void Resize(T& map, size_t num_buckets) {
199    map.Resize(num_buckets);
200  }
201
202  template <typename T>
203  static bool HasTreeBuckets(T& map) {
204    for (size_t i = 0; i < map.num_buckets_; ++i) {
205      if (TableEntryIsTree(map.table_[i])) return true;
206    }
207    return false;
208  }
209
210  static int CalculateHiCutoff(int num_buckets) {
211    return Map<int, int>::CalculateHiCutoff(num_buckets);
212  }
213};
214
215namespace {
216using internal::DownCast;
217
218// Map API Test =====================================================
219
220class MapImplTest : public ::testing::Test {
221 protected:
222  MapImplTest()
223      : map_ptr_(new Map<int32_t, int32_t>()),
224        map_(*map_ptr_),
225        const_map_(*map_ptr_) {
226    EXPECT_TRUE(map_.empty());
227    EXPECT_EQ(0, map_.size());
228  }
229
230  void ExpectSingleElement(int32_t key, int32_t value) {
231    EXPECT_FALSE(map_.empty());
232    EXPECT_EQ(1, map_.size());
233    ExpectElement(key, value);
234  }
235
236  void ExpectElements(const absl::flat_hash_map<int32_t, int32_t>& map) {
237    EXPECT_FALSE(map_.empty());
238    EXPECT_EQ(map.size(), map_.size());
239    for (const auto& e : map) {
240      ExpectElement(e.first, e.second);
241    }
242  }
243
244  void ExpectElement(int32_t key, int32_t value) {
245    // Test map size is correct.
246    EXPECT_EQ(value, map_[key]);
247    EXPECT_EQ(1, map_.count(key));
248    EXPECT_TRUE(map_.contains(key));
249
250    // Check mutable at and find work correctly.
251    EXPECT_EQ(value, map_.at(key));
252    Map<int32_t, int32_t>::iterator it = map_.find(key);
253
254    // iterator dereferenceable
255    EXPECT_EQ(key, (*it).first);
256    EXPECT_EQ(value, (*it).second);
257    EXPECT_EQ(key, it->first);
258    EXPECT_EQ(value, it->second);
259
260    // iterator mutable
261    ((*it).second) = value + 1;
262    EXPECT_EQ(value + 1, map_[key]);
263    ((*it).second) = value;
264    EXPECT_EQ(value, map_[key]);
265
266    it->second = value + 1;
267    EXPECT_EQ(value + 1, map_[key]);
268    it->second = value;
269    EXPECT_EQ(value, map_[key]);
270
271    // copy constructor
272    Map<int32_t, int32_t>::iterator it_copy = it;
273    EXPECT_EQ(key, it_copy->first);
274    EXPECT_EQ(value, it_copy->second);
275
276    // Immutable API ================================================
277
278    // Check immutable at and find work correctly.
279    EXPECT_EQ(value, const_map_.at(key));
280    Map<int32_t, int32_t>::const_iterator const_it = const_map_.find(key);
281
282    // iterator dereferenceable
283    EXPECT_EQ(key, (*const_it).first);
284    EXPECT_EQ(value, (*const_it).second);
285    EXPECT_EQ(key, const_it->first);
286    EXPECT_EQ(value, const_it->second);
287
288    // copy constructor
289    Map<int32_t, int32_t>::const_iterator const_it_copy = const_it;
290    EXPECT_EQ(key, const_it_copy->first);
291    EXPECT_EQ(value, const_it_copy->second);
292  }
293
294  std::unique_ptr<Map<int32_t, int32_t>> map_ptr_;
295  Map<int32_t, int32_t>& map_;
296  const Map<int32_t, int32_t>& const_map_;
297};
298
299TEST_F(MapImplTest, OperatorBracket) {
300  int32_t key = 0;
301  int32_t value1 = 100;
302  int32_t value2 = 101;
303
304  EXPECT_EQ(0, map_[key]);
305
306  map_[key] = value1;
307  ExpectSingleElement(key, value1);
308
309  map_[key] = value2;
310  ExpectSingleElement(key, value2);
311}
312
313}  // namespace
314}  // namespace internal
315}  // namespace protobuf
316}  // namespace google
317
318namespace std {
319
320template <>  // NOLINT
321struct hash<google::protobuf::internal::MoveTestKey> {
322  size_t operator()(const google::protobuf::internal::MoveTestKey& key) const {
323    return hash<uint64_t>{}(key.data);
324  }
325};
326}  // namespace std
327
328namespace google {
329namespace protobuf {
330namespace internal {
331
332template <>
333struct RealKeyToVariantKey<MoveTestKey> {
334  VariantKey operator()(const MoveTestKey& value) const {
335    return VariantKey(value.data);
336  }
337};
338
339template <>
340struct RealKeyToVariantKeyAlternative<MoveTestKey> {
341  VariantKey operator()(const MoveTestKey& value) const {
342    return VariantKey(value.data);
343  }
344};
345
346namespace {
347
348TEST_F(MapImplTest, OperatorBracketRValue) {
349  Arena arena;
350  for (Arena* arena_to_use : {&arena, static_cast<Arena*>(nullptr)}) {
351    int copies = 0;
352    Map<MoveTestKey, int> map(arena_to_use);
353    MoveTestKey key1(1, &copies);
354    EXPECT_EQ(copies, 0);
355    map[key1] = 0;
356    EXPECT_EQ(copies, 1);
357    map[MoveTestKey(2, &copies)] = 2;
358    EXPECT_EQ(copies, 1);
359  }
360}
361
362TEST_F(MapImplTest, OperatorBracketNonExist) {
363  int32_t key = 0;
364  int32_t default_value = 0;
365
366  EXPECT_EQ(default_value, map_[key]);
367  ExpectSingleElement(key, default_value);
368}
369
370TEST_F(MapImplTest, MutableAt) {
371  int32_t key = 0;
372  int32_t value1 = 100;
373  int32_t value2 = 101;
374
375  map_[key] = value1;
376  ExpectSingleElement(key, value1);
377
378  map_.at(key) = value2;
379  ExpectSingleElement(key, value2);
380}
381
382#if GTEST_HAS_DEATH_TEST
383
384TEST_F(MapImplTest, MutableAtNonExistDeathTest) {
385  EXPECT_DEATH(map_.at(0), "");
386}
387
388TEST_F(MapImplTest, ImmutableAtNonExistDeathTest) {
389  EXPECT_DEATH(const_map_.at(0), "");
390}
391
392TEST_F(MapImplTest, UsageErrors) {
393  MapKey key;
394  key.SetInt64Value(1);
395  EXPECT_DEATH(key.GetUInt64Value(),
396               "Protocol Buffer map usage error:\n"
397               "MapKey::GetUInt64Value type does not match\n"
398               "  Expected : uint64\n"
399               "  Actual   : int64");
400
401  MapValueRef value;
402  EXPECT_DEATH(
403      value.SetFloatValue(0.1),
404      testing::AnyOf(
405        testing::HasSubstr(
406          "Protocol Buffer map usage error:\n"
407          "MapValueRef::type MapValueRef is not initialized."),
408        testing::HasSubstr(
409          "Protocol Buffer map usage error:\n"
410          "MapValueConstRef::type MapValueConstRef is not initialized.")));
411}
412
413#endif  // GTEST_HAS_DEATH_TEST
414
415TEST_F(MapImplTest, MapKeyAssignment) {
416  MapKey from, to;
417  from.SetStringValue("abc");
418  to = from;
419  EXPECT_EQ("abc", to.GetStringValue());
420}
421
422TEST_F(MapImplTest, CountNonExist) { EXPECT_EQ(0, map_.count(0)); }
423
424TEST_F(MapImplTest, ContainNotExist) { EXPECT_FALSE(map_.contains(0)); }
425
426TEST_F(MapImplTest, ImmutableContainNotExist) {
427  EXPECT_FALSE(const_map_.contains(0));
428}
429
430TEST_F(MapImplTest, MutableFindNonExist) {
431  EXPECT_TRUE(map_.end() == map_.find(0));
432}
433
434TEST_F(MapImplTest, ImmutableFindNonExist) {
435  EXPECT_TRUE(const_map_.end() == const_map_.find(0));
436}
437
438TEST_F(MapImplTest, ConstEnd) {
439  EXPECT_TRUE(const_map_.end() == const_map_.cend());
440}
441
442TEST_F(MapImplTest, GetReferenceFromIterator) {
443  for (int i = 0; i < 10; i++) {
444    map_[i] = i;
445  }
446
447  for (Map<int32_t, int32_t>::const_iterator it = map_.cbegin();
448       it != map_.cend();) {
449    Map<int32_t, int32_t>::const_reference entry = *it++;
450    EXPECT_EQ(entry.first, entry.second);
451  }
452
453  for (Map<int32_t, int32_t>::const_iterator it = const_map_.begin();
454       it != const_map_.end();) {
455    Map<int32_t, int32_t>::const_reference entry = *it++;
456    EXPECT_EQ(entry.first, entry.second);
457  }
458
459  for (Map<int32_t, int32_t>::iterator it = map_.begin(); it != map_.end();) {
460    Map<int32_t, int32_t>::reference entry = *it++;
461    EXPECT_EQ(entry.first + 1, ++entry.second);
462  }
463}
464
465TEST_F(MapImplTest, IteratorBasic) {
466  map_[0] = 0;
467
468  // Default constructible (per forward iterator requirements).
469  Map<int, int>::const_iterator cit;
470  Map<int, int>::iterator it;
471
472  it = map_.begin();
473  cit = it;  // Converts to const_iterator
474
475  // Can compare between them.
476  EXPECT_TRUE(it == cit);
477  EXPECT_FALSE(cit != it);
478
479  // Pre increment.
480  EXPECT_FALSE(it == ++cit);
481
482  // Post increment.
483  EXPECT_FALSE(it++ == cit);
484  EXPECT_TRUE(it == cit);
485}
486
487// Arbitrary odd integers for creating test data.
488static int k0 = 812398771;
489static int k1 = 1312938717;
490static int k2 = 1321555333;
491
492// Finds inputs that will fall in the first few buckets for this particular map
493// (with the random seed it has) and this particular size.
494static std::vector<int> FindBadInputs(Map<int, int>& map, int num_inputs) {
495  // Make sure the seed and the size is set so that BucketNumber works.
496  while (map.size() < num_inputs) map[map.size()];
497  map.clear();
498
499  std::vector<int> out;
500
501  for (int i = 0; out.size() < num_inputs; ++i) {
502    if (MapTestPeer::BucketNumber(map, i) < 3) {
503      out.push_back(i);
504    }
505  }
506
507  // Reset the table to get it to grow from scratch again.
508  // The capacity will be lost, but we will get it again on insertion.
509  // It will also keep the seed.
510  map.clear();
511  MapTestPeer::Resize(map, 8);
512
513  return out;
514}
515
516TEST_F(MapImplTest, TreePathWorksAsExpected) {
517  const std::vector<int> s = FindBadInputs(map_, 1000);
518
519  for (int i : s) {
520    map_[i] = 0;
521  }
522  // Make sure we are testing what we think we are testing.
523  ASSERT_TRUE(MapTestPeer::HasTreeBuckets(map_));
524  for (int i : s) {
525    ASSERT_NE(map_.find(i), map_.end()) << i;
526  }
527  for (int i : s) {
528    ASSERT_EQ(1, map_.erase(i)) << i;
529  }
530  EXPECT_FALSE(MapTestPeer::HasTreeBuckets(map_));
531  EXPECT_TRUE(map_.empty());
532}
533
534
535TEST_F(MapImplTest, CopyIteratorStressTest) {
536  std::vector<Map<int32_t, int32_t>::iterator> v;
537  const int kIters = 1e5;
538  for (uint32_t i = 0; i < kIters; i++) {
539    int32_t key = (3 + i * (5 + i * (-8 + i * (62 + i)))) & 0x77777777;
540    map_[key] = i;
541    v.push_back(map_.find(key));
542  }
543  for (auto it = v.begin(); it != v.end(); it++) {
544    Map<int32_t, int32_t>::iterator i = *it;
545    ASSERT_EQ(i->first, (*it)->first);
546    ASSERT_EQ(i->second, (*it)->second);
547  }
548}
549
550template <typename T, typename U>
551static void TestValidityForAllKeysExcept(int key_to_avoid, const T& check_map,
552                                         const U& map) {
553  typedef typename U::value_type value_type;  // a key-value pair
554  for (typename U::const_iterator it = map.begin(); it != map.end(); ++it) {
555    const int key = it->first;
556    if (key == key_to_avoid) continue;
557    // All iterators relevant to this key, whether old (from check_map) or new,
558    // must point to the same memory.  So, test pointer equality here.
559    const value_type* check_val = &*check_map.find(key)->second;
560    EXPECT_EQ(check_val, &*it);
561    EXPECT_EQ(check_val, &*map.find(key));
562  }
563}
564
565// EXPECT i0 and i1 to be the same.
566template <typename Iter>
567static void TestEqualIterators(Iter i0, Iter i1, Iter end) {
568  EXPECT_EQ(i0 == end, i1 == end);
569  if (i0 == end) return;
570  EXPECT_EQ(&*i0, &*i1);
571}
572
573template <typename IteratorType>
574static void TestOldVersusNewIterator(int skip, Map<int, int>* m) {
575  const int initial_size = m->size();
576  IteratorType it = m->begin();
577  for (int i = 0; i < skip && it != m->end(); it++, i++) {
578  }
579  if (it == m->end()) return;
580  const IteratorType old = it;
581  ABSL_LOG(INFO) << "skip=" << skip << ", old->first=" << old->first;
582  const int target_size =
583      initial_size < 100 ? initial_size * 5 : initial_size * 5 / 4;
584  for (int i = 0; m->size() <= target_size; i++) {
585    (*m)[i] = 0;
586  }
587  // Iterator 'old' should still work just fine despite the growth of *m.
588  const IteratorType after_growth = m->find(old->first);
589  TestEqualIterators<IteratorType>(old, after_growth, m->end());
590
591  // Now shrink the number of elements.  Do this with a mix of erases and
592  // inserts to increase the chance that the hashtable will resize to a lower
593  // number of buckets.  (But, in any case, the test is still useful.)
594  for (int i = 0; i < 2 * (target_size - initial_size); i++) {
595    if (i != old->first) {
596      m->erase(i);
597    }
598    if (((i ^ m->begin()->first) & 15) == 0) {
599      (*m)[i * 342] = i;
600    }
601  }
602  // Now, the table has grown and shrunk; test again.
603  TestEqualIterators<IteratorType>(old, m->find(old->first), m->end());
604  TestEqualIterators<IteratorType>(old, after_growth, m->end());
605}
606
607// Create and test an n-element Map, with emphasis on iterator correctness.
608static void StressTestIterators(int n) {
609  ABSL_LOG(INFO) << "StressTestIterators " << n;
610  ABSL_CHECK_GT(n, 0);
611  // Create a random-looking map of size n.  Use non-negative integer keys.
612  Map<int, int> m;
613  uint32_t frog = 123987 + n;
614  int last_key = 0;
615  int counter = 0;
616  while (m.size() < n) {
617    frog *= static_cast<uint32_t>(k0);
618    frog ^= frog >> 17;
619    frog += counter++;
620    last_key =
621        static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1;
622    ABSL_DCHECK_GE(last_key, 0);
623    m[last_key] = last_key ^ 1;
624  }
625  // Test it.
626  ASSERT_EQ(n, m.size());
627  // Create maps of pointers and iterators.
628  // These should remain valid even if we modify m.
629  absl::flat_hash_map<int, Map<int, int>::value_type*> mp(n);
630  absl::flat_hash_map<int, Map<int, int>::iterator> mi(n);
631  for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
632    mp[it->first] = &*it;
633    mi[it->first] = it;
634  }
635  ASSERT_EQ(m.size(), mi.size());
636  ASSERT_EQ(m.size(), mp.size());
637  m.erase(last_key);
638  ASSERT_EQ(n - 1, m.size());
639  TestValidityForAllKeysExcept(last_key, mp, m);
640  TestValidityForAllKeysExcept(last_key, mi, m);
641
642  m[last_key] = 0;
643  ASSERT_EQ(n, m.size());
644  // Test old iterator vs new iterator, with table modification in between.
645  TestOldVersusNewIterator<Map<int, int>::const_iterator>(n % 3, &m);
646  TestOldVersusNewIterator<Map<int, int>::iterator>(n % (1 + (n / 40)), &m);
647  // Finally, ensure erase(iterator) doesn't reorder anything, because that is
648  // what its documentation says.
649  m[last_key] = m[last_key ^ 999] = 0;
650  std::vector<Map<int, int>::iterator> v;
651  v.reserve(m.size());
652  int position_of_last_key = 0;
653  for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) {
654    if (it->first == last_key) {
655      position_of_last_key = v.size();
656    }
657    v.push_back(it);
658  }
659  ASSERT_EQ(m.size(), v.size());
660  const auto erase_result = m.erase(m.find(last_key));
661  int index = 0;
662  for (auto it = m.begin(); it != m.end(); ++it, ++index) {
663    if (index == position_of_last_key) {
664      EXPECT_EQ(&*erase_result, &*v[++index]);
665    }
666    ASSERT_EQ(&*it, &*v[index]);
667  }
668}
669
670TEST_F(MapImplTest, IteratorInvalidation) {
671  // Create a set of pseudo-random sizes to test.
672#ifndef NDEBUG
673  const int kMaxSizeToTest = 100 * 1000;
674#else
675  const int kMaxSizeToTest = 1000 * 1000;
676#endif
677  absl::btree_set<int> s;
678  int n = kMaxSizeToTest;
679  unsigned int frog = k1 + n;
680  while (n > 1 && s.size() < 25) {
681    s.insert(n);
682    n = static_cast<int>(n * 100 / (101.0 + (frog & 63)));
683    frog *= k2;
684    frog ^= frog >> 17;
685  }
686  // Ensure we test a few small sizes.
687  s.insert(1);
688  s.insert(2);
689  s.insert(3);
690  // Now, the real work.
691  for (int i : s) {
692    StressTestIterators(i);
693  }
694}
695
696// Test that erase() revalidates iterators.
697TEST_F(MapImplTest, EraseRevalidates) {
698  map_[3] = map_[13] = map_[20] = 0;
699  const int initial_size = map_.size();
700  EXPECT_EQ(3, initial_size);
701  std::vector<Map<int, int>::iterator> v;
702  for (Map<int, int>::iterator it = map_.begin(); it != map_.end(); ++it) {
703    v.push_back(it);
704  }
705  EXPECT_EQ(initial_size, v.size());
706  for (int i = 0; map_.size() <= initial_size * 20; i++) {
707    map_[i] = 0;
708  }
709  const int larger_size = map_.size();
710  // We've greatly increased the size of the map, so it is highly likely that
711  // the following will corrupt m if erase() doesn't properly revalidate
712  // iterators passed to it.  Finishing this routine without crashing indicates
713  // success.
714  for (int i = 0; i < v.size(); i++) {
715    map_.erase(v[i]);
716  }
717  EXPECT_EQ(larger_size - v.size(), map_.size());
718}
719
720template <typename T>
721bool IsConstHelper(T& /*t*/) {  // NOLINT. We want to catch non-const refs here.
722  return false;
723}
724template <typename T>
725bool IsConstHelper(const T& /*t*/) {
726  return true;
727}
728
729TEST_F(MapImplTest, IteratorConstness) {
730  map_[0] = 0;
731  EXPECT_TRUE(IsConstHelper(*map_.cbegin()));
732  EXPECT_TRUE(IsConstHelper(*const_map_.begin()));
733  EXPECT_FALSE(IsConstHelper(*map_.begin()));
734}
735
736bool IsForwardIteratorHelper(std::forward_iterator_tag /*tag*/) { return true; }
737
738TEST_F(MapImplTest, IteratorCategory) {
739  EXPECT_TRUE(IsForwardIteratorHelper(
740      std::iterator_traits<Map<int, int>::iterator>::iterator_category()));
741  EXPECT_TRUE(IsForwardIteratorHelper(
742      std::iterator_traits<
743          Map<int, int>::const_iterator>::iterator_category()));
744}
745
746TEST_F(MapImplTest, InsertSingleLValue) {
747  int32_t key = 0;
748  int32_t value1 = 100;
749  int32_t value2 = 101;
750
751  // Insert a non-existing key.
752  Map<int32_t, int32_t>::value_type v1(key, value1);
753  std::pair<Map<int32_t, int32_t>::iterator, bool> result1 = map_.insert(v1);
754  ExpectSingleElement(key, value1);
755
756  Map<int32_t, int32_t>::iterator it1 = result1.first;
757  EXPECT_EQ(key, it1->first);
758  EXPECT_EQ(value1, it1->second);
759  EXPECT_TRUE(result1.second);
760
761  // Insert an existing key.
762  Map<int32_t, int32_t>::value_type v2(key, value2);
763  std::pair<Map<int32_t, int32_t>::iterator, bool> result2 = map_.insert(v2);
764  ExpectSingleElement(key, value1);
765
766  Map<int32_t, int32_t>::iterator it2 = result2.first;
767  EXPECT_TRUE(it1 == it2);
768  EXPECT_FALSE(result2.second);
769}
770
771TEST_F(MapImplTest, InsertSingleRValue) {
772  int32_t key = 0;
773  int32_t value1 = 100;
774  int32_t value2 = 101;
775
776  // Insert a non-existing key.
777  std::pair<Map<int32_t, int32_t>::iterator, bool> result1 =
778      map_.insert(Map<int32_t, int32_t>::value_type(key, value1));
779  ExpectSingleElement(key, value1);
780
781  Map<int32_t, int32_t>::iterator it1 = result1.first;
782  EXPECT_EQ(key, it1->first);
783  EXPECT_EQ(value1, it1->second);
784  EXPECT_TRUE(result1.second);
785
786  // Insert an existing key.
787  std::pair<Map<int32_t, int32_t>::iterator, bool> result2 =
788      map_.insert(Map<int32_t, int32_t>::value_type(key, value2));
789  ExpectSingleElement(key, value1);
790
791  Map<int32_t, int32_t>::iterator it2 = result2.first;
792  EXPECT_TRUE(it1 == it2);
793  EXPECT_FALSE(result2.second);
794}
795
796TEST_F(MapImplTest, InsertSingleBraceInitList) {
797  int32_t key = 0;
798  int32_t value1 = 100;
799  int32_t value2 = 101;
800
801  // Insert a non-existing key.
802  auto result1 = map_.insert({key, value1});
803  ExpectSingleElement(key, value1);
804
805  auto it1 = result1.first;
806  EXPECT_EQ(key, it1->first);
807  EXPECT_EQ(value1, it1->second);
808  EXPECT_TRUE(result1.second);
809
810  // Insert an existing key.
811  auto result2 = map_.insert({key, value2});
812  ExpectSingleElement(key, value1);
813
814  auto it2 = result2.first;
815  EXPECT_TRUE(it1 == it2);
816  EXPECT_FALSE(result2.second);
817}
818
819TEST_F(MapImplTest, InsertSingleBraceInitListTypeMismatch) {
820  int32_t key = 0;
821  int32_t value1 = 100;
822  int32_t value2 = 101;
823  Map<int64_t, int64_t> m;
824
825  // Insert a non-existing key.
826  auto result1 = m.insert({key, value1});
827  EXPECT_TRUE(result1.second);
828
829  // Insert an existing key.
830  auto result2 = m.insert({key, value2});
831  EXPECT_FALSE(result2.second);
832
833  EXPECT_TRUE(result1.first == result2.first);
834}
835
836TEST_F(MapImplTest, TryEmplace) {
837  using ::testing::Pair;
838  using ::testing::UnorderedElementsAre;
839
840  Map<int32_t, std::string> m;
841
842  m.try_emplace(1, "one");
843  EXPECT_EQ(m.size(), 1);
844
845  const int32_t key = 42;
846  m.try_emplace(key, 3, 'a');
847  m.try_emplace(2, std::string("two"));
848  EXPECT_THAT(
849      m, UnorderedElementsAre(Pair(1, "one"), Pair(2, "two"), Pair(42, "aaa")));
850}
851
852TEST_F(MapImplTest, Emplace) {
853  using ::testing::Pair;
854  using ::testing::UnorderedElementsAre;
855
856  Map<int32_t, std::string> m;
857
858  m.emplace(1, "one");
859  EXPECT_EQ(m.size(), 1);
860
861  const int32_t key = 42;
862  m.emplace(key, "aaa");
863  m.emplace(2, std::string("two"));
864  EXPECT_THAT(
865      m, UnorderedElementsAre(Pair(1, "one"), Pair(2, "two"), Pair(42, "aaa")));
866}
867
868TEST_F(MapImplTest, ValueTypeHasMoveConstructor) {
869  using vt = typename Map<ConstructorTag, ConstructorTag>::value_type;
870  ConstructorTag l, r;
871
872  vt pair(l, std::move(r));
873
874  EXPECT_EQ(pair.first.invoked_constructor, ConstructorType::kCopy);
875  EXPECT_EQ(pair.second.invoked_constructor, ConstructorType::kMove);
876}
877
878TEST_F(MapImplTest, TryEmplaceExisting) {
879  Map<int32_t, CountedInstance> m;
880
881  m.try_emplace(1, 1);
882  ASSERT_EQ(m.size(), 1);
883
884  CountedInstance::num_created = 0;
885  CountedInstance::num_assigned = 0;
886  m.try_emplace(1, 123);
887  EXPECT_EQ(m.size(), 1);
888  EXPECT_EQ(CountedInstance::num_created, 0);
889  EXPECT_EQ(CountedInstance::num_assigned, 0);
890}
891
892TEST_F(MapImplTest, TryEmplaceArenaConstructible) {
893  ASSERT_TRUE(Arena::is_arena_constructable<ArenaConstructible>::value);
894
895  ArenaConstructible v1, v2;
896
897  Map<int32_t, ArenaConstructible> m;
898
899  // "default" construction
900  CountedInstance::num_created = 0;
901  CountedInstance::num_assigned = 0;
902  m.try_emplace(1);
903  EXPECT_EQ(m.size(), 1);
904  EXPECT_EQ(CountedInstance::num_created, 1);
905  EXPECT_EQ(CountedInstance::num_assigned, 0);
906
907  // "default" construction + copy assignment
908  CountedInstance::num_created = 0;
909  CountedInstance::num_assigned = 0;
910  m.try_emplace(2, v1);
911  EXPECT_EQ(m.size(), 2);
912  EXPECT_EQ(CountedInstance::num_created, 1);
913  EXPECT_EQ(CountedInstance::num_assigned, 1);
914
915  // "default" construction + move assignment
916  CountedInstance::num_created = 0;
917  CountedInstance::num_assigned = 0;
918  m.try_emplace(3, std::move(v2));
919  EXPECT_EQ(m.size(), 3);
920  EXPECT_EQ(CountedInstance::num_created, 1);
921  EXPECT_EQ(CountedInstance::num_assigned, 1);
922
923  // "default" construction + in-place temporary + move assignment
924  CountedInstance::num_created = 0;
925  CountedInstance::num_assigned = 0;
926  m.try_emplace(4, 239);
927  EXPECT_EQ(m.size(), 4);
928  EXPECT_EQ(CountedInstance::num_created, 2);
929  EXPECT_EQ(CountedInstance::num_assigned, 1);
930}
931
932TEST_F(MapImplTest, TryEmplaceExistingArenaConstructible) {
933  ASSERT_TRUE(Arena::is_arena_constructable<ArenaConstructible>::value);
934
935  Map<int32_t, ArenaConstructible> m;
936
937  m.try_emplace(1, 1);
938  ASSERT_EQ(m.size(), 1);
939
940  CountedInstance::num_created = 0;
941  CountedInstance::num_assigned = 0;
942  m.try_emplace(1, 123);
943  EXPECT_EQ(m.size(), 1);
944  EXPECT_EQ(CountedInstance::num_created, 0);
945  EXPECT_EQ(CountedInstance::num_assigned, 0);
946}
947
948TEST_F(MapImplTest, EmplaceSingle) {
949  int32_t key = 0;
950  int32_t value1 = 100;
951  int32_t value2 = 101;
952
953  // Emplace a non-existing key.
954  auto result1 = map_.emplace(key, value1);
955  ExpectSingleElement(key, value1);
956
957  Map<int32_t, int32_t>::iterator it1 = result1.first;
958  EXPECT_EQ(key, it1->first);
959  EXPECT_EQ(value1, it1->second);
960  EXPECT_TRUE(result1.second);
961
962  // Emplace an existing key.
963  auto result2 = map_.emplace(key, value2);
964  ExpectSingleElement(key, value1);
965
966  Map<int32_t, int32_t>::iterator it2 = result2.first;
967  EXPECT_TRUE(it1 == it2);
968  EXPECT_FALSE(result2.second);
969}
970
971TEST_F(MapImplTest, InsertByIterator) {
972  int32_t key1 = 0;
973  int32_t key2 = 1;
974  int32_t value1a = 100;
975  int32_t value1b = 101;
976  int32_t value2a = 200;
977  int32_t value2b = 201;
978
979  absl::flat_hash_map<int32_t, int32_t> map1;
980  map1[key1] = value1a;
981  map1[key2] = value2a;
982
983  map_.insert(map1.begin(), map1.end());
984  ExpectElements(map1);
985
986  absl::flat_hash_map<int32_t, int32_t> map2;
987  map2[key1] = value1b;
988  map2[key2] = value2b;
989
990  map_.insert(map2.begin(), map2.end());
991  ExpectElements(map1);
992}
993
994TEST_F(MapImplTest, InsertByInitializerList) {
995  map_.insert({{1, 100}, {2, 200}});
996  ExpectElements({{1, 100}, {2, 200}});
997
998  map_.insert({{2, 201}, {3, 301}});
999  ExpectElements({{1, 100}, {2, 200}, {3, 301}});
1000}
1001
1002TEST_F(MapImplTest, EraseSingleByKey) {
1003  int32_t key = 0;
1004  int32_t value = 100;
1005
1006  map_[key] = value;
1007  ExpectSingleElement(key, value);
1008
1009  // Erase an existing key.
1010  EXPECT_EQ(1, map_.erase(key));
1011  EXPECT_TRUE(map_.empty());
1012  EXPECT_EQ(0, map_.size());
1013  EXPECT_TRUE(map_.end() == map_.find(key));
1014  EXPECT_TRUE(map_.begin() == map_.end());
1015
1016  // Erase a non-existing key.
1017  EXPECT_EQ(0, map_.erase(key));
1018}
1019
1020TEST_F(MapImplTest, EraseMultipleByKey) {
1021  // erase in one specific order to trigger corner cases
1022  for (int i = 0; i < 5; i++) {
1023    map_[i] = i;
1024  }
1025
1026  map_.erase(0);
1027  EXPECT_EQ(4, map_.size());
1028  EXPECT_TRUE(map_.end() == map_.find(0));
1029
1030  map_.erase(1);
1031  EXPECT_EQ(3, map_.size());
1032  EXPECT_TRUE(map_.end() == map_.find(1));
1033
1034  map_.erase(3);
1035  EXPECT_EQ(2, map_.size());
1036  EXPECT_TRUE(map_.end() == map_.find(3));
1037
1038  map_.erase(4);
1039  EXPECT_EQ(1, map_.size());
1040  EXPECT_TRUE(map_.end() == map_.find(4));
1041
1042  map_.erase(2);
1043  EXPECT_EQ(0, map_.size());
1044  EXPECT_TRUE(map_.end() == map_.find(2));
1045}
1046
1047TEST_F(MapImplTest, EraseSingleByIterator) {
1048  int32_t key = 0;
1049  int32_t value = 100;
1050
1051  map_[key] = value;
1052  ExpectSingleElement(key, value);
1053
1054  Map<int32_t, int32_t>::iterator it = map_.find(key);
1055  map_.erase(it);
1056  EXPECT_TRUE(map_.empty());
1057  EXPECT_EQ(0, map_.size());
1058  EXPECT_TRUE(map_.end() == map_.find(key));
1059  EXPECT_TRUE(map_.begin() == map_.end());
1060}
1061
1062TEST_F(MapImplTest, ValidIteratorAfterErase) {
1063  for (int i = 0; i < 10; i++) {
1064    map_[i] = i;
1065  }
1066
1067  int count = 0;
1068
1069  for (Map<int32_t, int32_t>::iterator it = map_.begin(); it != map_.end();) {
1070    count++;
1071    if (it->first % 2 == 1) {
1072      map_.erase(it++);
1073    } else {
1074      ++it;
1075    }
1076  }
1077
1078  EXPECT_EQ(10, count);
1079  EXPECT_EQ(5, map_.size());
1080}
1081
1082TEST_F(MapImplTest, EraseByIterator) {
1083  int32_t key1 = 0;
1084  int32_t key2 = 1;
1085  int32_t value1 = 100;
1086  int32_t value2 = 101;
1087
1088  absl::flat_hash_map<int32_t, int32_t> map;
1089  map[key1] = value1;
1090  map[key2] = value2;
1091
1092  map_.insert(map.begin(), map.end());
1093  ExpectElements(map);
1094
1095  map_.erase(map_.begin(), map_.end());
1096  EXPECT_TRUE(map_.empty());
1097  EXPECT_EQ(0, map_.size());
1098  EXPECT_TRUE(map_.end() == map_.find(key1));
1099  EXPECT_TRUE(map_.end() == map_.find(key2));
1100  EXPECT_TRUE(map_.begin() == map_.end());
1101}
1102
1103TEST_F(MapImplTest, Clear) {
1104  int32_t key = 0;
1105  int32_t value = 100;
1106
1107  map_[key] = value;
1108  ExpectSingleElement(key, value);
1109
1110  map_.clear();
1111
1112  EXPECT_TRUE(map_.empty());
1113  EXPECT_EQ(0, map_.size());
1114  EXPECT_TRUE(map_.end() == map_.find(key));
1115  EXPECT_TRUE(map_.begin() == map_.end());
1116}
1117
1118TEST_F(MapImplTest, IterConstructor) {
1119  int32_t key1 = 0;
1120  int32_t key2 = 1;
1121  int32_t value1 = 100;
1122  int32_t value2 = 101;
1123
1124  absl::flat_hash_map<int32_t, int32_t> map;
1125  map[key1] = value1;
1126  map[key2] = value2;
1127
1128  Map<int32_t, int32_t> new_map(map.begin(), map.end());
1129
1130  EXPECT_EQ(2, new_map.size());
1131  EXPECT_EQ(value1, new_map.at(key1));
1132  EXPECT_EQ(value2, new_map.at(key2));
1133}
1134
1135TEST_F(MapImplTest, Assigner) {
1136  int32_t key1 = 0;
1137  int32_t key2 = 1;
1138  int32_t value1 = 100;
1139  int32_t value2 = 101;
1140
1141  absl::flat_hash_map<int32_t, int32_t> map;
1142  map[key1] = value1;
1143  map[key2] = value2;
1144
1145  map_.insert(map.begin(), map.end());
1146
1147  Map<int32_t, int32_t> other;
1148  int32_t key_other = 123;
1149  int32_t value_other = 321;
1150  other[key_other] = value_other;
1151  EXPECT_EQ(1, other.size());
1152
1153  other = map_;
1154
1155  EXPECT_EQ(2, other.size());
1156  EXPECT_EQ(value1, other.at(key1));
1157  EXPECT_EQ(value2, other.at(key2));
1158  EXPECT_TRUE(other.find(key_other) == other.end());
1159
1160  // Self assign
1161  other = *&other;  // Avoid -Wself-assign.
1162  EXPECT_EQ(2, other.size());
1163  EXPECT_EQ(value1, other.at(key1));
1164  EXPECT_EQ(value2, other.at(key2));
1165}
1166
1167TEST_F(MapImplTest, Rehash) {
1168  const int test_size = 50;
1169  absl::flat_hash_map<int32_t, int32_t> reference_map;
1170  for (int i = 0; i < test_size; i++) {
1171    reference_map[i] = i;
1172  }
1173  for (int i = 0; i < test_size; i++) {
1174    map_[i] = reference_map[i];
1175    EXPECT_EQ(reference_map[i], map_[i]);
1176  }
1177  for (int i = 0; i < test_size; i++) {
1178    map_.erase(i);
1179    EXPECT_TRUE(map_.end() == map_.find(i));
1180  }
1181  EXPECT_TRUE(map_.empty());
1182}
1183
1184TEST_F(MapImplTest, EqualRange) {
1185  int key = 100, key_missing = 101;
1186  map_[key] = 100;
1187
1188  std::pair<Map<int32_t, int32_t>::iterator, Map<int32_t, int32_t>::iterator>
1189      range = map_.equal_range(key);
1190  EXPECT_TRUE(map_.find(key) == range.first);
1191  EXPECT_TRUE(++map_.find(key) == range.second);
1192
1193  range = map_.equal_range(key_missing);
1194  EXPECT_TRUE(map_.end() == range.first);
1195  EXPECT_TRUE(map_.end() == range.second);
1196
1197  std::pair<Map<int32_t, int32_t>::const_iterator,
1198            Map<int32_t, int32_t>::const_iterator>
1199      const_range = const_map_.equal_range(key);
1200  EXPECT_TRUE(const_map_.find(key) == const_range.first);
1201  EXPECT_TRUE(++const_map_.find(key) == const_range.second);
1202
1203  const_range = const_map_.equal_range(key_missing);
1204  EXPECT_TRUE(const_map_.end() == const_range.first);
1205  EXPECT_TRUE(const_map_.end() == const_range.second);
1206}
1207
1208TEST_F(MapImplTest, ConvertToStdMap) {
1209  map_[100] = 101;
1210  absl::flat_hash_map<int32_t, int32_t> std_map(map_.begin(), map_.end());
1211  EXPECT_EQ(1, std_map.size());
1212  EXPECT_EQ(101, std_map[100]);
1213}
1214
1215TEST_F(MapImplTest, ConvertToStdVectorOfPairs) {
1216  map_[100] = 101;
1217  std::vector<std::pair<int32_t, int32_t>> std_vec(map_.begin(), map_.end());
1218  EXPECT_EQ(1, std_vec.size());
1219  EXPECT_EQ(100, std_vec[0].first);
1220  EXPECT_EQ(101, std_vec[0].second);
1221}
1222
1223TEST_F(MapImplTest, SwapBasic) {
1224  Map<int32_t, int32_t> another;
1225  map_[9398] = 41999;
1226  another[9398] = 41999;
1227  another[8070] = 42056;
1228  another.swap(map_);
1229  EXPECT_THAT(another,
1230              testing::UnorderedElementsAre(testing::Pair(9398, 41999)));
1231  EXPECT_THAT(map_, testing::UnorderedElementsAre(testing::Pair(8070, 42056),
1232                                                  testing::Pair(9398, 41999)));
1233}
1234
1235TEST_F(MapImplTest, SwapArena) {
1236  Arena arena1, arena2;
1237  Map<int32_t, int32_t> m1(&arena1);
1238  Map<int32_t, int32_t> m2(&arena2);
1239  map_[9398] = 41999;
1240  m1[9398] = 41999;
1241  m1[8070] = 42056;
1242  m2[10244] = 10247;
1243  m2[8070] = 42056;
1244  m1.swap(map_);
1245  EXPECT_THAT(m1, testing::UnorderedElementsAre(testing::Pair(9398, 41999)));
1246  EXPECT_THAT(map_, testing::UnorderedElementsAre(testing::Pair(8070, 42056),
1247                                                  testing::Pair(9398, 41999)));
1248  m2.swap(m1);
1249  EXPECT_THAT(m1, testing::UnorderedElementsAre(testing::Pair(8070, 42056),
1250                                                testing::Pair(10244, 10247)));
1251  EXPECT_THAT(m2, testing::UnorderedElementsAre(testing::Pair(9398, 41999)));
1252}
1253
1254TEST_F(MapImplTest, SwapFieldArenaReflection) {
1255  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
1256
1257  {
1258    // Tests filled lfs and empty rhs.
1259    TestMap rhs;
1260
1261    {
1262      // Use local_arena to allocate lhs to trigger use-after-free error.
1263      Arena local_arena;
1264      auto* lhs = Arena::Create<TestMap>(&local_arena);
1265      const auto* reflection = lhs->GetReflection();
1266      std::vector<const FieldDescriptor*> fields;
1267
1268      reflection_tester.SetMapFieldsViaReflection(lhs);
1269      reflection->ListFields(*lhs, &fields);
1270
1271      reflection->SwapFields(lhs, &rhs, fields);
1272
1273      reflection_tester.ExpectClearViaReflection(*lhs);
1274
1275      // Add an entry to make sure it is using the right arena.
1276      (*lhs->mutable_map_int32_int32())[1234] = 1234;
1277    }
1278
1279    reflection_tester.ExpectMapFieldsSetViaReflection(rhs);
1280
1281      // Add an entry to make sure it is using the right arena.
1282    (*rhs.mutable_map_int32_int32())[1234] = 1234;
1283  }
1284}
1285
1286TEST_F(MapImplTest, CopyAssignMapIterator) {
1287  TestMap message;
1288  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
1289  reflection_tester.SetMapFieldsViaMapReflection(&message);
1290  MapIterator it1 = reflection_tester.MapBegin(&message, "map_int32_int32");
1291  MapIterator it2 = reflection_tester.MapEnd(&message, "map_int32_int32");
1292  it2 = it1;
1293  EXPECT_EQ(it1.GetKey().GetInt32Value(), it2.GetKey().GetInt32Value());
1294}
1295
1296TEST_F(MapImplTest, SpaceUsed) {
1297  constexpr size_t kMinCap = 16 / sizeof(void*);
1298
1299  Map<int32_t, int32_t> m;
1300  // An newly constructed map should have no space used.
1301  EXPECT_EQ(m.SpaceUsedExcludingSelfLong(), 0);
1302
1303  struct IntIntNode : internal::NodeBase {
1304    std::pair<int32_t, int32_t> kv;
1305  };
1306
1307  for (int i = 0; i < 100; ++i) {
1308    m[i];
1309    EXPECT_EQ(m.SpaceUsedExcludingSelfLong(),
1310              sizeof(void*) * MapTestPeer::NumBuckets(m) +
1311                  m.size() * sizeof(IntIntNode));
1312  }
1313
1314  // Test string, and non-scalar keys.
1315  Map<std::string, int32_t> m2;
1316  std::string str = "Some arbitrarily large string";
1317  m2[str] = 1;
1318
1319  struct StringIntNode : internal::NodeBase {
1320    std::pair<std::string, int32_t> kv;
1321  };
1322
1323  EXPECT_EQ(m2.SpaceUsedExcludingSelfLong(),
1324            sizeof(void*) * kMinCap + sizeof(StringIntNode) +
1325                internal::StringSpaceUsedExcludingSelfLong(str));
1326
1327  struct IntAllTypesNode : internal::NodeBase {
1328    std::pair<int32_t, TestAllTypes> kv;
1329  };
1330
1331  // Test messages, and non-scalar values.
1332  Map<int32_t, TestAllTypes> m3;
1333  m3[0].set_optional_string(str);
1334  EXPECT_EQ(m3.SpaceUsedExcludingSelfLong(),
1335            sizeof(void*) * kMinCap + sizeof(IntAllTypesNode) +
1336                m3[0].SpaceUsedLong() - sizeof(m3[0]));
1337}
1338
1339// Attempts to verify that a map with keys a and b has a random ordering. This
1340// function returns true if it succeeds in observing both possible orderings.
1341bool MapOrderingIsRandom(int a, int b) {
1342  bool saw_a_first = false;
1343  bool saw_b_first = false;
1344  std::vector<Map<int32_t, int32_t>> v(50);
1345  for (int i = 0; i < 50; ++i) {
1346    Map<int32_t, int32_t>& m = v[i];
1347    m[a] = 0;
1348    m[b] = 0;
1349    int32_t first_element = m.begin()->first;
1350    if (first_element == a) saw_a_first = true;
1351    if (first_element == b) saw_b_first = true;
1352    if (saw_a_first && saw_b_first) {
1353      return true;
1354    }
1355  }
1356  return false;
1357}
1358
1359// This test verifies that the iteration order is reasonably random even for
1360// small maps.
1361TEST_F(MapImplTest, RandomOrdering) {
1362  for (int i = 0; i < 10; ++i) {
1363    for (int j = i + 1; j < 10; ++j) {
1364      EXPECT_TRUE(MapOrderingIsRandom(i, j))
1365          << "Map with keys " << i << " and " << j
1366          << " has deterministic ordering";
1367    }
1368  }
1369}
1370
1371template <typename Key>
1372void TestTransparent(const Key& key, const Key& miss_key) {
1373  Map<std::string, int> m;
1374  const auto& cm = m;
1375
1376  m.insert({"ABC", 1});
1377  m.insert({"DEF", 2});
1378
1379  const auto abc_it = m.find("ABC");
1380
1381  using testing::Pair;
1382  using testing::UnorderedElementsAre;
1383
1384  EXPECT_EQ(m.at(key), 1);
1385  EXPECT_EQ(cm.at(key), 1);
1386
1387#if GTEST_HAS_DEATH_TEST
1388  EXPECT_DEATH(m.at(miss_key), "");
1389  EXPECT_DEATH(cm.at(miss_key), "");
1390#endif  // GTEST_HAS_DEATH_TEST
1391
1392  EXPECT_EQ(m.count(key), 1);
1393  EXPECT_EQ(cm.count(key), 1);
1394  EXPECT_EQ(m.count(miss_key), 0);
1395  EXPECT_EQ(cm.count(miss_key), 0);
1396
1397  EXPECT_EQ(m.find(key), abc_it);
1398  EXPECT_EQ(cm.find(key), abc_it);
1399  EXPECT_EQ(m.find(miss_key), m.end());
1400  EXPECT_EQ(cm.find(miss_key), cm.end());
1401
1402  EXPECT_TRUE(m.contains(key));
1403  EXPECT_TRUE(cm.contains(key));
1404  EXPECT_FALSE(m.contains(miss_key));
1405  EXPECT_FALSE(cm.contains(miss_key));
1406
1407  EXPECT_THAT(m.equal_range(key), Pair(abc_it, std::next(abc_it)));
1408  EXPECT_THAT(cm.equal_range(key), Pair(abc_it, std::next(abc_it)));
1409  EXPECT_THAT(m.equal_range(miss_key), Pair(m.end(), m.end()));
1410  EXPECT_THAT(cm.equal_range(miss_key), Pair(m.end(), m.end()));
1411
1412  EXPECT_THAT(m, UnorderedElementsAre(Pair("ABC", 1), Pair("DEF", 2)));
1413  EXPECT_EQ(m.erase(key), 1);
1414  EXPECT_THAT(m, UnorderedElementsAre(Pair("DEF", 2)));
1415  EXPECT_EQ(m.erase(key), 0);
1416  EXPECT_EQ(m.erase(miss_key), 0);
1417  EXPECT_THAT(m, UnorderedElementsAre(Pair("DEF", 2)));
1418
1419  m[key];
1420  EXPECT_THAT(m, UnorderedElementsAre(Pair("ABC", 0), Pair("DEF", 2)));
1421  m[key] = 1;
1422  EXPECT_THAT(m, UnorderedElementsAre(Pair("ABC", 1), Pair("DEF", 2)));
1423}
1424
1425// Emulate a non-Abseil string_view (e.g. STL when Abseil's alias is disabled).
1426class CustomStringView {
1427  public:
1428    CustomStringView(absl::string_view str) : str_(std::string(str)) {}
1429
1430    const char* data() const { return str_.data(); }
1431    size_t size() const { return str_.size(); }
1432
1433    bool operator==(const CustomStringView& other) const {
1434      return other.str_ == str_;
1435    }
1436    bool operator==(absl::string_view other) const { return other == str_; }
1437    explicit operator std::string() const { return str_; }
1438    friend std::ostream& operator<<(std::ostream& out, const CustomStringView& view) {
1439      return out << view.str_;
1440    }
1441
1442  private:
1443    std::string str_;
1444};
1445
1446TEST_F(MapImplTest, TransparentLookupForString) {
1447  TestTransparent("ABC", "LKJ");
1448  TestTransparent(std::string("ABC"), std::string("LKJ"));
1449  TestTransparent(absl::string_view("ABC"), absl::string_view("LKJ"));
1450  TestTransparent(CustomStringView("ABC"), CustomStringView("LKJ"));
1451
1452  // std::reference_wrapper
1453  std::string abc = "ABC", lkj = "LKJ";
1454  TestTransparent(std::ref(abc), std::ref(lkj));
1455  TestTransparent(std::cref(abc), std::cref(lkj));
1456}
1457
1458TEST_F(MapImplTest, ConstInit) {
1459  PROTOBUF_CONSTINIT static Map<int, int> map;  // NOLINT
1460  EXPECT_TRUE(map.empty());
1461}
1462
1463// Map Field Reflection Test ========================================
1464
1465static int Func(int i, int j) { return i * j; }
1466
1467static std::string StrFunc(int i, int j) { return absl::StrCat(Func(i, j)); }
1468
1469static int Int(const std::string& value) {
1470  int result = 0;
1471  std::istringstream(value) >> result;
1472  return result;
1473}
1474
1475}  // namespace
1476
1477// This class is a friend, so no anonymous namespace.
1478class MapFieldReflectionTest : public testing::Test {
1479 protected:
1480  typedef FieldDescriptor FD;
1481
1482  int MapSize(const Reflection* reflection, const FieldDescriptor* field,
1483              const Message& message) {
1484    return reflection->MapSize(message, field);
1485  }
1486};
1487
1488namespace {
1489
1490TEST_F(MapFieldReflectionTest, RegularFields) {
1491  TestMap message;
1492  const Reflection* refl = message.GetReflection();
1493  const Descriptor* desc = message.GetDescriptor();
1494
1495  Map<int32_t, int32_t>* map_int32_int32 = message.mutable_map_int32_int32();
1496  Map<int32_t, double>* map_int32_double = message.mutable_map_int32_double();
1497  Map<std::string, std::string>* map_string_string =
1498      message.mutable_map_string_string();
1499  Map<int32_t, ForeignMessage>* map_int32_foreign_message =
1500      message.mutable_map_int32_foreign_message();
1501
1502  for (int i = 0; i < 10; ++i) {
1503    (*map_int32_int32)[i] = Func(i, 1);
1504    (*map_int32_double)[i] = Func(i, 2);
1505    (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
1506    (*map_int32_foreign_message)[i].set_c(Func(i, 6));
1507  }
1508
1509  // Get FieldDescriptors for all the fields of interest.
1510  const FieldDescriptor* fd_map_int32_int32 =
1511      desc->FindFieldByName("map_int32_int32");
1512  const FieldDescriptor* fd_map_int32_double =
1513      desc->FindFieldByName("map_int32_double");
1514  const FieldDescriptor* fd_map_string_string =
1515      desc->FindFieldByName("map_string_string");
1516  const FieldDescriptor* fd_map_int32_foreign_message =
1517      desc->FindFieldByName("map_int32_foreign_message");
1518
1519  const FieldDescriptor* fd_map_int32_in32_key =
1520      fd_map_int32_int32->message_type()->map_key();
1521  const FieldDescriptor* fd_map_int32_in32_value =
1522      fd_map_int32_int32->message_type()->map_value();
1523  const FieldDescriptor* fd_map_int32_double_key =
1524      fd_map_int32_double->message_type()->map_key();
1525  const FieldDescriptor* fd_map_int32_double_value =
1526      fd_map_int32_double->message_type()->map_value();
1527  const FieldDescriptor* fd_map_string_string_key =
1528      fd_map_string_string->message_type()->map_key();
1529  const FieldDescriptor* fd_map_string_string_value =
1530      fd_map_string_string->message_type()->map_value();
1531  const FieldDescriptor* fd_map_int32_foreign_message_key =
1532      fd_map_int32_foreign_message->message_type()->map_key();
1533  const FieldDescriptor* fd_map_int32_foreign_message_value =
1534      fd_map_int32_foreign_message->message_type()->map_value();
1535
1536  // Get RepeatedPtrField objects for all fields of interest.
1537  const RepeatedPtrField<Message>& mf_int32_int32 =
1538      refl->GetRepeatedPtrField<Message>(message, fd_map_int32_int32);
1539  const RepeatedPtrField<Message>& mf_int32_double =
1540      refl->GetRepeatedPtrField<Message>(message, fd_map_int32_double);
1541  const RepeatedPtrField<Message>& mf_string_string =
1542      refl->GetRepeatedPtrField<Message>(message, fd_map_string_string);
1543  const RepeatedPtrField<Message>& mf_int32_foreign_message =
1544      refl->GetRepeatedPtrField<Message>(message, fd_map_int32_foreign_message);
1545
1546  // Get mutable RepeatedPtrField objects for all fields of interest.
1547  RepeatedPtrField<Message>* mmf_int32_int32 =
1548      refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_int32);
1549  RepeatedPtrField<Message>* mmf_int32_double =
1550      refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_double);
1551  RepeatedPtrField<Message>* mmf_string_string =
1552      refl->MutableRepeatedPtrField<Message>(&message, fd_map_string_string);
1553  RepeatedPtrField<Message>* mmf_int32_foreign_message =
1554      refl->MutableRepeatedPtrField<Message>(&message,
1555                                             fd_map_int32_foreign_message);
1556
1557  // Make sure we can do gets through the RepeatedPtrField objects.
1558  for (int i = 0; i < 10; ++i) {
1559    {
1560      // Check gets through const objects.
1561      const Message& message_int32_int32 = mf_int32_int32.Get(i);
1562      int32_t key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1563          message_int32_int32, fd_map_int32_in32_key);
1564      int32_t value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1565          message_int32_int32, fd_map_int32_in32_value);
1566      EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1567
1568      const Message& message_int32_double = mf_int32_double.Get(i);
1569      int32_t key_int32_double = message_int32_double.GetReflection()->GetInt32(
1570          message_int32_double, fd_map_int32_double_key);
1571      double value_int32_double =
1572          message_int32_double.GetReflection()->GetDouble(
1573              message_int32_double, fd_map_int32_double_value);
1574      EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1575
1576      const Message& message_string_string = mf_string_string.Get(i);
1577      std::string key_string_string =
1578          message_string_string.GetReflection()->GetString(
1579              message_string_string, fd_map_string_string_key);
1580      std::string value_string_string =
1581          message_string_string.GetReflection()->GetString(
1582              message_string_string, fd_map_string_string_value);
1583      EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1584
1585      const Message& message_int32_message = mf_int32_foreign_message.Get(i);
1586      int32_t key_int32_message =
1587          message_int32_message.GetReflection()->GetInt32(
1588              message_int32_message, fd_map_int32_foreign_message_key);
1589      const ForeignMessage& value_int32_message =
1590          DownCastMessage<ForeignMessage>(
1591              message_int32_message.GetReflection()->GetMessage(
1592                  message_int32_message, fd_map_int32_foreign_message_value));
1593      EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1594    }
1595
1596    {
1597      // Check gets through mutable objects.
1598      const Message& message_int32_int32 = mmf_int32_int32->Get(i);
1599      int32_t key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1600          message_int32_int32, fd_map_int32_in32_key);
1601      int32_t value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1602          message_int32_int32, fd_map_int32_in32_value);
1603      EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1604
1605      const Message& message_int32_double = mmf_int32_double->Get(i);
1606      int32_t key_int32_double = message_int32_double.GetReflection()->GetInt32(
1607          message_int32_double, fd_map_int32_double_key);
1608      double value_int32_double =
1609          message_int32_double.GetReflection()->GetDouble(
1610              message_int32_double, fd_map_int32_double_value);
1611      EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1612
1613      const Message& message_string_string = mmf_string_string->Get(i);
1614      std::string key_string_string =
1615          message_string_string.GetReflection()->GetString(
1616              message_string_string, fd_map_string_string_key);
1617      std::string value_string_string =
1618          message_string_string.GetReflection()->GetString(
1619              message_string_string, fd_map_string_string_value);
1620      EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1621
1622      const Message& message_int32_message = mmf_int32_foreign_message->Get(i);
1623      int32_t key_int32_message =
1624          message_int32_message.GetReflection()->GetInt32(
1625              message_int32_message, fd_map_int32_foreign_message_key);
1626      const ForeignMessage& value_int32_message =
1627          DownCastMessage<ForeignMessage>(
1628              message_int32_message.GetReflection()->GetMessage(
1629                  message_int32_message, fd_map_int32_foreign_message_value));
1630      EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1631    }
1632  }
1633
1634  // Do sets through the RepeatedPtrField objects.
1635  for (int i = 0; i < 10; i++) {
1636    {
1637      Message* message_int32_int32 = mmf_int32_int32->Mutable(i);
1638      int32_t key_int32_int32 = message_int32_int32->GetReflection()->GetInt32(
1639          *message_int32_int32, fd_map_int32_in32_key);
1640      message_int32_int32->GetReflection()->SetInt32(message_int32_int32,
1641                                                     fd_map_int32_in32_value,
1642                                                     Func(key_int32_int32, -1));
1643
1644      Message* message_int32_double = mmf_int32_double->Mutable(i);
1645      int32_t key_int32_double =
1646          message_int32_double->GetReflection()->GetInt32(
1647              *message_int32_double, fd_map_int32_double_key);
1648      message_int32_double->GetReflection()->SetDouble(
1649          message_int32_double, fd_map_int32_double_value,
1650          Func(key_int32_double, -2));
1651
1652      Message* message_string_string = mmf_string_string->Mutable(i);
1653      std::string key_string_string =
1654          message_string_string->GetReflection()->GetString(
1655              *message_string_string, fd_map_string_string_key);
1656      message_string_string->GetReflection()->SetString(
1657          message_string_string, fd_map_string_string_value,
1658          StrFunc(Int(key_string_string), -5));
1659
1660      Message* message_int32_message = mmf_int32_foreign_message->Mutable(i);
1661      int32_t key_int32_message =
1662          message_int32_message->GetReflection()->GetInt32(
1663              *message_int32_message, fd_map_int32_foreign_message_key);
1664      ForeignMessage* value_int32_message = DownCastMessage<ForeignMessage>(
1665          message_int32_message->GetReflection()->MutableMessage(
1666              message_int32_message, fd_map_int32_foreign_message_value));
1667      value_int32_message->set_c(Func(key_int32_message, -6));
1668    }
1669  }
1670
1671  // Check gets through mutable objects.
1672  for (int i = 0; i < 10; i++) {
1673    EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
1674    EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
1675    EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
1676    EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
1677  }
1678}
1679
1680TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) {
1681  TestMap message;
1682  const Reflection* refl = message.GetReflection();
1683  const Descriptor* desc = message.GetDescriptor();
1684
1685  Map<int32_t, int32_t>* map_int32_int32 = message.mutable_map_int32_int32();
1686  Map<int32_t, double>* map_int32_double = message.mutable_map_int32_double();
1687  Map<std::string, std::string>* map_string_string =
1688      message.mutable_map_string_string();
1689  Map<int32_t, ForeignMessage>* map_int32_foreign_message =
1690      message.mutable_map_int32_foreign_message();
1691
1692  for (int i = 0; i < 10; ++i) {
1693    (*map_int32_int32)[i] = Func(i, 1);
1694    (*map_int32_double)[i] = Func(i, 2);
1695    (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5);
1696    (*map_int32_foreign_message)[i].set_c(Func(i, 6));
1697  }
1698
1699  // Get FieldDescriptors for all the fields of interest.
1700  const FieldDescriptor* fd_map_int32_int32 =
1701      desc->FindFieldByName("map_int32_int32");
1702  const FieldDescriptor* fd_map_int32_double =
1703      desc->FindFieldByName("map_int32_double");
1704  const FieldDescriptor* fd_map_string_string =
1705      desc->FindFieldByName("map_string_string");
1706  const FieldDescriptor* fd_map_int32_foreign_message =
1707      desc->FindFieldByName("map_int32_foreign_message");
1708
1709  const FieldDescriptor* fd_map_int32_in32_key =
1710      fd_map_int32_int32->message_type()->map_key();
1711  const FieldDescriptor* fd_map_int32_in32_value =
1712      fd_map_int32_int32->message_type()->map_value();
1713  const FieldDescriptor* fd_map_int32_double_key =
1714      fd_map_int32_double->message_type()->map_key();
1715  const FieldDescriptor* fd_map_int32_double_value =
1716      fd_map_int32_double->message_type()->map_value();
1717  const FieldDescriptor* fd_map_string_string_key =
1718      fd_map_string_string->message_type()->map_key();
1719  const FieldDescriptor* fd_map_string_string_value =
1720      fd_map_string_string->message_type()->map_value();
1721  const FieldDescriptor* fd_map_int32_foreign_message_key =
1722      fd_map_int32_foreign_message->message_type()->map_key();
1723  const FieldDescriptor* fd_map_int32_foreign_message_value =
1724      fd_map_int32_foreign_message->message_type()->map_value();
1725
1726  // Get RepeatedFieldRef objects for all fields of interest.
1727  const RepeatedFieldRef<Message> mf_int32_int32 =
1728      refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_int32);
1729  const RepeatedFieldRef<Message> mf_int32_double =
1730      refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_double);
1731  const RepeatedFieldRef<Message> mf_string_string =
1732      refl->GetRepeatedFieldRef<Message>(message, fd_map_string_string);
1733  const RepeatedFieldRef<Message> mf_int32_foreign_message =
1734      refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_foreign_message);
1735
1736  // Get mutable RepeatedFieldRef objects for all fields of interest.
1737  const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
1738      refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_int32);
1739  const MutableRepeatedFieldRef<Message> mmf_int32_double =
1740      refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_double);
1741  const MutableRepeatedFieldRef<Message> mmf_string_string =
1742      refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_string_string);
1743  const MutableRepeatedFieldRef<Message> mmf_int32_foreign_message =
1744      refl->GetMutableRepeatedFieldRef<Message>(&message,
1745                                                fd_map_int32_foreign_message);
1746
1747  // Get entry default instances
1748  std::unique_ptr<Message> entry_int32_int32(
1749      MessageFactory::generated_factory()
1750          ->GetPrototype(fd_map_int32_int32->message_type())
1751          ->New(message.GetArena()));
1752  std::unique_ptr<Message> entry_int32_double(
1753      MessageFactory::generated_factory()
1754          ->GetPrototype(fd_map_int32_double->message_type())
1755          ->New(message.GetArena()));
1756  std::unique_ptr<Message> entry_string_string(
1757      MessageFactory::generated_factory()
1758          ->GetPrototype(fd_map_string_string->message_type())
1759          ->New(message.GetArena()));
1760  std::unique_ptr<Message> entry_int32_foreign_message(
1761      MessageFactory::generated_factory()
1762          ->GetPrototype(fd_map_int32_foreign_message->message_type())
1763          ->New(message.GetArena()));
1764
1765  EXPECT_EQ(10, mf_int32_int32.size());
1766  EXPECT_EQ(10, mmf_int32_int32.size());
1767  EXPECT_EQ(10, mf_int32_double.size());
1768  EXPECT_EQ(10, mmf_int32_double.size());
1769  EXPECT_EQ(10, mf_string_string.size());
1770  EXPECT_EQ(10, mmf_string_string.size());
1771  EXPECT_EQ(10, mf_int32_foreign_message.size());
1772  EXPECT_EQ(10, mmf_int32_foreign_message.size());
1773
1774  EXPECT_FALSE(mf_int32_int32.empty());
1775  EXPECT_FALSE(mmf_int32_int32.empty());
1776  EXPECT_FALSE(mf_int32_double.empty());
1777  EXPECT_FALSE(mmf_int32_double.empty());
1778  EXPECT_FALSE(mf_string_string.empty());
1779  EXPECT_FALSE(mmf_string_string.empty());
1780  EXPECT_FALSE(mf_int32_foreign_message.empty());
1781  EXPECT_FALSE(mmf_int32_foreign_message.empty());
1782
1783  // Make sure we can do gets through the RepeatedFieldRef objects.
1784  for (int i = 0; i < 10; ++i) {
1785    {
1786      // Check gets through const objects.
1787      const Message& message_int32_int32 =
1788          mf_int32_int32.Get(i, entry_int32_int32.get());
1789      int32_t key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1790          message_int32_int32, fd_map_int32_in32_key);
1791      int32_t value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1792          message_int32_int32, fd_map_int32_in32_value);
1793      EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1794
1795      const Message& message_int32_double =
1796          mf_int32_double.Get(i, entry_int32_double.get());
1797      int32_t key_int32_double = message_int32_double.GetReflection()->GetInt32(
1798          message_int32_double, fd_map_int32_double_key);
1799      double value_int32_double =
1800          message_int32_double.GetReflection()->GetDouble(
1801              message_int32_double, fd_map_int32_double_value);
1802      EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1803
1804      const Message& message_string_string =
1805          mf_string_string.Get(i, entry_string_string.get());
1806      std::string key_string_string =
1807          message_string_string.GetReflection()->GetString(
1808              message_string_string, fd_map_string_string_key);
1809      std::string value_string_string =
1810          message_string_string.GetReflection()->GetString(
1811              message_string_string, fd_map_string_string_value);
1812      EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1813
1814      const Message& message_int32_message =
1815          mf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
1816      int32_t key_int32_message =
1817          message_int32_message.GetReflection()->GetInt32(
1818              message_int32_message, fd_map_int32_foreign_message_key);
1819      const ForeignMessage& value_int32_message =
1820          DownCastMessage<ForeignMessage>(
1821              message_int32_message.GetReflection()->GetMessage(
1822                  message_int32_message, fd_map_int32_foreign_message_value));
1823      EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1824    }
1825
1826    {
1827      // Check gets through mutable objects.
1828      const Message& message_int32_int32 =
1829          mmf_int32_int32.Get(i, entry_int32_int32.get());
1830      int32_t key_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1831          message_int32_int32, fd_map_int32_in32_key);
1832      int32_t value_int32_int32 = message_int32_int32.GetReflection()->GetInt32(
1833          message_int32_int32, fd_map_int32_in32_value);
1834      EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1));
1835
1836      const Message& message_int32_double =
1837          mmf_int32_double.Get(i, entry_int32_double.get());
1838      int32_t key_int32_double = message_int32_double.GetReflection()->GetInt32(
1839          message_int32_double, fd_map_int32_double_key);
1840      double value_int32_double =
1841          message_int32_double.GetReflection()->GetDouble(
1842              message_int32_double, fd_map_int32_double_value);
1843      EXPECT_EQ(value_int32_double, Func(key_int32_double, 2));
1844
1845      const Message& message_string_string =
1846          mmf_string_string.Get(i, entry_string_string.get());
1847      std::string key_string_string =
1848          message_string_string.GetReflection()->GetString(
1849              message_string_string, fd_map_string_string_key);
1850      std::string value_string_string =
1851          message_string_string.GetReflection()->GetString(
1852              message_string_string, fd_map_string_string_value);
1853      EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5));
1854
1855      const Message& message_int32_message =
1856          mmf_int32_foreign_message.Get(i, entry_int32_foreign_message.get());
1857      int32_t key_int32_message =
1858          message_int32_message.GetReflection()->GetInt32(
1859              message_int32_message, fd_map_int32_foreign_message_key);
1860      const ForeignMessage& value_int32_message =
1861          DownCastMessage<ForeignMessage>(
1862              message_int32_message.GetReflection()->GetMessage(
1863                  message_int32_message, fd_map_int32_foreign_message_value));
1864      EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6));
1865    }
1866  }
1867
1868  // Make sure we can do sets through the RepeatedFieldRef objects.
1869  for (int i = 0; i < 10; i++) {
1870    const Message& message_int32_int32 =
1871        mmf_int32_int32.Get(i, entry_int32_int32.get());
1872    int key = message_int32_int32.GetReflection()->GetInt32(
1873        message_int32_int32, fd_map_int32_in32_key);
1874
1875    entry_int32_int32->GetReflection()->SetInt32(
1876        entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
1877        key);
1878    entry_int32_int32->GetReflection()->SetInt32(
1879        entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
1880        Func(key, -1));
1881    entry_int32_double->GetReflection()->SetInt32(
1882        entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
1883        key);
1884    entry_int32_double->GetReflection()->SetDouble(
1885        entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
1886        Func(key, -2));
1887    entry_string_string->GetReflection()->SetString(
1888        entry_string_string.get(),
1889        fd_map_string_string->message_type()->field(0), StrFunc(key, 1));
1890    entry_string_string->GetReflection()->SetString(
1891        entry_string_string.get(),
1892        fd_map_string_string->message_type()->field(1), StrFunc(key, -5));
1893    entry_int32_foreign_message->GetReflection()->SetInt32(
1894        entry_int32_foreign_message.get(),
1895        fd_map_int32_foreign_message->message_type()->field(0), key);
1896    Message* value_message =
1897        entry_int32_foreign_message->GetReflection()->MutableMessage(
1898            entry_int32_foreign_message.get(),
1899            fd_map_int32_foreign_message->message_type()->field(1));
1900    value_message->GetReflection()->SetInt32(
1901        value_message, value_message->GetDescriptor()->FindFieldByName("c"),
1902        Func(key, -6));
1903
1904    mmf_int32_int32.Set(i, *entry_int32_int32);
1905    mmf_int32_double.Set(i, *entry_int32_double);
1906    mmf_string_string.Set(i, *entry_string_string);
1907    mmf_int32_foreign_message.Set(i, *entry_int32_foreign_message);
1908  }
1909
1910  for (int i = 0; i < 10; i++) {
1911    EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i));
1912    EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i));
1913    EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1)));
1914    EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c());
1915  }
1916
1917  // Test iterators.
1918  {
1919    int index = 0;
1920    absl::flat_hash_map<int32_t, int32_t> result;
1921    for (const auto& message : mf_int32_int32) {
1922      int32_t key =
1923          message.GetReflection()->GetInt32(message, fd_map_int32_in32_key);
1924      int32_t value =
1925          message.GetReflection()->GetInt32(message, fd_map_int32_in32_value);
1926      result[key] = value;
1927      ++index;
1928    }
1929    EXPECT_EQ(10, index);
1930    for (const auto& kv : result) {
1931      EXPECT_EQ(message.map_int32_int32().at(kv.first), kv.second);
1932    }
1933  }
1934
1935  {
1936    int index = 0;
1937    absl::flat_hash_map<int32_t, double> result;
1938    for (const auto& message : mf_int32_double) {
1939      int32_t key =
1940          message.GetReflection()->GetInt32(message, fd_map_int32_double_key);
1941      double value = message.GetReflection()->GetDouble(
1942          message, fd_map_int32_double_value);
1943      result[key] = value;
1944      ++index;
1945    }
1946    EXPECT_EQ(10, index);
1947    for (const auto& kv : result) {
1948      EXPECT_EQ(message.map_int32_double().at(kv.first), kv.second);
1949    }
1950  }
1951
1952  {
1953    int index = 0;
1954    absl::flat_hash_map<std::string, std::string> result;
1955    for (const auto& message : mf_string_string) {
1956      std::string key =
1957          message.GetReflection()->GetString(message, fd_map_string_string_key);
1958      std::string value = message.GetReflection()->GetString(
1959          message, fd_map_string_string_value);
1960      result[key] = value;
1961      ++index;
1962    }
1963    EXPECT_EQ(10, index);
1964    for (const auto& kv : result) {
1965      EXPECT_EQ(message.map_string_string().at(kv.first), kv.second);
1966    }
1967  }
1968
1969  {
1970    int index = 0;
1971    absl::flat_hash_map<int32_t, ForeignMessage> result;
1972    for (RepeatedFieldRef<Message>::iterator it =
1973             mf_int32_foreign_message.begin();
1974         it != mf_int32_foreign_message.end(); ++it) {
1975      const Message& message = *it;
1976      int32_t key = message.GetReflection()->GetInt32(
1977          message, fd_map_int32_foreign_message_key);
1978      const ForeignMessage& sub_message =
1979          DownCastMessage<ForeignMessage>(message.GetReflection()->GetMessage(
1980              message, fd_map_int32_foreign_message_value));
1981      result[key].MergeFrom(sub_message);
1982      ++index;
1983    }
1984    EXPECT_EQ(10, index);
1985    for (const auto& e : result) {
1986      EXPECT_EQ(message.map_int32_foreign_message().at(e.first).c(),
1987                e.second.c());
1988    }
1989  }
1990
1991  // Test MutableRepeatedFieldRef::Add()
1992  entry_int32_int32->GetReflection()->SetInt32(
1993      entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0),
1994      4321);
1995  entry_int32_int32->GetReflection()->SetInt32(
1996      entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1),
1997      1234);
1998  mmf_int32_int32.Add(*entry_int32_int32);
1999  EXPECT_EQ(1234, message.map_int32_int32().at(4321));
2000
2001  entry_int32_double->GetReflection()->SetInt32(
2002      entry_int32_double.get(), fd_map_int32_double->message_type()->field(0),
2003      4321);
2004  entry_int32_double->GetReflection()->SetDouble(
2005      entry_int32_double.get(), fd_map_int32_double->message_type()->field(1),
2006      1234.0);
2007  mmf_int32_double.Add(*entry_int32_double);
2008  EXPECT_EQ(1234.0, message.map_int32_double().at(4321));
2009
2010  entry_string_string->GetReflection()->SetString(
2011      entry_string_string.get(), fd_map_string_string->message_type()->field(0),
2012      "4321");
2013  entry_string_string->GetReflection()->SetString(
2014      entry_string_string.get(), fd_map_string_string->message_type()->field(1),
2015      "1234");
2016  mmf_string_string.Add(*entry_string_string);
2017  EXPECT_EQ("1234", message.map_string_string().at("4321"));
2018
2019  entry_int32_foreign_message->GetReflection()->SetInt32(
2020      entry_int32_foreign_message.get(),
2021      fd_map_int32_foreign_message->message_type()->field(0), 4321);
2022  Message* value_message =
2023      entry_int32_foreign_message->GetReflection()->MutableMessage(
2024          entry_int32_foreign_message.get(),
2025          fd_map_int32_foreign_message->message_type()->field(1));
2026  ForeignMessage foreign_message;
2027  foreign_message.set_c(1234);
2028  value_message->CopyFrom(foreign_message);
2029
2030  mmf_int32_foreign_message.Add(*entry_int32_foreign_message);
2031  EXPECT_EQ(1234, message.map_int32_foreign_message().at(4321).c());
2032
2033  // Test Reflection::AddAllocatedMessage
2034  Message* free_entry_string_string =
2035      MessageFactory::generated_factory()
2036          ->GetPrototype(fd_map_string_string->message_type())
2037          ->New();
2038  entry_string_string->GetReflection()->SetString(
2039      free_entry_string_string, fd_map_string_string->message_type()->field(0),
2040      "4321");
2041  entry_string_string->GetReflection()->SetString(
2042      free_entry_string_string, fd_map_string_string->message_type()->field(1),
2043      "1234");
2044  refl->AddAllocatedMessage(&message, fd_map_string_string,
2045                            free_entry_string_string);
2046
2047  // Test MutableRepeatedFieldRef::RemoveLast()
2048  mmf_int32_int32.RemoveLast();
2049  mmf_int32_double.RemoveLast();
2050  mmf_string_string.RemoveLast();
2051  mmf_int32_foreign_message.RemoveLast();
2052  EXPECT_EQ(10, message.map_int32_int32().size());
2053  EXPECT_EQ(10, message.map_int32_double().size());
2054  EXPECT_EQ(11, message.map_string_string().size());
2055  EXPECT_EQ(10, message.map_int32_foreign_message().size());
2056
2057  // Test MutableRepeatedFieldRef::SwapElements()
2058  {
2059    const Message& message0a = mmf_int32_int32.Get(0, entry_int32_int32.get());
2060    int32_t int32_value0a =
2061        message0a.GetReflection()->GetInt32(message0a, fd_map_int32_in32_value);
2062    const Message& message9a = mmf_int32_int32.Get(9, entry_int32_int32.get());
2063    int32_t int32_value9a =
2064        message9a.GetReflection()->GetInt32(message9a, fd_map_int32_in32_value);
2065
2066    mmf_int32_int32.SwapElements(0, 9);
2067
2068    const Message& message0b = mmf_int32_int32.Get(0, entry_int32_int32.get());
2069    int32_t int32_value0b =
2070        message0b.GetReflection()->GetInt32(message0b, fd_map_int32_in32_value);
2071    const Message& message9b = mmf_int32_int32.Get(9, entry_int32_int32.get());
2072    int32_t int32_value9b =
2073        message9b.GetReflection()->GetInt32(message9b, fd_map_int32_in32_value);
2074
2075    EXPECT_EQ(int32_value9a, int32_value0b);
2076    EXPECT_EQ(int32_value0a, int32_value9b);
2077  }
2078
2079  {
2080    const Message& message0a =
2081        mmf_int32_double.Get(0, entry_int32_double.get());
2082    double double_value0a = message0a.GetReflection()->GetDouble(
2083        message0a, fd_map_int32_double_value);
2084    const Message& message9a =
2085        mmf_int32_double.Get(9, entry_int32_double.get());
2086    double double_value9a = message9a.GetReflection()->GetDouble(
2087        message9a, fd_map_int32_double_value);
2088
2089    mmf_int32_double.SwapElements(0, 9);
2090
2091    const Message& message0b =
2092        mmf_int32_double.Get(0, entry_int32_double.get());
2093    double double_value0b = message0b.GetReflection()->GetDouble(
2094        message0b, fd_map_int32_double_value);
2095    const Message& message9b =
2096        mmf_int32_double.Get(9, entry_int32_double.get());
2097    double double_value9b = message9b.GetReflection()->GetDouble(
2098        message9b, fd_map_int32_double_value);
2099
2100    EXPECT_EQ(double_value9a, double_value0b);
2101    EXPECT_EQ(double_value0a, double_value9b);
2102  }
2103
2104  {
2105    const Message& message0a =
2106        mmf_string_string.Get(0, entry_string_string.get());
2107    std::string string_value0a = message0a.GetReflection()->GetString(
2108        message0a, fd_map_string_string_value);
2109    const Message& message9a =
2110        mmf_string_string.Get(9, entry_string_string.get());
2111    std::string string_value9a = message9a.GetReflection()->GetString(
2112        message9a, fd_map_string_string_value);
2113
2114    mmf_string_string.SwapElements(0, 9);
2115
2116    const Message& message0b =
2117        mmf_string_string.Get(0, entry_string_string.get());
2118    std::string string_value0b = message0b.GetReflection()->GetString(
2119        message0b, fd_map_string_string_value);
2120    const Message& message9b =
2121        mmf_string_string.Get(9, entry_string_string.get());
2122    std::string string_value9b = message9b.GetReflection()->GetString(
2123        message9b, fd_map_string_string_value);
2124
2125    EXPECT_EQ(string_value9a, string_value0b);
2126    EXPECT_EQ(string_value0a, string_value9b);
2127  }
2128
2129  {
2130    const Message& message0a =
2131        mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get());
2132    const ForeignMessage& sub_message0a =
2133        DownCastMessage<ForeignMessage>(message0a.GetReflection()->GetMessage(
2134            message0a, fd_map_int32_foreign_message_value));
2135    int32_t int32_value0a = sub_message0a.c();
2136    const Message& message9a =
2137        mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get());
2138    const ForeignMessage& sub_message9a =
2139        DownCastMessage<ForeignMessage>(message9a.GetReflection()->GetMessage(
2140            message9a, fd_map_int32_foreign_message_value));
2141    int32_t int32_value9a = sub_message9a.c();
2142
2143    mmf_int32_foreign_message.SwapElements(0, 9);
2144
2145    const Message& message0b =
2146        mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get());
2147    const ForeignMessage& sub_message0b =
2148        DownCastMessage<ForeignMessage>(message0b.GetReflection()->GetMessage(
2149            message0b, fd_map_int32_foreign_message_value));
2150    int32_t int32_value0b = sub_message0b.c();
2151    const Message& message9b =
2152        mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get());
2153    const ForeignMessage& sub_message9b =
2154        DownCastMessage<ForeignMessage>(message9b.GetReflection()->GetMessage(
2155            message9b, fd_map_int32_foreign_message_value));
2156    int32_t int32_value9b = sub_message9b.c();
2157
2158    EXPECT_EQ(int32_value9a, int32_value0b);
2159    EXPECT_EQ(int32_value0a, int32_value9b);
2160  }
2161
2162  // TODO: After supporting arena agnostic delete or let map entry
2163  // handle heap allocation, this could be removed.
2164  if (message.GetArena() != nullptr) {
2165    entry_int32_int32.release();
2166    entry_int32_double.release();
2167    entry_string_string.release();
2168    entry_int32_foreign_message.release();
2169  }
2170}
2171
2172TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) {
2173  // Set-up message content.
2174  TestMap m0, m1, m2;
2175  for (int i = 0; i < 10; ++i) {
2176    (*m0.mutable_map_int32_int32())[i] = Func(i, 1);
2177    (*m0.mutable_map_int32_double())[i] = Func(i, 2);
2178    (*m0.mutable_map_string_string())[StrFunc(i, 1)] = StrFunc(i, 5);
2179    (*m0.mutable_map_int32_foreign_message())[i].set_c(Func(i, 6));
2180    (*m1.mutable_map_int32_int32())[i + 10] = Func(i, 11);
2181    (*m1.mutable_map_int32_double())[i + 10] = Func(i, 12);
2182    (*m1.mutable_map_string_string())[StrFunc(i + 10, 1)] = StrFunc(i, 15);
2183    (*m1.mutable_map_int32_foreign_message())[i + 10].set_c(Func(i, 16));
2184    (*m2.mutable_map_int32_int32())[i + 20] = Func(i, 21);
2185    (*m2.mutable_map_int32_double())[i + 20] = Func(i, 22);
2186    (*m2.mutable_map_string_string())[StrFunc(i + 20, 1)] = StrFunc(i, 25);
2187    (*m2.mutable_map_int32_foreign_message())[i + 20].set_c(Func(i, 26));
2188  }
2189
2190  const Reflection* refl = m0.GetReflection();
2191  const Descriptor* desc = m0.GetDescriptor();
2192
2193  // Get FieldDescriptors for all the fields of interest.
2194  const FieldDescriptor* fd_map_int32_int32 =
2195      desc->FindFieldByName("map_int32_int32");
2196  const FieldDescriptor* fd_map_int32_double =
2197      desc->FindFieldByName("map_int32_double");
2198  const FieldDescriptor* fd_map_string_string =
2199      desc->FindFieldByName("map_string_string");
2200  const FieldDescriptor* fd_map_int32_foreign_message =
2201      desc->FindFieldByName("map_int32_foreign_message");
2202
2203  // Get MutableRepeatedFieldRef objects for all fields of interest.
2204  const MutableRepeatedFieldRef<Message> mmf_int32_int32 =
2205      refl->GetMutableRepeatedFieldRef<Message>(&m0, fd_map_int32_int32);
2206  const MutableRepeatedFieldRef<Message> mmf_int32_double =
2207      refl->GetMutableRepeatedFieldRef<Message>(&m0, fd_map_int32_double);
2208  const MutableRepeatedFieldRef<Message> mmf_string_string =
2209      refl->GetMutableRepeatedFieldRef<Message>(&m0, fd_map_string_string);
2210  const MutableRepeatedFieldRef<Message> mmf_int32_foreign_message =
2211      refl->GetMutableRepeatedFieldRef<Message>(&m0,
2212                                                fd_map_int32_foreign_message);
2213
2214  // Test MutableRepeatedRef::CopyFrom
2215  mmf_int32_int32.CopyFrom(
2216      refl->GetRepeatedFieldRef<Message>(m1, fd_map_int32_int32));
2217  mmf_int32_double.CopyFrom(
2218      refl->GetRepeatedFieldRef<Message>(m1, fd_map_int32_double));
2219  mmf_string_string.CopyFrom(
2220      refl->GetRepeatedFieldRef<Message>(m1, fd_map_string_string));
2221  mmf_int32_foreign_message.CopyFrom(
2222      refl->GetRepeatedFieldRef<Message>(m1, fd_map_int32_foreign_message));
2223
2224  for (int i = 0; i < 10; ++i) {
2225    EXPECT_EQ(Func(i, 11), m0.map_int32_int32().at(i + 10));
2226    EXPECT_EQ(Func(i, 12), m0.map_int32_double().at(i + 10));
2227    EXPECT_EQ(StrFunc(i, 15), m0.map_string_string().at(StrFunc(i + 10, 1)));
2228    EXPECT_EQ(Func(i, 16), m0.map_int32_foreign_message().at(i + 10).c());
2229  }
2230
2231  // Test MutableRepeatedRef::MergeFrom
2232  mmf_int32_int32.MergeFrom(
2233      refl->GetRepeatedFieldRef<Message>(m2, fd_map_int32_int32));
2234  mmf_int32_double.MergeFrom(
2235      refl->GetRepeatedFieldRef<Message>(m2, fd_map_int32_double));
2236  mmf_string_string.MergeFrom(
2237      refl->GetRepeatedFieldRef<Message>(m2, fd_map_string_string));
2238  mmf_int32_foreign_message.MergeFrom(
2239      refl->GetRepeatedFieldRef<Message>(m2, fd_map_int32_foreign_message));
2240  for (int i = 0; i < 10; ++i) {
2241    EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20));
2242    EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20));
2243    EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
2244    EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
2245  }
2246
2247  // Test MutableRepeatedRef::Swap
2248  // Swap between m0 and m2.
2249  mmf_int32_int32.Swap(
2250      refl->GetMutableRepeatedFieldRef<Message>(&m2, fd_map_int32_int32));
2251  mmf_int32_double.Swap(
2252      refl->GetMutableRepeatedFieldRef<Message>(&m2, fd_map_int32_double));
2253  mmf_string_string.Swap(
2254      refl->GetMutableRepeatedFieldRef<Message>(&m2, fd_map_string_string));
2255  mmf_int32_foreign_message.Swap(refl->GetMutableRepeatedFieldRef<Message>(
2256      &m2, fd_map_int32_foreign_message));
2257  for (int i = 0; i < 10; ++i) {
2258    // Check the content of m0.
2259    EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20));
2260    EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20));
2261    EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1)));
2262    EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c());
2263
2264    // Check the content of m2.
2265    EXPECT_EQ(Func(i, 11), m2.map_int32_int32().at(i + 10));
2266    EXPECT_EQ(Func(i, 12), m2.map_int32_double().at(i + 10));
2267    EXPECT_EQ(StrFunc(i, 15), m2.map_string_string().at(StrFunc(i + 10, 1)));
2268    EXPECT_EQ(Func(i, 16), m2.map_int32_foreign_message().at(i + 10).c());
2269    EXPECT_EQ(Func(i, 21), m2.map_int32_int32().at(i + 20));
2270    EXPECT_EQ(Func(i, 22), m2.map_int32_double().at(i + 20));
2271    EXPECT_EQ(StrFunc(i, 25), m2.map_string_string().at(StrFunc(i + 20, 1)));
2272    EXPECT_EQ(Func(i, 26), m2.map_int32_foreign_message().at(i + 20).c());
2273  }
2274
2275  // TODO: add test for duplicated key
2276}
2277
2278TEST_F(MapFieldReflectionTest, MapSizeWithDuplicatedKey) {
2279  // Dynamic Message
2280  {
2281    DynamicMessageFactory factory;
2282    std::unique_ptr<Message> message(
2283        factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
2284    const Reflection* reflection = message->GetReflection();
2285    const FieldDescriptor* field =
2286        UNITTEST::TestMap::descriptor()->FindFieldByName("map_int32_int32");
2287
2288    Message* entry1 = reflection->AddMessage(message.get(), field);
2289    Message* entry2 = reflection->AddMessage(message.get(), field);
2290
2291    const Reflection* entry_reflection = entry1->GetReflection();
2292    const FieldDescriptor* key_field = entry1->GetDescriptor()->map_key();
2293    entry_reflection->SetInt32(entry1, key_field, 1);
2294    entry_reflection->SetInt32(entry2, key_field, 1);
2295
2296    EXPECT_EQ(2, reflection->FieldSize(*message, field));
2297    EXPECT_EQ(1, MapSize(reflection, field, *message));
2298    EXPECT_EQ(2, reflection->FieldSize(*message, field));
2299  }
2300
2301  // Generated Message
2302  {
2303    UNITTEST::TestMap message;
2304    const Reflection* reflection = message.GetReflection();
2305    const FieldDescriptor* field =
2306        message.GetDescriptor()->FindFieldByName("map_int32_int32");
2307
2308    Message* entry1 = reflection->AddMessage(&message, field);
2309    Message* entry2 = reflection->AddMessage(&message, field);
2310
2311    const Reflection* entry_reflection = entry1->GetReflection();
2312    const FieldDescriptor* key_field = entry1->GetDescriptor()->map_key();
2313    entry_reflection->SetInt32(entry1, key_field, 1);
2314    entry_reflection->SetInt32(entry2, key_field, 1);
2315
2316    EXPECT_EQ(2, reflection->FieldSize(message, field));
2317    EXPECT_EQ(1, MapSize(reflection, field, message));
2318  }
2319}
2320
2321TEST_F(MapFieldReflectionTest, UninitializedEntry) {
2322  UNITTEST::TestRequiredMessageMap message;
2323  const Reflection* reflection = message.GetReflection();
2324  const FieldDescriptor* field =
2325      message.GetDescriptor()->FindFieldByName("map_field");
2326  auto entry = reflection->AddMessage(&message, field);
2327  EXPECT_FALSE(entry->IsInitialized());
2328  EXPECT_FALSE(message.IsInitialized());
2329}
2330
2331class MyMapEntry
2332    : public internal::MapEntry<::int32_t, ::int32_t,
2333                                internal::WireFormatLite::TYPE_INT32,
2334                                internal::WireFormatLite::TYPE_INT32> {
2335 public:
2336  constexpr MyMapEntry()
2337      : MyMapEntry::MapEntry(static_cast<ClassData*>(nullptr)) {}
2338  MyMapEntry(Arena* arena) : MyMapEntry::MapEntry(arena, nullptr) {}
2339  const ClassData* GetClassData() const { ABSL_CHECK(false); }
2340  static bool ValidateKey(void*) { return true; }
2341  static bool ValidateValue(void*) { return true; }
2342};
2343
2344TEST(MapEntryTest, ConstInit) {
2345  // This verifies that `MapEntry` can be constant initialized.
2346  PROTOBUF_CONSTINIT static MyMapEntry entry{};
2347  // Use the object in some way to make sure the vtable is there.
2348  EXPECT_NE("", absl::StrFormat("%p", &entry));
2349}
2350
2351// Generated Message Test ===========================================
2352
2353TEST(GeneratedMapFieldTest, Accessors) {
2354  UNITTEST::TestMap message;
2355
2356  MapTestUtil::SetMapFields(&message);
2357  MapTestUtil::ExpectMapFieldsSet(message);
2358
2359  MapTestUtil::ModifyMapFields(&message);
2360  MapTestUtil::ExpectMapFieldsModified(message);
2361}
2362
2363TEST(GeneratedMapFieldTest, SetMapFieldsInitialized) {
2364  UNITTEST::TestMap message;
2365
2366  MapTestUtil::SetMapFieldsInitialized(&message);
2367  MapTestUtil::ExpectMapFieldsSetInitialized(message);
2368}
2369
2370TEST(GeneratedMapFieldTest, Proto2SetMapFieldsInitialized) {
2371  UNITTEST::TestEnumMap message;
2372  EXPECT_EQ(UNITTEST::PROTO2_MAP_ENUM_FOO,
2373            (*message.mutable_known_map_field())[0]);
2374}
2375
2376TEST(GeneratedMapFieldTest, Clear) {
2377  UNITTEST::TestMap message;
2378
2379  MapTestUtil::SetMapFields(&message);
2380  message.Clear();
2381  MapTestUtil::ExpectClear(message);
2382}
2383
2384TEST(GeneratedMapFieldTest, ClearMessageMap) {
2385  UNITTEST::TestMessageMap message;
2386
2387  // Creates a TestAllTypes with default value
2388  TestUtil::ExpectClear((*message.mutable_map_int32_message())[0]);
2389}
2390
2391TEST(GeneratedMapFieldTest, CopyFrom) {
2392  UNITTEST::TestMap message1, message2;
2393
2394  MapTestUtil::SetMapFields(&message1);
2395  message2.CopyFrom(message1);
2396  MapTestUtil::ExpectMapFieldsSet(message2);
2397
2398  // Copying from self should be a no-op.
2399  message2.CopyFrom(message2);
2400  MapTestUtil::ExpectMapFieldsSet(message2);
2401}
2402
2403TEST(GeneratedMapFieldTest, CopyFromMessageMap) {
2404  UNITTEST::TestMessageMap message1, message2;
2405
2406  (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
2407  (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
2408
2409  message1.CopyFrom(message2);
2410
2411  // Checks repeated field is overwritten.
2412  EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
2413  EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
2414}
2415
2416TEST(GeneratedMapFieldTest, SwapWithEmpty) {
2417  UNITTEST::TestMap message1, message2;
2418
2419  MapTestUtil::SetMapFields(&message1);
2420  MapTestUtil::ExpectMapFieldsSet(message1);
2421  MapTestUtil::ExpectClear(message2);
2422
2423  message1.Swap(&message2);
2424  MapTestUtil::ExpectMapFieldsSet(message2);
2425  MapTestUtil::ExpectClear(message1);
2426}
2427
2428TEST(GeneratedMapFieldTest, SwapWithSelf) {
2429  UNITTEST::TestMap message;
2430
2431  MapTestUtil::SetMapFields(&message);
2432  MapTestUtil::ExpectMapFieldsSet(message);
2433
2434  message.Swap(&message);
2435  MapTestUtil::ExpectMapFieldsSet(message);
2436}
2437
2438TEST(GeneratedMapFieldTest, SwapWithOther) {
2439  UNITTEST::TestMap message1, message2;
2440
2441  MapTestUtil::SetMapFields(&message1);
2442  MapTestUtil::SetMapFields(&message2);
2443  MapTestUtil::ModifyMapFields(&message2);
2444
2445  message1.Swap(&message2);
2446  MapTestUtil::ExpectMapFieldsModified(message1);
2447  MapTestUtil::ExpectMapFieldsSet(message2);
2448}
2449
2450TEST(GeneratedMapFieldTest, CopyConstructor) {
2451  UNITTEST::TestMap message1;
2452  MapTestUtil::SetMapFields(&message1);
2453
2454  UNITTEST::TestMap message2(message1);
2455  MapTestUtil::ExpectMapFieldsSet(message2);
2456}
2457
2458TEST(GeneratedMapFieldTest, CopyAssignmentOperator) {
2459  UNITTEST::TestMap message1;
2460  MapTestUtil::SetMapFields(&message1);
2461
2462  UNITTEST::TestMap message2;
2463  message2 = message1;
2464  MapTestUtil::ExpectMapFieldsSet(message2);
2465
2466  // Make sure that self-assignment does something sane.
2467  message2.operator=(message2);
2468  MapTestUtil::ExpectMapFieldsSet(message2);
2469}
2470
2471#if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || PROTOBUF_RTTI
2472TEST(GeneratedMapFieldTest, UpcastCopyFrom) {
2473  // Test the CopyFrom method that takes in the generic const Message&
2474  // parameter.
2475  UNITTEST::TestMap message1, message2;
2476
2477  MapTestUtil::SetMapFields(&message1);
2478
2479  const Message* source = absl::implicit_cast<const Message*>(&message1);
2480  message2.CopyFrom(*source);
2481
2482  MapTestUtil::ExpectMapFieldsSet(message2);
2483}
2484#endif
2485
2486#ifndef PROTOBUF_TEST_NO_DESCRIPTORS
2487
2488TEST(GeneratedMapFieldTest, CopyFromDynamicMessage) {
2489  // Test copying from a DynamicMessage, which must fall back to using
2490  // reflection.
2491  UNITTEST::TestMap message2;
2492
2493  // Construct a new version of the dynamic message via the factory.
2494  DynamicMessageFactory factory;
2495  std::unique_ptr<Message> message1;
2496  message1.reset(factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
2497  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
2498  reflection_tester.SetMapFieldsViaReflection(message1.get());
2499  reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
2500  reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
2501  message2.CopyFrom(*message1);
2502  MapTestUtil::ExpectMapFieldsSet(message2);
2503}
2504
2505TEST(GeneratedMapFieldTest, CopyFromDynamicMessageMapReflection) {
2506  UNITTEST::TestMap message2;
2507
2508  // Construct a new version of the dynamic message via the factory.
2509  DynamicMessageFactory factory;
2510  std::unique_ptr<Message> message1;
2511  message1.reset(factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
2512  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
2513  reflection_tester.SetMapFieldsViaMapReflection(message1.get());
2514  reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
2515  reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
2516  message2.CopyFrom(*message1);
2517  MapTestUtil::ExpectMapFieldsSet(message2);
2518}
2519
2520TEST(GeneratedMapFieldTest, DynamicMessageMergeFromDynamicMessage) {
2521  // Construct two dynamic message and sets via map reflection.
2522  DynamicMessageFactory factory;
2523  std::unique_ptr<Message> message1;
2524  message1.reset(factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
2525  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
2526  reflection_tester.SetMapFieldsViaMapReflection(message1.get());
2527
2528  // message2 is created by same factory.
2529  std::unique_ptr<Message> message2;
2530  message2.reset(factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
2531  reflection_tester.SetMapFieldsViaMapReflection(message2.get());
2532
2533  // message3 is created by different factory.
2534  DynamicMessageFactory factory3;
2535  std::unique_ptr<Message> message3;
2536  message3.reset(factory3.GetPrototype(UNITTEST::TestMap::descriptor())->New());
2537  reflection_tester.SetMapFieldsViaMapReflection(message3.get());
2538
2539  message2->MergeFrom(*message1);
2540  message3->MergeFrom(*message1);
2541
2542  // Test MergeFrom does not sync to repeated fields and
2543  // there is no duplicate keys in text format.
2544  std::string output1, output2, output3;
2545  TextFormat::PrintToString(*message1, &output1);
2546  TextFormat::PrintToString(*message2, &output2);
2547  TextFormat::PrintToString(*message3, &output3);
2548  EXPECT_EQ(output1, output2);
2549  EXPECT_EQ(output1, output3);
2550}
2551
2552TEST(GeneratedMapFieldTest, DynamicMessageCopyFrom) {
2553  // Test copying to a DynamicMessage, which must fall back to using reflection.
2554  UNITTEST::TestMap message2;
2555  MapTestUtil::SetMapFields(&message2);
2556
2557  // Construct a new version of the dynamic message via the factory.
2558  DynamicMessageFactory factory;
2559  std::unique_ptr<Message> message1;
2560  message1.reset(factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
2561
2562  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
2563  message1->MergeFrom(message2);
2564  reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
2565  reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
2566}
2567
2568TEST(GeneratedMapFieldTest, DynamicMessageCopyFromMapReflection) {
2569  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
2570  UNITTEST::TestMap message2;
2571  reflection_tester.SetMapFieldsViaMapReflection(&message2);
2572
2573  // Construct a dynamic message via the factory.
2574  DynamicMessageFactory factory;
2575  std::unique_ptr<Message> message1;
2576  message1.reset(factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
2577
2578  message1->MergeFrom(message2);
2579  reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1.get());
2580  reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
2581}
2582
2583TEST(GeneratedMapFieldTest, SyncDynamicMapWithRepeatedField) {
2584  // Construct a dynamic message via the factory.
2585  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
2586  DynamicMessageFactory factory;
2587  std::unique_ptr<Message> message;
2588  message.reset(factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
2589  reflection_tester.SetMapFieldsViaReflection(message.get());
2590  reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message.get());
2591  reflection_tester.ExpectMapFieldsSetViaReflection(*message);
2592}
2593
2594#endif  // !PROTOBUF_TEST_NO_DESCRIPTORS
2595
2596TEST(GeneratedMapFieldTest, NonEmptyMergeFrom) {
2597  UNITTEST::TestMap message1, message2;
2598
2599  MapTestUtil::SetMapFields(&message1);
2600
2601  // This field will test merging into an empty spot.
2602  (*message2.mutable_map_int32_int32())[1] = 1;
2603  message1.mutable_map_int32_int32()->erase(1);
2604
2605  // This tests overwriting.
2606  (*message2.mutable_map_int32_double())[1] = 1;
2607  (*message1.mutable_map_int32_double())[1] = 2;
2608
2609  message1.MergeFrom(message2);
2610  MapTestUtil::ExpectMapFieldsSet(message1);
2611
2612  // Test reflection MergeFrom does not sync to repeated field
2613  // and there is no duplicated keys.
2614  MapTestUtil::SetMapFields(&message1);
2615  MapTestUtil::SetMapFields(&message2);
2616
2617  message2.MergeFrom(message1);
2618
2619  std::string output1, output2;
2620  TextFormat::PrintToString(message1, &output1);
2621  TextFormat::PrintToString(message2, &output2);
2622  EXPECT_EQ(output1, output2);
2623}
2624
2625TEST(GeneratedMapFieldTest, MergeFromMessageMap) {
2626  UNITTEST::TestMessageMap message1, message2;
2627
2628  (*message1.mutable_map_int32_message())[0].add_repeated_int32(100);
2629  (*message2.mutable_map_int32_message())[0].add_repeated_int32(101);
2630
2631  message1.MergeFrom(message2);
2632
2633  // Checks repeated field is overwritten.
2634  EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size());
2635  EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0));
2636}
2637
2638// Test the generated SerializeWithCachedSizesToArray()
2639TEST(GeneratedMapFieldTest, SerializationToArray) {
2640  UNITTEST::TestMap message1, message2;
2641  std::string data;
2642  MapTestUtil::SetMapFields(&message1);
2643  size_t size = message1.ByteSizeLong();
2644  data.resize(size);
2645  uint8_t* start = reinterpret_cast<uint8_t*>(&data[0]);
2646  uint8_t* end = message1.SerializeWithCachedSizesToArray(start);
2647  EXPECT_EQ(size, end - start);
2648  EXPECT_TRUE(message2.ParseFromString(data));
2649  MapTestUtil::ExpectMapFieldsSet(message2);
2650}
2651
2652// Test the generated SerializeWithCachedSizes()
2653TEST(GeneratedMapFieldTest, SerializationToStream) {
2654  UNITTEST::TestMap message1, message2;
2655  MapTestUtil::SetMapFields(&message1);
2656  size_t size = message1.ByteSizeLong();
2657  std::string data;
2658  data.resize(size);
2659  {
2660    // Allow the output stream to buffer only one byte at a time.
2661    io::ArrayOutputStream array_stream(&data[0], size, 1);
2662    io::CodedOutputStream output_stream(&array_stream);
2663    message1.SerializeWithCachedSizes(&output_stream);
2664    EXPECT_FALSE(output_stream.HadError());
2665    EXPECT_EQ(size, output_stream.ByteCount());
2666  }
2667  EXPECT_TRUE(message2.ParseFromString(data));
2668  MapTestUtil::ExpectMapFieldsSet(message2);
2669}
2670
2671TEST(GeneratedMapFieldTest, ParseFailsIfMalformed) {
2672  UNITTEST::TestMapSubmessage o, p;
2673  auto m = o.mutable_test_map()->mutable_map_int32_foreign_message();
2674  (*m)[0].set_c(-1);
2675  std::string serialized;
2676  EXPECT_TRUE(o.SerializeToString(&serialized));
2677
2678  // Should parse correctly.
2679  EXPECT_TRUE(p.ParseFromString(serialized));
2680
2681  // Overwriting the last byte to 0xFF results in malformed wire.
2682  serialized[serialized.size() - 1] = 0xFF;
2683  EXPECT_FALSE(p.ParseFromString(serialized));
2684}
2685
2686
2687TEST(GeneratedMapFieldTest, SameTypeMaps) {
2688  const Descriptor* map1 = UNITTEST::TestSameTypeMap::descriptor()
2689                               ->FindFieldByName("map1")
2690                               ->message_type();
2691  const Descriptor* map2 = UNITTEST::TestSameTypeMap::descriptor()
2692                               ->FindFieldByName("map2")
2693                               ->message_type();
2694
2695  const Message* map1_entry =
2696      MessageFactory::generated_factory()->GetPrototype(map1);
2697  const Message* map2_entry =
2698      MessageFactory::generated_factory()->GetPrototype(map2);
2699
2700  EXPECT_EQ(map1, map1_entry->GetDescriptor());
2701  EXPECT_EQ(map2, map2_entry->GetDescriptor());
2702}
2703
2704TEST(GeneratedMapFieldTest, Proto2UnknownEnum) {
2705  UNITTEST::TestEnumMapPlusExtra from;
2706  (*from.mutable_known_map_field())[0] = UNITTEST::E_PROTO2_MAP_ENUM_FOO;
2707  (*from.mutable_unknown_map_field())[0] = UNITTEST::E_PROTO2_MAP_ENUM_EXTRA;
2708  std::string data;
2709  from.SerializeToString(&data);
2710
2711  UNITTEST::TestEnumMap to;
2712  EXPECT_TRUE(to.ParseFromString(data));
2713  EXPECT_EQ(0, to.unknown_map_field().size());
2714  EXPECT_EQ(1, to.GetReflection()->GetUnknownFields(to).field_count());
2715  EXPECT_EQ(1, to.known_map_field().size());
2716  EXPECT_EQ(UNITTEST::PROTO2_MAP_ENUM_FOO, to.known_map_field().at(0));
2717
2718  data.clear();
2719  from.Clear();
2720  to.SerializeToString(&data);
2721  EXPECT_TRUE(from.ParseFromString(data));
2722  EXPECT_EQ(0, from.GetReflection()->GetUnknownFields(from).field_count());
2723  EXPECT_EQ(1, from.known_map_field().size());
2724  EXPECT_EQ(UNITTEST::E_PROTO2_MAP_ENUM_FOO, from.known_map_field().at(0));
2725  EXPECT_EQ(1, from.unknown_map_field().size());
2726  EXPECT_EQ(UNITTEST::E_PROTO2_MAP_ENUM_EXTRA, from.unknown_map_field().at(0));
2727
2728  // Test the same behavior with the reflection based parser.
2729  to.Clear();
2730  const char* ptr;
2731  internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
2732                             false, &ptr, data);
2733  ptr = WireFormat::_InternalParse(&to, ptr, &ctx);
2734  ASSERT_TRUE(ptr);
2735  ASSERT_TRUE(ctx.EndedAtLimit());
2736
2737  EXPECT_EQ(0, to.unknown_map_field().size());
2738  EXPECT_EQ(1, to.GetReflection()->GetUnknownFields(to).field_count());
2739  EXPECT_EQ(1, to.known_map_field().size());
2740  EXPECT_EQ(UNITTEST::PROTO2_MAP_ENUM_FOO, to.known_map_field().at(0));
2741}
2742
2743TEST(GeneratedMapFieldTest, Proto2UnknownEnumThrowsAwayUnknownData) {
2744  UNITTEST::TestEnumMap to;
2745  // 101: {
2746  //   1: 2
2747  //   2: 3     # this is an unknown enum. It should be kept.
2748  //   3: 120   # this is an extra field that will be discarded.
2749  // }
2750  constexpr absl::string_view data = "\252\006\006\010\002\020\003\030x";
2751  // Same as above, but without the extra field.
2752  constexpr absl::string_view expected = "\252\006\004\010\002\020\003";
2753  ASSERT_TRUE(to.ParseFromString(data));
2754  EXPECT_EQ(expected, to.SerializeAsString());
2755
2756  // Test the same behavior with the reflection based parser.
2757  to.Clear();
2758  const char* ptr;
2759  internal::ParseContext ctx(io::CodedInputStream::GetDefaultRecursionLimit(),
2760                             false, &ptr, data);
2761  ptr = WireFormat::_InternalParse(&to, ptr, &ctx);
2762  ASSERT_TRUE(ptr);
2763  ASSERT_TRUE(ctx.EndedAtLimit());
2764  EXPECT_EQ(expected, to.SerializeAsString());
2765}
2766
2767TEST(GeneratedMapFieldTest, Proto2UnknownEnumAllKeyTypesWork) {
2768#define PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(Type, ...)                        \
2769  ABSL_LOG(INFO) << "Testing " << #Type;                                      \
2770  for (auto value : {__VA_ARGS__}) {                                          \
2771    UNITTEST::TestEnumMapPlusExtra from;                                      \
2772    (*from.mutable_unknown_map_field_##Type())[value] =                       \
2773        UNITTEST::E_PROTO2_MAP_ENUM_EXTRA;                                    \
2774    UNITTEST::TestEnumMap to;                                                 \
2775    ASSERT_TRUE(to.ParseFromString(from.SerializeAsString()));                \
2776    EXPECT_EQ(0, to.unknown_map_field_##Type().size())                        \
2777        << testing::PrintToString(to.unknown_map_field_##Type());             \
2778    const UnknownFieldSet& unknown_field_set =                                \
2779        to.GetReflection()->GetUnknownFields(to);                             \
2780    EXPECT_EQ(1, unknown_field_set.field_count());                            \
2781    from.Clear();                                                             \
2782    EXPECT_EQ(from.unknown_map_field_##Type().size(), 0);                     \
2783    EXPECT_TRUE(from.ParseFromString(to.SerializeAsString()));                \
2784    EXPECT_THAT(from.unknown_map_field_##Type(),                              \
2785                ElementsAre(Pair(value, UNITTEST::E_PROTO2_MAP_ENUM_EXTRA))); \
2786    EXPECT_EQ(from.SerializeAsString(), to.SerializeAsString());              \
2787  }
2788
2789  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(int64, int64_t{17},
2790                                      std::numeric_limits<int64_t>::min(),
2791                                      std::numeric_limits<int64_t>::max());
2792  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(uint64, uint64_t{17},
2793                                      std::numeric_limits<uint64_t>::min(),
2794                                      std::numeric_limits<uint64_t>::max());
2795  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(int32, int32_t{17},
2796                                      std::numeric_limits<int32_t>::min(),
2797                                      std::numeric_limits<int32_t>::max());
2798  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(uint32, uint32_t{17},
2799                                      std::numeric_limits<uint32_t>::min(),
2800                                      std::numeric_limits<uint32_t>::max());
2801  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(fixed32, uint32_t{17},
2802                                      std::numeric_limits<uint32_t>::min(),
2803                                      std::numeric_limits<uint32_t>::max());
2804  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(fixed64, uint64_t{17},
2805                                      std::numeric_limits<uint64_t>::min(),
2806                                      std::numeric_limits<uint64_t>::max());
2807  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(bool, true);
2808  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(string, "17");
2809  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(sint32, int32_t{17},
2810                                      std::numeric_limits<int32_t>::min(),
2811                                      std::numeric_limits<int32_t>::max());
2812  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(sint64, int64_t{17},
2813                                      std::numeric_limits<int64_t>::min(),
2814                                      std::numeric_limits<int64_t>::max());
2815  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(sfixed32, int32_t{17},
2816                                      std::numeric_limits<int32_t>::min(),
2817                                      std::numeric_limits<int32_t>::max());
2818  PROTOBUF_INTERNAL_TEST_MAP_KEY_TYPE(sfixed64, int64_t{17},
2819                                      std::numeric_limits<int64_t>::min(),
2820                                      std::numeric_limits<int64_t>::max());
2821}
2822
2823TEST(GeneratedMapFieldTest, StandardWireFormat) {
2824  UNITTEST::TestMap message;
2825  std::string data = "\x0A\x04\x08\x01\x10\x01";
2826
2827  EXPECT_TRUE(message.ParseFromString(data));
2828  EXPECT_EQ(1, message.map_int32_int32().size());
2829  EXPECT_EQ(1, message.map_int32_int32().at(1));
2830}
2831
2832TEST(GeneratedMapFieldTest, UnorderedWireFormat) {
2833  UNITTEST::TestMap message;
2834
2835  // put value before key in wire format
2836  std::string data = "\x0A\x04\x10\x01\x08\x02";
2837
2838  EXPECT_TRUE(message.ParseFromString(data));
2839  EXPECT_EQ(1, message.map_int32_int32().size());
2840  ASSERT_NE(message.map_int32_int32().find(2), message.map_int32_int32().end());
2841  EXPECT_EQ(1, message.map_int32_int32().at(2));
2842}
2843
2844TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) {
2845  UNITTEST::TestMap message;
2846
2847  // Two key fields in wire format
2848  std::string data = "\x0A\x06\x08\x01\x08\x02\x10\x01";
2849
2850  EXPECT_TRUE(message.ParseFromString(data));
2851  EXPECT_EQ(1, message.map_int32_int32().size());
2852  EXPECT_EQ(1, message.map_int32_int32().at(2));
2853
2854  // A similar test, but with a map from int to a message type.
2855  // Again, we want to be sure that the "second one wins" when
2856  // there are two separate entries with the same key.
2857  const int key = 99;
2858  UNITTEST::TestRequiredMessageMap map_message;
2859  UNITTEST::TestRequired with_dummy4;
2860  with_dummy4.set_a(0);
2861  with_dummy4.set_b(0);
2862  with_dummy4.set_c(0);
2863  with_dummy4.set_dummy4(11);
2864  (*map_message.mutable_map_field())[key] = with_dummy4;
2865  std::string s = map_message.SerializeAsString();
2866  UNITTEST::TestRequired with_dummy5;
2867  with_dummy5.set_a(0);
2868  with_dummy5.set_b(0);
2869  with_dummy5.set_c(0);
2870  with_dummy5.set_dummy5(12);
2871  (*map_message.mutable_map_field())[key] = with_dummy5;
2872  std::string both = s + map_message.SerializeAsString();
2873  // We don't expect a merge now.  The "second one wins."
2874  ASSERT_TRUE(map_message.ParseFromString(both));
2875  ASSERT_EQ(1, map_message.map_field().size());
2876  ASSERT_EQ(1, map_message.map_field().count(key));
2877  EXPECT_EQ(0, map_message.map_field().find(key)->second.a());
2878  EXPECT_EQ(0, map_message.map_field().find(key)->second.b());
2879  EXPECT_EQ(0, map_message.map_field().find(key)->second.c());
2880  EXPECT_FALSE(map_message.map_field().find(key)->second.has_dummy4());
2881  ASSERT_TRUE(map_message.map_field().find(key)->second.has_dummy5());
2882  EXPECT_EQ(12, map_message.map_field().find(key)->second.dummy5());
2883}
2884
2885// Exhaustive combinations of keys, values, and junk in any order.
2886// This re-tests some of the things tested above, but if it fails
2887// it's more work to determine what went wrong, so it isn't necessarily
2888// bad that we have the simpler tests too.
2889TEST(GeneratedMapFieldTest, KeysValuesUnknownsWireFormat) {
2890  UNITTEST::TestMap message;
2891  const int kMaxNumKeysAndValuesAndJunk = 4;
2892  const char kKeyTag = 0x08;
2893  const char kValueTag = 0x10;
2894  const char kJunkTag = 0x20;
2895  for (int items = 0; items <= kMaxNumKeysAndValuesAndJunk; items++) {
2896    std::string data = "\x0A";
2897    // Encode length of what will follow.
2898    data.push_back(items * 2);
2899    static const int kBitsOfIPerItem = 4;
2900    static const int mask = (1 << kBitsOfIPerItem) - 1;
2901    // Each iteration of the following is a test.  It uses i as bit vector
2902    // encoding the keys and values to put in the wire format.
2903    for (int i = 0; i < (1 << (items * kBitsOfIPerItem)); i++) {
2904      std::string wire_format = data;
2905      int expected_key = 0;
2906      int expected_value = 0;
2907      for (int k = i, j = 0; j < items; j++, k >>= kBitsOfIPerItem) {
2908        bool is_key = k & 0x1;
2909        bool is_value = !is_key && (k & 0x2);
2910        wire_format.push_back(is_key     ? kKeyTag
2911                              : is_value ? kValueTag
2912                                         : kJunkTag);
2913        char c = static_cast<char>(k & mask) >> 2;  // One char after the tag.
2914        wire_format.push_back(c);
2915        if (is_key) expected_key = static_cast<int>(c);
2916        if (is_value) expected_value = static_cast<int>(c);
2917        bool res = message.ParseFromString(wire_format);
2918        bool expect_success = true;
2919        // Unfortunately the old map parser accepts malformed input, the new
2920        // parser accepts only correct input.
2921        if (j != items - 1) expect_success = false;
2922        if (expect_success) {
2923          ASSERT_TRUE(res);
2924          ASSERT_EQ(1, message.map_int32_int32().size());
2925          ASSERT_EQ(expected_key, message.map_int32_int32().begin()->first);
2926          ASSERT_EQ(expected_value, message.map_int32_int32().begin()->second);
2927        } else {
2928          ASSERT_FALSE(res);
2929        }
2930      }
2931    }
2932  }
2933}
2934
2935TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) {
2936  UNITTEST::TestMap message;
2937
2938  // Two value fields in wire format
2939  std::string data = "\x0A\x06\x08\x01\x10\x01\x10\x02";
2940
2941  EXPECT_TRUE(message.ParseFromString(data));
2942  EXPECT_EQ(1, message.map_int32_int32().size());
2943  EXPECT_EQ(2, message.map_int32_int32().at(1));
2944}
2945
2946TEST(GeneratedMapFieldTest, MissedKeyWireFormat) {
2947  UNITTEST::TestMap message;
2948
2949  // No key field in wire format
2950  std::string data = "\x0A\x02\x10\x01";
2951
2952  EXPECT_TRUE(message.ParseFromString(data));
2953  EXPECT_EQ(1, message.map_int32_int32().size());
2954  ASSERT_NE(message.map_int32_int32().find(0), message.map_int32_int32().end());
2955  EXPECT_EQ(1, message.map_int32_int32().at(0));
2956}
2957
2958TEST(GeneratedMapFieldTest, MissedValueWireFormat) {
2959  UNITTEST::TestMap message;
2960
2961  // No value field in wire format
2962  std::string data = "\x0A\x02\x08\x01";
2963
2964  EXPECT_TRUE(message.ParseFromString(data));
2965  EXPECT_EQ(1, message.map_int32_int32().size());
2966  ASSERT_NE(message.map_int32_int32().find(1), message.map_int32_int32().end());
2967  EXPECT_EQ(0, message.map_int32_int32().at(1));
2968}
2969
2970TEST(GeneratedMapFieldTest, MissedValueTextFormat) {
2971  UNITTEST::TestMap message;
2972
2973  // No value field in text format
2974  std::string text =
2975      "map_int32_foreign_message {\n"
2976      "  key: 1234567890\n"
2977      "}";
2978
2979  EXPECT_TRUE(TextFormat::ParseFromString(text, &message));
2980  EXPECT_EQ(1, message.map_int32_foreign_message().size());
2981  EXPECT_EQ(11, message.ByteSizeLong());
2982}
2983
2984TEST(GeneratedMapFieldTest, UnknownFieldWireFormat) {
2985  UNITTEST::TestMap message;
2986
2987  // Unknown field in wire format
2988  std::string data = "\x0A\x06\x08\x02\x10\x03\x18\x01";
2989
2990  EXPECT_TRUE(message.ParseFromString(data));
2991  EXPECT_EQ(1, message.map_int32_int32().size());
2992  EXPECT_EQ(3, message.map_int32_int32().at(2));
2993}
2994
2995TEST(GeneratedMapFieldTest, ToplevelTagNotLengthPrefixed) {
2996  UNITTEST::TestMap message;
2997
2998  // The toplevel matches a field number, but does not match the wire type.
2999  std::string data = "\x08\x81\x04";
3000  EXPECT_TRUE(message.ParseFromString(data));
3001  EXPECT_EQ(0, message.map_int32_int32().size());
3002
3003  const UnknownFieldSet& unknown_field_set =
3004      message.GetReflection()->GetUnknownFields(message);
3005  ASSERT_EQ(1, unknown_field_set.field_count());
3006  auto& field = unknown_field_set.field(0);
3007  ASSERT_EQ(field.TYPE_VARINT, field.type());
3008  EXPECT_EQ(((0x1 << 0) | (0x04 << 7)), field.varint());
3009}
3010
3011TEST(GeneratedMapFieldTest, InnerTagsInLongForm) {
3012  UNITTEST::TestMap message;
3013
3014  // First, control
3015  absl::string_view data("\012\004\010\007\020\005", 6);
3016  ASSERT_TRUE(message.ParseFromString(data));
3017  EXPECT_THAT(message.map_int32_int32(), ElementsAre(Pair(7, 5)));
3018
3019  // Now we make the key tag long form
3020  data = absl::string_view("\012\005\210\000\007\020\005", 7);
3021  ASSERT_TRUE(message.ParseFromString(data));
3022  EXPECT_THAT(message.map_int32_int32(), ElementsAre(Pair(7, 5)));
3023
3024  // Now we make the value tag long form
3025  data = absl::string_view("\012\005\010\007\220\000\005", 7);
3026  ASSERT_TRUE(message.ParseFromString(data));
3027  EXPECT_THAT(message.map_int32_int32(), ElementsAre(Pair(7, 5)));
3028}
3029
3030TEST(GeneratedMapFieldTest, CorruptedWireFormat) {
3031  UNITTEST::TestMap message;
3032
3033  // corrupted data in wire format
3034  std::string data = "\x0A\x06\x08\x02\x11\x03";
3035
3036  EXPECT_FALSE(message.ParseFromString(data));
3037}
3038
3039TEST(GeneratedMapFieldTest, IsInitialized) {
3040  UNITTEST::TestRequiredMessageMap map_message;
3041
3042  // Add an uninitialized message.
3043  (*map_message.mutable_map_field())[0];
3044  EXPECT_FALSE(map_message.IsInitialized());
3045
3046  // Initialize uninitialized message
3047  (*map_message.mutable_map_field())[0].set_a(0);
3048  (*map_message.mutable_map_field())[0].set_b(0);
3049  (*map_message.mutable_map_field())[0].set_c(0);
3050  EXPECT_TRUE(map_message.IsInitialized());
3051}
3052
3053TEST(GeneratedMapFieldTest, SpaceUsed) {
3054  UNITTEST::TestRequiredMessageMap map_message;
3055  const size_t initial = map_message.SpaceUsed();
3056  const size_t space_used_message = UNITTEST::TestRequired().SpaceUsed();
3057
3058  auto& m = *map_message.mutable_map_field();
3059  constexpr int kNumValues = 100;
3060  for (int i = 0; i < kNumValues; ++i) {
3061    m[i];
3062  }
3063
3064  // The exact value will depend on internal state, like collisions,
3065  // so we can't predict it. But we can predict a lower bound.
3066  size_t lower_bound =
3067      initial + kNumValues * (space_used_message + sizeof(int32_t) +
3068                              /* Node::next */ sizeof(void*) +
3069                              /* table entry */ sizeof(void*));
3070
3071  EXPECT_LE(lower_bound, map_message.SpaceUsed());
3072}
3073
3074TEST(GeneratedMapFieldTest, MessagesMustMerge) {
3075  UNITTEST::TestRequiredMessageMap map_message;
3076
3077  UNITTEST::TestRequired with_dummy4;
3078  with_dummy4.set_a(97);
3079  with_dummy4.set_b(91);
3080  with_dummy4.set_dummy4(98);
3081  EXPECT_FALSE(with_dummy4.IsInitialized());
3082  (*map_message.mutable_map_field())[0] = with_dummy4;
3083  EXPECT_FALSE(map_message.IsInitialized());
3084
3085  UNITTEST::TestRequired with_dummy5;
3086  with_dummy5.set_b(0);
3087  with_dummy5.set_c(33);
3088  with_dummy5.set_dummy5(99);
3089  EXPECT_FALSE(with_dummy5.IsInitialized());
3090  (*map_message.mutable_map_field())[0] = with_dummy5;
3091  EXPECT_FALSE(map_message.IsInitialized());
3092
3093  // The wire format of MapEntry is straightforward (*) and can be manually
3094  // constructed to force merging of two uninitialized messages that would
3095  // result in an initialized message.
3096  //
3097  // (*) http://google3/net/proto2/internal/map_test.cc?l=2433&rcl=310012028
3098  std::string dummy4_s = with_dummy4.SerializePartialAsString();
3099  std::string dummy5_s = with_dummy5.SerializePartialAsString();
3100  int payload_size = dummy4_s.size() + dummy5_s.size();
3101  // Makes sure the payload size fits into one byte.
3102  ASSERT_LT(payload_size, 128);
3103
3104  std::string s(6, 0);
3105  char* p = &s[0];
3106  *p++ = WireFormatLite::MakeTag(1, WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
3107  // Length: 2B for key tag & val and 2B for val tag and length of the following
3108  // payload.
3109  *p++ = 4 + payload_size;
3110  *p++ = WireFormatLite::MakeTag(1, WireFormatLite::WIRETYPE_VARINT);
3111  *p++ = 0;
3112  *p++ = WireFormatLite::MakeTag(2, WireFormatLite::WIRETYPE_LENGTH_DELIMITED);
3113  *p++ = payload_size;
3114  absl::StrAppend(&s, dummy4_s, dummy5_s);
3115
3116  // Test key then value then value.
3117  int key = 0;
3118  ASSERT_TRUE(map_message.ParseFromString(s));
3119  ASSERT_EQ(1, map_message.map_field().size());
3120  ASSERT_EQ(1, map_message.map_field().count(key));
3121  EXPECT_EQ(97, map_message.map_field().find(key)->second.a());
3122  EXPECT_EQ(0, map_message.map_field().find(key)->second.b());
3123  EXPECT_EQ(33, map_message.map_field().find(key)->second.c());
3124  EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4());
3125  EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5());
3126
3127  // Test key then value then value then key.
3128  s.push_back(s[2]);  // Copy the key's tag.
3129  key = 19;
3130  s.push_back(key);  // Second key is 19 instead of 0.
3131  s[1] += 2;         // Adjust encoded size.
3132  ASSERT_TRUE(map_message.ParseFromString(s));
3133  ASSERT_EQ(1, map_message.map_field().size());
3134  ASSERT_EQ(1, map_message.map_field().count(key));
3135  EXPECT_EQ(97, map_message.map_field().find(key)->second.a());
3136  EXPECT_EQ(0, map_message.map_field().find(key)->second.b());
3137  EXPECT_EQ(33, map_message.map_field().find(key)->second.c());
3138  EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4());
3139  EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5());
3140}
3141
3142// Generated Message Reflection Test ================================
3143
3144TEST(GeneratedMapFieldReflectionTest, SpaceUsed) {
3145  UNITTEST::TestMap message;
3146  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3147  reflection_tester.SetMapFieldsViaReflection(&message);
3148
3149  EXPECT_LT(0, message.GetReflection()->SpaceUsedLong(message));
3150}
3151
3152TEST(GeneratedMapFieldReflectionTest, Accessors) {
3153  // Set every field to a unique value then go back and check all those
3154  // values.
3155  UNITTEST::TestMap message;
3156  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3157  reflection_tester.SetMapFieldsViaReflection(&message);
3158  MapTestUtil::ExpectMapFieldsSet(message);
3159  reflection_tester.ExpectMapFieldsSetViaReflection(message);
3160  reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message);
3161
3162  reflection_tester.ModifyMapFieldsViaReflection(&message);
3163  MapTestUtil::ExpectMapFieldsModified(message);
3164}
3165
3166TEST(GeneratedMapFieldReflectionTest,
3167     AccessingTheDefaultInstanceWithReflectionDoesNotModifyIt) {
3168  const auto& msg = UNITTEST::TestMap::default_instance();
3169  const auto clone_bytes = [&] {
3170    return std::string(reinterpret_cast<const char*>(&msg), sizeof(msg));
3171  };
3172  const auto before = clone_bytes();
3173  const auto& rep = msg.GetReflection()->GetRepeatedFieldRef<google::protobuf::Message>(
3174      msg, msg.GetDescriptor()->FindFieldByName("map_int32_int32"));
3175  EXPECT_THAT(rep, IsEmpty());
3176  const auto after = clone_bytes();
3177  EXPECT_EQ(before, after);
3178}
3179
3180TEST(GeneratedMapFieldReflectionTest, Swap) {
3181  UNITTEST::TestMap message1;
3182  UNITTEST::TestMap message2;
3183
3184  MapTestUtil::SetMapFields(&message1);
3185
3186  const Reflection* reflection = message1.GetReflection();
3187  reflection->Swap(&message1, &message2);
3188
3189  MapTestUtil::ExpectClear(message1);
3190  MapTestUtil::ExpectMapFieldsSet(message2);
3191}
3192
3193TEST(GeneratedMapFieldReflectionTest, SwapWithBothSet) {
3194  UNITTEST::TestMap message1;
3195  UNITTEST::TestMap message2;
3196
3197  MapTestUtil::SetMapFields(&message1);
3198  MapTestUtil::SetMapFields(&message2);
3199  MapTestUtil::ModifyMapFields(&message2);
3200
3201  const Reflection* reflection = message1.GetReflection();
3202  reflection->Swap(&message1, &message2);
3203
3204  MapTestUtil::ExpectMapFieldsModified(message1);
3205  MapTestUtil::ExpectMapFieldsSet(message2);
3206}
3207
3208TEST(GeneratedMapFieldReflectionTest, SwapFields) {
3209  UNITTEST::TestMap message1;
3210  UNITTEST::TestMap message2;
3211
3212  MapTestUtil::SetMapFields(&message2);
3213
3214  std::vector<const FieldDescriptor*> fields;
3215  const Reflection* reflection = message1.GetReflection();
3216  reflection->ListFields(message2, &fields);
3217  reflection->SwapFields(&message1, &message2, fields);
3218
3219  MapTestUtil::ExpectMapFieldsSet(message1);
3220  MapTestUtil::ExpectClear(message2);
3221}
3222
3223TEST(GeneratedMapFieldReflectionTest, ClearField) {
3224  UNITTEST::TestMap message;
3225  MapTestUtil::SetMapFields(&message);
3226  MapTestUtil::ExpectMapFieldsSet(message);
3227
3228  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3229  reflection_tester.ClearMapFieldsViaReflection(&message);
3230  reflection_tester.ExpectClearViaReflection(message);
3231  reflection_tester.ExpectClearViaReflectionIterator(&message);
3232}
3233
3234TEST(GeneratedMapFieldReflectionTest, RemoveLast) {
3235  UNITTEST::TestMap message;
3236  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3237
3238  MapTestUtil::SetMapFields(&message);
3239  MapTestUtil::ExpectMapsSize(message, 2);
3240  std::vector<const Message*> expected_entries =
3241      MapTestUtil::GetMapEntries(message, 0);
3242
3243  reflection_tester.RemoveLastMapsViaReflection(&message);
3244
3245  MapTestUtil::ExpectMapsSize(message, 1);
3246  std::vector<const Message*> remained_entries =
3247      MapTestUtil::GetMapEntries(message, 0);
3248  EXPECT_TRUE(expected_entries == remained_entries);
3249}
3250
3251TEST(GeneratedMapFieldReflectionTest, ReleaseLast) {
3252  UNITTEST::TestMap message;
3253  const Descriptor* descriptor = message.GetDescriptor();
3254  MapReflectionTester reflection_tester(descriptor);
3255
3256  MapTestUtil::SetMapFields(&message);
3257
3258  MapTestUtil::ExpectMapsSize(message, 2);
3259
3260  reflection_tester.ReleaseLastMapsViaReflection(&message);
3261
3262  MapTestUtil::ExpectMapsSize(message, 1);
3263
3264  // Now test that we actually release the right message.
3265  message.Clear();
3266  MapTestUtil::SetMapFields(&message);
3267
3268  MapTestUtil::ExpectMapsSize(message, 2);
3269  std::vector<const Message*> expect_last =
3270      MapTestUtil::GetMapEntries(message, 1);
3271  std::vector<const Message*> release_last =
3272      MapTestUtil::GetMapEntriesFromRelease(&message);
3273  MapTestUtil::ExpectMapsSize(message, 1);
3274  if (!internal::DebugHardenForceCopyInRelease()) {
3275    EXPECT_TRUE(expect_last == release_last);
3276  }
3277  for (std::vector<const Message*>::iterator it = release_last.begin();
3278       it != release_last.end(); ++it) {
3279    delete *it;
3280  }
3281}
3282
3283TEST(GeneratedMapFieldReflectionTest, SwapElements) {
3284  UNITTEST::TestMap message;
3285  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3286
3287  MapTestUtil::SetMapFields(&message);
3288
3289  // Get pointers of map entries at their original position
3290  std::vector<const Message*> entries0 = MapTestUtil::GetMapEntries(message, 0);
3291  std::vector<const Message*> entries1 = MapTestUtil::GetMapEntries(message, 1);
3292
3293  // Swap the first time.
3294  reflection_tester.SwapMapsViaReflection(&message);
3295
3296  // Get pointer of map entry after swap once.
3297  std::vector<const Message*> entries0_once =
3298      MapTestUtil::GetMapEntries(message, 0);
3299  std::vector<const Message*> entries1_once =
3300      MapTestUtil::GetMapEntries(message, 1);
3301
3302  // Test map entries are swapped.
3303  MapTestUtil::ExpectMapsSize(message, 2);
3304  EXPECT_TRUE(entries0 == entries1_once);
3305  EXPECT_TRUE(entries1 == entries0_once);
3306
3307  // Swap the second time.
3308  reflection_tester.SwapMapsViaReflection(&message);
3309
3310  // Get pointer of map entry after swap once.
3311  std::vector<const Message*> entries0_twice =
3312      MapTestUtil::GetMapEntries(message, 0);
3313  std::vector<const Message*> entries1_twice =
3314      MapTestUtil::GetMapEntries(message, 1);
3315
3316  // Test map entries are swapped back.
3317  MapTestUtil::ExpectMapsSize(message, 2);
3318  EXPECT_TRUE(entries0 == entries0_twice);
3319  EXPECT_TRUE(entries1 == entries1_twice);
3320}
3321
3322TEST(GeneratedMapFieldReflectionTest, MutableUnknownFields) {
3323  UNITTEST::TestMap message;
3324  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3325  reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
3326}
3327
3328TEST(GeneratedMapFieldReflectionTest, EmbedProto2Message) {
3329  UNITTEST::TestMessageMap message;
3330
3331  const FieldDescriptor* map_field =
3332      UNITTEST::TestMessageMap::descriptor()->FindFieldByName(
3333          "map_int32_message");
3334  const FieldDescriptor* value = map_field->message_type()->map_value();
3335
3336  Message* entry_message =
3337      message.GetReflection()->AddMessage(&message, map_field);
3338  EXPECT_EQ(
3339      &entry_message->GetReflection()->GetMessage(*entry_message, value),
3340      reinterpret_cast<const Message*>(&TestAllTypes::default_instance()));
3341
3342  Message* proto2_message =
3343      entry_message->GetReflection()->MutableMessage(entry_message, value);
3344  EXPECT_EQ(UNITTEST::TestAllTypes::descriptor(),
3345            proto2_message->GetDescriptor());
3346  ASSERT_EQ(1, message.map_int32_message().size());
3347}
3348
3349TEST(GeneratedMapFieldReflectionTest, MergeFromClearMapEntry) {
3350  UNITTEST::TestMap message;
3351  const FieldDescriptor* map_field =
3352      UNITTEST::TestMap::descriptor()->FindFieldByName("map_int32_int32");
3353  const FieldDescriptor* key = map_field->message_type()->map_key();
3354  const FieldDescriptor* value = map_field->message_type()->map_value();
3355
3356  Message* entry_message1 =
3357      message.GetReflection()->AddMessage(&message, map_field);
3358  EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
3359  EXPECT_FALSE(
3360      entry_message1->GetReflection()->HasField(*entry_message1, value));
3361
3362  Message* entry_message2 =
3363      message.GetReflection()->AddMessage(&message, map_field);
3364  EXPECT_FALSE(entry_message2->GetReflection()->HasField(*entry_message2, key));
3365  EXPECT_FALSE(
3366      entry_message2->GetReflection()->HasField(*entry_message2, value));
3367
3368  entry_message1->MergeFrom(*entry_message2);
3369  EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key));
3370  EXPECT_FALSE(
3371      entry_message1->GetReflection()->HasField(*entry_message1, value));
3372}
3373
3374TEST(GeneratedMapFieldReflectionTest, MapEntryClear) {
3375  UNITTEST::TestMap message;
3376  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3377  reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message);
3378}
3379
3380TEST(GeneratedMapFieldReflectionTest, Proto2MapEntryClear) {
3381  UNITTEST::TestEnumMap message;
3382  const Descriptor* descriptor = message.GetDescriptor();
3383  const FieldDescriptor* field_descriptor =
3384      descriptor->FindFieldByName("known_map_field");
3385  const FieldDescriptor* value_descriptor =
3386      field_descriptor->message_type()->map_value();
3387  Message* sub_message =
3388      message.GetReflection()->AddMessage(&message, field_descriptor);
3389  EXPECT_EQ(0, sub_message->GetReflection()->GetEnumValue(*sub_message,
3390                                                          value_descriptor));
3391}
3392
3393// Map Reflection API Test =========================================
3394
3395TEST(GeneratedMapFieldReflectionTest, SetViaMapReflection) {
3396  UNITTEST::TestMap message;
3397  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3398  reflection_tester.SetMapFieldsViaMapReflection(&message);
3399  reflection_tester.ExpectMapFieldsSetViaReflection(message);
3400  reflection_tester.ExpectMapFieldsSetViaReflectionIterator(&message);
3401}
3402
3403// Dynamic Message Test =============================================
3404
3405class MapFieldInDynamicMessageTest : public testing::Test {
3406 protected:
3407  const DescriptorPool* pool_;
3408  DynamicMessageFactory factory_;
3409  const Descriptor* map_descriptor_;
3410  const Descriptor* recursive_map_descriptor_;
3411  const Message* map_prototype_;
3412
3413  MapFieldInDynamicMessageTest()
3414      : pool_(DescriptorPool::generated_pool()), factory_(pool_) {}
3415
3416  void SetUp() override {
3417    map_descriptor_ = pool_->FindMessageTypeByName(
3418        absl::StrCat(UNITTEST_PACKAGE_NAME, ".TestMap"));
3419    recursive_map_descriptor_ = pool_->FindMessageTypeByName(
3420        absl::StrCat(UNITTEST_PACKAGE_NAME, ".TestRecursiveMapMessage"));
3421    ASSERT_TRUE(map_descriptor_ != nullptr);
3422    ASSERT_TRUE(recursive_map_descriptor_ != nullptr);
3423    map_prototype_ = factory_.GetPrototype(map_descriptor_);
3424  }
3425};
3426
3427TEST_F(MapFieldInDynamicMessageTest, MapIndependentOffsets) {
3428  // Check that all fields have independent offsets by setting each
3429  // one to a unique value then checking that they all still have those
3430  // unique values (i.e. they don't stomp each other).
3431  std::unique_ptr<Message> message(map_prototype_->New());
3432  MapReflectionTester reflection_tester(map_descriptor_);
3433
3434  reflection_tester.SetMapFieldsViaReflection(message.get());
3435  reflection_tester.ExpectMapFieldsSetViaReflection(*message);
3436}
3437
3438TEST_F(MapFieldInDynamicMessageTest, DynamicMapReflection) {
3439  // Check that map fields work properly.
3440  std::unique_ptr<Message> message(map_prototype_->New());
3441
3442  // Check set functions.
3443  MapReflectionTester reflection_tester(map_descriptor_);
3444  reflection_tester.SetMapFieldsViaMapReflection(message.get());
3445  reflection_tester.ExpectMapFieldsSetViaReflection(*message);
3446}
3447
3448TEST_F(MapFieldInDynamicMessageTest, MapSpaceUsed) {
3449  // Test that SpaceUsedLong() works properly
3450
3451  // Since we share the implementation with generated messages, we don't need
3452  // to test very much here.  Just make sure it appears to be working.
3453
3454  std::unique_ptr<Message> message(map_prototype_->New());
3455  MapReflectionTester reflection_tester(map_descriptor_);
3456
3457  int initial_space_used = message->SpaceUsedLong();
3458
3459  reflection_tester.SetMapFieldsViaReflection(message.get());
3460  EXPECT_LT(initial_space_used, message->SpaceUsedLong());
3461}
3462
3463TEST_F(MapFieldInDynamicMessageTest, RecursiveMap) {
3464  TestRecursiveMapMessage from;
3465  (*from.mutable_a())[""];
3466  std::string data = from.SerializeAsString();
3467  std::unique_ptr<Message> to(
3468      factory_.GetPrototype(recursive_map_descriptor_)->New());
3469  ASSERT_TRUE(to->ParseFromString(data));
3470}
3471
3472TEST_F(MapFieldInDynamicMessageTest, MapValueReferernceValidAfterSerialize) {
3473  std::unique_ptr<Message> message(map_prototype_->New());
3474  MapReflectionTester reflection_tester(map_descriptor_);
3475  reflection_tester.SetMapFieldsViaMapReflection(message.get());
3476
3477  // Get value reference before serialization, so that we know the value is from
3478  // map.
3479  MapKey map_key;
3480  MapValueRef map_val;
3481  map_key.SetInt32Value(0);
3482  reflection_tester.GetMapValueViaMapReflection(
3483      message.get(), "map_int32_foreign_message", map_key, &map_val);
3484  Message* submsg = map_val.MutableMessageValue();
3485
3486  // In previous implementation, calling SerializeToString will cause syncing
3487  // from map to repeated field, which will invalidate the submsg we previously
3488  // got.
3489  std::string data;
3490  message->SerializeToString(&data);
3491
3492  const Reflection* submsg_reflection = submsg->GetReflection();
3493  const Descriptor* submsg_desc = submsg->GetDescriptor();
3494  const FieldDescriptor* submsg_field = submsg_desc->FindFieldByName("c");
3495  submsg_reflection->SetInt32(submsg, submsg_field, 128);
3496
3497  message->SerializeToString(&data);
3498  TestMap to;
3499  ASSERT_TRUE(to.ParseFromString(data));
3500  EXPECT_EQ(128, to.map_int32_foreign_message().at(0).c());
3501}
3502
3503TEST_F(MapFieldInDynamicMessageTest, MapEntryReferernceValidAfterSerialize) {
3504  std::unique_ptr<Message> message(map_prototype_->New());
3505  MapReflectionTester reflection_tester(map_descriptor_);
3506  reflection_tester.SetMapFieldsViaReflection(message.get());
3507
3508  // Get map entry before serialization, so that we know the it is from
3509  // repeated field.
3510  Message* map_entry = reflection_tester.GetMapEntryViaReflection(
3511      message.get(), "map_int32_foreign_message", 0);
3512  const Reflection* map_entry_reflection = map_entry->GetReflection();
3513  const Descriptor* map_entry_desc = map_entry->GetDescriptor();
3514  const FieldDescriptor* value_field = map_entry_desc->map_value();
3515  Message* submsg =
3516      map_entry_reflection->MutableMessage(map_entry, value_field);
3517
3518  // In previous implementation, calling SerializeToString will cause syncing
3519  // from repeated field to map, which will invalidate the map_entry we
3520  // previously got.
3521  std::string data;
3522  message->SerializeToString(&data);
3523
3524  const Reflection* submsg_reflection = submsg->GetReflection();
3525  const Descriptor* submsg_desc = submsg->GetDescriptor();
3526  const FieldDescriptor* submsg_field = submsg_desc->FindFieldByName("c");
3527  submsg_reflection->SetInt32(submsg, submsg_field, 128);
3528
3529  message->SerializeToString(&data);
3530  TestMap to;
3531  to.ParseFromString(data);
3532  EXPECT_EQ(128, to.map_int32_foreign_message().at(0).c());
3533}
3534
3535// ReflectionOps Test ===============================================
3536
3537TEST(ReflectionOpsForMapFieldTest, MapSanityCheck) {
3538  UNITTEST::TestMap message;
3539
3540  MapTestUtil::SetMapFields(&message);
3541  MapTestUtil::ExpectMapFieldsSet(message);
3542}
3543
3544TEST(ReflectionOpsForMapFieldTest, MapCopy) {
3545  UNITTEST::TestMap message, message2;
3546
3547  MapTestUtil::SetMapFields(&message);
3548
3549  ReflectionOps::Copy(message, &message2);
3550
3551  MapTestUtil::ExpectMapFieldsSet(message2);
3552
3553  // Copying from self should be a no-op.
3554  ReflectionOps::Copy(message2, &message2);
3555  MapTestUtil::ExpectMapFieldsSet(message2);
3556}
3557
3558TEST(ReflectionOpsForMapFieldTest, MergeMap) {
3559  // Note:  Copy is implemented in terms of Merge() so technically the Copy
3560  //   test already tested most of this.
3561
3562  UNITTEST::TestMap message, message2;
3563
3564  MapTestUtil::SetMapFields(&message);
3565
3566  ReflectionOps::Merge(message2, &message);
3567
3568  MapTestUtil::ExpectMapFieldsSet(message);
3569}
3570
3571TEST(ReflectionOpsForMapFieldTest, ClearMap) {
3572  UNITTEST::TestMap message;
3573
3574  MapTestUtil::SetMapFields(&message);
3575
3576  ReflectionOps::Clear(&message);
3577
3578  MapTestUtil::ExpectClear(message);
3579}
3580
3581TEST(ReflectionOpsForMapFieldTest, MapDiscardUnknownFields) {
3582  UNITTEST::TestMap message;
3583  MapTestUtil::SetMapFields(&message);
3584
3585  // Set some unknown fields in message.
3586  message.GetReflection()->MutableUnknownFields(&message)->AddVarint(123456,
3587                                                                     654321);
3588
3589  // Discard them.
3590  ReflectionOps::DiscardUnknownFields(&message);
3591  MapTestUtil::ExpectMapFieldsSet(message);
3592
3593  EXPECT_EQ(0,
3594            message.GetReflection()->GetUnknownFields(message).field_count());
3595}
3596
3597TEST(ReflectionOpsForMapFieldTest, IsInitialized) {
3598  UNITTEST::TestRequiredMessageMap map_message;
3599
3600  // Add an uninitialized message.
3601  (*map_message.mutable_map_field())[0];
3602  EXPECT_FALSE(ReflectionOps::IsInitialized(map_message));
3603
3604  // Initialize uninitialized message
3605  (*map_message.mutable_map_field())[0].set_a(0);
3606  (*map_message.mutable_map_field())[0].set_b(0);
3607  (*map_message.mutable_map_field())[0].set_c(0);
3608  EXPECT_TRUE(ReflectionOps::IsInitialized(map_message));
3609}
3610
3611// Wire Format Test =================================================
3612
3613TEST(WireFormatForMapFieldTest, ParseMap) {
3614  UNITTEST::TestMap source, dest;
3615  std::string data;
3616
3617  // Serialize using the generated code.
3618  MapTestUtil::SetMapFields(&source);
3619  source.SerializeToString(&data);
3620
3621  // Parse using WireFormat.
3622  io::ArrayInputStream raw_input(data.data(), data.size());
3623  io::CodedInputStream input(&raw_input);
3624  WireFormat::ParseAndMergePartial(&input, &dest);
3625
3626  // Check.
3627  MapTestUtil::ExpectMapFieldsSet(dest);
3628}
3629
3630TEST(WireFormatForMapFieldTest, MapByteSize) {
3631  UNITTEST::TestMap message;
3632  MapTestUtil::SetMapFields(&message);
3633
3634  EXPECT_EQ(message.ByteSizeLong(), WireFormat::ByteSize(message));
3635  message.Clear();
3636  EXPECT_EQ(0, message.ByteSizeLong());
3637  EXPECT_EQ(0, WireFormat::ByteSize(message));
3638}
3639
3640TEST(WireFormatForMapFieldTest, SerializeMap) {
3641  UNITTEST::TestMap message;
3642  std::string generated_data;
3643  std::string dynamic_data;
3644
3645  MapTestUtil::SetMapFields(&message);
3646
3647  // Serialize using the generated code.
3648  {
3649    message.ByteSizeLong();
3650    io::StringOutputStream raw_output(&generated_data);
3651    io::CodedOutputStream output(&raw_output);
3652    message.SerializeWithCachedSizes(&output);
3653    ASSERT_FALSE(output.HadError());
3654  }
3655
3656  // Serialize using WireFormat.
3657  {
3658    io::StringOutputStream raw_output(&dynamic_data);
3659    io::CodedOutputStream output(&raw_output);
3660    size_t size = WireFormat::ByteSize(message);
3661    WireFormat::SerializeWithCachedSizes(message, size, &output);
3662    ASSERT_FALSE(output.HadError());
3663  }
3664
3665  // Should parse to the same message.
3666  EXPECT_TRUE(TestUtil::EqualsToSerialized(message, generated_data));
3667  EXPECT_TRUE(TestUtil::EqualsToSerialized(message, dynamic_data));
3668}
3669
3670TEST(WireFormatForMapFieldTest, SerializeMapDynamicMessage) {
3671  DynamicMessageFactory factory;
3672  std::unique_ptr<Message> dynamic_message;
3673  dynamic_message.reset(
3674      factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
3675  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3676  reflection_tester.SetMapFieldsViaReflection(dynamic_message.get());
3677  reflection_tester.ExpectMapFieldsSetViaReflection(*dynamic_message);
3678
3679  UNITTEST::TestMap generated_message;
3680  MapTestUtil::SetMapFields(&generated_message);
3681  MapTestUtil::ExpectMapFieldsSet(generated_message);
3682
3683  std::string generated_data;
3684  std::string dynamic_data;
3685
3686  // Serialize.
3687  generated_message.SerializeToString(&generated_data);
3688  dynamic_message->SerializeToString(&dynamic_data);
3689
3690  // Because map serialization doesn't guarantee order, we just compare
3691  // serialized size here. This is enough to tell dynamic message doesn't miss
3692  // anything in serialization.
3693  EXPECT_TRUE(dynamic_data.size() == generated_data.size());
3694}
3695
3696TEST(WireFormatForMapFieldTest, MapByteSizeDynamicMessage) {
3697  DynamicMessageFactory factory;
3698  std::unique_ptr<Message> dynamic_message;
3699  dynamic_message.reset(
3700      factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
3701  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
3702  reflection_tester.SetMapFieldsViaReflection(dynamic_message.get());
3703  reflection_tester.ExpectMapFieldsSetViaReflection(*dynamic_message);
3704  std::string expected_serialized_data;
3705  dynamic_message->SerializeToString(&expected_serialized_data);
3706  int expected_size = expected_serialized_data.size();
3707  EXPECT_EQ(dynamic_message->ByteSizeLong(), expected_size);
3708  TestMap expected_message;
3709  expected_message.ParseFromString(expected_serialized_data);
3710
3711  std::unique_ptr<Message> message2;
3712  message2.reset(factory.GetPrototype(UNITTEST::TestMap::descriptor())->New());
3713  reflection_tester.SetMapFieldsViaMapReflection(message2.get());
3714
3715  const FieldDescriptor* field =
3716      UNITTEST::TestMap::descriptor()->FindFieldByName("map_int32_int32");
3717  const Reflection* reflection = dynamic_message->GetReflection();
3718
3719  // Force the map field to mark with STATE_MODIFIED_REPEATED
3720  reflection->RemoveLast(dynamic_message.get(), field);
3721  dynamic_message->MergeFrom(*message2);
3722  dynamic_message->MergeFrom(*message2);
3723  // The map field is marked as STATE_MODIFIED_REPEATED, ByteSizeLong() will use
3724  // repeated field which have duplicate keys to calculate.
3725  size_t duplicate_size = dynamic_message->ByteSizeLong();
3726  EXPECT_TRUE(duplicate_size > expected_size);
3727  std::string duplicate_serialized_data;
3728  dynamic_message->SerializeToString(&duplicate_serialized_data);
3729  EXPECT_EQ(dynamic_message->ByteSizeLong(), duplicate_serialized_data.size());
3730
3731  // Force the map field to mark with map CLEAN
3732  auto& msg = *dynamic_message;
3733  EXPECT_EQ(reflection_tester.MapSize(msg, "map_int32_int32"), 2);
3734  EXPECT_EQ(reflection_tester.MapSize(msg, "map_int32_int32"), 2);
3735  EXPECT_EQ(reflection_tester.MapSize(msg, "map_int64_int64"), 2);
3736  EXPECT_EQ(reflection_tester.MapSize(msg, "map_uint32_uint32"), 2);
3737  EXPECT_EQ(reflection_tester.MapSize(msg, "map_uint64_uint64"), 2);
3738  EXPECT_EQ(reflection_tester.MapSize(msg, "map_sint32_sint32"), 2);
3739  EXPECT_EQ(reflection_tester.MapSize(msg, "map_sint64_sint64"), 2);
3740  EXPECT_EQ(reflection_tester.MapSize(msg, "map_fixed32_fixed32"), 2);
3741  EXPECT_EQ(reflection_tester.MapSize(msg, "map_fixed64_fixed64"), 2);
3742  EXPECT_EQ(reflection_tester.MapSize(msg, "map_sfixed32_sfixed32"), 2);
3743  EXPECT_EQ(reflection_tester.MapSize(msg, "map_sfixed64_sfixed64"), 2);
3744  EXPECT_EQ(reflection_tester.MapSize(msg, "map_int32_float"), 2);
3745  EXPECT_EQ(reflection_tester.MapSize(msg, "map_int32_double"), 2);
3746  EXPECT_EQ(reflection_tester.MapSize(msg, "map_bool_bool"), 2);
3747  EXPECT_EQ(reflection_tester.MapSize(msg, "map_string_string"), 2);
3748  EXPECT_EQ(reflection_tester.MapSize(msg, "map_int32_bytes"), 2);
3749  EXPECT_EQ(reflection_tester.MapSize(msg, "map_int32_enum"), 2);
3750  EXPECT_EQ(reflection_tester.MapSize(msg, "map_int32_foreign_message"), 2);
3751
3752  // The map field is marked as CLEAN, ByteSizeLong() will use map which do not
3753  // have duplicate keys to calculate.
3754  int size = dynamic_message->ByteSizeLong();
3755  EXPECT_EQ(expected_size, size);
3756
3757  // Protobuf used to have a bug for serialize when map it marked CLEAN. It used
3758  // repeated field to calculate ByteSizeLong but use map to serialize the real
3759  // data, thus the ByteSizeLong may bigger than real serialized size. A crash
3760  // might be happen at SerializeToString(). Or an "unexpected end group"
3761  // warning was raised at parse back if user use SerializeWithCachedSizes()
3762  // which avoids size check at serialize.
3763  std::string serialized_data;
3764  dynamic_message->SerializeToString(&serialized_data);
3765  EXPECT_TRUE(dynamic_message->ParseFromString(serialized_data));
3766}
3767
3768TEST(WireFormatForMapFieldTest, MapParseHelpers) {
3769  std::string data;
3770
3771  {
3772    // Set up.
3773    UNITTEST::TestMap message;
3774    MapTestUtil::SetMapFields(&message);
3775    message.SerializeToString(&data);
3776  }
3777
3778  {
3779    // Test ParseFromString.
3780    UNITTEST::TestMap message;
3781    EXPECT_TRUE(message.ParseFromString(data));
3782    MapTestUtil::ExpectMapFieldsSet(message);
3783  }
3784
3785  {
3786    // Test ParseFromIstream.
3787    UNITTEST::TestMap message;
3788    std::stringstream stream(data);
3789    EXPECT_TRUE(message.ParseFromIstream(&stream));
3790    EXPECT_TRUE(stream.eof());
3791    MapTestUtil::ExpectMapFieldsSet(message);
3792  }
3793
3794  {
3795    // Test ParseFromBoundedZeroCopyStream.
3796    std::string data_with_junk(data);
3797    data_with_junk.append("some junk on the end");
3798    io::ArrayInputStream stream(data_with_junk.data(), data_with_junk.size());
3799    UNITTEST::TestMap message;
3800    EXPECT_TRUE(message.ParseFromBoundedZeroCopyStream(&stream, data.size()));
3801    MapTestUtil::ExpectMapFieldsSet(message);
3802  }
3803
3804  {
3805    // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if
3806    // EOF is reached before the expected number of bytes.
3807    io::ArrayInputStream stream(data.data(), data.size());
3808    UNITTEST::TestAllTypes message;
3809    EXPECT_FALSE(
3810        message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1));
3811  }
3812}
3813
3814std::string WriteVarint(int number, uint64_t v) {
3815  uint8_t buf[16];
3816  return std::string(buf, WireFormatLite::WriteUInt64ToArray(number, v, buf));
3817}
3818
3819std::string WriteString(int number, const std::string& str) {
3820  uint8_t buf[100];
3821  return std::string(buf, WireFormatLite::WriteStringToArray(number, str, buf));
3822}
3823
3824TEST(WireFormatForMapFieldTest, BoolWorksWithOverlongValues) {
3825  // map<bool, bool> map_bool_bool = 13;
3826  for (uint64_t v : {uint64_t{1}, uint64_t{1000}, uint64_t{100000},
3827                     uint64_t{1} << 32, uint64_t{1} << 63}) {
3828    SCOPED_TRACE(v);
3829    std::string payload =
3830        WriteString(13, WriteVarint(1, v) + WriteVarint(2, v));
3831    UNITTEST::TestMap obj;
3832    ASSERT_TRUE(obj.ParseFromString(payload));
3833    EXPECT_THAT(obj.map_bool_bool(), ElementsAre(Pair(true, true)));
3834
3835    io::ArrayInputStream raw_input(payload.data(), payload.size());
3836    io::CodedInputStream input(&raw_input);
3837    obj.Clear();
3838    ASSERT_TRUE(WireFormat::ParseAndMergePartial(&input, &obj));
3839    EXPECT_THAT(obj.map_bool_bool(), ElementsAre(Pair(true, true)));
3840  }
3841}
3842
3843// Deterministic Serialization Test ==========================================
3844
3845template <typename T>
3846static std::string DeterministicSerializationWithSerializePartialToCodedStream(
3847    const T& t) {
3848  const size_t size = t.ByteSizeLong();
3849  std::string result(size, '\0');
3850  io::ArrayOutputStream array_stream(&result[0], size);
3851  io::CodedOutputStream output_stream(&array_stream);
3852  output_stream.SetSerializationDeterministic(true);
3853  t.SerializePartialToCodedStream(&output_stream);
3854  EXPECT_FALSE(output_stream.HadError());
3855  EXPECT_EQ(size, output_stream.ByteCount());
3856  return result;
3857}
3858
3859template <typename T>
3860static std::string DeterministicSerializationWithSerializeToCodedStream(
3861    const T& t) {
3862  const size_t size = t.ByteSizeLong();
3863  std::string result(size, '\0');
3864  io::ArrayOutputStream array_stream(&result[0], size);
3865  io::CodedOutputStream output_stream(&array_stream);
3866  output_stream.SetSerializationDeterministic(true);
3867  t.SerializeToCodedStream(&output_stream);
3868  EXPECT_FALSE(output_stream.HadError());
3869  EXPECT_EQ(size, output_stream.ByteCount());
3870  return result;
3871}
3872
3873template <typename T>
3874static std::string DeterministicSerialization(const T& t) {
3875  const size_t size = t.ByteSizeLong();
3876  std::string result(size, '\0');
3877  io::ArrayOutputStream array_stream(&result[0], size);
3878  {
3879    io::CodedOutputStream output_stream(&array_stream);
3880    output_stream.SetSerializationDeterministic(true);
3881    t.SerializeWithCachedSizes(&output_stream);
3882    EXPECT_FALSE(output_stream.HadError());
3883    EXPECT_EQ(size, output_stream.ByteCount());
3884  }
3885  EXPECT_EQ(result, DeterministicSerializationWithSerializeToCodedStream(t));
3886  EXPECT_EQ(result,
3887            DeterministicSerializationWithSerializePartialToCodedStream(t));
3888  return result;
3889}
3890
3891// Helper for MapSerializationTest.  Return a 7-bit ASCII string.
3892static std::string ConstructKey(uint64_t n) {
3893  std::string s(n % static_cast<uint64_t>(9), '\0');
3894  if (s.empty()) {
3895    return absl::StrCat(n);
3896  } else {
3897    while (n != 0) {
3898      s[n % s.size()] = (n >> 10) & 0x7f;
3899      n /= 888;
3900    }
3901    return s;
3902  }
3903}
3904
3905TEST(MapSerializationTest, Deterministic) {
3906  const int kIters = 25;
3907  UNITTEST::TestMaps t;
3908  UNITTEST::TestIntIntMap inner;
3909  (*inner.mutable_m())[0] = (*inner.mutable_m())[10] =
3910      (*inner.mutable_m())[-200] = 0;
3911  uint64_t frog = 9;
3912  const uint64_t multiplier = 0xa29cd16f;
3913  for (int i = 0; i < kIters; i++) {
3914    const int32_t i32 = static_cast<int32_t>(frog & 0xffffffff);
3915    const uint32_t u32 = static_cast<uint32_t>(i32) * 91919;
3916    const int64_t i64 = static_cast<int64_t>(frog);
3917    const uint64_t u64 = frog * static_cast<uint64_t>(187321);
3918    const bool b = i32 > 0;
3919    const std::string s = ConstructKey(frog);
3920    (*inner.mutable_m())[i] = i32;
3921    (*t.mutable_m_int32())[i32] = (*t.mutable_m_sint32())[i32] =
3922        (*t.mutable_m_sfixed32())[i32] = inner;
3923    (*t.mutable_m_uint32())[u32] = (*t.mutable_m_fixed32())[u32] = inner;
3924    (*t.mutable_m_int64())[i64] = (*t.mutable_m_sint64())[i64] =
3925        (*t.mutable_m_sfixed64())[i64] = inner;
3926    (*t.mutable_m_uint64())[u64] = (*t.mutable_m_fixed64())[u64] = inner;
3927    (*t.mutable_m_bool())[b] = inner;
3928    (*t.mutable_m_string())[s] = inner;
3929    (*t.mutable_m_string())[s + std::string(
3930                                    1 << (u32 % static_cast<uint32_t>(9)), b)] =
3931        inner;
3932    inner.mutable_m()->erase(i);
3933    frog = frog * multiplier + i;
3934    frog ^= (frog >> 41);
3935  }
3936
3937  // Verifies if two consecutive calls to deterministic serialization produce
3938  // the same bytes. Deterministic serialization means the same serialization
3939  // bytes in the same binary.
3940  const std::string s1 = DeterministicSerialization(t);
3941  const std::string s2 = DeterministicSerialization(t);
3942  EXPECT_EQ(s1, s2);
3943
3944  UNITTEST::TestMaps u;
3945  EXPECT_TRUE(u.ParseFromString(s1));
3946  EXPECT_TRUE(util::MessageDifferencer::Equals(u, t));
3947}
3948
3949static std::string GetGoldenMessageTextProto() {
3950  static std::string* golden_message_textproto = [] {
3951    std::string* textproto = new std::string;
3952    ABSL_CHECK_OK(File::GetContents(
3953        TestUtil::GetTestDataPath("google/protobuf/"
3954                                  "testdata/map_test_data.txt"),
3955        textproto, true));
3956    return textproto;
3957  }();
3958  return *golden_message_textproto;
3959}
3960
3961static std::string GetGoldenMessageBinary() {
3962  static std::string* golden_message_binary = [] {
3963    UNITTEST::TestMaps t;
3964    TextFormat::Parser parser;
3965    parser.ParseFromString(GetGoldenMessageTextProto(), &t);
3966
3967    std::string* result = new std::string;
3968    t.SerializeToString(result);
3969    return result;
3970  }();
3971  return *golden_message_binary;
3972}
3973
3974TEST(MapSerializationTest, DeterministicSubmessage) {
3975  UNITTEST::TestSubmessageMaps p;
3976  UNITTEST::TestMaps t;
3977  t.ParseFromString(GetGoldenMessageBinary());
3978  *(p.mutable_m()) = t;
3979  std::vector<std::string> v;
3980  // Use multiple attempts to increase the chance of a failure if something is
3981  // buggy.  For example, each separate copy of a map might use a different
3982  // randomly-chosen hash function.
3983  const int kAttempts = 10;
3984  for (int i = 0; i < kAttempts; i++) {
3985    // NOLINTNEXTLINE(performance-unnecessary-copy-initialization)
3986    UNITTEST::TestSubmessageMaps q(p);
3987    ASSERT_EQ(DeterministicSerialization(q), DeterministicSerialization(p));
3988  }
3989}
3990
3991// Text Format Test =================================================
3992
3993TEST(TextFormatMapTest, SerializeAndParse) {
3994  UNITTEST::TestMap source;
3995  UNITTEST::TestMap dest;
3996  MapTestUtil::SetMapFields(&source);
3997  std::string output;
3998
3999  // Test compact ASCII
4000  TextFormat::Printer printer;
4001  printer.PrintToString(source, &output);
4002  TextFormat::Parser parser;
4003  EXPECT_TRUE(parser.ParseFromString(output, &dest));
4004  MapTestUtil::ExpectMapFieldsSet(dest);
4005}
4006
4007TEST(TextFormatMapTest, DynamicMessage) {
4008  TestMap prototype;
4009  DynamicMessageFactory factory;
4010  std::unique_ptr<Message> message(
4011      factory.GetPrototype(prototype.GetDescriptor())->New());
4012  MapReflectionTester tester(message->GetDescriptor());
4013  tester.SetMapFieldsViaReflection(message.get());
4014
4015  std::string actual_text;
4016  TextFormat::PrintToString(*message, &actual_text);
4017  EXPECT_EQ(actual_text, GetGoldenMessageTextProto());
4018}
4019
4020TEST(TextFormatMapTest, Sorted) {
4021  UNITTEST::TestMap message;
4022  MapReflectionTester tester(message.GetDescriptor());
4023  tester.SetMapFieldsViaReflection(&message);
4024
4025  TextFormat::Printer printer;
4026  std::string actual_text;
4027  printer.PrintToString(message, &actual_text);
4028  EXPECT_EQ(actual_text, GetGoldenMessageTextProto());
4029
4030  // Test again on the reverse order.
4031  UNITTEST::TestMap message2;
4032  tester.SetMapFieldsViaReflection(&message2);
4033  tester.SwapMapsViaReflection(&message2);
4034  printer.PrintToString(message2, &actual_text);
4035  EXPECT_EQ(actual_text, GetGoldenMessageTextProto());
4036}
4037
4038// Previously, serializing to text format will disable iterator from generated
4039// API. Now, the iterator can be still used even after serializing to text
4040// format.
4041TEST(TextFormatMapTest, NoDisableIterator) {
4042  UNITTEST::TestMap source;
4043  (*source.mutable_map_int32_int32())[1] = 1;
4044
4045  // Get iterator.
4046  Map<int32_t, int32_t>::iterator iter =
4047      source.mutable_map_int32_int32()->find(1);
4048
4049  // Serialize message to text format, which will invalidate the previous
4050  // iterator previously.
4051  std::string output;
4052  TextFormat::Printer printer;
4053  printer.PrintToString(source, &output);
4054
4055  // Modify map via the iterator (invalidated in previous implementation.).
4056  iter->second = 2;
4057
4058  // In previous implementation, the new change won't be reflected in text
4059  // format, because the previous iterator has been invalidated.
4060  output.clear();
4061  printer.PrintToString(source, &output);
4062  std::string expected =
4063      "map_int32_int32 {\n"
4064      "  key: 1\n"
4065      "  value: 2\n"
4066      "}\n";
4067  EXPECT_EQ(output, expected);
4068}
4069
4070// Previously, serializing to text format will disable iterator from reflection
4071// API.
4072TEST(TextFormatMapTest, NoDisableReflectionIterator) {
4073  UNITTEST::TestMap source;
4074  (*source.mutable_map_int32_int32())[1] = 1;
4075
4076  // Get iterator. This will also sync internal repeated field with map inside
4077  // of MapField.
4078  const Reflection* reflection = source.GetReflection();
4079  const FieldDescriptor* field_desc =
4080      source.GetDescriptor()->FindFieldByName("map_int32_int32");
4081  RepeatedPtrField<Message>* map_field =
4082      reflection->MutableRepeatedPtrField<Message>(&source, field_desc);
4083  RepeatedPtrField<Message>::iterator iter = map_field->begin();
4084
4085  // Serialize message to text format, which will invalidate the previous
4086  // iterator previously.
4087  std::string output;
4088  TextFormat::Printer printer;
4089  printer.PrintToString(source, &output);
4090
4091  // Modify map via the iterator (invalidated in previous implementation.).
4092  const Reflection* map_entry_reflection = iter->GetReflection();
4093  const FieldDescriptor* value_field_desc = iter->GetDescriptor()->map_value();
4094  map_entry_reflection->SetInt32(&(*iter), value_field_desc, 2);
4095  ABSL_LOG(INFO) << iter->DebugString();
4096
4097  // In previous implementation, the new change won't be reflected in text
4098  // format, because the previous iterator has been invalidated.
4099  output.clear();
4100  printer.PrintToString(source, &output);
4101  std::string expected =
4102      "map_int32_int32 {\n"
4103      "  key: 1\n"
4104      "  value: 2\n"
4105      "}\n";
4106  EXPECT_EQ(output, expected);
4107}
4108
4109// arena support =================================================
4110TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) {
4111  // Allocate a large initial block to avoid mallocs during hooked test.
4112  std::vector<char> arena_block(128 * 1024);
4113  ArenaOptions options;
4114  options.initial_block = &arena_block[0];
4115  options.initial_block_size = arena_block.size();
4116  Arena arena(options);
4117  std::string data;
4118  data.reserve(128 * 1024);
4119
4120  {
4121    // TODO: Enable no heap check when ArenaStringPtr is used in map.
4122    // NoHeapChecker no_heap;
4123
4124    UNITTEST::TestArenaMap* from =
4125        Arena::Create<UNITTEST::TestArenaMap>(&arena);
4126    MapTestUtil::SetArenaMapFields(from);
4127    from->SerializeToString(&data);
4128
4129    UNITTEST::TestArenaMap* to = Arena::Create<UNITTEST::TestArenaMap>(&arena);
4130    to->ParseFromString(data);
4131    MapTestUtil::ExpectArenaMapFieldsSet(*to);
4132  }
4133}
4134
4135TEST(ArenaTest, SubmessageOnSameArena) {
4136  Arena arena;
4137  for (Arena* arena_to_use : {&arena, static_cast<Arena*>(nullptr)}) {
4138    ArenaHolder<UNITTEST::TestArenaMap> m(arena_to_use);
4139    auto* subm = &(*m->mutable_map_int32_foreign_message())[0];
4140    EXPECT_EQ(subm->GetArena(), arena_to_use);
4141  }
4142}
4143
4144// Use text format parsing and serializing to test reflection api.
4145TEST(ArenaTest, ReflectionInTextFormat) {
4146  Arena arena;
4147  std::string data;
4148
4149  TextFormat::Printer printer;
4150  TextFormat::Parser parser;
4151
4152  UNITTEST::TestArenaMap* from = Arena::Create<UNITTEST::TestArenaMap>(&arena);
4153  UNITTEST::TestArenaMap* to = Arena::Create<UNITTEST::TestArenaMap>(&arena);
4154
4155  MapTestUtil::SetArenaMapFields(from);
4156  printer.PrintToString(*from, &data);
4157
4158  EXPECT_TRUE(parser.ParseFromString(data, to));
4159  MapTestUtil::ExpectArenaMapFieldsSet(*to);
4160}
4161
4162// Make sure the memory allocated for string in map is deallocated.
4163TEST(ArenaTest, StringMapNoLeak) {
4164  Arena arena;
4165  UNITTEST::TestArenaMap* message =
4166      Arena::Create<UNITTEST::TestArenaMap>(&arena);
4167  std::string data;
4168  // String with length less than 16 will not be allocated from heap.
4169  int original_capacity = data.capacity();
4170  while (data.capacity() <= original_capacity) {
4171    data.append("a");
4172  }
4173  (*message->mutable_map_string_string())[data] = data;
4174  // Now we recreate the map via parsing
4175  message->ParseFromString(message->SerializeAsString());
4176  // And we cause value strings to grow to make sure they are handled correctly.
4177  std::string& value = (*message->mutable_map_string_string())[data];
4178  ASSERT_EQ(value, data);
4179  // Consume all the capacity.
4180  while (value.size() < value.capacity()) value.append("a");
4181  // And then grow the value.
4182  value.append("a");
4183}
4184
4185TEST(ArenaTest, IsInitialized) {
4186  // Allocate a large initial polluted block.
4187  std::vector<char> arena_block(128 * 1024);
4188  std::fill(arena_block.begin(), arena_block.end(), '\xff');
4189
4190  ArenaOptions options;
4191  options.initial_block = &arena_block[0];
4192  options.initial_block_size = arena_block.size();
4193  Arena arena(options);
4194
4195  UNITTEST::TestArenaMap* message =
4196      Arena::Create<UNITTEST::TestArenaMap>(&arena);
4197  EXPECT_EQ(0, (*message->mutable_map_int32_int32())[0]);
4198}
4199
4200TEST(ArenaTest, DynamicMapFieldOnArena) {
4201  Arena arena;
4202  UNITTEST::TestMap message2;
4203
4204  DynamicMessageFactory factory;
4205  Message* message1 =
4206      factory.GetPrototype(UNITTEST::TestMap::descriptor())->New(&arena);
4207  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
4208  reflection_tester.SetMapFieldsViaReflection(message1);
4209  reflection_tester.ExpectMapFieldsSetViaReflection(*message1);
4210  reflection_tester.ExpectMapFieldsSetViaReflectionIterator(message1);
4211  message2.CopyFrom(*message1);
4212  MapTestUtil::ExpectMapFieldsSet(message2);
4213}
4214
4215TEST(ArenaTest, DynamicMapFieldOnArenaMemoryLeak) {
4216  auto* desc = UNITTEST::TestMap::descriptor();
4217  auto* field = desc->FindFieldByName("map_int32_int32");
4218
4219  Arena arena;
4220  DynamicMessageFactory factory;
4221  auto* message = factory.GetPrototype(desc)->New(&arena);
4222  auto* reflection = message->GetReflection();
4223  reflection->AddMessage(message, field);
4224
4225  // Force internal syncing, which initializes the mutex.
4226  MapReflectionTester reflection_tester(UNITTEST::TestMap::descriptor());
4227  int size = reflection_tester.MapSize(*message, "map_int32_int32");
4228  EXPECT_EQ(size, 1);
4229}
4230
4231TEST(MoveTest, MoveConstructorWorks) {
4232  Map<int32_t, TestAllTypes> original_map;
4233  original_map[42].mutable_optional_nested_message()->set_bb(42);
4234  original_map[43].mutable_optional_nested_message()->set_bb(43);
4235  const auto* nested_msg42_ptr = &original_map[42].optional_nested_message();
4236  const auto* nested_msg43_ptr = &original_map[43].optional_nested_message();
4237
4238  Map<int32_t, TestAllTypes> moved_to_map(std::move(original_map));
4239  EXPECT_TRUE(original_map.empty());
4240  EXPECT_EQ(2, moved_to_map.size());
4241  EXPECT_EQ(42, moved_to_map[42].optional_nested_message().bb());
4242  EXPECT_EQ(43, moved_to_map[43].optional_nested_message().bb());
4243  // This test takes advantage of the fact that pointers are swapped, so there
4244  // should be pointer stability.
4245  EXPECT_EQ(nested_msg42_ptr, &moved_to_map[42].optional_nested_message());
4246  EXPECT_EQ(nested_msg43_ptr, &moved_to_map[43].optional_nested_message());
4247}
4248
4249TEST(MoveTest, MoveAssignmentWorks) {
4250  Map<int32_t, TestAllTypes> original_map;
4251  original_map[42].mutable_optional_nested_message()->set_bb(42);
4252  original_map[43].mutable_optional_nested_message()->set_bb(43);
4253  const auto* nested_msg42_ptr = &original_map[42].optional_nested_message();
4254  const auto* nested_msg43_ptr = &original_map[43].optional_nested_message();
4255
4256  Map<int32_t, TestAllTypes> moved_to_map = std::move(original_map);
4257  EXPECT_TRUE(original_map.empty());
4258  EXPECT_EQ(2, moved_to_map.size());
4259  EXPECT_EQ(42, moved_to_map[42].optional_nested_message().bb());
4260  EXPECT_EQ(43, moved_to_map[43].optional_nested_message().bb());
4261  // This test takes advantage of the fact that pointers are swapped, so there
4262  // should be pointer stability.
4263  EXPECT_EQ(nested_msg42_ptr, &moved_to_map[42].optional_nested_message());
4264  EXPECT_EQ(nested_msg43_ptr, &moved_to_map[43].optional_nested_message());
4265}
4266
4267TEST(KeyMapBaseTest, InsertOrReplaceNodeWorks) {
4268  using M = Map<int32_t, std::string>;
4269  M map;
4270  EXPECT_TRUE(MapTestPeer::InsertOrReplaceNode(map, 10, "Foo"));
4271  EXPECT_EQ(map.size(), 1);
4272  EXPECT_EQ(map[10], "Foo");
4273  EXPECT_FALSE(MapTestPeer::InsertOrReplaceNode(map, 10, "Bar"));
4274  EXPECT_EQ(map.size(), 1);
4275  EXPECT_EQ(map[10], "Bar");
4276  map[100] = "BAD";
4277  EXPECT_EQ(map.size(), 2);
4278  EXPECT_EQ(map[10], "Bar");
4279  EXPECT_EQ(map[100], "BAD");
4280  EXPECT_FALSE(MapTestPeer::InsertOrReplaceNode(map, 100, "GOOD"));
4281  EXPECT_EQ(map.size(), 2);
4282  EXPECT_EQ(map[10], "Bar");
4283  EXPECT_EQ(map[100], "GOOD");
4284}
4285
4286TEST(NonUtf8Test, StringValuePassesInProto2) {
4287  protobuf_unittest::TestProto2BytesMap message;
4288  (*message.mutable_map_string())[1] = "\xFF";
4289
4290  auto serialized = message.SerializeAsString();
4291  // Parsing passes, but a failure is logged in debug mode.
4292  ASSERT_TRUE(message.ParseFromString(serialized));
4293  EXPECT_EQ((*message.mutable_map_string())[1], "\xFF");
4294}
4295
4296TEST(NonUtf8Test, BytesValuePassesInProto2) {
4297  protobuf_unittest::TestProto2BytesMap message;
4298  (*message.mutable_map_bytes())[1] = "\xFF";
4299
4300  auto serialized = message.SerializeAsString();
4301  ASSERT_TRUE(message.ParseFromString(serialized));
4302  EXPECT_EQ((*message.mutable_map_bytes())[1], "\xFF");
4303}
4304
4305TEST(NonUtf8Test, StringValueFailsInProto3) {
4306  proto3_unittest::TestProto3BytesMap message;
4307  (*message.mutable_map_string())[1] = "\xFF";
4308
4309  auto serialized = message.SerializeAsString();
4310  // It will fail, and log an error.
4311  ASSERT_FALSE(message.ParseFromString(serialized));
4312}
4313
4314
4315TEST(NonUtf8Test, BytesValuePassesInProto3) {
4316  proto3_unittest::TestProto3BytesMap message;
4317  (*message.mutable_map_bytes())[1] = "\xFF";
4318
4319  auto serialized = message.SerializeAsString();
4320  ASSERT_TRUE(message.ParseFromString(serialized));
4321  EXPECT_EQ((*message.mutable_map_bytes())[1], "\xFF");
4322}
4323
4324}  // namespace
4325}  // namespace internal
4326}  // namespace protobuf
4327}  // namespace google
4328
4329#include "google/protobuf/port_undef.inc"
4330