@@ -6,90 +6,107 @@ function shared_test_conversions(
66 complex_types:: Tuple ,
77)
88 @testset " Format Conversions $array_type " verbose= true begin
9- # Test CSC → COO → CSC round-trip
10- @testset " CSC ↔ COO" begin
11- A = sparse([1 , 2 , 3 , 1 , 2 ], [1 , 2 , 3 , 2 , 3 ], float_types[end ][1.0 , 2.0 , 3.0 , 4.0 , 5.0 ], 3 , 3 )
12-
13- # CSC → COO
14- A_csc = adapt(op, DeviceSparseMatrixCSC(A))
15- A_coo_from_csc = DeviceSparseMatrixCOO(A_csc)
16- @test collect(SparseMatrixCSC(A_coo_from_csc)) ≈ collect(A)
17-
18- # COO → CSC
19- A_coo = adapt(op, DeviceSparseMatrixCOO(A))
20- A_csc_from_coo = DeviceSparseMatrixCSC(A_coo)
21- @test collect(SparseMatrixCSC(A_csc_from_coo)) ≈ collect(A)
22-
23- # Round-trip
24- A_csc_roundtrip = DeviceSparseMatrixCSC(DeviceSparseMatrixCOO(A_csc))
25- @test collect(SparseMatrixCSC(A_csc_roundtrip)) ≈ collect(A)
26- end
27-
28- # Test CSR → COO → CSR round-trip
29- @testset " CSR ↔ COO" begin
30- A = sparse([1 , 2 , 3 , 1 , 2 ], [1 , 2 , 3 , 2 , 3 ], float_types[end ][1.0 , 2.0 , 3.0 , 4.0 , 5.0 ], 3 , 3 )
31-
32- # CSR → COO
33- A_csr = adapt(op, DeviceSparseMatrixCSR(A))
34- A_coo_from_csr = DeviceSparseMatrixCOO(A_csr)
35- @test collect(SparseMatrixCSC(A_coo_from_csr)) ≈ collect(A)
36-
37- # COO → CSR
38- A_coo = adapt(op, DeviceSparseMatrixCOO(A))
39- A_csr_from_coo = DeviceSparseMatrixCSR(A_coo)
40- @test collect(SparseMatrixCSC(A_csr_from_coo)) ≈ collect(A)
41-
42- # Round-trip
43- A_csr_roundtrip = DeviceSparseMatrixCSR(DeviceSparseMatrixCOO(A_csr))
44- @test collect(SparseMatrixCSC(A_csr_roundtrip)) ≈ collect(A)
45- end
46-
47- # Test with different data types
48- @testset " Different Types" begin
49- # Test with Float32
50- A_f32 = sparse([1 , 2 ], [1 , 2 ], float_types[1 ][1.0f0 , 2.0f0 ], 2 , 2 )
51- A_csc_f32 = adapt(op, DeviceSparseMatrixCSC(A_f32))
52- A_coo_f32 = DeviceSparseMatrixCOO(A_csc_f32)
53- @test collect(SparseMatrixCSC(A_coo_f32)) ≈ collect(A_f32)
54-
55- # Test with ComplexF64
56- A_c64 = sparse([1 , 2 ], [1 , 2 ], complex_types[end ][1.0 + im, 2.0 - im], 2 , 2 )
57- A_csr_c64 = adapt(op, DeviceSparseMatrixCSR(A_c64))
58- A_coo_c64 = DeviceSparseMatrixCOO(A_csr_c64)
59- @test collect(SparseMatrixCSC(A_coo_c64)) ≈ collect(A_c64)
60- end
61-
62- # Test with empty matrices
63- @testset " Edge Cases" begin
64- # Empty matrix
65- A_empty = spzeros(float_types[end ], 3 , 3 )
66- A_csc_empty = adapt(op, DeviceSparseMatrixCSC(A_empty))
67- A_coo_empty = DeviceSparseMatrixCOO(A_csc_empty)
68- @test nnz(A_coo_empty) == 0
69- @test size(A_coo_empty) == (3 , 3 )
70-
71- # Single element
72- A_single = sparse([1 ], [1 ], float_types[end ][42.0 ], 1 , 1 )
73- A_csr_single = adapt(op, DeviceSparseMatrixCSR(A_single))
74- A_coo_single = DeviceSparseMatrixCOO(A_csr_single)
75- @test collect(SparseMatrixCSC(A_coo_single)) ≈ collect(A_single)
76- end
77-
78- # Test large matrix conversion
79- @testset " Large Matrix" begin
80- A_large = sprand(float_types[end ], 100 , 100 , 0.05 )
81-
82- # CSC → COO → CSC
83- A_csc_large = adapt(op, DeviceSparseMatrixCSC(A_large))
84- A_coo_large = DeviceSparseMatrixCOO(A_csc_large)
85- A_csc_back = DeviceSparseMatrixCSC(A_coo_large)
86- @test collect(SparseMatrixCSC(A_csc_back)) ≈ collect(A_large)
87-
88- # CSR → COO → CSR
89- A_csr_large = adapt(op, DeviceSparseMatrixCSR(A_large))
90- A_coo_large2 = DeviceSparseMatrixCOO(A_csr_large)
91- A_csr_back = DeviceSparseMatrixCSR(A_coo_large2)
92- @test collect(SparseMatrixCSC(A_csr_back)) ≈ collect(A_large)
9+ # Many conversion functions rely on AcceleratedKernels sortperm
10+ # which is not supported on JLBackend. Therefore, we skip conversion
11+ # tests for JLArray.
12+ if array_type != " JLArray"
13+ # Test CSC → COO → CSC round-trip
14+ @testset " CSC ↔ COO" begin
15+ A = sparse(
16+ [1 , 2 , 3 , 1 , 2 ],
17+ [1 , 2 , 3 , 2 , 3 ],
18+ float_types[end ][1.0 , 2.0 , 3.0 , 4.0 , 5.0 ],
19+ 3 ,
20+ 3 ,
21+ )
22+
23+ # CSC → COO
24+ A_csc = adapt(op, DeviceSparseMatrixCSC(A))
25+ A_coo_from_csc = DeviceSparseMatrixCOO(A_csc)
26+ @test collect(SparseMatrixCSC(A_coo_from_csc)) ≈ collect(A)
27+
28+ # COO → CSC
29+ A_coo = adapt(op, DeviceSparseMatrixCOO(A))
30+ A_csc_from_coo = DeviceSparseMatrixCSC(A_coo)
31+ @test collect(SparseMatrixCSC(A_csc_from_coo)) ≈ collect(A)
32+
33+ # Round-trip
34+ A_csc_roundtrip = DeviceSparseMatrixCSC(DeviceSparseMatrixCOO(A_csc))
35+ @test collect(SparseMatrixCSC(A_csc_roundtrip)) ≈ collect(A)
36+ end
37+
38+ # Test CSR → COO → CSR round-trip
39+ @testset " CSR ↔ COO" begin
40+ A = sparse(
41+ [1 , 2 , 3 , 1 , 2 ],
42+ [1 , 2 , 3 , 2 , 3 ],
43+ float_types[end ][1.0 , 2.0 , 3.0 , 4.0 , 5.0 ],
44+ 3 ,
45+ 3 ,
46+ )
47+
48+ # CSR → COO
49+ A_csr = adapt(op, DeviceSparseMatrixCSR(A))
50+ A_coo_from_csr = DeviceSparseMatrixCOO(A_csr)
51+ @test collect(SparseMatrixCSC(A_coo_from_csr)) ≈ collect(A)
52+
53+ # COO → CSR
54+ A_coo = adapt(op, DeviceSparseMatrixCOO(A))
55+ A_csr_from_coo = DeviceSparseMatrixCSR(A_coo)
56+ @test collect(SparseMatrixCSC(A_csr_from_coo)) ≈ collect(A)
57+
58+ # Round-trip
59+ A_csr_roundtrip = DeviceSparseMatrixCSR(DeviceSparseMatrixCOO(A_csr))
60+ @test collect(SparseMatrixCSC(A_csr_roundtrip)) ≈ collect(A)
61+ end
62+
63+ # Test with different data types
64+ @testset " Different Types" begin
65+ # Test with Float32
66+ A_f32 = sparse([1 , 2 ], [1 , 2 ], float_types[1 ][1.0f0 , 2.0f0 ], 2 , 2 )
67+ A_csc_f32 = adapt(op, DeviceSparseMatrixCSC(A_f32))
68+ A_coo_f32 = DeviceSparseMatrixCOO(A_csc_f32)
69+ @test collect(SparseMatrixCSC(A_coo_f32)) ≈ collect(A_f32)
70+
71+ # Test with ComplexF64
72+ A_c64 = sparse([1 , 2 ], [1 , 2 ], complex_types[end ][1.0 + im, 2.0 - im], 2 , 2 )
73+ A_csr_c64 = adapt(op, DeviceSparseMatrixCSR(A_c64))
74+ A_coo_c64 = DeviceSparseMatrixCOO(A_csr_c64)
75+ @test collect(SparseMatrixCSC(A_coo_c64)) ≈ collect(A_c64)
76+ end
77+
78+ # Test with empty matrices
79+ @testset " Edge Cases" begin
80+ # Empty matrix
81+ A_empty = spzeros(float_types[end ], 3 , 3 )
82+ A_csc_empty = adapt(op, DeviceSparseMatrixCSC(A_empty))
83+ A_coo_empty = DeviceSparseMatrixCOO(A_csc_empty)
84+ @test nnz(A_coo_empty) == 0
85+ @test size(A_coo_empty) == (3 , 3 )
86+
87+ # Single element
88+ A_single = sparse([1 ], [1 ], float_types[end ][42.0 ], 1 , 1 )
89+ A_csr_single = adapt(op, DeviceSparseMatrixCSR(A_single))
90+ A_coo_single = DeviceSparseMatrixCOO(A_csr_single)
91+ @test collect(SparseMatrixCSC(A_coo_single)) ≈ collect(A_single)
92+ end
93+
94+ # Test large matrix conversion
95+ @testset " Large Matrix" begin
96+ A_large = sprand(float_types[end ], 100 , 100 , 0.05 )
97+
98+ # CSC → COO → CSC
99+ A_csc_large = adapt(op, DeviceSparseMatrixCSC(A_large))
100+ A_coo_large = DeviceSparseMatrixCOO(A_csc_large)
101+ A_csc_back = DeviceSparseMatrixCSC(A_coo_large)
102+ @test collect(SparseMatrixCSC(A_csc_back)) ≈ collect(A_large)
103+
104+ # CSR → COO → CSR
105+ A_csr_large = adapt(op, DeviceSparseMatrixCSR(A_large))
106+ A_coo_large2 = DeviceSparseMatrixCOO(A_csr_large)
107+ A_csr_back = DeviceSparseMatrixCSR(A_coo_large2)
108+ @test collect(SparseMatrixCSC(A_csr_back)) ≈ collect(A_large)
109+ end
93110 end
94111 end
95112end
0 commit comments