|
| 1 | +using BlockArrays: Block, BlockIndex, BlockedVector, block, blockindex |
| 2 | +using BlockSparseArrays: BlockSparseArrays, BlockIndexVector, GenericBlockIndex |
| 3 | +using Test: @test, @test_broken, @testset |
| 4 | + |
| 5 | +# blockrange |
| 6 | +# checkindex |
| 7 | +# to_indices |
| 8 | +# to_index |
| 9 | +# blockedunitrange_getindices |
| 10 | +# viewblock |
| 11 | +# to_blockindexrange |
| 12 | + |
| 13 | +@testset "GenericBlockIndex" begin |
| 14 | + i1 = GenericBlockIndex(Block(1), ("x",)) |
| 15 | + i2 = GenericBlockIndex(Block(2), ("y",)) |
| 16 | + i = GenericBlockIndex(Block(1, 2), ("x", "y")) |
| 17 | + @test sprint(show, i) == "Block(1, 2)[x, y]" |
| 18 | + @test i isa GenericBlockIndex{2,Tuple{Int64,Int64},Tuple{String,String}} |
| 19 | + @test GenericBlockIndex(Block(1), "x") === i1 |
| 20 | + @test GenericBlockIndex(1, "x") === i1 |
| 21 | + @test GenericBlockIndex(1, ("x",)) === i1 |
| 22 | + @test GenericBlockIndex((1,), "x") === i1 |
| 23 | + @test GenericBlockIndex((1, 2), ("x", "y")) === i |
| 24 | + @test GenericBlockIndex((Block(1), Block(2)), ("x", "y")) === i |
| 25 | + @test GenericBlockIndex((i1, i2)) === i |
| 26 | + @test block(i1) == Block(1) |
| 27 | + @test block(i) == Block(1, 2) |
| 28 | + @test blockindex(i1) == "x" |
| 29 | + @test GenericBlockIndex((), ()) == GenericBlockIndex(Block(), ()) |
| 30 | + @test GenericBlockIndex(Block(1, 2), ("x",)) == GenericBlockIndex(Block(1, 2), ("x", 1)) |
| 31 | + |
| 32 | + i1 = GenericBlockIndex(Block(1), (1,)) |
| 33 | + i2 = GenericBlockIndex(Block(2), (2,)) |
| 34 | + i = GenericBlockIndex(Block(1, 2), (1, 2)) |
| 35 | + v = BlockedVector(["a", "b", "c", "d"], [2, 2]) |
| 36 | + @test v[i1] == "a" |
| 37 | + @test v[i2] == "d" |
| 38 | + |
| 39 | + a = collect(Iterators.product(v, v)) |
| 40 | + @test a[i1, i1] == ("a", "a") |
| 41 | + @test a[i2, i1] == ("d", "a") |
| 42 | + @test a[i1, i2] == ("a", "d") |
| 43 | + @test a[i] == ("a", "d") |
| 44 | + @test a[i2, i2] == ("d", "d") |
| 45 | + |
| 46 | + I = BlockIndexVector(Block(1), [1, 2]) |
| 47 | + @test eltype(I) === BlockIndex{1,Tuple{Int},Tuple{Int}} |
| 48 | + @test ndims(I) === 1 |
| 49 | + @test length(I) === 2 |
| 50 | + @test size(I) === (2,) |
| 51 | + @test I[1] === Block(1)[1] |
| 52 | + @test I[2] === Block(1)[2] |
| 53 | + @test block(I) === Block(1) |
| 54 | + @test Block(I) === Block(1) |
| 55 | + @test copy(I) == BlockIndexVector(Block(1), [1, 2]) |
| 56 | + |
| 57 | + I = BlockIndexVector(Block(1, 2), ([1, 2], [3, 4])) |
| 58 | + @test eltype(I) === BlockIndex{2,Tuple{Int,Int},Tuple{Int,Int}} |
| 59 | + @test ndims(I) === 2 |
| 60 | + @test length(I) === 4 |
| 61 | + @test size(I) === (2, 2) |
| 62 | + @test I[1, 1] === Block(1, 2)[1, 3] |
| 63 | + @test I[2, 1] === Block(1, 2)[2, 3] |
| 64 | + @test I[1, 2] === Block(1, 2)[1, 4] |
| 65 | + @test I[2, 2] === Block(1, 2)[2, 4] |
| 66 | + @test block(I) === Block(1, 2) |
| 67 | + @test Block(I) === Block(1, 2) |
| 68 | + @test copy(I) == BlockIndexVector(Block(1, 2), ([1, 2], [3, 4])) |
| 69 | + |
| 70 | + I = BlockIndexVector(Block(1), ["x", "y"]) |
| 71 | + @test eltype(I) === GenericBlockIndex{1,Tuple{Int},Tuple{String}} |
| 72 | + @test ndims(I) === 1 |
| 73 | + @test length(I) === 2 |
| 74 | + @test size(I) === (2,) |
| 75 | + @test I[1] === GenericBlockIndex(Block(1), "x") |
| 76 | + @test I[2] === GenericBlockIndex(Block(1), "y") |
| 77 | + @test block(I) === Block(1) |
| 78 | + @test Block(I) === Block(1) |
| 79 | + @test copy(I) == BlockIndexVector(Block(1), ["x", "y"]) |
| 80 | + |
| 81 | + I = BlockIndexVector(Block(1, 2), (["x", "y"], ["z", "w"])) |
| 82 | + @test eltype(I) === GenericBlockIndex{2,Tuple{Int,Int},Tuple{String,String}} |
| 83 | + @test ndims(I) === 2 |
| 84 | + @test length(I) === 4 |
| 85 | + @test size(I) === (2, 2) |
| 86 | + @test I[1, 1] === GenericBlockIndex(Block(1, 2), ("x", "z")) |
| 87 | + @test I[2, 1] === GenericBlockIndex(Block(1, 2), ("y", "z")) |
| 88 | + @test I[1, 2] === GenericBlockIndex(Block(1, 2), ("x", "w")) |
| 89 | + @test I[2, 2] === GenericBlockIndex(Block(1, 2), ("y", "w")) |
| 90 | + @test block(I) === Block(1, 2) |
| 91 | + @test Block(I) === Block(1, 2) |
| 92 | + @test copy(I) == BlockIndexVector(Block(1, 2), (["x", "y"], ["z", "w"])) |
| 93 | + |
| 94 | + v = BlockedVector(["a", "b", "c", "d"], [2, 2]) |
| 95 | + i = BlockIndexVector(Block(1), [2, 1]) |
| 96 | + @test v[i] == ["b", "a"] |
| 97 | + i = BlockIndexVector(Block(2), [2, 1]) |
| 98 | + @test v[i] == ["d", "c"] |
| 99 | + |
| 100 | + v = BlockedVector(["a", "b", "c", "d"], [2, 2]) |
| 101 | + i = BlockIndexVector{1,GenericBlockIndex{1,Tuple{Int},Tuple{String}}}(Block(1), [2, 1]) |
| 102 | + @test v[i] == ["b", "a"] |
| 103 | + i = BlockIndexVector(Block(2), [2, 1]) |
| 104 | + @test v[i] == ["d", "c"] |
| 105 | + |
| 106 | + a = collect(Iterators.product(v, v)) |
| 107 | + i1 = BlockIndexVector(Block(1), [2, 1]) |
| 108 | + i2 = BlockIndexVector(Block(2), [1, 2]) |
| 109 | + i = BlockIndexVector(Block(1, 2), ([2, 1], [1, 2])) |
| 110 | + @test a[i1, i1] == [("b", "b") ("b", "a"); ("a", "b") ("a", "a")] |
| 111 | + @test a[i2, i1] == [("c", "b") ("c", "a"); ("d", "b") ("d", "a")] |
| 112 | + @test a[i1, i2] == [("b", "c") ("b", "d"); ("a", "c") ("a", "d")] |
| 113 | + @test a[i] == [("b", "c") ("b", "d"); ("a", "c") ("a", "d")] |
| 114 | + @test a[i2, i2] == [("c", "c") ("c", "d"); ("d", "c") ("d", "d")] |
| 115 | + |
| 116 | + a = collect(Iterators.product(v, v)) |
| 117 | + i1 = BlockIndexVector{1,GenericBlockIndex{1,Tuple{Int},Tuple{String}}}(Block(1), [2, 1]) |
| 118 | + i2 = BlockIndexVector{1,GenericBlockIndex{1,Tuple{Int},Tuple{String}}}(Block(2), [1, 2]) |
| 119 | + i = BlockIndexVector{2,GenericBlockIndex{2,Tuple{Int,Int},Tuple{String,String}}}( |
| 120 | + Block(1, 2), ([2, 1], [1, 2]) |
| 121 | + ) |
| 122 | + @test a[i1, i1] == [("b", "b") ("b", "a"); ("a", "b") ("a", "a")] |
| 123 | + @test a[i2, i1] == [("c", "b") ("c", "a"); ("d", "b") ("d", "a")] |
| 124 | + @test a[i1, i2] == [("b", "c") ("b", "d"); ("a", "c") ("a", "d")] |
| 125 | + @test a[i] == [("b", "c") ("b", "d"); ("a", "c") ("a", "d")] |
| 126 | + @test a[i2, i2] == [("c", "c") ("c", "d"); ("d", "c") ("d", "d")] |
| 127 | +end |
0 commit comments