• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   bluetooth::common::TimestampedCircularBuffer<std::string> buffer(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   long long 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(10, 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