-
Notifications
You must be signed in to change notification settings - Fork 1
/
shrink.jl
237 lines (211 loc) · 6.36 KB
/
shrink.jl
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
function shrink!(ts::TestState)
isnothing(ts.result) && isnothing(ts.target_err) && return
attempt = @something err_choices(ts) ts.result
improved = true
while improved
improved = false
@debug "Shrinking through dropping values"
for k in UInt.((8,4,2,1))
while true
res = shrink_remove(ts, attempt, k)
attempt = @something res break
improved = true
end
end
@debug "Shrinking through floating point shrinks"
res = shrink_float(ts, attempt)
if !isnothing(res)
attempt = @something res
improved = true
end
@debug "Shrinking through setting zero"
for k in UInt.((8,4,2))
while true
res = shrink_zeros(ts, attempt, k)
attempt = @something res break
improved = true
end
end
@debug "Shrinking through shrinking elements"
res = shrink_reduce(ts, attempt)
if !isnothing(res)
attempt = @something res
improved = true
end
@debug "Shrinking through sorting views"
for k in UInt.((8,4,2))
while true
res = shrink_sort(ts, attempt, k)
attempt = @something res break
improved = true
end
end
@debug "Shrinking through swapping elements"
for k in UInt.((2,1))
while true
res = shrink_swap(ts, attempt, k)
attempt = @something res break
improved = true
end
end
@debug "Shrinking through redistributing in a window"
for k in UInt.((2,1))
while true
res = shrink_redistribute(ts, attempt, k)
attempt = @something res break
improved = true
end
end
end
end
"""
shrink_remove(ts::TestState, attempt::Vector{UInt64}, k::UInt)
Try to shrink `attempt` by removing `k` elements at a time
"""
function shrink_remove(ts::TestState, attempt::Vector{UInt64}, k::UInt)::Option{Vector{UInt64}}
k > length(attempt) && return nothing
valid = ( (j, j+k-1) for j in (length(attempt)-k+1):-1:1 )
for (x,y) in valid
head, _, tail = windows(attempt, x, y)
new = UInt[head; tail]
if consider(ts, new)
return Some(new)
elseif x > 1 && new[x-1] > 0
new[x-1] -= 1
if consider(ts, new)
return Some(new)
end
end
end
nothing
end
function shrink_float(ts::TestState, attempt::Vector{UInt64})
new = copy(attempt)
for idx in eachindex(new)
old = new[idx]
old_float = reinterpret(Float64, old)
if isnan(old_float)
n_val = copysign(Inf, old_float)
new[idx] = reinterpret(UInt64, n_val)
if consider(ts, new)
return Some(new)
end
end
end
nothing
end
"""
shrink_zeros(::TestSTate, attempt::Vector{UInt64}, k::UInt)
Try to shrink `attempt` by setting `k` elements at a time to zero.
"""
function shrink_zeros(ts::TestState, attempt::Vector{UInt64}, k::UInt)
k >= length(attempt) && return nothing
valid = ( (j, j+k) for j in (length(attempt)-k):-1:1 )
for (x,y) in valid
if all(iszero, @view attempt[x:y-1])
continue
end
head, _, tail = windows(attempt, x, y)
new = [head; zeros(UInt64, y-x); tail]
if consider(ts, new)
return Some(new)
end
end
nothing
end
"""
shrink_reduce(::TestState, attempt::Vector{UInt64})
Try to shrink `attempt` by making the elements smaller.
"""
function shrink_reduce(ts::TestState, attempt::Vector{UInt64})
new = copy(attempt)
for i in reverse(1:length(attempt))
res = bin_search_down(0, new[i], n -> begin
new[i] = n
consider(ts, new)
end)
new[i] = @something res Some(new[i])
end
if new == attempt
nothing
else
Some(new)
end
end
"""
shrink_sort(::TestState, attempt::Vector{UInt64}, k::UInt)
Try to shrink `attempt` by sorting `k` contiguous elements at a time.
"""
function shrink_sort(ts::TestState, attempt::Vector{UInt64}, k::UInt)
k >= length(attempt) && return nothing
valid = ( (j-k+1, j) for j in length(attempt):-1:k)
for (x,y) in valid
head, middle, tail = windows(attempt, x, y)
issorted(middle) && continue
newmid = sort(middle)
new = [head; newmid; tail]
consider(ts, new) && return Some(new)
end
nothing
end
"""
shrink_swap(::TestState, attempt::Vector{UInt64}, k::UInt)
Try to shrink `attempt` by swapping two elements length `k` apart.
"""
function shrink_swap(ts::TestState, attempt::Vector{UInt64}, k::UInt)
valid = ( (j-k+1, j) for j in (length(attempt):-1:k))
for (x,y) in valid
attempt[x] == attempt[y] && continue
new = copy(attempt)
new[y] = attempt[x]
res = bin_search_down(0, attempt[y], n -> begin
new[x] = n
consider(ts, new)
end)
if !isnothing(res)
new[x] = @something res
return Some(new)
end
end
nothing
end
"""
shrink_redistribute(ts::TestState, attempt::Vector{UInt64}, k::UInt)
Try to shrink `attempt` by redistributing value between two elements length `k` apart.
"""
function shrink_redistribute(ts::TestState, attempt::Vector{UInt64}, k::UInt)
length(attempt) < k && return nothing
new = copy(attempt)
valid = ( (j,j+k) for j in 1:(length(attempt)-k))
for (x,y) in valid
iszero(attempt[x]) && continue
res = bin_search_down(0, attempt[x], n -> begin
new[x] = n
new[y] = attempt[x] + attempt[y] - n
consider(ts, new)
end)
if !isnothing(res)
v = @something res
new[x] = v
new[y] = attempt[x] + attempt[y] - v
end
end
if new == attempt
return nothing
else
return Some(new)
end
end
function bin_search_down(low, high, pred)
pred(low) && return Some(low)
!pred(high) && return nothing
while (low+1) < high
mid = low + (high - low)÷2
if pred(mid)
high = mid
else
low = mid
end
end
Some(high)
end