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:
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user