4 ### Generate input script and expected output for dynamic array testing.
9 ###--------------------------------------------------------------------------
10 ### Command-line parsing.
13 def arg(default
= None):
22 SEED
= int(arg(str(R
.randrange(0, 1 << 32))), 0)
25 LINES
= int(arg(1000))
27 ###--------------------------------------------------------------------------
33 SCRIPT
= open('da.script', 'w')
34 WIN
= open('expout', 'w')
36 ###--------------------------------------------------------------------------
37 ### Utility functions.
42 Operation decorator. Add the following function to the operations table,
43 with the given probability WEIGHT. This works as follows: if TOTAL is the
44 total of all the WEIGHTs, then this operation has a probability of
45 WEIGHT/TOTAL of being selected.
48 OPS
.append((weight
, cls
))
53 """Return the next number in a simple sequence."""
59 """Return a short list of stuff to be added to the array."""
61 while R
.randrange(0, 4) < 3: seq
.append(serial())
65 """Return a length of stuff to be removed from the array."""
67 while R
.randrange(0, 4) < 3: n
+= 1
70 ###--------------------------------------------------------------------------
71 ### The actual operations.
76 SCRIPT
.write('push %d\n' % n
)
82 WIN
.write('*UFLOW*\n')
90 SCRIPT
.write('unshift %d\n' % n
)
94 SCRIPT
.write('shift\n')
96 WIN
.write('*UFLOW*\n')
100 WIN
.write('%d\n' % n
)
105 SCRIPT
.write('insert ' + ' '.join(['%d' % i for i in stuff
]) + '\n')
109 global ARRAY
# += is a binding occurrence
111 SCRIPT
.write('append ' + ' '.join(['%d' % i for i in stuff
]) + '\n')
116 if len(ARRAY
) < LINES
/10: return
118 SCRIPT
.write('delete %d\n' % n
)
119 if n
> len(ARRAY
): WIN
.write('*UFLOW*\n')
120 else: ARRAY
[0:n
] = []
123 if len(ARRAY
) < LINES
/10: return
125 SCRIPT
.write('reduce %d\n' % n
)
126 if n
> len(ARRAY
): WIN
.write('*UFLOW*\n')
127 else: ARRAY
[-n
:] = []
131 else: ix
= R
.randrange(0, len(ARRAY
))
132 while R
.randrange(0, 2) < 1: ix
+= 1
137 global ARRAY
# += is a binding occurrence
138 ix
, x
= mkindex(), serial()
139 SCRIPT
.write('set %d %d\n' %
(ix
, x
))
140 if ix
>= len(ARRAY
): ARRAY
+= [-1] * (ix
- len(ARRAY
) + 1)
145 SCRIPT
.write('get %d\n' % ix
)
146 if ix
>= len(ARRAY
): WIN
.write('*RANGE*\n')
147 else: WIN
.write('%d\n' % ARRAY
[ix
])
151 SCRIPT
.write('first\n')
152 if len(ARRAY
): WIN
.write('%d\n' % ARRAY
[0])
153 else: WIN
.write('*RANGE*\n')
156 SCRIPT
.write('last\n')
157 if len(ARRAY
): WIN
.write('%d\n' % ARRAY
[-1])
158 else: WIN
.write('*RANGE*\n')
162 SCRIPT
.write('show\n')
163 if not ARRAY
: WIN
.write('*EMPTY*\n')
164 else: WIN
.write(' '.join(['%d' % i for i in ARRAY
]) + '\n')
166 ###--------------------------------------------------------------------------
167 ### Generate the output.
172 for i
in xrange(LINES
):
173 OPTAB
[R
.randrange(0, len(OPTAB
))]()
178 open('da.seed', 'w').write('da-gtest seed = %08x\n' % SEED
)
180 ###----- That's all, folks --------------------------------------------------