1
0
mirror of https://github.com/mfontanini/libtins synced 2026-01-26 20:01:35 +01:00

Implemented operator< on both HWAddress and IPv4Address.

This commit is contained in:
Matias Fontanini
2012-10-10 21:14:25 -03:00
parent 3139020df2
commit 5b54cdefad
4 changed files with 49 additions and 0 deletions

View File

@@ -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.
*

View File

@@ -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.
*

View File

@@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include <algorithm>
#include <string>
#include <map>
#include <sstream>
#include <stdint.h>
#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<HWAddress<6>, 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);

View File

@@ -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");
}