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 Pickle { |
| 531 public: |
| 532 TestingPickle() {} |
| 533 |
| 534 void* ClaimBytes(size_t num_bytes) { return Pickle::ClaimBytes(num_bytes); } |
| 535 }; |
| 536 |
| 537 } // namespace |
| 538 |
| 539 // Checks that claimed bytes are zero-initialized. |
| 540 TEST(PickleTest, ClaimBytesInitialization) { |
| 541 static const int kChunkSize = 64; |
| 542 TestingPickle pickle; |
| 543 const char* bytes = static_cast<const char*>(pickle.ClaimBytes(kChunkSize)); |
| 544 for (size_t i = 0; i < kChunkSize; ++i) { |
| 545 EXPECT_EQ(0, bytes[i]); |
| 546 } |
| 547 } |
| 548 |
| 549 // Checks that ClaimBytes properly advances the write offset. |
| 550 TEST(PickleTest, ClaimBytes) { |
| 551 std::string data("Hello, world!"); |
| 552 |
| 553 TestingPickle pickle; |
| 554 pickle.WriteSizeT(data.size()); |
| 555 void* bytes = pickle.ClaimBytes(data.size()); |
| 556 pickle.WriteInt(42); |
| 557 memcpy(bytes, data.data(), data.size()); |
| 558 |
| 559 PickleIterator iter(pickle); |
| 560 size_t out_data_length; |
| 561 EXPECT_TRUE(iter.ReadSizeT(&out_data_length)); |
| 562 EXPECT_EQ(data.size(), out_data_length); |
| 563 |
| 564 const char* out_data = nullptr; |
| 565 EXPECT_TRUE(iter.ReadBytes(&out_data, out_data_length)); |
| 566 EXPECT_EQ(data, std::string(out_data, out_data_length)); |
| 567 |
| 568 int out_value; |
| 569 EXPECT_TRUE(iter.ReadInt(&out_value)); |
| 570 EXPECT_EQ(42, out_value); |
| 571 } |
| 572 |
527 } // namespace base | 573 } // namespace base |
OLD | NEW |