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