cord_rep_crc_test.cc
Go to the documentation of this file.
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 
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"
22 
23 namespace absl {
25 namespace cord_internal {
26 namespace {
27 
31 
32 #if !defined(NDEBUG) && GTEST_HAS_DEATH_TEST
33 
34 TEST(CordRepCrc, NewWithNullPtr) {
35  EXPECT_DEATH(CordRepCrc::New(nullptr, 0), "");
36 }
37 
38 TEST(CordRepCrc, RemoveCrcWithNullptr) {
39  EXPECT_DEATH(RemoveCrcNode(nullptr), "");
40 }
41 
42 #endif // !NDEBUG && GTEST_HAS_DEATH_TEST
43 
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));
52 }
53 
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));
63  CordRepCrc::Destroy(new_crc);
64 }
65 
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());
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 
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 
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));
98  CordRep::Unref(nocrc);
99 }
100 
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));
108  CordRep::Unref(nocrc);
109  CordRep::Unref(crc);
110 }
111 
112 } // namespace
113 } // namespace cord_internal
115 } // namespace absl
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
absl::cord_internal::CordRepCrc::New
static CordRepCrc * New(CordRep *child, uint32_t crc)
Definition: cord_rep_crc.cc:27
absl::cord_internal::CordRep::Unref
static void Unref(CordRep *rep)
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:642
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
testing::Ne
internal::NeMatcher< Rhs > Ne(Rhs x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8609
absl::TEST
TEST(NotificationTest, SanityTest)
Definition: abseil-cpp/absl/synchronization/notification_test.cc:126
absl::cord_internal::RemoveCrcNode
CordRep * RemoveCrcNode(CordRep *rep)
Definition: cord_rep_crc.h:53
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::cordrep_testing::MakeFlat
cord_internal::CordRepFlat * MakeFlat(absl::string_view value)
Definition: cord_rep_test_util.h:45
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
absl::ABSL_NAMESPACE_BEGIN::MakeFlat
CordRep * MakeFlat(absl::string_view s, size_t extra=0)
Definition: abseil-cpp/absl/strings/cord_ring_test.cc:197
absl::cord_internal::CordRep::refcount
RefcountAndFlags refcount
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:229
testing::Eq
internal::EqMatcher< T > Eq(T x)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8561
absl::cord_internal::CordRepCrc::Destroy
static void Destroy(CordRepCrc *node)
Definition: cord_rep_crc.cc:47
rep
const CordRep * rep
Definition: cord_analysis.cc:53
absl::cord_internal::CordRep::Ref
static CordRep * Ref(CordRep *rep)
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:634
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
cord_rep_test_util.h
absl::cord_internal::RefcountAndFlags::IsOne
bool IsOne()
Definition: abseil-cpp/absl/strings/internal/cord_internal.h:144
cord_rep_crc.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:58:03