diff options
author | 2021-11-19 10:46:01 -0800 | |
---|---|---|
committer | 2021-11-19 14:24:39 -0500 | |
commit | 72c765111173a61de6e4184bb837f855b7869952 (patch) | |
tree | 4dbbe8c6536828d6d86dc6b43b3d5b9e3a06bdf1 /absl/strings/cord_test.cc | |
parent | 8a4e6b8cc9baa26ebdc87fd813dc808d77e0640c (diff) |
Export of internal Abseil changes
--
28ba85cbeb766341969fda677bbd8df8615b349f by Martijn Vels <mvels@google.com>:
Internally allow for CordRepFlat sizes up to 256KB
The default flat size will remain at 4KB, but this allows for future specializations such as file io and compression where larger buffers can be used providing 'zero copy' streaming optimizations.
PiperOrigin-RevId: 411095538
--
0c1cae5ff376a0955a5b67868723836665737801 by Abseil Team <absl-team@google.com>:
cord_rep_btree: Reduce nesting in `DestroyTree` using guard clauses
PiperOrigin-RevId: 411093741
GitOrigin-RevId: 28ba85cbeb766341969fda677bbd8df8615b349f
Change-Id: Ieb84ed409f1e8a8bca74edebbf2d097a9ec828a7
Diffstat (limited to 'absl/strings/cord_test.cc')
-rw-r--r-- | absl/strings/cord_test.cc | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc index 188fbc2e..8a311799 100644 --- a/absl/strings/cord_test.cc +++ b/absl/strings/cord_test.cc @@ -43,6 +43,10 @@ #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" +// convenience local constants +static constexpr auto FLAT = absl::cord_internal::FLAT; +static constexpr auto MAX_FLAT_TAG = absl::cord_internal::MAX_FLAT_TAG; + typedef std::mt19937_64 RandomEngine; static std::string RandomLowercaseString(RandomEngine* rng); @@ -260,6 +264,74 @@ class CordTest : public testing::TestWithParam<int> { INSTANTIATE_TEST_SUITE_P(WithParam, CordTest, testing::Values(0, 1, 2, 3), CordTest::ToString); + +TEST(CordRepFlat, AllFlatCapacities) { + using absl::cord_internal::CordRep; + using absl::cord_internal::CordRepFlat; + using absl::cord_internal::kFlatOverhead; + + // Explicitly and redundantly assert built-in min/max limits + static_assert(absl::cord_internal::kFlatOverhead < 32, ""); + static_assert(absl::cord_internal::kMinFlatSize == 32, ""); + static_assert(absl::cord_internal::kMaxLargeFlatSize == 256 << 10, ""); + EXPECT_EQ(absl::cord_internal::TagToAllocatedSize(FLAT), 32); + EXPECT_EQ(absl::cord_internal::TagToAllocatedSize(MAX_FLAT_TAG), 256 << 10); + + // Verify all tags to map perfectly back and forth, and + // that sizes are monotonically increasing. + size_t last_size = 0; + for (int tag = FLAT; tag <= MAX_FLAT_TAG; ++tag) { + size_t size = absl::cord_internal::TagToAllocatedSize(tag); + ASSERT_GT(size, last_size); + ASSERT_EQ(absl::cord_internal::TagToAllocatedSize(tag), size); + last_size = size; + } + + // All flat size from 32 - 512 are 8 byte granularity + for (size_t size = 32; size <= 512; size += 8) { + ASSERT_EQ(absl::cord_internal::RoundUpForTag(size), size); + uint8_t tag = absl::cord_internal::AllocatedSizeToTag(size); + ASSERT_EQ(absl::cord_internal::TagToAllocatedSize(tag), size); + } + + // All flat sizes from 512 - 8192 are 64 byte granularity + for (size_t size = 512; size <= 8192; size += 64) { + ASSERT_EQ(absl::cord_internal::RoundUpForTag(size), size); + uint8_t tag = absl::cord_internal::AllocatedSizeToTag(size); + ASSERT_EQ(absl::cord_internal::TagToAllocatedSize(tag), size); + } + + // All flat sizes from 8KB to 256KB are 4KB granularity + for (size_t size = 8192; size <= 256 * 1024; size += 4 * 1024) { + ASSERT_EQ(absl::cord_internal::RoundUpForTag(size), size); + uint8_t tag = absl::cord_internal::AllocatedSizeToTag(size); + ASSERT_EQ(absl::cord_internal::TagToAllocatedSize(tag), size); + } +} + +TEST(CordRepFlat, MaxFlatSize) { + using absl::cord_internal::CordRep; + using absl::cord_internal::CordRepFlat; + using absl::cord_internal::kMaxFlatLength; + CordRepFlat* flat = CordRepFlat::New(kMaxFlatLength); + EXPECT_EQ(flat->Capacity(), kMaxFlatLength); + CordRep::Unref(flat); + + flat = CordRepFlat::New(kMaxFlatLength * 4); + EXPECT_EQ(flat->Capacity(), kMaxFlatLength); + CordRep::Unref(flat); +} + +TEST(CordRepFlat, MaxLargeFlatSize) { + using absl::cord_internal::CordRep; + using absl::cord_internal::CordRepFlat; + using absl::cord_internal::kFlatOverhead; + const size_t size = 256 * 1024 - kFlatOverhead; + CordRepFlat* flat = CordRepFlat::New(CordRepFlat::Large(), size); + EXPECT_GE(flat->Capacity(), size); + CordRep::Unref(flat); +} + TEST_P(CordTest, AllFlatSizes) { using absl::strings_internal::CordTestAccess; |