|
1 | 1 | using CircularArrays |
| 2 | +using OffsetArrays |
2 | 3 | using Test |
3 | 4 |
|
4 | | -v1 = CircularVector(rand(Int64, 5)) |
5 | | - |
6 | 5 | @test IndexStyle(CircularArray) == IndexCartesian() |
7 | 6 | @test IndexStyle(CircularVector) == IndexLinear() |
8 | 7 |
|
9 | | -@test size(v1, 1) == 5 |
10 | | -@test typeof(v1) == CircularVector{Int64} |
11 | | -@test isa(v1, CircularVector) |
12 | | -@test isa(v1, AbstractVector{Int}) |
13 | | -@test !isa(v1, AbstractVector{String}) |
14 | | -@test v1[2] == v1[2 + length(v1)] |
15 | | -v1[2] = 0 |
16 | | -v1[3] = 0 |
17 | | -@test v1[2] == v1[3] |
18 | | -@test_throws MethodError v1[2] = "Hello" |
19 | | - |
20 | | -v2 = CircularVector("abcde", 5) |
21 | | - |
22 | | -@test prod(v2) == "abcde"^5 |
23 | | - |
24 | | -@test_throws MethodError push!(v1, 15) |
25 | | - |
26 | | -b_arr = [2 4 6 8; 10 12 14 16; 18 20 22 24] |
27 | | -a1 = CircularArray(b_arr) |
28 | | -@test size(a1) == (3, 4) |
29 | | -@test a1[2, 3] == 14 |
30 | | -a1[2, 3] = 17 |
31 | | -@test a1[2, 3] == 17 |
32 | | -@test !isa(a1, CircularVector) |
33 | | -@test !isa(a1, AbstractVector) |
34 | | -@test isa(a1, AbstractArray) |
35 | | - |
36 | | -@test size(reshape(a1, (2, 2, 3))) == (2, 2, 3) |
37 | | - |
38 | | -a2 = CircularArray(4, (2, 3)) |
39 | | -@test isa(a2, CircularArray{Int, 2}) |
| 8 | +@testset "vector" begin |
| 9 | + data = rand(Int64, 5) |
| 10 | + v1 = CircularVector(data) |
| 11 | + |
| 12 | + @test size(v1, 1) == 5 |
| 13 | + @test typeof(v1) == CircularVector{Int64} |
| 14 | + @test isa(v1, CircularVector) |
| 15 | + @test isa(v1, AbstractVector{Int}) |
| 16 | + @test !isa(v1, AbstractVector{String}) |
| 17 | + @test v1[2] == v1[2 + length(v1)] |
| 18 | + |
| 19 | + @test v1[0] == data[end] |
| 20 | + @test v1[-4:10] == [data; data; data] |
| 21 | + @test v1[-3:1][-1] == data[end] |
| 22 | + @test v1[[true,false,true,false,true]] == v1[[1,3,0]] |
| 23 | + |
| 24 | + v1copy = copy(v1) |
| 25 | + v1_2 = v1[2] |
| 26 | + v1[2] = 0 |
| 27 | + v1[3] = 0 |
| 28 | + @test v1[2] == v1[3] == 0 |
| 29 | + @test v1copy[2] == v1_2 |
| 30 | + @test v1copy[7] == v1_2 |
| 31 | + @test_throws MethodError v1[2] = "Hello" |
| 32 | + |
| 33 | + v2 = CircularVector("abcde", 5) |
| 34 | + |
| 35 | + @test prod(v2) == "abcde"^5 |
| 36 | + |
| 37 | + @test_throws MethodError push!(v1, 15) |
| 38 | +end |
| 39 | + |
| 40 | +@testset "matrix" begin |
| 41 | + b_arr = [2 4 6 8; 10 12 14 16; 18 20 22 24] |
| 42 | + a1 = CircularArray(b_arr) |
| 43 | + @test size(a1) == (3, 4) |
| 44 | + @test a1[2, 3] == 14 |
| 45 | + a1[2, 3] = 17 |
| 46 | + @test a1[2, 3] == 17 |
| 47 | + @test a1[-1, 7] == 17 |
| 48 | + @test a1[-1:5, 4:10][1, 4] == 17 |
| 49 | + @test a1[:, -1:-1][2, 1] == 17 |
| 50 | + @test !isa(a1, CircularVector) |
| 51 | + @test !isa(a1, AbstractVector) |
| 52 | + @test isa(a1, AbstractArray) |
| 53 | + |
| 54 | + @test size(reshape(a1, (2, 2, 3))) == (2, 2, 3) |
| 55 | + |
| 56 | + a2 = CircularArray(4, (2, 3)) |
| 57 | + @test isa(a2, CircularArray{Int, 2}) |
| 58 | +end |
| 59 | + |
| 60 | +@testset "offset indices" begin |
| 61 | + i = OffsetArray(1:5,-3) |
| 62 | + a = CircularArray(i) |
| 63 | + @test axes(a) == axes(i) |
| 64 | + @test a[1] == 4 |
| 65 | + @test a[10] == a[-10] == a[0] == 3 |
| 66 | + @test a[-2:7] == [1:5; 1:5] |
| 67 | + @test a[0:9] == [3:5; 1:5; 1:2] |
| 68 | + @test a[1:10][-10] == 3 |
| 69 | + @test a[i] == OffsetArray([4,5,1,2,3],-3) |
| 70 | + |
| 71 | + circ_a = circshift(a,3) |
| 72 | + @test axes(circ_a) == axes(a) |
| 73 | + @test circ_a[1:5] == 1:5 |
| 74 | + |
| 75 | + j = OffsetArray([true,false,true],1) |
| 76 | + @test a[j] == [5,2] |
| 77 | + |
| 78 | + data = reshape(1:9,3,3) |
| 79 | + a = CircularArray(OffsetArray(data,-1,-1)) |
| 80 | + @test collect(a) == data |
| 81 | + @test all(a[x,y] == data[mod1(x+1,3),mod1(y+1,3)] for x=-10:10, y=-10:10) |
| 82 | + @test a[i,1] == CircularArray(OffsetArray([5,6,4,5,6],-2:2)) |
| 83 | + @test a[CartesianIndex.(i,i)] == CircularArray(OffsetArray([5,9,1,5,9],-2:2)) |
| 84 | + @test a[a .> 4] == 5:9 |
| 85 | +end |
0 commit comments