Skip to content

Commit 6560ff5

Browse files
committed
test on a bunch more matrix types
1 parent 7869741 commit 6560ff5

File tree

4 files changed

+40
-18
lines changed

4 files changed

+40
-18
lines changed

test/affprop.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ using Clustering
66
using LinearAlgebra
77
using Random
88
using Statistics
9+
include("test_helpers.jl")
910

1011
@testset "affinityprop() (affinity propagation)" begin
1112
@testset "Argument checks" begin
@@ -47,11 +48,10 @@ using Statistics
4748
end
4849

4950
@testset "Support for arrays other than Matrix{T}" begin
50-
R2 = affinityprop(@view S[:,:]) # run on complete subarray
51-
@test R2.assignments == R.assignments
52-
53-
R3 = affinityprop(Symmetric(S)) # run on complete subarray
54-
@test R3.assignments == R.assignments
51+
@testset "$(typeof(M))" for M in equivalent_matrices(S)
52+
R2 = affinityprop(M)
53+
@test R2.assignments == R.assignments
54+
end
5555
end
5656

5757
#= compare with python result

test/dbscan.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Test
22
using Clustering
33
using Distances
4+
include("test_helpers.jl")
45

56
@testset "dbscan() (DBSCAN clustering)" begin
67

@@ -37,11 +38,10 @@ end
3738
@test all(R.counts .>= 180)
3839

3940
@testset "Support for arrays other than Matrix{T}" begin
40-
R2 = dbscan(@view(D[:,:]), 1.0, 10) # run on complete subarray
41-
@test R2.assignments == R.assignments
42-
43-
R3 = dbscan(Symmetric(D), 1.0, 10) # run on complete subarray
44-
@test R3.assignments == R.assignments
41+
@testset "$(typeof(M))" for M in equivalent_matrices(D)
42+
R2 = dbscan(M, 1.0, 10) # run on complete subarray
43+
@test R2.assignments == R.assignments
44+
end
4545
end
4646

4747
@testset "normal points" begin

test/kmedoids.jl

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Test
2-
32
using Distances
43
using Clustering
4+
include("test_helpers.jl")
55

66
@testset "kmedoids() (k-medoids)" begin
77

@@ -41,13 +41,11 @@ R = kmedoids(dist, k)
4141
@test R.converged
4242

4343
@testset "Support for arrays other than Matrix{T}" begin
44-
Random.seed!(34568) # restore seed as kmedoids is not determantistic
45-
R2 = kmedoids(@view(dist[:,:]), k) # run on complete subarray
46-
@test R2.assignments == R.assignments
47-
48-
Random.seed!(34568) # restore seed as kmedoids is not determantistic
49-
R3 = kmedoids(Symmetric(dist), k) # run on complete subarray
50-
@test R3.assignments == R.assignments
44+
@testset "$(typeof(M))" for M in equivalent_matrices(dist)
45+
Random.seed!(34568) # restore seed as kmedoids is not determantistic
46+
R2 = kmedoids(M, k)
47+
@test R2.assignments == R.assignments
48+
end
5149
end
5250

5351
# k=1 and k=n cases

test/test_helpers.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using LinearAlgebra
2+
using SparseArrays
3+
4+
"""
5+
equivalent_matrices(x::AbstractMatrix)
6+
7+
Returns a collection of matrixes that are equal to the input `x`, but of a different type.
8+
Useful for testing if things still work on different types of matrix.
9+
"""
10+
function equivalent_matrices(x::AbstractMatrix)
11+
mats = [
12+
Base.PermutedDimsArray(x, (1,2)), # identity permutation
13+
view(x, :, :),
14+
view(x, collect.(axes(x))...), # breaks `strides`
15+
sparse(x),
16+
]
17+
if issymmetric(x)
18+
append!(mats, [
19+
Symmetric(x),
20+
Transpose(x),
21+
])
22+
end
23+
return mats
24+
end

0 commit comments

Comments
 (0)