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
|
#!/usr/bin/python3
"""Generate a few strings with unbalanced distributions to test the
regeneration of the Huffman tree when it gets too deep.
USAGE: make-test-vectors DIR
This will fill up DIR with test files.
"""
import sys
import random
from collections import defaultdict
if '--help' in sys.argv or '-h' in sys.argv or len(sys.argv) != 2:
print(__doc__)
exit(len(sys.argv) != 2)
DIR = sys.argv[1]
SIZE = (1 << 17) + (23) # two and a bit blocks
SIZE_NAME = "128k+"
# SIZE = (1 << 16)
# SIZE_NAME = "64"
random.seed(1)
def squares(n):
array = []
for i in range(n):
a = random.random()
b = random.random()
array.append(int(a * b * 256))
return bytes(array)
def skewed_choices(n):
b = list(range(256))
array = random.choices(b, weights=b, k=n)
return bytes(array)
def fib_shuffle(n):
array = []
a, b = 1, 1
for i in range(100):
array.extend([i] * a)
a, b = a + b, a
if len(array) > 1000000:
break
random.shuffle(array)
return bytes(array[:n])
def exp_shuffle(n):
array = []
for i in range(256):
array.extend([i] * int(1.04 ** i))
if len(array) > 1000000:
break
random.shuffle(array)
return bytes(array[:n])
def and_rand(n):
array = []
for i in range(n):
a = random.randrange(256)
b = random.randrange(256)
array.append(a & b)
return bytes(array)
def betavar(n, a, b):
array = []
for i in range(n):
x = random.betavariate(a, b)
array.append(int(x * 255.999999999999))
return bytes(array)
def repeated_alphabet(n):
a = b'abcdefghijklmnopqrstuvwxyz'
na = n // len(a) + 1
s = a * na
return s[:n]
def decayed_alphabet(n):
s = list(repeated_alphabet(n))
for i in range(256):
j = random.randrange(n)
s[j] = i
return bytes(s)
def trigram_model(n):
with open(__file__, 'rb') as f:
data = f.read()
lut = defaultdict(list)
for a, b, c in zip(data, data[1:], data[2:]):
k = bytes([a, b])
lut[k].append(c)
k = random.choice(list(lut.keys()))
s = []
p = k[1]
for i in range(n + 10):
c = random.choice(lut[k])
s.append(c)
k = bytes([p, c])
p = c
return bytes(s[10:])
def trigram_sum_model(n):
with open(__file__, 'rb') as f:
data = f.read()
lut = [[random.randrange(256)] for i in range(512)]
for a, b, c in zip(data, data[1:], data[2:]):
lut[a + b].append(c)
s = []
i = random.randrange(len(data) - 1)
a = data[i]
b = data[i + 1]
for i in range(n + 10):
x = lut[a + b]
c = random.choice(x)
s.append(c)
a = b
b = c
return bytes(s[10:])
def the_classics():
# this used to be main()
sq = squares(SIZE)
ch = skewed_choices(SIZE)
fs = fib_shuffle(SIZE)
es = exp_shuffle(SIZE)
ar = and_rand(SIZE)
bv1 = betavar(SIZE, 0.1, 1.5)
bv2 = betavar(SIZE, 0.5, 2.0)
bv3 = betavar(SIZE, 0.05, 0.05)
print("n sq ch fs es")
for i in range(256):
print(f"{i:3} {sq.count(i):5} {ch.count(i):5} "
f"{fs.count(i):5} {es.count(i):5}"
f"{ar.count(i):5} {bv1.count(i):5}"
f"{bv2.count(i):5} {bv3.count(i):5}"
)
for series, fn in ((sq, "square_series"),
(ch, "skewed_choices"),
(fs, "fib_shuffle"),
(es, "exp_shuffle"),
(ar, "and_rand"),
(bv1, "beta-variate1"),
(bv2, "beta-variate2"),
(bv3, "beta-variate3"),
):
with open(f"{DIR}/{fn}-{SIZE_NAME}", "wb") as f:
f.write(series)
def main():
if True:
the_classics()
for series, fn in ((decayed_alphabet(SIZE), "decayed_alphabet"),
(trigram_model(SIZE), "trigram"),
(trigram_sum_model(SIZE), "trigram_sum"),
):
with open(f"{DIR}/{fn}_{SIZE_NAME}", "wb") as f:
f.write(series)
main()
|