Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(693)

Side by Side Diff: ipaddr_test.py

Issue 67107: collapse_address_list is too slow Base URL: http://ipaddr-py.googlecode.com/svn/trunk/
Patch Set: Treat a list of /32 or /128 Networks as IP addresses so collapse_address_list can summarize them. Created 14 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« ipaddr.py ('K') | « ipaddr.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # 2 #
3 # Copyright 2007 Google Inc. 3 # Copyright 2007 Google Inc.
4 # Licensed to PSF under a Contributor Agreement. 4 # Licensed to PSF under a Contributor Agreement.
5 # 5 #
6 # Licensed under the Apache License, Version 2.0 (the "License"); 6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License. 7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at 8 # You may obtain a copy of the License at
9 # 9 #
10 # http://www.apache.org/licenses/LICENSE-2.0 10 # http://www.apache.org/licenses/LICENSE-2.0
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 '1.2.3.4/32') 352 '1.2.3.4/32')
353 353
354 def testSlash128Constructor(self): 354 def testSlash128Constructor(self):
355 self.assertEquals(str(ipaddr.IPv6('::1/128')), 355 self.assertEquals(str(ipaddr.IPv6('::1/128')),
356 '::1/128') 356 '::1/128')
357 357
358 def testSlash0Constructor(self): 358 def testSlash0Constructor(self):
359 self.assertEquals(str(ipaddr.IPv4('1.2.3.4/0.0.0.0')), '1.2.3.4/0') 359 self.assertEquals(str(ipaddr.IPv4('1.2.3.4/0.0.0.0')), '1.2.3.4/0')
360 360
361 def testCollapsing(self): 361 def testCollapsing(self):
362 # test only IP addresses including some duplicates
363 ip1 = ipaddr.IPv4('1.1.1.0', host=True)
364 ip2 = ipaddr.IPv4('1.1.1.1', host=True)
365 ip3 = ipaddr.IPv4('1.1.1.2', host=True)
366 ip4 = ipaddr.IPv4('1.1.1.3', host=True)
367 ip5 = ipaddr.IPv4('1.1.1.4', host=True)
368 ip6 = ipaddr.IPv4('1.1.1.0', host=True)
369 # check that addreses are subsumed properly.
370 collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3, ip4, ip5, ip6])
371 self.assertEqual(collapsed, [ipaddr.IPv4('1.1.1.0/30'),
372 ipaddr.IPv4('1.1.1.4/32')])
373
374 # test a mix of IP addresses and networks including some duplicates
375 ip1 = ipaddr.IPv4('1.1.1.0', host=True)
376 ip2 = ipaddr.IPv4('1.1.1.1', host=True)
377 ip3 = ipaddr.IPv4('1.1.1.2', host=True)
378 ip4 = ipaddr.IPv4('1.1.1.3', host=True)
379 ip5 = ipaddr.IPv4('1.1.1.4/30')
380 ip6 = ipaddr.IPv4('1.1.1.4/30')
381 # check that addreses are subsumed properly.
382 collapsed = ipaddr.collapse_address_list([ip5, ip1, ip2, ip3, ip4, ip6])
383 self.assertEqual(collapsed, [ipaddr.IPv4('1.1.1.0/29')])
384
385 # test only IP networks
362 ip1 = ipaddr.IPv4('1.1.0.0/24') 386 ip1 = ipaddr.IPv4('1.1.0.0/24')
363 ip2 = ipaddr.IPv4('1.1.1.0/24') 387 ip2 = ipaddr.IPv4('1.1.1.0/24')
364 ip3 = ipaddr.IPv4('1.1.2.0/24') 388 ip3 = ipaddr.IPv4('1.1.2.0/24')
365 ip4 = ipaddr.IPv4('1.1.3.0/24') 389 ip4 = ipaddr.IPv4('1.1.3.0/24')
366 ip5 = ipaddr.IPv4('1.1.4.0/24') 390 ip5 = ipaddr.IPv4('1.1.4.0/24')
367 # stored in no particular order b/c we want CollapseAddr to call [].sort 391 # stored in no particular order b/c we want CollapseAddr to call [].sort
368 ip6 = ipaddr.IPv4('1.1.0.0/22') 392 ip6 = ipaddr.IPv4('1.1.0.0/22')
369 # check that addreses are subsumed properlly. 393 # check that addreses are subsumed properly.
370 collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3, ip4, ip5, ip6]) 394 collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3, ip4, ip5, ip6])
371 self.assertEqual(collapsed, [ipaddr.IPv4('1.1.0.0/22'), 395 self.assertEqual(collapsed, [ipaddr.IPv4('1.1.0.0/22'),
372 ipaddr.IPv4('1.1.4.0/24')]) 396 ipaddr.IPv4('1.1.4.0/24')])
373 397
374 # test that two addresses are supernet'ed properlly 398 # test that two addresses are supernet'ed properly
375 collapsed = ipaddr.collapse_address_list([ip1, ip2]) 399 collapsed = ipaddr.collapse_address_list([ip1, ip2])
376 self.assertEqual(collapsed, [ipaddr.IPv4('1.1.0.0/23')]) 400 self.assertEqual(collapsed, [ipaddr.IPv4('1.1.0.0/23')])
377 401
402 # test same IP networks
378 ip_same1 = ip_same2 = ipaddr.IPv4('1.1.1.1/32') 403 ip_same1 = ip_same2 = ipaddr.IPv4('1.1.1.1/32')
379 self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]), 404 self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]),
380 [ip_same1]) 405 [ip_same1])
406
407 # test same IP addresses
408 ip_same1 = ip_same2 = ipaddr.IPv4('1.1.1.1', host=True)
409 self.assertEqual(ipaddr.collapse_address_list([ip_same1, ip_same2]),
410 [ip_same1])
381 ip1 = ipaddr.IPv6('::2001:1/100') 411 ip1 = ipaddr.IPv6('::2001:1/100')
382 ip2 = ipaddr.IPv6('::2002:1/120') 412 ip2 = ipaddr.IPv6('::2002:1/120')
383 ip3 = ipaddr.IPv6('::2001:1/96') 413 ip3 = ipaddr.IPv6('::2001:1/96')
384 # test that ipv6 addresses are subsumed properly. 414 # test that ipv6 addresses are subsumed properly.
385 collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3]) 415 collapsed = ipaddr.collapse_address_list([ip1, ip2, ip3])
386 self.assertEqual(collapsed, [ip3]) 416 self.assertEqual(collapsed, [ip3])
387 417
418 def testSummarizing(self):
419 ip = ipaddr.IP
420 summarize = ipaddr.summarize_address_range
421 ip1 = ip('1.1.1.0', host=True)
422 ip2 = ip('1.1.1.255', host=True)
423 # test a /24 is sumamrized properly
424 self.assertEqual(summarize(ip1, ip2)[0], ip('1.1.1.0/24'))
425 # test an IPv4 range that isn't on a network byte boundary
426 ip2 = ip('1.1.1.8', host=True)
427 self.assertEqual(summarize(ip1, ip2), [ip('1.1.1.0/29'), ip('1.1.1.8')])
428
429 ip1 = ip('1::', host=True)
430 ip2 = ip('1:ffff:ffff:ffff:ffff:ffff:ffff:ffff', host=True)
431 # test a IPv6 is sumamrized properly
432 self.assertEqual(summarize(ip1, ip2)[0], ip('1::/16'))
433 # test an IPv6 range that isn't on a network byte boundary
434 ip2 = ip('2::', host=True)
435 self.assertEqual(summarize(ip1, ip2), [ip('1::/16'), ip('2::/128')])
436
437 # test exception raised when first is greater than last
438 self.assertRaises(ValueError, summarize, ip('1.1.1.0', host=True),
439 ip('1.1.0.0', host=True))
440 # test exception raised when first and last aren't IP addresses
441 self.assertRaises(ipaddr.IPTypeError, summarize, ip('1.1.1.0'),
442 ip('1.1.0.0'))
443 self.assertRaises(ipaddr.IPTypeError, summarize,
444 ip('1.1.1.0', host=True), ip('1.1.0.0'))
445 # test exception raised when first and last are not same version
446 self.assertRaises(ipaddr.IPTypeError, summarize, ip('::', host=True),
447 ip('1.1.0.0', host=True))
448
388 def testNetworkComparison(self): 449 def testNetworkComparison(self):
389 # ip1 and ip2 have the same network address 450 # ip1 and ip2 have the same network address
390 ip1 = ipaddr.IPv4('1.1.1.0/24') 451 ip1 = ipaddr.IPv4('1.1.1.0/24')
391 ip2 = ipaddr.IPv4('1.1.1.1/24') 452 ip2 = ipaddr.IPv4('1.1.1.1/24')
392 ip3 = ipaddr.IPv4('1.1.2.0/24') 453 ip3 = ipaddr.IPv4('1.1.2.0/24')
393 454
394 self.assertTrue(ip1 < ip3) 455 self.assertTrue(ip1 < ip3)
395 self.assertTrue(ip3 > ip2) 456 self.assertTrue(ip3 > ip2)
396 457
397 self.assertEquals(ip1.compare_networks(ip2), 0) 458 self.assertEquals(ip1.compare_networks(ip2), 0)
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
529 addr2 = ipaddr.IP('10.1.1.0/26') 590 addr2 = ipaddr.IP('10.1.1.0/26')
530 addr3 = ipaddr.IP('10.2.1.0/24') 591 addr3 = ipaddr.IP('10.2.1.0/24')
531 self.assertEqual(addr1.address_exclude(addr2), 592 self.assertEqual(addr1.address_exclude(addr2),
532 [ipaddr.IP('10.1.1.64/26'), 593 [ipaddr.IP('10.1.1.64/26'),
533 ipaddr.IP('10.1.1.128/25')]) 594 ipaddr.IP('10.1.1.128/25')])
534 self.assertRaises(ValueError, addr1.address_exclude, addr3) 595 self.assertRaises(ValueError, addr1.address_exclude, addr3)
535 596
536 def testHash(self): 597 def testHash(self):
537 self.assertEquals(hash(ipaddr.IP('10.1.1.0/24')), 598 self.assertEquals(hash(ipaddr.IP('10.1.1.0/24')),
538 hash(ipaddr.IP('10.1.1.0/24'))) 599 hash(ipaddr.IP('10.1.1.0/24')))
600 self.assertEquals(hash(ipaddr.IP('10.1.1.0', host=True)),
601 hash(ipaddr.IP('10.1.1.0', host=True)))
Peter Moody 2009/08/20 18:21:17 can you verify that your diff is against branches/
Matthew Flanagan 2009/08/21 00:55:57 These are new tests because I added __hash__() to
602 ip1 = ipaddr.IP('10.1.1.0', host=True)
603 ip2 = ipaddr.IP('1::', host=True)
539 dummy = {} 604 dummy = {}
540 dummy[self.ipv4] = None 605 dummy[self.ipv4] = None
541 dummy[self.ipv6] = None 606 dummy[self.ipv6] = None
607 dummy[ip1] = None
608 dummy[ip2] = None
542 self.assertTrue(self.ipv4 in dummy) 609 self.assertTrue(self.ipv4 in dummy)
610 self.assertTrue(ip2 in dummy)
543 611
544 def testIPv4PrefixFromInt(self): 612 def testIPv4PrefixFromInt(self):
545 addr1 = ipaddr.IP('10.1.1.0/24') 613 addr1 = ipaddr.IP('10.1.1.0/24')
546 addr2 = ipaddr.IPv4(int(addr1.ip)) # clone prefix 614 addr2 = ipaddr.IPv4(int(addr1.ip)) # clone prefix
547 addr2.set_prefix(addr1.prefixlen) 615 addr2.set_prefix(addr1.prefixlen)
548 addr3 = ipaddr.IP(123456) 616 addr3 = ipaddr.IP(123456)
549 617
550 self.assertEqual(123456, int(addr3.ip)) 618 self.assertEqual(123456, int(addr3.ip))
551 self.assertRaises(ipaddr.IPv4NetmaskValidationError, 619 self.assertRaises(ipaddr.IPv4NetmaskValidationError,
552 addr2.set_prefix, -1L) 620 addr2.set_prefix, -1L)
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
709 ipaddr.IPv6Address('::ffff:ffff:ffff:ffff')) 777 ipaddr.IPv6Address('::ffff:ffff:ffff:ffff'))
710 778
711 # V6 - check we're cached 779 # V6 - check we're cached
712 self.assertTrue(self.ipv6._cache.has_key('network')) 780 self.assertTrue(self.ipv6._cache.has_key('network'))
713 self.assertTrue(self.ipv6._cache.has_key('broadcast')) 781 self.assertTrue(self.ipv6._cache.has_key('broadcast'))
714 self.assertTrue(self.ipv6._cache.has_key('hostmask')) 782 self.assertTrue(self.ipv6._cache.has_key('hostmask'))
715 783
716 784
717 if __name__ == '__main__': 785 if __name__ == '__main__':
718 unittest.main() 786 unittest.main()
OLDNEW
« ipaddr.py ('K') | « ipaddr.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b