|
48 | 48 | x = rand(dist)
|
49 | 49 | @test vectorize(dist, x) == vec(x.UL)
|
50 | 50 | end
|
| 51 | + |
| 52 | + @testset "BangBang.possible" begin |
| 53 | + using DynamicPPL.BangBang: setindex!! |
| 54 | + |
| 55 | + # Some utility methods for testing `setindex!`. |
| 56 | + test_linear_index_only(::Tuple, ::AbstractArray) = false |
| 57 | + test_linear_index_only(inds::NTuple{1}, ::AbstractArray) = true |
| 58 | + test_linear_index_only(inds::NTuple{1}, ::AbstractVector) = false |
| 59 | + |
| 60 | + function replace_colon_with_axis(inds::Tuple, x) |
| 61 | + ntuple(length(inds)) do i |
| 62 | + inds[i] isa Colon ? axes(x, i) : inds[i] |
| 63 | + end |
| 64 | + end |
| 65 | + function replace_colon_with_vector(inds::Tuple, x) |
| 66 | + ntuple(length(inds)) do i |
| 67 | + inds[i] isa Colon ? collect(axes(x, i)) : inds[i] |
| 68 | + end |
| 69 | + end |
| 70 | + function replace_colon_with_range(inds::Tuple, x) |
| 71 | + ntuple(length(inds)) do i |
| 72 | + inds[i] isa Colon ? (1:size(x, i)) : inds[i] |
| 73 | + end |
| 74 | + end |
| 75 | + function replace_colon_with_booleans(inds::Tuple, x) |
| 76 | + ntuple(length(inds)) do i |
| 77 | + inds[i] isa Colon ? trues(size(x, i)) : inds[i] |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + function replace_colon_with_range_linear(inds::NTuple{1}, x::AbstractArray) |
| 82 | + return inds[1] isa Colon ? (1:length(x),) : inds |
| 83 | + end |
| 84 | + |
| 85 | + @testset begin |
| 86 | + @test setindex!!((1, 2, 3), :two, 2) === (1, :two, 3) |
| 87 | + @test setindex!!((a=1, b=2, c=3), :two, :b) === (a=1, b=:two, c=3) |
| 88 | + @test setindex!!([1, 2, 3], :two, 2) == [1, :two, 3] |
| 89 | + @test setindex!!(Dict{Symbol,Int}(:a => 1, :b => 2), 10, :a) == |
| 90 | + Dict(:a => 10, :b => 2) |
| 91 | + @test setindex!!(Dict{Symbol,Int}(:a => 1, :b => 2), 3, "c") == |
| 92 | + Dict(:a => 1, :b => 2, "c" => 3) |
| 93 | + end |
| 94 | + |
| 95 | + @testset "mutation" begin |
| 96 | + @testset "without type expansion" begin |
| 97 | + for args in [([1, 2, 3], 20, 2), (Dict(:a => 1, :b => 2), 10, :a)] |
| 98 | + @test setindex!!(args...) === args[1] |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + @testset "with type expansion" begin |
| 103 | + @test setindex!!([1, 2, 3], [4, 5], 1) == [[4, 5], 2, 3] |
| 104 | + @test setindex!!([1, 2, 3], [4, 5, 6], :, 1) == [4, 5, 6] |
| 105 | + end |
| 106 | + end |
| 107 | + |
| 108 | + @testset "slices" begin |
| 109 | + @testset "$(typeof(x)) with $(src_idx)" for (x, src_idx) in [ |
| 110 | + # Vector. |
| 111 | + (randn(2), (:,)), |
| 112 | + (randn(2), (1:2,)), |
| 113 | + # Matrix. |
| 114 | + (randn(2, 3), (:,)), |
| 115 | + (randn(2, 3), (:, 1)), |
| 116 | + (randn(2, 3), (:, 1:3)), |
| 117 | + # 3D array. |
| 118 | + (randn(2, 3, 4), (:, 1, :)), |
| 119 | + (randn(2, 3, 4), (:, 1:3, :)), |
| 120 | + (randn(2, 3, 4), (1, 1:3, :)), |
| 121 | + ] |
| 122 | + # Base case. |
| 123 | + @test @inferred(setindex!!(x, x[src_idx...], src_idx...)) === x |
| 124 | + |
| 125 | + # If we have `Colon` in the index, we replace this with other equivalent indices. |
| 126 | + if any(Base.Fix2(isa, Colon), src_idx) |
| 127 | + if test_linear_index_only(src_idx, x) |
| 128 | + # With range instead of `Colon`. |
| 129 | + @test @inferred( |
| 130 | + setindex!!( |
| 131 | + x, |
| 132 | + x[src_idx...], |
| 133 | + replace_colon_with_range_linear(src_idx, x)..., |
| 134 | + ) |
| 135 | + ) === x |
| 136 | + else |
| 137 | + # With axis instead of `Colon`. |
| 138 | + @test @inferred( |
| 139 | + setindex!!( |
| 140 | + x, x[src_idx...], replace_colon_with_axis(src_idx, x)... |
| 141 | + ) |
| 142 | + ) === x |
| 143 | + # With range instead of `Colon`. |
| 144 | + @test @inferred( |
| 145 | + setindex!!( |
| 146 | + x, x[src_idx...], replace_colon_with_range(src_idx, x)... |
| 147 | + ) |
| 148 | + ) === x |
| 149 | + # With vectors instead of `Colon`. |
| 150 | + @test @inferred( |
| 151 | + setindex!!( |
| 152 | + x, x[src_idx...], replace_colon_with_vector(src_idx, x)... |
| 153 | + ) |
| 154 | + ) === x |
| 155 | + # With boolean index instead of `Colon`. |
| 156 | + @test @inferred( |
| 157 | + setindex!!( |
| 158 | + x, x[src_idx...], replace_colon_with_booleans(src_idx, x)... |
| 159 | + ) |
| 160 | + ) === x |
| 161 | + end |
| 162 | + end |
| 163 | + end |
| 164 | + end |
| 165 | + end |
51 | 166 | end
|
0 commit comments