Skip to content

Commit 7c97abd

Browse files
Define basic tests
1 parent f0d65a1 commit 7c97abd

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

test/runtests.jl

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,82 @@ using VectorizedReduction
22
using Test
33

44
@testset "VectorizedReduction.jl" begin
5-
# Write your tests here.
5+
@testset "vvmapreduce" begin
6+
A = rand(5,5,5,5)
7+
@test vvmapreduce(abs2, +, A, dims=(1,3)) mapreduce(abs2, +, A, dims=(1,3))
8+
@test vvmapreduce(cos, *, A, dims=(2,4)) mapreduce(cos, *, A, dims=(2,4))
9+
@test vvprod(log, A1, dims=1) prod(log, A1, dims=1)
10+
@test vvminimum(sin, A1, dims=(3,4)) minimum(sin, A1, dims=(3,4))
11+
@test vvmaximum(sin, A1, dims=(3,4)) maximum(sin, A1, dims=(3,4))
12+
end
13+
@testset "vvmapreduce_vararg" begin
14+
A1 = rand(5,5,5,5)
15+
A2 = rand(5,5,5,5)
16+
A3 = rand(5,5,5,5)
17+
A4 = rand(1:10, 5,5,5,5)
18+
as = (A1, A2, A3)
19+
@test vvmapreduce(+, +, as, dims=(1,2,4)) mapreduce(+, +, A1, A2, A3, dims=(1,2,4))
20+
# Tests of variably typed arrays
21+
@test vvmapreduce(+, +, A1, A2, dims=(2,3,4)) mapreduce(+, +, A1, A2, dims=(2,3,4))
22+
@test vvmapreduce(+, +, A1, A4, dims=(2,3,4)) mapreduce(+, +, A1, A4, dims=(2,3,4))
23+
# interface tests
24+
r = mapreduce(*, +, A1, A2, A3)
25+
@test r vvmapreduce(*, +, zero, A1, A2, A3)
26+
@test r vvmapreduce(*, +, A1, A2, A3)
27+
@test r vvmapreduce(*, +, A1, A2, A3, dims=:)
28+
@test r vvmapreduce(*, +, A1, A2, A3, dims=:, init=0)
29+
@test r vvmapreduce(*, +, A1, A2, A3, dims=:, init=zero)
30+
@test r vvmapreduce(*, +, as)
31+
# And for really strange stuff (e.g. posterior predictive transformations)
32+
@benchmark vvmapreduce((x,y,z) -> ifelse(x*y+z 1, 1, 0), +, A1, A2, A3)
33+
@benchmark vvmapreduce((x,y,z) -> ifelse(x*y+z 1, 1, 0), +, A1, A2, A3, dims=(2,3,4))
34+
# using ifelse for just a boolean is quite slow, but the above is just for demonstration
35+
@benchmark vvmapreduce(, +, A1, A2)
36+
@benchmark vvmapreduce((x,y,z) -> (x*y+z, 1), +, A1, A2, A3)
37+
@benchmark vvmapreduce((x,y,z) -> (x*y+z, 1), +, A1, A2, A3, dims=(2,3,4))
38+
@benchmark mapreduce((x,y,z) -> (x*y+z, 1), +, A1, A2, A3)
39+
# What I mean by posterior predictive transformation? Well, one might encounter
40+
# this in Bayesian model checking, which provides a convenient example.
41+
# If one wishes to compute the Pr = ∫∫𝕀(T(yʳᵉᵖ, θ) ≥ T(y, θ))p(yʳᵉᵖ|θ)p(θ|y)dyʳᵉᵖdθ
42+
# Let's imagine that A1 represents T(yʳᵉᵖ, θ) and A2 represents T(y, θ)
43+
# i.e. the test variable samples computed as a functional of the Markov chain (samples of θ)
44+
# Then, Pr is computed as
45+
vvmapreduce(, +, A1, A2) / length(A1)
46+
# Or, if only the probability is of interest, and we do not wish to use the functionals
47+
# for any other purpose, we could compute it as:
48+
vvmapreduce((x, y) -> (f(x), f(y)), +, A1, A2)
49+
# where `f` is the functional of interest, e.g.
50+
@benchmark vvmapreduce((x, y) -> (abs2(x), abs2(y)), +, A1, A2)
51+
@benchmark vvmapreduce((x, y) -> (abs2(x), abs2(y)), +, A1, A2, dims=(2,3,4))
52+
# One can also express commonly encountered reductions with ease;
53+
# these will be fused once a post-reduction operator can be specified
54+
# MSE
55+
B = vvmapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)) ./ (size(A1, 2) * size(A1, 4) )
56+
@test B mapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)) ./ (size(A1, 2) * size(A1, 4) )
57+
# Euclidean distance
58+
B = ().(vvmapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)))
59+
@test B ().(mapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)))
60+
end
61+
@testset "vfindminmax" begin
62+
# Simple
63+
A1 = rand(5,5)
64+
A2 = rand(5,5)
65+
A3 = rand(5,5)
66+
A′ = @. A1 + A2 + A3
67+
@test findmin(A′) == vfindmin(+, A1, A2, A3)
68+
@test findmin(A′, dims=2) == vfindmin(+, A1, A2, A3, dims=2)
69+
v1 = rand(5)
70+
v2 = rand(5)
71+
v3 = rand(5)
72+
v′ = @. v1 + v2 + v3;
73+
@test findmin(v′) == vfindmin(+, v1, v2, v3)
74+
# Varargs
75+
end
76+
@testset "vfindminmax_vararg" begin
77+
A′ = @. A1 * A2 + A3;
78+
@test findmin(A′) == vfindmin((x, y, z) -> x * y + z, A1, A2, A3)
79+
val1, ind1 = findmin(A′, dims=2)
80+
val2, ind2 = vfindmin((x, y, z) -> x * y + z, A1, A2, A3, dims=2)
81+
@test ind1 == ind2 && val1 val2
82+
end
683
end

0 commit comments

Comments
 (0)