OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
(...skipping 506 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 while (pickle.capacity_after_header() != pickle.payload_size()) | 517 while (pickle.capacity_after_header() != pickle.payload_size()) |
518 pickle.WriteBool(true); | 518 pickle.WriteBool(true); |
519 | 519 |
520 // Make a deep copy. | 520 // Make a deep copy. |
521 Pickle pickle2(pickle); | 521 Pickle pickle2(pickle); |
522 | 522 |
523 // Check that there isn't any extraneous capacity. | 523 // Check that there isn't any extraneous capacity. |
524 EXPECT_EQ(pickle.capacity_after_header(), pickle2.capacity_after_header()); | 524 EXPECT_EQ(pickle.capacity_after_header(), pickle2.capacity_after_header()); |
525 } | 525 } |
526 | 526 |
| 527 namespace { |
| 528 |
| 529 // Publicly exposes the ClaimBytes interface for testing. |
| 530 class TestingPickle : public base::Pickle { |
| 531 public: |
| 532 TestingPickle() {} |
| 533 |
| 534 void* ClaimBytes(size_t num_bytes) { |
| 535 return base::Pickle::ClaimBytes(num_bytes); |
| 536 } |
| 537 }; |
| 538 |
| 539 } // namespace |
| 540 |
| 541 // Checks that claimed bytes are zero-initialized. |
| 542 TEST(PickleTest, ClaimBytesInitialization) { |
| 543 static const int kChunkSize = 64; |
| 544 TestingPickle pickle; |
| 545 void* bytes = pickle.ClaimBytes(kChunkSize); |
| 546 for (size_t i = 0; i < kChunkSize; ++i) { |
| 547 EXPECT_EQ(0, static_cast<char*>(bytes)[i]); |
| 548 } |
| 549 } |
| 550 |
| 551 // Checks that ClaimBytes properly advances the write offset. |
| 552 TEST(PickleTest, ClaimBytes) { |
| 553 std::string data("Hello, world!"); |
| 554 |
| 555 TestingPickle pickle; |
| 556 pickle.WriteSizeT(data.size()); |
| 557 void* bytes = pickle.ClaimBytes(data.size()); |
| 558 pickle.WriteInt(42); |
| 559 memcpy(bytes, data.data(), data.size()); |
| 560 |
| 561 PickleIterator iter(pickle); |
| 562 size_t out_data_length; |
| 563 EXPECT_TRUE(iter.ReadSizeT(&out_data_length)); |
| 564 EXPECT_EQ(data.size(), out_data_length); |
| 565 |
| 566 const char* out_data = nullptr; |
| 567 EXPECT_TRUE(iter.ReadBytes(&out_data, out_data_length)); |
| 568 EXPECT_EQ(data, std::string(out_data, out_data_length)); |
| 569 |
| 570 int out_value; |
| 571 EXPECT_TRUE(iter.ReadInt(&out_value)); |
| 572 EXPECT_EQ(42, out_value); |
| 573 } |
| 574 |
527 } // namespace base | 575 } // namespace base |
OLD | NEW |