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

Commit e8deba6c authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes Ic9f55d5c,I1412d795 into tm-qpr-dev

* changes:
  Fix LeDevice, ClassicDevice, Device, AdapterConfig less than operators
  Fix address_with_type inequality comparison operator<
parents 15e0f885 3d4cb19f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ class AddressWithType final {
  }

  bool operator<(const AddressWithType& rhs) const {
    return address_ < rhs.address_ && address_type_ < rhs.address_type_;
    return (address_ != rhs.address_) ? address_ < rhs.address_ : address_type_ < rhs.address_type_;
  }
  bool operator==(const AddressWithType& rhs) const {
    return address_ == rhs.address_ && address_type_ == rhs.address_type_;
+138 −3
Original line number Diff line number Diff line
@@ -16,12 +16,14 @@
 *
 ******************************************************************************/

#include <unordered_map>
#include "hci/address_with_type.h"

#include <gtest/gtest.h>

#include <map>
#include <unordered_map>

#include "hci/address.h"
#include "hci/address_with_type.h"
#include "hci/hci_packets.h"

namespace bluetooth {
@@ -97,5 +99,138 @@ TEST(AddressWithTypeTest, IsRpaThatMatchesIrk) {
  EXPECT_FALSE(address_2.IsRpaThatMatchesIrk(irk_1));
}

TEST(AddressWithTypeTest, OperatorLessThan) {
  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x50, 0x02, 0x03, 0xC9, 0x12, 0xDE}}, AddressType::RANDOM_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x50, 0x02, 0x03, 0xC9, 0x12, 0xDD}}, AddressType::RANDOM_DEVICE_ADDRESS);

    ASSERT_TRUE(address_2 < address_1);
  }

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x50, 0x02, 0x03, 0xC9, 0x12, 0xDE}}, AddressType::RANDOM_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x70, 0x02, 0x03, 0xC9, 0x12, 0xDE}}, AddressType::RANDOM_DEVICE_ADDRESS);

    ASSERT_TRUE(address_1 < address_2);
  }

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x50, 0x02, 0x03, 0xC9, 0x12, 0xDE}}, AddressType::RANDOM_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x70, 0x02, 0x03, 0xC9, 0x12, 0xDD}}, AddressType::RANDOM_DEVICE_ADDRESS);

    ASSERT_TRUE(address_1 < address_2);
  }

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::PUBLIC_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::RANDOM_DEVICE_ADDRESS);

    ASSERT_TRUE(address_1 < address_2);
  }

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::PUBLIC_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::PUBLIC_DEVICE_ADDRESS);

    ASSERT_FALSE(address_1 < address_2);
  }
}

TEST(AddressWithTypeTest, OrderedMap) {
  std::map<AddressWithType, int> map;

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x50, 0x02, 0x03, 0xC9, 0x12, 0xDE}}, AddressType::RANDOM_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x70, 0x02, 0x03, 0xC9, 0x12, 0xDD}}, AddressType::RANDOM_DEVICE_ADDRESS);

    map[address_1] = 1;
    map[address_2] = 2;

    ASSERT_EQ(2UL, map.size());
    map.clear();
  }

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::RANDOM_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::PUBLIC_DEVICE_ADDRESS);

    map[address_1] = 1;
    map[address_2] = 2;

    ASSERT_EQ(2UL, map.size());
    map.clear();
  }

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::PUBLIC_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::PUBLIC_DEVICE_ADDRESS);

    map[address_1] = 1;
    map[address_2] = 2;

    ASSERT_EQ(1UL, map.size());
    map.clear();
  }
}

TEST(AddressWithTypeTest, HashMap) {
  std::unordered_map<AddressWithType, int> map;

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x50, 0x02, 0x03, 0xC9, 0x12, 0xDE}}, AddressType::RANDOM_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x70, 0x02, 0x03, 0xC9, 0x12, 0xDD}}, AddressType::RANDOM_DEVICE_ADDRESS);

    map[address_1] = 1;
    map[address_2] = 2;

    ASSERT_EQ(2UL, map.size());
    map.clear();
  }

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::RANDOM_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::PUBLIC_DEVICE_ADDRESS);

    map[address_1] = 1;
    map[address_2] = 2;

    ASSERT_EQ(2UL, map.size());
    map.clear();
  }

  {
    AddressWithType address_1 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::PUBLIC_DEVICE_ADDRESS);
    AddressWithType address_2 =
        AddressWithType(Address{{0x11, 0x22, 0x33, 0x44, 0x55, 0x66}}, AddressType::PUBLIC_DEVICE_ADDRESS);

    map[address_1] = 1;
    map[address_2] = 2;

    ASSERT_EQ(1UL, map.size());
    map.clear();
  }
}

}  // namespace hci
}  // namespace bluetooth
+7 −1
Original line number Diff line number Diff line
@@ -45,7 +45,13 @@ class AdapterConfig {
    return !(*this == other);
  }
  bool operator<(const AdapterConfig& other) const {
    return config_ < other.config_ && memory_only_config_ < other.memory_only_config_ && section_ < other.section_;
    if (config_ != other.config_) {
      return config_ < other.config_;
    }
    if (memory_only_config_ != other.memory_only_config_) {
      return memory_only_config_ < other.memory_only_config_;
    }
    return section_ < other.section_;
  }
  bool operator>(const AdapterConfig& rhs) const {
    return (rhs < *this);
+96 −0
Original line number Diff line number Diff line
@@ -61,3 +61,99 @@ TEST(AdapterConfigTest, equality_test) {
  ASSERT_NE(adapter_config_1, adapter_config_3);
}

TEST(AdapterConfigTest, operator_less_than) {
  ConfigCache config1(10, Device::kLinkKeyProperties);
  ConfigCache config2(10, Device::kLinkKeyProperties);
  ASSERT_NE(&config1, &config2);
  ConfigCache* smaller_config_ptr = &config1;
  ConfigCache* larger_config_ptr = &config2;
  if (&config2 < &config1) {
    smaller_config_ptr = &config2;
    larger_config_ptr = &config1;
  }

  ConfigCache memory_only_config1(10, {});
  ConfigCache memory_only_config2(10, {});
  ASSERT_NE(&memory_only_config1, &memory_only_config2);
  ConfigCache* smaller_memory_only_config_ptr = &memory_only_config1;
  ConfigCache* larger_memory_only_config_ptr = &memory_only_config2;
  if (&memory_only_config2 < &memory_only_config1) {
    smaller_memory_only_config_ptr = &memory_only_config2;
    larger_memory_only_config_ptr = &memory_only_config1;
  }

  bluetooth::hci::Address smaller_address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}};
  bluetooth::hci::Address larger_address = {{0x01, 0x02, 0x03, 0x04, 0x05, 0x07}};

  {
    AdapterConfig adapter_config1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    AdapterConfig adapter_config2(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    AdapterConfig adapter_config2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ASSERT_FALSE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    AdapterConfig adapter_config2(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    AdapterConfig adapter_config2(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    AdapterConfig adapter_config2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ASSERT_FALSE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    AdapterConfig adapter_config2(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_FALSE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    AdapterConfig adapter_config2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    AdapterConfig adapter_config2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_FALSE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    AdapterConfig adapter_config2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    AdapterConfig adapter_config2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    AdapterConfig adapter_config2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(adapter_config1 < adapter_config2);
  }

  {
    AdapterConfig adapter_config1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    AdapterConfig adapter_config2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(adapter_config1 < adapter_config2);
  }
}
+7 −1
Original line number Diff line number Diff line
@@ -49,7 +49,13 @@ class ClassicDevice {
    return !(*this == other);
  }
  bool operator<(const ClassicDevice& other) const {
    return config_ < other.config_ && memory_only_config_ < other.memory_only_config_ && section_ < other.section_;
    if (config_ != other.config_) {
      return config_ < other.config_;
    }
    if (memory_only_config_ != other.memory_only_config_) {
      return memory_only_config_ < other.memory_only_config_;
    }
    return section_ < other.section_;
  }
  bool operator>(const ClassicDevice& rhs) const {
    return (rhs < *this);
Loading