Clean up, remove compat with py < 3.6
[pyi2ncommon] / test / test_v4_addr_range.py
1 #!/usr/bin/env python
2 # The software in this package is distributed under the GNU General
3 # Public License version 2 (with a special exception described below).
4 #
5 # A copy of GNU General Public License (GPL) is included in this distribution,
6 # in the file COPYING.GPL.
7 #
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.
13 #
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.
16 #
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.
19 #
20 # Copyright (c) 2016-2018 Intra2net AG <info@intra2net.com>
21
22 import unittest
23
24 from src import v4_addr_range
25
26
27 specific_range_lo = 13
28 specific_range_large = 37
29 specific_range_short = 2
30 specific_range_extreme = 0xffffffff
31 specific_range_invalid = -1
32
33
34 class V4_addr_range_test(unittest.TestCase):
35
36     def test_create_default_range(self):
37         r = v4_addr_range.V4_addr_range()
38         self.assertIsNotNone(r)
39
40     def test_create_specific_range(self):
41         r = v4_addr_range.V4_addr_range(
42             specific_range_lo, specific_range_large)
43         self.assertIsNotNone(r)
44
45     def test_get_from_range(self):
46         r = v4_addr_range.V4_addr_range(specific_range_lo)
47         a = r.get()
48         self.assertEqual(a, specific_range_lo)
49         b = r.get()
50         self.assertEqual(b, specific_range_lo + 1)
51
52     def test_exhaust_range(self):
53         r = v4_addr_range.V4_addr_range(
54             specific_range_lo, specific_range_short)
55         self.assertIsNotNone(r.get())
56         self.assertIsNotNone(r.get())
57         # I have absolutely no idea why the following should work. The
58         # ``.assertRaises`` method should test exceptions for equality, yet
59         # ``error.TestError != Exception``. Whatever.
60         self.assertRaises(Exception, r.get)
61
62     def test_overflow(self):
63         self.assertRaises(Exception,
64                           v4_addr_range.V4_addr_range,
65                           specific_range_lo,
66                           specific_range_extreme)
67
68     def test_nonnatural(self):
69         self.assertRaises(Exception,
70                           v4_addr_range.V4_addr_range,
71                           addr_range=specific_range_invalid)
72         self.assertRaises(Exception,
73                           v4_addr_range.V4_addr_range,
74                           addr_range=0)
75
76     def test_type_inconsistency(self):
77         self.assertRaises(TypeError,
78                           v4_addr_range.V4_addr_range,
79                           lo="<garbage>")
80         self.assertRaises(TypeError,
81                           v4_addr_range.V4_addr_range,
82                           addr_range="<garbage>")
83
84     def test_bound_methods(self):
85         r = v4_addr_range.V4_addr_range(
86             specific_range_lo, specific_range_large)
87         self.assertEqual(r.get(), specific_range_lo)
88         self.assertEqual(r.lo(), specific_range_lo)
89         self.assertEqual(r.range(), specific_range_large)
90         self.assertEqual(r.hi(), specific_range_lo + specific_range_large)
91
92     def test_access(self):
93         r = v4_addr_range.V4_addr_range(
94             specific_range_lo, specific_range_short)
95         self.assertEqual(r.get(), specific_range_lo)
96         self.assertTrue(r[specific_range_lo])
97         self.assertFalse(r[specific_range_lo + 42])
98
99     def test_len(self):
100         r = v4_addr_range.V4_addr_range(
101             specific_range_lo, specific_range_short)
102         _ = r.get()
103         self.assertEqual(len(r), 1)
104         _ = r.get()
105         self.assertEqual(len(r), 2)
106
107 if __name__ == '__main__':
108     unittest.main()