4 ### Generate input script and expected output for dynamic array testing.
9 if SYS
.version_info
>= (3,): xrange = range
11 ###--------------------------------------------------------------------------
12 ### Command-line parsing.
15 def arg(default
= None):
24 SEED
= int(arg(str(R
.randrange(0, 1 << 32))), 0)
27 LINES
= int(arg(1000))
29 ###--------------------------------------------------------------------------
35 SCRIPT
= open('da.script', 'w')
36 WIN
= open('expout', 'w')
38 ###--------------------------------------------------------------------------
39 ### Utility functions.
44 Operation decorator. Add the following function to the operations table,
45 with the given probability WEIGHT. This works as follows: if TOTAL is the
46 total of all the WEIGHTs, then this operation has a probability of
47 WEIGHT/TOTAL of being selected.
50 OPS
.append((weight
, cls
))
55 """Return the next number in a simple sequence."""
61 """Return a short list of stuff to be added to the array."""
63 while R
.randrange(0, 4) < 3: seq
.append(serial())
67 """Return a length of stuff to be removed from the array."""
69 while R
.randrange(0, 4) < 3: n
+= 1
72 ###--------------------------------------------------------------------------
73 ### The actual operations.
78 SCRIPT
.write('push %d\n' % n
)
84 WIN
.write('*UFLOW*\n')
92 SCRIPT
.write('unshift %d\n' % n
)
96 SCRIPT
.write('shift\n')
98 WIN
.write('*UFLOW*\n')
102 WIN
.write('%d\n' % n
)
107 SCRIPT
.write('insert ' + ' '.join(['%d' % i for i in stuff
]) + '\n')
111 global ARRAY
# += is a binding occurrence
113 SCRIPT
.write('append ' + ' '.join(['%d' % i for i in stuff
]) + '\n')
118 if len(ARRAY
) < LINES
/10: return
120 SCRIPT
.write('delete %d\n' % n
)
121 if n
> len(ARRAY
): WIN
.write('*UFLOW*\n')
122 else: ARRAY
[0:n
] = []
125 if len(ARRAY
) < LINES
/10: return
127 SCRIPT
.write('reduce %d\n' % n
)
128 if n
> len(ARRAY
): WIN
.write('*UFLOW*\n')
129 else: ARRAY
[-n
:] = []
133 else: ix
= R
.randrange(0, len(ARRAY
))
134 while R
.randrange(0, 2) < 1: ix
+= 1
139 global ARRAY
# += is a binding occurrence
140 ix
, x
= mkindex(), serial()
141 SCRIPT
.write('set %d %d\n' %
(ix
, x
))
142 if ix
>= len(ARRAY
): ARRAY
+= [-1] * (ix
- len(ARRAY
) + 1)
147 SCRIPT
.write('get %d\n' % ix
)
148 if ix
>= len(ARRAY
): WIN
.write('*RANGE*\n')
149 else: WIN
.write('%d\n' % ARRAY
[ix
])
153 SCRIPT
.write('first\n')
154 if len(ARRAY
): WIN
.write('%d\n' % ARRAY
[0])
155 else: WIN
.write('*RANGE*\n')
158 SCRIPT
.write('last\n')
159 if len(ARRAY
): WIN
.write('%d\n' % ARRAY
[-1])
160 else: WIN
.write('*RANGE*\n')
164 SCRIPT
.write('show\n')
165 if not ARRAY
: WIN
.write('*EMPTY*\n')
166 else: WIN
.write(' '.join(['%d' % i for i in ARRAY
]) + '\n')
168 ###--------------------------------------------------------------------------
169 ### Generate the output.
174 for i
in xrange(LINES
):
175 OPTAB
[R
.randrange(0, len(OPTAB
))]()
180 open('da.seed', 'w').write('da-gtest seed = %08x\n' % SEED
)
182 ###----- That's all, folks --------------------------------------------------