Skip to content

Commit d1c4e4a

Browse files
author
Tobias Oberstein
committed
add unit test for pytrie
1 parent 93fff2a commit d1c4e4a

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

crossbar/router/test/test_pytrie.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#####################################################################################
2+
#
3+
# Copyright (C) Tavendo GmbH
4+
#
5+
# Unless a separate license agreement exists between you and Tavendo GmbH (e.g. you
6+
# have purchased a commercial license), the license terms below apply.
7+
#
8+
# Should you enter into a separate license agreement after having received a copy of
9+
# this software, then the terms of such license agreement replace the terms below at
10+
# the time at which such license agreement becomes effective.
11+
#
12+
# In case a separate license agreement ends, and such agreement ends without being
13+
# replaced by another separate license agreement, the license terms below apply
14+
# from the time at which said agreement ends.
15+
#
16+
# LICENSE TERMS
17+
#
18+
# This program is free software: you can redistribute it and/or modify it under the
19+
# terms of the GNU Affero General Public License, version 3, as published by the
20+
# Free Software Foundation. This program is distributed in the hope that it will be
21+
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23+
#
24+
# See the GNU Affero General Public License Version 3 for more details.
25+
#
26+
# You should have received a copy of the GNU Affero General Public license along
27+
# with this program. If not, see <http://www.gnu.org/licenses/agpl-3.0.en.html>.
28+
#
29+
#####################################################################################
30+
31+
from __future__ import absolute_import
32+
33+
from twisted.trial import unittest
34+
35+
from pytrie import StringTrie
36+
37+
38+
class TestPyTrie(unittest.TestCase):
39+
40+
def test_empty_tree(self):
41+
t = StringTrie()
42+
for key in [u'', u'f', u'foo', u'foobar']:
43+
with self.assertRaises(KeyError) as e:
44+
t.longest_prefix_value(key)
45+
46+
def test_contains(self):
47+
t = StringTrie()
48+
test_keys = [u'', u'f', u'foo', u'foobar', u'baz']
49+
for key in test_keys:
50+
t[key] = key
51+
for key in test_keys:
52+
self.assertTrue(key in t)
53+
54+
def test_longest_prefix_1(self):
55+
t = StringTrie()
56+
test_keys = [u'f', u'foo', u'foobar', u'baz']
57+
for key in test_keys:
58+
t[key] = key
59+
for key in test_keys:
60+
self.assertEqual(t.longest_prefix_value(key), key)
61+
62+
def test_longest_prefix_2(self):
63+
t = StringTrie()
64+
test_keys = [u'f', u'foo', u'foobar']
65+
for key in test_keys:
66+
t[key] = key
67+
68+
test_keys = {
69+
u'foobarbaz': u'foobar',
70+
u'foobaz': u'foo',
71+
u'fool': u'foo',
72+
u'foo': u'foo',
73+
u'fob': u'f',
74+
u'fo': u'f',
75+
u'fx': u'f',
76+
u'f': u'f',
77+
}
78+
for key in test_keys:
79+
self.assertEqual(t.longest_prefix_value(key), test_keys[key])
80+
81+
def test_longest_prefix_3(self):
82+
t = StringTrie()
83+
test_keys = [u'x', u'foo', u'foobar']
84+
85+
for key in [u'y', u'yfoo', u'fox', u'fooba']:
86+
with self.assertRaises(KeyError) as e:
87+
t.longest_prefix_value(key)
88+
89+
def test_longest_prefix_4(self):
90+
stored_key = u'x'
91+
test_key = u'xyz'
92+
93+
t = StringTrie()
94+
t[stored_key] = stored_key
95+
self.assertTrue(stored_key in t)
96+
self.assertTrue(test_key.startswith(stored_key))
97+
self.assertEqual(t.longest_prefix_value(test_key), stored_key)
98+
99+
def test_longest_prefix_5(self):
100+
self.skip = True
101+
stored_key = u''
102+
test_key = u'xyz'
103+
104+
t = StringTrie()
105+
t[stored_key] = stored_key
106+
self.assertTrue(stored_key in t)
107+
self.assertTrue(test_key.startswith(stored_key))
108+
self.assertEqual(t.longest_prefix_value(test_key), stored_key)
109+
110+
test_longest_prefix_5.skip = "pytrie behavior is broken wrt to string keys of zero length!"

0 commit comments

Comments
 (0)