From 5b54cdefad7ecb78264e5b9879abcfd945b3efe2 Mon Sep 17 00:00:00 2001 From: Matias Fontanini Date: Wed, 10 Oct 2012 21:14:25 -0300 Subject: [PATCH] Implemented operator< on both HWAddress and IPv4Address. --- include/hw_address.h | 11 +++++++++++ include/ip_address.h | 10 ++++++++++ tests/src/hwaddress.cpp | 14 ++++++++++++++ tests/src/ipaddress.cpp | 14 ++++++++++++++ 4 files changed, 49 insertions(+) diff --git a/include/hw_address.h b/include/hw_address.h index 3cc2bef..001ad1c 100644 --- a/include/hw_address.h +++ b/include/hw_address.h @@ -206,6 +206,17 @@ public: return !(*this == rhs); } + /** + * \brief Compares this HWAddress for less-than inequality. + * + * \param rhs The HWAddress to be compared to. + * + * \return bool indicating whether this address is less-than rhs. + */ + bool operator<(const HWAddress &rhs) const { + return std::lexicographical_compare(begin(), end(), rhs.begin(), rhs.end()); + } + /** * \brief Retrieves the size of this address. * diff --git a/include/ip_address.h b/include/ip_address.h index 68b815d..f45179a 100644 --- a/include/ip_address.h +++ b/include/ip_address.h @@ -110,6 +110,16 @@ namespace Tins { return !(*this == rhs); } + /** + * \brief Compare this IPv4Address for less-than inequality. + * + * \param rhs The address to be compared. + * \return bool indicating whether this address is less-than rhs. + */ + bool operator< (const IPv4Address &rhs) const { + return ip_addr < rhs.ip_addr; + } + /** * \brief Writes this address to a std::ostream. * diff --git a/tests/src/hwaddress.cpp b/tests/src/hwaddress.cpp index ad4966f..6166011 100644 --- a/tests/src/hwaddress.cpp +++ b/tests/src/hwaddress.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include "hw_address.h" @@ -35,6 +36,19 @@ TEST_F(HWAddressTest, DistinctOperator) { EXPECT_NE(addr1, addr2); } +TEST_F(HWAddressTest, LessThanOperator) { + HWAddress<6> addr1(byte_address), addr2(empty_addr); + HWAddress<6> bcast = "ff:ff:ff:ff:ff:ff"; + EXPECT_LT(addr2, addr1); + EXPECT_LT(addr2, bcast); + std::map, int> dict; + dict[addr1] = 12; + dict[addr2] = 15; + EXPECT_EQ(dict[addr1], 12); + EXPECT_EQ(dict[addr2], 15); +} + + TEST_F(HWAddressTest, CopyConstructor) { HWAddress<6> addr1(byte_address), addr2(addr1); EXPECT_EQ(addr1, addr2); diff --git a/tests/src/ipaddress.cpp b/tests/src/ipaddress.cpp index 6f780b4..d262264 100644 --- a/tests/src/ipaddress.cpp +++ b/tests/src/ipaddress.cpp @@ -35,3 +35,17 @@ TEST(IPAddressTest, OutputOperator) { oss << addr; EXPECT_EQ(oss.str(), ip_string); } + +TEST(IPAddressTest, EqualityOperator) { + IPv4Address addr1(ip_string), addr2(ip_string); + EXPECT_EQ(addr1, addr2); + EXPECT_NE(addr1, "127.0.0.1"); +} + +TEST(IPAddressTest, LessThanOperator) { + IPv4Address addr1(ip_string), addr2(ip_string); + EXPECT_FALSE(addr1 < addr2); + EXPECT_LT(addr1, "192.168.1.2"); + EXPECT_LT(addr1, "192.168.0.226"); + EXPECT_LT(addr1, "193.0.0.0"); +}