2 # The software in this package is distributed under the GNU General
3 # Public License version 2 (with a special exception described below).
5 # A copy of GNU General Public License (GPL) is included in this distribution,
6 # in the file COPYING.GPL.
8 # As a special exception, if other files instantiate templates or use macros
9 # or inline functions from this file, or you compile this file and link it
10 # with other works to produce a work based on this file, this file
11 # does not by itself cause the resulting work to be covered
12 # by the GNU General Public License.
14 # However the source code for this file must still be made available
15 # in accordance with section (3) of the GNU General Public License.
17 # This exception does not invalidate any other reasons why a work based
18 # on this file might be covered by the GNU General Public License.
20 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
22 from __future__ import absolute_import
26 from src import v4_addr_range
29 specific_range_lo = 13
30 specific_range_large = 37
31 specific_range_short = 2
32 specific_range_extreme = 0xffffffff
33 specific_range_invalid = -1
36 class V4_addr_range_test(unittest.TestCase):
38 def test_create_default_range(self):
39 r = v4_addr_range.V4_addr_range()
40 self.assertIsNotNone(r)
42 def test_create_specific_range(self):
43 r = v4_addr_range.V4_addr_range(
44 specific_range_lo, specific_range_large)
45 self.assertIsNotNone(r)
47 def test_get_from_range(self):
48 r = v4_addr_range.V4_addr_range(specific_range_lo)
50 self.assertEqual(a, specific_range_lo)
52 self.assertEqual(b, specific_range_lo + 1)
54 def test_exhaust_range(self):
55 r = v4_addr_range.V4_addr_range(
56 specific_range_lo, specific_range_short)
57 self.assertIsNotNone(r.get())
58 self.assertIsNotNone(r.get())
59 # I have absolutely no idea why the following should work. The
60 # ``.assertRaises`` method should test exceptions for equality, yet
61 # ``error.TestError != Exception``. Whatever.
62 self.assertRaises(Exception, r.get)
64 def test_overflow(self):
65 self.assertRaises(Exception,
66 v4_addr_range.V4_addr_range,
68 specific_range_extreme)
70 def test_nonnatural(self):
71 self.assertRaises(Exception,
72 v4_addr_range.V4_addr_range,
73 addr_range=specific_range_invalid)
74 self.assertRaises(Exception,
75 v4_addr_range.V4_addr_range,
78 def test_type_inconsistency(self):
79 self.assertRaises(TypeError,
80 v4_addr_range.V4_addr_range,
82 self.assertRaises(TypeError,
83 v4_addr_range.V4_addr_range,
84 addr_range="<garbage>")
86 def test_bound_methods(self):
87 r = v4_addr_range.V4_addr_range(
88 specific_range_lo, specific_range_large)
89 self.assertEqual(r.get(), specific_range_lo)
90 self.assertEqual(r.lo(), specific_range_lo)
91 self.assertEqual(r.range(), specific_range_large)
92 self.assertEqual(r.hi(), specific_range_lo + specific_range_large)
94 def test_access(self):
95 r = v4_addr_range.V4_addr_range(
96 specific_range_lo, specific_range_short)
97 self.assertEqual(r.get(), specific_range_lo)
98 self.assertTrue(r[specific_range_lo])
99 self.assertFalse(r[specific_range_lo + 42])
102 r = v4_addr_range.V4_addr_range(
103 specific_range_lo, specific_range_short)
105 self.assertEqual(len(r), 1)
107 self.assertEqual(len(r), 2)
109 if __name__ == '__main__':