-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckbinary.py
58 lines (52 loc) · 1.35 KB
/
checkbinary.py
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
#!/usr/bin/env python3
import parenlang
from parenlang.util import random_paren
import random
hashset = set()
parenset = set()
def test_paren_hash(p):
b = str(p.bintree_form())
r = p.binary_repr()
s = p.shorthand_form()
h = hash(s)
if h in hashset:
print('collision', str(s), '{:016X}'.format(h))
else:
hashset.add(h)
if s in parenset:
print('paren collision', str(s), '{:016X}'.format(h))
else:
parenset.add(s)
h = '{:016X}'.format(h)
s = str(s)
print(' | '.join([
# binary_repr.rjust(16,'0'),
#str(p).ljust(60),
#b.ljust(60),
#r.rjust(16),
s.ljust(100),
#'N' if b==s else 'Y',
h,
]))
for i in range(256):
binary_repr = '{0:016b}'.format(i)
try:
p = parenlang.BinaryReprParser(binary_repr).parse()
except:
# print('0'*(8-len(binary_repr))+binary_repr, 'illegal', 'illegal', binary_repr, 'illegal', '-')
continue
test_paren_hash(p)
# for i in range(65536):
# binary_repr = '{0:016b}'.format(i)
# try:
# p = parenlang.BinaryReprParser(binary_repr).parse()
# except:
# # print('0'*(8-len(binary_repr))+binary_repr, 'illegal', 'illegal', binary_repr, 'illegal', '-')
# continue
# test_paren_hash(p)
trials = 5000
for i in range(trials):
p_str = '(' + random_paren(98) + ')'
p = parenlang.Parser().parse(p_str)[0]
# p = parenlang.FlatParser().parse(p_str).collect()[0]
test_paren_hash(p)