1 // Copyright (C) 2021 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include "RingBufferUtil.h"
18 
19 using namespace audio_proxy::service;
20 
21 using Buffer = std::vector<int8_t>;
22 
23 class RingBufferUtilTest : public testing::TestWithParam<
24                                std::tuple<Buffer, Buffer, Buffer, Buffer>> {};
25 
TEST_P(RingBufferUtilTest,DifferentBufferSize)26 TEST_P(RingBufferUtilTest, DifferentBufferSize) {
27   auto [src1, src2, expectedDst1, expectedDst2] = GetParam();
28 
29   Buffer dst1(expectedDst1.size());
30   Buffer dst2(expectedDst2.size());
31 
32   copyRingBuffer(dst1.data(), dst1.size(), dst2.data(), dst2.size(),
33                  src1.data(), src1.size(), src2.data(), src2.size());
34 
35   EXPECT_EQ(dst1, expectedDst1);
36   EXPECT_EQ(dst2, expectedDst2);
37 }
38 
39 // clang-format off
40 const std::vector<std::tuple<Buffer, Buffer, Buffer, Buffer>> testParams = {
41   // The layout are the same for src and dst.
42   {
43     {0, 1, 2, 3, 4},
44     {5, 6, 7, 8, 9},
45     {0, 1, 2, 3, 4},
46     {5, 6, 7, 8, 9}
47   },
48   // src1 size is samller than dst1 size.
49   {
50     {0, 1, 2, 3},
51     {4, 5, 6, 7, 8, 9},
52     {0, 1, 2, 3, 4},
53     {5, 6, 7, 8, 9}
54   },
55   // src2 size is larger than dst1 size.
56   {
57     {0, 1, 2, 3, 4, 5},
58     {6, 7, 8, 9},
59     {0, 1, 2, 3, 4},
60     {5, 6, 7, 8, 9}
61   },
62   // dst1 size is larger enough to hold all the src data.
63   {
64     {0, 1, 2, 3, 4},
65     {5, 6, 7, 8, 9},
66     {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0},
67     {0, 0, 0, 0, 0}
68   },
69   // Empty src
70   {{}, {}, {}, {}}
71 };
72 // clang-format off
73 
74 INSTANTIATE_TEST_SUITE_P(RingBufferUtilTestSuite, RingBufferUtilTest,
75                          testing::ValuesIn(testParams));
76 
TEST(RingBufferUtilTest,CopyNullptr)77 TEST(RingBufferUtilTest, CopyNullptr) {
78   // Test should not crash.
79   copyRingBuffer(nullptr, 0, nullptr, 0, nullptr, 0, nullptr, 0);
80 }
81