xref: /aosp_15_r20/external/pigweed/pw_bluetooth_sapphire/host/common/identifier_test.cc (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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/identifier.h"
16 
17 #include <unordered_set>
18 
19 #include "pw_unit_test/framework.h"
20 
21 namespace bt {
22 namespace {
23 
24 constexpr Identifier<int> id1(1);
25 constexpr Identifier<int> id2(1);
26 constexpr Identifier<int> id3(2);
27 
TEST(IdentifierTest,Equality)28 TEST(IdentifierTest, Equality) {
29   EXPECT_EQ(id1, id2);
30   EXPECT_NE(id2, id3);
31   EXPECT_NE(id1, id3);
32 }
33 
TEST(IdentifierTest,Hash)34 TEST(IdentifierTest, Hash) {
35   std::unordered_set<Identifier<int>> ids;
36   EXPECT_EQ(0u, ids.count(id1));
37   EXPECT_EQ(0u, ids.size());
38 
39   ids.insert(id1);
40   EXPECT_EQ(1u, ids.count(id1));
41   EXPECT_EQ(1u, ids.size());
42 
43   ids.insert(id1);
44   EXPECT_EQ(1u, ids.count(id1));
45   EXPECT_EQ(1u, ids.size());
46 
47   ids.insert(id2);
48   EXPECT_EQ(1u, ids.count(id1));
49   EXPECT_EQ(1u, ids.size());
50 
51   ids.insert(id3);
52   EXPECT_EQ(1u, ids.count(id2));
53   EXPECT_EQ(2u, ids.size());
54 
55   ids.insert(id3);
56   EXPECT_EQ(1u, ids.count(id2));
57   EXPECT_EQ(2u, ids.size());
58 }
59 
TEST(IdentifierTest,PeerIdIsValid)60 TEST(IdentifierTest, PeerIdIsValid) {
61   {
62     PeerId id;
63     EXPECT_FALSE(id.IsValid());
64   }
65 
66   {
67     PeerId id(1);
68     EXPECT_TRUE(id.IsValid());
69   }
70 }
71 
72 }  // namespace
73 }  // namespace bt
74