From 9f9289e1bdc096fdd147c18484de93af7a7bf39c Mon Sep 17 00:00:00 2001 From: Thomas Jarosch Date: Sun, 5 Sep 2004 17:18:40 +0000 Subject: [PATCH] fixed typo, one more unit-test --- src/ipfunc.cpp | 12 ++++++++---- test/ip_range.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/ipfunc.cpp b/src/ipfunc.cpp index 2d02e7d..a87e104 100644 --- a/src/ipfunc.cpp +++ b/src/ipfunc.cpp @@ -290,13 +290,17 @@ vector IP_RANGE::substract(const std::set &to_substract) con if (!sub->is_within(*this)) throw runtime_error("IP_RANGE::substract error: "+ sub->to_string() + " is not within " + to_string()); - // Get beginn of network/range/IP + // Get begin of network/range/IP unsigned int sub_begin = turn_ip(sub->get_base()); // Got a fragment? - if (ip_pos < sub_begin) - rtn.push_back(IP_RANGE(ip_type::RANGE, turn_ip(ip_pos), turn_ip(sub_begin-1))); - + if (ip_pos < sub_begin) { + if (ip_pos == sub_begin-1) + rtn.push_back(IP_RANGE(ip_type::IP, turn_ip(ip_pos))); + else + rtn.push_back(IP_RANGE(ip_type::RANGE, turn_ip(ip_pos), turn_ip(sub_begin-1))); + } + // Set position to end if end is higher than current position if (ip_pos < turn_ip(sub->get_broadcast())+1) ip_pos = turn_ip(sub->get_broadcast())+1; diff --git a/test/ip_range.cpp b/test/ip_range.cpp index cedd20b..94f05c9 100644 --- a/test/ip_range.cpp +++ b/test/ip_range.cpp @@ -115,6 +115,7 @@ class ip_range : public TestFixture CPPUNIT_TEST(Substract12); CPPUNIT_TEST(Substract13); CPPUNIT_TEST(Substract14); + CPPUNIT_TEST(Substract15); CPPUNIT_TEST_SUITE_END(); @@ -818,6 +819,29 @@ class ip_range : public TestFixture CPPUNIT_ASSERT_EQUAL(check_range,new_range); } + + void Substract15() + { + IP_RANGE range("192.168.1.1-192.168.1.100"); + set to_sub; + to_sub.insert(IP_RANGE("192.168.1.1-192.168.1.60")); + to_sub.insert(IP_RANGE("192.168.1.62-192.168.1.100")); + + vector new_range = range.substract(to_sub); + + vector check_range; + check_range.push_back("192.168.1.61"); + + vector::const_iterator it, it_end = new_range.end(); + for (it = new_range.begin(); it != it_end; it++) { + vector cidrs = it->to_cidr(); + vector::const_iterator cidr, cidrs_end = cidrs.end(); + for (cidr = cidrs.begin(); cidr != cidrs_end; cidr++) + cerr << "Range as CIDR: " << cidr->to_string() << endl; + } + + CPPUNIT_ASSERT_EQUAL(check_range,new_range); + } }; CPPUNIT_TEST_SUITE_REGISTRATION(ip_range); -- 1.7.1