|
| 1 | +using AllocCheck |
| 2 | +using BenchmarkTools |
| 3 | +using FastAlmostBandedMatrices |
| 4 | +using FastAlmostBandedMatrices: DisjointRange |
| 5 | +using ArrayLayouts: colsupport, rowsupport |
| 6 | +using Test |
| 7 | + |
| 8 | +@testset "Allocation Tests" begin |
| 9 | + @testset "DisjointRange - Zero Allocations" begin |
| 10 | + # Test that DisjointRange operations don't allocate |
| 11 | + r1 = Base.OneTo(5) |
| 12 | + r2 = 10:15 |
| 13 | + dr = DisjointRange(r1, r2) |
| 14 | + |
| 15 | + # Test length |
| 16 | + allocs = @allocated length(dr) |
| 17 | + @test allocs == 0 |
| 18 | + |
| 19 | + # Test getindex |
| 20 | + allocs = @allocated dr[3] |
| 21 | + @test allocs == 0 |
| 22 | + |
| 23 | + allocs = @allocated dr[8] |
| 24 | + @test allocs == 0 |
| 25 | + |
| 26 | + # Test first/last |
| 27 | + allocs = @allocated first(dr) |
| 28 | + @test allocs == 0 |
| 29 | + |
| 30 | + allocs = @allocated last(dr) |
| 31 | + @test allocs == 0 |
| 32 | + |
| 33 | + # Test iteration (after warmup) |
| 34 | + sum_test = 0 |
| 35 | + for x in dr |
| 36 | + sum_test += x |
| 37 | + end |
| 38 | + allocs = @allocated begin |
| 39 | + s = 0 |
| 40 | + for x in dr |
| 41 | + s += x |
| 42 | + end |
| 43 | + s |
| 44 | + end |
| 45 | + @test allocs == 0 |
| 46 | + end |
| 47 | + |
| 48 | + @testset "colsupport - Zero Allocations" begin |
| 49 | + n = 100 |
| 50 | + m = 2 |
| 51 | + B = brand(Float64, n, n, m + 1, m) |
| 52 | + F = rand(Float64, m, n) |
| 53 | + A = AlmostBandedMatrix(B, F) |
| 54 | + |
| 55 | + # Warmup |
| 56 | + colsupport(A, 5) |
| 57 | + colsupport(A, 50) |
| 58 | + |
| 59 | + # Test colsupport for j <= l+u (should return OneTo, no allocation) |
| 60 | + allocs = @allocated colsupport(A, 5) |
| 61 | + @test allocs == 0 |
| 62 | + |
| 63 | + # Test colsupport for j > l+u (now returns DisjointRange instead of vcat) |
| 64 | + allocs = @allocated colsupport(A, 50) |
| 65 | + @test allocs == 0 |
| 66 | + end |
| 67 | + |
| 68 | + @testset "rowsupport - Zero Allocations" begin |
| 69 | + n = 100 |
| 70 | + m = 2 |
| 71 | + B = brand(Float64, n, n, m + 1, m) |
| 72 | + F = rand(Float64, m, n) |
| 73 | + A = AlmostBandedMatrix(B, F) |
| 74 | + |
| 75 | + # Warmup |
| 76 | + rowsupport(A, 1) |
| 77 | + rowsupport(A, 50) |
| 78 | + |
| 79 | + # Test rowsupport (always returns UnitRange, no allocation) |
| 80 | + allocs = @allocated rowsupport(A, 1) |
| 81 | + @test allocs == 0 |
| 82 | + |
| 83 | + allocs = @allocated rowsupport(A, 50) |
| 84 | + @test allocs == 0 |
| 85 | + end |
| 86 | + |
| 87 | + @testset "getindex/setindex! - Zero Allocations" begin |
| 88 | + n = 100 |
| 89 | + m = 2 |
| 90 | + B = brand(Float64, n, n, m + 1, m) |
| 91 | + F = rand(Float64, m, n) |
| 92 | + A = AlmostBandedMatrix(B, F) |
| 93 | + |
| 94 | + # Warmup |
| 95 | + _ = A[50, 50] |
| 96 | + A[50, 50] = 1.0 |
| 97 | + |
| 98 | + # Test getindex |
| 99 | + allocs = @allocated A[50, 50] |
| 100 | + @test allocs == 0 |
| 101 | + |
| 102 | + # Test setindex! in band part |
| 103 | + allocs = @allocated A[50, 50] = 2.0 |
| 104 | + @test allocs == 0 |
| 105 | + |
| 106 | + # Test setindex! in fill part |
| 107 | + allocs = @allocated A[1, 50] = 3.0 |
| 108 | + @test allocs == 0 |
| 109 | + |
| 110 | + # Test setindex! in overlapping part |
| 111 | + allocs = @allocated A[1, 1] = 4.0 |
| 112 | + @test allocs == 0 |
| 113 | + end |
| 114 | + |
| 115 | + @testset "bandpart/fillpart - Zero Allocations" begin |
| 116 | + n = 100 |
| 117 | + m = 2 |
| 118 | + B = brand(Float64, n, n, m + 1, m) |
| 119 | + F = rand(Float64, m, n) |
| 120 | + A = AlmostBandedMatrix(B, F) |
| 121 | + |
| 122 | + # Warmup |
| 123 | + bandpart(A) |
| 124 | + fillpart(A) |
| 125 | + |
| 126 | + # Test bandpart |
| 127 | + allocs = @allocated bandpart(A) |
| 128 | + @test allocs == 0 |
| 129 | + |
| 130 | + # Test fillpart |
| 131 | + allocs = @allocated fillpart(A) |
| 132 | + @test allocs == 0 |
| 133 | + end |
| 134 | +end |
0 commit comments