summaryrefslogtreecommitdiff
path: root/absl/strings/cord_test.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2020-10-19 15:25:26 -0700
committerGravatar Gennadiy Rozental <rogeeff@google.com>2020-10-19 19:24:23 -0400
commit4b915e70929ca2d6152240facc83d3d38c5d5f28 (patch)
tree3663ad6f3ddb8f0a94045bac592901ee7298447e /absl/strings/cord_test.cc
parent8f1c34a77a2ba04512b7f9cbc6013d405e6a0b31 (diff)
Export of internal Abseil changes
-- 77c85460dc3c46593b231c5161ac55273bb0c7ef by Abseil Team <absl-team@google.com>: Support int128 in SimpleAtoi. PiperOrigin-RevId: 337946262 -- 0be53049ccf8309650e4e22f23b290e5f75ee828 by Gennadiy Rozental <rogeeff@google.com>: Update build scripts to use --mount instead of --volume to mount the host data into a container. This is somewhat more verbose, but more readable and flexible. More importantly this is what docker documentation is recomending to use now. PiperOrigin-RevId: 337898761 -- 3bc877f1679fdf61ecbf4365287a0403cfc9b53e by Samuel Benzaquen <sbenza@google.com>: Add Cord constructor for constinit instances. PiperOrigin-RevId: 337871148 -- 8b87701892b9c325e78ad4e8e4f16b785a744622 by Chris Kennelly <ckennelly@google.com>: Use the address of kSeed, rather than its value. We initialize kSeed with &kSeed, so these are equivalent, but kSeed requires a load from memory while &kSeed can be formed as a RIP-relative lea. PiperOrigin-RevId: 337868415 GitOrigin-RevId: 77c85460dc3c46593b231c5161ac55273bb0c7ef Change-Id: I3d06c18a123f1be29dad5801e8625952dc41cd95
Diffstat (limited to 'absl/strings/cord_test.cc')
-rw-r--r--absl/strings/cord_test.cc82
1 files changed, 82 insertions, 0 deletions
diff --git a/absl/strings/cord_test.cc b/absl/strings/cord_test.cc
index dbed3e9a..7942bfc0 100644
--- a/absl/strings/cord_test.cc
+++ b/absl/strings/cord_test.cc
@@ -181,6 +181,8 @@ 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(); }
};
ABSL_NAMESPACE_END
@@ -1627,3 +1629,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{}));
+}