| 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> |
| 53 #include <google/protobuf/stubs/stringprintf.h> | 54 #include <google/protobuf/stubs/stringprintf.h> |
| 54 #include <google/protobuf/testing/file.h> | 55 #include <google/protobuf/testing/file.h> |
| 55 #include <google/protobuf/arena_test_util.h> | 56 #include <google/protobuf/arena_test_util.h> |
| 56 #include <google/protobuf/map_proto2_unittest.pb.h> | 57 #include <google/protobuf/map_proto2_unittest.pb.h> |
| 57 #include <google/protobuf/map_unittest.pb.h> | 58 #include <google/protobuf/map_unittest.pb.h> |
| 58 #include <google/protobuf/map_test_util.h> | 59 #include <google/protobuf/map_test_util.h> |
| 59 #include <google/protobuf/test_util.h> | 60 #include <google/protobuf/test_util.h> |
| 60 #include <google/protobuf/unittest.pb.h> | 61 #include <google/protobuf/unittest.pb.h> |
| 61 #include <google/protobuf/descriptor.pb.h> | 62 #include <google/protobuf/descriptor.pb.h> |
| 62 #include <google/protobuf/descriptor.h> | 63 #include <google/protobuf/descriptor.h> |
| 63 #include <google/protobuf/descriptor_database.h> | 64 #include <google/protobuf/descriptor_database.h> |
| 64 #include <google/protobuf/dynamic_message.h> | 65 #include <google/protobuf/dynamic_message.h> |
| 65 #include <google/protobuf/map.h> | 66 #include <google/protobuf/map.h> |
| 66 #include <google/protobuf/map_field_inl.h> | 67 #include <google/protobuf/map_field_inl.h> |
| 67 #include <google/protobuf/message.h> | 68 #include <google/protobuf/message.h> |
| 68 #include <google/protobuf/reflection.h> | 69 #include <google/protobuf/reflection.h> |
| 69 #include <google/protobuf/reflection_ops.h> | 70 #include <google/protobuf/reflection_ops.h> |
| 70 #include <google/protobuf/text_format.h> | 71 #include <google/protobuf/text_format.h> |
| 71 #include <google/protobuf/wire_format.h> | 72 #include <google/protobuf/wire_format.h> |
| 72 #include <google/protobuf/wire_format_lite_inl.h> | 73 #include <google/protobuf/wire_format_lite_inl.h> |
| 73 #include <google/protobuf/io/coded_stream.h> | 74 #include <google/protobuf/io/coded_stream.h> |
| 74 #include <google/protobuf/io/tokenizer.h> | 75 #include <google/protobuf/io/tokenizer.h> |
| 75 #include <google/protobuf/io/zero_copy_stream_impl.h> | 76 #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 76 #include <google/protobuf/util/time_util.h> | 77 #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> | |
| 81 #include <google/protobuf/testing/googletest.h> | 80 #include <google/protobuf/testing/googletest.h> |
| 82 #include <gtest/gtest.h> | 81 #include <gtest/gtest.h> |
| 83 | 82 |
| 84 namespace google { | 83 namespace google { |
| 85 | 84 |
| 86 using google::protobuf::unittest::ForeignMessage; | 85 using google::protobuf::unittest::ForeignMessage; |
| 87 using google::protobuf::unittest::TestAllTypes; | 86 using google::protobuf::unittest::TestAllTypes; |
| 88 using google::protobuf::unittest::TestMap; | 87 using google::protobuf::unittest::TestMap; |
| 89 using google::protobuf::unittest::TestRecursiveMapMessage; | 88 using google::protobuf::unittest::TestRecursiveMapMessage; |
| 90 | 89 |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 // Pre increment. | 290 // Pre increment. |
| 292 EXPECT_FALSE(it == ++cit); | 291 EXPECT_FALSE(it == ++cit); |
| 293 | 292 |
| 294 // Post increment. | 293 // Post increment. |
| 295 EXPECT_FALSE(it++ == cit); | 294 EXPECT_FALSE(it++ == cit); |
| 296 EXPECT_TRUE(it == cit); | 295 EXPECT_TRUE(it == cit); |
| 297 } | 296 } |
| 298 | 297 |
| 299 template <typename Iterator> | 298 template <typename Iterator> |
| 300 static int64 median(Iterator i0, Iterator i1) { | 299 static int64 median(Iterator i0, Iterator i1) { |
| 301 std::vector<int64> v(i0, i1); | 300 vector<int64> v(i0, i1); |
| 302 std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end()); | 301 std::nth_element(v.begin(), v.begin() + v.size() / 2, v.end()); |
| 303 return v[v.size() / 2]; | 302 return v[v.size() / 2]; |
| 304 } | 303 } |
| 305 | 304 |
| 306 static int64 Now() { | 305 static int64 Now() { |
| 307 return google::protobuf::util::TimeUtil::TimestampToNanoseconds( | 306 return google::protobuf::util::TimeUtil::TimestampToNanoseconds( |
| 308 google::protobuf::util::TimeUtil::GetCurrentTime()); | 307 google::protobuf::util::TimeUtil::GetCurrentTime()); |
| 309 } | 308 } |
| 310 | 309 |
| 311 // Arbitrary odd integers for creating test data. | 310 // Arbitrary odd integers for creating test data. |
| (...skipping 15 matching lines...) Expand all Loading... |
| 327 int counter = 0; | 326 int counter = 0; |
| 328 while (map.size() < kTestSize) { | 327 while (map.size() < kTestSize) { |
| 329 frog *= static_cast<uint32>(k0); | 328 frog *= static_cast<uint32>(k0); |
| 330 frog ^= frog >> 17; | 329 frog ^= frog >> 17; |
| 331 frog += counter++; | 330 frog += counter++; |
| 332 last_key = | 331 last_key = |
| 333 static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1; | 332 static_cast<int>(frog) >= 0 ? static_cast<int>(frog) : last_key ^ 1; |
| 334 GOOGLE_DCHECK_GE(last_key, 0); | 333 GOOGLE_DCHECK_GE(last_key, 0); |
| 335 map[last_key] = last_key ^ 1; | 334 map[last_key] = last_key ^ 1; |
| 336 } | 335 } |
| 337 std::vector<int64> times; | 336 vector<int64> times; |
| 338 // We're going to do map.erase(map.begin()) over and over again. But, | 337 // We're going to do map.erase(map.begin()) over and over again. But, |
| 339 // just in case one iteration is fast compared to the granularity of | 338 // just in case one iteration is fast compared to the granularity of |
| 340 // our time keeping, we measure kChunkSize iterations per outer-loop iter. | 339 // our time keeping, we measure kChunkSize iterations per outer-loop iter. |
| 341 const int kChunkSize = 1000; | 340 const int kChunkSize = 1000; |
| 342 GOOGLE_CHECK_EQ(kTestSize % kChunkSize, 0); | 341 GOOGLE_CHECK_EQ(kTestSize % kChunkSize, 0); |
| 343 do { | 342 do { |
| 344 const int64 start = Now(); | 343 const int64 start = Now(); |
| 345 for (int i = 0; i < kChunkSize; i++) { | 344 for (int i = 0; i < kChunkSize; i++) { |
| 346 map.erase(map.begin()); | 345 map.erase(map.begin()); |
| 347 } | 346 } |
| (...skipping 24 matching lines...) Expand all Loading... |
| 372 std::set<int> s; | 371 std::set<int> s; |
| 373 for (int i = 0; s.size() < kTestSize; i++) { | 372 for (int i = 0; s.size() < kTestSize; i++) { |
| 374 if ((map_.hash_function()(i) & (kTestSize - 1)) < 3) { | 373 if ((map_.hash_function()(i) & (kTestSize - 1)) < 3) { |
| 375 s.insert(i); | 374 s.insert(i); |
| 376 } | 375 } |
| 377 } | 376 } |
| 378 // Create hash table with kTestSize entries that hash flood a table with | 377 // Create hash table with kTestSize entries that hash flood a table with |
| 379 // 1024 (or 512 or 2048 or ...) entries. This assumes that map_ uses powers | 378 // 1024 (or 512 or 2048 or ...) entries. This assumes that map_ uses powers |
| 380 // of 2 for table sizes, and that it's sufficient to "flood" with respect to | 379 // of 2 for table sizes, and that it's sufficient to "flood" with respect to |
| 381 // the low bits of the output of map_.hash_function(). | 380 // the low bits of the output of map_.hash_function(). |
| 382 std::vector<int64> times; | 381 vector<int64> times; |
| 383 std::set<int>::iterator it = s.begin(); | 382 std::set<int>::iterator it = s.begin(); |
| 384 int count = 0; | 383 int count = 0; |
| 385 do { | 384 do { |
| 386 const int64 start = Now(); | 385 const int64 start = Now(); |
| 387 map_[*it] = 0; | 386 map_[*it] = 0; |
| 388 const int64 end = Now(); | 387 const int64 end = Now(); |
| 389 if (end > start) { | 388 if (end > start) { |
| 390 times.push_back(end - start); | 389 times.push_back(end - start); |
| 391 } | 390 } |
| 392 ++count; | 391 ++count; |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 496 m.erase(last_key); | 495 m.erase(last_key); |
| 497 ASSERT_EQ(n - 1, m.size()); | 496 ASSERT_EQ(n - 1, m.size()); |
| 498 TestValidityForAllKeysExcept(last_key, mp, m); | 497 TestValidityForAllKeysExcept(last_key, mp, m); |
| 499 TestValidityForAllKeysExcept(last_key, mi, m); | 498 TestValidityForAllKeysExcept(last_key, mi, m); |
| 500 | 499 |
| 501 m[last_key] = 0; | 500 m[last_key] = 0; |
| 502 ASSERT_EQ(n, m.size()); | 501 ASSERT_EQ(n, m.size()); |
| 503 // Test old iterator vs new iterator, with table modification in between. | 502 // Test old iterator vs new iterator, with table modification in between. |
| 504 TestOldVersusNewIterator<Map<int, int>::const_iterator>(n % 3, &m); | 503 TestOldVersusNewIterator<Map<int, int>::const_iterator>(n % 3, &m); |
| 505 TestOldVersusNewIterator<Map<int, int>::iterator>(n % (1 + (n / 40)), &m); | 504 TestOldVersusNewIterator<Map<int, int>::iterator>(n % (1 + (n / 40)), &m); |
| 506 // Finally, ensure erase(iterator) doesn't reorder anything, because that is | 505 // Finally, ensure erase(iterator) doesn't reorder anything, becuase that is |
| 507 // what its documentation says. | 506 // what its documentation says. |
| 508 m[last_key] = m[last_key ^ 999] = 0; | 507 m[last_key] = m[last_key ^ 999] = 0; |
| 509 std::vector<Map<int, int>::iterator> v; | 508 vector<Map<int, int>::iterator> v; |
| 510 v.reserve(m.size()); | 509 v.reserve(m.size()); |
| 511 int position_of_last_key = 0; | 510 int position_of_last_key = 0; |
| 512 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) { | 511 for (Map<int, int>::iterator it = m.begin(); it != m.end(); ++it) { |
| 513 if (it->first == last_key) { | 512 if (it->first == last_key) { |
| 514 position_of_last_key = v.size(); | 513 position_of_last_key = v.size(); |
| 515 } | 514 } |
| 516 v.push_back(it); | 515 v.push_back(it); |
| 517 } | 516 } |
| 518 ASSERT_EQ(m.size(), v.size()); | 517 ASSERT_EQ(m.size(), v.size()); |
| 519 const Map<int, int>::iterator erase_result = m.erase(m.find(last_key)); | 518 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... |
| 556 } | 555 } |
| 557 | 556 |
| 558 // Test that erase() revalidates iterators. | 557 // Test that erase() revalidates iterators. |
| 559 TEST_P(MapImplTest, EraseRevalidates) { | 558 TEST_P(MapImplTest, EraseRevalidates) { |
| 560 // As multiple underlying hash_map implementations do not follow the | 559 // As multiple underlying hash_map implementations do not follow the |
| 561 // validation requirement, the test is disabled for old-style maps. | 560 // validation requirement, the test is disabled for old-style maps. |
| 562 if (GetParam()) return; | 561 if (GetParam()) return; |
| 563 map_[3] = map_[13] = map_[20] = 0; | 562 map_[3] = map_[13] = map_[20] = 0; |
| 564 const int initial_size = map_.size(); | 563 const int initial_size = map_.size(); |
| 565 EXPECT_EQ(3, initial_size); | 564 EXPECT_EQ(3, initial_size); |
| 566 std::vector<Map<int, int>::iterator> v; | 565 vector<Map<int, int>::iterator> v; |
| 567 for (Map<int, int>::iterator it = map_.begin(); it != map_.end(); ++it) { | 566 for (Map<int, int>::iterator it = map_.begin(); it != map_.end(); ++it) { |
| 568 v.push_back(it); | 567 v.push_back(it); |
| 569 } | 568 } |
| 570 EXPECT_EQ(initial_size, v.size()); | 569 EXPECT_EQ(initial_size, v.size()); |
| 571 for (int i = 0; map_.size() <= initial_size * 20; i++) { | 570 for (int i = 0; map_.size() <= initial_size * 20; i++) { |
| 572 map_[i] = 0; | 571 map_[i] = 0; |
| 573 } | 572 } |
| 574 const int larger_size = map_.size(); | 573 const int larger_size = map_.size(); |
| 575 // We've greatly increased the size of the map, so it is highly likely that | 574 // We've greatly increased the size of the map, so it is highly likely that |
| 576 // the following will corrupt m if erase() doesn't properly revalidate | 575 // the following will corrupt m if erase() doesn't properly revalidate |
| (...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 909 } | 908 } |
| 910 | 909 |
| 911 TEST_P(MapImplTest, ConvertToStdVectorOfPairs) { | 910 TEST_P(MapImplTest, ConvertToStdVectorOfPairs) { |
| 912 map_[100] = 101; | 911 map_[100] = 101; |
| 913 std::vector<std::pair<int32, int32> > std_vec(map_.begin(), map_.end()); | 912 std::vector<std::pair<int32, int32> > std_vec(map_.begin(), map_.end()); |
| 914 EXPECT_EQ(1, std_vec.size()); | 913 EXPECT_EQ(1, std_vec.size()); |
| 915 EXPECT_EQ(100, std_vec[0].first); | 914 EXPECT_EQ(100, std_vec[0].first); |
| 916 EXPECT_EQ(101, std_vec[0].second); | 915 EXPECT_EQ(101, std_vec[0].second); |
| 917 } | 916 } |
| 918 | 917 |
| 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 | |
| 968 INSTANTIATE_TEST_CASE_P(BoolSequence, MapImplTest, testing::Bool()); | 918 INSTANTIATE_TEST_CASE_P(BoolSequence, MapImplTest, testing::Bool()); |
| 969 | 919 |
| 970 // Map Field Reflection Test ======================================== | 920 // Map Field Reflection Test ======================================== |
| 971 | 921 |
| 972 static int Func(int i, int j) { | 922 static int Func(int i, int j) { |
| 973 return i * j; | 923 return i * j; |
| 974 } | 924 } |
| 975 | 925 |
| 976 static string StrFunc(int i, int j) { | 926 static string StrFunc(int i, int j) { |
| 977 string str; | 927 string str; |
| (...skipping 1171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2149 | 2099 |
| 2150 TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) { | 2100 TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) { |
| 2151 unittest::TestMap message; | 2101 unittest::TestMap message; |
| 2152 | 2102 |
| 2153 // Two key fields in wire format | 2103 // Two key fields in wire format |
| 2154 string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; | 2104 string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; |
| 2155 | 2105 |
| 2156 EXPECT_TRUE(message.ParseFromString(data)); | 2106 EXPECT_TRUE(message.ParseFromString(data)); |
| 2157 EXPECT_EQ(1, message.map_int32_int32().size()); | 2107 EXPECT_EQ(1, message.map_int32_int32().size()); |
| 2158 EXPECT_EQ(1, message.map_int32_int32().at(2)); | 2108 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 } | |
| 2229 } | 2109 } |
| 2230 | 2110 |
| 2231 TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) { | 2111 TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) { |
| 2232 unittest::TestMap message; | 2112 unittest::TestMap message; |
| 2233 | 2113 |
| 2234 // Two value fields in wire format | 2114 // Two value fields in wire format |
| 2235 string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; | 2115 string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; |
| 2236 | 2116 |
| 2237 EXPECT_TRUE(message.ParseFromString(data)); | 2117 EXPECT_TRUE(message.ParseFromString(data)); |
| 2238 EXPECT_EQ(1, message.map_int32_int32().size()); | 2118 EXPECT_EQ(1, message.map_int32_int32().size()); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2302 (*map_message.mutable_map_field())[0]; | 2182 (*map_message.mutable_map_field())[0]; |
| 2303 EXPECT_FALSE(map_message.IsInitialized()); | 2183 EXPECT_FALSE(map_message.IsInitialized()); |
| 2304 | 2184 |
| 2305 // Initialize uninitialized message | 2185 // Initialize uninitialized message |
| 2306 (*map_message.mutable_map_field())[0].set_a(0); | 2186 (*map_message.mutable_map_field())[0].set_a(0); |
| 2307 (*map_message.mutable_map_field())[0].set_b(0); | 2187 (*map_message.mutable_map_field())[0].set_b(0); |
| 2308 (*map_message.mutable_map_field())[0].set_c(0); | 2188 (*map_message.mutable_map_field())[0].set_c(0); |
| 2309 EXPECT_TRUE(map_message.IsInitialized()); | 2189 EXPECT_TRUE(map_message.IsInitialized()); |
| 2310 } | 2190 } |
| 2311 | 2191 |
| 2312 TEST(GeneratedMapFieldTest, MessagesMustMerge) { | |
| 2313 unittest::TestRequiredMessageMap map_message; | |
| 2314 unittest::TestRequired with_dummy4; | |
| 2315 with_dummy4.set_a(97); | |
| 2316 with_dummy4.set_b(0); | |
| 2317 with_dummy4.set_c(0); | |
| 2318 with_dummy4.set_dummy4(98); | |
| 2319 | |
| 2320 EXPECT_TRUE(with_dummy4.IsInitialized()); | |
| 2321 (*map_message.mutable_map_field())[0] = with_dummy4; | |
| 2322 EXPECT_TRUE(map_message.IsInitialized()); | |
| 2323 string s = map_message.SerializeAsString(); | |
| 2324 | |
| 2325 // Modify s so that there are two values in the entry for key 0. | |
| 2326 // The first will have no value for c. The second will have no value for a. | |
| 2327 // Those are required fields. Also, make some other little changes, to | |
| 2328 // ensure we are merging the two values (because they're messages). | |
| 2329 ASSERT_EQ(s.size() - 2, s[1]); // encoding of the length of what follows | |
| 2330 string encoded_val(s.data() + 4, s.data() + s.size()); | |
| 2331 // In s, change the encoding of c to an encoding of dummy32. | |
| 2332 s[s.size() - 3] -= 8; | |
| 2333 // Make encoded_val slightly different from what's in s. | |
| 2334 encoded_val[encoded_val.size() - 1] += 33; // Encode c = 33. | |
| 2335 for (int i = 0; i < encoded_val.size(); i++) { | |
| 2336 if (encoded_val[i] == 97) { | |
| 2337 // Encode b = 91 instead of a = 97. But this won't matter, because | |
| 2338 // we also encode b = 0 right after this. The point is to leave out | |
| 2339 // a required field, and make sure the parser doesn't complain, because | |
| 2340 // every required field is set after the merge of the two values. | |
| 2341 encoded_val[i - 1] += 16; | |
| 2342 encoded_val[i] = 91; | |
| 2343 } else if (encoded_val[i] == 98) { | |
| 2344 // Encode dummy5 = 99 instead of dummy4 = 98. | |
| 2345 encoded_val[i - 1] += 8; // The tag for dummy5 is 8 more. | |
| 2346 encoded_val[i]++; | |
| 2347 break; | |
| 2348 } | |
| 2349 } | |
| 2350 | |
| 2351 s += encoded_val; // Add the second message. | |
| 2352 s[1] += encoded_val.size(); // Adjust encoded size. | |
| 2353 | |
| 2354 // Test key then value then value. | |
| 2355 int key = 0; | |
| 2356 ASSERT_TRUE(map_message.ParseFromString(s)); | |
| 2357 ASSERT_EQ(1, map_message.map_field().size()); | |
| 2358 ASSERT_EQ(1, map_message.map_field().count(key)); | |
| 2359 EXPECT_EQ(97, map_message.map_field().find(key)->second.a()); | |
| 2360 EXPECT_EQ(0, map_message.map_field().find(key)->second.b()); | |
| 2361 EXPECT_EQ(33, map_message.map_field().find(key)->second.c()); | |
| 2362 EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4()); | |
| 2363 EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5()); | |
| 2364 | |
| 2365 // Test key then value then value then key. | |
| 2366 s.push_back(s[2]); // Copy the key's tag. | |
| 2367 key = 19; | |
| 2368 s.push_back(key); // Second key is 19 instead of 0. | |
| 2369 s[1] += 2; // Adjust encoded size. | |
| 2370 ASSERT_TRUE(map_message.ParseFromString(s)); | |
| 2371 ASSERT_EQ(1, map_message.map_field().size()); | |
| 2372 ASSERT_EQ(1, map_message.map_field().count(key)); | |
| 2373 EXPECT_EQ(97, map_message.map_field().find(key)->second.a()); | |
| 2374 EXPECT_EQ(0, map_message.map_field().find(key)->second.b()); | |
| 2375 EXPECT_EQ(33, map_message.map_field().find(key)->second.c()); | |
| 2376 EXPECT_EQ(98, map_message.map_field().find(key)->second.dummy4()); | |
| 2377 EXPECT_EQ(99, map_message.map_field().find(key)->second.dummy5()); | |
| 2378 } | |
| 2379 | |
| 2380 // Generated Message Reflection Test ================================ | 2192 // Generated Message Reflection Test ================================ |
| 2381 | 2193 |
| 2382 TEST(GeneratedMapFieldReflectionTest, SpaceUsed) { | 2194 TEST(GeneratedMapFieldReflectionTest, SpaceUsed) { |
| 2383 unittest::TestMap message; | 2195 unittest::TestMap message; |
| 2384 MapReflectionTester reflection_tester( | 2196 MapReflectionTester reflection_tester( |
| 2385 unittest::TestMap::descriptor()); | 2197 unittest::TestMap::descriptor()); |
| 2386 reflection_tester.SetMapFieldsViaReflection(&message); | 2198 reflection_tester.SetMapFieldsViaReflection(&message); |
| 2387 | 2199 |
| 2388 EXPECT_LT(0, message.GetReflection()->SpaceUsed(message)); | 2200 EXPECT_LT(0, message.GetReflection()->SpaceUsed(message)); |
| 2389 } | 2201 } |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2430 MapTestUtil::ExpectMapFieldsModified(message1); | 2242 MapTestUtil::ExpectMapFieldsModified(message1); |
| 2431 MapTestUtil::ExpectMapFieldsSet(message2); | 2243 MapTestUtil::ExpectMapFieldsSet(message2); |
| 2432 } | 2244 } |
| 2433 | 2245 |
| 2434 TEST(GeneratedMapFieldReflectionTest, SwapFields) { | 2246 TEST(GeneratedMapFieldReflectionTest, SwapFields) { |
| 2435 unittest::TestMap message1; | 2247 unittest::TestMap message1; |
| 2436 unittest::TestMap message2; | 2248 unittest::TestMap message2; |
| 2437 | 2249 |
| 2438 MapTestUtil::SetMapFields(&message2); | 2250 MapTestUtil::SetMapFields(&message2); |
| 2439 | 2251 |
| 2440 std::vector<const FieldDescriptor*> fields; | 2252 vector<const FieldDescriptor*> fields; |
| 2441 const Reflection* reflection = message1.GetReflection(); | 2253 const Reflection* reflection = message1.GetReflection(); |
| 2442 reflection->ListFields(message2, &fields); | 2254 reflection->ListFields(message2, &fields); |
| 2443 reflection->SwapFields(&message1, &message2, fields); | 2255 reflection->SwapFields(&message1, &message2, fields); |
| 2444 | 2256 |
| 2445 MapTestUtil::ExpectMapFieldsSet(message1); | 2257 MapTestUtil::ExpectMapFieldsSet(message1); |
| 2446 MapTestUtil::ExpectClear(message2); | 2258 MapTestUtil::ExpectClear(message2); |
| 2447 } | 2259 } |
| 2448 | 2260 |
| 2449 TEST(GeneratedMapFieldReflectionTest, ClearField) { | 2261 TEST(GeneratedMapFieldReflectionTest, ClearField) { |
| 2450 unittest::TestMap message; | 2262 unittest::TestMap message; |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2863 { | 2675 { |
| 2864 // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if | 2676 // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if |
| 2865 // EOF is reached before the expected number of bytes. | 2677 // EOF is reached before the expected number of bytes. |
| 2866 io::ArrayInputStream stream(data.data(), data.size()); | 2678 io::ArrayInputStream stream(data.data(), data.size()); |
| 2867 protobuf_unittest::TestAllTypes message; | 2679 protobuf_unittest::TestAllTypes message; |
| 2868 EXPECT_FALSE( | 2680 EXPECT_FALSE( |
| 2869 message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); | 2681 message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); |
| 2870 } | 2682 } |
| 2871 } | 2683 } |
| 2872 | 2684 |
| 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 | |
| 2979 // Text Format Test ================================================= | 2685 // Text Format Test ================================================= |
| 2980 | 2686 |
| 2981 TEST(TextFormatMapTest, SerializeAndParse) { | 2687 TEST(TextFormatMapTest, SerializeAndParse) { |
| 2982 unittest::TestMap source; | 2688 unittest::TestMap source; |
| 2983 unittest::TestMap dest; | 2689 unittest::TestMap dest; |
| 2984 MapTestUtil::SetMapFields(&source); | 2690 MapTestUtil::SetMapFields(&source); |
| 2985 string output; | 2691 string output; |
| 2986 | 2692 |
| 2987 // Test compact ASCII | 2693 // Test compact ASCII |
| 2988 TextFormat::Printer printer; | 2694 TextFormat::Printer printer; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 3006 | 2712 |
| 3007 EXPECT_EQ(message.DebugString(), expected_text); | 2713 EXPECT_EQ(message.DebugString(), expected_text); |
| 3008 | 2714 |
| 3009 // Test again on the reverse order. | 2715 // Test again on the reverse order. |
| 3010 unittest::TestMap message2; | 2716 unittest::TestMap message2; |
| 3011 tester.SetMapFieldsViaReflection(&message2); | 2717 tester.SetMapFieldsViaReflection(&message2); |
| 3012 tester.SwapMapsViaReflection(&message2); | 2718 tester.SwapMapsViaReflection(&message2); |
| 3013 EXPECT_EQ(message2.DebugString(), expected_text); | 2719 EXPECT_EQ(message2.DebugString(), expected_text); |
| 3014 } | 2720 } |
| 3015 | 2721 |
| 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 | |
| 3028 | 2722 |
| 3029 // arena support ================================================= | 2723 // arena support ================================================= |
| 3030 TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) { | 2724 TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) { |
| 3031 // Allocate a large initial block to avoid mallocs during hooked test. | 2725 // Allocate a large initial block to avoid mallocs during hooked test. |
| 3032 std::vector<char> arena_block(128 * 1024); | 2726 std::vector<char> arena_block(128 * 1024); |
| 3033 ArenaOptions options; | 2727 ArenaOptions options; |
| 3034 options.initial_block = &arena_block[0]; | 2728 options.initial_block = &arena_block[0]; |
| 3035 options.initial_block_size = arena_block.size(); | 2729 options.initial_block_size = arena_block.size(); |
| 3036 Arena arena(options); | 2730 Arena arena(options); |
| 3037 string data; | 2731 string data; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3082 // String with length less than 16 will not be allocated from heap. | 2776 // String with length less than 16 will not be allocated from heap. |
| 3083 int original_capacity = data.capacity(); | 2777 int original_capacity = data.capacity(); |
| 3084 while (data.capacity() <= original_capacity) { | 2778 while (data.capacity() <= original_capacity) { |
| 3085 data.append("a"); | 2779 data.append("a"); |
| 3086 } | 2780 } |
| 3087 (*message->mutable_map_string_string())[data] = data; | 2781 (*message->mutable_map_string_string())[data] = data; |
| 3088 // We rely on heap checkers to detect memory leak for us. | 2782 // We rely on heap checkers to detect memory leak for us. |
| 3089 ASSERT_FALSE(message == NULL); | 2783 ASSERT_FALSE(message == NULL); |
| 3090 } | 2784 } |
| 3091 | 2785 |
| 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 | |
| 3107 } // namespace internal | 2786 } // namespace internal |
| 3108 } // namespace protobuf | 2787 } // namespace protobuf |
| 3109 } // namespace google | 2788 } // namespace google |
| OLD | NEW |