no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / xpcom / ds / test / test_dafsa.py
blob9becdd6c06c7b1b814a6aade1b706a3bafc3f06d
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 import unittest
6 from io import StringIO
8 import mozunit
9 from incremental_dafsa import Dafsa, Node
12 def _node_to_string(node: Node, prefix, buffer, cache):
13 if not node.is_end_node:
14 prefix += (
15 str(ord(node.character)) if ord(node.character) < 10 else node.character
17 else:
18 prefix += "$"
19 cached = cache.get(id(node))
20 buffer.write("{}{}".format(prefix, "=" if cached else "").strip() + "\n")
22 if not cached:
23 cache[id(node)] = node
24 if node:
25 for node in sorted(node.children.values(), key=lambda n: n.character):
26 _node_to_string(node, prefix, buffer, cache)
29 def _dafsa_to_string(dafsa: Dafsa):
30 """Encodes the dafsa into a string notation.
32 Each node is printed on its own line with all the nodes that precede it.
33 The end node is designated with the "$" character.
34 If it joins into an existing node, the end of the line is adorned with a "=".
35 Though this doesn't carry information about which other prefix it has joined with,
36 it has seemed to be precise enough for testing.
38 For example, with the input data of:
39 * a1
40 * ac1
41 * bc1
43 [root] --- a ---- 1 --- [end]
44 | | /
45 -- b -- c---
47 The output will be:
50 a1$ <- end node was found
52 ac1= <- joins with the "a1" prefix
54 bc= <- joins with the "ac" prefix
55 """
56 buffer = StringIO()
57 cache = {}
59 for node in sorted(dafsa.root_node.children.values(), key=lambda n: n.character):
60 _node_to_string(node, "", buffer, cache)
61 return buffer.getvalue().strip()
64 def _to_words(data):
65 return [line.strip() for line in data.strip().split("\n")]
68 def _assert_dafsa(data, expected):
69 words = _to_words(data)
70 dafsa = Dafsa.from_tld_data(words)
72 expected = expected.strip()
73 expected = "\n".join([line.strip() for line in expected.split("\n")])
74 as_string = _dafsa_to_string(dafsa)
75 assert as_string == expected
78 class TestDafsa(unittest.TestCase):
79 def test_1(self):
80 _assert_dafsa(
81 """
83 ac1
84 acc1
85 bd1
86 bc1
87 bcc1
88 """,
89 """
92 a1$
94 ac1=
95 acc
96 acc1=
98 bc=
100 bd1=
101 """,
104 def test_2(self):
105 _assert_dafsa(
110 bbb1
111 """,
116 ab1$
120 bb1=
121 bbb=
122 """,
125 def test_3(self):
126 _assert_dafsa(
128 a.ca1
129 a.com1
130 c.corg1
131 b.ca1
132 b.com1
133 b.corg1
134 """,
139 a.ca
140 a.ca1
141 a.ca1$
142 a.co
143 a.com
144 a.com1=
148 b.ca=
149 b.co
150 b.com=
151 b.cor
152 b.corg
153 b.corg1=
157 c.co
158 c.cor=
159 """,
162 def test_4(self):
163 _assert_dafsa(
165 acom1
166 bcomcom1
167 acomcom1
168 """,
173 acom
174 acom1
175 acom1$
176 acomc
177 acomco
178 acomcom
179 acomcom1=
183 bcom
184 bcomc=
185 """,
188 def test_5(self):
189 _assert_dafsa(
191 a.d1
192 a.c.d1
193 b.d1
194 b.c.d1
195 """,
200 a.c.
201 a.c.d
202 a.c.d1
203 a.c.d1$
204 a.d=
207 """,
210 def test_6(self):
211 _assert_dafsa(
214 a661
216 b661
217 """,
222 a61$
224 a661=
227 """,
230 def test_7(self):
231 _assert_dafsa(
234 a6661
236 b6661
237 """,
242 a61$
244 a666
245 a6661=
248 """,
251 def test_8(self):
252 _assert_dafsa(
254 acc1
256 bccc1
257 """,
262 acc1
263 acc1$
266 bc1=
267 bcc=
268 """,
271 def test_9(self):
272 _assert_dafsa(
274 acc1
276 bcc1
277 """,
282 acc1
283 acc1$
286 bc1=
287 bcc=
288 """,
291 def test_10(self):
292 _assert_dafsa(
294 acc1
296 cccc1
297 """,
302 acc1
303 acc1$
306 cc1=
307 ccc=
308 """,
311 def test_11(self):
312 _assert_dafsa(
315 acc1
317 bcc1
318 """,
323 ac1$
325 acc1=
328 """,
331 def test_12(self):
332 _assert_dafsa(
334 acd1
335 bcd1
336 bcdd1
337 """,
342 acd1
343 acd1$
347 bcd1=
348 bcdd=
349 """,
352 def test_13(self):
353 _assert_dafsa(
356 acc1
358 bcc1
359 bccc1
360 """,
365 ac1$
367 acc1=
370 bc1=
371 bcc=
372 """,
375 def test_14(self):
376 _assert_dafsa(
378 acc1
379 acccc1
380 bcc1
381 bcccc1
382 bcccccc1
383 """,
388 acc1
389 acc1$
390 accc
391 acccc
392 acccc1=
396 bcc1=
397 bccc=
398 """,
401 def test_15(self):
402 _assert_dafsa(
406 acac1
407 """,
412 ac1$
414 acac
415 acac1=
418 """,
421 def test_16(self):
422 _assert_dafsa(
424 bat1
426 tbat1
427 """,
432 bat1
433 bat1$
437 """,
440 def test_17(self):
441 _assert_dafsa(
443 acow1
444 acat1
446 tcat1
447 acatcat1
448 """,
453 acat
454 acat1
455 acat1$
456 acatc
457 acatca
458 acatcat
459 acatcat1=
461 acow
462 acow1=
464 """,
467 def test_18(self):
468 _assert_dafsa(
471 abc1
472 abcxyzc1
473 """,
478 abc1
479 abc1$
480 abcx
481 abcxy
482 abcxyz
483 abcxyzc
484 abcxyzc1=
487 """,
490 def test_19(self):
491 _assert_dafsa(
493 a.z1
494 a.y1
495 c.z1
496 d.z1
497 d.y1
498 """,
503 a.y1
504 a.y1$
506 a.z1=
509 c.z=
512 """,
515 def test_20(self):
516 _assert_dafsa(
518 acz1
519 acy1
520 accz1
521 acccz1
522 bcz1
523 bcy1
524 bccz1
525 bcccz1
526 """,
531 accc
532 acccz
533 acccz1
534 acccz1$
535 accz=
537 acy1=
538 acz=
541 """,
545 if __name__ == "__main__":
546 mozunit.main()