Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | #include <map> |
| 32 | #include <memory> |
| 33 | #ifndef _SHARED_PTR_H |
| 34 | #include <google/protobuf/stubs/shared_ptr.h> |
| 35 | #endif |
| 36 | #include <sstream> |
| 37 | |
| 38 | #include <google/protobuf/stubs/casts.h> |
| 39 | #include <google/protobuf/stubs/common.h> |
| 40 | #include <google/protobuf/stubs/stringprintf.h> |
| 41 | #include <google/protobuf/testing/file.h> |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 42 | #include <google/protobuf/arena_test_util.h> |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 43 | #include <google/protobuf/map_proto2_unittest.pb.h> |
| 44 | #include <google/protobuf/map_unittest.pb.h> |
| 45 | #include <google/protobuf/map_test_util.h> |
| 46 | #include <google/protobuf/test_util.h> |
| 47 | #include <google/protobuf/unittest.pb.h> |
| 48 | #include <google/protobuf/descriptor.pb.h> |
| 49 | #include <google/protobuf/descriptor.h> |
| 50 | #include <google/protobuf/descriptor_database.h> |
| 51 | #include <google/protobuf/dynamic_message.h> |
| 52 | #include <google/protobuf/map.h> |
| 53 | #include <google/protobuf/map_field_inl.h> |
| 54 | #include <google/protobuf/message.h> |
| 55 | #include <google/protobuf/reflection.h> |
| 56 | #include <google/protobuf/reflection_ops.h> |
| 57 | #include <google/protobuf/text_format.h> |
| 58 | #include <google/protobuf/wire_format.h> |
| 59 | #include <google/protobuf/wire_format_lite_inl.h> |
| 60 | #include <google/protobuf/io/coded_stream.h> |
| 61 | #include <google/protobuf/io/tokenizer.h> |
| 62 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 63 | #include <google/protobuf/stubs/strutil.h> |
| 64 | #include <google/protobuf/stubs/substitute.h> |
| 65 | #include <google/protobuf/testing/googletest.h> |
| 66 | #include <gtest/gtest.h> |
| 67 | |
| 68 | namespace google { |
| 69 | |
| 70 | using google::protobuf::unittest::ForeignMessage; |
| 71 | using google::protobuf::unittest::TestAllTypes; |
| 72 | using google::protobuf::unittest::TestMap; |
| 73 | |
| 74 | namespace protobuf { |
| 75 | namespace internal { |
| 76 | |
| 77 | // Map API Test ===================================================== |
| 78 | |
| 79 | class MapImplTest : public ::testing::Test { |
| 80 | protected: |
| 81 | MapImplTest() |
| 82 | : map_ptr_(new Map<int32, int32>), |
| 83 | map_(*map_ptr_), |
| 84 | const_map_(*map_ptr_) { |
| 85 | EXPECT_TRUE(map_.empty()); |
| 86 | EXPECT_EQ(0, map_.size()); |
| 87 | } |
Tres Seaver | 4113685 | 2014-11-18 11:58:21 -0500 | [diff] [blame] | 88 | ~MapImplTest() {} |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 89 | |
| 90 | void ExpectSingleElement(int32 key, int32 value) { |
| 91 | EXPECT_FALSE(map_.empty()); |
| 92 | EXPECT_EQ(1, map_.size()); |
| 93 | ExpectElement(key, value); |
| 94 | } |
| 95 | |
| 96 | void ExpectElements(const std::map<int32, int32>& map) { |
| 97 | EXPECT_FALSE(map_.empty()); |
| 98 | EXPECT_EQ(map.size(), map_.size()); |
| 99 | for (std::map<int32, int32>::const_iterator it = map.begin(); |
| 100 | it != map.end(); ++it) { |
| 101 | ExpectElement(it->first, it->second); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | void ExpectElement(int32 key, int32 value) { |
| 106 | // Test map size is correct. |
| 107 | EXPECT_EQ(value, map_[key]); |
| 108 | EXPECT_EQ(1, map_.count(key)); |
| 109 | |
| 110 | // Check mutable at and find work correctly. |
| 111 | EXPECT_EQ(value, map_.at(key)); |
| 112 | Map<int32, int32>::iterator it = map_.find(key); |
| 113 | |
| 114 | // interator dereferenceable |
| 115 | EXPECT_EQ(key, (*it).first); |
| 116 | EXPECT_EQ(value, (*it).second); |
| 117 | EXPECT_EQ(key, it->first); |
| 118 | EXPECT_EQ(value, it->second); |
| 119 | |
| 120 | // iterator mutable |
| 121 | ((*it).second) = value + 1; |
| 122 | EXPECT_EQ(value + 1, map_[key]); |
| 123 | ((*it).second) = value; |
| 124 | EXPECT_EQ(value, map_[key]); |
| 125 | |
| 126 | it->second = value + 1; |
| 127 | EXPECT_EQ(value + 1, map_[key]); |
| 128 | it->second = value; |
| 129 | EXPECT_EQ(value, map_[key]); |
| 130 | |
| 131 | // copy constructor |
| 132 | Map<int32, int32>::iterator it_copy = it; |
| 133 | EXPECT_EQ(key, it_copy->first); |
| 134 | EXPECT_EQ(value, it_copy->second); |
| 135 | |
| 136 | // Immutable API ================================================ |
| 137 | |
| 138 | // Check immutable at and find work correctly. |
| 139 | EXPECT_EQ(value, const_map_.at(key)); |
| 140 | Map<int32, int32>::const_iterator const_it = const_map_.find(key); |
| 141 | |
| 142 | // interator dereferenceable |
| 143 | EXPECT_EQ(key, (*const_it).first); |
| 144 | EXPECT_EQ(value, (*const_it).second); |
| 145 | EXPECT_EQ(key, const_it->first); |
| 146 | EXPECT_EQ(value, const_it->second); |
| 147 | |
| 148 | // copy constructor |
| 149 | Map<int32, int32>::const_iterator const_it_copy = const_it; |
| 150 | EXPECT_EQ(key, const_it_copy->first); |
| 151 | EXPECT_EQ(value, const_it_copy->second); |
| 152 | } |
| 153 | |
| 154 | google::protobuf::scoped_ptr<Map<int32, int32> > map_ptr_; |
| 155 | Map<int32, int32>& map_; |
| 156 | const Map<int32, int32>& const_map_; |
| 157 | }; |
| 158 | |
| 159 | TEST_F(MapImplTest, OperatorBracket) { |
| 160 | int32 key = 0; |
| 161 | int32 value1 = 100; |
| 162 | int32 value2 = 101; |
| 163 | |
| 164 | EXPECT_EQ(0, map_[key]); |
| 165 | |
| 166 | map_[key] = value1; |
| 167 | ExpectSingleElement(key, value1); |
| 168 | |
| 169 | map_[key] = value2; |
| 170 | ExpectSingleElement(key, value2); |
| 171 | } |
| 172 | |
| 173 | TEST_F(MapImplTest, OperatorBracketNonExist) { |
| 174 | int32 key = 0; |
| 175 | int32 default_value = 0; |
| 176 | |
| 177 | EXPECT_EQ(default_value, map_[key]); |
| 178 | ExpectSingleElement(key, default_value); |
| 179 | } |
| 180 | |
| 181 | TEST_F(MapImplTest, MutableAt) { |
| 182 | int32 key = 0; |
| 183 | int32 value1 = 100; |
| 184 | int32 value2 = 101; |
| 185 | |
| 186 | map_[key] = value1; |
| 187 | ExpectSingleElement(key, value1); |
| 188 | |
| 189 | map_.at(key) = value2; |
| 190 | ExpectSingleElement(key, value2); |
| 191 | } |
| 192 | |
Feng Xiao | 58dfce9 | 2014-12-03 12:12:17 -0800 | [diff] [blame] | 193 | #ifdef PROTOBUF_HAS_DEATH_TEST |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 194 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 195 | TEST_F(MapImplTest, MutableAtNonExistDeathTest) { |
| 196 | EXPECT_DEATH(map_.at(0), ""); |
| 197 | } |
| 198 | |
| 199 | TEST_F(MapImplTest, ImmutableAtNonExistDeathTest) { |
| 200 | EXPECT_DEATH(const_map_.at(0), ""); |
| 201 | } |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 202 | |
Feng Xiao | 58dfce9 | 2014-12-03 12:12:17 -0800 | [diff] [blame] | 203 | #endif // PROTOBUF_HAS_DEATH_TEST |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 204 | |
| 205 | TEST_F(MapImplTest, CountNonExist) { |
| 206 | EXPECT_EQ(0, map_.count(0)); |
| 207 | } |
| 208 | |
| 209 | TEST_F(MapImplTest, MutableFindNonExist) { |
| 210 | EXPECT_TRUE(map_.end() == map_.find(0)); |
| 211 | } |
| 212 | |
| 213 | TEST_F(MapImplTest, ImmutableFindNonExist) { |
| 214 | EXPECT_TRUE(const_map_.end() == const_map_.find(0)); |
| 215 | } |
| 216 | |
| 217 | TEST_F(MapImplTest, ConstEnd) { |
| 218 | EXPECT_TRUE(const_map_.end() == const_map_.cend()); |
| 219 | } |
| 220 | |
| 221 | TEST_F(MapImplTest, GetReferenceFromIterator) { |
| 222 | for (int i = 0; i < 10; i++) { |
| 223 | map_[i] = i; |
| 224 | } |
| 225 | |
| 226 | for (Map<int32, int32>::const_iterator it = map_.cbegin(); |
| 227 | it != map_.cend();) { |
| 228 | Map<int32, int32>::const_reference entry = *it++; |
| 229 | EXPECT_EQ(entry.first, entry.second); |
| 230 | } |
| 231 | |
| 232 | for (Map<int32, int32>::const_iterator it = const_map_.begin(); |
| 233 | it != const_map_.end();) { |
| 234 | Map<int32, int32>::const_reference entry = *it++; |
| 235 | EXPECT_EQ(entry.first, entry.second); |
| 236 | } |
| 237 | |
| 238 | for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) { |
| 239 | Map<int32, int32>::reference entry = *it++; |
| 240 | EXPECT_EQ(entry.first + 1, ++entry.second); |
| 241 | } |
| 242 | } |
| 243 | |
Feng Xiao | 99aa0f9 | 2014-11-20 16:18:53 -0800 | [diff] [blame] | 244 | TEST_F(MapImplTest, IteratorBasic) { |
| 245 | map_[0] = 0; |
| 246 | |
| 247 | // Default constructible (per forward iterator requirements). |
| 248 | Map<int, int>::const_iterator cit; |
| 249 | Map<int, int>::iterator it; |
| 250 | |
| 251 | it = map_.begin(); |
| 252 | cit = it; // Converts to const_iterator |
| 253 | |
| 254 | // Can compare between them. |
| 255 | EXPECT_TRUE(it == cit); |
| 256 | EXPECT_FALSE(cit != it); |
| 257 | |
| 258 | // Pre increment. |
| 259 | EXPECT_FALSE(it == ++cit); |
| 260 | |
| 261 | // Post increment. |
| 262 | EXPECT_FALSE(it++ == cit); |
| 263 | EXPECT_TRUE(it == cit); |
| 264 | } |
| 265 | |
| 266 | template <typename T> |
| 267 | bool IsConstHelper(T& /*t*/) { // NOLINT. We want to catch non-const refs here. |
| 268 | return false; |
| 269 | } |
| 270 | template <typename T> |
| 271 | bool IsConstHelper(const T& /*t*/) { |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | TEST_F(MapImplTest, IteratorConstness) { |
| 276 | map_[0] = 0; |
| 277 | EXPECT_TRUE(IsConstHelper(*map_.cbegin())); |
| 278 | EXPECT_TRUE(IsConstHelper(*const_map_.begin())); |
| 279 | EXPECT_FALSE(IsConstHelper(*map_.begin())); |
| 280 | } |
| 281 | |
| 282 | bool IsForwardIteratorHelper(std::forward_iterator_tag /*tag*/) { return true; } |
| 283 | template <typename T> |
| 284 | bool IsForwardIteratorHelper(T /*t*/) { |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | TEST_F(MapImplTest, IteratorCategory) { |
| 289 | EXPECT_TRUE(IsForwardIteratorHelper( |
| 290 | std::iterator_traits<Map<int, int>::iterator>::iterator_category())); |
| 291 | EXPECT_TRUE(IsForwardIteratorHelper(std::iterator_traits< |
| 292 | Map<int, int>::const_iterator>::iterator_category())); |
| 293 | } |
| 294 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 295 | TEST_F(MapImplTest, InsertSingle) { |
| 296 | int32 key = 0; |
| 297 | int32 value1 = 100; |
| 298 | int32 value2 = 101; |
| 299 | |
| 300 | // Insert a non-existed key. |
| 301 | std::pair<Map<int32, int32>::iterator, bool> result1 = |
| 302 | map_.insert(Map<int32, int32>::value_type(key, value1)); |
| 303 | ExpectSingleElement(key, value1); |
| 304 | |
| 305 | Map<int32, int32>::iterator it1 = result1.first; |
| 306 | EXPECT_EQ(key, it1->first); |
| 307 | EXPECT_EQ(value1, it1->second); |
| 308 | EXPECT_TRUE(result1.second); |
| 309 | |
| 310 | // Insert an existed key. |
| 311 | std::pair<Map<int32, int32>::iterator, bool> result2 = |
| 312 | map_.insert(Map<int32, int32>::value_type(key, value2)); |
| 313 | ExpectSingleElement(key, value1); |
| 314 | |
| 315 | Map<int32, int32>::iterator it2 = result2.first; |
| 316 | EXPECT_TRUE(it1 == it2); |
| 317 | EXPECT_FALSE(result2.second); |
| 318 | } |
| 319 | |
| 320 | TEST_F(MapImplTest, InsertByIterator) { |
| 321 | int32 key1 = 0; |
| 322 | int32 key2 = 1; |
| 323 | int32 value1a = 100; |
| 324 | int32 value1b = 101; |
| 325 | int32 value2a = 200; |
| 326 | int32 value2b = 201; |
| 327 | |
| 328 | std::map<int32, int32> map1; |
| 329 | map1[key1] = value1a; |
| 330 | map1[key2] = value2a; |
| 331 | |
| 332 | map_.insert(map1.begin(), map1.end()); |
| 333 | ExpectElements(map1); |
| 334 | |
| 335 | std::map<int32, int32> map2; |
| 336 | map2[key1] = value1b; |
| 337 | map2[key2] = value2b; |
| 338 | |
| 339 | map_.insert(map2.begin(), map2.end()); |
| 340 | ExpectElements(map1); |
| 341 | } |
| 342 | |
| 343 | TEST_F(MapImplTest, EraseSingleByKey) { |
| 344 | int32 key = 0; |
| 345 | int32 value = 100; |
| 346 | |
| 347 | map_[key] = value; |
| 348 | ExpectSingleElement(key, value); |
| 349 | |
| 350 | // Erase an existing key. |
| 351 | EXPECT_EQ(1, map_.erase(key)); |
| 352 | EXPECT_TRUE(map_.empty()); |
| 353 | EXPECT_EQ(0, map_.size()); |
| 354 | EXPECT_TRUE(map_.end() == map_.find(key)); |
| 355 | EXPECT_TRUE(map_.begin() == map_.end()); |
| 356 | |
| 357 | // Erase a non-existing key. |
| 358 | EXPECT_EQ(0, map_.erase(key)); |
| 359 | } |
| 360 | |
| 361 | TEST_F(MapImplTest, EraseMutipleByKey) { |
| 362 | // erase in one specific order to trigger corner cases |
| 363 | for (int i = 0; i < 5; i++) { |
| 364 | map_[i] = i; |
| 365 | } |
| 366 | |
| 367 | map_.erase(0); |
| 368 | EXPECT_EQ(4, map_.size()); |
| 369 | EXPECT_TRUE(map_.end() == map_.find(0)); |
| 370 | |
| 371 | map_.erase(1); |
| 372 | EXPECT_EQ(3, map_.size()); |
| 373 | EXPECT_TRUE(map_.end() == map_.find(1)); |
| 374 | |
| 375 | map_.erase(3); |
| 376 | EXPECT_EQ(2, map_.size()); |
| 377 | EXPECT_TRUE(map_.end() == map_.find(3)); |
| 378 | |
| 379 | map_.erase(4); |
| 380 | EXPECT_EQ(1, map_.size()); |
| 381 | EXPECT_TRUE(map_.end() == map_.find(4)); |
| 382 | |
| 383 | map_.erase(2); |
| 384 | EXPECT_EQ(0, map_.size()); |
| 385 | EXPECT_TRUE(map_.end() == map_.find(2)); |
| 386 | } |
| 387 | |
| 388 | TEST_F(MapImplTest, EraseSingleByIterator) { |
| 389 | int32 key = 0; |
| 390 | int32 value = 100; |
| 391 | |
| 392 | map_[key] = value; |
| 393 | ExpectSingleElement(key, value); |
| 394 | |
| 395 | Map<int32, int32>::iterator it = map_.find(key); |
| 396 | map_.erase(it); |
| 397 | EXPECT_TRUE(map_.empty()); |
| 398 | EXPECT_EQ(0, map_.size()); |
| 399 | EXPECT_TRUE(map_.end() == map_.find(key)); |
| 400 | EXPECT_TRUE(map_.begin() == map_.end()); |
| 401 | } |
| 402 | |
| 403 | TEST_F(MapImplTest, ValidIteratorAfterErase) { |
| 404 | for (int i = 0; i < 10; i++) { |
| 405 | map_[i] = i; |
| 406 | } |
| 407 | |
| 408 | int count = 0; |
| 409 | |
| 410 | for (Map<int32, int32>::iterator it = map_.begin(); it != map_.end();) { |
| 411 | count++; |
| 412 | if (it->first % 2 == 1) { |
| 413 | map_.erase(it++); |
| 414 | } else { |
| 415 | ++it; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | EXPECT_EQ(10, count); |
| 420 | EXPECT_EQ(5, map_.size()); |
| 421 | } |
| 422 | |
| 423 | TEST_F(MapImplTest, EraseByIterator) { |
| 424 | int32 key1 = 0; |
| 425 | int32 key2 = 1; |
| 426 | int32 value1 = 100; |
| 427 | int32 value2 = 101; |
| 428 | |
| 429 | std::map<int32, int32> map; |
| 430 | map[key1] = value1; |
| 431 | map[key2] = value2; |
| 432 | |
| 433 | map_.insert(map.begin(), map.end()); |
| 434 | ExpectElements(map); |
| 435 | |
| 436 | map_.erase(map_.begin(), map_.end()); |
| 437 | EXPECT_TRUE(map_.empty()); |
| 438 | EXPECT_EQ(0, map_.size()); |
| 439 | EXPECT_TRUE(map_.end() == map_.find(key1)); |
| 440 | EXPECT_TRUE(map_.end() == map_.find(key2)); |
| 441 | EXPECT_TRUE(map_.begin() == map_.end()); |
| 442 | } |
| 443 | |
| 444 | TEST_F(MapImplTest, Clear) { |
| 445 | int32 key = 0; |
| 446 | int32 value = 100; |
| 447 | |
| 448 | map_[key] = value; |
| 449 | ExpectSingleElement(key, value); |
| 450 | |
| 451 | map_.clear(); |
| 452 | |
| 453 | EXPECT_TRUE(map_.empty()); |
| 454 | EXPECT_EQ(0, map_.size()); |
| 455 | EXPECT_TRUE(map_.end() == map_.find(key)); |
| 456 | EXPECT_TRUE(map_.begin() == map_.end()); |
| 457 | } |
| 458 | |
| 459 | TEST_F(MapImplTest, CopyConstructor) { |
| 460 | int32 key1 = 0; |
| 461 | int32 key2 = 1; |
| 462 | int32 value1 = 100; |
| 463 | int32 value2 = 101; |
| 464 | |
| 465 | std::map<int32, int32> map; |
| 466 | map[key1] = value1; |
| 467 | map[key2] = value2; |
| 468 | |
| 469 | map_.insert(map.begin(), map.end()); |
| 470 | |
| 471 | Map<int32, int32> other(map_); |
| 472 | |
| 473 | EXPECT_EQ(2, other.size()); |
| 474 | EXPECT_EQ(value1, other.at(key1)); |
| 475 | EXPECT_EQ(value2, other.at(key2)); |
| 476 | } |
| 477 | |
| 478 | TEST_F(MapImplTest, Assigner) { |
| 479 | int32 key1 = 0; |
| 480 | int32 key2 = 1; |
| 481 | int32 value1 = 100; |
| 482 | int32 value2 = 101; |
| 483 | |
| 484 | std::map<int32, int32> map; |
| 485 | map[key1] = value1; |
| 486 | map[key2] = value2; |
| 487 | |
| 488 | map_.insert(map.begin(), map.end()); |
| 489 | |
| 490 | Map<int32, int32> other; |
Feng Xiao | 99aa0f9 | 2014-11-20 16:18:53 -0800 | [diff] [blame] | 491 | int32 key_other = 123; |
| 492 | int32 value_other = 321; |
| 493 | other[key_other] = value_other; |
| 494 | EXPECT_EQ(1, other.size()); |
| 495 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 496 | other = map_; |
| 497 | |
| 498 | EXPECT_EQ(2, other.size()); |
| 499 | EXPECT_EQ(value1, other.at(key1)); |
| 500 | EXPECT_EQ(value2, other.at(key2)); |
Feng Xiao | 99aa0f9 | 2014-11-20 16:18:53 -0800 | [diff] [blame] | 501 | EXPECT_TRUE(other.find(key_other) == other.end()); |
| 502 | |
| 503 | // Self assign |
| 504 | other = other; |
| 505 | EXPECT_EQ(2, other.size()); |
| 506 | EXPECT_EQ(value1, other.at(key1)); |
| 507 | EXPECT_EQ(value2, other.at(key2)); |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | TEST_F(MapImplTest, Rehash) { |
| 511 | const int test_size = 50; |
| 512 | std::map<int32, int32> reference_map; |
| 513 | for (int i = 0; i < test_size; i++) { |
| 514 | reference_map[i] = i; |
| 515 | } |
| 516 | for (int i = 0; i < test_size; i++) { |
| 517 | map_[i] = reference_map[i]; |
| 518 | EXPECT_EQ(reference_map[i], map_[i]); |
| 519 | } |
| 520 | for (int i = 0; i < test_size; i++) { |
| 521 | map_.erase(i); |
| 522 | EXPECT_TRUE(map_.end() == map_.find(i)); |
| 523 | } |
| 524 | EXPECT_TRUE(map_.empty()); |
| 525 | } |
| 526 | |
Feng Xiao | 99aa0f9 | 2014-11-20 16:18:53 -0800 | [diff] [blame] | 527 | TEST_F(MapImplTest, EqualRange) { |
| 528 | int key = 100, key_missing = 101; |
| 529 | map_[key] = 100; |
| 530 | |
| 531 | std::pair<google::protobuf::Map<int32, int32>::iterator, |
| 532 | google::protobuf::Map<int32, int32>::iterator> range = map_.equal_range(key); |
| 533 | EXPECT_TRUE(map_.find(key) == range.first); |
| 534 | EXPECT_TRUE(++map_.find(key) == range.second); |
| 535 | |
| 536 | range = map_.equal_range(key_missing); |
| 537 | EXPECT_TRUE(map_.end() == range.first); |
| 538 | EXPECT_TRUE(map_.end() == range.second); |
| 539 | |
| 540 | std::pair<google::protobuf::Map<int32, int32>::const_iterator, |
| 541 | google::protobuf::Map<int32, int32>::const_iterator> const_range = |
| 542 | const_map_.equal_range(key); |
| 543 | EXPECT_TRUE(const_map_.find(key) == const_range.first); |
| 544 | EXPECT_TRUE(++const_map_.find(key) == const_range.second); |
| 545 | |
| 546 | const_range = const_map_.equal_range(key_missing); |
| 547 | EXPECT_TRUE(const_map_.end() == const_range.first); |
| 548 | EXPECT_TRUE(const_map_.end() == const_range.second); |
| 549 | } |
| 550 | |
Feng Xiao | 6ae3bde | 2014-11-25 14:01:44 -0800 | [diff] [blame] | 551 | TEST_F(MapImplTest, ConvertToStdMap) { |
| 552 | map_[100] = 101; |
| 553 | std::map<int32, int32> std_map(map_.begin(), map_.end()); |
| 554 | EXPECT_EQ(1, std_map.size()); |
| 555 | EXPECT_EQ(101, std_map[100]); |
| 556 | } |
| 557 | |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 558 | TEST_F(MapImplTest, ConvertToStdVectorOfPairs) { |
| 559 | map_[100] = 101; |
| 560 | std::vector<std::pair<int32, int32> > std_vec(map_.begin(), map_.end()); |
| 561 | EXPECT_EQ(1, std_vec.size()); |
| 562 | EXPECT_EQ(100, std_vec[0].first); |
| 563 | EXPECT_EQ(101, std_vec[0].second); |
| 564 | } |
| 565 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 566 | // Map Field Reflection Test ======================================== |
| 567 | |
| 568 | static int Func(int i, int j) { |
| 569 | return i * j; |
| 570 | } |
| 571 | |
| 572 | static string StrFunc(int i, int j) { |
| 573 | string str; |
| 574 | SStringPrintf(&str, "%d", Func(i, j)); |
| 575 | return str; |
| 576 | } |
| 577 | |
| 578 | static int Int(const string& value) { |
| 579 | int result = 0; |
| 580 | std::istringstream(value) >> result; |
| 581 | return result; |
| 582 | } |
| 583 | |
| 584 | class MapFieldReflectionTest : public testing::Test { |
| 585 | protected: |
| 586 | typedef FieldDescriptor FD; |
| 587 | }; |
| 588 | |
| 589 | TEST_F(MapFieldReflectionTest, RegularFields) { |
| 590 | TestMap message; |
| 591 | const Reflection* refl = message.GetReflection(); |
| 592 | const Descriptor* desc = message.GetDescriptor(); |
| 593 | |
| 594 | Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32(); |
| 595 | Map<int32, double>* map_int32_double = message.mutable_map_int32_double(); |
| 596 | Map<string, string>* map_string_string = message.mutable_map_string_string(); |
| 597 | Map<int32, ForeignMessage>* map_int32_foreign_message = |
| 598 | message.mutable_map_int32_foreign_message(); |
| 599 | |
| 600 | for (int i = 0; i < 10; ++i) { |
| 601 | (*map_int32_int32)[i] = Func(i, 1); |
| 602 | (*map_int32_double)[i] = Func(i, 2); |
| 603 | (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5); |
| 604 | (*map_int32_foreign_message)[i].set_c(Func(i, 6)); |
| 605 | } |
| 606 | |
| 607 | // Get FieldDescriptors for all the fields of interest. |
| 608 | const FieldDescriptor* fd_map_int32_int32 = |
| 609 | desc->FindFieldByName("map_int32_int32"); |
| 610 | const FieldDescriptor* fd_map_int32_double = |
| 611 | desc->FindFieldByName("map_int32_double"); |
| 612 | const FieldDescriptor* fd_map_string_string = |
| 613 | desc->FindFieldByName("map_string_string"); |
| 614 | const FieldDescriptor* fd_map_int32_foreign_message = |
| 615 | desc->FindFieldByName("map_int32_foreign_message"); |
| 616 | |
| 617 | const FieldDescriptor* fd_map_int32_in32_key = |
| 618 | fd_map_int32_int32->message_type()->FindFieldByName("key"); |
| 619 | const FieldDescriptor* fd_map_int32_in32_value = |
| 620 | fd_map_int32_int32->message_type()->FindFieldByName("value"); |
| 621 | const FieldDescriptor* fd_map_int32_double_key = |
| 622 | fd_map_int32_double->message_type()->FindFieldByName("key"); |
| 623 | const FieldDescriptor* fd_map_int32_double_value = |
| 624 | fd_map_int32_double->message_type()->FindFieldByName("value"); |
| 625 | const FieldDescriptor* fd_map_string_string_key = |
| 626 | fd_map_string_string->message_type()->FindFieldByName("key"); |
| 627 | const FieldDescriptor* fd_map_string_string_value = |
| 628 | fd_map_string_string->message_type()->FindFieldByName("value"); |
| 629 | const FieldDescriptor* fd_map_int32_foreign_message_key = |
| 630 | fd_map_int32_foreign_message->message_type()->FindFieldByName("key"); |
| 631 | const FieldDescriptor* fd_map_int32_foreign_message_value = |
| 632 | fd_map_int32_foreign_message->message_type()->FindFieldByName("value"); |
| 633 | |
| 634 | // Get RepeatedPtrField objects for all fields of interest. |
| 635 | const RepeatedPtrField<Message>& mf_int32_int32 = |
| 636 | refl->GetRepeatedPtrField<Message>(message, fd_map_int32_int32); |
| 637 | const RepeatedPtrField<Message>& mf_int32_double = |
| 638 | refl->GetRepeatedPtrField<Message>(message, fd_map_int32_double); |
| 639 | const RepeatedPtrField<Message>& mf_string_string = |
| 640 | refl->GetRepeatedPtrField<Message>(message, fd_map_string_string); |
| 641 | const RepeatedPtrField<Message>& |
| 642 | mf_int32_foreign_message = |
| 643 | refl->GetRepeatedPtrField<Message>( |
| 644 | message, fd_map_int32_foreign_message); |
| 645 | |
| 646 | // Get mutable RepeatedPtrField objects for all fields of interest. |
| 647 | RepeatedPtrField<Message>* mmf_int32_int32 = |
| 648 | refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_int32); |
| 649 | RepeatedPtrField<Message>* mmf_int32_double = |
| 650 | refl->MutableRepeatedPtrField<Message>(&message, fd_map_int32_double); |
| 651 | RepeatedPtrField<Message>* mmf_string_string = |
| 652 | refl->MutableRepeatedPtrField<Message>(&message, fd_map_string_string); |
| 653 | RepeatedPtrField<Message>* mmf_int32_foreign_message = |
| 654 | refl->MutableRepeatedPtrField<Message>( |
| 655 | &message, fd_map_int32_foreign_message); |
| 656 | |
| 657 | // Make sure we can do gets through the RepeatedPtrField objects. |
| 658 | for (int i = 0; i < 10; ++i) { |
| 659 | { |
| 660 | // Check gets through const objects. |
| 661 | const Message& message_int32_int32 = mf_int32_int32.Get(i); |
| 662 | int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32( |
| 663 | message_int32_int32, fd_map_int32_in32_key); |
| 664 | int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32( |
| 665 | message_int32_int32, fd_map_int32_in32_value); |
| 666 | EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1)); |
| 667 | |
| 668 | const Message& message_int32_double = mf_int32_double.Get(i); |
| 669 | int32 key_int32_double = message_int32_double.GetReflection()->GetInt32( |
| 670 | message_int32_double, fd_map_int32_double_key); |
| 671 | double value_int32_double = |
| 672 | message_int32_double.GetReflection()->GetDouble( |
| 673 | message_int32_double, fd_map_int32_double_value); |
| 674 | EXPECT_EQ(value_int32_double, Func(key_int32_double, 2)); |
| 675 | |
| 676 | const Message& message_string_string = mf_string_string.Get(i); |
| 677 | string key_string_string = |
| 678 | message_string_string.GetReflection()->GetString( |
| 679 | message_string_string, fd_map_string_string_key); |
| 680 | string value_string_string = |
| 681 | message_string_string.GetReflection()->GetString( |
| 682 | message_string_string, fd_map_string_string_value); |
| 683 | EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5)); |
| 684 | |
| 685 | const Message& message_int32_message = mf_int32_foreign_message.Get(i); |
| 686 | int32 key_int32_message = message_int32_message.GetReflection()->GetInt32( |
| 687 | message_int32_message, fd_map_int32_foreign_message_key); |
| 688 | const ForeignMessage& value_int32_message = |
| 689 | down_cast<const ForeignMessage&>( |
| 690 | message_int32_message.GetReflection() |
| 691 | ->GetMessage(message_int32_message, |
| 692 | fd_map_int32_foreign_message_value)); |
| 693 | EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); |
| 694 | } |
| 695 | |
| 696 | { |
| 697 | // Check gets through mutable objects. |
| 698 | const Message& message_int32_int32 = mmf_int32_int32->Get(i); |
| 699 | int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32( |
| 700 | message_int32_int32, fd_map_int32_in32_key); |
| 701 | int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32( |
| 702 | message_int32_int32, fd_map_int32_in32_value); |
| 703 | EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1)); |
| 704 | |
| 705 | const Message& message_int32_double = mmf_int32_double->Get(i); |
| 706 | int32 key_int32_double = message_int32_double.GetReflection()->GetInt32( |
| 707 | message_int32_double, fd_map_int32_double_key); |
| 708 | double value_int32_double = |
| 709 | message_int32_double.GetReflection()->GetDouble( |
| 710 | message_int32_double, fd_map_int32_double_value); |
| 711 | EXPECT_EQ(value_int32_double, Func(key_int32_double, 2)); |
| 712 | |
| 713 | const Message& message_string_string = mmf_string_string->Get(i); |
| 714 | string key_string_string = |
| 715 | message_string_string.GetReflection()->GetString( |
| 716 | message_string_string, fd_map_string_string_key); |
| 717 | string value_string_string = |
| 718 | message_string_string.GetReflection()->GetString( |
| 719 | message_string_string, fd_map_string_string_value); |
| 720 | EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5)); |
| 721 | |
| 722 | const Message& message_int32_message = mmf_int32_foreign_message->Get(i); |
| 723 | int32 key_int32_message = message_int32_message.GetReflection()->GetInt32( |
| 724 | message_int32_message, fd_map_int32_foreign_message_key); |
| 725 | const ForeignMessage& value_int32_message = |
| 726 | down_cast<const ForeignMessage&>( |
| 727 | message_int32_message.GetReflection() |
| 728 | ->GetMessage(message_int32_message, |
| 729 | fd_map_int32_foreign_message_value)); |
| 730 | EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | // Do sets through the RepeatedPtrField objects. |
| 735 | for (int i = 0; i < 10; i++) { |
| 736 | { |
| 737 | Message* message_int32_int32 = mmf_int32_int32->Mutable(i); |
| 738 | int32 key_int32_int32 = message_int32_int32->GetReflection()->GetInt32( |
| 739 | *message_int32_int32, fd_map_int32_in32_key); |
| 740 | message_int32_int32->GetReflection()->SetInt32(message_int32_int32, |
| 741 | fd_map_int32_in32_value, |
| 742 | Func(key_int32_int32, -1)); |
| 743 | |
| 744 | Message* message_int32_double = mmf_int32_double->Mutable(i); |
| 745 | int32 key_int32_double = message_int32_double->GetReflection()->GetInt32( |
| 746 | *message_int32_double, fd_map_int32_double_key); |
| 747 | message_int32_double->GetReflection()->SetDouble( |
| 748 | message_int32_double, fd_map_int32_double_value, |
| 749 | Func(key_int32_double, -2)); |
| 750 | |
| 751 | Message* message_string_string = mmf_string_string->Mutable(i); |
| 752 | string key_string_string = |
| 753 | message_string_string->GetReflection()->GetString( |
| 754 | *message_string_string, fd_map_string_string_key); |
| 755 | message_string_string->GetReflection()->SetString( |
| 756 | message_string_string, fd_map_string_string_value, |
| 757 | StrFunc(Int(key_string_string), -5)); |
| 758 | |
| 759 | Message* message_int32_message = mmf_int32_foreign_message->Mutable(i); |
| 760 | int32 key_int32_message = |
| 761 | message_int32_message->GetReflection()->GetInt32( |
| 762 | *message_int32_message, fd_map_int32_foreign_message_key); |
| 763 | ForeignMessage* value_int32_message = down_cast<ForeignMessage*>( |
| 764 | message_int32_message->GetReflection() |
| 765 | ->MutableMessage(message_int32_message, |
| 766 | fd_map_int32_foreign_message_value)); |
| 767 | value_int32_message->set_c(Func(key_int32_message, -6)); |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | // Check gets through mutable objects. |
| 772 | for (int i = 0; i < 10; i++) { |
| 773 | EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i)); |
| 774 | EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i)); |
| 775 | EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1))); |
| 776 | EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c()); |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | TEST_F(MapFieldReflectionTest, RepeatedFieldRefForRegularFields) { |
| 781 | TestMap message; |
| 782 | const Reflection* refl = message.GetReflection(); |
| 783 | const Descriptor* desc = message.GetDescriptor(); |
| 784 | |
| 785 | Map<int32, int32>* map_int32_int32 = message.mutable_map_int32_int32(); |
| 786 | Map<int32, double>* map_int32_double = message.mutable_map_int32_double(); |
| 787 | Map<string, string>* map_string_string = message.mutable_map_string_string(); |
| 788 | Map<int32, ForeignMessage>* map_int32_foreign_message = |
| 789 | message.mutable_map_int32_foreign_message(); |
| 790 | |
| 791 | for (int i = 0; i < 10; ++i) { |
| 792 | (*map_int32_int32)[i] = Func(i, 1); |
| 793 | (*map_int32_double)[i] = Func(i, 2); |
| 794 | (*map_string_string)[StrFunc(i, 1)] = StrFunc(i, 5); |
| 795 | (*map_int32_foreign_message)[i].set_c(Func(i, 6)); |
| 796 | } |
| 797 | |
| 798 | // Get FieldDescriptors for all the fields of interest. |
| 799 | const FieldDescriptor* fd_map_int32_int32 = |
| 800 | desc->FindFieldByName("map_int32_int32"); |
| 801 | const FieldDescriptor* fd_map_int32_double = |
| 802 | desc->FindFieldByName("map_int32_double"); |
| 803 | const FieldDescriptor* fd_map_string_string = |
| 804 | desc->FindFieldByName("map_string_string"); |
| 805 | const FieldDescriptor* fd_map_int32_foreign_message = |
| 806 | desc->FindFieldByName("map_int32_foreign_message"); |
| 807 | |
| 808 | const FieldDescriptor* fd_map_int32_in32_key = |
| 809 | fd_map_int32_int32->message_type()->FindFieldByName("key"); |
| 810 | const FieldDescriptor* fd_map_int32_in32_value = |
| 811 | fd_map_int32_int32->message_type()->FindFieldByName("value"); |
| 812 | const FieldDescriptor* fd_map_int32_double_key = |
| 813 | fd_map_int32_double->message_type()->FindFieldByName("key"); |
| 814 | const FieldDescriptor* fd_map_int32_double_value = |
| 815 | fd_map_int32_double->message_type()->FindFieldByName("value"); |
| 816 | const FieldDescriptor* fd_map_string_string_key = |
| 817 | fd_map_string_string->message_type()->FindFieldByName("key"); |
| 818 | const FieldDescriptor* fd_map_string_string_value = |
| 819 | fd_map_string_string->message_type()->FindFieldByName("value"); |
| 820 | const FieldDescriptor* fd_map_int32_foreign_message_key = |
| 821 | fd_map_int32_foreign_message->message_type()->FindFieldByName("key"); |
| 822 | const FieldDescriptor* fd_map_int32_foreign_message_value = |
| 823 | fd_map_int32_foreign_message->message_type()->FindFieldByName("value"); |
| 824 | |
| 825 | // Get RepeatedFieldRef objects for all fields of interest. |
| 826 | const RepeatedFieldRef<Message> mf_int32_int32 = |
| 827 | refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_int32); |
| 828 | const RepeatedFieldRef<Message> mf_int32_double = |
| 829 | refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_double); |
| 830 | const RepeatedFieldRef<Message> mf_string_string = |
| 831 | refl->GetRepeatedFieldRef<Message>(message, fd_map_string_string); |
| 832 | const RepeatedFieldRef<Message> mf_int32_foreign_message = |
| 833 | refl->GetRepeatedFieldRef<Message>(message, fd_map_int32_foreign_message); |
| 834 | |
| 835 | // Get mutable RepeatedFieldRef objects for all fields of interest. |
| 836 | const MutableRepeatedFieldRef<Message> mmf_int32_int32 = |
| 837 | refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_int32); |
| 838 | const MutableRepeatedFieldRef<Message> mmf_int32_double = |
| 839 | refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_int32_double); |
| 840 | const MutableRepeatedFieldRef<Message> mmf_string_string = |
| 841 | refl->GetMutableRepeatedFieldRef<Message>(&message, fd_map_string_string); |
| 842 | const MutableRepeatedFieldRef<Message> |
| 843 | mmf_int32_foreign_message = |
| 844 | refl->GetMutableRepeatedFieldRef<Message>( |
| 845 | &message, fd_map_int32_foreign_message); |
| 846 | |
| 847 | // Get entry default instances |
| 848 | google::protobuf::scoped_ptr<Message> entry_int32_int32( |
| 849 | MessageFactory::generated_factory() |
| 850 | ->GetPrototype(fd_map_int32_int32->message_type()) |
| 851 | ->New()); |
| 852 | google::protobuf::scoped_ptr<Message> entry_int32_double( |
| 853 | MessageFactory::generated_factory() |
| 854 | ->GetPrototype(fd_map_int32_double->message_type()) |
| 855 | ->New()); |
| 856 | google::protobuf::scoped_ptr<Message> entry_string_string( |
| 857 | MessageFactory::generated_factory() |
| 858 | ->GetPrototype(fd_map_string_string->message_type()) |
| 859 | ->New()); |
| 860 | google::protobuf::scoped_ptr<Message> entry_int32_foreign_message( |
| 861 | MessageFactory::generated_factory() |
| 862 | ->GetPrototype(fd_map_int32_foreign_message->message_type()) |
| 863 | ->New()); |
| 864 | |
| 865 | EXPECT_EQ(10, mf_int32_int32.size()); |
| 866 | EXPECT_EQ(10, mmf_int32_int32.size()); |
| 867 | EXPECT_EQ(10, mf_int32_double.size()); |
| 868 | EXPECT_EQ(10, mmf_int32_double.size()); |
| 869 | EXPECT_EQ(10, mf_string_string.size()); |
| 870 | EXPECT_EQ(10, mmf_string_string.size()); |
| 871 | EXPECT_EQ(10, mf_int32_foreign_message.size()); |
| 872 | EXPECT_EQ(10, mmf_int32_foreign_message.size()); |
| 873 | |
| 874 | EXPECT_FALSE(mf_int32_int32.empty()); |
| 875 | EXPECT_FALSE(mmf_int32_int32.empty()); |
| 876 | EXPECT_FALSE(mf_int32_double.empty()); |
| 877 | EXPECT_FALSE(mmf_int32_double.empty()); |
| 878 | EXPECT_FALSE(mf_string_string.empty()); |
| 879 | EXPECT_FALSE(mmf_string_string.empty()); |
| 880 | EXPECT_FALSE(mf_int32_foreign_message.empty()); |
| 881 | EXPECT_FALSE(mmf_int32_foreign_message.empty()); |
| 882 | |
| 883 | // Make sure we can do gets through the RepeatedFieldRef objects. |
| 884 | for (int i = 0; i < 10; ++i) { |
| 885 | { |
| 886 | // Check gets through const objects. |
| 887 | const Message& message_int32_int32 = |
| 888 | mf_int32_int32.Get(i, entry_int32_int32.get()); |
| 889 | int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32( |
| 890 | message_int32_int32, fd_map_int32_in32_key); |
| 891 | int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32( |
| 892 | message_int32_int32, fd_map_int32_in32_value); |
| 893 | EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1)); |
| 894 | |
| 895 | const Message& message_int32_double = |
| 896 | mf_int32_double.Get(i, entry_int32_double.get()); |
| 897 | int32 key_int32_double = message_int32_double.GetReflection()->GetInt32( |
| 898 | message_int32_double, fd_map_int32_double_key); |
| 899 | double value_int32_double = |
| 900 | message_int32_double.GetReflection()->GetDouble( |
| 901 | message_int32_double, fd_map_int32_double_value); |
| 902 | EXPECT_EQ(value_int32_double, Func(key_int32_double, 2)); |
| 903 | |
| 904 | const Message& message_string_string = |
| 905 | mf_string_string.Get(i, entry_string_string.get()); |
| 906 | string key_string_string = |
| 907 | message_string_string.GetReflection()->GetString( |
| 908 | message_string_string, fd_map_string_string_key); |
| 909 | string value_string_string = |
| 910 | message_string_string.GetReflection()->GetString( |
| 911 | message_string_string, fd_map_string_string_value); |
| 912 | EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5)); |
| 913 | |
| 914 | const Message& message_int32_message = |
| 915 | mf_int32_foreign_message.Get(i, entry_int32_foreign_message.get()); |
| 916 | int32 key_int32_message = message_int32_message.GetReflection()->GetInt32( |
| 917 | message_int32_message, fd_map_int32_foreign_message_key); |
| 918 | const ForeignMessage& value_int32_message = |
| 919 | down_cast<const ForeignMessage&>( |
| 920 | message_int32_message.GetReflection() |
| 921 | ->GetMessage(message_int32_message, |
| 922 | fd_map_int32_foreign_message_value)); |
| 923 | EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); |
| 924 | } |
| 925 | |
| 926 | { |
| 927 | // Check gets through mutable objects. |
| 928 | const Message& message_int32_int32 = |
| 929 | mmf_int32_int32.Get(i, entry_int32_int32.get()); |
| 930 | int32 key_int32_int32 = message_int32_int32.GetReflection()->GetInt32( |
| 931 | message_int32_int32, fd_map_int32_in32_key); |
| 932 | int32 value_int32_int32 = message_int32_int32.GetReflection()->GetInt32( |
| 933 | message_int32_int32, fd_map_int32_in32_value); |
| 934 | EXPECT_EQ(value_int32_int32, Func(key_int32_int32, 1)); |
| 935 | |
| 936 | const Message& message_int32_double = |
| 937 | mmf_int32_double.Get(i, entry_int32_double.get()); |
| 938 | int32 key_int32_double = message_int32_double.GetReflection()->GetInt32( |
| 939 | message_int32_double, fd_map_int32_double_key); |
| 940 | double value_int32_double = |
| 941 | message_int32_double.GetReflection()->GetDouble( |
| 942 | message_int32_double, fd_map_int32_double_value); |
| 943 | EXPECT_EQ(value_int32_double, Func(key_int32_double, 2)); |
| 944 | |
| 945 | const Message& message_string_string = |
| 946 | mmf_string_string.Get(i, entry_string_string.get()); |
| 947 | string key_string_string = |
| 948 | message_string_string.GetReflection()->GetString( |
| 949 | message_string_string, fd_map_string_string_key); |
| 950 | string value_string_string = |
| 951 | message_string_string.GetReflection()->GetString( |
| 952 | message_string_string, fd_map_string_string_value); |
| 953 | EXPECT_EQ(value_string_string, StrFunc(Int(key_string_string), 5)); |
| 954 | |
| 955 | const Message& message_int32_message = |
| 956 | mmf_int32_foreign_message.Get(i, entry_int32_foreign_message.get()); |
| 957 | int32 key_int32_message = message_int32_message.GetReflection()->GetInt32( |
| 958 | message_int32_message, fd_map_int32_foreign_message_key); |
| 959 | const ForeignMessage& value_int32_message = |
| 960 | down_cast<const ForeignMessage&>( |
| 961 | message_int32_message.GetReflection() |
| 962 | ->GetMessage(message_int32_message, |
| 963 | fd_map_int32_foreign_message_value)); |
| 964 | EXPECT_EQ(value_int32_message.c(), Func(key_int32_message, 6)); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | // Make sure we can do sets through the RepeatedFieldRef objects. |
| 969 | for (int i = 0; i < 10; i++) { |
| 970 | const Message& message_int32_int32 = |
| 971 | mmf_int32_int32.Get(i, entry_int32_int32.get()); |
| 972 | int key = message_int32_int32.GetReflection()->GetInt32( |
| 973 | message_int32_int32, fd_map_int32_in32_key); |
| 974 | |
| 975 | entry_int32_int32->GetReflection()->SetInt32( |
| 976 | entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0), |
| 977 | key); |
| 978 | entry_int32_int32->GetReflection()->SetInt32( |
| 979 | entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1), |
| 980 | Func(key, -1)); |
| 981 | entry_int32_double->GetReflection()->SetInt32( |
| 982 | entry_int32_double.get(), fd_map_int32_double->message_type()->field(0), |
| 983 | key); |
| 984 | entry_int32_double->GetReflection()->SetDouble( |
| 985 | entry_int32_double.get(), fd_map_int32_double->message_type()->field(1), |
| 986 | Func(key, -2)); |
| 987 | entry_string_string->GetReflection()->SetString( |
Feng Xiao | 99aa0f9 | 2014-11-20 16:18:53 -0800 | [diff] [blame] | 988 | entry_string_string.get(), |
| 989 | fd_map_string_string->message_type()->field(0), StrFunc(key, 1)); |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 990 | entry_string_string->GetReflection()->SetString( |
Feng Xiao | 99aa0f9 | 2014-11-20 16:18:53 -0800 | [diff] [blame] | 991 | entry_string_string.get(), |
| 992 | fd_map_string_string->message_type()->field(1), StrFunc(key, -5)); |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 993 | entry_int32_foreign_message->GetReflection()->SetInt32( |
| 994 | entry_int32_foreign_message.get(), |
Feng Xiao | 99aa0f9 | 2014-11-20 16:18:53 -0800 | [diff] [blame] | 995 | fd_map_int32_foreign_message->message_type()->field(0), key); |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 996 | Message* value_message = |
| 997 | entry_int32_foreign_message->GetReflection()->MutableMessage( |
| 998 | entry_int32_foreign_message.get(), |
| 999 | fd_map_int32_foreign_message->message_type()->field(1)); |
| 1000 | value_message->GetReflection()->SetInt32( |
| 1001 | value_message, value_message->GetDescriptor()->FindFieldByName("c"), |
| 1002 | Func(key, -6)); |
| 1003 | |
Feng Xiao | 99aa0f9 | 2014-11-20 16:18:53 -0800 | [diff] [blame] | 1004 | mmf_int32_int32.Set(i, *entry_int32_int32); |
| 1005 | mmf_int32_double.Set(i, *entry_int32_double); |
| 1006 | mmf_string_string.Set(i, *entry_string_string); |
| 1007 | mmf_int32_foreign_message.Set(i, *entry_int32_foreign_message); |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | for (int i = 0; i < 10; i++) { |
| 1011 | EXPECT_EQ(Func(i, -1), message.map_int32_int32().at(i)); |
| 1012 | EXPECT_EQ(Func(i, -2), message.map_int32_double().at(i)); |
| 1013 | EXPECT_EQ(StrFunc(i, -5), message.map_string_string().at(StrFunc(i, 1))); |
| 1014 | EXPECT_EQ(Func(i, -6), message.map_int32_foreign_message().at(i).c()); |
| 1015 | } |
| 1016 | |
| 1017 | // Test iterators. |
| 1018 | { |
| 1019 | int index = 0; |
| 1020 | hash_map<int32, int32> result; |
| 1021 | for (RepeatedFieldRef<Message>::iterator it = mf_int32_int32.begin(); |
| 1022 | it != mf_int32_int32.end(); ++it) { |
| 1023 | const Message& message = *it; |
| 1024 | int32 key = |
| 1025 | message.GetReflection()->GetInt32(message, fd_map_int32_in32_key); |
| 1026 | int32 value = |
| 1027 | message.GetReflection()->GetInt32(message, fd_map_int32_in32_value); |
| 1028 | result[key] = value; |
| 1029 | ++index; |
| 1030 | } |
| 1031 | EXPECT_EQ(10, index); |
| 1032 | for (hash_map<int32, int32>::const_iterator it = result.begin(); |
| 1033 | it != result.end(); ++it) { |
| 1034 | EXPECT_EQ(message.map_int32_int32().at(it->first), it->second); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | { |
| 1039 | int index = 0; |
| 1040 | hash_map<int32, double> result; |
| 1041 | for (RepeatedFieldRef<Message>::iterator it = mf_int32_double.begin(); |
| 1042 | it != mf_int32_double.end(); ++it) { |
| 1043 | const Message& message = *it; |
| 1044 | int32 key = |
| 1045 | message.GetReflection()->GetInt32(message, fd_map_int32_double_key); |
| 1046 | double value = message.GetReflection()->GetDouble( |
| 1047 | message, fd_map_int32_double_value); |
| 1048 | result[key] = value; |
| 1049 | ++index; |
| 1050 | } |
| 1051 | EXPECT_EQ(10, index); |
| 1052 | for (hash_map<int32, double>::const_iterator it = result.begin(); |
| 1053 | it != result.end(); ++it) { |
| 1054 | EXPECT_EQ(message.map_int32_double().at(it->first), it->second); |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | { |
| 1059 | int index = 0; |
| 1060 | hash_map<string, string> result; |
| 1061 | for (RepeatedFieldRef<Message>::iterator it = mf_string_string.begin(); |
| 1062 | it != mf_string_string.end(); ++it) { |
| 1063 | const Message& message = *it; |
| 1064 | string key = |
| 1065 | message.GetReflection()->GetString(message, fd_map_string_string_key); |
| 1066 | string value = message.GetReflection()->GetString( |
| 1067 | message, fd_map_string_string_value); |
| 1068 | result[key] = value; |
| 1069 | ++index; |
| 1070 | } |
| 1071 | EXPECT_EQ(10, index); |
| 1072 | for (hash_map<string, string>::const_iterator it = result.begin(); |
| 1073 | it != result.end(); ++it) { |
| 1074 | EXPECT_EQ(message.map_string_string().at(it->first), it->second); |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | { |
| 1079 | int index = 0; |
| 1080 | std::map<int32, ForeignMessage> result; |
| 1081 | for (RepeatedFieldRef<Message>::iterator it = |
| 1082 | mf_int32_foreign_message.begin(); |
| 1083 | it != mf_int32_foreign_message.end(); ++it) { |
| 1084 | const Message& message = *it; |
| 1085 | int32 key = message.GetReflection()->GetInt32( |
| 1086 | message, fd_map_int32_foreign_message_key); |
| 1087 | const ForeignMessage& sub_message = down_cast<const ForeignMessage&>( |
| 1088 | message.GetReflection() |
| 1089 | ->GetMessage(message, fd_map_int32_foreign_message_value)); |
| 1090 | result[key].MergeFrom(sub_message); |
| 1091 | ++index; |
| 1092 | } |
| 1093 | EXPECT_EQ(10, index); |
| 1094 | for (std::map<int32, ForeignMessage>::const_iterator it = result.begin(); |
| 1095 | it != result.end(); ++it) { |
| 1096 | EXPECT_EQ(message.map_int32_foreign_message().at(it->first).c(), |
| 1097 | it->second.c()); |
| 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | // Test MutableRepeatedFieldRef::Add() |
| 1102 | entry_int32_int32->GetReflection()->SetInt32( |
| 1103 | entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(0), |
| 1104 | 4321); |
| 1105 | entry_int32_int32->GetReflection()->SetInt32( |
| 1106 | entry_int32_int32.get(), fd_map_int32_int32->message_type()->field(1), |
| 1107 | 1234); |
| 1108 | mmf_int32_int32.Add(*entry_int32_int32); |
| 1109 | EXPECT_EQ(1234, message.map_int32_int32().at(4321)); |
| 1110 | |
| 1111 | entry_int32_double->GetReflection()->SetInt32( |
| 1112 | entry_int32_double.get(), fd_map_int32_double->message_type()->field(0), |
| 1113 | 4321); |
| 1114 | entry_int32_double->GetReflection()->SetDouble( |
| 1115 | entry_int32_double.get(), fd_map_int32_double->message_type()->field(1), |
| 1116 | 1234.0); |
| 1117 | mmf_int32_double.Add(*entry_int32_double); |
| 1118 | EXPECT_EQ(1234.0, message.map_int32_double().at(4321)); |
| 1119 | |
| 1120 | entry_string_string->GetReflection()->SetString( |
| 1121 | entry_string_string.get(), |
| 1122 | fd_map_string_string->message_type()->field(0), "4321"); |
| 1123 | entry_string_string->GetReflection()->SetString( |
| 1124 | entry_string_string.get(), fd_map_string_string->message_type()->field(1), |
| 1125 | "1234"); |
| 1126 | mmf_string_string.Add(*entry_string_string); |
| 1127 | EXPECT_EQ("1234", message.map_string_string().at("4321")); |
| 1128 | |
| 1129 | entry_int32_foreign_message->GetReflection()->SetInt32( |
| 1130 | entry_int32_foreign_message.get(), |
| 1131 | fd_map_int32_foreign_message->message_type()->field(0), 4321); |
| 1132 | Message* value_message = |
| 1133 | entry_int32_foreign_message->GetReflection()->MutableMessage( |
| 1134 | entry_int32_foreign_message.get(), |
| 1135 | fd_map_int32_foreign_message->message_type()->field(1)); |
| 1136 | ForeignMessage foreign_message; |
| 1137 | foreign_message.set_c(1234); |
| 1138 | value_message->CopyFrom(foreign_message); |
| 1139 | |
| 1140 | mmf_int32_foreign_message.Add(*entry_int32_foreign_message); |
| 1141 | EXPECT_EQ(1234, message.map_int32_foreign_message().at(4321).c()); |
| 1142 | |
| 1143 | // Test MutableRepeatedFieldRef::RemoveLast() |
| 1144 | mmf_int32_int32.RemoveLast(); |
| 1145 | mmf_int32_double.RemoveLast(); |
| 1146 | mmf_string_string.RemoveLast(); |
| 1147 | mmf_int32_foreign_message.RemoveLast(); |
| 1148 | EXPECT_EQ(10, message.map_int32_int32().size()); |
| 1149 | EXPECT_EQ(10, message.map_int32_double().size()); |
| 1150 | EXPECT_EQ(10, message.map_string_string().size()); |
| 1151 | EXPECT_EQ(10, message.map_int32_foreign_message().size()); |
| 1152 | |
| 1153 | // Test MutableRepeatedFieldRef::SwapElements() |
| 1154 | { |
| 1155 | const Message& message0a = mmf_int32_int32.Get(0, entry_int32_int32.get()); |
| 1156 | int32 int32_value0a = |
| 1157 | message0a.GetReflection()->GetInt32(message0a, fd_map_int32_in32_value); |
| 1158 | const Message& message9a = mmf_int32_int32.Get(9, entry_int32_int32.get()); |
| 1159 | int32 int32_value9a = |
| 1160 | message9a.GetReflection()->GetInt32(message9a, fd_map_int32_in32_value); |
| 1161 | |
| 1162 | mmf_int32_int32.SwapElements(0, 9); |
| 1163 | |
| 1164 | const Message& message0b = mmf_int32_int32.Get(0, entry_int32_int32.get()); |
| 1165 | int32 int32_value0b = |
| 1166 | message0b.GetReflection()->GetInt32(message0b, fd_map_int32_in32_value); |
| 1167 | const Message& message9b = mmf_int32_int32.Get(9, entry_int32_int32.get()); |
| 1168 | int32 int32_value9b = |
| 1169 | message9b.GetReflection()->GetInt32(message9b, fd_map_int32_in32_value); |
| 1170 | |
| 1171 | EXPECT_EQ(int32_value9a, int32_value0b); |
| 1172 | EXPECT_EQ(int32_value0a, int32_value9b); |
| 1173 | } |
| 1174 | |
| 1175 | { |
| 1176 | const Message& message0a = |
| 1177 | mmf_int32_double.Get(0, entry_int32_double.get()); |
| 1178 | double double_value0a = message0a.GetReflection()->GetDouble( |
| 1179 | message0a, fd_map_int32_double_value); |
| 1180 | const Message& message9a = |
| 1181 | mmf_int32_double.Get(9, entry_int32_double.get()); |
| 1182 | double double_value9a = message9a.GetReflection()->GetDouble( |
| 1183 | message9a, fd_map_int32_double_value); |
| 1184 | |
| 1185 | mmf_int32_double.SwapElements(0, 9); |
| 1186 | |
| 1187 | const Message& message0b = |
| 1188 | mmf_int32_double.Get(0, entry_int32_double.get()); |
| 1189 | double double_value0b = message0b.GetReflection()->GetDouble( |
| 1190 | message0b, fd_map_int32_double_value); |
| 1191 | const Message& message9b = |
| 1192 | mmf_int32_double.Get(9, entry_int32_double.get()); |
| 1193 | double double_value9b = message9b.GetReflection()->GetDouble( |
| 1194 | message9b, fd_map_int32_double_value); |
| 1195 | |
| 1196 | EXPECT_EQ(double_value9a, double_value0b); |
| 1197 | EXPECT_EQ(double_value0a, double_value9b); |
| 1198 | } |
| 1199 | |
| 1200 | { |
| 1201 | const Message& message0a = |
| 1202 | mmf_string_string.Get(0, entry_string_string.get()); |
| 1203 | string string_value0a = message0a.GetReflection()->GetString( |
| 1204 | message0a, fd_map_string_string_value); |
| 1205 | const Message& message9a = |
| 1206 | mmf_string_string.Get(9, entry_string_string.get()); |
| 1207 | string string_value9a = message9a.GetReflection()->GetString( |
| 1208 | message9a, fd_map_string_string_value); |
| 1209 | |
| 1210 | mmf_string_string.SwapElements(0, 9); |
| 1211 | |
| 1212 | const Message& message0b = |
| 1213 | mmf_string_string.Get(0, entry_string_string.get()); |
| 1214 | string string_value0b = message0b.GetReflection()->GetString( |
| 1215 | message0b, fd_map_string_string_value); |
| 1216 | const Message& message9b = |
| 1217 | mmf_string_string.Get(9, entry_string_string.get()); |
| 1218 | string string_value9b = message9b.GetReflection()->GetString( |
| 1219 | message9b, fd_map_string_string_value); |
| 1220 | |
| 1221 | EXPECT_EQ(string_value9a, string_value0b); |
| 1222 | EXPECT_EQ(string_value0a, string_value9b); |
| 1223 | } |
| 1224 | |
| 1225 | { |
| 1226 | const Message& message0a = |
| 1227 | mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); |
| 1228 | const ForeignMessage& sub_message0a = down_cast<const ForeignMessage&>( |
| 1229 | message0a.GetReflection() |
| 1230 | ->GetMessage(message0a, fd_map_int32_foreign_message_value)); |
| 1231 | int32 int32_value0a = sub_message0a.c(); |
| 1232 | const Message& message9a = |
| 1233 | mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); |
| 1234 | const ForeignMessage& sub_message9a = down_cast<const ForeignMessage&>( |
| 1235 | message9a.GetReflection() |
| 1236 | ->GetMessage(message9a, fd_map_int32_foreign_message_value)); |
| 1237 | int32 int32_value9a = sub_message9a.c(); |
| 1238 | |
| 1239 | mmf_int32_foreign_message.SwapElements(0, 9); |
| 1240 | |
| 1241 | const Message& message0b = |
| 1242 | mmf_int32_foreign_message.Get(0, entry_int32_foreign_message.get()); |
| 1243 | const ForeignMessage& sub_message0b = down_cast<const ForeignMessage&>( |
| 1244 | message0b.GetReflection() |
| 1245 | ->GetMessage(message0b, fd_map_int32_foreign_message_value)); |
| 1246 | int32 int32_value0b = sub_message0b.c(); |
| 1247 | const Message& message9b = |
| 1248 | mmf_int32_foreign_message.Get(9, entry_int32_foreign_message.get()); |
| 1249 | const ForeignMessage& sub_message9b = down_cast<const ForeignMessage&>( |
| 1250 | message9b.GetReflection() |
| 1251 | ->GetMessage(message9b, fd_map_int32_foreign_message_value)); |
| 1252 | int32 int32_value9b = sub_message9b.c(); |
| 1253 | |
| 1254 | EXPECT_EQ(int32_value9a, int32_value0b); |
| 1255 | EXPECT_EQ(int32_value0a, int32_value9b); |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | TEST_F(MapFieldReflectionTest, RepeatedFieldRefMergeFromAndSwap) { |
| 1260 | // Set-up message content. |
| 1261 | TestMap m0, m1, m2; |
| 1262 | for (int i = 0; i < 10; ++i) { |
| 1263 | (*m0.mutable_map_int32_int32())[i] = Func(i, 1); |
| 1264 | (*m0.mutable_map_int32_double())[i] = Func(i, 2); |
| 1265 | (*m0.mutable_map_string_string())[StrFunc(i, 1)] = StrFunc(i, 5); |
| 1266 | (*m0.mutable_map_int32_foreign_message())[i].set_c(Func(i, 6)); |
| 1267 | (*m1.mutable_map_int32_int32())[i + 10] = Func(i, 11); |
| 1268 | (*m1.mutable_map_int32_double())[i + 10] = Func(i, 12); |
| 1269 | (*m1.mutable_map_string_string())[StrFunc(i + 10, 1)] = StrFunc(i, 15); |
| 1270 | (*m1.mutable_map_int32_foreign_message())[i + 10].set_c(Func(i, 16)); |
| 1271 | (*m2.mutable_map_int32_int32())[i + 20] = Func(i, 21); |
| 1272 | (*m2.mutable_map_int32_double())[i + 20] = Func(i, 22); |
| 1273 | (*m2.mutable_map_string_string())[StrFunc(i + 20, 1)] = StrFunc(i, 25); |
| 1274 | (*m2.mutable_map_int32_foreign_message())[i + 20].set_c(Func(i, 26)); |
| 1275 | } |
| 1276 | |
| 1277 | const Reflection* refl = m0.GetReflection(); |
| 1278 | const Descriptor* desc = m0.GetDescriptor(); |
| 1279 | |
| 1280 | // Get FieldDescriptors for all the fields of interest. |
| 1281 | const FieldDescriptor* fd_map_int32_int32 = |
| 1282 | desc->FindFieldByName("map_int32_int32"); |
| 1283 | const FieldDescriptor* fd_map_int32_double = |
| 1284 | desc->FindFieldByName("map_int32_double"); |
| 1285 | const FieldDescriptor* fd_map_string_string = |
| 1286 | desc->FindFieldByName("map_string_string"); |
| 1287 | const FieldDescriptor* fd_map_int32_foreign_message = |
| 1288 | desc->FindFieldByName("map_int32_foreign_message"); |
| 1289 | |
| 1290 | // Get MutableRepeatedFieldRef objects for all fields of interest. |
| 1291 | const MutableRepeatedFieldRef<Message> mmf_int32_int32 = |
| 1292 | refl->GetMutableRepeatedFieldRef<Message>( |
| 1293 | &m0, fd_map_int32_int32); |
| 1294 | const MutableRepeatedFieldRef<Message> mmf_int32_double = |
| 1295 | refl->GetMutableRepeatedFieldRef<Message>( |
| 1296 | &m0, fd_map_int32_double); |
| 1297 | const MutableRepeatedFieldRef<Message> mmf_string_string = |
| 1298 | refl->GetMutableRepeatedFieldRef<Message>( |
| 1299 | &m0, fd_map_string_string); |
| 1300 | const MutableRepeatedFieldRef<Message> |
| 1301 | mmf_int32_foreign_message = |
| 1302 | refl->GetMutableRepeatedFieldRef<Message>( |
| 1303 | &m0, fd_map_int32_foreign_message); |
| 1304 | |
| 1305 | // Test MutableRepeatedRef::CopyFrom |
| 1306 | mmf_int32_int32.CopyFrom( |
| 1307 | refl->GetRepeatedFieldRef<Message>( |
| 1308 | m1, fd_map_int32_int32)); |
| 1309 | mmf_int32_double.CopyFrom( |
| 1310 | refl->GetRepeatedFieldRef<Message>( |
| 1311 | m1, fd_map_int32_double)); |
| 1312 | mmf_string_string.CopyFrom( |
| 1313 | refl->GetRepeatedFieldRef<Message>( |
| 1314 | m1, fd_map_string_string)); |
| 1315 | mmf_int32_foreign_message.CopyFrom( |
| 1316 | refl->GetRepeatedFieldRef<Message>( |
| 1317 | m1, fd_map_int32_foreign_message)); |
| 1318 | |
| 1319 | for (int i = 0; i < 10; ++i) { |
| 1320 | EXPECT_EQ(Func(i, 11), m0.map_int32_int32().at(i + 10)); |
| 1321 | EXPECT_EQ(Func(i, 12), m0.map_int32_double().at(i + 10)); |
| 1322 | EXPECT_EQ(StrFunc(i, 15), m0.map_string_string().at(StrFunc(i + 10, 1))); |
| 1323 | EXPECT_EQ(Func(i, 16), m0.map_int32_foreign_message().at(i + 10).c()); |
| 1324 | } |
| 1325 | |
| 1326 | // Test MutableRepeatedRef::MergeFrom |
| 1327 | mmf_int32_int32.MergeFrom( |
| 1328 | refl->GetRepeatedFieldRef<Message>( |
| 1329 | m2, fd_map_int32_int32)); |
| 1330 | mmf_int32_double.MergeFrom( |
| 1331 | refl->GetRepeatedFieldRef<Message>( |
| 1332 | m2, fd_map_int32_double)); |
| 1333 | mmf_string_string.MergeFrom( |
| 1334 | refl->GetRepeatedFieldRef<Message>( |
| 1335 | m2, fd_map_string_string)); |
| 1336 | mmf_int32_foreign_message.MergeFrom( |
| 1337 | refl->GetRepeatedFieldRef<Message>( |
| 1338 | m2, fd_map_int32_foreign_message)); |
| 1339 | for (int i = 0; i < 10; ++i) { |
| 1340 | EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20)); |
| 1341 | EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20)); |
| 1342 | EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1))); |
| 1343 | EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c()); |
| 1344 | } |
| 1345 | |
| 1346 | // Test MutableRepeatedRef::Swap |
| 1347 | // Swap between m0 and m2. |
| 1348 | mmf_int32_int32.Swap( |
| 1349 | refl->GetMutableRepeatedFieldRef<Message>( |
| 1350 | &m2, fd_map_int32_int32)); |
| 1351 | mmf_int32_double.Swap( |
| 1352 | refl->GetMutableRepeatedFieldRef<Message>( |
| 1353 | &m2, fd_map_int32_double)); |
| 1354 | mmf_string_string.Swap( |
| 1355 | refl->GetMutableRepeatedFieldRef<Message>( |
| 1356 | &m2, fd_map_string_string)); |
| 1357 | mmf_int32_foreign_message.Swap( |
| 1358 | refl->GetMutableRepeatedFieldRef<Message>( |
| 1359 | &m2, fd_map_int32_foreign_message)); |
| 1360 | for (int i = 0; i < 10; ++i) { |
| 1361 | // Check the content of m0. |
| 1362 | EXPECT_EQ(Func(i, 21), m0.map_int32_int32().at(i + 20)); |
| 1363 | EXPECT_EQ(Func(i, 22), m0.map_int32_double().at(i + 20)); |
| 1364 | EXPECT_EQ(StrFunc(i, 25), m0.map_string_string().at(StrFunc(i + 20, 1))); |
| 1365 | EXPECT_EQ(Func(i, 26), m0.map_int32_foreign_message().at(i + 20).c()); |
| 1366 | |
| 1367 | // Check the content of m2. |
| 1368 | EXPECT_EQ(Func(i, 11), m2.map_int32_int32().at(i + 10)); |
| 1369 | EXPECT_EQ(Func(i, 12), m2.map_int32_double().at(i + 10)); |
| 1370 | EXPECT_EQ(StrFunc(i, 15), m2.map_string_string().at(StrFunc(i + 10, 1))); |
| 1371 | EXPECT_EQ(Func(i, 16), m2.map_int32_foreign_message().at(i + 10).c()); |
| 1372 | EXPECT_EQ(Func(i, 21), m2.map_int32_int32().at(i + 20)); |
| 1373 | EXPECT_EQ(Func(i, 22), m2.map_int32_double().at(i + 20)); |
| 1374 | EXPECT_EQ(StrFunc(i, 25), m2.map_string_string().at(StrFunc(i + 20, 1))); |
| 1375 | EXPECT_EQ(Func(i, 26), m2.map_int32_foreign_message().at(i + 20).c()); |
| 1376 | } |
| 1377 | |
| 1378 | // TODO(teboring): add test for duplicated key |
| 1379 | } |
| 1380 | |
| 1381 | // Generated Message Test =========================================== |
| 1382 | |
| 1383 | TEST(GeneratedMapFieldTest, Accessors) { |
| 1384 | unittest::TestMap message; |
| 1385 | |
| 1386 | MapTestUtil::SetMapFields(&message); |
| 1387 | MapTestUtil::ExpectMapFieldsSet(message); |
| 1388 | |
| 1389 | MapTestUtil::ModifyMapFields(&message); |
| 1390 | MapTestUtil::ExpectMapFieldsModified(message); |
| 1391 | } |
| 1392 | |
| 1393 | TEST(GeneratedMapFieldTest, SetMapFieldsInitialized) { |
| 1394 | unittest::TestMap message; |
| 1395 | |
| 1396 | MapTestUtil::SetMapFieldsInitialized(&message); |
| 1397 | MapTestUtil::ExpectMapFieldsSetInitialized(message); |
| 1398 | } |
| 1399 | |
| 1400 | TEST(GeneratedMapFieldTest, Proto2SetMapFieldsInitialized) { |
| 1401 | unittest::TestEnumStartWithNonZeroMap message; |
| 1402 | EXPECT_EQ(unittest::PROTO2_NON_ZERO_MAP_ENUM_FOO, |
| 1403 | (*message.mutable_map_field())[0]); |
| 1404 | } |
| 1405 | |
| 1406 | TEST(GeneratedMapFieldTest, Clear) { |
| 1407 | unittest::TestMap message; |
| 1408 | |
| 1409 | MapTestUtil::SetMapFields(&message); |
| 1410 | message.Clear(); |
| 1411 | MapTestUtil::ExpectClear(message); |
| 1412 | } |
| 1413 | |
| 1414 | TEST(GeneratedMapFieldTest, ClearMessageMap) { |
| 1415 | unittest::TestMessageMap message; |
| 1416 | |
| 1417 | // Creates a TestAllTypes with default value |
| 1418 | TestUtil::ExpectClear((*message.mutable_map_int32_message())[0]); |
| 1419 | } |
| 1420 | |
| 1421 | TEST(GeneratedMapFieldTest, CopyFrom) { |
| 1422 | unittest::TestMap message1, message2; |
| 1423 | |
| 1424 | MapTestUtil::SetMapFields(&message1); |
| 1425 | message2.CopyFrom(message1); |
| 1426 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1427 | |
| 1428 | // Copying from self should be a no-op. |
| 1429 | message2.CopyFrom(message2); |
| 1430 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1431 | } |
| 1432 | |
| 1433 | TEST(GeneratedMapFieldTest, CopyFromMessageMap) { |
| 1434 | unittest::TestMessageMap message1, message2; |
| 1435 | |
| 1436 | (*message1.mutable_map_int32_message())[0].add_repeated_int32(100); |
| 1437 | (*message2.mutable_map_int32_message())[0].add_repeated_int32(101); |
| 1438 | |
| 1439 | message1.CopyFrom(message2); |
| 1440 | |
| 1441 | // Checks repeated field is overwritten. |
| 1442 | EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size()); |
| 1443 | EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0)); |
| 1444 | } |
| 1445 | |
| 1446 | TEST(GeneratedMapFieldTest, SwapWithEmpty) { |
| 1447 | unittest::TestMap message1, message2; |
| 1448 | |
| 1449 | MapTestUtil::SetMapFields(&message1); |
| 1450 | MapTestUtil::ExpectMapFieldsSet(message1); |
| 1451 | MapTestUtil::ExpectClear(message2); |
| 1452 | |
| 1453 | message1.Swap(&message2); |
| 1454 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1455 | MapTestUtil::ExpectClear(message1); |
| 1456 | } |
| 1457 | |
| 1458 | TEST(GeneratedMapFieldTest, SwapWithSelf) { |
| 1459 | unittest::TestMap message; |
| 1460 | |
| 1461 | MapTestUtil::SetMapFields(&message); |
| 1462 | MapTestUtil::ExpectMapFieldsSet(message); |
| 1463 | |
| 1464 | message.Swap(&message); |
| 1465 | MapTestUtil::ExpectMapFieldsSet(message); |
| 1466 | } |
| 1467 | |
| 1468 | TEST(GeneratedMapFieldTest, SwapWithOther) { |
| 1469 | unittest::TestMap message1, message2; |
| 1470 | |
| 1471 | MapTestUtil::SetMapFields(&message1); |
| 1472 | MapTestUtil::SetMapFields(&message2); |
| 1473 | MapTestUtil::ModifyMapFields(&message2); |
| 1474 | |
| 1475 | message1.Swap(&message2); |
| 1476 | MapTestUtil::ExpectMapFieldsModified(message1); |
| 1477 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1478 | } |
| 1479 | |
| 1480 | TEST(GeneratedMapFieldTest, CopyConstructor) { |
| 1481 | unittest::TestMap message1; |
| 1482 | MapTestUtil::SetMapFields(&message1); |
| 1483 | |
| 1484 | unittest::TestMap message2(message1); |
| 1485 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1486 | } |
| 1487 | |
| 1488 | TEST(GeneratedMapFieldTest, CopyAssignmentOperator) { |
| 1489 | unittest::TestMap message1; |
| 1490 | MapTestUtil::SetMapFields(&message1); |
| 1491 | |
| 1492 | unittest::TestMap message2; |
| 1493 | message2 = message1; |
| 1494 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1495 | |
| 1496 | // Make sure that self-assignment does something sane. |
| 1497 | message2.operator=(message2); |
| 1498 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1499 | } |
| 1500 | |
| 1501 | #if !defined(PROTOBUF_TEST_NO_DESCRIPTORS) || \ |
| 1502 | !defined(GOOGLE_PROTOBUF_NO_RTTI) |
| 1503 | TEST(GeneratedMapFieldTest, UpcastCopyFrom) { |
| 1504 | // Test the CopyFrom method that takes in the generic const Message& |
| 1505 | // parameter. |
| 1506 | unittest::TestMap message1, message2; |
| 1507 | |
| 1508 | MapTestUtil::SetMapFields(&message1); |
| 1509 | |
| 1510 | const Message* source = implicit_cast<const Message*>(&message1); |
| 1511 | message2.CopyFrom(*source); |
| 1512 | |
| 1513 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1514 | } |
| 1515 | #endif |
| 1516 | |
| 1517 | #ifndef PROTOBUF_TEST_NO_DESCRIPTORS |
| 1518 | |
| 1519 | TEST(GeneratedMapFieldTest, CopyFromDynamicMessage) { |
| 1520 | // Test copying from a DynamicMessage, which must fall back to using |
| 1521 | // reflection. |
| 1522 | unittest::TestMap message2; |
| 1523 | |
| 1524 | // Construct a new version of the dynamic message via the factory. |
| 1525 | DynamicMessageFactory factory; |
| 1526 | google::protobuf::scoped_ptr<Message> message1; |
| 1527 | message1.reset( |
| 1528 | factory.GetPrototype(unittest::TestMap::descriptor())->New()); |
| 1529 | |
| 1530 | MapTestUtil::MapReflectionTester reflection_tester( |
| 1531 | unittest::TestMap::descriptor()); |
| 1532 | reflection_tester.SetMapFieldsViaReflection(message1.get()); |
| 1533 | reflection_tester.ExpectMapFieldsSetViaReflection(*message1); |
| 1534 | |
| 1535 | message2.CopyFrom(*message1); |
| 1536 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1537 | } |
| 1538 | |
| 1539 | TEST(GeneratedMapFieldTest, DynamicMessageCopyFrom) { |
| 1540 | // Test copying to a DynamicMessage, which must fall back to using reflection. |
| 1541 | unittest::TestMap message2; |
| 1542 | MapTestUtil::SetMapFields(&message2); |
| 1543 | |
| 1544 | // Construct a new version of the dynamic message via the factory. |
| 1545 | DynamicMessageFactory factory; |
| 1546 | google::protobuf::scoped_ptr<Message> message1; |
| 1547 | message1.reset( |
| 1548 | factory.GetPrototype(unittest::TestMap::descriptor())->New()); |
| 1549 | |
| 1550 | MapTestUtil::MapReflectionTester reflection_tester( |
| 1551 | unittest::TestMap::descriptor()); |
| 1552 | message1->MergeFrom(message2); |
| 1553 | reflection_tester.ExpectMapFieldsSetViaReflection(*message1); |
| 1554 | } |
| 1555 | |
| 1556 | #endif // !PROTOBUF_TEST_NO_DESCRIPTORS |
| 1557 | |
| 1558 | TEST(GeneratedMapFieldTest, NonEmptyMergeFrom) { |
| 1559 | unittest::TestMap message1, message2; |
| 1560 | |
| 1561 | MapTestUtil::SetMapFields(&message1); |
| 1562 | |
| 1563 | // This field will test merging into an empty spot. |
| 1564 | (*message2.mutable_map_int32_int32())[1] = 1; |
| 1565 | message1.mutable_map_int32_int32()->erase(1); |
| 1566 | |
| 1567 | // This tests overwriting. |
| 1568 | (*message2.mutable_map_int32_double())[1] = 1; |
| 1569 | (*message1.mutable_map_int32_double())[1] = 2; |
| 1570 | |
| 1571 | message1.MergeFrom(message2); |
| 1572 | MapTestUtil::ExpectMapFieldsSet(message1); |
| 1573 | } |
| 1574 | |
| 1575 | TEST(GeneratedMapFieldTest, MergeFromMessageMap) { |
| 1576 | unittest::TestMessageMap message1, message2; |
| 1577 | |
| 1578 | (*message1.mutable_map_int32_message())[0].add_repeated_int32(100); |
| 1579 | (*message2.mutable_map_int32_message())[0].add_repeated_int32(101); |
| 1580 | |
| 1581 | message1.MergeFrom(message2); |
| 1582 | |
| 1583 | // Checks repeated field is overwritten. |
| 1584 | EXPECT_EQ(1, message1.map_int32_message().at(0).repeated_int32_size()); |
| 1585 | EXPECT_EQ(101, message1.map_int32_message().at(0).repeated_int32(0)); |
| 1586 | } |
| 1587 | |
| 1588 | // Test the generated SerializeWithCachedSizesToArray() |
| 1589 | TEST(GeneratedMapFieldTest, SerializationToArray) { |
| 1590 | unittest::TestMap message1, message2; |
| 1591 | string data; |
| 1592 | MapTestUtil::SetMapFields(&message1); |
| 1593 | int size = message1.ByteSize(); |
| 1594 | data.resize(size); |
| 1595 | uint8* start = reinterpret_cast<uint8*>(string_as_array(&data)); |
| 1596 | uint8* end = message1.SerializeWithCachedSizesToArray(start); |
| 1597 | EXPECT_EQ(size, end - start); |
| 1598 | EXPECT_TRUE(message2.ParseFromString(data)); |
| 1599 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1600 | } |
| 1601 | |
| 1602 | // Test the generated SerializeWithCachedSizes() |
| 1603 | TEST(GeneratedMapFieldTest, SerializationToStream) { |
| 1604 | unittest::TestMap message1, message2; |
| 1605 | MapTestUtil::SetMapFields(&message1); |
| 1606 | int size = message1.ByteSize(); |
| 1607 | string data; |
| 1608 | data.resize(size); |
| 1609 | { |
| 1610 | // Allow the output stream to buffer only one byte at a time. |
| 1611 | io::ArrayOutputStream array_stream(string_as_array(&data), size, 1); |
| 1612 | io::CodedOutputStream output_stream(&array_stream); |
| 1613 | message1.SerializeWithCachedSizes(&output_stream); |
| 1614 | EXPECT_FALSE(output_stream.HadError()); |
| 1615 | EXPECT_EQ(size, output_stream.ByteCount()); |
| 1616 | } |
| 1617 | EXPECT_TRUE(message2.ParseFromString(data)); |
| 1618 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1619 | } |
| 1620 | |
| 1621 | |
| 1622 | TEST(GeneratedMapFieldTest, SameTypeMaps) { |
| 1623 | const Descriptor* map1 = unittest::TestSameTypeMap::descriptor() |
| 1624 | ->FindFieldByName("map1") |
| 1625 | ->message_type(); |
| 1626 | const Descriptor* map2 = unittest::TestSameTypeMap::descriptor() |
| 1627 | ->FindFieldByName("map2") |
| 1628 | ->message_type(); |
| 1629 | |
| 1630 | const Message* map1_entry = |
| 1631 | MessageFactory::generated_factory()->GetPrototype(map1); |
| 1632 | const Message* map2_entry = |
| 1633 | MessageFactory::generated_factory()->GetPrototype(map2); |
| 1634 | |
| 1635 | EXPECT_EQ(map1, map1_entry->GetDescriptor()); |
| 1636 | EXPECT_EQ(map2, map2_entry->GetDescriptor()); |
| 1637 | } |
| 1638 | |
| 1639 | TEST(GeneratedMapFieldTest, Proto2UnknownEnum) { |
| 1640 | unittest::TestEnumMapPlusExtra from; |
| 1641 | (*from.mutable_known_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_FOO; |
| 1642 | (*from.mutable_unknown_map_field())[0] = unittest::E_PROTO2_MAP_ENUM_EXTRA; |
| 1643 | string data; |
| 1644 | from.SerializeToString(&data); |
| 1645 | |
| 1646 | unittest::TestEnumMap to; |
| 1647 | EXPECT_TRUE(to.ParseFromString(data)); |
| 1648 | EXPECT_EQ(0, to.unknown_map_field().size()); |
| 1649 | const UnknownFieldSet& unknown_field_set = |
| 1650 | to.GetReflection()->GetUnknownFields(to); |
| 1651 | EXPECT_EQ(1, unknown_field_set.field_count()); |
| 1652 | EXPECT_EQ(1, to.known_map_field().size()); |
| 1653 | EXPECT_EQ(unittest::PROTO2_MAP_ENUM_FOO, to.known_map_field().at(0)); |
| 1654 | |
| 1655 | data.clear(); |
| 1656 | from.Clear(); |
| 1657 | to.SerializeToString(&data); |
| 1658 | EXPECT_TRUE(from.ParseFromString(data)); |
| 1659 | EXPECT_EQ(0, from.GetReflection()->GetUnknownFields(from).field_count()); |
| 1660 | EXPECT_EQ(1, from.known_map_field().size()); |
| 1661 | EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_FOO, from.known_map_field().at(0)); |
| 1662 | EXPECT_EQ(1, from.unknown_map_field().size()); |
| 1663 | EXPECT_EQ(unittest::E_PROTO2_MAP_ENUM_EXTRA, from.unknown_map_field().at(0)); |
| 1664 | } |
| 1665 | |
| 1666 | TEST(GeneratedMapFieldTest, StandardWireFormat) { |
| 1667 | unittest::TestMap message; |
| 1668 | string data = "\x0A\x04\x08\x01\x10\x01"; |
| 1669 | |
| 1670 | EXPECT_TRUE(message.ParseFromString(data)); |
| 1671 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 1672 | EXPECT_EQ(1, message.map_int32_int32().at(1)); |
| 1673 | } |
| 1674 | |
| 1675 | TEST(GeneratedMapFieldTest, UnorderedWireFormat) { |
| 1676 | unittest::TestMap message; |
| 1677 | |
| 1678 | // put value before key in wire format |
| 1679 | string data = "\x0A\x04\x10\x01\x08\x02"; |
| 1680 | |
| 1681 | EXPECT_TRUE(message.ParseFromString(data)); |
| 1682 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 1683 | EXPECT_EQ(1, message.map_int32_int32().at(2)); |
| 1684 | } |
| 1685 | |
| 1686 | TEST(GeneratedMapFieldTest, DuplicatedKeyWireFormat) { |
| 1687 | unittest::TestMap message; |
| 1688 | |
| 1689 | // Two key fields in wire format |
| 1690 | string data = "\x0A\x06\x08\x01\x08\x02\x10\x01"; |
| 1691 | |
| 1692 | EXPECT_TRUE(message.ParseFromString(data)); |
| 1693 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 1694 | EXPECT_EQ(1, message.map_int32_int32().at(2)); |
| 1695 | } |
| 1696 | |
| 1697 | TEST(GeneratedMapFieldTest, DuplicatedValueWireFormat) { |
| 1698 | unittest::TestMap message; |
| 1699 | |
| 1700 | // Two value fields in wire format |
| 1701 | string data = "\x0A\x06\x08\x01\x10\x01\x10\x02"; |
| 1702 | |
| 1703 | EXPECT_TRUE(message.ParseFromString(data)); |
| 1704 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 1705 | EXPECT_EQ(2, message.map_int32_int32().at(1)); |
| 1706 | } |
| 1707 | |
| 1708 | TEST(GeneratedMapFieldTest, MissedKeyWireFormat) { |
| 1709 | unittest::TestMap message; |
| 1710 | |
| 1711 | // No key field in wire format |
| 1712 | string data = "\x0A\x02\x10\x01"; |
| 1713 | |
| 1714 | EXPECT_TRUE(message.ParseFromString(data)); |
| 1715 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 1716 | EXPECT_EQ(1, message.map_int32_int32().at(0)); |
| 1717 | } |
| 1718 | |
| 1719 | TEST(GeneratedMapFieldTest, MissedValueWireFormat) { |
| 1720 | unittest::TestMap message; |
| 1721 | |
| 1722 | // No value field in wire format |
| 1723 | string data = "\x0A\x02\x08\x01"; |
| 1724 | |
| 1725 | EXPECT_TRUE(message.ParseFromString(data)); |
| 1726 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 1727 | EXPECT_EQ(0, message.map_int32_int32().at(1)); |
| 1728 | } |
| 1729 | |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 1730 | TEST(GeneratedMapFieldTest, MissedValueTextFormat) { |
| 1731 | unittest::TestMap message; |
| 1732 | |
| 1733 | // No value field in text format |
| 1734 | string text = |
| 1735 | "map_int32_foreign_message {\n" |
| 1736 | " key: 1234567890\n" |
| 1737 | "}"; |
| 1738 | |
| 1739 | EXPECT_TRUE(google::protobuf::TextFormat::ParseFromString(text, &message)); |
| 1740 | EXPECT_EQ(1, message.map_int32_foreign_message().size()); |
| 1741 | EXPECT_EQ(11, message.ByteSize()); |
| 1742 | } |
| 1743 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 1744 | TEST(GeneratedMapFieldTest, UnknownFieldWireFormat) { |
| 1745 | unittest::TestMap message; |
| 1746 | |
| 1747 | // Unknown field in wire format |
| 1748 | string data = "\x0A\x06\x08\x02\x10\x03\x18\x01"; |
| 1749 | |
| 1750 | EXPECT_TRUE(message.ParseFromString(data)); |
| 1751 | EXPECT_EQ(1, message.map_int32_int32().size()); |
| 1752 | EXPECT_EQ(3, message.map_int32_int32().at(2)); |
| 1753 | } |
| 1754 | |
| 1755 | TEST(GeneratedMapFieldTest, CorruptedWireFormat) { |
| 1756 | unittest::TestMap message; |
| 1757 | |
| 1758 | // corrupted data in wire format |
| 1759 | string data = "\x0A\x06\x08\x02\x11\x03"; |
| 1760 | |
| 1761 | EXPECT_FALSE(message.ParseFromString(data)); |
| 1762 | } |
| 1763 | |
Feng Xiao | 6ae3bde | 2014-11-25 14:01:44 -0800 | [diff] [blame] | 1764 | TEST(GeneratedMapFieldTest, IsInitialized) { |
| 1765 | unittest::TestRequiredMessageMap map_message; |
| 1766 | |
| 1767 | // Add an uninitialized message. |
| 1768 | (*map_message.mutable_map_field())[0]; |
| 1769 | EXPECT_FALSE(map_message.IsInitialized()); |
| 1770 | |
| 1771 | // Initialize uninitialized message |
| 1772 | (*map_message.mutable_map_field())[0].set_a(0); |
| 1773 | (*map_message.mutable_map_field())[0].set_b(0); |
| 1774 | (*map_message.mutable_map_field())[0].set_c(0); |
| 1775 | EXPECT_TRUE(map_message.IsInitialized()); |
| 1776 | } |
| 1777 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 1778 | // Generated Message Reflection Test ================================ |
| 1779 | |
| 1780 | TEST(GeneratedMapFieldReflectionTest, SpaceUsed) { |
| 1781 | unittest::TestMap message; |
| 1782 | MapTestUtil::MapReflectionTester reflection_tester( |
| 1783 | unittest::TestMap::descriptor()); |
| 1784 | reflection_tester.SetMapFieldsViaReflection(&message); |
| 1785 | |
| 1786 | EXPECT_LT(0, message.GetReflection()->SpaceUsed(message)); |
| 1787 | } |
| 1788 | |
| 1789 | TEST(GeneratedMapFieldReflectionTest, Accessors) { |
| 1790 | // Set every field to a unique value then go back and check all those |
| 1791 | // values. |
| 1792 | unittest::TestMap message; |
| 1793 | MapTestUtil::MapReflectionTester reflection_tester( |
| 1794 | unittest::TestMap::descriptor()); |
| 1795 | reflection_tester.SetMapFieldsViaReflection(&message); |
| 1796 | MapTestUtil::ExpectMapFieldsSet(message); |
| 1797 | reflection_tester.ExpectMapFieldsSetViaReflection(message); |
| 1798 | |
| 1799 | reflection_tester.ModifyMapFieldsViaReflection(&message); |
| 1800 | MapTestUtil::ExpectMapFieldsModified(message); |
| 1801 | } |
| 1802 | |
| 1803 | TEST(GeneratedMapFieldReflectionTest, Swap) { |
| 1804 | unittest::TestMap message1; |
| 1805 | unittest::TestMap message2; |
| 1806 | |
| 1807 | MapTestUtil::SetMapFields(&message1); |
| 1808 | |
| 1809 | const Reflection* reflection = message1.GetReflection(); |
| 1810 | reflection->Swap(&message1, &message2); |
| 1811 | |
| 1812 | MapTestUtil::ExpectClear(message1); |
| 1813 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1814 | } |
| 1815 | |
| 1816 | TEST(GeneratedMapFieldReflectionTest, SwapWithBothSet) { |
| 1817 | unittest::TestMap message1; |
| 1818 | unittest::TestMap message2; |
| 1819 | |
| 1820 | MapTestUtil::SetMapFields(&message1); |
| 1821 | MapTestUtil::SetMapFields(&message2); |
| 1822 | MapTestUtil::ModifyMapFields(&message2); |
| 1823 | |
| 1824 | const Reflection* reflection = message1.GetReflection(); |
| 1825 | reflection->Swap(&message1, &message2); |
| 1826 | |
| 1827 | MapTestUtil::ExpectMapFieldsModified(message1); |
| 1828 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 1829 | } |
| 1830 | |
| 1831 | TEST(GeneratedMapFieldReflectionTest, SwapFields) { |
| 1832 | unittest::TestMap message1; |
| 1833 | unittest::TestMap message2; |
| 1834 | |
| 1835 | MapTestUtil::SetMapFields(&message2); |
| 1836 | |
| 1837 | vector<const FieldDescriptor*> fields; |
| 1838 | const Reflection* reflection = message1.GetReflection(); |
| 1839 | reflection->ListFields(message2, &fields); |
| 1840 | reflection->SwapFields(&message1, &message2, fields); |
| 1841 | |
| 1842 | MapTestUtil::ExpectMapFieldsSet(message1); |
| 1843 | MapTestUtil::ExpectClear(message2); |
| 1844 | } |
| 1845 | |
| 1846 | TEST(GeneratedMapFieldReflectionTest, ClearField) { |
| 1847 | unittest::TestMap message; |
| 1848 | MapTestUtil::SetMapFields(&message); |
| 1849 | MapTestUtil::ExpectMapFieldsSet(message); |
| 1850 | |
| 1851 | MapTestUtil::MapReflectionTester reflection_tester( |
| 1852 | unittest::TestMap::descriptor()); |
| 1853 | reflection_tester.ClearMapFieldsViaReflection(&message); |
| 1854 | MapTestUtil::ExpectClear(message); |
| 1855 | } |
| 1856 | |
| 1857 | TEST(GeneratedMapFieldReflectionTest, RemoveLast) { |
| 1858 | unittest::TestMap message; |
| 1859 | MapTestUtil::MapReflectionTester reflection_tester( |
| 1860 | unittest::TestMap::descriptor()); |
| 1861 | |
| 1862 | MapTestUtil::SetMapFields(&message); |
| 1863 | MapTestUtil::ExpectMapsSize(message, 2); |
| 1864 | std::vector<const Message*> expected_entries = |
| 1865 | MapTestUtil::GetMapEntries(message, 0); |
| 1866 | |
| 1867 | reflection_tester.RemoveLastMapsViaReflection(&message); |
| 1868 | |
| 1869 | MapTestUtil::ExpectMapsSize(message, 1); |
| 1870 | std::vector<const Message*> remained_entries = |
| 1871 | MapTestUtil::GetMapEntries(message, 0); |
| 1872 | EXPECT_TRUE(expected_entries == remained_entries); |
| 1873 | } |
| 1874 | |
| 1875 | TEST(GeneratedMapFieldReflectionTest, ReleaseLast) { |
| 1876 | unittest::TestMap message; |
| 1877 | const Descriptor* descriptor = message.GetDescriptor(); |
| 1878 | MapTestUtil::MapReflectionTester reflection_tester(descriptor); |
| 1879 | |
| 1880 | MapTestUtil::SetMapFields(&message); |
| 1881 | |
| 1882 | MapTestUtil::ExpectMapsSize(message, 2); |
| 1883 | |
| 1884 | reflection_tester.ReleaseLastMapsViaReflection(&message); |
| 1885 | |
| 1886 | MapTestUtil::ExpectMapsSize(message, 1); |
| 1887 | |
| 1888 | // Now test that we actually release the right message. |
| 1889 | message.Clear(); |
| 1890 | MapTestUtil::SetMapFields(&message); |
| 1891 | |
| 1892 | MapTestUtil::ExpectMapsSize(message, 2); |
| 1893 | std::vector<const Message*> expect_last = |
| 1894 | MapTestUtil::GetMapEntries(message, 1); |
| 1895 | std::vector<const Message*> release_last = |
| 1896 | MapTestUtil::GetMapEntriesFromRelease(&message); |
| 1897 | MapTestUtil::ExpectMapsSize(message, 1); |
| 1898 | EXPECT_TRUE(expect_last == release_last); |
| 1899 | for (std::vector<const Message*>::iterator it = release_last.begin(); |
| 1900 | it != release_last.end(); ++it) { |
| 1901 | delete *it; |
| 1902 | } |
| 1903 | } |
| 1904 | |
| 1905 | TEST(GeneratedMapFieldReflectionTest, SwapElements) { |
| 1906 | unittest::TestMap message; |
| 1907 | MapTestUtil::MapReflectionTester reflection_tester( |
| 1908 | unittest::TestMap::descriptor()); |
| 1909 | |
| 1910 | MapTestUtil::SetMapFields(&message); |
| 1911 | |
| 1912 | // Get pointers of map entries at their original position |
| 1913 | std::vector<const Message*> entries0 = MapTestUtil::GetMapEntries(message, 0); |
| 1914 | std::vector<const Message*> entries1 = MapTestUtil::GetMapEntries(message, 1); |
| 1915 | |
| 1916 | // Swap the first time. |
| 1917 | reflection_tester.SwapMapsViaReflection(&message); |
| 1918 | |
| 1919 | // Get pointer of map entry after swap once. |
| 1920 | std::vector<const Message*> entries0_once = |
| 1921 | MapTestUtil::GetMapEntries(message, 0); |
| 1922 | std::vector<const Message*> entries1_once = |
| 1923 | MapTestUtil::GetMapEntries(message, 1); |
| 1924 | |
| 1925 | // Test map entries are swapped. |
| 1926 | MapTestUtil::ExpectMapsSize(message, 2); |
| 1927 | EXPECT_TRUE(entries0 == entries1_once); |
| 1928 | EXPECT_TRUE(entries1 == entries0_once); |
| 1929 | |
| 1930 | // Swap the second time. |
| 1931 | reflection_tester.SwapMapsViaReflection(&message); |
| 1932 | |
| 1933 | // Get pointer of map entry after swap once. |
| 1934 | std::vector<const Message*> entries0_twice = |
| 1935 | MapTestUtil::GetMapEntries(message, 0); |
| 1936 | std::vector<const Message*> entries1_twice = |
| 1937 | MapTestUtil::GetMapEntries(message, 1); |
| 1938 | |
| 1939 | // Test map entries are swapped back. |
| 1940 | MapTestUtil::ExpectMapsSize(message, 2); |
| 1941 | EXPECT_TRUE(entries0 == entries0_twice); |
| 1942 | EXPECT_TRUE(entries1 == entries1_twice); |
| 1943 | } |
| 1944 | |
| 1945 | TEST(GeneratedMapFieldReflectionTest, MutableUnknownFields) { |
| 1946 | unittest::TestMap message; |
| 1947 | MapTestUtil::MapReflectionTester reflection_tester( |
| 1948 | unittest::TestMap::descriptor()); |
| 1949 | reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message); |
| 1950 | } |
| 1951 | |
| 1952 | TEST(GeneratedMapFieldReflectionTest, EmbedProto2Message) { |
| 1953 | unittest::TestMessageMap message; |
| 1954 | |
| 1955 | const FieldDescriptor* map_field = |
| 1956 | unittest::TestMessageMap::descriptor()->FindFieldByName( |
| 1957 | "map_int32_message"); |
| 1958 | const FieldDescriptor* value = |
| 1959 | map_field->message_type()->FindFieldByName("value"); |
| 1960 | |
| 1961 | Message* entry_message = |
| 1962 | message.GetReflection()->AddMessage(&message, map_field); |
| 1963 | EXPECT_EQ( |
| 1964 | &entry_message->GetReflection()->GetMessage(*entry_message, value), |
| 1965 | reinterpret_cast<const Message*>(&TestAllTypes::default_instance())); |
| 1966 | |
| 1967 | Message* proto2_message = |
| 1968 | entry_message->GetReflection()->MutableMessage(entry_message, value); |
| 1969 | EXPECT_EQ(unittest::TestAllTypes::descriptor(), |
| 1970 | proto2_message->GetDescriptor()); |
| 1971 | ASSERT_EQ(1, message.map_int32_message().size()); |
| 1972 | } |
| 1973 | |
| 1974 | TEST(GeneratedMapFieldReflectionTest, MergeFromClearMapEntry) { |
| 1975 | unittest::TestMap message; |
| 1976 | const FieldDescriptor* map_field = |
| 1977 | unittest::TestMap::descriptor()->FindFieldByName("map_int32_int32"); |
| 1978 | const FieldDescriptor* key = |
| 1979 | map_field->message_type()->FindFieldByName("key"); |
| 1980 | const FieldDescriptor* value = |
| 1981 | map_field->message_type()->FindFieldByName("value"); |
| 1982 | |
| 1983 | Message* entry_message1 = |
| 1984 | message.GetReflection()->AddMessage(&message, map_field); |
| 1985 | EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key)); |
| 1986 | EXPECT_FALSE( |
| 1987 | entry_message1->GetReflection()->HasField(*entry_message1, value)); |
| 1988 | |
| 1989 | Message* entry_message2 = |
| 1990 | message.GetReflection()->AddMessage(&message, map_field); |
| 1991 | EXPECT_FALSE(entry_message2->GetReflection()->HasField(*entry_message2, key)); |
| 1992 | EXPECT_FALSE( |
| 1993 | entry_message2->GetReflection()->HasField(*entry_message2, value)); |
| 1994 | |
| 1995 | entry_message1->MergeFrom(*entry_message2); |
| 1996 | EXPECT_FALSE(entry_message1->GetReflection()->HasField(*entry_message1, key)); |
| 1997 | EXPECT_FALSE( |
| 1998 | entry_message1->GetReflection()->HasField(*entry_message1, value)); |
| 1999 | } |
| 2000 | |
| 2001 | TEST(GeneratedMapFieldReflectionTest, MapEntryClear) { |
| 2002 | unittest::TestMap message; |
| 2003 | MapTestUtil::MapReflectionTester reflection_tester( |
| 2004 | unittest::TestMap::descriptor()); |
| 2005 | reflection_tester.MutableUnknownFieldsOfMapFieldsViaReflection(&message); |
| 2006 | } |
| 2007 | |
| 2008 | TEST(GeneratedMapFieldReflectionTest, Proto2MapEntryClear) { |
| 2009 | unittest::TestEnumStartWithNonZeroMap message; |
| 2010 | const Descriptor* descriptor = message.GetDescriptor(); |
| 2011 | const FieldDescriptor* field_descriptor = |
| 2012 | descriptor->FindFieldByName("map_field"); |
| 2013 | const FieldDescriptor* value_descriptor = |
| 2014 | field_descriptor->message_type()->FindFieldByName("value"); |
| 2015 | Message* sub_message = |
| 2016 | message.GetReflection()->AddMessage(&message, field_descriptor); |
| 2017 | EXPECT_EQ(1, sub_message->GetReflection()->GetEnumValue(*sub_message, |
| 2018 | value_descriptor)); |
| 2019 | } |
| 2020 | |
| 2021 | // Dynamic Message Test ============================================= |
| 2022 | |
| 2023 | class MapFieldInDynamicMessageTest : public testing::Test { |
| 2024 | protected: |
| 2025 | const DescriptorPool* pool_; |
| 2026 | DynamicMessageFactory factory_; |
| 2027 | const Descriptor* map_descriptor_; |
| 2028 | const Message* map_prototype_; |
| 2029 | |
| 2030 | MapFieldInDynamicMessageTest() |
| 2031 | : pool_(DescriptorPool::generated_pool()), factory_(pool_) {} |
| 2032 | |
| 2033 | virtual void SetUp() { |
| 2034 | map_descriptor_ = |
| 2035 | pool_->FindMessageTypeByName("protobuf_unittest.TestMap"); |
| 2036 | ASSERT_TRUE(map_descriptor_ != NULL); |
| 2037 | map_prototype_ = factory_.GetPrototype(map_descriptor_); |
| 2038 | } |
| 2039 | }; |
| 2040 | |
| 2041 | TEST_F(MapFieldInDynamicMessageTest, MapIndependentOffsets) { |
| 2042 | // Check that all fields have independent offsets by setting each |
| 2043 | // one to a unique value then checking that they all still have those |
| 2044 | // unique values (i.e. they don't stomp each other). |
| 2045 | scoped_ptr<Message> message(map_prototype_->New()); |
| 2046 | MapTestUtil::MapReflectionTester reflection_tester(map_descriptor_); |
| 2047 | |
| 2048 | reflection_tester.SetMapFieldsViaReflection(message.get()); |
| 2049 | reflection_tester.ExpectMapFieldsSetViaReflection(*message); |
| 2050 | } |
| 2051 | |
| 2052 | TEST_F(MapFieldInDynamicMessageTest, Map) { |
| 2053 | // Check that map fields work properly. |
| 2054 | scoped_ptr<Message> message(map_prototype_->New()); |
| 2055 | |
| 2056 | // Check set functions. |
| 2057 | MapTestUtil::MapReflectionTester reflection_tester(map_descriptor_); |
| 2058 | reflection_tester.SetMapFieldsViaReflection(message.get()); |
| 2059 | reflection_tester.ExpectMapFieldsSetViaReflection(*message); |
| 2060 | } |
| 2061 | |
| 2062 | TEST_F(MapFieldInDynamicMessageTest, MapSpaceUsed) { |
| 2063 | // Test that SpaceUsed() works properly |
| 2064 | |
| 2065 | // Since we share the implementation with generated messages, we don't need |
| 2066 | // to test very much here. Just make sure it appears to be working. |
| 2067 | |
| 2068 | scoped_ptr<Message> message(map_prototype_->New()); |
| 2069 | MapTestUtil::MapReflectionTester reflection_tester(map_descriptor_); |
| 2070 | |
| 2071 | int initial_space_used = message->SpaceUsed(); |
| 2072 | |
| 2073 | reflection_tester.SetMapFieldsViaReflection(message.get()); |
| 2074 | EXPECT_LT(initial_space_used, message->SpaceUsed()); |
| 2075 | } |
| 2076 | |
| 2077 | // ReflectionOps Test =============================================== |
| 2078 | |
| 2079 | TEST(ReflectionOpsForMapFieldTest, MapSanityCheck) { |
| 2080 | unittest::TestMap message; |
| 2081 | |
| 2082 | MapTestUtil::SetMapFields(&message); |
| 2083 | MapTestUtil::ExpectMapFieldsSet(message); |
| 2084 | } |
| 2085 | |
| 2086 | TEST(ReflectionOpsForMapFieldTest, MapCopy) { |
| 2087 | unittest::TestMap message, message2; |
| 2088 | |
| 2089 | MapTestUtil::SetMapFields(&message); |
| 2090 | |
| 2091 | ReflectionOps::Copy(message, &message2); |
| 2092 | |
| 2093 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 2094 | |
| 2095 | // Copying from self should be a no-op. |
| 2096 | ReflectionOps::Copy(message2, &message2); |
| 2097 | MapTestUtil::ExpectMapFieldsSet(message2); |
| 2098 | } |
| 2099 | |
| 2100 | TEST(ReflectionOpsForMapFieldTest, MergeMap) { |
| 2101 | // Note: Copy is implemented in terms of Merge() so technically the Copy |
| 2102 | // test already tested most of this. |
| 2103 | |
| 2104 | unittest::TestMap message, message2; |
| 2105 | |
| 2106 | MapTestUtil::SetMapFields(&message); |
| 2107 | |
| 2108 | ReflectionOps::Merge(message2, &message); |
| 2109 | |
| 2110 | MapTestUtil::ExpectMapFieldsSet(message); |
| 2111 | } |
| 2112 | |
| 2113 | TEST(ReflectionOpsForMapFieldTest, ClearMap) { |
| 2114 | unittest::TestMap message; |
| 2115 | |
| 2116 | MapTestUtil::SetMapFields(&message); |
| 2117 | |
| 2118 | ReflectionOps::Clear(&message); |
| 2119 | |
| 2120 | MapTestUtil::ExpectClear(message); |
| 2121 | } |
| 2122 | |
| 2123 | TEST(ReflectionOpsForMapFieldTest, MapDiscardUnknownFields) { |
| 2124 | unittest::TestMap message; |
| 2125 | MapTestUtil::SetMapFields(&message); |
| 2126 | |
| 2127 | // Set some unknown fields in message. |
| 2128 | message.GetReflection()->MutableUnknownFields(&message)-> |
| 2129 | AddVarint(123456, 654321); |
| 2130 | |
| 2131 | // Discard them. |
| 2132 | ReflectionOps::DiscardUnknownFields(&message); |
| 2133 | MapTestUtil::ExpectMapFieldsSet(message); |
| 2134 | |
| 2135 | EXPECT_EQ(0, message.GetReflection()-> |
| 2136 | GetUnknownFields(message).field_count()); |
| 2137 | } |
| 2138 | |
| 2139 | // Wire Format Test ================================================= |
| 2140 | |
| 2141 | TEST(WireFormatForMapFieldTest, ParseMap) { |
| 2142 | unittest::TestMap source, dest; |
| 2143 | string data; |
| 2144 | |
| 2145 | // Serialize using the generated code. |
| 2146 | MapTestUtil::SetMapFields(&source); |
| 2147 | source.SerializeToString(&data); |
| 2148 | |
| 2149 | // Parse using WireFormat. |
| 2150 | io::ArrayInputStream raw_input(data.data(), data.size()); |
| 2151 | io::CodedInputStream input(&raw_input); |
| 2152 | WireFormat::ParseAndMergePartial(&input, &dest); |
| 2153 | |
| 2154 | // Check. |
| 2155 | MapTestUtil::ExpectMapFieldsSet(dest); |
| 2156 | } |
| 2157 | |
| 2158 | TEST(WireFormatForMapFieldTest, MapByteSize) { |
| 2159 | unittest::TestMap message; |
| 2160 | MapTestUtil::SetMapFields(&message); |
| 2161 | |
| 2162 | EXPECT_EQ(message.ByteSize(), WireFormat::ByteSize(message)); |
| 2163 | message.Clear(); |
| 2164 | EXPECT_EQ(0, message.ByteSize()); |
| 2165 | EXPECT_EQ(0, WireFormat::ByteSize(message)); |
| 2166 | } |
| 2167 | |
| 2168 | TEST(WireFormatForMapFieldTest, SerializeMap) { |
| 2169 | unittest::TestMap message; |
| 2170 | string generated_data; |
| 2171 | string dynamic_data; |
| 2172 | |
| 2173 | MapTestUtil::SetMapFields(&message); |
| 2174 | |
| 2175 | // Serialize using the generated code. |
| 2176 | { |
| 2177 | message.ByteSize(); |
| 2178 | io::StringOutputStream raw_output(&generated_data); |
| 2179 | io::CodedOutputStream output(&raw_output); |
| 2180 | message.SerializeWithCachedSizes(&output); |
| 2181 | ASSERT_FALSE(output.HadError()); |
| 2182 | } |
| 2183 | |
| 2184 | // Serialize using WireFormat. |
| 2185 | { |
| 2186 | io::StringOutputStream raw_output(&dynamic_data); |
| 2187 | io::CodedOutputStream output(&raw_output); |
| 2188 | int size = WireFormat::ByteSize(message); |
| 2189 | WireFormat::SerializeWithCachedSizes(message, size, &output); |
| 2190 | ASSERT_FALSE(output.HadError()); |
| 2191 | } |
| 2192 | |
| 2193 | // Should be the same. |
| 2194 | // Don't use EXPECT_EQ here because we're comparing raw binary data and |
| 2195 | // we really don't want it dumped to stdout on failure. |
| 2196 | EXPECT_TRUE(dynamic_data == generated_data); |
| 2197 | } |
| 2198 | |
| 2199 | TEST(WireFormatForMapFieldTest, MapParseHelpers) { |
| 2200 | string data; |
| 2201 | |
| 2202 | { |
| 2203 | // Set up. |
| 2204 | protobuf_unittest::TestMap message; |
| 2205 | MapTestUtil::SetMapFields(&message); |
| 2206 | message.SerializeToString(&data); |
| 2207 | } |
| 2208 | |
| 2209 | { |
| 2210 | // Test ParseFromString. |
| 2211 | protobuf_unittest::TestMap message; |
| 2212 | EXPECT_TRUE(message.ParseFromString(data)); |
| 2213 | MapTestUtil::ExpectMapFieldsSet(message); |
| 2214 | } |
| 2215 | |
| 2216 | { |
| 2217 | // Test ParseFromIstream. |
| 2218 | protobuf_unittest::TestMap message; |
| 2219 | stringstream stream(data); |
| 2220 | EXPECT_TRUE(message.ParseFromIstream(&stream)); |
| 2221 | EXPECT_TRUE(stream.eof()); |
| 2222 | MapTestUtil::ExpectMapFieldsSet(message); |
| 2223 | } |
| 2224 | |
| 2225 | { |
| 2226 | // Test ParseFromBoundedZeroCopyStream. |
| 2227 | string data_with_junk(data); |
| 2228 | data_with_junk.append("some junk on the end"); |
| 2229 | io::ArrayInputStream stream(data_with_junk.data(), data_with_junk.size()); |
| 2230 | protobuf_unittest::TestMap message; |
| 2231 | EXPECT_TRUE(message.ParseFromBoundedZeroCopyStream(&stream, data.size())); |
| 2232 | MapTestUtil::ExpectMapFieldsSet(message); |
| 2233 | } |
| 2234 | |
| 2235 | { |
| 2236 | // Test that ParseFromBoundedZeroCopyStream fails (but doesn't crash) if |
| 2237 | // EOF is reached before the expected number of bytes. |
| 2238 | io::ArrayInputStream stream(data.data(), data.size()); |
| 2239 | protobuf_unittest::TestAllTypes message; |
| 2240 | EXPECT_FALSE( |
| 2241 | message.ParseFromBoundedZeroCopyStream(&stream, data.size() + 1)); |
| 2242 | } |
| 2243 | } |
| 2244 | |
| 2245 | // Text Format Test ================================================= |
| 2246 | |
| 2247 | TEST(TextFormatMapTest, SerializeAndParse) { |
| 2248 | unittest::TestMap source; |
| 2249 | unittest::TestMap dest; |
| 2250 | MapTestUtil::SetMapFields(&source); |
| 2251 | string output; |
| 2252 | |
| 2253 | // Test compact ASCII |
| 2254 | TextFormat::Printer printer; |
| 2255 | printer.PrintToString(source, &output); |
| 2256 | TextFormat::Parser parser; |
| 2257 | EXPECT_TRUE(parser.ParseFromString(output, &dest)); |
| 2258 | MapTestUtil::ExpectMapFieldsSet(dest); |
| 2259 | } |
| 2260 | |
| 2261 | |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 2262 | // arena support ================================================= |
| 2263 | TEST(ArenaTest, ParsingAndSerializingNoHeapAllocation) { |
| 2264 | // Allocate a large initial block to avoid mallocs during hooked test. |
| 2265 | std::vector<char> arena_block(128 * 1024); |
| 2266 | ArenaOptions options; |
| 2267 | options.initial_block = arena_block.data(); |
| 2268 | options.initial_block_size = arena_block.size(); |
| 2269 | Arena arena(options); |
| 2270 | string data; |
| 2271 | data.reserve(128 * 1024); |
| 2272 | |
| 2273 | { |
| 2274 | NoHeapChecker no_heap; |
| 2275 | |
| 2276 | unittest::TestArenaMap* from = |
| 2277 | Arena::CreateMessage<unittest::TestArenaMap>(&arena); |
| 2278 | MapTestUtil::SetArenaMapFields(from); |
| 2279 | from->SerializeToString(&data); |
| 2280 | |
| 2281 | unittest::TestArenaMap* to = |
| 2282 | Arena::CreateMessage<unittest::TestArenaMap>(&arena); |
| 2283 | to->ParseFromString(data); |
| 2284 | MapTestUtil::ExpectArenaMapFieldsSet(*to); |
| 2285 | } |
| 2286 | } |
| 2287 | |
| 2288 | // Use text format parsing and serializing to test reflection api. |
| 2289 | TEST(ArenaTest, RelfectionInTextFormat) { |
| 2290 | Arena arena; |
| 2291 | string data; |
| 2292 | |
| 2293 | TextFormat::Printer printer; |
| 2294 | TextFormat::Parser parser; |
| 2295 | |
| 2296 | unittest::TestArenaMap* from = |
| 2297 | Arena::CreateMessage<unittest::TestArenaMap>(&arena); |
| 2298 | unittest::TestArenaMap* to = |
| 2299 | Arena::CreateMessage<unittest::TestArenaMap>(&arena); |
| 2300 | |
| 2301 | MapTestUtil::SetArenaMapFields(from); |
| 2302 | printer.PrintToString(*from, &data); |
| 2303 | |
| 2304 | EXPECT_TRUE(parser.ParseFromString(data, to)); |
| 2305 | MapTestUtil::ExpectArenaMapFieldsSet(*to); |
| 2306 | } |
| 2307 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 2308 | } // namespace internal |
| 2309 | } // namespace protobuf |
| 2310 | } // namespace google |