This repository was archived by the owner on Feb 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuzz.jl
163 lines (155 loc) · 4.29 KB
/
fuzz.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
using ArrayViewsAPL, Base.Test
## Higher dimensional fuzz testing
A = reshape(1:625, 5, 5, 5, 5)
function test0(A)
## Indexing to extract a scalar
# Linear indexing
for i = 5:7:400
S = subview(A, i)
@test S[1] == A[i]
S = sliceview(A, i)
@test S[1] == A[i]
end
# Linear indexing in trailing dimensions
for j = 10:13:125, i = 1:5
S = subview(A, i, j)
@test S[1] == A[i, j]
S = sliceview(A, i, j)
@test S[1] == A[i, j]
end
for k = 3:2:25, j = 1:5, i = 1:5
S = subview(A, i, j, k)
@test S[1] == A[i, j, k]
S = sliceview(A, i, j, k)
@test S[1] == A[i, j, k]
end
# Scalar indexing
for l = 1:5, k = 1:5, j = 1:5, i = 1:5
S = subview(A, i, j, k, l)
@test S[1] == A[i,j,k,l]
S = sliceview(A, i, j, k, l)
@test S[1] == A[i,j,k,l]
end
end
test0(A)
if !isdefined(:eq_arrays_linear) # to make subsequent runs much faster
function eq_arrays_linear(S, B)
length(S) == length(B) || error("length mismatch")
alleq = true
for j = 1:length(B)
alleq &= S[j] == B[j]
end
@test alleq
nothing
end
end
## Generic indexing
index_choices = (1,3,5,2:3,3:3,1:2:5,4:-1:2,1:5,[1,4,3])
function test1(n)
for i = 1:n
ii = rand(1:length(index_choices), 4)
indexes = index_choices[ii]
B = A[indexes...]
S = subview(A, indexes...)
@test size(S) == size(B)
eq_arrays_linear(S, B)
S = sliceview(A, indexes...)
@test size(S) == size(B)[[map(x->!isa(x,Int), indexes)...]]
eq_arrays_linear(S, B)
end
end
test1(10^4)
index3_choices = (2,7,13,22,5:17,2:3:20,[18,14,9,11])
function test2(n)
indexes = Array(Any, 3)
for i = 1:n
ii = rand(1:length(index_choices), 2)
indexes[1] = index_choices[ii[1]]
indexes[2] = index_choices[ii[2]]
indexes[3] = index3_choices[rand(1:length(index3_choices))]
B = A[indexes...]
S = subview(A, indexes...)
@test size(S) == size(B)
eq_arrays_linear(S, B)
S = sliceview(A, indexes...)
@test size(S) == size(B)[[map(x->!isa(x,Int), indexes)...]]
eq_arrays_linear(S, B)
end
end
test2(10^4)
index2_choices = (13,42,55,99,111,88:99,2:5:54,[72,38,37,101])
function test3(n)
indexes = Array(Any, 2)
for i = 1:n
indexes[1] = index_choices[rand(1:length(index_choices))]
indexes[2] = index2_choices[rand(1:length(index2_choices))]
B = A[indexes...]
S = subview(A, indexes...)
@test size(S) == size(B)
eq_arrays_linear(S, B)
S = sliceview(A, indexes...)
@test size(S) == size(B)[[map(x->!isa(x,Int), indexes)...]]
eq_arrays_linear(S, B)
end
end
test3(10^4)
index1_choices = (25,63,128,344,599,121:315,43:51:600,[19,1,603,623,555,229])
function test4()
for indexes in index1_choices
B = A[indexes]
S = subview(A, indexes)
@test size(S) == size(B)
eq_arrays_linear(S, B)
S = sliceview(A, indexes)
@test size(S) == size(B)[[!isa(indexes,Int)]]
eq_arrays_linear(S, B)
end
end
test4()
function randsubset(indexes1)
n = length(indexes1)
indexesnew = Array(Any, n)
for i = 1:n
I = indexes1[i]
if isa(I, Int)
indexesnew[i] = 1
else
k = length(I)
r = rand()
if r < 0.3
l = rand(1:k)
indexesnew[i] = rand(1:k, l)
elseif r < 0.6
indexesnew[i] = rand(1:k)
else
i1 = rand(1:k)
i2 = rand(1:k)
indexesnew[i] = i1 >= i2 ? (i1:-1:i2) : (i1:1:i2)
end
end
end
indexes2 = [indexes1[k][indexesnew[k]] for k = 1:n]
indexesnew, indexes2
end
## Views of views
function test5(A, n)
for i = 1:n
ii = rand(1:length(index_choices), ndims(A))
indexes = index_choices[ii]
indexesnew, indexescomposed = randsubset(indexes)
B = A[indexescomposed...]
S1 = subview(A, indexes...)
S = subview(S1, indexesnew...)
@test size(S) == size(B)
eq_arrays_linear(S, B)
S1 = sliceview(A, indexes...)
T, N, P, IV = typeof(S1).parameters
keepdim = [map(x->!(x<:Int), IV)...]
S = sliceview(S1, indexesnew[keepdim]...)
@test size(S) == size(B)[[map(x->!isa(x,Int), indexescomposed)...]]
eq_arrays_linear(S, B)
end
end
# Test this one in just 3d because of the combinatorial explosion from views-of-views
A = reshape(1:125, 5, 5, 5)
test5(A, 10^5) # peaks out at more than 2000 separate stagedfunction generation events