15899e8e9a13117fa73864a2e96ee6bdba048397fxshu/*
25899e8e9a13117fa73864a2e96ee6bdba048397fxshu * Copyright (C) 2018, The Android Open Source Project
35899e8e9a13117fa73864a2e96ee6bdba048397fxshu *
45899e8e9a13117fa73864a2e96ee6bdba048397fxshu * Licensed under the Apache License, Version 2.0 (the "License");
55899e8e9a13117fa73864a2e96ee6bdba048397fxshu * you may not use this file except in compliance with the License.
65899e8e9a13117fa73864a2e96ee6bdba048397fxshu * You may obtain a copy of the License at
75899e8e9a13117fa73864a2e96ee6bdba048397fxshu *
85899e8e9a13117fa73864a2e96ee6bdba048397fxshu *     http://www.apache.org/licenses/LICENSE-2.0
95899e8e9a13117fa73864a2e96ee6bdba048397fxshu *
105899e8e9a13117fa73864a2e96ee6bdba048397fxshu * Unless required by applicable law or agreed to in writing, software
115899e8e9a13117fa73864a2e96ee6bdba048397fxshu * distributed under the License is distributed on an "AS IS" BASIS,
125899e8e9a13117fa73864a2e96ee6bdba048397fxshu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135899e8e9a13117fa73864a2e96ee6bdba048397fxshu * See the License for the specific language governing permissions and
145899e8e9a13117fa73864a2e96ee6bdba048397fxshu * limitations under the License.
155899e8e9a13117fa73864a2e96ee6bdba048397fxshu */
165899e8e9a13117fa73864a2e96ee6bdba048397fxshu
175899e8e9a13117fa73864a2e96ee6bdba048397fxshu#include <gmock/gmock.h>
185899e8e9a13117fa73864a2e96ee6bdba048397fxshu
195899e8e9a13117fa73864a2e96ee6bdba048397fxshu#include "ringbuffer.h"
205899e8e9a13117fa73864a2e96ee6bdba048397fxshu
215899e8e9a13117fa73864a2e96ee6bdba048397fxshuusing testing::Return;
225899e8e9a13117fa73864a2e96ee6bdba048397fxshuusing testing::Test;
235899e8e9a13117fa73864a2e96ee6bdba048397fxshu
245899e8e9a13117fa73864a2e96ee6bdba048397fxshunamespace android {
255899e8e9a13117fa73864a2e96ee6bdba048397fxshunamespace hardware {
265899e8e9a13117fa73864a2e96ee6bdba048397fxshunamespace wifi {
275899e8e9a13117fa73864a2e96ee6bdba048397fxshunamespace V1_2 {
285899e8e9a13117fa73864a2e96ee6bdba048397fxshunamespace implementation {
295899e8e9a13117fa73864a2e96ee6bdba048397fxshu
305899e8e9a13117fa73864a2e96ee6bdba048397fxshuclass RingbufferTest : public Test {
315899e8e9a13117fa73864a2e96ee6bdba048397fxshu   public:
325899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const uint32_t maxBufferSize_ = 10;
335899e8e9a13117fa73864a2e96ee6bdba048397fxshu    Ringbuffer buffer_{maxBufferSize_};
345899e8e9a13117fa73864a2e96ee6bdba048397fxshu};
355899e8e9a13117fa73864a2e96ee6bdba048397fxshu
365899e8e9a13117fa73864a2e96ee6bdba048397fxshuTEST_F(RingbufferTest, CreateEmptyBuffer) {
375899e8e9a13117fa73864a2e96ee6bdba048397fxshu    ASSERT_TRUE(buffer_.getData().empty());
385899e8e9a13117fa73864a2e96ee6bdba048397fxshu}
395899e8e9a13117fa73864a2e96ee6bdba048397fxshu
405899e8e9a13117fa73864a2e96ee6bdba048397fxshuTEST_F(RingbufferTest, CanUseFullBufferCapacity) {
415899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input(maxBufferSize_ / 2, '0');
425899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input2(maxBufferSize_ / 2, '1');
435899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input);
445899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input2);
455899e8e9a13117fa73864a2e96ee6bdba048397fxshu    ASSERT_EQ(2u, buffer_.getData().size());
465899e8e9a13117fa73864a2e96ee6bdba048397fxshu    EXPECT_EQ(input, buffer_.getData().front());
475899e8e9a13117fa73864a2e96ee6bdba048397fxshu    EXPECT_EQ(input2, buffer_.getData().back());
485899e8e9a13117fa73864a2e96ee6bdba048397fxshu}
495899e8e9a13117fa73864a2e96ee6bdba048397fxshu
505899e8e9a13117fa73864a2e96ee6bdba048397fxshuTEST_F(RingbufferTest, OldDataIsRemovedOnOverflow) {
515899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input(maxBufferSize_ / 2, '0');
525899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input2(maxBufferSize_ / 2, '1');
535899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input3 = {'G'};
545899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input);
555899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input2);
565899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input3);
575899e8e9a13117fa73864a2e96ee6bdba048397fxshu    ASSERT_EQ(2u, buffer_.getData().size());
585899e8e9a13117fa73864a2e96ee6bdba048397fxshu    EXPECT_EQ(input2, buffer_.getData().front());
595899e8e9a13117fa73864a2e96ee6bdba048397fxshu    EXPECT_EQ(input3, buffer_.getData().back());
605899e8e9a13117fa73864a2e96ee6bdba048397fxshu}
615899e8e9a13117fa73864a2e96ee6bdba048397fxshu
625899e8e9a13117fa73864a2e96ee6bdba048397fxshuTEST_F(RingbufferTest, MultipleOldDataIsRemovedOnOverflow) {
635899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input(maxBufferSize_ / 2, '0');
645899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input2(maxBufferSize_ / 2, '1');
655899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input3(maxBufferSize_, '2');
665899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input);
675899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input2);
685899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input3);
695899e8e9a13117fa73864a2e96ee6bdba048397fxshu    ASSERT_EQ(1u, buffer_.getData().size());
705899e8e9a13117fa73864a2e96ee6bdba048397fxshu    EXPECT_EQ(input3, buffer_.getData().front());
715899e8e9a13117fa73864a2e96ee6bdba048397fxshu}
725899e8e9a13117fa73864a2e96ee6bdba048397fxshu
735899e8e9a13117fa73864a2e96ee6bdba048397fxshuTEST_F(RingbufferTest, AppendingEmptyBufferDoesNotAddGarbage) {
745899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input = {};
755899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input);
765899e8e9a13117fa73864a2e96ee6bdba048397fxshu    ASSERT_TRUE(buffer_.getData().empty());
775899e8e9a13117fa73864a2e96ee6bdba048397fxshu}
785899e8e9a13117fa73864a2e96ee6bdba048397fxshu
795899e8e9a13117fa73864a2e96ee6bdba048397fxshuTEST_F(RingbufferTest, OversizedAppendIsDropped) {
805899e8e9a13117fa73864a2e96ee6bdba048397fxshu    const std::vector<uint8_t> input(maxBufferSize_ + 1, '0');
815899e8e9a13117fa73864a2e96ee6bdba048397fxshu    buffer_.append(input);
825899e8e9a13117fa73864a2e96ee6bdba048397fxshu    ASSERT_TRUE(buffer_.getData().empty());
835899e8e9a13117fa73864a2e96ee6bdba048397fxshu}
844cb3316da4b2a92a7dfc9c555fc184ea0d29ca41xshu
854cb3316da4b2a92a7dfc9c555fc184ea0d29ca41xshuTEST_F(RingbufferTest, OversizedAppendDoesNotDropExistingData) {
864cb3316da4b2a92a7dfc9c555fc184ea0d29ca41xshu    const std::vector<uint8_t> input(maxBufferSize_, '0');
874cb3316da4b2a92a7dfc9c555fc184ea0d29ca41xshu    const std::vector<uint8_t> input2(maxBufferSize_ + 1, '1');
884cb3316da4b2a92a7dfc9c555fc184ea0d29ca41xshu    buffer_.append(input);
894cb3316da4b2a92a7dfc9c555fc184ea0d29ca41xshu    buffer_.append(input2);
904cb3316da4b2a92a7dfc9c555fc184ea0d29ca41xshu    ASSERT_EQ(1u, buffer_.getData().size());
914cb3316da4b2a92a7dfc9c555fc184ea0d29ca41xshu    EXPECT_EQ(input, buffer_.getData().front());
924cb3316da4b2a92a7dfc9c555fc184ea0d29ca41xshu}
935899e8e9a13117fa73864a2e96ee6bdba048397fxshu}  // namespace implementation
945899e8e9a13117fa73864a2e96ee6bdba048397fxshu}  // namespace V1_2
955899e8e9a13117fa73864a2e96ee6bdba048397fxshu}  // namespace wifi
965899e8e9a13117fa73864a2e96ee6bdba048397fxshu}  // namespace hardware
975899e8e9a13117fa73864a2e96ee6bdba048397fxshu}  // namespace android
98