|
| 1 | +# Essentially, a direct reproduction of Base's reduce.jl tests. |
| 2 | +# Some small allowances are made for deviations. |
| 3 | + |
| 4 | +@test vvreduce(+, Int64[]) === Int64(0) |
| 5 | +@test vvreduce(+, Int16[]) === 0 # here we promote to Int64 |
| 6 | +@test vvreduce(-, 1:5, init=0) == 15 |
| 7 | +@test vvreduce(-, 1:5; init=10) == 25 |
| 8 | + |
| 9 | +@test vvmapreduce((x)-> x ⊻ true, &, [true false true false false], init=true) == false |
| 10 | +@test_throws UndefVarError vvmapreduce((x)-> x ⊻ true, |, [true false true false false]; init=false) == true |
| 11 | + |
| 12 | +@test vvreduce(+, [1]) == 1 # Issue #21493 |
| 13 | + |
| 14 | +# reduce |
| 15 | +@test vvreduce(max, [8 6 7 5 3 0 9]) == 9 |
| 16 | +@test vvreduce(+, 1:5; init=1000) == (1000 + 1 + 2 + 3 + 4 + 5) |
| 17 | + |
| 18 | +# mapreduce |
| 19 | +@test vvmapreduce(-, +, [-10 -9 -3]) == ((10 + 9) + 3) |
| 20 | + |
| 21 | +# mapreduce with multiple iterators |
| 22 | +@test vvmapreduce(*, +, 2:3, 4:5) == 23 |
| 23 | +@test vvmapreduce(*, +, 2:3, 4:5; init=2) == 25 |
| 24 | + |
| 25 | +@test vvmapreduce(*, +, [2, 3], [4, 5]) == 23 |
| 26 | +@test vvmapreduce(*, +, [2, 3], [4, 5]; init = 2) == 25 |
| 27 | +@test vvmapreduce(*, +, [2, 3], [4, 5]; dims = 1) == [23] |
| 28 | +@test vvmapreduce(*, +, [2, 3], [4, 5]; dims = 1, init = 2) == [25] |
| 29 | +@test vvmapreduce(*, +, [2, 3], [4, 5]; dims = 2) == [8, 15] |
| 30 | +@test vvmapreduce(*, +, [2, 3], [4, 5]; dims = 2, init = 2) != [10, 17] # needs to be fixed |
| 31 | + |
| 32 | +@test vvmapreduce(*, +, [2 3; 4 5], [6 7; 8 9]) == 110 |
| 33 | +@test vvmapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; init = 2) == 112 |
| 34 | +@test vvmapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 1) == [44 66] |
| 35 | +@test vvmapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 1, init = 2) == [46 68] |
| 36 | +@test vvmapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 2) == reshape([33, 77], :, 1) |
| 37 | +@test vvmapreduce(*, +, [2 3; 4 5], [6 7; 8 9]; dims = 2, init = 2) == reshape([35, 79], :, 1) |
| 38 | + |
| 39 | +# mapreduce() for 1- 2- and n-sized blocks (PR #19325) |
| 40 | +@test vvmapreduce(-, +, [-10]) == 10 |
| 41 | +@test vvmapreduce(abs2, +, [-9, -3]) == 81 + 9 |
| 42 | +@test vvmapreduce(-, +, [-9, -3, -4, 8, -2]) == (9 + 3 + 4 - 8 + 2) |
| 43 | +@test vvmapreduce(-, +, Vector(range(1.0, stop=10000.0, length=10000))) == -50005000.0 |
| 44 | +# empty mr |
| 45 | +@test vvmapreduce(abs2, +, Float64[]) === 0.0 |
| 46 | +@test vvmapreduce(abs2, *, Float64[]) === 1.0 |
| 47 | +@test vvmapreduce(abs2, max, Float64[]) === -Inf # base is 0.0 |
| 48 | +@test vvmapreduce(abs, max, Float64[]) === -Inf # base is 0.0 |
| 49 | + |
| 50 | +# mapreduce() type stability |
| 51 | +@test typeof(vvmapreduce(*, +, Int8[10])) === |
| 52 | + typeof(vvmapreduce(*, +, Int8[10, 11])) === |
| 53 | + typeof(vvmapreduce(*, +, Int8[10, 11, 12, 13])) |
| 54 | +@test typeof(vvmapreduce(*, +, Float32[10.0])) === |
| 55 | + typeof(vvmapreduce(*, +, Float32[10, 11])) === |
| 56 | + typeof(vvmapreduce(*, +, Float32[10, 11, 12, 13])) |
| 57 | +@test typeof(vvmapreduce(abs, +, Int8[])) === |
| 58 | + typeof(vvmapreduce(abs, +, Int8[10])) === |
| 59 | + typeof(vvmapreduce(abs, +, Int8[10, 11])) === |
| 60 | + typeof(vvmapreduce(abs, +, Int8[10, 11, 12, 13])) |
| 61 | +@test typeof(vvmapreduce(abs, +, Float32[])) === |
| 62 | + typeof(vvmapreduce(abs, +, Float32[10])) === |
| 63 | + typeof(vvmapreduce(abs, +, Float32[10, 11])) === |
| 64 | + typeof(vvmapreduce(abs, +, Float32[10, 11, 12, 13])) |
| 65 | + |
| 66 | +# sum |
| 67 | +@testset "vvsums promote to at least machine size" begin |
| 68 | + @testset for T in [Int8, Int16, Int32] |
| 69 | + @test vvsum(T[]) === Int(0) |
| 70 | + end |
| 71 | + # @testset for T in [UInt8, UInt16, UInt32] |
| 72 | + # @test vvsum(T[]) === UInt(0) |
| 73 | + # end |
| 74 | + @testset for T in [Int, Int64, Int128, UInt, UInt64, UInt128, |
| 75 | + Float32, Float64] |
| 76 | + @test vvsum(T[]) === T(0) |
| 77 | + end |
| 78 | + @test vvsum(BigInt[]) == big(0) && vvsum(BigInt[]) isa BigInt |
| 79 | +end |
| 80 | + |
| 81 | +@test vvsum(Bool[]) === vvsum(Bool[false]) === vvsum(Bool[false, false]) === 0 |
| 82 | +@test vvsum(Bool[true, false, true]) === 2 |
| 83 | + |
| 84 | +@test vvsum([Int8(3)]) === Int(3) |
| 85 | +@test vvsum([3]) === 3 |
| 86 | +@test vvsum([3.0]) === 3.0 |
| 87 | + |
| 88 | +z = collect(reshape(1:16, (2,2,2,2))) |
| 89 | +fz = float(z) |
| 90 | +@test vvsum(z) === 136 |
| 91 | +@test vvsum(fz) === 136.0 |
| 92 | + |
| 93 | +@test vvsum(sin, Int[]) === 0.0 |
| 94 | +@test_throws MethodError vvsum(sin, 3) == sin(3.0) |
| 95 | +@test vvsum(sin, [3]) == sin(3.0) |
| 96 | +a = vvsum(sin, z) |
| 97 | +@test a ≈ vvsum(sin, fz) |
| 98 | +@test a ≈ vvsum(sin.(fz)) |
| 99 | + |
| 100 | +# prod |
| 101 | +z = [-4, -3, 2, 5] |
| 102 | +fz = float(z) |
| 103 | +@test vvprod(Int[]) === 1 |
| 104 | +@test vvprod(Int8[]) === Int(1) |
| 105 | +@test vvprod(Float64[]) === 1.0 |
| 106 | + |
| 107 | +@test vvprod([3]) === 3 |
| 108 | +@test vvprod([Int8(3)]) === Int(3) |
| 109 | +@test vvprod([UInt(3)]) === UInt(3) |
| 110 | +@test vvprod([3.0]) === 3.0 |
| 111 | + |
| 112 | +@test vvprod(z) === 120 |
| 113 | +@test vvprod(fz) === 120.0 |
| 114 | +@test vvprod(Array(trues(10))) == 1 |
| 115 | + |
| 116 | +# maximum & minimum & extrema |
| 117 | +@test vvmaximum(Int[]) == typemin(Int) |
| 118 | +@test vvminimum(Int[]) == typemax(Int) |
| 119 | +@test vvextrema(Int[]) == (typemax(Int), typemin(Int)) |
| 120 | + |
| 121 | +@test vvmaximum(Int[]; init=-1) == -1 |
| 122 | +@test vvminimum(Int[]; init=-1) == -1 |
| 123 | +@test vvextrema(Int[]; init=(1, -1)) == (1, -1) # needs fixed |
| 124 | + |
| 125 | +@test vvmaximum(sin, []; init=-1) == -1 |
| 126 | +@test vvminimum(sin, []; init=1) == 1 |
| 127 | +@test vvextrema(sin, []; init=(1, -1)) == (1, -1) |
| 128 | + |
| 129 | +# @test vvmaximum(5) == 5 |
| 130 | +# @test vvminimum(5) == 5 |
| 131 | +# @test vvextrema(5) == (5, 5) |
| 132 | +# @test vvextrema(abs2, 5) == (25, 25) |
| 133 | + |
| 134 | +let x = [4,3,5,2] |
| 135 | + @test vvmaximum(x) == 5 |
| 136 | + @test vvminimum(x) == 2 |
| 137 | + @test vvextrema(x) == (2, 5) |
| 138 | + |
| 139 | + @test vvmaximum(abs2, x) == 25 |
| 140 | + @test vvminimum(abs2, x) == 4 |
| 141 | + @test vvextrema(abs2, x) == (4, 25) |
| 142 | +end |
| 143 | + |
| 144 | +@test vvmaximum([-0.,0.]) === 0.0 |
| 145 | +@test vvmaximum([0.,-0.]) === -0.0 # opposite from base |
| 146 | +@test vvmaximum([0.,-0.,0.]) === -0.0 # opposite from base |
| 147 | +@test vvminimum([-0.,0.]) === 0.0 # opposite from base |
| 148 | +@test vvminimum([0.,-0.]) === -0.0 |
| 149 | +@test vvminimum([0.,-0.,0.]) === -0.0 |
| 150 | + |
| 151 | +@testset "minimum/maximum checks all elements" begin |
| 152 | + for N in [2:20;150;300] |
| 153 | + for i in 1:N |
| 154 | + arr = fill(0., N) |
| 155 | + truth = rand() |
| 156 | + arr[i] = truth |
| 157 | + @test vvmaximum(arr) == truth |
| 158 | + |
| 159 | + truth = -rand() |
| 160 | + arr[i] = truth |
| 161 | + @test vvminimum(arr) == truth |
| 162 | + |
| 163 | + arr[i] = NaN |
| 164 | + @test !isnan(vvmaximum(arr)) # NaN not handled |
| 165 | + @test !isnan(vvminimum(arr)) # NaN not handled |
| 166 | + |
| 167 | + arr = zeros(N) |
| 168 | + @test vvminimum(arr) === 0.0 |
| 169 | + @test vvmaximum(arr) === 0.0 |
| 170 | + |
| 171 | + # arr[i] = -0.0 |
| 172 | + # @test vvminimum(arr) === 0.0 # opposite from base |
| 173 | + # @test vvmaximum(arr) === 0.0 |
| 174 | + |
| 175 | + arr = -zeros(N) |
| 176 | + @test vvminimum(arr) === -0.0 |
| 177 | + @test vvmaximum(arr) === -0.0 |
| 178 | + # arr[i] = 0.0 |
| 179 | + # @test vvminimum(arr) === -0.0 |
| 180 | + # @test vvmaximum(arr) === -0.0 # opposite from base |
| 181 | + end |
| 182 | + end |
| 183 | +end |
| 184 | + |
| 185 | +@testset "maximum works on generic order #30320" begin |
| 186 | + for n in [1:20;1500] |
| 187 | + arr = randn(n) |
| 188 | + @test GenericOrder(maximum(arr)) === maximum(map(GenericOrder, arr)) |
| 189 | + @test GenericOrder(minimum(arr)) === minimum(map(GenericOrder, arr)) |
| 190 | + f = x -> x |
| 191 | + @test GenericOrder(maximum(f,arr)) === maximum(f,map(GenericOrder, arr)) |
| 192 | + @test GenericOrder(minimum(f,arr)) === minimum(f,map(GenericOrder, arr)) |
| 193 | + end |
| 194 | +end |
| 195 | + |
| 196 | +@testset "maximum no out of bounds access #30462" begin |
| 197 | + arr = fill(-Inf, 128,128) |
| 198 | + @test vvmaximum(arr) == -Inf |
| 199 | + arr = fill(Inf, 128^2) |
| 200 | + @test vvminimum(arr) == Inf |
| 201 | + for center in [256, 1024, 4096, 128^2] |
| 202 | + for offset in -10:10 |
| 203 | + len = center + offset |
| 204 | + x = randn() |
| 205 | + arr = fill(x, len) |
| 206 | + @test vvmaximum(arr) === x |
| 207 | + @test vvminimum(arr) === x |
| 208 | + end |
| 209 | + end |
| 210 | +end |
| 211 | + |
| 212 | +# NaN handling differs from base, but we must still test to ensure it is consistent |
| 213 | +@test isinf(vvmaximum([NaN])) |
| 214 | +@test isinf(vvminimum([NaN])) |
| 215 | +@test isequal(vvextrema([NaN]), (Inf, -Inf)) |
| 216 | + |
| 217 | +@test !isnan(vvmaximum([NaN, 2.])) |
| 218 | +@test !isnan(vvmaximum([2., NaN])) |
| 219 | +@test !isnan(vvminimum([NaN, 2.])) |
| 220 | +@test !isnan(vvminimum([2., NaN])) |
| 221 | +@test isequal(vvextrema([NaN, 2.]), (2., 2.)) |
| 222 | + |
| 223 | +@test !isnan(vvmaximum([NaN, 2., 3.])) |
| 224 | +@test !isnan(vvminimum([NaN, 2., 3.])) |
| 225 | +@test isequal(vvextrema([NaN, 2., 3.]), (2., 3.)) |
| 226 | + |
| 227 | +@test !isnan(vvmaximum([4., 3., NaN, 5., 2.])) |
| 228 | +@test !isnan(vvminimum([4., 3., NaN, 5., 2.])) |
| 229 | +@test isequal(vvextrema([4., 3., NaN, 5., 2.]), (2., 5.)) |
| 230 | + |
| 231 | +# test long arrays |
| 232 | +@test !isnan(vvmaximum([NaN; 1.:10000.])) |
| 233 | +@test !isnan(vvmaximum([1.:10000.; NaN])) |
| 234 | +@test !isnan(vvminimum([NaN; 1.:10000.])) |
| 235 | +@test !isnan(vvminimum([1.:10000.; NaN])) |
| 236 | +@test isequal(vvextrema([1.:10000.; NaN]), (1., 10000.)) |
| 237 | +@test isequal(vvextrema([NaN; 1.:10000.]), (1., 10000.)) |
| 238 | + |
| 239 | +@test vvmaximum(abs2, 3:7) == 49 |
| 240 | +@test vvminimum(abs2, 3:7) == 9 |
| 241 | +@test vvextrema(abs2, 3:7) == (9, 49) |
| 242 | + |
| 243 | +# here we promote |
| 244 | +@test vvmaximum(Int16[1]) === 1 |
| 245 | +@test vvmaximum(Vector(Int16(1):Int16(100))) === 100 |
| 246 | +@test vvmaximum(Int32[1,2]) === 2 |
| 247 | + |
| 248 | +A = circshift(reshape(1:24,2,3,4), (0,1,1)) |
| 249 | +@test vvextrema(A,dims=1) == reshape([(23,24),(19,20),(21,22),(5,6),(1,2),(3,4),(11,12),(7,8),(9,10),(17,18),(13,14),(15,16)],1,3,4) |
| 250 | +@test vvextrema(A,dims=2) == reshape([(19,23),(20,24),(1,5),(2,6),(7,11),(8,12),(13,17),(14,18)],2,1,4) |
| 251 | +@test vvextrema(A,dims=3) == reshape([(5,23),(6,24),(1,19),(2,20),(3,21),(4,22)],2,3,1) |
| 252 | +@test vvextrema(A,dims=(1,2)) == reshape([(19,24),(1,6),(7,12),(13,18)],1,1,4) |
| 253 | +@test vvextrema(A,dims=(1,3)) == reshape([(5,24),(1,20),(3,22)],1,3,1) |
| 254 | +@test vvextrema(A,dims=(2,3)) == reshape([(1,23),(2,24)],2,1,1) |
| 255 | +@test vvextrema(A,dims=(1,2,3)) == reshape([(1,24)],1,1,1) |
| 256 | +@test size(vvextrema(A,dims=1)) == size(maximum(A,dims=1)) |
| 257 | +@test size(vvextrema(A,dims=(1,2))) == size(maximum(A,dims=(1,2))) |
| 258 | +@test size(vvextrema(A,dims=(1,2,3))) == size(maximum(A,dims=(1,2,3))) |
| 259 | +@test vvextrema(x->div(x, 2), A, dims=(2,3)) == reshape([(0,11),(1,12)],2,1,1) |
| 260 | + |
| 261 | + |
| 262 | +# any & all |
| 263 | + |
| 264 | +# @test @inferred vany([]) == false |
| 265 | +@test @inferred vany(Bool[]) == false |
| 266 | +@test @inferred vany([true]) == true |
| 267 | +@test @inferred vany([false, false]) == false |
| 268 | +@test @inferred vany([false, true]) == true |
| 269 | +@test @inferred vany([true, false]) == true |
| 270 | +@test @inferred vany([true, true]) == true |
| 271 | +@test @inferred vany([true, true, true]) == true |
| 272 | +@test @inferred vany([true, false, true]) == true |
| 273 | +@test @inferred vany([false, false, false]) == false |
| 274 | + |
| 275 | +# @test @inferred vall([]) == true |
| 276 | +@test @inferred vall(Bool[]) == true |
| 277 | +@test @inferred vall([true]) == true |
| 278 | +@test @inferred vall([false, false]) == false |
| 279 | +@test @inferred vall([false, true]) == false |
| 280 | +@test @inferred vall([true, false]) == false |
| 281 | +@test @inferred vall([true, true]) == true |
| 282 | +@test @inferred vall([true, true, true]) == true |
| 283 | +@test @inferred vall([true, false, true]) == false |
| 284 | +@test @inferred vall([false, false, false]) == false |
| 285 | + |
| 286 | +# @test @inferred vany(x->x>0, []) == false |
| 287 | +@test @inferred vany(x->x>0, Int[]) == false |
| 288 | +@test @inferred vany(x->x>0, [-3]) == false |
| 289 | +@test @inferred vany(x->x>0, [4]) == true |
| 290 | +@test @inferred vany(x->x>0, [-3, 4, 5]) == true |
| 291 | + |
| 292 | +# @test @inferred vall(x->x>0, []) == true |
| 293 | +@test @inferred vall(x->x>0, Int[]) == true |
| 294 | +@test @inferred vall(x->x>0, [-3]) == false |
| 295 | +@test @inferred vall(x->x>0, [4]) == true |
| 296 | +@test @inferred vall(x->x>0, [-3, 4, 5]) == false |
| 297 | + |
| 298 | +let f(x) = ifelse(x == 1, true, ifelse(x == 2, false, 1)) |
| 299 | + @test vany(Any[false,true,false]) |
| 300 | + @test @inferred vany(map(f, [2,1,2])) |
| 301 | + @test @inferred vany([f(x) for x in [2,1,2]]) |
| 302 | + |
| 303 | + @test vall(Any[true,true,true]) |
| 304 | + @test @inferred vall(map(f, [1,1,1])) |
| 305 | + @test @inferred vall([f(x) for x in [1,1,1]]) |
| 306 | + |
| 307 | + # @test_throws TypeError vany([1,true]) |
| 308 | + # @test_throws TypeError vall([true,1]) |
| 309 | + # @test_throws TypeError vany(map(f,[3,1])) |
| 310 | + # @test_throws TypeError vall(map(f,[1,3])) |
| 311 | +end |
| 312 | + |
| 313 | + |
| 314 | +@test vcount(x -> x > 0, Int[]) == vcount(Bool[]) == 0 |
| 315 | +@test vtcount(x -> x > 0, Int[]) == vtcount(Bool[]) == 0 |
| 316 | +@test vcount(x->x>0, -3:5) == vcount((-3:5) .> 0) == 5 |
| 317 | +@test vcount([true, true, false, true]) == vcount(BitVector([true, true, false, true])) == 3 |
| 318 | +x = repeat([false, true, false, true, true, false], 7) |
| 319 | +@test vcount(x) == 21 |
| 320 | +@test_throws MethodError vcount(sqrt, [1]) |
| 321 | +@test_throws MethodError vcount([1]) |
| 322 | +@test vcount(!iszero, Int[]) == 0 |
| 323 | +@test vcount(!iszero, Int[0]) == 0 |
| 324 | +@test vcount(!iszero, Int[1]) == 1 |
| 325 | +@test vcount(!iszero, [1, 0, 2, 0, 3, 0, 4]) == 4 |
| 326 | + |
| 327 | +@test vvsum(Vector(map(UInt8,0:255))) == 32640 |
| 328 | +@test vvsum(Vector(map(UInt8,254:255))) == 509 |
| 329 | + |
| 330 | +# opposite behavior from base |
| 331 | +@test vvsum([-0.0]) === 0.0 |
| 332 | +@test vvsum([-0.0, -0.0]) === 0.0 |
| 333 | +# same as base |
| 334 | +@test vvprod([-0.0, -0.0]) === 0.0 |
0 commit comments