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