Skip to content

Commit 7f03b98

Browse files
committed
Initial commit
1 parent 56c17dd commit 7f03b98

9 files changed

+284
-0
lines changed

test/test_array_interface.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module TestArrayInterface
2+
3+
using MultidimensionalSparseArrays
4+
using Test
5+
6+
7+
@testset "Array Interface" begin
8+
A = SparseArray{Int, 2}((2, 3))
9+
A[1, 1] = 10
10+
A[2, 2] = 20
11+
12+
@test length(A) == 6
13+
@test ndims(A) == 2
14+
@test size(A) == (2, 3)
15+
@test size(A, 1) == 2
16+
@test size(A, 2) == 3
17+
18+
# Iteration
19+
values = collect(A)
20+
expected = [10 0 0; 0 20 0] # 2D array layout (column-major)
21+
@test values == expected
22+
23+
# Test flat iteration using linear indexing
24+
flat_values = [A[CartesianIndices(A)[i]] for i in 1:length(A)]
25+
expected_flat = [10, 0, 0, 20, 0, 0] # Column-major order
26+
@test flat_values == expected_flat
27+
end
28+
end

test/test_construction.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module TestConstruction
2+
3+
using MultidimensionalSparseArrays
4+
using Test
5+
6+
@testset "Construction" begin
7+
# Basic construction
8+
A = SparseArray{Float64, 2}((3, 4))
9+
@test size(A) == (3, 4)
10+
@test eltype(A) == Float64
11+
@test nnz(A) == 0
12+
@test A.default_value == 0.0
13+
14+
# Construction with custom default value
15+
B = SparseArray{Int, 2}((2, 2), -1)
16+
@test B.default_value == -1
17+
@test B[1, 1] == -1 # Should return default value
18+
19+
# Convenience constructors
20+
C = SparseArray{Float64}((3, 3))
21+
@test size(C) == (3, 3)
22+
@test eltype(C) == Float64
23+
24+
D = SparseArray{Int}(2, 3, 4)
25+
@test size(D) == (2, 3, 4)
26+
@test eltype(D) == Int
27+
28+
# Construction from dense array
29+
dense = [1 0 3; 0 0 0; 2 0 4]
30+
E = SparseArray(dense)
31+
@test size(E) == (3, 3)
32+
@test E[1, 1] == 1
33+
@test E[1, 2] == 0
34+
@test E[1, 3] == 3
35+
@test E[2, 1] == 0
36+
@test E[3, 1] == 2
37+
@test E[3, 3] == 4
38+
@test nnz(E) == 4 # Only non-zero elements stored
39+
end
40+
41+
end

test/test_edge_cases.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module TestEdgeCases
2+
3+
using MultidimensionalSparseArrays
4+
using Test
5+
6+
7+
@testset "Edge Cases" begin
8+
# Empty array
9+
A = SparseArray{Float64, 2}((0, 0))
10+
@test size(A) == (0, 0)
11+
@test length(A) == 0
12+
@test nnz(A) == 0
13+
14+
# 1D array
15+
B = SparseArray{Int, 1}((5,))
16+
B[3] = 42
17+
@test B[3] == 42
18+
@test B[1] == 0
19+
@test nnz(B) == 1
20+
21+
# Large sparse array
22+
C = SparseArray{Float64, 2}((1000, 1000))
23+
C[1, 1] = 1.0
24+
C[500, 500] = 2.0
25+
C[1000, 1000] = 3.0
26+
@test nnz(C) == 3
27+
@test sparsity(C) > 0.999 # Very sparse
28+
end
29+
30+
end

test/test_equality_and_copying.jl

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
module TestEqualityAndCopying
3+
4+
using MultidimensionalSparseArrays
5+
using Test
6+
7+
8+
@testset "Equality and Copying" begin
9+
A = SparseArray{Int, 2}((3, 3))
10+
A[1, 1] = 5
11+
A[2, 2] = 10
12+
13+
B = SparseArray{Int, 2}((3, 3))
14+
B[1, 1] = 5
15+
B[2, 2] = 10
16+
17+
@test A == B
18+
19+
# Different values
20+
B[1, 1] = 6
21+
@test A != B
22+
23+
# Different sizes
24+
C = SparseArray{Int, 2}((2, 2))
25+
@test A != C
26+
27+
# Copy
28+
D = copy(A)
29+
@test A == D
30+
@test A.data !== D.data # Different objects
31+
32+
# Modify copy shouldn't affect original
33+
D[3, 3] = 15
34+
@test A != D
35+
@test A[3, 3] == 0
36+
@test D[3, 3] == 15
37+
end
38+
39+
end

test/test_indexing.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
module TestIndexing
3+
4+
using MultidimensionalSparseArrays
5+
using Test
6+
7+
8+
@testset "Indexing" begin
9+
A = SparseArray{Float64, 2}((3, 3))
10+
11+
# Setting values
12+
A[1, 1] = 5.0
13+
A[2, 3] = -2.5
14+
A[3, 2] = 1.0
15+
16+
@test A[1, 1] == 5.0
17+
@test A[2, 3] == -2.5
18+
@test A[3, 2] == 1.0
19+
@test A[1, 2] == 0.0 # Default value
20+
@test A[2, 2] == 0.0 # Default value
21+
22+
# CartesianIndex access
23+
@test A[CartesianIndex(1, 1)] == 5.0
24+
@test A[CartesianIndex(2, 2)] == 0.0
25+
26+
A[CartesianIndex(2, 1)] = 3.0
27+
@test A[2, 1] == 3.0
28+
29+
# Setting to default value should remove from storage
30+
A[1, 1] = 0.0
31+
@test A[1, 1] == 0.0
32+
@test nnz(A) == 3 # Should be one less stored element
33+
34+
# Bounds checking
35+
@test_throws BoundsError A[0, 1]
36+
@test_throws BoundsError A[4, 1]
37+
@test_throws BoundsError A[1, 4]
38+
end
39+
40+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module TestMultidimensionalArrays
2+
3+
using MultidimensionalSparseArrays
4+
using Test
5+
6+
7+
@testset "Multidimensional Arrays" begin
8+
# 3D array
9+
A = SparseArray{Float64, 3}((2, 2, 2))
10+
A[1, 1, 1] = 1.0
11+
A[2, 2, 2] = 8.0
12+
13+
@test A[1, 1, 1] == 1.0
14+
@test A[2, 2, 2] == 8.0
15+
@test A[1, 2, 1] == 0.0
16+
@test nnz(A) == 2
17+
18+
# 4D array
19+
B = SparseArray{Int, 4}((2, 2, 2, 2))
20+
B[1, 1, 1, 1] = 100
21+
@test B[1, 1, 1, 1] == 100
22+
@test nnz(B) == 1
23+
end
24+
25+
end

test/test_similar.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module TestSimilar
2+
3+
using MultidimensionalSparseArrays
4+
using Test
5+
6+
7+
@testset "Similar" begin
8+
A = SparseArray{Float64, 2}((3, 4))
9+
A[1, 1] = 5.0
10+
11+
B = similar(A)
12+
@test size(B) == size(A)
13+
@test eltype(B) == eltype(A)
14+
@test nnz(B) == 0 # Should be empty
15+
@test B.default_value == A.default_value
16+
17+
C = similar(A, Int)
18+
@test size(C) == size(A)
19+
@test eltype(C) == Int
20+
@test nnz(C) == 0
21+
@test C.default_value == 0
22+
end
23+
end

test/test_type_stability.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
module TestTypeStability
2+
3+
using MultidimensionalSparseArrays
4+
using Test
5+
6+
7+
@testset "Type Stability" begin
8+
A = SparseArray{Float64, 2}((2, 2))
9+
10+
# Test that operations return correct types
11+
@test typeof(A[1, 1]) == Float64
12+
@test typeof(nnz(A)) == Int
13+
@test typeof(sparsity(A)) == Float64
14+
15+
# Test with different element types
16+
B = SparseArray{Complex{Float64}, 2}((2, 2))
17+
B[1, 1] = 1.0 + 2.0im
18+
@test B[1, 1] == 1.0 + 2.0im
19+
@test typeof(B[1, 1]) == Complex{Float64}
20+
end
21+
end

test/test_utility_functions.jl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
module TestUtilityFunctions
3+
4+
using MultidimensionalSparseArrays
5+
using Test
6+
7+
8+
@testset "Utility Functions" begin
9+
A = SparseArray{Float64, 2}((4, 4))
10+
A[1, 1] = 1.0
11+
A[2, 3] = 2.0
12+
A[4, 4] = 3.0
13+
14+
@test nnz(A) == 3
15+
@test sparsity(A) 13/16 # 13 zeros out of 16 elements
16+
17+
# Test stored indices
18+
indices = collect(stored_indices(A))
19+
@test length(indices) == 3
20+
@test CartesianIndex(1, 1) in indices
21+
@test CartesianIndex(2, 3) in indices
22+
@test CartesianIndex(4, 4) in indices
23+
24+
# Test stored values
25+
values = collect(stored_values(A))
26+
@test length(values) == 3
27+
@test 1.0 in values
28+
@test 2.0 in values
29+
@test 3.0 in values
30+
31+
# Test stored pairs
32+
pairs_dict = Dict(stored_pairs(A))
33+
@test pairs_dict[CartesianIndex(1, 1)] == 1.0
34+
@test pairs_dict[CartesianIndex(2, 3)] == 2.0
35+
@test pairs_dict[CartesianIndex(4, 4)] == 3.0
36+
end
37+
end

0 commit comments

Comments
 (0)