File: test_trie.py

package info (click to toggle)
python-cogent 1.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,424 kB
  • ctags: 24,343
  • sloc: python: 134,200; makefile: 100; ansic: 17; sh: 10
file content (329 lines) | stat: -rw-r--r-- 9,100 bytes parent folder | download
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#!/usr/bin/env python

"""tests for Trie and compressed Trie class."""

__author__ = "Jens Reeder"
__copyright__ = "Copyright 2007-2012, The Cogent Project"
__credits__ = ["Jens Reeder"]
__license__ = "GPL"
__version__ = "1.5.3"
__maintainer__ = "Jens Reeder"
__email__ = "jens.reeder@gmail.com"
__status__ = "Prototype"


from cogent.util.unit_test import TestCase, main
from cogent.util.trie import Trie, Compressed_Trie, build_prefix_map, build_trie, \
    _build_prefix_map

class TrieTests(TestCase):
    
    def setUp(self):
        self.data = dict({"0": "ab", "1":"abababa", "2":"abab",
                          "3":"baba", "4":"ababaa","5":"a", "6":"abababa",
                          "7":"bab", "8":"babba"})
    
    def test_init(self):
        """Trie init should create an empty trie."""
        
        t = Trie()
        self.assertEqual(t.root.labels, [])
        self.assertEqual(t.root.children, {})
        
    def test_insert_find(self):
        """An added key should be found by find."""

        data = self.data
        
        t = Trie()
        for (label, seq) in data.iteritems():
            t.insert(seq, label)
           
        for  (label, seq) in data.iteritems():
            self.assertEqual(label in t.find(seq), True)
        self.assertEqual(t.find("cacgchagc"), [])
        self.assertEqual(t.find("abababa"), ["1","6"])

    def test_insert_unique(self):
        """insert_unique should insert only unique words."""

        data = self.data
        
        t = Trie()
        for (label, seq) in data.iteritems():
            t._insert_unique(seq, label)
                       
        self.assertEqual(t.find("ab"), [])
        self.assertEqual(t.find("cacgchagc"), [])
        self.assertEqual(t.find("abababa"), ["1"])


    def test_build_prefix_map(self):
        """prefix_map should map prefix strings."""
        
        self.assertEqual(dict(_build_prefix_map(self.data.iteritems())),
                         {'1': ['0', '2', '5', '6'],
                          '8': [],
                          '3': ['7'],
                          '4': []})

class Compressed_Trie_Tests(TestCase):
    
    def setUp(self):
        self.data = dict({"0": "ab", "1":"abababa", "2":"abab",
                          "3":"baba", "4":"ababaa","5":"a", "6":"abababa",
                          "7":"bab", "8":"babba"})

        self.trie = build_trie(self.data.iteritems())
    
    def test_init(self):
        """Trie init should create an empty trie."""
        
        t = Compressed_Trie()
        self.assertEqual(t.root.labels, [])
        self.assertEqual(t.root.children, {})
        self.assertEqual(t.root.key, "")
        
    def test_non_zero(self):
        """__non_zero__ should cehck for any data in the trie."""
        t = Compressed_Trie()
        self.assertEqual(t.__nonzero__(), False)
        self.assertEqual(self.trie.__nonzero__(), True)

    def test_len(self):
        """__len__ should return the number of seqs in the trie."""
        
        self.assertEqual(len(self.trie), 9)
        t = Compressed_Trie()
        self.assertEqual(len(t), 0)

    def test_size(self):
        """size should return the number of nodes in the trie."""
        
        self.assertEqual(self.trie.size(), 10)

        #empty trie contins only root node
        t = Compressed_Trie()
        self.assertEqual(size(t), 1)

    def test_to_string(self):
        """_to_string should create a string representation."""

        string_rep = """
key 
{
\tkey a['5']
\t{
\t\tkey b['0']
\t\t{
\t\t\tkey ab['2']
\t\t\t{
\t\t\t\tkey a
\t\t\t\t{
\t\t\t\t\tkey a['4']
\t\t\t\t}
\t\t\t\t{
\t\t\t\t\tkey ba['1', '6']
\t\t\t\t}
\t\t\t}
\t\t}
\t}
}
{
\tkey bab['7']
\t{
\t\tkey a['3']
\t}
\t{
\t\tkey ba['8']
\t}
}
"""
        self.assertEqual(str(self.trie), string_rep)
        
    def test_insert_find(self):
        """An added key should be found by find."""

        data = self.data
        t = Compressed_Trie()
        for (label, seq) in data.iteritems():
            t.insert(seq, label)
    
        for  (label, seq) in data.iteritems():
            self.assertEqual(label in t.find(seq), True)
        self.assertEqual(t.find("abababa"), ["1","6"])
        self.assertEqual(t.find("cacgchagc"), [])


    def test_prefixMap(self):
        """prefix_map (Compressed_Trie) should map prefix strings."""
        
        self.assertEqual(self.trie.prefixMap(),
                         {'1': ['6', '2', '0', '5'],
                          '8': ['7'],
                          '3': [], 
                          '4': []})
    
    def test_init(self):
        """Trie init should create an empty trie."""
        
        t = Trie()
        self.assertEqual(t.root.labels, [])
        self.assertEqual(t.root.children, {})
        
    def test_insert_find(self):
        """An added key should be found by find."""

        data = self.data
        
        t = Trie()
        for (label, seq) in data.iteritems():
            t.insert(seq, label)
           
        for  (label, seq) in data.iteritems():
            self.assertEqual(label in t.find(seq), True)
        self.assertEqual(t.find("cacgchagc"), [])
        self.assertEqual(t.find("abababa"), ["1","6"])

    def test_insert_unique(self):
        """insert_unique should insert only unique words."""

        data = self.data
        
        t = Trie()
        for (label, seq) in data.iteritems():
            t._insert_unique(seq, label)
                       
        self.assertEqual(t.find("ab"), [])
        self.assertEqual(t.find("cacgchagc"), [])
        self.assertEqual(t.find("abababa"), ["1"])


    def test_build_prefix_map(self):
        """prefix_map should map prefix strings."""
        
        self.assertEqual(dict(_build_prefix_map(self.data.iteritems())),
                         {'1': ['0', '2', '5', '6'],
                          '8': [],
                          '3': ['7'],
                          '4': []})
   
    def test_build_trie(self):
        """Build_trie should build trie from seqs."""
        
        t = build_trie(self.data.iteritems(), Trie)
        self.assertTrue(isinstance(t, Trie))
        for (label, seq) in self.data.iteritems():
            self.assertContains(t.find(seq), label)

        self.assertEqual(t.find(""), [])
        self.assertEqual(t.find("ccc"), [])


class Compressed_Trie_Tests(TestCase):
    
    def setUp(self):
        self.data = dict({"0": "ab", "1":"abababa", "2":"abab",
                          "3":"baba", "4":"ababaa","5":"a", "6":"abababa",
                          "7":"bab", "8":"babba"})

        self.trie = build_trie(self.data.iteritems())
    
    def test_init(self):
        """Trie init should create an empty trie."""
        
        t = Compressed_Trie()
        self.assertEqual(t.root.labels, [])
        self.assertEqual(t.root.children, {})
        self.assertEqual(t.root.key, "")
        
    def test_non_zero(self):
        """__non_zero__ should cehck for any data in the trie."""
        t = Compressed_Trie()
        self.assertEqual(t.__nonzero__(), False)
        self.assertEqual(self.trie.__nonzero__(), True)

    def test_len(self):
        """__len__ should return the number of seqs in the trie."""
        
        self.assertEqual(len(self.trie), 9)

    def test_size(self):
        """size should return the number of nodes in the trie."""
        
        self.assertEqual(self.trie.size(), 10)

    def test_to_string(self):
        """_to_string should create a string representation."""

        string_rep = """
key 
{
\tkey a['5']
\t{
\t\tkey b['0']
\t\t{
\t\t\tkey ab['2']
\t\t\t{
\t\t\t\tkey a
\t\t\t\t{
\t\t\t\t\tkey a['4']
\t\t\t\t}
\t\t\t\t{
\t\t\t\t\tkey ba['1', '6']
\t\t\t\t}
\t\t\t}
\t\t}
\t}
}
{
\tkey bab['7']
\t{
\t\tkey a['3']
\t}
\t{
\t\tkey ba['8']
\t}
}
"""
        self.assertEqual(str(self.trie), string_rep)
        
    def test_insert_find(self):
        """An added key should be found by find."""

        data = self.data
        t = Compressed_Trie()
        for (label, seq) in data.iteritems():
            t.insert(seq, label)
    
        for  (label, seq) in data.iteritems():
            self.assertEqual(label in t.find(seq), True)
        self.assertEqual(t.find("abababa"), ["1","6"])
        self.assertEqual(t.find("cacgchagc"), [])


    def test_prefixMap(self):
        """prefix_map (Compressed_Trie) should map prefix strings."""
        
        self.assertEqual(self.trie.prefixMap(),
                         {'1': ['6', '2', '0', '5'],
                          '8': ['7'],
                          '3': [], 
                          '4': []})

    def test_build_trie(self):
        """Build_trie should build trie from seqs."""
        
        t = build_trie(self.data.iteritems())
        
        self.assertTrue(isinstance(t, Compressed_Trie))
        
        for (label, seq) in self.data.iteritems():
            self.assertContains(t.find(seq), label)

        self.assertEqual(t.find(""), [])
        self.assertEqual(t.find("ccc"), [])


if __name__ == "__main__":
    main()