Skip to content

Commit ade3654

Browse files
KristofferCKristofferC
andauthored
port bitarray tests to LinearAlgebra.jl (#1148)
Fixes #1146 Co-authored-by: KristofferC <kristoffer.carlsson@juliacomputing.com>
1 parent 130b94a commit ade3654

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

test/bitarray.jl

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using LinearAlgebra, Test, Random
2+
3+
tc(r1::NTuple{N,Any}, r2::NTuple{N,Any}) where {N} = all(x->tc(x...), [zip(r1,r2)...])
4+
tc(r1::BitArray{N}, r2::Union{BitArray{N},Array{Bool,N}}) where {N} = true
5+
tc(r1::SubArray{Bool,N1,BitArray{N2}}, r2::SubArray{Bool,N1,<:Union{BitArray{N2},Array{Bool,N2}}}) where {N1,N2} = true
6+
tc(r1::Transpose{Bool,BitVector}, r2::Union{Transpose{Bool,BitVector},Transpose{Bool,Vector{Bool}}}) = true
7+
tc(r1::T, r2::T) where {T} = true
8+
tc(r1,r2) = false
9+
10+
# vectors size
11+
const v1 = 260
12+
# matrices size
13+
const n1, n2 = 17, 20
14+
# arrays size
15+
const s1, s2, s3, s4 = 5, 8, 3, 7
16+
17+
bitcheck(b::BitArray) = Test._check_bitarray_consistency(b)
18+
bitcheck(x) = true
19+
20+
function check_bitop_call(ret_type, func, args...; kwargs...)
21+
r2 = func(map(x->(isa(x, BitArray) ? Array(x) : x), args)...; kwargs...)
22+
r1 = func(args...; kwargs...)
23+
ret_type nothing && (@test isa(r1, ret_type) || @show ret_type, typeof(r1))
24+
@test tc(r1, r2)
25+
@test isequal(r1, r2)
26+
@test bitcheck(r1)
27+
end
28+
macro check_bit_operation(ex, ret_type)
29+
@assert Meta.isexpr(ex, :call)
30+
Expr(:call, :check_bitop_call, esc(ret_type), map(esc, ex.args)...)
31+
end
32+
macro check_bit_operation(ex)
33+
@assert Meta.isexpr(ex, :call)
34+
Expr(:call, :check_bitop_call, nothing, map(esc, ex.args)...)
35+
end
36+
37+
38+
b1 = bitrand(v1)
39+
b2 = bitrand(v1)
40+
@check_bit_operation dot(b1, b2) Int
41+
42+
b1 = bitrand(n1, n2)
43+
@test_throws ArgumentError tril(b1, -n1 - 2)
44+
@test_throws ArgumentError tril(b1, n2)
45+
@test_throws ArgumentError triu(b1, -n1)
46+
@test_throws ArgumentError triu(b1, n2 + 2)
47+
for k in (-n1 - 1):(n2 - 1)
48+
@check_bit_operation tril(b1, k) BitMatrix
49+
end
50+
for k in (-n1 + 1):(n2 + 1)
51+
@check_bit_operation triu(b1, k) BitMatrix
52+
end
53+
54+
for sz = [(n1,n1), (n1,n2), (n2,n1)], (f,isf) = [(tril,istril), (triu,istriu)]
55+
_b1 = bitrand(sz...)
56+
@check_bit_operation isf(_b1) Bool
57+
_b1 = f(bitrand(sz...))
58+
@check_bit_operation isf(_b1) Bool
59+
end
60+
61+
b1 = bitrand(n1,n1)
62+
b1 .|= copy(b1')
63+
@check_bit_operation issymmetric(b1) Bool
64+
@check_bit_operation ishermitian(b1) Bool
65+
66+
b1 = bitrand(n1)
67+
b2 = bitrand(n2)
68+
@check_bit_operation kron(b1, b2) BitVector
69+
70+
b1 = bitrand(s1, s2)
71+
b2 = bitrand(s3, s4)
72+
@check_bit_operation kron(b1, b2) BitMatrix
73+
74+
b1 = bitrand(v1)
75+
@check_bit_operation diff(b1) Vector{Int}
76+
77+
b1 = bitrand(n1, n2)
78+
@check_bit_operation diff(b1, dims=1) Matrix{Int}
79+
@check_bit_operation diff(b1, dims=2) Matrix{Int}
80+
81+
b1 = bitrand(n1, n1)
82+
@test ((svdb1, svdb1A) = (svd(b1), svd(Array(b1)));
83+
svdb1.U == svdb1A.U && svdb1.S == svdb1A.S && svdb1.V == svdb1A.V)
84+
@test ((qrb1, qrb1A) = (qr(b1), qr(Array(b1)));
85+
Matrix(qrb1.Q) == Matrix(qrb1A.Q) && qrb1.R == qrb1A.R)
86+
87+
b1 = bitrand(v1)
88+
@check_bit_operation diagm(0 => b1) BitMatrix
89+
90+
b1 = bitrand(v1)
91+
b2 = bitrand(v1)
92+
@check_bit_operation diagm(-1 => b1, 1 => b2) BitMatrix
93+
94+
b1 = bitrand(n1, n1)
95+
@check_bit_operation diag(b1)

test/testgroups

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
triangular
22
addmul
33
bidiag
4+
bitarray
45
matmul
56
dense
67
symmetric

0 commit comments

Comments
 (0)