Skip to content

Commit 3d880bc

Browse files
authored
Fix strict weak ordering of Address (#277)
1 parent 5a33e19 commit 3d880bc

2 files changed

Lines changed: 38 additions & 5 deletions

File tree

cpp-driver/gtests/src/unit/tests/test_address.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,37 @@ TEST(AddressUnitTest, Hash) {
163163
set.insert(Address("0.0.0.0", 9041));
164164
EXPECT_EQ(set.size(), 5u); // Added
165165
}
166+
167+
TEST(AddressUnitTest, StrictWeakOrder) {
168+
{ // Family
169+
Address a("localhost", 9042);
170+
Address b("127.0.0.1", 30002, "a");
171+
ASSERT_NE(a, b);
172+
ASSERT_TRUE(a < b);
173+
ASSERT_FALSE(b < a);
174+
}
175+
176+
{ // Port
177+
Address a("localhost", 9042, "b");
178+
Address b("localhost", 30002, "a");
179+
ASSERT_NE(a, b);
180+
ASSERT_TRUE(a < b);
181+
ASSERT_FALSE(b < a);
182+
}
183+
184+
{ // Server name
185+
Address a("127.0.0.2", 9042, "a");
186+
Address b("127.0.0.1", 9042, "b");
187+
ASSERT_NE(a, b);
188+
ASSERT_TRUE(a < b);
189+
ASSERT_FALSE(b < a);
190+
}
191+
192+
{ // Hostname or address
193+
Address a("127.0.0.1", 9042, "a");
194+
Address b("127.0.0.2", 9042, "a");
195+
ASSERT_NE(a, b);
196+
ASSERT_TRUE(a < b);
197+
ASSERT_FALSE(b < a);
198+
}
199+
}

cpp-driver/src/address.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,10 @@ bool Address::equals(const Address& other, bool with_port) const {
100100
}
101101

102102
bool Address::operator<(const Address& other) const {
103-
if (family_ < other.family_) return true;
104-
if (port_ < other.port_) return true;
105-
if (server_name_ < other.server_name_) return true;
106-
if (hostname_or_address_ < other.hostname_or_address_) return true;
107-
return false;
103+
if (family_ != other.family_) return family_ < other.family_;
104+
if (port_ != other.port_) return port_ < other.port_;
105+
if (server_name_ != other.server_name_) return server_name_ < other.server_name_;
106+
return hostname_or_address_ < other.hostname_or_address_;
108107
}
109108

110109
String Address::hostname_or_address() const {

0 commit comments

Comments
 (0)