|
62 | 62 | #include "absl/strings/str_format.h" |
63 | 63 | #include "absl/strings/string_view.h" |
64 | 64 | #include "absl/types/compare.h" |
| 65 | +#include "absl/types/span.h" |
65 | 66 |
|
66 | 67 | // convenience local constants |
67 | 68 | static constexpr auto FLAT = absl::cord_internal::FLAT; |
@@ -735,6 +736,53 @@ TEST_P(CordTest, AppendToString) { |
735 | 736 | "appending ", "to ", "a ", "string."}))); |
736 | 737 | } |
737 | 738 |
|
| 739 | +static void VerifyCopyToSpan(const absl::Cord& cord) { |
| 740 | + // Test with span exactly the same size as the cord. |
| 741 | + { |
| 742 | + std::string dst(cord.size(), '\0'); |
| 743 | + size_t copied = absl::CopyCordToSpan(cord, absl::MakeSpan(dst)); |
| 744 | + EXPECT_EQ(copied, cord.size()); |
| 745 | + EXPECT_EQ(dst, cord); |
| 746 | + } |
| 747 | + |
| 748 | + // Test with span larger than the cord. |
| 749 | + { |
| 750 | + std::string dst(cord.size() + 10, 'x'); |
| 751 | + size_t copied = absl::CopyCordToSpan(cord, absl::MakeSpan(dst)); |
| 752 | + EXPECT_EQ(copied, cord.size()); |
| 753 | + EXPECT_EQ(absl::string_view(dst).substr(0, copied), cord); |
| 754 | + if (cord.size() < dst.size()) { |
| 755 | + absl::string_view tail = absl::string_view(dst).substr(copied); |
| 756 | + EXPECT_EQ(tail, std::string(tail.size(), 'x')); |
| 757 | + } |
| 758 | + } |
| 759 | + |
| 760 | + // Test with span smaller than the cord. |
| 761 | + { |
| 762 | + size_t target_size = cord.size() / 2; |
| 763 | + std::string dst(target_size, '\0'); |
| 764 | + size_t copied = absl::CopyCordToSpan(cord, absl::MakeSpan(dst)); |
| 765 | + EXPECT_EQ(copied, target_size); |
| 766 | + EXPECT_EQ(dst, std::string(cord).substr(0, target_size)); |
| 767 | + } |
| 768 | + |
| 769 | + // Test with empty span. |
| 770 | + { |
| 771 | + char c = 'x'; |
| 772 | + size_t copied = absl::CopyCordToSpan(cord, absl::MakeSpan(&c, 0)); |
| 773 | + EXPECT_EQ(copied, 0); |
| 774 | + EXPECT_EQ(c, 'x'); |
| 775 | + } |
| 776 | +} |
| 777 | + |
| 778 | +TEST_P(CordTest, CopyToSpan) { |
| 779 | + VerifyCopyToSpan(absl::Cord()); // Empty cords cannot be hardened. |
| 780 | + VerifyCopyToSpan(MaybeHardened(absl::Cord("small cord"))); |
| 781 | + VerifyCopyToSpan(MaybeHardened( |
| 782 | + absl::MakeFragmentedCord({"fragmented ", "cord ", "to ", "test ", |
| 783 | + "copying ", "to ", "a ", "span."}))); |
| 784 | +} |
| 785 | + |
738 | 786 | TEST_P(CordTest, AppendEmptyBuffer) { |
739 | 787 | absl::Cord cord; |
740 | 788 | cord.Append(absl::CordBuffer()); |
|
0 commit comments