1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
/*
* Copyright 2020 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <limits>
#include <string>
#include "common/circular_buffer.h"
#include "os/log.h"
namespace testing {
long long timestamp_{0};
struct TestTimestamper : public bluetooth::common::Timestamper {
virtual long long GetTimestamp() const override {
return timestamp_++;
}
};
TEST(CircularBufferTest, simple) {
bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10);
buffer.Push(std::string("One"));
buffer.Push(std::string("Two"));
buffer.Push(std::string("Three"));
auto vec = buffer.Pull();
ASSERT_STREQ("One", vec[0].entry.c_str());
ASSERT_STREQ("Two", vec[1].entry.c_str());
ASSERT_STREQ("Three", vec[2].entry.c_str());
auto vec2 = buffer.Pull();
ASSERT_FALSE(vec2.empty());
}
TEST(CircularBufferTest, simple_drain) {
bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10);
buffer.Push(std::string("One"));
buffer.Push(std::string("Two"));
buffer.Push(std::string("Three"));
auto vec = buffer.Drain();
ASSERT_STREQ("One", vec[0].entry.c_str());
ASSERT_STREQ("Two", vec[1].entry.c_str());
ASSERT_STREQ("Three", vec[2].entry.c_str());
auto vec2 = buffer.Pull();
ASSERT_TRUE(vec2.empty());
}
TEST(CircularBufferTest, test_timestamps) {
bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10, std::make_unique<TestTimestamper>());
buffer.Push(std::string("One"));
buffer.Push(std::string("Two"));
buffer.Push(std::string("Three"));
auto vec = buffer.Pull();
long long timestamp = 0;
for (auto v : vec) {
ASSERT_EQ(timestamp, v.timestamp);
timestamp++;
}
}
TEST(CircularBufferTest, max_timestamps) {
bluetooth::common::TimestampedCircularBuffer<std::string> buffer(10);
std::vector<std::string> test_data;
for (int i = 0; i < 10 + 1; i++) {
char buf[255];
snprintf(buf, 255, "value:%d", i);
test_data.push_back(std::string(buf));
buffer.Push(std::string(buf));
}
auto vec = buffer.Pull();
ASSERT_EQ(10, vec.size());
int i = 0 + 1;
for (auto v : vec) {
ASSERT_EQ(test_data[i], v.entry.c_str());
i++;
}
}
} // namespace testing
|