1 //
2 //
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/util/ring_buffer.h"
20
21 #include <grpc/support/port_platform.h>
22
23 #include "gtest/gtest.h"
24
25 namespace grpc_core {
26
27 constexpr int kBufferCapacity = 1000;
28
TEST(RingBufferTest,BufferAppendPopTest)29 TEST(RingBufferTest, BufferAppendPopTest) {
30 RingBuffer<int, kBufferCapacity> buffer;
31 EXPECT_FALSE(buffer.PopIfNotEmpty().has_value());
32
33 for (int i = 0; i < (3 * kBufferCapacity) / 2; ++i) {
34 buffer.Append(i);
35 }
36 // Pop half of the elements. Elements in [kBufferCapacity / 2,
37 // kBufferCapacity) are popped.
38 int j = kBufferCapacity / 2;
39 for (int i = 0; i < kBufferCapacity / 2; ++i) {
40 EXPECT_EQ(buffer.PopIfNotEmpty(), j++);
41 }
42 EXPECT_EQ(j, kBufferCapacity);
43 // Iterate over the remaining elements.
44 for (auto it = buffer.begin(); it != buffer.end(); ++it) {
45 EXPECT_EQ(*it, j++);
46 }
47 // Elements in [kBufferCapacity, (3 * kBufferCapacity) / 2) should be present.
48 EXPECT_EQ(j, (3 * kBufferCapacity) / 2);
49
50 // Append some more elements. The buffer should now have elements in
51 // [kBufferCapacity, 2 * kBufferCapacity).
52 for (int i = 0; i < kBufferCapacity / 2; ++i) {
53 buffer.Append(j++);
54 }
55 // Pop all the elements.
56 j = kBufferCapacity;
57 while (true) {
58 auto ret = buffer.PopIfNotEmpty();
59 if (!ret.has_value()) break;
60 EXPECT_EQ(*ret, j++);
61 }
62 EXPECT_EQ(j, 2 * kBufferCapacity);
63 }
64
TEST(RingBufferTest,BufferAppendIterateTest)65 TEST(RingBufferTest, BufferAppendIterateTest) {
66 RingBuffer<int, kBufferCapacity> buffer;
67 for (int i = 0; i < 5 * kBufferCapacity; ++i) {
68 buffer.Append(i);
69 int j = std::max(0, i + 1 - kBufferCapacity);
70 // If i >= kBufferCapacity, check that the buffer contains only the last
71 // kBufferCapacity elements [i + 1 - kBufferCapacity, i]. Otherwise check
72 // that the buffer contains all elements from 0 to i.
73 for (auto it = buffer.begin(); it != buffer.end(); ++it) {
74 EXPECT_EQ(*it, j++);
75 }
76 // Check that j was incremented at each step which implies that all the
77 // required elements were present in the buffer.
78 EXPECT_EQ(j, i + 1);
79 }
80 buffer.Clear();
81 EXPECT_EQ(buffer.begin(), buffer.end());
82 }
83
84 } // namespace grpc_core
85
main(int argc,char ** argv)86 int main(int argc, char** argv) {
87 ::testing::InitGoogleTest(&argc, argv);
88 return RUN_ALL_TESTS();
89 }
90