summaryrefslogtreecommitdiff
path: root/absl/strings/cord_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'absl/strings/cord_test.cc')
-rw-r--r--absl/strings/cord_test.cc106
1 files changed, 103 insertions, 3 deletions
diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc
index 4443c828..f9982428 100644
--- a/absl/strings/cord_test.cc
+++ b/absl/strings/cord_test.cc
@@ -1,3 +1,17 @@
+// Copyright 2020 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
#include "absl/strings/cord.h"
#include <algorithm>
@@ -167,6 +181,12 @@ class CordTestPeer {
const Cord& c, absl::FunctionRef<void(absl::string_view)> callback) {
c.ForEachChunk(callback);
}
+
+ static bool IsTree(const Cord& c) { return c.contents_.is_tree(); }
+
+ static cord_internal::CordzInfo* GetCordzInfo(const Cord& c) {
+ return c.contents_.cordz_info();
+ }
};
ABSL_NAMESPACE_END
@@ -351,7 +371,7 @@ TEST(Cord, Subcord) {
for (size_t end_pos : positions) {
if (end_pos < pos || end_pos > a.size()) continue;
absl::Cord sa = a.Subcord(pos, end_pos - pos);
- EXPECT_EQ(absl::string_view(s).substr(pos, end_pos - pos),
+ ASSERT_EQ(absl::string_view(s).substr(pos, end_pos - pos),
std::string(sa))
<< a;
}
@@ -363,7 +383,7 @@ TEST(Cord, Subcord) {
for (size_t pos = 0; pos <= sh.size(); ++pos) {
for (size_t n = 0; n <= sh.size() - pos; ++n) {
absl::Cord sc = c.Subcord(pos, n);
- EXPECT_EQ(sh.substr(pos, n), std::string(sc)) << c;
+ ASSERT_EQ(sh.substr(pos, n), std::string(sc)) << c;
}
}
@@ -373,7 +393,7 @@ TEST(Cord, Subcord) {
while (sa.size() > 1) {
sa = sa.Subcord(1, sa.size() - 2);
ss = ss.substr(1, ss.size() - 2);
- EXPECT_EQ(ss, std::string(sa)) << a;
+ ASSERT_EQ(ss, std::string(sa)) << a;
if (HasFailure()) break; // halt cascade
}
@@ -1613,3 +1633,83 @@ TEST(CordDeathTest, Hardening) {
EXPECT_DEATH_IF_SUPPORTED(static_cast<void>(cord.chunk_end()->empty()), "");
EXPECT_DEATH_IF_SUPPORTED(++cord.chunk_end(), "");
}
+
+class AfterExitCordTester {
+ public:
+ bool Set(absl::Cord* cord, absl::string_view expected) {
+ cord_ = cord;
+ expected_ = expected;
+ return true;
+ }
+
+ ~AfterExitCordTester() {
+ EXPECT_EQ(*cord_, expected_);
+ }
+ private:
+ absl::Cord* cord_;
+ absl::string_view expected_;
+};
+
+template <typename Str>
+void TestConstinitConstructor(Str) {
+ const auto expected = Str::value;
+ // Defined before `cord` to be destroyed after it.
+ static AfterExitCordTester exit_tester; // NOLINT
+ ABSL_CONST_INIT static absl::Cord cord(Str{}); // NOLINT
+ static bool init_exit_tester = exit_tester.Set(&cord, expected);
+ (void)init_exit_tester;
+
+ EXPECT_EQ(cord, expected);
+ // Copy the object and test the copy, and the original.
+ {
+ absl::Cord copy = cord;
+ EXPECT_EQ(copy, expected);
+ }
+ // The original still works
+ EXPECT_EQ(cord, expected);
+
+ // Try making adding more structure to the tree.
+ {
+ absl::Cord copy = cord;
+ std::string expected_copy(expected);
+ for (int i = 0; i < 10; ++i) {
+ copy.Append(cord);
+ absl::StrAppend(&expected_copy, expected);
+ EXPECT_EQ(copy, expected_copy);
+ }
+ }
+
+ // Make sure we are using the right branch during constant evaluation.
+ EXPECT_EQ(absl::CordTestPeer::IsTree(cord), cord.size() >= 16);
+
+ for (int i = 0; i < 10; ++i) {
+ // Make a few more Cords from the same global rep.
+ // This tests what happens when the refcount for it gets below 1.
+ EXPECT_EQ(expected, absl::Cord(Str{}));
+ }
+}
+
+constexpr int SimpleStrlen(const char* p) {
+ return *p ? 1 + SimpleStrlen(p + 1) : 0;
+}
+
+struct ShortView {
+ constexpr absl::string_view operator()() const {
+ return absl::string_view("SSO string", SimpleStrlen("SSO string"));
+ }
+};
+
+struct LongView {
+ constexpr absl::string_view operator()() const {
+ return absl::string_view("String that does not fit SSO.",
+ SimpleStrlen("String that does not fit SSO."));
+ }
+};
+
+
+TEST(Cord, ConstinitConstructor) {
+ TestConstinitConstructor(
+ absl::strings_internal::MakeStringConstant(ShortView{}));
+ TestConstinitConstructor(
+ absl::strings_internal::MakeStringConstant(LongView{}));
+}