Skip to content

Commit d5ad121

Browse files
committed
Initial commit
1 parent 7f03b98 commit d5ad121

11 files changed

+197
-433
lines changed

src/sparse_array.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,4 @@ Base.similar(A::SparseArray{T, N}) where {T, N} =
165165
SparseArray{T, N}(A.dims, A.default_value)
166166

167167
Base.similar(A::SparseArray{T, N}, ::Type{S}) where {T, S, N} =
168-
SparseArray{S, N}(A.dims, zero(S))
168+
SparseArray{S, N}(A.dims, zero(S))

test/test_array_interface.jl

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,25 @@ module TestArrayInterface
33
using MultidimensionalSparseArrays
44
using Test
55

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
6+
@testset "Array Interface" begin
7+
A = SparseArray{Int, 2}((2, 3))
8+
A[1, 1] = 10
9+
A[2, 2] = 20
10+
11+
@test length(A) == 6
12+
@test ndims(A) == 2
13+
@test size(A) == (2, 3)
14+
@test size(A, 1) == 2
15+
@test size(A, 2) == 3
16+
17+
# Iteration
18+
values = collect(A)
19+
expected = [10 0 0; 0 20 0] # 2D array layout (column-major)
20+
@test values == expected
21+
22+
# Test flat iteration using linear indexing
23+
flat_values = [A[CartesianIndices(A)[i]] for i in 1:length(A)]
24+
expected_flat = [10, 0, 0, 20, 0, 0] # Column-major order
25+
@test flat_values == expected_flat
26+
end
27+
end

test/test_construction.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ using Test
3838
@test nnz(E) == 4 # Only non-zero elements stored
3939
end
4040

41-
end
41+
end

test/test_edge_cases.jl

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,27 @@ module TestEdgeCases
33
using MultidimensionalSparseArrays
44
using Test
55

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
6+
@testset "Edge Cases" begin
7+
# Empty array
8+
A = SparseArray{Float64, 2}((0, 0))
9+
@test size(A) == (0, 0)
10+
@test length(A) == 0
11+
@test nnz(A) == 0
12+
13+
# 1D array
14+
B = SparseArray{Int, 1}((5,))
15+
B[3] = 42
16+
@test B[3] == 42
17+
@test B[1] == 0
18+
@test nnz(B) == 1
19+
20+
# Large sparse array
21+
C = SparseArray{Float64, 2}((1000, 1000))
22+
C[1, 1] = 1.0
23+
C[500, 500] = 2.0
24+
C[1000, 1000] = 3.0
25+
@test nnz(C) == 3
26+
@test sparsity(C) > 0.999 # Very sparse
27+
end
28+
29+
end

test/test_equality_and_copying.jl

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11

2-
module TestEqualityAndCopying
2+
module TestEqualityAndCopying
33

44
using MultidimensionalSparseArrays
55
using Test
66

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
7+
@testset "Equality and Copying" begin
8+
A = SparseArray{Int, 2}((3, 3))
9+
A[1, 1] = 5
10+
A[2, 2] = 10
11+
12+
B = SparseArray{Int, 2}((3, 3))
13+
B[1, 1] = 5
14+
B[2, 2] = 10
15+
16+
@test A == B
17+
18+
# Different values
19+
B[1, 1] = 6
20+
@test A != B
21+
22+
# Different sizes
23+
C = SparseArray{Int, 2}((2, 2))
24+
@test A != C
25+
26+
# Copy
27+
D = copy(A)
28+
@test A == D
29+
@test A.data !== D.data # Different objects
30+
31+
# Modify copy shouldn't affect original
32+
D[3, 3] = 15
33+
@test A != D
34+
@test A[3, 3] == 0
35+
@test D[3, 3] == 15
36+
end
37+
38+
end

test/test_indexing.jl

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
11

2-
module TestIndexing
2+
module TestIndexing
33

44
using MultidimensionalSparseArrays
55
using Test
66

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
7+
@testset "Indexing" begin
8+
A = SparseArray{Float64, 2}((3, 3))
9+
10+
# Setting values
11+
A[1, 1] = 5.0
12+
A[2, 3] = -2.5
13+
A[3, 2] = 1.0
14+
15+
@test A[1, 1] == 5.0
16+
@test A[2, 3] == -2.5
17+
@test A[3, 2] == 1.0
18+
@test A[1, 2] == 0.0 # Default value
19+
@test A[2, 2] == 0.0 # Default value
20+
21+
# CartesianIndex access
22+
@test A[CartesianIndex(1, 1)] == 5.0
23+
@test A[CartesianIndex(2, 2)] == 0.0
24+
25+
A[CartesianIndex(2, 1)] = 3.0
26+
@test A[2, 1] == 3.0
27+
28+
# Setting to default value should remove from storage
29+
A[1, 1] = 0.0
30+
@test A[1, 1] == 0.0
31+
@test nnz(A) == 3 # Should be one less stored element
32+
33+
# Bounds checking
34+
@test_throws BoundsError A[0, 1]
35+
@test_throws BoundsError A[4, 1]
36+
@test_throws BoundsError A[1, 4]
37+
end
38+
39+
end

test/test_multidimensional_arrays.jl

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,22 @@ module TestMultidimensionalArrays
33
using MultidimensionalSparseArrays
44
using Test
55

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
6+
@testset "Multidimensional Arrays" begin
7+
# 3D array
8+
A = SparseArray{Float64, 3}((2, 2, 2))
9+
A[1, 1, 1] = 1.0
10+
A[2, 2, 2] = 8.0
11+
12+
@test A[1, 1, 1] == 1.0
13+
@test A[2, 2, 2] == 8.0
14+
@test A[1, 2, 1] == 0.0
15+
@test nnz(A) == 2
16+
17+
# 4D array
18+
B = SparseArray{Int, 4}((2, 2, 2, 2))
19+
B[1, 1, 1, 1] = 100
20+
@test B[1, 1, 1, 1] == 100
21+
@test nnz(B) == 1
22+
end
23+
24+
end

test/test_similar.jl

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@ module TestSimilar
33
using MultidimensionalSparseArrays
44
using Test
55

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
6+
@testset "Similar" begin
7+
A = SparseArray{Float64, 2}((3, 4))
8+
A[1, 1] = 5.0
9+
10+
B = similar(A)
11+
@test size(B) == size(A)
12+
@test eltype(B) == eltype(A)
13+
@test nnz(B) == 0 # Should be empty
14+
@test B.default_value == A.default_value
15+
16+
C = similar(A, Int)
17+
@test size(C) == size(A)
18+
@test eltype(C) == Int
19+
@test nnz(C) == 0
20+
@test C.default_value == 0
21+
end
22+
end

0 commit comments

Comments
 (0)