33 #include <unordered_map>
35 #include <google/protobuf/stubs/logging.h>
36 #include <google/protobuf/stubs/common.h>
37 #include <google/protobuf/arena_test_util.h>
38 #include <google/protobuf/map_test_util.h>
39 #include <google/protobuf/map_unittest.pb.h>
40 #include <google/protobuf/unittest.pb.h>
41 #include <google/protobuf/arena.h>
42 #include <google/protobuf/map.h>
43 #include <google/protobuf/map_field_inl.h>
44 #include <google/protobuf/message.h>
45 #include <google/protobuf/repeated_field.h>
46 #include <gtest/gtest.h>
49 #include <google/protobuf/port_def.inc>
56 using unittest::TestAllTypes;
58 class MapFieldBaseStub :
public MapFieldBase {
92 int size()
const override {
return 0; }
101 const MapIterator& other_iterator)
const override {}
112 typedef unittest::TestMap_MapInt32Int32Entry_DoNotUse
EntryType;
123 ->FindFieldByName(
"map_int32_int32")
152 EXPECT_LT(0, map_field_base_->SpaceUsedExcludingSelf());
158 map_field_base_->GetRepeatedField());
171 map_field_base_->MutableRepeatedField());
183 std::vector<char> arena_block(128 * 1024);
185 options.initial_block = &arena_block[0];
186 options.initial_block_size = arena_block.size();
194 MapFieldType* map_field = Arena::CreateMessage<MapFieldType>(&
arena);
197 (*map_field->MutableMap())[100] = 101;
200 map_field->GetRepeatedField();
209 Arena::CreateMessage<MapFieldBaseStub>(&
arena);
212 EXPECT_TRUE(map_field->MutableRepeatedField() !=
nullptr);
219 std::unique_ptr<MapFieldBaseStub> map_field(
220 Arena::CreateMessage<MapFieldBaseStub>(
nullptr));
221 EXPECT_EQ(map_field->GetArenaForInternalRepeatedField(),
nullptr);
225 enum State { CLEAN, MAP_DIRTY, REPEATED_DIRTY };
228 class MapFieldStateTest
231 typedef unittest::TestMap_MapInt32Int32Entry_DoNotUse
EntryType;
262 Expect(map_field, CLEAN, 1, 1,
false);
268 Expect(map_field, MAP_DIRTY, 1, 0,
true);
279 Expect(map_field, REPEATED_DIRTY, 0, 1,
false);
283 int repeated_size,
bool is_repeated_null) {
310 if (is_repeated_null) {
334 if (
state_ != MAP_DIRTY) {
341 TEST_P(MapFieldStateTest, MutableMap) {
343 if (
state_ != MAP_DIRTY) {
350 TEST_P(MapFieldStateTest, MergeFromClean) {
351 ArenaHolder<MapFieldType> other(
arena_.get());
352 AddOneStillClean(other.get());
356 if (
state_ != MAP_DIRTY) {
362 Expect(other.get(), CLEAN, 1, 1,
false);
365 TEST_P(MapFieldStateTest, MergeFromMapDirty) {
366 ArenaHolder<MapFieldType> other(
arena_.get());
367 MakeMapDirty(other.get());
371 if (
state_ != MAP_DIRTY) {
377 Expect(other.get(), MAP_DIRTY, 1, 0,
true);
380 TEST_P(MapFieldStateTest, MergeFromRepeatedDirty) {
381 ArenaHolder<MapFieldType> other(
arena_.get());
382 MakeRepeatedDirty(other.get());
386 if (
state_ != MAP_DIRTY) {
392 Expect(other.get(), CLEAN, 1, 1,
false);
395 TEST_P(MapFieldStateTest, SwapClean) {
396 ArenaHolder<MapFieldType> other(
arena_.get());
397 AddOneStillClean(other.get());
405 Expect(other.get(), CLEAN, 1, 1,
false);
408 Expect(other.get(), MAP_DIRTY, 1, 0,
true);
411 Expect(other.get(), REPEATED_DIRTY, 0, 1,
false);
418 TEST_P(MapFieldStateTest, SwapMapDirty) {
419 ArenaHolder<MapFieldType> other(
arena_.get());
420 MakeMapDirty(other.get());
428 Expect(other.get(), CLEAN, 1, 1,
false);
431 Expect(other.get(), MAP_DIRTY, 1, 0,
true);
434 Expect(other.get(), REPEATED_DIRTY, 0, 1,
false);
441 TEST_P(MapFieldStateTest, SwapRepeatedDirty) {
442 ArenaHolder<MapFieldType> other(
arena_.get());
443 MakeRepeatedDirty(other.get());
451 Expect(other.get(), CLEAN, 1, 1,
false);
454 Expect(other.get(), MAP_DIRTY, 1, 0,
true);
457 Expect(other.get(), REPEATED_DIRTY, 0, 1,
false);
470 TEST_P(MapFieldStateTest, SpaceUsedExcludingSelf) {
471 map_field_base_->SpaceUsedExcludingSelf();
488 TEST_P(MapFieldStateTest, GetMapField) {
489 map_field_base_->GetRepeatedField();
491 if (
state_ != REPEATED_DIRTY) {
498 TEST_P(MapFieldStateTest, MutableMapField) {
499 map_field_base_->MutableRepeatedField();
501 if (
state_ != REPEATED_DIRTY) {
509 :
public MapField<unittest::TestMap_MapInt32Int32Entry_DoNotUse, int32,
510 int32, internal::WireFormatLite::TYPE_INT32,
511 internal::WireFormatLite::TYPE_INT32> {
517 TEST(MapFieldTest, ConstInit) {