fixed typo, one more unit-test
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Sun, 5 Sep 2004 17:18:40 +0000 (17:18 +0000)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Sun, 5 Sep 2004 17:18:40 +0000 (17:18 +0000)
src/ipfunc.cpp
test/ip_range.cpp

index 2d02e7d..a87e104 100644 (file)
@@ -290,13 +290,17 @@ vector<IP_RANGE> IP_RANGE::substract(const std::set<IP_RANGE> &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;
index cedd20b..94f05c9 100644 (file)
@@ -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<IP_RANGE> 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<IP_RANGE> new_range = range.substract(to_sub);
+            
+            vector<IP_RANGE> check_range;
+            check_range.push_back("192.168.1.61");
+            
+            vector<IP_RANGE>::const_iterator it, it_end = new_range.end();
+            for (it = new_range.begin(); it != it_end; it++) {
+                vector<IP_RANGE> cidrs = it->to_cidr();
+                vector<IP_RANGE>::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);