[subtitles] Added tests to check correct behavior when no subtitles are
[youtube-dl] / devscripts / youtube_genalgo.py
1 #!/usr/bin/env python
2
3 # Generate youtube signature algorithm from test cases
4
5 import sys
6
7 tests = [
8     # 92 - vflQw-fB4 2013/07/17
9     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`~\"",
10      "mrtyuioplkjhgfdsazxcvbnq1234567890QWERTY}IOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]\"|:;"),
11     # 90
12     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<'`",
13      "mrtyuioplkjhgfdsazxcvbne1234567890QWER[YUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={`]}|"),
14     # 88
15     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[]}|:;?/>.<",
16      "J:|}][{=+-_)(*&;%$#@>MNBVCXZASDFGH^KLPOIUYTREWQ0987654321mnbvcxzasdfghrklpoiuytej"),
17     # 87
18     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$^&*()_-+={[]}|:;?/>.<",
19      "!?;:|}][{=+-_)(*&^$#@/MNBVCXZASqFGHJKLPOIUYTREWQ0987654321mnbvcxzasdfghjklpoiuytr"),
20     # 86 - vfl_ymO4Z 2013/06/27
21     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[|};?/>.<",
22      "ertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!/#$%^&*()_-+={[|};?@"),
23     # 85 - vflSAFCP9 2013/07/19
24     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?/>.<",
25      "ertyuiqplkjhgfdsazx$vbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#<%^&*()_-+={[};?/c"),
26     # 84
27     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!@#$%^&*()_-+={[};?>.<",
28      "<.>?;}[{=+-_)(*&^%$#@!MNBVCXZASDFGHJKLPOIUYTREWe098765432rmnbvcxzasdfghjklpoiuyt1"),
29     # 83 - vflcaqGO8 2013/07/11
30     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKJHGFDSAZXCVBNM!#$%^&*()_+={[};?/>.<",
31      "urty8ioplkjhgfdsazxcvbqm1234567S90QWERTYUIOPLKJHGFDnAZXCVBNM!#$%^&*()_+={[};?/>.<"),
32     # 82
33     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.<",
34      "Q>/?;}[{=+-(*<^%$#@!MNBVCXZASDFGHKLPOIUY8REWT0q&7654321mnbvcxzasdfghjklpoiuytrew9"),
35     # 81
36     ("qwertyuioplkjhgfdsazxcvbnm1234567890QWERTYUIOPLKHGFDSAZXCVBNM!@#$%^&*(-+={[};?/>.",
37      "urty8ioplkjhgfdsazxcvbqm1234567e90QWERTYUIOPLKHGFDSnZXCVBNM!@#$%^&*(-+={[};?/>."),
38 ]
39
40 def find_matching(wrong, right):
41     idxs = [wrong.index(c) for c in right]
42     return compress(idxs)
43     return ('s[%d]' % i for i in idxs)
44
45 def compress(idxs):
46     def _genslice(start, end, step):
47         starts = '' if start == 0 else str(start)
48         ends = ':%d' % (end+step)
49         steps = '' if step == 1 else (':%d' % step)
50         return 's[%s%s%s]' % (starts, ends, steps)
51
52     step = None
53     for i, prev in zip(idxs[1:], idxs[:-1]):
54         if step is not None:
55             if i - prev == step:
56                 continue
57             yield _genslice(start, prev, step)
58             step = None
59             continue
60         if i - prev in [-1, 1]:
61             step = i - prev
62             start = prev
63             continue
64         else:
65             yield 's[%d]' % prev
66     if step is None:
67         yield 's[%d]' % i
68     else:
69         yield _genslice(start, i, step)
70
71 def _assert_compress(inp, exp):
72     res = list(compress(inp))
73     if res != exp:
74         print('Got %r, expected %r' % (res, exp))
75         assert res == exp
76 _assert_compress([0,2,4,6], ['s[0]', 's[2]', 's[4]', 's[6]'])
77 _assert_compress([0,1,2,4,6,7], ['s[:3]', 's[4]', 's[6:8]'])
78 _assert_compress([8,0,1,2,4,7,6,9], ['s[8]', 's[:3]', 's[4]', 's[7:5:-1]', 's[9]'])
79
80 def gen(wrong, right, indent):
81     code = ' + '.join(find_matching(wrong, right))
82     return 'if len(s) == %d:\n%s    return %s\n' % (len(wrong), indent, code)
83
84 def genall(tests):
85     indent = ' ' * 8
86     return indent + (indent + 'el').join(gen(wrong, right, indent) for wrong,right in tests)
87
88 def main():
89     print(genall(tests))
90
91 if __name__ == '__main__':
92     main()