Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 86e774bc authored by Jack He's avatar Jack He Committed by android-build-merger
Browse files

Address: Add a hash function for Bluetooth MAC address am: bc6014bd am: 3a1e2c1c

am: 0c855e9b

Change-Id: I10b68096318c9760263d2edc3529e546dc14b7af
parents baa5e5f0 0c855e9b
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -79,3 +79,14 @@ inline std::ostream& operator<<(std::ostream& os, const Address& a) {

}  // namespace common
}  // namespace bluetooth

namespace std {
template <>
struct hash<bluetooth::common::Address> {
  std::size_t operator()(const bluetooth::common::Address& val) const {
    uint64_t int_addr = 0;
    memcpy(reinterpret_cast<uint8_t*>(&int_addr), val.address, bluetooth::common::Address::kLength);
    return std::hash<uint64_t>{}(int_addr);
  }
};
}  // namespace std
 No newline at end of file
+33 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@
 *
 ******************************************************************************/

#include <unordered_map>

#include <gtest/gtest.h>

#include "common/address.h"
@@ -197,3 +199,34 @@ TEST(AddressTest, BdAddrFromStringToStringEquivalent) {
  EXPECT_TRUE(Address::FromString(address, addr));
  EXPECT_EQ(addr.ToString(), address);
}

TEST(AddressTest, BdAddrSameValueSameOrder) {
  Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  Address addr2{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  // Test if two addresses with same byte value have the same hash
  struct std::hash<bluetooth::common::Address> hasher;
  EXPECT_EQ(hasher(addr1), hasher(addr2));
  // Test if two addresses with the same hash and the same value, they will
  // still map to the same value
  std::unordered_map<Address, int> data = {};
  data[addr1] = 5;
  data[addr2] = 8;
  EXPECT_EQ(data[addr1], data[addr2]);
}

TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesZeroAddr) {
  Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  struct std::hash<Address> hasher;
  EXPECT_NE(hasher(addr1), hasher(Address::kEmpty));
}

TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesFullAddr) {
  Address addr1{{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  struct std::hash<Address> hasher;
  EXPECT_NE(hasher(addr1), hasher(Address::kAny));
}

TEST(AddressTest, BdAddrHashDifferentForDifferentAddressesZeroAndFullAddr) {
  struct std::hash<Address> hasher;
  EXPECT_NE(hasher(Address::kEmpty), hasher(Address::kAny));
}