1 /*
2  * Copyright (C) 2018, 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 <gmock/gmock.h>
18 
19 #include "ringbuffer.h"
20 
21 using testing::Return;
22 using testing::Test;
23 
24 namespace android {
25 namespace hardware {
26 namespace wifi {
27 namespace V1_5 {
28 namespace implementation {
29 
30 class RingbufferTest : public Test {
31    public:
32     const uint32_t maxBufferSize_ = 10;
33     Ringbuffer buffer_{maxBufferSize_};
34 };
35 
TEST_F(RingbufferTest,CreateEmptyBuffer)36 TEST_F(RingbufferTest, CreateEmptyBuffer) {
37     ASSERT_TRUE(buffer_.getData().empty());
38 }
39 
TEST_F(RingbufferTest,CanUseFullBufferCapacity)40 TEST_F(RingbufferTest, CanUseFullBufferCapacity) {
41     const std::vector<uint8_t> input(maxBufferSize_ / 2, '0');
42     const std::vector<uint8_t> input2(maxBufferSize_ / 2, '1');
43     buffer_.append(input);
44     buffer_.append(input2);
45     ASSERT_EQ(2u, buffer_.getData().size());
46     EXPECT_EQ(input, buffer_.getData().front());
47     EXPECT_EQ(input2, buffer_.getData().back());
48 }
49 
TEST_F(RingbufferTest,OldDataIsRemovedOnOverflow)50 TEST_F(RingbufferTest, OldDataIsRemovedOnOverflow) {
51     const std::vector<uint8_t> input(maxBufferSize_ / 2, '0');
52     const std::vector<uint8_t> input2(maxBufferSize_ / 2, '1');
53     const std::vector<uint8_t> input3 = {'G'};
54     buffer_.append(input);
55     buffer_.append(input2);
56     buffer_.append(input3);
57     ASSERT_EQ(2u, buffer_.getData().size());
58     EXPECT_EQ(input2, buffer_.getData().front());
59     EXPECT_EQ(input3, buffer_.getData().back());
60 }
61 
TEST_F(RingbufferTest,MultipleOldDataIsRemovedOnOverflow)62 TEST_F(RingbufferTest, MultipleOldDataIsRemovedOnOverflow) {
63     const std::vector<uint8_t> input(maxBufferSize_ / 2, '0');
64     const std::vector<uint8_t> input2(maxBufferSize_ / 2, '1');
65     const std::vector<uint8_t> input3(maxBufferSize_, '2');
66     buffer_.append(input);
67     buffer_.append(input2);
68     buffer_.append(input3);
69     ASSERT_EQ(1u, buffer_.getData().size());
70     EXPECT_EQ(input3, buffer_.getData().front());
71 }
72 
TEST_F(RingbufferTest,AppendingEmptyBufferDoesNotAddGarbage)73 TEST_F(RingbufferTest, AppendingEmptyBufferDoesNotAddGarbage) {
74     const std::vector<uint8_t> input = {};
75     buffer_.append(input);
76     ASSERT_TRUE(buffer_.getData().empty());
77 }
78 
TEST_F(RingbufferTest,OversizedAppendIsDropped)79 TEST_F(RingbufferTest, OversizedAppendIsDropped) {
80     const std::vector<uint8_t> input(maxBufferSize_ + 1, '0');
81     buffer_.append(input);
82     ASSERT_TRUE(buffer_.getData().empty());
83 }
84 
TEST_F(RingbufferTest,OversizedAppendDoesNotDropExistingData)85 TEST_F(RingbufferTest, OversizedAppendDoesNotDropExistingData) {
86     const std::vector<uint8_t> input(maxBufferSize_, '0');
87     const std::vector<uint8_t> input2(maxBufferSize_ + 1, '1');
88     buffer_.append(input);
89     buffer_.append(input2);
90     ASSERT_EQ(1u, buffer_.getData().size());
91     EXPECT_EQ(input, buffer_.getData().front());
92 }
93 }  // namespace implementation
94 }  // namespace V1_5
95 }  // namespace wifi
96 }  // namespace hardware
97 }  // namespace android
98