1 // Copyright 2021 The Abseil Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "absl/strings/internal/cord_rep_crc.h"
16
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 #include "absl/base/config.h"
20 #include "absl/strings/internal/cord_internal.h"
21 #include "absl/strings/internal/cord_rep_test_util.h"
22
23 namespace absl {
24 ABSL_NAMESPACE_BEGIN
25 namespace cord_internal {
26 namespace {
27
28 using ::absl::cordrep_testing::MakeFlat;
29 using ::testing::Eq;
30 using ::testing::Ne;
31
32 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
33
TEST(CordRepCrc,NewWithNullPtr)34 TEST(CordRepCrc, NewWithNullPtr) {
35 EXPECT_DEATH(CordRepCrc::New(nullptr, 0), "");
36 }
37
TEST(CordRepCrc,RemoveCrcWithNullptr)38 TEST(CordRepCrc, RemoveCrcWithNullptr) {
39 EXPECT_DEATH(RemoveCrcNode(nullptr), "");
40 }
41
42 #endif // !NDEBUG && GTEST_HAS_DEATH_TEST
43
TEST(CordRepCrc,NewDestroy)44 TEST(CordRepCrc, NewDestroy) {
45 CordRep* rep = cordrep_testing::MakeFlat("Hello world");
46 CordRepCrc* crc = CordRepCrc::New(rep, 12345);
47 EXPECT_TRUE(crc->refcount.IsOne());
48 EXPECT_THAT(crc->child, Eq(rep));
49 EXPECT_THAT(crc->crc, Eq(12345));
50 EXPECT_TRUE(rep->refcount.IsOne());
51 CordRepCrc::Destroy(crc);
52 }
53
TEST(CordRepCrc,NewExistingCrcNotShared)54 TEST(CordRepCrc, NewExistingCrcNotShared) {
55 CordRep* rep = cordrep_testing::MakeFlat("Hello world");
56 CordRepCrc* crc = CordRepCrc::New(rep, 12345);
57 CordRepCrc* new_crc = CordRepCrc::New(crc, 54321);
58 EXPECT_THAT(new_crc, Eq(crc));
59 EXPECT_TRUE(new_crc->refcount.IsOne());
60 EXPECT_THAT(new_crc->child, Eq(rep));
61 EXPECT_THAT(new_crc->crc, Eq(54321));
62 EXPECT_TRUE(rep->refcount.IsOne());
63 CordRepCrc::Destroy(new_crc);
64 }
65
TEST(CordRepCrc,NewExistingCrcShared)66 TEST(CordRepCrc, NewExistingCrcShared) {
67 CordRep* rep = cordrep_testing::MakeFlat("Hello world");
68 CordRepCrc* crc = CordRepCrc::New(rep, 12345);
69 CordRep::Ref(crc);
70 CordRepCrc* new_crc = CordRepCrc::New(crc, 54321);
71
72 EXPECT_THAT(new_crc, Ne(crc));
73 EXPECT_TRUE(new_crc->refcount.IsOne());
74 EXPECT_TRUE(crc->refcount.IsOne());
75 EXPECT_FALSE(rep->refcount.IsOne());
76 EXPECT_THAT(crc->child, Eq(rep));
77 EXPECT_THAT(new_crc->child, Eq(rep));
78 EXPECT_THAT(crc->crc, Eq(12345));
79 EXPECT_THAT(new_crc->crc, Eq(54321));
80
81 CordRep::Unref(crc);
82 CordRep::Unref(new_crc);
83 }
84
TEST(CordRepCrc,RemoveCrcNotCrc)85 TEST(CordRepCrc, RemoveCrcNotCrc) {
86 CordRep* rep = cordrep_testing::MakeFlat("Hello world");
87 CordRep* nocrc = RemoveCrcNode(rep);
88 EXPECT_THAT(nocrc, Eq(rep));
89 CordRep::Unref(nocrc);
90 }
91
TEST(CordRepCrc,RemoveCrcNotShared)92 TEST(CordRepCrc, RemoveCrcNotShared) {
93 CordRep* rep = cordrep_testing::MakeFlat("Hello world");
94 CordRepCrc* crc = CordRepCrc::New(rep, 12345);
95 CordRep* nocrc = RemoveCrcNode(crc);
96 EXPECT_THAT(nocrc, Eq(rep));
97 EXPECT_TRUE(rep->refcount.IsOne());
98 CordRep::Unref(nocrc);
99 }
100
TEST(CordRepCrc,RemoveCrcShared)101 TEST(CordRepCrc, RemoveCrcShared) {
102 CordRep* rep = cordrep_testing::MakeFlat("Hello world");
103 CordRepCrc* crc = CordRepCrc::New(rep, 12345);
104 CordRep::Ref(crc);
105 CordRep* nocrc = RemoveCrcNode(crc);
106 EXPECT_THAT(nocrc, Eq(rep));
107 EXPECT_FALSE(rep->refcount.IsOne());
108 CordRep::Unref(nocrc);
109 CordRep::Unref(crc);
110 }
111
112 } // namespace
113 } // namespace cord_internal
114 ABSL_NAMESPACE_END
115 } // namespace absl
116