Browse Source

[2238] <= operator for IOAddress implemented.

Tomek Mrugalski 12 years ago
parent
commit
eda1249428
2 changed files with 29 additions and 2 deletions
  1. 23 2
      src/lib/asiolink/io_address.h
  2. 6 0
      src/lib/asiolink/tests/io_address_unittest.cc

+ 23 - 2
src/lib/asiolink/io_address.h

@@ -150,7 +150,7 @@ public:
     /// Operations within one protocol family are obvious.
     /// Comparisons between v4 and v6 will allways return v4
     /// being smaller. This follows boost::asio::ip implementation
-    bool smaller_than(const IOAddress& other) const {
+    bool smallerThan(const IOAddress& other) const {
         if (this->getFamily() < other.getFamily()) {
             return (true);
         }
@@ -164,13 +164,34 @@ public:
         }
     }
 
+    /// \brief Checks if one address is smaller or equal than the other
+    ///
+    /// \param other Address to compare against.
+    ///
+    /// \return true if this address is smaller than the other address.
+    bool smallerEqual(const IOAddress& other) const {
+        if (equals(other)) {
+            return (true);
+        }
+        return (smallerThan(other));
+    }
+
     /// \brief Checks if one address is smaller than the other
     ///
     /// \param other Address to compare against.
     ///
     /// See \ref smaller_than method for details.
     bool operator<(const IOAddress& other) const {
-        return (smaller_than(other));
+        return (smallerThan(other));
+    }
+
+    /// \brief Checks if one address is smaller or equal than the other
+    ///
+    /// \param other Address to compare against.
+    ///
+    /// See \ref smaller_equal method for details.
+    bool operator<=(const IOAddress& other) const {
+        return (smallerEqual(other));
     }
 
     /// \brief Compare addresses for inequality

+ 6 - 0
src/lib/asiolink/tests/io_address_unittest.cc

@@ -108,20 +108,26 @@ TEST(IOAddressTest, compare) {
     IOAddress addr4("::");
     IOAddress addr5("2001:db8::1");
     IOAddress addr6("2001:db8::1:0");
+    IOAddress addr7("2001:db8::1:0"); // the same as 6
 
     // v4 comparisons
     EXPECT_TRUE(addr1 < addr2);
     EXPECT_FALSE(addr2 < addr1);
+    EXPECT_FALSE(addr2 <= addr1);
     EXPECT_TRUE(addr3 < addr1);
     EXPECT_TRUE(addr3 < addr2);
+    EXPECT_TRUE(addr3 <= addr2);
 
     // v6 comparisons
     EXPECT_TRUE(addr4 < addr5);
     EXPECT_TRUE(addr5 < addr6);
     EXPECT_FALSE(addr6 < addr5);
+    EXPECT_FALSE(addr6 <= addr5);
 
     // v4 to v6 - v4 should always be smaller
     EXPECT_TRUE(addr1 < addr4);
     EXPECT_TRUE(addr3 < addr4);
     EXPECT_TRUE(addr2 < addr5);
+
+    EXPECT_TRUE(addr6 <= addr7);
 }