forked from jsommers/pytricia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test3.py
147 lines (120 loc) · 4.93 KB
/
test3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import unittest
import pytricia
def dumppyt(t):
print ("\nDumping Pytricia")
for x in t.keys():
print ("\t {}: {}".format(x,t[x]))
class PyTriciaTests(unittest.TestCase):
def testInit(self):
with self.assertRaises(ValueError) as cm:
t = pytricia.PyTricia('a')
self.assertIsInstance(cm.exception, ValueError)
with self.assertRaises(ValueError) as cm:
t = pytricia.PyTricia(-1)
self.assertIsInstance(cm.exception, ValueError)
self.assertIsInstance(pytricia.PyTricia(1), pytricia.PyTricia)
self.assertIsInstance(pytricia.PyTricia(128), pytricia.PyTricia)
with self.assertRaises(ValueError) as cm:
t = pytricia.PyTricia(129)
self.assertIsInstance(cm.exception, ValueError)
def testBasic(self):
pyt = pytricia.PyTricia()
pyt["10.0.0.0/8"] = 'a'
pyt["10.1.0.0/16"] = 'b'
# dumppyt(pyt)
self.assertEqual(pyt["10.0.0.0/8"], 'a')
self.assertEqual(pyt["10.1.0.0/16"], 'b')
self.assertEqual(pyt["10.0.0.0"], 'a')
self.assertEqual(pyt["10.1.0.0"], 'b')
self.assertEqual(pyt["10.1.0.1"], 'b')
self.assertEqual(pyt["10.0.0.1"], 'a')
self.assertTrue('10.0.0.0' in pyt)
self.assertTrue('10.1.0.0' in pyt)
self.assertTrue('10.0.0.1' in pyt)
self.assertFalse('9.0.0.0' in pyt)
self.assertFalse('0.0.0.0' in pyt)
self.assertTrue(pyt.has_key('10.0.0.0/8'))
self.assertTrue(pyt.has_key('10.1.0.0/16'))
self.assertFalse(pyt.has_key('10.2.0.0/16'))
self.assertFalse(pyt.has_key('9.0.0.0/8'))
self.assertFalse(pyt.has_key('10.0.0.1'))
self.assertTrue(pyt.has_key('10.0.0.0/8'))
self.assertTrue(pyt.has_key('10.1.0.0/16'))
self.assertFalse(pyt.has_key('10.2.0.0/16'))
self.assertFalse(pyt.has_key('9.0.0.0/8'))
self.assertFalse(pyt.has_key('10.0.0.0'))
self.assertListEqual(['10.0.0.0/8','10.1.0.0/16'], pyt.keys())
def testMoreComplex(self):
pyt = pytricia.PyTricia()
pyt["10.0.0.0/8"] = 'a'
pyt["10.1.0.0/16"] = 'b'
pyt["10.0.1.0/24"] = 'c'
pyt["0.0.0.0/0"] = 'default route'
# dumppyt(pyt)
# FIXME: make some more tests
self.assertFalse(pyt.has_key('1.0.0.0/8'))
def testInsertRemove(self):
pyt = pytricia.PyTricia()
pyt['10.0.0.0/8'] = list(range(10))
self.assertListEqual(['10.0.0.0/8'], pyt.keys())
pyt.delete('10.0.0.0/8')
self.assertListEqual([], pyt.keys())
self.assertFalse(pyt.has_key('10.0.0.0/8'))
pyt['10.0.0.0/8'] = list(range(10))
self.assertListEqual(['10.0.0.0/8'], pyt.keys())
pyt.delete('10.0.0.0/8')
self.assertListEqual([], pyt.keys())
self.assertFalse(pyt.has_key('10.0.0.0/8'))
pyt['10.0.0.0/8'] = list(range(10))
self.assertListEqual(['10.0.0.0/8'], pyt.keys())
del pyt['10.0.0.0/8']
self.assertListEqual([], pyt.keys())
self.assertFalse(pyt.has_key('10.0.0.0/8'))
with self.assertRaises(KeyError) as cm:
t = pytricia.PyTricia()
pyt['10.0.0.0/8'] = list(range(10))
t.delete('10.0.0.0/9')
self.assertIsInstance(cm.exception, KeyError)
def testIteration(self):
pyt = pytricia.PyTricia()
pyt["10.1.0.0/16"] = 'b'
pyt["10.0.0.0/8"] = 'a'
pyt["10.0.1.0/24"] = 'c'
pyt["0.0.0.0/0"] = 'default route'
self.assertListEqual(sorted(['0.0.0.0/0', '10.0.0.0/8','10.1.0.0/16','10.0.1.0/24']), sorted(list(pyt.__iter__())))
# self.assertListEqual(['0.0.0.0/0', '10.0.0.0/8','10.1.0.0/16','10.0.1.0/24'], list(iter(pyt)))
def testIteration2(self):
pyt = pytricia.PyTricia()
pyt["10.1.0.0/16"] = 'b'
pyt["10.0.0.0/8"] = 'a'
pyt["10.0.1.0/24"] = 'c'
x = iter(pyt)
self.assertIsNotNone(next(x))
self.assertIsNotNone(next(x))
self.assertIsNotNone(next(x))
self.assertRaises(StopIteration, next, x)
self.assertRaises(StopIteration, next, x)
def testIteration3(self):
pyt = pytricia.PyTricia()
pyt["10.1.0.0/16"] = 'b'
pyt["10.0.0.0/8"] = 'a'
pyt["10.0.1.0/24"] = 'c'
x = iter(pyt)
del pyt["10.0.1.0/24"]
self.assertIsNotNone(next(x))
self.assertIsNotNone(next(x))
self.assertRaises(StopIteration, next, x)
self.assertRaises(StopIteration, next, x)
def testBroken(self):
import ipaddr
d1 = {'net': ipaddr.IPv4Network('10.0.0.0/24'), 'dests': [u'a']}
d2 = {'net': ipaddr.IPv4Network('10.0.1.0/24'), 'dests': [u'b']}
pyt = pytricia.PyTricia()
pyt['10.0.0.0/24'] = d1
pyt['10.0.1.0/24'] = d2
for k in pyt:
print k,pyt[k]
# tests should cover:
# get w,w/o default, has_key, keys, in, [] access, [] assigment
if __name__ == '__main__':
unittest.main()