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

Commit 3d4cb19f authored by Jack He's avatar Jack He
Browse files

Fix LeDevice, ClassicDevice, Device, AdapterConfig less than operators

If a class has two members A and B, and we want to order the class by A
first and then by B:
  obj1.A < obj2.A && obj1.B < obj2.B
is the wrong alrogithm. Instead, something like
  if (obj1.A != obj1.A) {
    return obj1.A < obj2.A;
  }
  return obj1.B < obj2.B;
should be used.

An incorrectly implemented less than operator will affect std::map's
functionality as std::map thinks that
  if (!(obj1 < obj2) && !(obj2 < obj1)) ==> obj1 == obj2

Bug: 242054540
Test: gd/cert/run
Tag: #refactor
BYPASS_LONG_LINES_REASON: Bluetooth likes 120 lines

Merged-In: Ic9f55d5c776a935c49ca889c46e667bc7b961b4f
Change-Id: Ic9f55d5c776a935c49ca889c46e667bc7b961b4f
(cherry picked from commit b5044d0e493109cea7163a47a27f2bb28fe4df79)
parent a0e5e448
Loading
Loading
Loading
Loading
+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);
+96 −0
Original line number Diff line number Diff line
@@ -65,3 +65,99 @@ TEST(ClassicDeviceTest, equality_test) {
  ASSERT_NE(device1, device3);
}

TEST(ClassicDeviceTest, 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}};

  {
    ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ClassicDevice device2(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    ClassicDevice device1(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ClassicDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ASSERT_FALSE(device1 < device2);
  }

  {
    ClassicDevice device1(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ClassicDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ClassicDevice device2(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    ClassicDevice device1(larger_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ClassicDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ASSERT_FALSE(device1 < device2);
  }

  {
    ClassicDevice device1(larger_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ClassicDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_FALSE(device1 < device2);
  }

  {
    ClassicDevice device1(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ClassicDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    ClassicDevice device1(larger_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ClassicDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_FALSE(device1 < device2);
  }

  {
    ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ClassicDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ClassicDevice device2(smaller_config_ptr, smaller_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ClassicDevice device2(larger_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }

  {
    ClassicDevice device1(smaller_config_ptr, smaller_memory_only_config_ptr, smaller_address.ToString());
    ClassicDevice device2(smaller_config_ptr, larger_memory_only_config_ptr, larger_address.ToString());
    ASSERT_TRUE(device1 < device2);
  }
}
+7 −1
Original line number Diff line number Diff line
@@ -132,7 +132,13 @@ class Device {
    return !(*this == other);
  }
  bool operator<(const Device& 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 Device& rhs) const {
    return (rhs < *this);
Loading