Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: net/tools/tld_cleanup/make_dafsa_unittest.py

Issue 197183002: Reduce footprint of registry controlled domain table (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed more style issues and doc strings Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #!/usr/bin/env python
2
3 # Copyright 2014 The Chromium Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7
8 import sys
9 import unittest
10 import make_dafsa
11
12
13 class ParseGperfTest(unittest.TestCase):
14 def testMalformedKey(self):
15 """Tests exception is thrown at bad format."""
16 infile1 = [ '%%', '', '%%' ]
17 self.assertRaises(make_dafsa.InputError, make_dafsa.parse_gperf, infile1)
18
19 infile2 = [ '%%', 'apa,1', '%%' ]
20 self.assertRaises(make_dafsa.InputError, make_dafsa.parse_gperf, infile2)
21
22 infile3 = [ '%%', 'apa, 1', '%%' ]
23 self.assertRaises(make_dafsa.InputError, make_dafsa.parse_gperf, infile3)
24
25 def testBadValues(self):
26 """Tests exception is thrown when value is out of range."""
27 infile1 = [ '%%', 'a, -1', '%%' ]
28 self.assertRaises(make_dafsa.InputError, make_dafsa.parse_gperf, infile1)
29
30 infile2 = [ '%%', 'a, x', '%%' ]
31 self.assertRaises(make_dafsa.InputError, make_dafsa.parse_gperf, infile2)
32
33 infile3 = [ '%%', 'a, 3', '%%' ]
34 self.assertRaises(make_dafsa.InputError, make_dafsa.parse_gperf, infile3)
35
36 infile4 = [ '%%', 'a, 6', '%%' ]
37 self.assertRaises(make_dafsa.InputError, make_dafsa.parse_gperf, infile4)
38
39 infile5 = [ '%%', 'a, 12', '%%' ]
40 self.assertRaises(make_dafsa.InputError, make_dafsa.parse_gperf, infile5)
41
42 def testValues(self):
43 """Tests legal values are accepted."""
44 infile1 = [ '%%', 'a, 0', '%%' ]
45 words1 = [ 'a0' ]
46 self.assertEqual(make_dafsa.parse_gperf(infile1), words1)
47
48 infile2 = [ '%%', 'a, 1', '%%' ]
49 words2 = [ 'a1' ]
50 self.assertEqual(make_dafsa.parse_gperf(infile2), words2)
51
52 infile3 = [ '%%', 'a, 2', '%%' ]
53 words3 = [ 'a2' ]
54 self.assertEqual(make_dafsa.parse_gperf(infile3), words3)
55
56 infile4 = [ '%%', 'a, 4', '%%' ]
57 words4 = [ 'a4' ]
58 self.assertEqual(make_dafsa.parse_gperf(infile4), words4)
59
60 def testOneWord(self):
61 """Tests a single key can be parsed."""
62 infile = [ '%%', 'apa, 1', '%%' ]
63 words = [ 'apa1' ]
64 self.assertEqual(make_dafsa.parse_gperf(infile), words)
65
66 def testTwoWords(self):
67 """Tests a sequence of keys can be parsed."""
68 infile = [ '%%', 'apa, 1', 'bepa.com, 2', '%%' ]
69 words = [ 'apa1', 'bepa.com2' ]
70 self.assertEqual(make_dafsa.parse_gperf(infile), words)
71
72
73 class ToDafsaTest(unittest.TestCase):
74 def testEmptyInput(self):
75 """Tests exception is thrown at empty input."""
76 words = ()
77 self.assertRaises(make_dafsa.InputError, make_dafsa.to_dafsa, words)
78
79 def testNonASCII(self):
80 """Tests exception is thrown if illegal characters are used."""
81 words1 = ( chr(0x1F) + 'a1', )
82 self.assertRaises(make_dafsa.InputError, make_dafsa.to_dafsa, words1)
83
84 words2 = ( 'a' + chr(0x1F) + '1', )
85 self.assertRaises(make_dafsa.InputError, make_dafsa.to_dafsa, words2)
86
87 words3 = ( chr(0x80) + 'a1', )
88 self.assertRaises(make_dafsa.InputError, make_dafsa.to_dafsa, words3)
89
90 words4 = ( 'a' + chr(0x80) + '1', )
91 self.assertRaises(make_dafsa.InputError, make_dafsa.to_dafsa, words4)
92
93 def testChar(self):
94 """Tests a DAFSA can be created from a single character domain name."""
95 words = [ 'a0' ]
96 node2 = ( chr(0), [ None ] )
97 node1 = ( 'a', [ node2 ] )
98 source = [ node1 ]
99 self.assertEqual(make_dafsa.to_dafsa(words), source)
100
101 def testChars(self):
102 """Tests a DAFSA can be created from a multi character domain name."""
103 words = [ 'ab0' ]
104 node3 = ( chr(0), [ None ] )
105 node2 = ( 'b', [ node3 ] )
106 node1 = ( 'a', [ node2 ] )
107 source = [ node1 ]
108 self.assertEqual(make_dafsa.to_dafsa(words), source)
109
110 def testWords(self):
111 """Tests a DAFSA can be created from a sequence of domain names."""
112 words = [ 'a0', 'b1' ]
113 node4 = ( chr(1), [ None ] )
114 node3 = ( 'b', [ node4 ] )
115 node2 = ( chr(0), [ None ] )
116 node1 = ( 'a', [ node2 ] )
117 source = [ node1, node3 ]
118 self.assertEqual(make_dafsa.to_dafsa(words), source)
119
120
121 class ToWordsTest(unittest.TestCase):
122 def testSink(self):
123 """Tests the sink is exapnded to a list with an empty string."""
124 node1 = None
125 words = [ '' ]
126 self.assertEqual(make_dafsa.to_words(node1), words)
127
128 def testSingleNode(self):
129 """Tests a single node is expanded to a list with the label string."""
130
131 # 'ab' -> [ 'ab' ]
132
133 node1 = ( 'ab', [ None ] )
134 words = [ 'ab' ]
135 self.assertEqual(make_dafsa.to_words(node1), words)
136
137 def testChain(self):
138 """Tests a sequence of nodes are preoperly expanded."""
139
140 # 'ab' -> 'cd' => [ 'abcd' ]
141
142 node2 = ( 'cd', [ None ] )
143 node1 = ( 'ab', [ node2 ] )
144 words = [ 'abcd' ]
145 self.assertEqual(make_dafsa.to_words(node1), words)
146
147 def testInnerTerminator(self):
148 """Tests a sequence with an inner terminator is expanded to two strings."""
149
150 # 'a' -> 'b'
151 # \ => [ 'ab', 'a' ]
152 # {sink}
153
154 node2 = ( 'b', [ None ] )
155 node1 = ( 'a', [ node2, None ] )
156 words = [ 'ab', 'a' ]
157 self.assertEqual(make_dafsa.to_words(node1), words)
158
159 def testDiamond(self):
160 """Tests a diamond can be expanded to a word list."""
161
162 # 'cd'
163 # / \
164 # 'ab' 'gh'
165 # \ /
166 # 'ef'
167
168 node4 = ( 'gh', [ None ] )
169 node3 = ( 'ef', [ node4 ] )
170 node2 = ( 'cd', [ node4 ] )
171 node1 = ( 'ab', [ node2, node3 ] )
172 words = [ 'abcdgh', 'abefgh' ]
173 self.assertEqual(make_dafsa.to_words(node1), words)
174
175
176 class JoinLabelsTest(unittest.TestCase):
177 def testLabel(self):
178 """Tests a single label passes unchanged."""
179
180 # 'a' => 'a'
181
182 node1 = ( 'a', [ None ] )
183 source = [ node1 ]
184 self.assertEqual(make_dafsa.join_labels(source), source)
185
186 def testInnerTerminator(self):
187 """Tests a sequence with an inner terminator passes unchanged."""
188
189 # 'a' -> 'b' 'a' -> 'b'
190 # \ => \
191 # {sink} {sink}
192
193 node2 = ( 'b', [ None ] )
194 node1 = ( 'a', [ node2, None ] )
195 source = [ node1 ]
196 self.assertEqual(make_dafsa.join_labels(source), source)
197
198 def testLabels(self):
199 """Tests a sequence of labels can be joined."""
200
201 # 'a' -> 'b' => 'ab'
202
203 node2 = ( 'b', [ None ] )
204 node1 = ( 'a', [ node2 ] )
205 source1 = [ node1 ]
206 node3 = ( 'ab', [ None ] )
207 source2 = [ node3 ]
208 self.assertEqual(make_dafsa.join_labels(source1), source2)
209
210 def testCompositeLabels(self):
211 """Tests a sequence of multi character labels can be joined."""
212
213 # 'ab' -> 'cd' => 'abcd'
214
215 node2 = ( 'cd', [ None ] )
216 node1 = ( 'ab', [ node2 ] )
217 source1 = [ node1 ]
218 node3 = ( 'abcd', [ None ] )
219 source2 = [ node3 ]
220 self.assertEqual(make_dafsa.join_labels(source1), source2)
221
222 def testAtomicTrie(self):
223 """Tests a trie formed DAFSA with atomic labels passes unchanged."""
224
225 # 'b' 'b'
226 # / /
227 # 'a' => 'a'
228 # \ \
229 # 'c' 'c'
230
231 node3 = ( 'c', [ None ] )
232 node2 = ( 'b', [ None ] )
233 node1 = ( 'a', [ node2, node3 ] )
234 source = [ node1 ]
235 self.assertEqual(make_dafsa.join_labels(source), source)
236
237 def testReverseAtomicTrie(self):
238 """Tests a reverse trie formed DAFSA with atomic labels passes unchanged."""
239
240 # 'a' 'a'
241 # \ \
242 # 'c' => 'c'
243 # / /
244 # 'b' 'b'
245
246 node3 = ( 'c', [ None ] )
247 node2 = ( 'b', [ node3 ] )
248 node1 = ( 'a', [ node3 ] )
249 source = [ node1, node2 ]
250 self.assertEqual(make_dafsa.join_labels(source), source)
251
252 def testChainedTrie(self):
253 """Tests a trie formed DAFSA with chained labels can be joined."""
254
255 # 'c' -> 'd' 'cd'
256 # / /
257 # 'a' -> 'b' => 'ab'
258 # \ \
259 # 'e' -> 'f' 'ef'
260
261 node6 = ( 'f', [ None ] )
262 node5 = ( 'e', [ node6 ] )
263 node4 = ( 'd', [ None ] )
264 node3 = ( 'c', [ node4 ] )
265 node2 = ( 'b', [ node3, node5 ] )
266 node1 = ( 'a', [ node2 ] )
267 source1 = [ node1 ]
268 node9 = ( 'ef', [ None ] )
269 node8 = ( 'cd', [ None ] )
270 node7 = ( 'ab', [ node8, node9 ] )
271 source2 = [ node7 ]
272 self.assertEqual(make_dafsa.join_labels(source1), source2)
273
274 def testReverseChainedTrie(self):
275 """Tests a reverse trie formed DAFSA with chained labels can be joined."""
276
277 # 'a' -> 'b' 'ab'
278 # \ \
279 # 'e' -> 'f' => 'ef'
280 # / /
281 # 'c' -> 'd' 'cd'
282
283 node6 = ( 'f', [ None ] )
284 node5 = ( 'e', [ node6 ] )
285 node4 = ( 'd', [ node5 ] )
286 node3 = ( 'c', [ node4 ] )
287 node2 = ( 'b', [ node5 ] )
288 node1 = ( 'a', [ node2 ] )
289 source1 = [ node1, node3 ]
290 node9 = ( 'ef', [ None ] )
291 node8 = ( 'cd', [ node9 ] )
292 node7 = ( 'ab', [ node9 ] )
293 source2 = [ node7, node8 ]
294 self.assertEqual(make_dafsa.join_labels(source1), source2)
295
296
297 class JoinSuffixesTest(unittest.TestCase):
298 def testSingleLabel(self):
299 """Tests a single label passes unchanged."""
300
301 # 'a' => 'a'
302
303 node1 = ( 'a', [ None ] )
304 source = [ node1 ]
305 self.assertEqual(make_dafsa.join_suffixes(source), source)
306
307 def testInnerTerminator(self):
308 """Tests a sequence with an inner terminator passes unchanged."""
309
310 # 'a' -> 'b' 'a' -> 'b'
311 # \ => \
312 # {sink} {sink}
313
314 node2 = ( 'b', [ None ] )
315 node1 = ( 'a', [ node2, None ] )
316 source = [ node1 ]
317 self.assertEqual(make_dafsa.join_suffixes(source), source)
318
319 def testDistinctTrie(self):
320 """Tests a trie formed DAFSA with distinct labels passes unchanged."""
321
322 # 'b' 'b'
323 # / /
324 # 'a' => 'a'
325 # \ \
326 # 'c' 'c'
327
328 node3 = ( 'c', [ None ] )
329 node2 = ( 'b', [ None ] )
330 node1 = ( 'a', [ node2, node3 ] )
331 source = [ node1 ]
332 self.assertEqual(make_dafsa.join_suffixes(source), source)
333
334 def testReverseDistinctTrie(self):
335 """Tests a reverse trie formed DAFSA with distinct labels passes unchanged.
336 """
337
338 # 'a' 'a'
339 # \ \
340 # 'c' => 'c'
341 # / /
342 # 'b' 'b'
343
344 node3 = ( 'c', [ None ] )
345 node2 = ( 'b', [ node3 ] )
346 node1 = ( 'a', [ node3 ] )
347 source = [ node1, node2 ]
348 self.assertEqual(make_dafsa.join_suffixes(source), source)
349
350 def testJoinTwoHeads(self):
351 """Tests two heads can be joined even if there is something else between."""
352
353 # 'a' ------'a'
354 # /
355 # 'b' => 'b' /
356 # /
357 # 'a' ---
358 #
359 # The picture above should shows that the new version should have just one
360 # instance of the node with label 'a'.
361
362 node3 = ( 'a', [ None ] )
363 node2 = ( 'b', [ None ] )
364 node1 = ( 'a', [ None ] )
365 source1 = [ node1, node2, node3 ]
366 source2 = make_dafsa.join_suffixes(source1)
367
368 # Both versions should expand to the same content.
369 self.assertEqual(source1, source2)
370 # But the new version should have just one instance of 'a'.
371 self.assertIs(source2[0], source2[2])
372
373 def testJoinTails(self):
374 """Tests tails can be joined."""
375
376 # 'a' -> 'c' 'a'
377 # \
378 # => 'c'
379 # /
380 # 'b' -> 'c' 'b'
381
382 node4 = ( 'c', [ None ] )
383 node3 = ( 'b', [ node4 ] )
384 node2 = ( 'c', [ None ] )
385 node1 = ( 'a', [ node2 ] )
386 source1 = [ node1, node3 ]
387 source2 = make_dafsa.join_suffixes(source1)
388
389 # Both versions should expand to the same content.
390 self.assertEqual(source1, source2)
391 # But the new version should have just one tail.
392 self.assertIs(source2[0][1][0], source2[1][1][0])
393
394 def testMakeRecursiveTrie(self):
395 """Tests recursive suffix join."""
396
397 # 'a' -> 'e' -> 'g' 'a'
398 # \
399 # 'e'
400 # / \
401 # 'b' -> 'e' -> 'g' 'b' \
402 # \
403 # => 'g'
404 # /
405 # 'c' -> 'f' -> 'g' 'c' /
406 # \ /
407 # 'f'
408 # /
409 # 'd' -> 'f' -> 'g' 'd'
410
411 node7 = ( 'g', [ None ] )
412 node6 = ( 'f', [ node7 ] )
413 node5 = ( 'e', [ node7 ] )
414 node4 = ( 'd', [ node6 ] )
415 node3 = ( 'c', [ node6 ] )
416 node2 = ( 'b', [ node5 ] )
417 node1 = ( 'a', [ node5 ] )
418 source1 = [ node1, node2, node3, node4 ]
419 source2 = make_dafsa.join_suffixes(source1)
420
421 # Both versions should expand to the same content.
422 self.assertEqual(source1, source2)
423 # But the new version should have just one 'e'.
424 self.assertIs(source2[0][1][0], source2[1][1][0])
425 # And one 'f'.
426 self.assertIs(source2[2][1][0], source2[3][1][0])
427 # And one 'g'.
428 self.assertIs(source2[0][1][0][1][0], source2[2][1][0][1][0])
429
430 def testMakeDiamond(self):
431 """Test we can join suffixes of a trie."""
432
433 # 'b' -> 'd' 'b'
434 # / / \
435 # 'a' => 'a' 'd'
436 # \ \ /
437 # 'c' -> 'd' 'c'
438
439 node5 = ( 'd', [ None ] )
440 node4 = ( 'c', [ node5 ] )
441 node3 = ( 'd', [ None ] )
442 node2 = ( 'b', [ node3 ] )
443 node1 = ( 'a', [ node2, node4 ] )
444 source1 = [ node1 ]
445 source2 = make_dafsa.join_suffixes(source1)
446
447 # Both versions should expand to the same content.
448 self.assertEqual(source1, source2)
449 # But the new version should have just one 'd'.
450 self.assertIs(source2[0][1][0][1][0], source2[0][1][1][1][0])
451
452 def testJoinOneChild(self):
453 """Tests that we can join some children but not all."""
454
455 # 'c' ----'c'
456 # / / /
457 # 'a' 'a' /
458 # \ \ /
459 # 'd' 'd'/
460 # => /
461 # 'c' /
462 # / /
463 # 'b' 'b'
464 # \ \
465 # 'e' 'e'
466
467 node6 = ( 'e', [ None ] )
468 node5 = ( 'c', [ None ] )
469 node4 = ( 'b', [ node5, node6 ] )
470 node3 = ( 'd', [ None ] )
471 node2 = ( 'c', [ None ] )
472 node1 = ( 'a', [ node2, node3 ] )
473 source1 = [ node1, node4 ]
474 source2 = make_dafsa.join_suffixes(source1)
475
476 # Both versions should expand to the same content.
477 self.assertEqual(source1, source2)
478 # But the new version should have just one 'c'.
479 self.assertIs(source2[0][1][0], source2[1][1][0])
480
481
482 class ReverseTest(unittest.TestCase):
483 def testAtomicLabel(self):
484 """Tests an atomic label passes unchanged."""
485
486 # 'a' => 'a'
487
488 node1 = ( 'a', [ None ] )
489 source = [ node1 ]
490 self.assertEqual(make_dafsa.reverse(source), source)
491
492 def testLabel(self):
493 """Tests that labels are reversed."""
494
495 # 'ab' => 'ba'
496
497 node1 = ( 'ab', [ None ] )
498 source1 = [ node1 ]
499 node2 = ( 'ba', [ None ] )
500 source2 = [ node2 ]
501 self.assertEqual(make_dafsa.reverse(source1), source2)
502
503 def testChain(self):
504 """Tests that edges are reversed."""
505
506 # 'a' -> 'b' => 'b' -> 'a'
507
508 node2 = ( 'b', [ None ] )
509 node1 = ( 'a', [ node2 ] )
510 source1 = [ node1 ]
511 node4 = ( 'a', [ None ] )
512 node3 = ( 'b', [ node4 ] )
513 source2 = [ node3 ]
514 self.assertEqual(make_dafsa.reverse(source1), source2)
515
516 def testInnerTerminator(self):
517 """Tests a sequence with an inner terminator can be reversed."""
518
519 # 'a' -> 'b' 'b' -> 'a'
520 # \ => /
521 # {sink} ------
522
523 node2 = ( 'b', [ None ] )
524 node1 = ( 'a', [ node2, None ] )
525 source1 = [ node1 ]
526 node4 = ( 'a', [ None ] )
527 node3 = ( 'b', [ node4 ] )
528 source2 = [ node3, node4 ]
529 self.assertEqual(make_dafsa.reverse(source1), source2)
530
531 def testAtomicTrie(self):
532 """Tests a trie formed DAFSA can be reversed."""
533
534 # 'b' 'b'
535 # / \
536 # 'a' => 'a'
537 # \ /
538 # 'c' 'c'
539
540 node3 = ( 'c', [ None ] )
541 node2 = ( 'b', [ None ] )
542 node1 = ( 'a', [ node2, node3 ] )
543 source1 = [ node1 ]
544 node6 = ( 'a', [ None ] )
545 node5 = ( 'c', [ node6 ] )
546 node4 = ( 'b', [ node6 ] )
547 source2 = [ node4, node5 ]
548 self.assertEqual(make_dafsa.reverse(source1), source2)
549
550 def testReverseAtomicTrie(self):
551 """Tests a reverse trie formed DAFSA can be reversed."""
552
553 # 'a' 'a'
554 # \ /
555 # 'c' => 'c'
556 # / \
557 # 'b' 'b'
558
559 node3 = ( 'c', [ None ] )
560 node2 = ( 'b', [ node3 ] )
561 node1 = ( 'a', [ node3 ] )
562 source1 = [ node1, node2 ]
563 node6 = ( 'b', [ None ] )
564 node5 = ( 'a', [ None ] )
565 node4 = ( 'c', [ node5, node6 ] )
566 source2 = [ node4 ]
567 self.assertEqual(make_dafsa.reverse(source1), source2)
568
569 def testDiamond(self):
570 """Tests we can reverse both edges and nodes in a diamond."""
571
572 # 'cd' 'dc'
573 # / \ / \
574 # 'ab' 'gh' => 'hg' 'ba'
575 # \ / \ /
576 # 'ef' 'fe'
577
578 node4 = ( 'gh', [ None ] )
579 node3 = ( 'ef', [ node4 ] )
580 node2 = ( 'cd', [ node4 ] )
581 node1 = ( 'ab', [ node2, node3 ] )
582 source1 = [ node1 ]
583 node8 = ( 'ba', [ None ] )
584 node7 = ( 'fe', [ node8 ] )
585 node6 = ( 'dc', [ node8 ] )
586 node5 = ( 'hg', [ node6, node7 ] )
587 source2 = [ node5 ]
588 self.assertEqual(make_dafsa.reverse(source1), source2)
589
590
591 class TopSortTest(unittest.TestCase):
592 def testNode(self):
593 """Tests a DAFSA with one node can be sorted."""
594
595 # 'a' => [ 'a' ]
596
597 node1 = ( 'a', [ None ] )
598 source = [ node1 ]
599 nodes = [ node1 ]
600 self.assertEqual(make_dafsa.top_sort(source), nodes)
601
602 def testDiamond(self):
603 """Tests nodes in a diamond can be sorted."""
604
605 # 'b'
606 # / \
607 # 'a' 'd'
608 # \ /
609 # 'c'
610
611 node4 = ( 'd', [ None ] )
612 node3 = ( 'c', [ node4 ] )
613 node2 = ( 'b', [ node4 ] )
614 node1 = ( 'a', [ node2, node3 ] )
615 source = [ node1 ]
616 nodes = make_dafsa.top_sort(source)
617 self.assertLess(nodes.index(node1), nodes.index(node2))
618 self.assertLess(nodes.index(node2), nodes.index(node4))
619 self.assertLess(nodes.index(node3), nodes.index(node4))
620
621
622 class EncodePrefixTest(unittest.TestCase):
623 def testChar(self):
624 """Tests to encode a single character prefix."""
625 label = 'a'
626 bytes = [ ord('a') ]
627 self.assertEqual(make_dafsa.encode_prefix(label), bytes)
628
629 def testChars(self):
630 """Tests to encode a multi character prefix."""
631 label = 'ab'
632 bytes = [ ord('b'), ord('a') ]
633 self.assertEqual(make_dafsa.encode_prefix(label), bytes)
634
635
636 class EncodeLabelTest(unittest.TestCase):
637 def testChar(self):
638 """Tests to encode a single character label."""
639 label = 'a'
640 bytes = [ ord('a') + 0x80 ]
641 self.assertEqual(make_dafsa.encode_label(label), bytes)
642
643 def testChars(self):
644 """Tests to encode a multi character label."""
645 label = 'ab'
646 bytes = [ ord('b') + 0x80, ord('a') ]
647 self.assertEqual(make_dafsa.encode_label(label), bytes)
648
649
650 class EncodeLinksTest(unittest.TestCase):
651 def testEndLabel(self):
652 """Tests to encode link to the sink."""
653 children = [ None ]
654 offsets = {}
655 bytes = 0
656 output = []
657 self.assertEqual(make_dafsa.encode_links(children, offsets, bytes),
658 output)
659
660 def testOneByteOffset(self):
661 """Tests to encode a single one byte offset."""
662 node = ( '', [ None ] )
663 children = [ node ]
664 offsets = { id(node) : 2 }
665 bytes = 5
666 output = [ 132 ]
667 self.assertEqual(make_dafsa.encode_links(children, offsets, bytes),
668 output)
669
670 def testOneByteOffsets(self):
671 """Tests to encode a sequence of one byte offsets."""
672 node1 = ( '', [ None ] )
673 node2 = ( '', [ None ] )
674 children = [ node1, node2 ]
675 offsets = { id(node1) : 2, id(node2) : 1 }
676 bytes = 5
677 output = [ 129, 5 ]
678 self.assertEqual(make_dafsa.encode_links(children, offsets, bytes),
679 output)
680
681 def testTwoBytesOffset(self):
682 """Tests to encode a single two byte offset."""
683 node = ( '', [ None ] )
684 children = [ node ]
685 offsets = { id(node) : 2 }
686 bytes = 1005
687 output = [ 237, 195]
688 self.assertEqual(make_dafsa.encode_links(children, offsets, bytes),
689 output)
690
691 def testTwoBytesOffsets(self):
692 """Tests to encode a sequence of two byte offsets."""
693 node1 = ( '', [ None ] )
694 node2 = ( '', [ None ] )
695 node3 = ( '', [ None ] )
696 children = [ node1, node2, node3 ]
697 offsets = { id(node1) : 1002, id(node2) : 2, id(node3) : 2002 }
698 bytes = 3005
699 output = [ 232, 195, 232, 67, 241, 67 ]
700 self.assertEqual(make_dafsa.encode_links(children, offsets, bytes),
701 output)
702
703 def testThreeBytesOffset(self):
704 """Tests to encode a single three byte offset."""
705 node = ( '', [ None ] )
706 children = [ node ]
707 offsets = { id(node) : 2 }
708 bytes = 100005
709 output = [ 166, 134, 225 ]
710 self.assertEqual(make_dafsa.encode_links(children, offsets, bytes),
711 output)
712
713 def testThreeBytesOffsets(self):
714 """Tests to encode a sequence of three byte offsets."""
715 node1 = ( '', [ None ] )
716 node2 = ( '', [ None ] )
717 node3 = ( '', [ None ] )
718 children = [ node1, node2, node3 ]
719 offsets = { id(node1) : 100002, id(node2) : 2, id(node3) : 200002 }
720 bytes = 300005
721 output = [ 160, 134, 225, 160, 134, 97, 172, 134, 97 ]
722 self.assertEqual(make_dafsa.encode_links(children, offsets, bytes),
723 output)
724
725 def testOneTwoThreeBytesOffsets(self):
726 """Tests to encode offsets of different sizes."""
727 node1 = ( '', [ None ] )
728 node2 = ( '', [ None ] )
729 node3 = ( '', [ None ] )
730 children = [ node1, node2, node3 ]
731 offsets = { id(node1) : 10003, id(node2) : 10002, id(node3) : 100002 }
732 bytes = 300005
733 output = [ 129, 143, 95, 97, 74, 13, 99 ]
734 self.assertEqual(make_dafsa.encode_links(children, offsets, bytes),
735 output)
736
737
738 class ExamplesTest(unittest.TestCase):
739 def testExample1(self):
740 """Tests Example 1 from make_dafsa.py."""
741 infile = [ '%%', 'aa, 1', 'a, 2', '%%' ]
742 bytes = [ 0x81, 0xE1, 0x02, 0x81, 0x82, 0x61, 0x81 ]
743 outfile = make_dafsa.to_cxx(bytes)
744 self.assertEqual(make_dafsa.words_to_cxx(make_dafsa.parse_gperf(infile)),
745 outfile)
746
747 def testExample2(self):
748 """Tests Example 2 from make_dafsa.py."""
749 infile = [ '%%', 'aa, 1', 'bbb, 2', 'baa, 1', '%%' ]
750 bytes = [ 0x02, 0x83, 0xE2, 0x02, 0x83, 0x61, 0x61, 0x81, 0x62, 0x62,
751 0x82 ]
752 outfile = make_dafsa.to_cxx(bytes)
753 self.assertEqual(make_dafsa.words_to_cxx(make_dafsa.parse_gperf(infile)),
754 outfile)
755
756
757 if __name__ == '__main__':
758 unittest.main()
OLDNEW
« net/tools/tld_cleanup/make_dafsa.py ('K') | « net/tools/tld_cleanup/make_dafsa.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698