• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <algorithm>
18 #include <iterator>
19 #include <memory>
20 #include <vector>
21 
22 #include <gmock/gmock.h>
23 #include <gtest/gtest.h>
24 #include <input/RingBuffer.h>
25 
26 namespace android {
27 namespace {
28 
29 using ::testing::ElementsAre;
30 using ::testing::ElementsAreArray;
31 using ::testing::IsEmpty;
32 using ::testing::Not;
33 using ::testing::SizeIs;
34 
TEST(RingBufferTest,PushPop)35 TEST(RingBufferTest, PushPop) {
36     RingBuffer<int> buffer(/*capacity=*/3);
37 
38     buffer.pushBack(1);
39     buffer.pushBack(2);
40     buffer.pushBack(3);
41     EXPECT_THAT(buffer, ElementsAre(1, 2, 3));
42 
43     buffer.pushBack(4);
44     EXPECT_THAT(buffer, ElementsAre(2, 3, 4));
45 
46     buffer.pushFront(1);
47     EXPECT_THAT(buffer, ElementsAre(1, 2, 3));
48 
49     EXPECT_EQ(1, buffer.popFront());
50     EXPECT_THAT(buffer, ElementsAre(2, 3));
51 
52     buffer.pushBack(4);
53     EXPECT_THAT(buffer, ElementsAre(2, 3, 4));
54 
55     buffer.pushBack(5);
56     EXPECT_THAT(buffer, ElementsAre(3, 4, 5));
57 
58     EXPECT_EQ(5, buffer.popBack());
59     EXPECT_THAT(buffer, ElementsAre(3, 4));
60 
61     EXPECT_EQ(4, buffer.popBack());
62     EXPECT_THAT(buffer, ElementsAre(3));
63 
64     EXPECT_EQ(3, buffer.popBack());
65     EXPECT_THAT(buffer, ElementsAre());
66 
67     buffer.pushBack(1);
68     EXPECT_THAT(buffer, ElementsAre(1));
69 
70     EXPECT_EQ(1, buffer.popFront());
71     EXPECT_THAT(buffer, ElementsAre());
72 }
73 
TEST(RingBufferTest,ObjectType)74 TEST(RingBufferTest, ObjectType) {
75     RingBuffer<std::unique_ptr<int>> buffer(/*capacity=*/2);
76     buffer.pushBack(std::make_unique<int>(1));
77     buffer.pushBack(std::make_unique<int>(2));
78     buffer.pushBack(std::make_unique<int>(3));
79 
80     EXPECT_EQ(2, *buffer[0]);
81     EXPECT_EQ(3, *buffer[1]);
82 }
83 
TEST(RingBufferTest,ConstructConstantValue)84 TEST(RingBufferTest, ConstructConstantValue) {
85     RingBuffer<int> buffer(/*count=*/3, /*value=*/10);
86     EXPECT_THAT(buffer, ElementsAre(10, 10, 10));
87     EXPECT_EQ(3u, buffer.capacity());
88 }
89 
TEST(RingBufferTest,Assignment)90 TEST(RingBufferTest, Assignment) {
91     RingBuffer<int> a(/*capacity=*/2);
92     a.pushBack(1);
93     a.pushBack(2);
94 
95     RingBuffer<int> b(/*capacity=*/3);
96     b.pushBack(10);
97     b.pushBack(20);
98     b.pushBack(30);
99 
100     std::swap(a, b);
101     EXPECT_THAT(a, ElementsAre(10, 20, 30));
102     EXPECT_THAT(b, ElementsAre(1, 2));
103 
104     a = b;
105     EXPECT_THAT(a, ElementsAreArray(b));
106 
107     RingBuffer<int> c(b);
108     EXPECT_THAT(c, ElementsAreArray(b));
109 
110     RingBuffer<int> d(std::move(b));
111     EXPECT_EQ(0u, b.capacity());
112     EXPECT_THAT(b, ElementsAre());
113     EXPECT_THAT(d, ElementsAre(1, 2));
114 
115     b = std::move(d);
116     EXPECT_THAT(b, ElementsAre(1, 2));
117     EXPECT_THAT(d, ElementsAre());
118     EXPECT_EQ(0u, d.capacity());
119 }
120 
TEST(RingBufferTest,FrontBackAccess)121 TEST(RingBufferTest, FrontBackAccess) {
122     RingBuffer<int> buffer(/*capacity=*/2);
123     buffer.pushBack(1);
124     EXPECT_EQ(1, buffer.front());
125     EXPECT_EQ(1, buffer.back());
126 
127     buffer.pushFront(0);
128     EXPECT_EQ(0, buffer.front());
129     EXPECT_EQ(1, buffer.back());
130 
131     buffer.pushFront(-1);
132     EXPECT_EQ(-1, buffer.front());
133     EXPECT_EQ(0, buffer.back());
134 }
135 
TEST(RingBufferTest,Subscripting)136 TEST(RingBufferTest, Subscripting) {
137     RingBuffer<int> buffer(/*capacity=*/2);
138     buffer.pushBack(1);
139     EXPECT_EQ(1, buffer[0]);
140 
141     buffer.pushFront(0);
142     EXPECT_EQ(0, buffer[0]);
143     EXPECT_EQ(1, buffer[1]);
144 
145     buffer.pushFront(-1);
146     EXPECT_EQ(-1, buffer[0]);
147     EXPECT_EQ(0, buffer[1]);
148 }
149 
TEST(RingBufferTest,Iterator)150 TEST(RingBufferTest, Iterator) {
151     RingBuffer<int> buffer(/*capacity=*/3);
152     buffer.pushFront(2);
153     buffer.pushBack(3);
154 
155     auto begin = buffer.begin();
156     auto end = buffer.end();
157 
158     EXPECT_NE(begin, end);
159     EXPECT_LE(begin, end);
160     EXPECT_GT(end, begin);
161     EXPECT_EQ(end, begin + 2);
162     EXPECT_EQ(begin, end - 2);
163 
164     EXPECT_EQ(2, end - begin);
165     EXPECT_EQ(1, end - (begin + 1));
166 
167     EXPECT_EQ(2, *begin);
168     ++begin;
169     EXPECT_EQ(3, *begin);
170     --begin;
171     EXPECT_EQ(2, *begin);
172     begin += 1;
173     EXPECT_EQ(3, *begin);
174     begin += -1;
175     EXPECT_EQ(2, *begin);
176     begin -= -1;
177     EXPECT_EQ(3, *begin);
178 }
179 
TEST(RingBufferTest,Clear)180 TEST(RingBufferTest, Clear) {
181     RingBuffer<int> buffer(/*capacity=*/2);
182     EXPECT_THAT(buffer, ElementsAre());
183 
184     buffer.pushBack(1);
185     EXPECT_THAT(buffer, ElementsAre(1));
186 
187     buffer.clear();
188     EXPECT_THAT(buffer, ElementsAre());
189     EXPECT_THAT(buffer, SizeIs(0));
190     EXPECT_THAT(buffer, IsEmpty());
191 
192     buffer.pushFront(1);
193     EXPECT_THAT(buffer, ElementsAre(1));
194 }
195 
TEST(RingBufferTest,SizeAndIsEmpty)196 TEST(RingBufferTest, SizeAndIsEmpty) {
197     RingBuffer<int> buffer(/*capacity=*/2);
198     EXPECT_THAT(buffer, SizeIs(0));
199     EXPECT_THAT(buffer, IsEmpty());
200 
201     buffer.pushBack(1);
202     EXPECT_THAT(buffer, SizeIs(1));
203     EXPECT_THAT(buffer, Not(IsEmpty()));
204 
205     buffer.pushBack(2);
206     EXPECT_THAT(buffer, SizeIs(2));
207     EXPECT_THAT(buffer, Not(IsEmpty()));
208 
209     buffer.pushBack(3);
210     EXPECT_THAT(buffer, SizeIs(2));
211     EXPECT_THAT(buffer, Not(IsEmpty()));
212 
213     buffer.popFront();
214     EXPECT_THAT(buffer, SizeIs(1));
215     EXPECT_THAT(buffer, Not(IsEmpty()));
216 
217     buffer.popBack();
218     EXPECT_THAT(buffer, SizeIs(0));
219     EXPECT_THAT(buffer, IsEmpty());
220 }
221 
222 } // namespace
223 } // namespace android
224