chunked_vector_test.cc
Go to the documentation of this file.
1 // Copyright 2021 gRPC 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 // http://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 <gtest/gtest.h>
18 
20 
21 namespace grpc_core {
22 namespace testing {
23 
25  ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test"));
26 
27 static constexpr size_t kInitialArenaSize = 1024;
28 static constexpr size_t kChunkSize = 3;
29 
33  EXPECT_EQ(0, v.size());
34 }
35 
39 
40  // Populate 2 chunks of memory, and 2/3 of a final chunk.
41  EXPECT_EQ(0, v.size());
42  v.EmplaceBack(1);
43  EXPECT_EQ(1, v.size());
44  v.EmplaceBack(2);
45  EXPECT_EQ(2, v.size());
46  v.EmplaceBack(3);
47  EXPECT_EQ(3, v.size());
48  v.EmplaceBack(4);
49  EXPECT_EQ(4, v.size());
50  v.EmplaceBack(5);
51  EXPECT_EQ(5, v.size());
52  v.EmplaceBack(6);
53  EXPECT_EQ(6, v.size());
54  v.EmplaceBack(7);
55  EXPECT_EQ(7, v.size());
56  v.EmplaceBack(8);
57  EXPECT_EQ(8, v.size());
58 
59  // Now pop all of them out and check the expected ordering.
60  EXPECT_EQ(8, v.PopBack());
61  EXPECT_EQ(7, v.size());
62  EXPECT_EQ(7, v.PopBack());
63  EXPECT_EQ(6, v.size());
64  EXPECT_EQ(6, v.PopBack());
65  EXPECT_EQ(5, v.size());
66  EXPECT_EQ(5, v.PopBack());
67  EXPECT_EQ(4, v.size());
68  EXPECT_EQ(4, v.PopBack());
69  EXPECT_EQ(3, v.size());
70  EXPECT_EQ(3, v.PopBack());
71  EXPECT_EQ(2, v.size());
72  EXPECT_EQ(2, v.PopBack());
73  EXPECT_EQ(1, v.size());
74  EXPECT_EQ(1, v.PopBack());
75  EXPECT_EQ(0, v.size());
76 }
77 
78 TEST(ChunkedVector, Iterate) {
81  v.EmplaceBack(1);
82  v.EmplaceBack(2);
83  v.EmplaceBack(3);
84  v.EmplaceBack(4);
85  v.EmplaceBack(5);
86  v.EmplaceBack(6);
87  v.EmplaceBack(7);
88  v.EmplaceBack(8);
89 
90  auto it = v.begin();
91  EXPECT_EQ(1, *it);
92  ++it;
93  EXPECT_EQ(2, *it);
94  ++it;
95  EXPECT_EQ(3, *it);
96  ++it;
97  EXPECT_EQ(4, *it);
98  ++it;
99  EXPECT_EQ(5, *it);
100  ++it;
101  EXPECT_EQ(6, *it);
102  ++it;
103  EXPECT_EQ(7, *it);
104  ++it;
105  EXPECT_EQ(8, *it);
106  ++it;
107  EXPECT_EQ(v.end(), it);
108 }
109 
110 TEST(ChunkedVector, ConstIterate) {
113  v.EmplaceBack(1);
114  v.EmplaceBack(2);
115  v.EmplaceBack(3);
116  v.EmplaceBack(4);
117  v.EmplaceBack(5);
118  v.EmplaceBack(6);
119  v.EmplaceBack(7);
120  v.EmplaceBack(8);
121 
122  auto it = v.cbegin();
123  EXPECT_EQ(1, *it);
124  ++it;
125  EXPECT_EQ(2, *it);
126  ++it;
127  EXPECT_EQ(3, *it);
128  ++it;
129  EXPECT_EQ(4, *it);
130  ++it;
131  EXPECT_EQ(5, *it);
132  ++it;
133  EXPECT_EQ(6, *it);
134  ++it;
135  EXPECT_EQ(7, *it);
136  ++it;
137  EXPECT_EQ(8, *it);
138  ++it;
139  EXPECT_EQ(v.cend(), it);
140 }
141 
145  v.EmplaceBack(1);
146  EXPECT_EQ(v.size(), 1);
147  v.Clear();
148  EXPECT_EQ(v.size(), 0);
149  EXPECT_EQ(v.begin(), v.end());
150 }
151 
152 TEST(ChunkedVector, RemoveIf) {
155  v.EmplaceBack(1);
156  v.SetEnd(std::remove_if(v.begin(), v.end(), [](int i) { return i == 1; }));
157  EXPECT_EQ(v.size(), 0);
158 }
159 
160 } // namespace testing
161 
162 } // namespace grpc_core
163 
164 int main(int argc, char** argv) {
165  ::testing::InitGoogleTest(&argc, argv);
166  return RUN_ALL_TESTS();
167 }
grpc_core::MakeScopedArena
ScopedArenaPtr MakeScopedArena(size_t initial_size, MemoryAllocator *memory_allocator)
Definition: src/core/lib/resource_quota/arena.h:130
testing
Definition: aws_request_signer_test.cc:25
regen-readme.it
it
Definition: regen-readme.py:15
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
grpc_core
Definition: call_metric_recorder.h:31
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
grpc_core::ResourceQuota::Default
static ResourceQuotaRefPtr Default()
Definition: resource_quota.cc:27
grpc_core::testing::kInitialArenaSize
static constexpr size_t kInitialArenaSize
Definition: chunked_vector_test.cc:27
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
grpc_core::ChunkedVector
Definition: chunked_vector.h:37
resource_quota.h
chunked_vector.h
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
google::protobuf.internal.python_message.Clear
Clear
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/python_message.py:1430
main
int main(int argc, char **argv)
Definition: chunked_vector_test.cc:164
grpc_core::testing::g_memory_allocator
static auto * g_memory_allocator
Definition: chunked_vector_test.cc:24
grpc_core::testing::TEST
TEST(ServiceConfigParserTest, DoubleRegistration)
Definition: service_config_test.cc:448
grpc_core::testing::kChunkSize
static constexpr size_t kChunkSize
Definition: chunked_vector_test.cc:28
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:45