1 /*
2  * Copyright 2020 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 "common/circular_buffer.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 
22 #include <limits>
23 #include <string>
24 
25 namespace testing {
26 
27 uint64_t timestamp_{0};
28 struct TestTimestamper : public bluetooth::common::Timestamper {
GetTimestamptesting::TestTimestamper29   virtual uint64_t GetTimestamp() const override { return timestamp_++; }
30 };
31 
TEST(CircularBufferTest,simple)32 TEST(CircularBufferTest, simple) {
33   bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10);
34 
35   buffer.Push(std::string("One"));
36   buffer.Push(std::string("Two"));
37   buffer.Push(std::string("Three"));
38 
39   auto vec = buffer.Pull();
40 
41   ASSERT_STREQ("One", vec[0].entry.c_str());
42   ASSERT_STREQ("Two", vec[1].entry.c_str());
43   ASSERT_STREQ("Three", vec[2].entry.c_str());
44 
45   auto vec2 = buffer.Pull();
46 
47   ASSERT_FALSE(vec2.empty());
48 }
49 
TEST(CircularBufferTest,simple_drain)50 TEST(CircularBufferTest, simple_drain) {
51   bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10);
52 
53   buffer.Push(std::string("One"));
54   buffer.Push(std::string("Two"));
55   buffer.Push(std::string("Three"));
56 
57   auto vec = buffer.Drain();
58 
59   ASSERT_STREQ("One", vec[0].entry.c_str());
60   ASSERT_STREQ("Two", vec[1].entry.c_str());
61   ASSERT_STREQ("Three", vec[2].entry.c_str());
62 
63   auto vec2 = buffer.Pull();
64 
65   ASSERT_TRUE(vec2.empty());
66 }
67 
TEST(CircularBufferTest,test_timestamps)68 TEST(CircularBufferTest, test_timestamps) {
69   timestamp_ = 0;
70   bluetooth::common::TimestampedCircularBuffer<std::string> buffer(
71           10, std::make_unique<TestTimestamper>());
72 
73   buffer.Push(std::string("One"));
74   buffer.Push(std::string("Two"));
75   buffer.Push(std::string("Three"));
76 
77   auto vec = buffer.Pull();
78   uint64_t timestamp = 0;
79   for (auto v : vec) {
80     ASSERT_EQ(timestamp, v.timestamp);
81     timestamp++;
82   }
83 }
84 
TEST(CircularBufferTest,max_timestamps)85 TEST(CircularBufferTest, max_timestamps) {
86   bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10);
87 
88   std::vector<std::string> test_data;
89   for (int i = 0; i < 10 + 1; i++) {
90     char buf[255];
91     snprintf(buf, 255, "value:%d", i);
92     test_data.push_back(std::string(buf));
93     buffer.Push(std::string(buf));
94   }
95 
96   auto vec = buffer.Pull();
97   ASSERT_EQ(10ul, vec.size());
98 
99   int i = 0 + 1;
100   for (auto v : vec) {
101     ASSERT_EQ(test_data[i], v.entry.c_str());
102     i++;
103   }
104 }
105 
106 }  // namespace testing
107