| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 #ifndef _SHARED_PTR_H | 43 #ifndef _SHARED_PTR_H |
| 44 #include <google/protobuf/stubs/shared_ptr.h> | 44 #include <google/protobuf/stubs/shared_ptr.h> |
| 45 #endif | 45 #endif |
| 46 #include <set> | 46 #include <set> |
| 47 #include <sstream> | 47 #include <sstream> |
| 48 #include <vector> | 48 #include <vector> |
| 49 | 49 |
| 50 #include <google/protobuf/stubs/casts.h> | 50 #include <google/protobuf/stubs/casts.h> |
| 51 #include <google/protobuf/stubs/logging.h> | 51 #include <google/protobuf/stubs/logging.h> |
| 52 #include <google/protobuf/stubs/common.h> | 52 #include <google/protobuf/stubs/common.h> |
| 53 #include <google/protobuf/stubs/scoped_ptr.h> | |
| 54 #include <google/protobuf/stubs/stringprintf.h> | 53 #include <google/protobuf/stubs/stringprintf.h> |
| 55 #include <google/protobuf/testing/file.h> | 54 #include <google/protobuf/testing/file.h> |
| 56 #include <google/protobuf/arena_test_util.h> | 55 #include <google/protobuf/arena_test_util.h> |
| 57 #include <google/protobuf/map_proto2_unittest.pb.h> | 56 #include <google/protobuf/map_proto2_unittest.pb.h> |
| 58 #include <google/protobuf/map_unittest.pb.h> | 57 #include <google/protobuf/map_unittest.pb.h> |
| 59 #include <google/protobuf/map_test_util.h> | 58 #include <google/protobuf/map_test_util.h> |
| 60 #include <google/protobuf/test_util.h> | 59 #include <google/protobuf/test_util.h> |
| 61 #include <google/protobuf/unittest.pb.h> | 60 #include <google/protobuf/unittest.pb.h> |
| 62 #include <google/protobuf/descriptor.pb.h> | 61 #include <google/protobuf/descriptor.pb.h> |
| 63 #include <google/protobuf/descriptor.h> | 62 #include <google/protobuf/descriptor.h> |
| 64 #include <google/protobuf/descriptor_database.h> | 63 #include <google/protobuf/descriptor_database.h> |
| 65 #include <google/protobuf/dynamic_message.h> | 64 #include <google/protobuf/dynamic_message.h> |
| 66 #include <google/protobuf/map.h> | 65 #include <google/protobuf/map.h> |
| 67 #include <google/protobuf/map_field_inl.h> | 66 #include <google/protobuf/map_field_inl.h> |
| 68 #include <google/protobuf/message.h> | 67 #include <google/protobuf/message.h> |
| 69 #include <google/protobuf/reflection.h> | 68 #include <google/protobuf/reflection.h> |
| 70 #include <google/protobuf/reflection_ops.h> | 69 #include <google/protobuf/reflection_ops.h> |
| 71 #include <google/protobuf/text_format.h> | 70 #include <google/protobuf/text_format.h> |
| 72 #include <google/protobuf/wire_format.h> | 71 #include <google/protobuf/wire_format.h> |
| 73 #include <google/protobuf/wire_format_lite_inl.h> | 72 #include <google/protobuf/wire_format_lite_inl.h> |
| 74 #include <google/protobuf/io/coded_stream.h> | 73 #include <google/protobuf/io/coded_stream.h> |
| 75 #include <google/protobuf/io/tokenizer.h> | 74 #include <google/protobuf/io/tokenizer.h> |
| 76 #include <google/protobuf/io/zero_copy_stream_impl.h> | 75 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 77 #include <google/protobuf/util/time_util.h> | 76 #include <google/protobuf/util/time_util.h> |
| 77 #include <google/protobuf/util/message_differencer.h> |
| 78 #include <google/protobuf/stubs/strutil.h> | 78 #include <google/protobuf/stubs/strutil.h> |
| 79 #include <google/protobuf/stubs/substitute.h> | 79 #include <google/protobuf/stubs/substitute.h> |
| 80 #include <gmock/gmock.h> |
| 80 #include <google/protobuf/testing/googletest.h> | 81 #include <google/protobuf/testing/googletest.h> |
| 81 #include <gtest/gtest.h> | 82 #include <gtest/gtest.h> |
| 82 | 83 |
| 83 namespace google { | 84 namespace google { |
| 84 | 85 |
| 85 using google::protobuf::unittest::ForeignMessage; | 86 using google::protobuf::unittest::ForeignMessage; |
| 86 using google::protobuf::unittest::TestAllTypes; | 87 using google::protobuf::unittest::TestAllTypes; |
| 87 using google::protobuf::unittest::TestMap; | 88 using google::protobuf::unittest::TestMap; |
| 88 using google::protobuf::unittest::TestRecursiveMapMessage; | 89 using google::protobuf::unittest::TestRecursiveMapMessage; |
| 89 | 90 |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 // Pre increment. | 291 // Pre increment. |
| 291 EXPECT_FALSE(it == ++cit); | 292 EXPECT_FALSE(it == ++cit); |
| 292 | 293 |
| 293 // Post increment. | 294 // Post increment. |
| 294 EXPECT_FALSE(it++ == cit); | 295 EXPECT_FALSE(it++ == cit); |
| 295 EXPECT_TRUE(it == cit); | 296 EXPECT_TRUE(it == cit); |
| 296 } | 297 } |
| 297 | 298 |
| 298 template <typename Iterator> | 299 template <typename Iterator> |
| 299 static int64 median(Iterator i0, Iterator i1) { | 300 static int64 median(Iterator i0, Iterator i1) { |
| 300 vector<int64> v(i0, i1); | 301 std::vector<int64> v(i0, i1); |
| 301 std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end()); | 302 std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end()); |
| 302 return v[v.size() / 2]; | 303 return v[v.size() / 2]; |
| 303 } | 304 } |
| 304 | 305 |
| 305 static int64 Now() { | 306 static int64 Now() { |
| 306 return google::protobuf::util::TimeUtil::TimestampToNanoseconds( | 307 return google::protobuf::util::TimeUtil::TimestampToNanoseconds( |
| 307 google::protobuf::util::TimeUtil::GetCurrentTime()); | 308 google::protobuf::util::TimeUtil::GetCurrentTime()); |
| 308 } | 309 } |
| 309 | 310 |
| 310 // Arbitrary odd integers for creating test data. | 311 // Arbitrary odd integers for creating test data. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 326 int counter = 0; | 327 int counter = 0; |
| 327 while (map.size() < kTestSize) { | 328 while (map.size() < kTestSize) { |
| 328 frog *= static_cast<uint32>(k0); | 329 frog *= static_cast<uint32>(k0); |
| 329 frog ^= frog >> 17; | 330 frog ^= frog >> 17; |
| 330 frog += counter++; | 331 frog += counter++; |
| 331 last_key = | 332 last_key = |
| 332 static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1; | 333 static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1; |
| 333 GOOGLE_DCHECK_GE(last_key, 0); | 334 GOOGLE_DCHECK_GE(last_key, 0); |
| 334 map[last_key] = last_key ^ 1; | 335 map[last_key] = last_key ^ 1; |
| 335 } | 336 } |
| 336 vector<int64> times; | 337 std::vector<int64> times; |
| 337 // We're going to do map.erase(map.begin()) over and over again. But, | 338 // We're going to do map.erase(map.begin()) over and over again. But, |
| 338 // just in case one iteration is fast compared to the granularity of | 339 // just in case one iteration is fast compared to the granularity of |
| 339 // our time keeping, we measure kChunkSize iterations per outer-loop iter. | 340 // our time keeping, we measure kChunkSize iterations per outer-loop iter. |
| 340 const int kChunkSize = 1000; | 341 const int kChunkSize = 1000; |
| 341 GOOGLE_CHECK_EQ(kTestSize % kChunkSize, 0); | 342 GOOGLE_CHECK_EQ(kTestSize % kChunkSize, 0); |
| 342 do { | 343 do { |
| 343 const int64 start = Now(); | 344 const int64 start = Now(); |
| 344 for (int i = 0; i < kChunkSize; i++) { | 345 for (int i = 0; i < kChunkSize; i++) { |
| 345 map.erase(map.begin()); | 346 map.erase(map.begin()); |
| 346 } | 347 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 371 std::set<int> s; | 372 std::set<int> s; |
| 372 for (int i = 0; s.size() < kTestSize; i++) { | 373 for (int i = 0; s.size() < kTestSize; i++) { |
| 373 if ((map_.hash_function()(i) & (kTestSize - 1)) < 3) { | 374 if ((map_.hash_function()(i) & (kTestSize - 1)) < 3) { |
| 374 s.insert(i); | 375 s.insert(i); |
| 375 } | 376 } |
| 376 } | 377 } |
| 377 // Create hash table with kTestSize entries that hash flood a table with | 378 // Create hash table with kTestSize entries that hash flood a table with |
| 378 // 1024 (or 512 or 2048 or ...) entries. This assumes that map_ uses powers | 379 // 1024 (or 512 or 2048 or ...) entries. This assumes that map_ uses powers |
| 379 // of 2 for table sizes, and that it's sufficient to "flood" with respect to | 380 // of 2 for table sizes, and that it's sufficient to "flood" with respect to |
| 380 // the low bits of the output of map_.hash_function(). | 381 // the low bits of the output of map_.hash_function(). |
| 381 vector<int64> times; | 382 std::vector<int64> times; |
| 382 std::set<int>::iterator it = s.begin(); | 383 std::set<int>::iterator it = s.begin(); |
| 383 int count = 0; | 384 int count = 0; |
| 384 do { | 385 do { |
| 385 const int64 start = Now(); | 386 const int64 start = Now(); |
| 386 map_[*it] = 0; | 387 map_[*it] = 0; |
| 387 const int64 end = Now(); | 388 const int64 end = Now(); |
| 388 if (end > start) { | 389 if (end > start) { |
| 389 times.push_back(end - start); | 390 times.push_back(end - start); |
| 390 } | 391 } |
| 391 ++count; | 392 ++count; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 m.erase(last_key); | 496 m.erase(last_key); |
| 496 ASSERT_EQ(n - 1, m.size()); | 497 ASSERT_EQ(n - 1, m.size()); |
| 497 TestValidityForAllKeysExcept(last_key, mp, m); | 498 TestValidityForAllKeysExcept(last_key, mp, m); |
| 498 TestValidityForAllKeysExcept(last_key, mi, m); | 499 TestValidityForAllKeysExcept(last_key, mi, m); |
| 499 | 500 |
| 500 m[last_key] = 0; | 501 m[last_key] = 0; |
| 501 ASSERT_EQ(n, m.size()); | 502 ASSERT_EQ(n, m.size()); |
| 502 // Test old iterator vs new iterator, with table modification in between. | 503 // Test old iterator vs new iterator, with table modification in between. |
| 503 TestOldVersusNewIterator<Map<int, int>::const_iterator>(n % 3, &m); | 504 TestOldVersusNewIterator<Map<int, int>::const_iterator>(n % 3, &m); |
| 504 TestOldVersusNewIterator<Map<int, int>::iterator>(n % (1 + (n / 40)), &m); | 505 TestOldVersusNewIterator<Map<int, int>::iterator>(n % (1 + (n / 40)), &m); |
| 505 // Finally, ensure erase(iterator) doesn't reorder anything, becuase that is | 506 // Finally, ensure erase(iterator) doesn't reorder anything, because that is |
| 506 // what its documentation says. | 507 // what its documentation says. |
| 507 m[last_key] = m[last_key ^ 999] = 0; | 508 m[last_key] = m[last_key ^ 999] = 0; |
| 508 vector<Map<int, int>::iterator> v; | 509 std::vector<Map<int, int>::iterator> v; |
| 509 v.reserve(m.size()); | 510 v.reserve(m.size()); |
| 510 int position_of_last_key = 0; | 511 int position_of_last_key = 0; |
| 511 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) { | 512 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) { |
| 512 if (it->first == last_key) { | 513 if (it->first == last_key) { |
| 513 position_of_last_key = v.size(); | 514 position_of_last_key = v.size(); |
| 514 } | 515 } |
| 515 v.push_back(it); | 516 v.push_back(it); |
| 516 } | 517 } |
| 517 ASSERT_EQ(m.size(), v.size()); | 518 ASSERT_EQ(m.size(), v.size()); |
| 518 const Map<int, int>::iterator erase_result = m.erase(m.find(last_key)); | 519 const Map<int, int>::iterator erase_result = m.erase(m.find(last_key)); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 555 } | 556 } |
| 556 | 557 |
| 557 // Test that erase() revalidates iterators. | 558 // Test that erase() revalidates iterators. |
| 558 TEST_P(MapImplTest, EraseRevalidates) { | 559 TEST_P(MapImplTest, EraseRevalidates) { |
| 559 // As multiple underlying hash_map implementations do not follow the | 560 // As multiple underlying hash_map implementations do not follow the |
| 560 // validation requirement, the test is disabled for old-style maps. | 561 // validation requirement, the test is disabled for old-style maps. |
| 561 if (GetParam()) return; | 562 if (GetParam()) return; |
| 562 map_[3] = map_[13] = map_[20] = 0; | 563 map_[3] = map_[13] = map_[20] = 0; |
| 563 const int initial_size = map_.size(); | 564 const int initial_size = map_.size(); |
| 564 EXPECT_EQ(3, initial_size); | 565 EXPECT_EQ(3, initial_size); |
| 565 vector<Map<int, int>::iterator> v; | 566 std::vector<Map<int, int>::iterator> v; |
| 566 for (Map<int, int>::iterator it = map_.begin(); it != map_.end(); ++it) { | 567 for (Map<int, int>::iterator it = map_.begin(); it != map_.end(); ++it) { |
| 567 v.push_back(it); | 568 v.push_back(it); |
| 568 } | 569 } |
| 569 EXPECT_EQ(initial_size, v.size()); | 570 EXPECT_EQ(initial_size, v.size()); |
| 570 for (int i = 0; map_.size() <= initial_size * 20; i++) { | 571 for (int i = 0; map_.size() <= initial_size * 20; i++) { |
| 571 map_[i] = 0; | 572 map_[i] = 0; |
| 572 } | 573 } |
| 573 const int larger_size = map_.size(); | 574 const int larger_size = map_.size(); |
| 574 // We've greatly increased the size of the map, so it is highly likely that | 575 // We've greatly increased the size of the map, so it is highly likely that |
| 575 // the following will corrupt m if erase() doesn't properly revalidate | 576 // the following will corrupt m if erase() doesn't properly revalidate |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 908 } | 909 } |
| 909 | 910 |
| 910 TEST_P(MapImplTest, ConvertToStdVectorOfPairs) { | 911 TEST_P(MapImplTest, ConvertToStdVectorOfPairs) { |
| 911 map_[100] = 101; | 912 map_[100] = 101; |
| 912 std::vector<std::pair<int32, int32> > std_vec(map_.begin(), map_.end()); | 913 std::vector<std::pair<int32, int32> > std_vec(map_.begin(), map_.end()); |
| 913 EXPECT_EQ(1, std_vec.size()); | 914 EXPECT_EQ(1, std_vec.size()); |
| 914 EXPECT_EQ(100, std_vec[0].first); | 915 EXPECT_EQ(100, std_vec[0].first); |
| 915 EXPECT_EQ(101, std_vec[0].second); | 916 EXPECT_EQ(101, std_vec[0].second); |
| 916 } | 917 } |
| 917 | 918 |
| 919 TEST_P(MapImplTest, SwapSameStyle) { |
| 920 Map<int32, int32> another(GetParam()); // same old_style_ value |
| 921 map_[9398] = 41999; |
| 922 another[9398] = 41999; |
| 923 another[8070] = 42056; |
| 924 another.swap(map_); |
| 925 EXPECT_THAT(another, testing::UnorderedElementsAre( |
| 926 testing::Pair(9398, 41999))); |
| 927 EXPECT_THAT(map_, testing::UnorderedElementsAre( |
| 928 testing::Pair(8070, 42056), |
| 929 testing::Pair(9398, 41999))); |
| 930 } |
| 931 |
| 932 TEST_P(MapImplTest, SwapDifferentStyle) { |
| 933 Map<int32, int32> another(!GetParam()); // different old_style_ value |
| 934 map_[9398] = 41999; |
| 935 another[9398] = 41999; |
| 936 another[8070] = 42056; |
| 937 another.swap(map_); |
| 938 EXPECT_THAT(another, testing::UnorderedElementsAre( |
| 939 testing::Pair(9398, 41999))); |
| 940 EXPECT_THAT(map_, testing::UnorderedElementsAre( |
| 941 testing::Pair(8070, 42056), |
| 942 testing::Pair(9398, 41999))); |
| 943 } |
| 944 |
| 945 TEST_P(MapImplTest, SwapArena) { |
| 946 Arena arena1, arena2; |
| 947 Map<int32, int32> m1(&arena1, false); |
| 948 Map<int32, int32> m2(&arena2, false); |
| 949 map_[9398] = 41999; |
| 950 m1[9398] = 41999; |
| 951 m1[8070] = 42056; |
| 952 m2[10244] = 10247; |
| 953 m2[8070] = 42056; |
| 954 m1.swap(map_); |
| 955 EXPECT_THAT(m1, testing::UnorderedElementsAre( |
| 956 testing::Pair(9398, 41999))); |
| 957 EXPECT_THAT(map_, testing::UnorderedElementsAre( |
| 958 testing::Pair(8070, 42056), |
| 959 testing::Pair(9398, 41999))); |
| 960 m2.swap(m1); |
| 961 EXPECT_THAT(m1, testing::UnorderedElementsAre( |
| 962 testing::Pair(8070, 42056), |
| 963 testing::Pair(10244, 10247))); |
| 964 EXPECT_THAT(m2, testing::UnorderedElementsAre( |
| 965 testing::Pair(9398, 41999))); |
| 966 } |
| 967 |
| 918 INSTANTIATE_TEST_CASE_P(BoolSequence, MapImplTest, testing::Bool()); | 968 INSTANTIATE_TEST_CASE_P(BoolSequence, MapImplTest, testing::Bool()); |
| 919 | 969 |
| 920 // Map Field Reflection Test ======================================== | 970 // Map Field Reflection Test ======================================== |
| 921 | 971 |
| 922 static int Func(int i, int j) { | 972 static int Func(int i, int j) { |
| 923 return i * j; | 973 return i * j; |
| 924 } | 974 } |
| 925 | 975 |
| 926 static string StrFunc(int i, int j) { | 976 static string StrFunc(int i, int j) { |
| 927 string str; | 977 string str; |
| (...skipping 1171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2099 | 2149 |
| 2100 TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) { | 2150 TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) { |
| 2101 unittest::TestMap message; | 2151 unittest::TestMap message; |
| 2102 | 2152 |
| 2103 // Two key fields in wire format | 2153 // Two key fields in wire format |
| 2104 string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; | 2154 string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; |
| 2105 | 2155 |
| 2106 EXPECT_TRUE(message.ParseFromString(data)); | 2156 EXPECT_TRUE(message.ParseFromString(data)); |
| 2107 EXPECT_EQ(1, message.map_int32_int32().size()); | 2157 EXPECT_EQ(1, message.map_int32_int32().size()); |
| 2108 EXPECT_EQ(1, message.map_int32_int32().at(2)); | 2158 EXPECT_EQ(1, message.map_int32_int32().at(2)); |
| 2159 |
| 2160 // A similar test, but with a map from int to a message type. |
| 2161 // Again, we want to be sure that the "second one wins" when |
| 2162 // there are two separate entries with the same key. |
| 2163 const int key = 99; |
| 2164 unittest::TestRequiredMessageMap map_message; |
| 2165 unittest::TestRequired with_dummy4; |
| 2166 with_dummy4.set_a(0); |
| 2167 with_dummy4.set_b(0); |
| 2168 with_dummy4.set_c(0); |
| 2169 with_dummy4.set_dummy4(11); |
| 2170 (*map_message.mutable_map_field())[key] = with_dummy4; |
| 2171 string s = map_message.SerializeAsString(); |
| 2172 unittest::TestRequired with_dummy5; |
| 2173 with_dummy5.set_a(0); |
| 2174 with_dummy5.set_b(0); |
| 2175 with_dummy5.set_c(0); |
| 2176 with_dummy5.set_dummy5(12); |
| 2177 (*map_message.mutable_map_field())[key] = with_dummy5; |
| 2178 string both = s + map_message.SerializeAsString(); |
| 2179 // We don't expect a merge now. The "second one wins." |
| 2180 ASSERT_TRUE(map_message.ParseFromString(both)); |
| 2181 ASSERT_EQ(1, map_message.map_field().size()); |
| 2182 ASSERT_EQ(1, map_message.map_field().count(key)); |
| 2183 EXPECT_EQ(0, map_message.map_field().find(key)->second.a()); |
| 2184 EXPECT_EQ(0, map_message.map_field().find(key)->second.b()); |
| 2185 EXPECT_EQ(0, map_message.map_field().find(key)->second.c()); |
| 2186 EXPECT_FALSE(map_message.map_field().find(key)->second.has_dummy4()); |
| 2187 ASSERT_TRUE(map_message.map_field().find(key)->second.has_dummy5()); |
| 2188 EXPECT_EQ(12, map_message.map_field().find(key)->second.dummy5()); |
| 2189 } |
| 2190 |
| 2191 // Exhaustive combinations of keys, values, and junk in any order. |
| 2192 // This re-tests some of the things tested above, but if it fails |
| 2193 // it's more work to determine what went wrong, so it isn't necessarily |
| 2194 // bad that we have the simpler tests too. |
| 2195 TEST(GeneratedMapFieldTest, KeysValuesUnknownsWireFormat) { |
| 2196 unittest::TestMap message; |
| 2197 const int kMaxNumKeysAndValuesAndJunk = 4; |
| 2198 const char kKeyTag = 0x08; |
| 2199 const char kValueTag = 0x10; |
| 2200 const char kJunkTag = 0x20; |
| 2201 for (int items = 0; items <= kMaxNumKeysAndValuesAndJunk; items++) { |
| 2202 string data = "\x0A"; |
| 2203 // Encode length of what will follow. |
| 2204 data.push_back(items * 2); |
| 2205 static const int kBitsOfIPerItem = 4; |
| 2206 static const int mask = (1 << kBitsOfIPerItem) - 1; |
| 2207 // Each iteration of the following is a test. It uses i as bit vector |
| 2208 // encoding the keys and values to put in the wire format. |
| 2209 for (int i = 0; i < (1 << (items * kBitsOfIPerItem)); i++) { |
| 2210 string wire_format = data; |
| 2211 int expected_key = 0; |
| 2212 int expected_value = 0; |
| 2213 for (int k = i, j = 0; j < items; j++, k >>= kBitsOfIPerItem) { |
| 2214 bool is_key = k & 0x1; |
| 2215 bool is_value = !is_key && (k & 0x2); |
| 2216 wire_format.push_back(is_key ? kKeyTag : |
| 2217 is_value ? kValueTag : kJunkTag); |
| 2218 char c = static_cast<char>(k & mask) >> 2; // One char after the tag. |
| 2219 wire_format.push_back(c); |
| 2220 if (is_key) expected_key = static_cast<int>(c); |
| 2221 if (is_value) expected_value = static_cast<int>(c); |
| 2222 ASSERT_TRUE(message.ParseFromString(wire_format)); |
| 2223 ASSERT_EQ(1, message.map_int32_int32().size()); |
| 2224 ASSERT_EQ(expected_key, message.map_int32_int32().begin()->first); |
| 2225 ASSERT_EQ(expected_value, message.map_int32_int32().begin()->second); |
| 2226 } |
| 2227 } |
| 2228 } |
| 2109 } | 2229 } |
| 2110 | 2230 |
| 2111 TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) { | 2231 TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) { |
| 2112 unittest::TestMap message; | 2232 unittest::TestMap message; |
| 2113 | 2233 |
| 2114 // Two value fields in wire format | 2234 // Two value fields in wire format |
| 2115 string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; | 2235 string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; |
| 2116 | 2236 |
| 2117 EXPECT_TRUE(message.ParseFromString(data)); | 2237 EXPECT_TRUE(message.ParseFromString(data)); |
| 2118 EXPECT_EQ(1, message.map_int32_int32().size()); | 2238 EXPECT_EQ(1, message.map_int32_int32().size()); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2182 (*map_message.mutable_map_field())[0]; | 2302 (*map_message.mutable_map_field())[0]; |
| 2183 EXPECT_FALSE(map_message.IsInitialized()); | 2303 EXPECT_FALSE(map_message.IsInitialized()); |
| 2184 | 2304 |
| 2185 // Initialize uninitialized message | 2305 // Initialize uninitialized message |
| 2186 (*map_message.mutable_map_field())[0].set_a(0); | 2306 (*map_message.mutable_map_field())[0].set_a(0); |
| 2187 (*map_message.mutable_map_field())[0].set_b(0); | 2307 (*map_message.mutable_map_field())[0].set_b(0); |
| 2188 (*map_message.mutable_map_field())[0].set_c(0); | 2308 (*map_message.mutable_map_field())[0].set_c(0); |
| 2189 EXPECT_TRUE(map_message.IsInitialized()); | 2309 EXPECT_TRUE(map_message.IsInitialized()); |
| 2190 } | 2310 } |
| 2191 | 2311 |
| 2312 TEST(GeneratedMapFieldTest, MessagesMustMerge) { |
| 2313 unittest::TestRequiredMessageMap map_message; |
| 2314 unittest::TestRequired with_dummy4; |
| 2315 with_dummy4.set_a(97); |
| 2316 with_dummy4.set_b(0); |
| 2317 with_dummy4.set_c(0); |
| 2318 with_dummy4.set_dummy4(98); |
| 2319 |
| 2320 EXPECT_TRUE(with_dummy4.IsInitialized()); |
| 2321 (*map_message.mutable_map_field())[0] = with_dummy4; |
| 2322 EXPECT_TRUE(map_message.IsInitialized()); |
| 2323 string s = map_message.SerializeAsString(); |
| 2324 |
| 2325 // Modify s so that there are two values in the entry for key 0. |
| 2326 // The first will have no value for c. The second will have no value for a. |
| 2327 // Those are required fields. Also, make some other little changes, to |
| 2328 // ensure we are merging the two values (because they're messages). |
| 2329 ASSERT_EQ(s.size() - 2, s[1]); // encoding of the length of what follows |
| 2330 string encoded_val(s.data() + 4, s.data() + s.size()); |
| 2331 // In s, change the encoding of c to an encoding of dummy32. |
| 2332 s[s.size() - 3] -= 8; |
| 2333 // Make encoded_val slightly different from what's in s. |
| 2334 encoded_val[encoded_val.size() - 1] += 33; // Encode c = 33. |
| 2335 for (int i = 0; i < encoded_val.size(); i++) { |
| 2336 if (encoded_val[i] == 97) { |
| 2337 // Encode b = 91 instead of a = 97. But this won't matter, because |
| 2338 // we also encode b = 0 right after this. The point is to leave out |
| 2339 // a required field, and make sure the parser doesn't complain, because |
| 2340 // every required field is set after the merge of the two values. |
| 2341 encoded_val[i - 1] += 16; |
| 2342 encoded_val[i] = 91; |
| 2343 } else if (encoded_val[i] == 98) { |
| 2344 // Encode dummy5 = 99 instead of dummy4 = 98. |
| 2345 encoded_val[i - 1] += 8; // The tag for dummy5 is 8 more. |
| 2346 encoded_val[i]++; |
| 2347 break; |
| 2348 } |
| 2349 } |
| 2350 |
| 2351 s += encoded_val; // Add the second message. |
| 2352 s[1] += encoded_val.size(); // Adjust encoded size. |
| 2353 |
| 2354 // Test key then value then value. |
| 2355 int key = 0; |
| 2356 ASSERT_TRUE(map_message.ParseFromString(s)); |
| 2357 ASSERT_EQ(1, map_message.map_field().size()); |
| 2358 ASSERT_EQ(1, map_message.map_field().count(key)); |
| 2359 EXPECT_EQ(97, map_message.map_field().find(key)->second.a()); |
| 2360 EXPECT_EQ(0, map_message.map_field().find(key)->second.b()); |
| 2361 EXPECT_EQ(33, map_message.map_field().find(key)->second.c()); |
| 2362 EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4()); |
| 2363 EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5()); |
| 2364 |
| 2365 // Test key then value then value then key. |
| 2366 s.push_back(s[2]); // Copy the key's tag. |
| 2367 key = 19; |
| 2368 s.push_back(key); // Second key is 19 instead of 0. |
| 2369 s[1] += 2; // Adjust encoded size. |
| 2370 ASSERT_TRUE(map_message.ParseFromString(s)); |
| 2371 ASSERT_EQ(1, map_message.map_field().size()); |
| 2372 ASSERT_EQ(1, map_message.map_field().count(key)); |
| 2373 EXPECT_EQ(97, map_message.map_field().find(key)->second.a()); |
| 2374 EXPECT_EQ(0, map_message.map_field().find(key)->second.b()); |
| 2375 EXPECT_EQ(33, map_message.map_field().find(key)->second.c()); |
| 2376 EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4()); |
| 2377 EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5()); |
| 2378 } |
| 2379 |
| 2192 // Generated Message Reflection Test ================================ | 2380 // Generated Message Reflection Test ================================ |
| 2193 | 2381 |
| 2194 TEST(GeneratedMapFieldReflectionTest, SpaceUsed) { | 2382 TEST(GeneratedMapFieldReflectionTest, SpaceUsed) { |
| 2195 unittest::TestMap message; | 2383 unittest::TestMap message; |
| 2196 MapReflectionTester reflection_tester( | 2384 MapReflectionTester reflection_tester( |
| 2197 unittest::TestMap::descriptor()); | 2385 unittest::TestMap::descriptor()); |
| 2198 reflection_tester.SetMapFieldsViaReflection(&message); | 2386 reflection_tester.SetMapFieldsViaReflection(&message); |
| 2199 | 2387 |
| 2200 EXPECT_LT(0, message.GetReflection()->SpaceUsed(message)); | 2388 EXPECT_LT(0, message.GetReflection()->SpaceUsed(message)); |
| 2201 } | 2389 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2242 MapTestUtil::ExpectMapFieldsModified(message1); | 2430 MapTestUtil::ExpectMapFieldsModified(message1); |
| 2243 MapTestUtil::ExpectMapFieldsSet(message2); | 2431 MapTestUtil::ExpectMapFieldsSet(message2); |
| 2244 } | 2432 } |
| 2245 | 2433 |
| 2246 TEST(GeneratedMapFieldReflectionTest, SwapFields) { | 2434 TEST(GeneratedMapFieldReflectionTest, SwapFields) { |
| 2247 unittest::TestMap message1; | 2435 unittest::TestMap message1; |
| 2248 unittest::TestMap message2; | 2436 unittest::TestMap message2; |
| 2249 | 2437 |
| 2250 MapTestUtil::SetMapFields(&message2); | 2438 MapTestUtil::SetMapFields(&message2); |
| 2251 | 2439 |
| 2252 vector<const FieldDescriptor*> fields; | 2440 std::vector<const FieldDescriptor*> fields; |
| 2253 const Reflection* reflection = message1.GetReflection(); | 2441 const Reflection* reflection = message1.GetReflection(); |
| 2254 reflection->ListFields(message2, &fields); | 2442 reflection->ListFields(message2, &fields); |
| 2255 reflection->SwapFields(&message1, &message2, fields); | 2443 reflection->SwapFields(&message1, &message2, fields); |
| 2256 | 2444 |
| 2257 MapTestUtil::ExpectMapFieldsSet(message1); | 2445 MapTestUtil::ExpectMapFieldsSet(message1); |
| 2258 MapTestUtil::ExpectClear(message2); | 2446 MapTestUtil::ExpectClear(message2); |
| 2259 } | 2447 } |
| 2260 | 2448 |
| 2261 TEST(GeneratedMapFieldReflectionTest, ClearField) { | 2449 TEST(GeneratedMapFieldReflectionTest, ClearField) { |
| 2262 unittest::TestMap message; | 2450 unittest::TestMap message; |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2675 { | 2863 { |
| 2676 // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if | 2864 // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if |
| 2677 // EOF is reached before the expected number of bytes. | 2865 // EOF is reached before the expected number of bytes. |
| 2678 io::ArrayInputStream stream(data.data(), data.size()); | 2866 io::ArrayInputStream stream(data.data(), data.size()); |
| 2679 protobuf_unittest::TestAllTypes message; | 2867 protobuf_unittest::TestAllTypes message; |
| 2680 EXPECT_FALSE( | 2868 EXPECT_FALSE( |
| 2681 message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); | 2869 message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); |
| 2682 } | 2870 } |
| 2683 } | 2871 } |
| 2684 | 2872 |
| 2873 // Deterministic Serialization Test ========================================== |
| 2874 |
| 2875 template <typename T> |
| 2876 static string DeterministicSerializationWithSerializePartialToCodedStream( |
| 2877 const T& t) { |
| 2878 const int size = t.ByteSize(); |
| 2879 string result(size, '\0'); |
| 2880 io::ArrayOutputStream array_stream(string_as_array(&result), size); |
| 2881 io::CodedOutputStream output_stream(&array_stream); |
| 2882 output_stream.SetSerializationDeterministic(true); |
| 2883 t.SerializePartialToCodedStream(&output_stream); |
| 2884 EXPECT_FALSE(output_stream.HadError()); |
| 2885 EXPECT_EQ(size, output_stream.ByteCount()); |
| 2886 return result; |
| 2887 } |
| 2888 |
| 2889 template <typename T> |
| 2890 static string DeterministicSerializationWithSerializeToCodedStream(const T& t) { |
| 2891 const int size = t.ByteSize(); |
| 2892 string result(size, '\0'); |
| 2893 io::ArrayOutputStream array_stream(string_as_array(&result), size); |
| 2894 io::CodedOutputStream output_stream(&array_stream); |
| 2895 output_stream.SetSerializationDeterministic(true); |
| 2896 t.SerializeToCodedStream(&output_stream); |
| 2897 EXPECT_FALSE(output_stream.HadError()); |
| 2898 EXPECT_EQ(size, output_stream.ByteCount()); |
| 2899 return result; |
| 2900 } |
| 2901 |
| 2902 template <typename T> |
| 2903 static string DeterministicSerialization(const T& t) { |
| 2904 const int size = t.ByteSize(); |
| 2905 string result(size, '\0'); |
| 2906 io::ArrayOutputStream array_stream(string_as_array(&result), size); |
| 2907 io::CodedOutputStream output_stream(&array_stream); |
| 2908 output_stream.SetSerializationDeterministic(true); |
| 2909 t.SerializeWithCachedSizes(&output_stream); |
| 2910 EXPECT_FALSE(output_stream.HadError()); |
| 2911 EXPECT_EQ(size, output_stream.ByteCount()); |
| 2912 EXPECT_EQ(result, DeterministicSerializationWithSerializeToCodedStream(t)); |
| 2913 EXPECT_EQ(result, |
| 2914 DeterministicSerializationWithSerializePartialToCodedStream(t)); |
| 2915 return result; |
| 2916 } |
| 2917 |
| 2918 // Helper to test the serialization of the first arg against a golden file. |
| 2919 static void TestDeterministicSerialization(const protobuf_unittest::TestMaps& t, |
| 2920 const string& filename) { |
| 2921 string expected; |
| 2922 GOOGLE_CHECK_OK(File::GetContents( |
| 2923 TestSourceDir() + "/google/protobuf/testdata/" + filename, |
| 2924 &expected, true)); |
| 2925 const string actual = DeterministicSerialization(t); |
| 2926 EXPECT_EQ(expected, actual); |
| 2927 protobuf_unittest::TestMaps u; |
| 2928 EXPECT_TRUE(u.ParseFromString(actual)); |
| 2929 EXPECT_TRUE(google::protobuf::util::MessageDifferencer::Equals(u, t)); |
| 2930 } |
| 2931 |
| 2932 // Helper for MapSerializationTest. Return a 7-bit ASCII string. |
| 2933 static string ConstructKey(uint64 n) { |
| 2934 string s(n % static_cast<uint64>(9), '\0'); |
| 2935 if (s.empty()) { |
| 2936 return StrCat(n); |
| 2937 } else { |
| 2938 while (n != 0) { |
| 2939 s[n % s.size()] = (n >> 10) & 0x7f; |
| 2940 n /= 888; |
| 2941 } |
| 2942 return s; |
| 2943 } |
| 2944 } |
| 2945 |
| 2946 TEST(MapSerializationTest, Deterministic) { |
| 2947 const int kIters = 25; |
| 2948 protobuf_unittest::TestMaps t; |
| 2949 protobuf_unittest::TestIntIntMap inner; |
| 2950 (*inner.mutable_m())[0] = (*inner.mutable_m())[10] = |
| 2951 (*inner.mutable_m())[-200] = 0; |
| 2952 uint64 frog = 9; |
| 2953 const uint64 multiplier = 0xa29cd16f; |
| 2954 for (int i = 0; i < kIters; i++) { |
| 2955 const int32 i32 = static_cast<int32>(frog & 0xffffffff); |
| 2956 const uint32 u32 = static_cast<uint32>(i32) * 91919; |
| 2957 const int64 i64 = static_cast<int64>(frog); |
| 2958 const uint64 u64 = frog * static_cast<uint64>(187321); |
| 2959 const bool b = i32 > 0; |
| 2960 const string s = ConstructKey(frog); |
| 2961 (*inner.mutable_m())[i] = i32; |
| 2962 (*t.mutable_m_int32())[i32] = (*t.mutable_m_sint32())[i32] = |
| 2963 (*t.mutable_m_sfixed32())[i32] = inner; |
| 2964 (*t.mutable_m_uint32())[u32] = (*t.mutable_m_fixed32())[u32] = inner; |
| 2965 (*t.mutable_m_int64())[i64] = (*t.mutable_m_sint64())[i64] = |
| 2966 (*t.mutable_m_sfixed64())[i64] = inner; |
| 2967 (*t.mutable_m_uint64())[u64] = (*t.mutable_m_fixed64())[u64] = inner; |
| 2968 (*t.mutable_m_bool())[b] = inner; |
| 2969 (*t.mutable_m_string())[s] = inner; |
| 2970 (*t.mutable_m_string())[s + string(1 << (u32 % static_cast<uint32>(9)), |
| 2971 b)] = inner; |
| 2972 inner.mutable_m()->erase(i); |
| 2973 frog = frog * multiplier + i; |
| 2974 frog ^= (frog >> 41); |
| 2975 } |
| 2976 TestDeterministicSerialization(t, "golden_message_maps"); |
| 2977 } |
| 2978 |
| 2685 // Text Format Test ================================================= | 2979 // Text Format Test ================================================= |
| 2686 | 2980 |
| 2687 TEST(TextFormatMapTest, SerializeAndParse) { | 2981 TEST(TextFormatMapTest, SerializeAndParse) { |
| 2688 unittest::TestMap source; | 2982 unittest::TestMap source; |
| 2689 unittest::TestMap dest; | 2983 unittest::TestMap dest; |
| 2690 MapTestUtil::SetMapFields(&source); | 2984 MapTestUtil::SetMapFields(&source); |
| 2691 string output; | 2985 string output; |
| 2692 | 2986 |
| 2693 // Test compact ASCII | 2987 // Test compact ASCII |
| 2694 TextFormat::Printer printer; | 2988 TextFormat::Printer printer; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 2712 | 3006 |
| 2713 EXPECT_EQ(message.DebugString(), expected_text); | 3007 EXPECT_EQ(message.DebugString(), expected_text); |
| 2714 | 3008 |
| 2715 // Test again on the reverse order. | 3009 // Test again on the reverse order. |
| 2716 unittest::TestMap message2; | 3010 unittest::TestMap message2; |
| 2717 tester.SetMapFieldsViaReflection(&message2); | 3011 tester.SetMapFieldsViaReflection(&message2); |
| 2718 tester.SwapMapsViaReflection(&message2); | 3012 tester.SwapMapsViaReflection(&message2); |
| 2719 EXPECT_EQ(message2.DebugString(), expected_text); | 3013 EXPECT_EQ(message2.DebugString(), expected_text); |
| 2720 } | 3014 } |
| 2721 | 3015 |
| 3016 TEST(TextFormatMapTest, ParseCorruptedString) { |
| 3017 string serialized_message; |
| 3018 GOOGLE_CHECK_OK(File::GetContents( |
| 3019 TestSourceDir() + |
| 3020 "/google/protobuf/testdata/golden_message_maps", |
| 3021 &serialized_message, true)); |
| 3022 protobuf_unittest::TestMaps message; |
| 3023 GOOGLE_CHECK(message.ParseFromString(serialized_message)); |
| 3024 TestParseCorruptedString<protobuf_unittest::TestMaps, true>(message); |
| 3025 TestParseCorruptedString<protobuf_unittest::TestMaps, false>(message); |
| 3026 } |
| 3027 |
| 2722 | 3028 |
| 2723 // arena support ================================================= | 3029 // arena support ================================================= |
| 2724 TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) { | 3030 TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) { |
| 2725 // Allocate a large initial block to avoid mallocs during hooked test. | 3031 // Allocate a large initial block to avoid mallocs during hooked test. |
| 2726 std::vector<char> arena_block(128 * 1024); | 3032 std::vector<char> arena_block(128 * 1024); |
| 2727 ArenaOptions options; | 3033 ArenaOptions options; |
| 2728 options.initial_block = &arena_block[0]; | 3034 options.initial_block = &arena_block[0]; |
| 2729 options.initial_block_size = arena_block.size(); | 3035 options.initial_block_size = arena_block.size(); |
| 2730 Arena arena(options); | 3036 Arena arena(options); |
| 2731 string data; | 3037 string data; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2776 // String with length less than 16 will not be allocated from heap. | 3082 // String with length less than 16 will not be allocated from heap. |
| 2777 int original_capacity = data.capacity(); | 3083 int original_capacity = data.capacity(); |
| 2778 while (data.capacity() <= original_capacity) { | 3084 while (data.capacity() <= original_capacity) { |
| 2779 data.append("a"); | 3085 data.append("a"); |
| 2780 } | 3086 } |
| 2781 (*message->mutable_map_string_string())[data] = data; | 3087 (*message->mutable_map_string_string())[data] = data; |
| 2782 // We rely on heap checkers to detect memory leak for us. | 3088 // We rely on heap checkers to detect memory leak for us. |
| 2783 ASSERT_FALSE(message == NULL); | 3089 ASSERT_FALSE(message == NULL); |
| 2784 } | 3090 } |
| 2785 | 3091 |
| 3092 TEST(ArenaTest, IsInitialized) { |
| 3093 // Allocate a large initial polluted block. |
| 3094 std::vector<char> arena_block(128 * 1024); |
| 3095 std::fill(arena_block.begin(), arena_block.end(), '\xff'); |
| 3096 |
| 3097 ArenaOptions options; |
| 3098 options.initial_block = &arena_block[0]; |
| 3099 options.initial_block_size = arena_block.size(); |
| 3100 Arena arena(options); |
| 3101 |
| 3102 unittest::TestArenaMap* message = |
| 3103 Arena::CreateMessage<unittest::TestArenaMap>(&arena); |
| 3104 EXPECT_EQ(0, (*message->mutable_map_int32_int32())[0]); |
| 3105 } |
| 3106 |
| 2786 } // namespace internal | 3107 } // namespace internal |
| 2787 } // namespace protobuf | 3108 } // namespace protobuf |
| 2788 } // namespace google | 3109 } // namespace google |
| OLD | NEW |