*/t/*.py: Fix the various testing scripts for Python 3 compatibility.
[mLib] / struct / t / sym-gtest.py
CommitLineData
7cf5c72a
MW
1#! /usr/bin/python
2### -*-python-*-
3###
4### Generate input script and expected output for hash tables.
5
6import sys as SYS
7import random as R
8
37342632
MW
9if SYS.version_info >= (3,): xrange = range
10
7cf5c72a
MW
11###--------------------------------------------------------------------------
12### Command-line parsing.
13
14SYS.argv[0:1] = []
15def arg(default = None):
16 if len(SYS.argv):
17 r = SYS.argv[0]
18 SYS.argv[0:1] = []
19 return r
20 else:
21 return default
22
23R.seed(None)
24SEED = int(arg(str(R.randrange(0, 1 << 32))), 0)
25R.seed(SEED)
26
27LINES = int(arg(1000))
28
29###--------------------------------------------------------------------------
30### Word list.
31
32def word():
33 def char(): return 'abcdefghijklmnopqrstuvwxyz'[R.randrange(0, 26)]
34 word = char() + char() + char()
35 while R.randrange(0, 6) != 0: word += char()
36 return word
37
38## for i in ['/usr/share/dict/words', '/usr/dict/words']:
39## try:
40## WORDS = [line[:-1] for line in open(i)]
41## raise Exception
42## except:
43## pass
44## else:
45## def word():
46## return WORDS[R.randrange(0, len(WORDS))]
47## break
48
49###--------------------------------------------------------------------------
50### Initialization.
51
52SERIAL = 1
53MAP = {}
54
55SCRIPT = open('sym.script', 'w')
56WIN = open('expout', 'w')
57
58###--------------------------------------------------------------------------
59### Utility functions.
60
61OPS = []
62def op(weight):
63 """
64 Operation decorator. Add the following function to the operations table,
65 with the given probability WEIGHT. This works as follows: if TOTAL is the
66 total of all the WEIGHTs, then this operation has a probability of
67 WEIGHT/TOTAL of being selected.
68 """
69 def _(cls):
70 OPS.append((weight, cls))
71 return cls
72 return _
73
74def serial():
75 """Return the next number in a simple sequence."""
76 global SERIAL
77 SERIAL += 1
78 return SERIAL - 1
79
80###--------------------------------------------------------------------------
81### The actual operations.
82
83@op(10)
84def op_set():
85 w = word()
86 n = serial()
87 SCRIPT.write('set %s %d\n' % (w, n))
88 MAP[w] = n
89
90@op(10)
91def op_get():
92 w = word()
93 try:
94 WIN.write('%d\n' % MAP[w])
95 except KeyError:
96 if R.randrange(8): return
97 WIN.write('*MISSING*\n')
98 SCRIPT.write('get %s\n' % (w))
99
100@op(10)
101def op_del():
102 w = word()
103 try:
104 del MAP[w]
105 except KeyError:
106 if R.randrange(8): return
107 WIN.write('*MISSING*\n')
108 SCRIPT.write('del %s\n' % (w))
109
110@op(4)
111def op_count():
112 SCRIPT.write('count\n')
113 WIN.write('%d\n' % len(MAP))
114
115@op(1)
116def op_show():
117 SCRIPT.write('show\n')
118 if not MAP:
119 WIN.write('*EMPTY*\n')
120 else:
37342632 121 kk = list(MAP.keys())
7cf5c72a
MW
122 kk.sort()
123 WIN.write(' '.join(['%s:%d' % (k, MAP[k]) for k in kk]) + '\n')
124
125###--------------------------------------------------------------------------
126### Generate the output.
127
128OPTAB = []
129for p, func in OPS:
130 OPTAB += [func] * p
131for i in xrange(LINES):
132 OPTAB[R.randrange(0, len(OPTAB))]()
133op_show()
134
135SCRIPT.close()
136WIN.close()
137open('sym.seed', 'w').write('sym-gtest seed = %08x\n' % SEED)
138
139###----- That's all, folks --------------------------------------------------