1 // Copyright 2023 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_bluetooth_sapphire/internal/host/common/expiring_set.h"
16
17 #include <pw_async/fake_dispatcher_fixture.h>
18
19 #include "pw_bluetooth_sapphire/internal/host/testing/test_helpers.h"
20 #include "pw_unit_test/framework.h"
21
22 namespace bt {
23
24 namespace {
25
26 using ExpiringSetTest = pw::async::test::FakeDispatcherFixture;
27
TEST_F(ExpiringSetTest,Expiration)28 TEST_F(ExpiringSetTest, Expiration) {
29 ExpiringSet<std::string> set(dispatcher());
30
31 set.add_until("Expired", now() - std::chrono::milliseconds(1));
32 EXPECT_FALSE(set.contains("Expired"));
33
34 set.add_until("Just a minute", now() + std::chrono::minutes(1));
35 set.add_until("Two minutes", now() + std::chrono::minutes(2));
36 EXPECT_TRUE(set.contains("Just a minute"));
37 EXPECT_TRUE(set.contains("Two minutes"));
38
39 RunFor(std::chrono::seconds(1));
40 EXPECT_TRUE(set.contains("Just a minute"));
41 EXPECT_TRUE(set.contains("Two minutes"));
42
43 RunFor(std::chrono::minutes(1));
44 EXPECT_FALSE(set.contains("Just a minute"));
45 EXPECT_TRUE(set.contains("Two minutes"));
46
47 RunFor(std::chrono::minutes(1));
48 EXPECT_FALSE(set.contains("Just a minute"));
49 EXPECT_FALSE(set.contains("Two minutes"));
50 }
51
TEST_F(ExpiringSetTest,Remove)52 TEST_F(ExpiringSetTest, Remove) {
53 ExpiringSet<std::string> set(dispatcher());
54
55 set.add_until("Expired", now() - std::chrono::milliseconds(1));
56 EXPECT_FALSE(set.contains("Expired"));
57 set.remove("Expired");
58
59 set.add_until("Temporary", now() + std::chrono::seconds(1000));
60 EXPECT_TRUE(set.contains("Temporary"));
61
62 set.remove("Temporary");
63
64 EXPECT_FALSE(set.contains("Temporary"));
65 }
66
67 } // namespace
68
69 } // namespace bt
70