Skip to content

Commit

Permalink
test: add basic subtyping tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-dunham committed Feb 7, 2024
1 parent b13a4c3 commit 87cddee
Showing 1 changed file with 37 additions and 37 deletions.
74 changes: 37 additions & 37 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ end

@testset "construction" begin
@testset "construction ($T)" for T = (Float64, Int)
data = rand(T,10)
data = rand(T, 10)
arrays = [CircularVector(data), CircularVector{T}(data),
CircularArray(data), CircularArray{T}(data), CircularArray{T,1}(data)]
CircularArray(data), CircularArray{T}(data), CircularArray{T,1}(data)]
@test all(a == first(arrays) for a in arrays)
@test all(a isa CircularVector{T,Vector{T}} for a in arrays)
end
Expand All @@ -20,15 +20,15 @@ end
data = zeros(Float64, 2, 2)
ref = CircularArray(data)
@test CircularArray{Float64}(data) == ref
@test CircularArray{Float64, 2}(data) == ref
@test CircularArray{Float64, 2, Array{Float64, 2}}(data) == ref
@test CircularArray{Float64, 2, Matrix{Float64}}(data) == ref
@test CircularArray{Float64,2}(data) == ref
@test CircularArray{Float64,2,Array{Float64,2}}(data) == ref
@test CircularArray{Float64,2,Matrix{Float64}}(data) == ref
end
end

@testset "type stability" begin
@testset "type stability $(n)d" for n in 1:10
a = CircularArray(fill(1, ntuple(_->1, n)))
a = CircularArray(fill(1, ntuple(_ -> 1, n)))

@test @inferred(a[1]) isa Int64
@test @inferred(a[[1]]) isa CircularVector{Int64}
Expand All @@ -42,7 +42,7 @@ end

@testset "display" begin
@testset "display $(n)d" for n in 1:3
data = rand(Int64, ntuple(_->3, n))
data = rand(Int64, ntuple(_ -> 3, n))
v1 = CircularArray(data)
io = IOBuffer()
io_compare = IOBuffer()
Expand Down Expand Up @@ -73,13 +73,13 @@ end
@test isa(v1, CircularVector)
@test isa(v1, AbstractVector{Int})
@test !isa(v1, AbstractVector{String})
@test v1[2] == v1[2 + length(v1)]
@test v1[2] == v1[2+length(v1)]

@test IndexStyle(v1) == IndexStyle(typeof(v1)) == IndexLinear()
@test v1[0] == data[end]
@test v1[-4:10] == [data; data; data]
@test v1[-3:1][-1] == data[end]
@test v1[[true,false,true,false,true]] == v1[[1,3,0]]
@test v1[[true, false, true, false, true]] == v1[[1, 3, 0]]
@test all(e in data for e in v1)
@test all(e in v1 for e in data)

Expand Down Expand Up @@ -130,7 +130,7 @@ end
end

@testset "type stability" begin
v3 = @inferred(map(x -> x+1, CircularArray([1, 2, 3, 4])))
v3 = @inferred(map(x -> x + 1, CircularArray([1, 2, 3, 4])))
@test v3 isa CircularVector{Int64}
@test v3 == CircularArray([2, 3, 4, 5])
@test similar(v3, Base.OneTo(4)) isa typeof(v3)
Expand All @@ -143,7 +143,7 @@ end
@test v4 == CircularArray([2, 3, 4, 5])

v5 = v4 .> 3
@test v5 isa CircularVector{Bool, BitVector}
@test v5 isa CircularVector{Bool,BitVector}
@test v5 == CircularArray([0, 0, 1, 1])
end
end
Expand All @@ -166,11 +166,11 @@ end
@test a1[1, 3] == 99

a1[18] = 9
@test a1[18] == a1[-6] == a1[6] == a1[3,2] == a1[0,6] == b_arr[3,2] == b_arr[6] == 9
@test a1[18] == a1[-6] == a1[6] == a1[3, 2] == a1[0, 6] == b_arr[3, 2] == b_arr[6] == 9

@test IndexStyle(a1) == IndexStyle(typeof(a1)) == IndexCartesian()
@test a1[3] == a1[3,1]
@test a1[Int32(4)] == a1[1,2]
@test a1[3] == a1[3, 1]
@test a1[Int32(4)] == a1[1, 2]
@test a1[-1] == a1[length(a1)-1]

@test a1[2, 3, 1] == 17 # trailing index
Expand All @@ -186,11 +186,11 @@ end

a2 = CircularMatrix(4, (2, 3))
@test isa(a2, CircularMatrix{Int})
@test isa(a2, CircularArray{Int, 2})
@test isa(a2, CircularArray{Int,2})

a3 = @inferred(a2 .+ 1)
@test a3 isa CircularMatrix{Int64}
@test a3 isa CircularArray{Int64, 2}
@test a3 isa CircularArray{Int64,2}
@test a3 == CircularArray(5, (2, 3))

@testset "doubly circular" begin
Expand All @@ -204,30 +204,30 @@ end
end

@testset "3-array" begin
t3 = collect(reshape('a':'x', 2,3,4))
t3 = collect(reshape('a':'x', 2, 3, 4))
c3 = CircularArray(t3)

@test parent(c3) == t3

@test c3[1,3,3] == c3[3,3,3] == c3[3,3,7] == c3[3,3,7,1]
@test c3[1, 3, 3] == c3[3, 3, 3] == c3[3, 3, 7] == c3[3, 3, 7, 1]

c3[3,3,7] = 'Z'
@test t3[1,3,3] == 'Z'
c3[3, 3, 7] = 'Z'
@test t3[1, 3, 3] == 'Z'

@test c3[3, CartesianIndex(3,7)] == 'Z'
c3[Int32(3), CartesianIndex(3,7)] = 'ζ'
@test t3[1,3,3] == 'ζ'
@test c3[3, CartesianIndex(3, 7)] == 'Z'
c3[Int32(3), CartesianIndex(3, 7)] = 'ζ'
@test t3[1, 3, 3] == 'ζ'

c3[34] = 'J'
@test c3[34] == c3[-38] == c3[10] == c3[2,2,2] == c3[4,5,6] == t3[2,2,2] == t3[10] == 'J'
@test c3[34] == c3[-38] == c3[10] == c3[2, 2, 2] == c3[4, 5, 6] == t3[2, 2, 2] == t3[10] == 'J'

@test vec(c3[:, [CartesianIndex()], 1, 5]) == vec(t3[:, 1, 1])

@test IndexStyle(c3) == IndexStyle(typeof(c3)) == IndexCartesian()
@test c3[-1] == t3[length(t3)-1]

@test_throws BoundsError c3[2,3] # too few indices
@test_throws BoundsError c3[CartesianIndex(2,3)]
@test_throws BoundsError c3[2, 3] # too few indices
@test_throws BoundsError c3[CartesianIndex(2, 3)]

@testset "doubly circular" begin
c = CircularArray(t3)
Expand All @@ -239,15 +239,15 @@ end
end

@testset "offset indices" begin
i = OffsetArray(1:5,-3)
i = OffsetArray(1:5, -3)
a = CircularArray(i)
@test axes(a) == axes(i)
@test a[1] == 4
@test a[10] == a[-10] == a[0] == 3
@test a[-2:7] == [1:5; 1:5]
@test a[0:9] == [3:5; 1:5; 1:2]
@test a[1:10][-10] == 3
@test a[i] == OffsetArray([4,5,1,2,3],-3)
@test a[i] == OffsetArray([4, 5, 1, 2, 3], -3)

@testset "type stability" begin
@test @inferred(similar(a)) isa CircularVector
Expand All @@ -258,18 +258,18 @@ end
@test @inferred(similar(typeof(b), 3:5)) isa CircularVector
end

circ_a = circshift(a,3)
circ_a = circshift(a, 3)
@test axes(circ_a) == axes(a)
@test circ_a[1:5] == 1:5

j = OffsetArray([true,false,true],1)
@test a[j] == [5,2]
j = OffsetArray([true, false, true], 1)
@test a[j] == [5, 2]

data = reshape(1:9,3,3)
a = CircularArray(OffsetArray(data,-1,-1))
data = reshape(1:9, 3, 3)
a = CircularArray(OffsetArray(data, -1, -1))
@test collect(a) == data
@test all(a[x,y] == data[mod1(x+1,3),mod1(y+1,3)] for x=-10:10, y=-10:10)
@test a[i,1] == CircularArray(OffsetArray([5,6,4,5,6],-2:2))
@test a[CartesianIndex.(i,i)] == CircularArray(OffsetArray([5,9,1,5,9],-2:2))
@test a[a .> 4] == 5:9
@test all(a[x, y] == data[mod1(x + 1, 3), mod1(y + 1, 3)] for x = -10:10, y = -10:10)
@test a[i, 1] == CircularArray(OffsetArray([5, 6, 4, 5, 6], -2:2))
@test a[CartesianIndex.(i, i)] == CircularArray(OffsetArray([5, 9, 1, 5, 9], -2:2))
@test a[a.>4] == 5:9
end

0 comments on commit 87cddee

Please sign in to comment.