Skip to content

Commit 093aef3

Browse files
Simplify test structure by merging conditionals
Co-authored-by: albertomercurio <61953577+albertomercurio@users.noreply.github.com>
1 parent d8c2ecb commit 093aef3

File tree

3 files changed

+84
-123
lines changed

3 files changed

+84
-123
lines changed

test/shared/matrix_coo.jl

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -284,23 +284,35 @@ function shared_test_linearalgebra_matrix_coo(
284284
(identity, transpose, adjoint),
285285
(identity, transpose, adjoint),
286286
)
287-
# Skip identity + identity for int types when using transpose/adjoint
288-
# since we test that separately below
287+
# Skip transpose/adjoint for int types
288+
if T in (int_types...) && (op_A !== identity || op_B !== identity)
289+
continue
290+
end
291+
292+
# Skip adjoint for real types (same as transpose)
293+
if T <: Real && (op_A === adjoint || op_B === adjoint)
294+
continue
295+
end
296+
297+
# Use rectangular matrices for identity+identity, square for transpose/adjoint
298+
m, n = (op_A === identity && op_B === identity) ? (50, 40) : (30, 30)
299+
dims_A = op_A === identity ? (m, n) : (n, m)
300+
dims_B = op_B === identity ? (m, n) : (n, m)
301+
302+
A = sprand(T, dims_A..., 0.1)
303+
B = sprand(T, dims_B..., 0.15)
304+
305+
dA = adapt(op, DeviceSparseMatrixCOO(A))
306+
dB = adapt(op, DeviceSparseMatrixCOO(B))
307+
308+
# Test sparse + sparse
309+
result = op_A(dA) + op_B(dB)
310+
expected = op_A(A) + op_B(B)
311+
@test collect(result) Matrix(expected)
312+
@test result isa DeviceSparseMatrixCOO
313+
314+
# Additional tests only for identity + identity
289315
if op_A === identity && op_B === identity
290-
# Basic addition tests
291-
m, n = 50, 40
292-
A = sprand(T, m, n, 0.1)
293-
B = sprand(T, m, n, 0.15)
294-
295-
dA = adapt(op, DeviceSparseMatrixCOO(A))
296-
dB = adapt(op, DeviceSparseMatrixCOO(B))
297-
298-
# Test sparse + sparse
299-
result = dA + dB
300-
expected = A + B
301-
@test collect(result) Matrix(expected)
302-
@test result isa DeviceSparseMatrixCOO
303-
304316
# Test with overlapping entries
305317
A_overlap = sparse([1, 2, 3], [1, 2, 3], T[1, 2, 3], m, n)
306318
B_overlap = sparse([1, 2, 4], [1, 2, 4], T[4, 5, 6], m, n)
@@ -314,31 +326,6 @@ function shared_test_linearalgebra_matrix_coo(
314326
B_wrong = sprand(T, m + 1, n, 0.1)
315327
dB_wrong = adapt(op, DeviceSparseMatrixCOO(B_wrong))
316328
@test_throws DimensionMismatch dA + dB_wrong
317-
else
318-
# Transpose/adjoint tests - only for float and complex types
319-
if T in (int_types...)
320-
continue
321-
end
322-
323-
# Skip adjoint for real types (same as transpose)
324-
if T <: Real && (op_A === adjoint || op_B === adjoint)
325-
continue
326-
end
327-
328-
# Square matrices for transpose tests
329-
m, n = 30, 30
330-
dims_A = op_A === identity ? (m, n) : (n, m)
331-
dims_B = op_B === identity ? (m, n) : (n, m)
332-
333-
A = sprand(T, dims_A..., 0.1)
334-
B = sprand(T, dims_B..., 0.1)
335-
336-
dA = adapt(op, DeviceSparseMatrixCOO(A))
337-
dB = adapt(op, DeviceSparseMatrixCOO(B))
338-
339-
result = op_A(dA) + op_B(dB)
340-
expected = op_A(A) + op_B(B)
341-
@test collect(result) Matrix(expected)
342329
end
343330
end
344331
end

test/shared/matrix_csc.jl

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -282,23 +282,35 @@ function shared_test_linearalgebra_matrix_csc(
282282
(identity, transpose, adjoint),
283283
(identity, transpose, adjoint),
284284
)
285-
# Skip identity + identity for int types when using transpose/adjoint
286-
# since we test that separately below
285+
# Skip transpose/adjoint for int types
286+
if T in (int_types...) && (op_A !== identity || op_B !== identity)
287+
continue
288+
end
289+
290+
# Skip adjoint for real types (same as transpose)
291+
if T <: Real && (op_A === adjoint || op_B === adjoint)
292+
continue
293+
end
294+
295+
# Use rectangular matrices for identity+identity, square for transpose/adjoint
296+
m, n = (op_A === identity && op_B === identity) ? (50, 40) : (30, 30)
297+
dims_A = op_A === identity ? (m, n) : (n, m)
298+
dims_B = op_B === identity ? (m, n) : (n, m)
299+
300+
A = sprand(T, dims_A..., 0.1)
301+
B = sprand(T, dims_B..., 0.15)
302+
303+
dA = adapt(op, DeviceSparseMatrixCSC(A))
304+
dB = adapt(op, DeviceSparseMatrixCSC(B))
305+
306+
# Test sparse + sparse
307+
result = op_A(dA) + op_B(dB)
308+
expected = op_A(A) + op_B(B)
309+
@test collect(result) Matrix(expected)
310+
@test result isa DeviceSparseMatrixCSC
311+
312+
# Additional tests only for identity + identity
287313
if op_A === identity && op_B === identity
288-
# Basic addition tests
289-
m, n = 50, 40
290-
A = sprand(T, m, n, 0.1)
291-
B = sprand(T, m, n, 0.15)
292-
293-
dA = adapt(op, DeviceSparseMatrixCSC(A))
294-
dB = adapt(op, DeviceSparseMatrixCSC(B))
295-
296-
# Test sparse + sparse
297-
result = dA + dB
298-
expected = A + B
299-
@test collect(result) Matrix(expected)
300-
@test result isa DeviceSparseMatrixCSC
301-
302314
# Test with overlapping entries
303315
A_overlap = sparse([1, 2, 3], [1, 2, 3], T[1, 2, 3], m, n)
304316
B_overlap = sparse([1, 2, 4], [1, 2, 4], T[4, 5, 6], m, n)
@@ -312,31 +324,6 @@ function shared_test_linearalgebra_matrix_csc(
312324
B_wrong = sprand(T, m + 1, n, 0.1)
313325
dB_wrong = adapt(op, DeviceSparseMatrixCSC(B_wrong))
314326
@test_throws DimensionMismatch dA + dB_wrong
315-
else
316-
# Transpose/adjoint tests - only for float and complex types
317-
if T in (int_types...)
318-
continue
319-
end
320-
321-
# Skip adjoint for real types (same as transpose)
322-
if T <: Real && (op_A === adjoint || op_B === adjoint)
323-
continue
324-
end
325-
326-
# Square matrices for transpose tests
327-
m, n = 30, 30
328-
dims_A = op_A === identity ? (m, n) : (n, m)
329-
dims_B = op_B === identity ? (m, n) : (n, m)
330-
331-
A = sprand(T, dims_A..., 0.1)
332-
B = sprand(T, dims_B..., 0.1)
333-
334-
dA = adapt(op, DeviceSparseMatrixCSC(A))
335-
dB = adapt(op, DeviceSparseMatrixCSC(B))
336-
337-
result = op_A(dA) + op_B(dB)
338-
expected = op_A(A) + op_B(B)
339-
@test collect(result) Matrix(expected)
340327
end
341328
end
342329
end

test/shared/matrix_csr.jl

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -281,23 +281,35 @@ function shared_test_linearalgebra_matrix_csr(
281281
(identity, transpose, adjoint),
282282
(identity, transpose, adjoint),
283283
)
284-
# Skip identity + identity for int types when using transpose/adjoint
285-
# since we test that separately below
284+
# Skip transpose/adjoint for int types
285+
if T in (int_types...) && (op_A !== identity || op_B !== identity)
286+
continue
287+
end
288+
289+
# Skip adjoint for real types (same as transpose)
290+
if T <: Real && (op_A === adjoint || op_B === adjoint)
291+
continue
292+
end
293+
294+
# Use rectangular matrices for identity+identity, square for transpose/adjoint
295+
m, n = (op_A === identity && op_B === identity) ? (50, 40) : (30, 30)
296+
dims_A = op_A === identity ? (m, n) : (n, m)
297+
dims_B = op_B === identity ? (m, n) : (n, m)
298+
299+
A = sprand(T, dims_A..., 0.1)
300+
B = sprand(T, dims_B..., 0.15)
301+
302+
dA = adapt(op, DeviceSparseMatrixCSR(A))
303+
dB = adapt(op, DeviceSparseMatrixCSR(B))
304+
305+
# Test sparse + sparse
306+
result = op_A(dA) + op_B(dB)
307+
expected = op_A(A) + op_B(B)
308+
@test collect(result) Matrix(expected)
309+
@test result isa DeviceSparseMatrixCSR
310+
311+
# Additional tests only for identity + identity
286312
if op_A === identity && op_B === identity
287-
# Basic addition tests
288-
m, n = 50, 40
289-
A = sprand(T, m, n, 0.1)
290-
B = sprand(T, m, n, 0.15)
291-
292-
dA = adapt(op, DeviceSparseMatrixCSR(A))
293-
dB = adapt(op, DeviceSparseMatrixCSR(B))
294-
295-
# Test sparse + sparse
296-
result = dA + dB
297-
expected = A + B
298-
@test collect(result) Matrix(expected)
299-
@test result isa DeviceSparseMatrixCSR
300-
301313
# Test with overlapping entries
302314
A_overlap = sparse([1, 2, 3], [1, 2, 3], T[1, 2, 3], m, n)
303315
B_overlap = sparse([1, 2, 4], [1, 2, 4], T[4, 5, 6], m, n)
@@ -311,31 +323,6 @@ function shared_test_linearalgebra_matrix_csr(
311323
B_wrong = sprand(T, m + 1, n, 0.1)
312324
dB_wrong = adapt(op, DeviceSparseMatrixCSR(B_wrong))
313325
@test_throws DimensionMismatch dA + dB_wrong
314-
else
315-
# Transpose/adjoint tests - only for float and complex types
316-
if T in (int_types...)
317-
continue
318-
end
319-
320-
# Skip adjoint for real types (same as transpose)
321-
if T <: Real && (op_A === adjoint || op_B === adjoint)
322-
continue
323-
end
324-
325-
# Square matrices for transpose tests
326-
m, n = 30, 30
327-
dims_A = op_A === identity ? (m, n) : (n, m)
328-
dims_B = op_B === identity ? (m, n) : (n, m)
329-
330-
A = sprand(T, dims_A..., 0.1)
331-
B = sprand(T, dims_B..., 0.1)
332-
333-
dA = adapt(op, DeviceSparseMatrixCSR(A))
334-
dB = adapt(op, DeviceSparseMatrixCSR(B))
335-
336-
result = op_A(dA) + op_B(dB)
337-
expected = op_A(A) + op_B(B)
338-
@test collect(result) Matrix(expected)
339326
end
340327
end
341328
end

0 commit comments

Comments
 (0)