|
1 | 1 | using StructArrays
|
| 2 | +using StructArrays: staticschema, iscompatible, _promote_typejoin |
2 | 3 | using OffsetArrays: OffsetArray
|
3 | 4 | import Tables, PooledArrays, WeakRefStrings
|
4 | 5 | using Test
|
|
18 | 19 | @test StructArrays.propertynames(StructArrays.fieldarrays(t)) == (:a, :b)
|
19 | 20 | end
|
20 | 21 |
|
| 22 | +@testset "utils" begin |
| 23 | + t = StructArray(rand(ComplexF64, 2, 2)) |
| 24 | + T = staticschema(typeof(t)) |
| 25 | + @test StructArrays.eltypes(T) == NamedTuple{(:re, :im), Tuple{Float64, Float64}} |
| 26 | + @test StructArrays.map_params(eltype, T) == NamedTuple{(:re, :im), Tuple{Float64, Float64}} |
| 27 | + @test StructArrays.map_params(eltype, StructArrays.astuple(T)) == Tuple{Float64, Float64} |
| 28 | + @test !iscompatible(typeof((1, 2)), typeof(([1],))) |
| 29 | + @test iscompatible(typeof((1, 2)), typeof(([1], [2]))) |
| 30 | + @test !iscompatible(typeof((1, 2)), typeof(([1.1], [2]))) |
| 31 | + @test iscompatible(typeof(()), typeof(())) |
| 32 | + @test _promote_typejoin(Tuple{Int, Missing}, Tuple{Int, Int}) == Tuple{Int, Union{Int, Missing}} |
| 33 | + @test _promote_typejoin(Pair{Int, Missing}, Pair{Int, Int}) == Pair{Int, Union{Int, Missing}} |
| 34 | + @test _promote_typejoin(NamedTuple{(:a, :b), Tuple{Int, Missing}}, NamedTuple{(:a, :b), Tuple{Int, Int}}) == NamedTuple{(:a, :b), Tuple{Int, Union{Int, Missing}}} |
| 35 | + @test _promote_typejoin(Tuple{}, Tuple{}) == Tuple{} |
| 36 | + @test _promote_typejoin(Tuple{Int}, Tuple{Int, Int}) == Tuple{Int, Vararg{Int, N} where N} |
| 37 | + |
| 38 | + @test StructArrays.astuple(Tuple{Int}) == Tuple{Int} |
| 39 | + @test StructArrays.strip_params(Tuple{Int}) == Tuple |
| 40 | + @test StructArrays.astuple(NamedTuple{(:a,), Tuple{Float64}}) == Tuple{Float64} |
| 41 | + @test StructArrays.strip_params(NamedTuple{(:a,), Tuple{Float64}}) == NamedTuple{(:a,)} |
| 42 | +end |
| 43 | + |
21 | 44 | @testset "indexstyle" begin
|
22 | 45 | @inferred IndexStyle(StructArray(a=rand(10,10), b=view(rand(100,100), 1:10, 1:10)))
|
23 | 46 | s = StructArray(a=rand(10,10), b=view(rand(100,100), 1:10, 1:10))
|
|
66 | 89 | @test StructArrays.roweq(strs, 1, 2)
|
67 | 90 | @test !StructArrays.roweq(strs, 1, 3)
|
68 | 91 | @test !StructArrays.roweq(strs, 2, 3)
|
| 92 | + |
| 93 | + a = ["a", "c", "z", "a"] |
| 94 | + b = PooledArrays.PooledArray(["p", "y", "a", "x"]) |
| 95 | + t = StructArray((a, b)) |
| 96 | + @test StructArrays.rowcmp(s, 4, t, 4) == 0 |
| 97 | + @test StructArrays.rowcmp(s, 1, t, 1) == 1 |
| 98 | + @test StructArrays.rowcmp(s, 2, t, 3) == -1 |
69 | 99 | end
|
70 | 100 |
|
71 | 101 | @testset "permute" begin
|
|
144 | 174 | a = [1, 2, 1, 1, 0, 9, -100]
|
145 | 175 | b = [-2, 12, 1, 1, 0, 11, 9]
|
146 | 176 | itr = StructArrays.GroupJoinPerm(a, b)
|
| 177 | + @test Base.IteratorSize(itr) == Base.SizeUnknown() |
| 178 | + @test Base.IteratorEltype(itr) == Base.HasEltype() |
| 179 | + @test eltype(itr) == Tuple{UnitRange{Int}, UnitRange{Int}} |
147 | 180 | s = StructArray(itr)
|
148 | 181 | as, bs = fieldarrays(s)
|
149 | 182 | @test as == [1:1, 1:0, 2:2, 3:5, 6:6, 7:7, 1:0, 1:0]
|
|
302 | 335 | @test Tables.columns(s).a == [1]
|
303 | 336 | @test Tables.columns(s).b == ["test"]
|
304 | 337 | @test Tables.istable(s)
|
| 338 | + @test Tables.istable(typeof(s)) |
305 | 339 | @test Tables.rowaccess(s)
|
| 340 | + @test Tables.rowaccess(typeof(s)) |
306 | 341 | @test Tables.columnaccess(s)
|
| 342 | + @test Tables.columnaccess(typeof(s)) |
307 | 343 | end
|
308 | 344 |
|
309 | 345 | struct S
|
@@ -502,16 +538,38 @@ end
|
502 | 538 | end
|
503 | 539 |
|
504 | 540 | @testset "lazy" begin
|
| 541 | + s = StructArray{ComplexF64}((rand(10, 10), view(rand(100, 100), 1:10, 1:10))) |
| 542 | + rows = LazyRows(s) |
| 543 | + @test propertynames(rows) == (:re, :im) |
| 544 | + @test propertynames(rows[1]) == (:re, :im) |
| 545 | + @test staticschema(typeof(rows)) == staticschema(eltype(rows)) == staticschema(ComplexF64) |
| 546 | + @test getproperty(rows, 1) isa Matrix{Float64} |
| 547 | + @test getproperty(rows, :re) isa Matrix{Float64} |
| 548 | + @test IndexStyle(rows) isa IndexCartesian |
| 549 | + @test IndexStyle(typeof(rows)) isa IndexCartesian |
| 550 | + @test all(t -> t.re >= 0, s) |
| 551 | + @test all(t -> t.re >= 0, rows) |
| 552 | + rows[13].re = -12 |
| 553 | + rows[13].im = 0 |
| 554 | + |
505 | 555 | s = StructArray(rand(ComplexF64, 10, 10))
|
506 | 556 | rows = LazyRows(s)
|
| 557 | + @test propertynames(rows) == (:re, :im) |
| 558 | + @test propertynames(rows[1]) == (:re, :im) |
| 559 | + @test staticschema(typeof(rows)) == staticschema(eltype(rows)) == staticschema(ComplexF64) |
| 560 | + @test getproperty(rows, 1) isa Matrix{Float64} |
| 561 | + @test getproperty(rows, :re) isa Matrix{Float64} |
507 | 562 | @test IndexStyle(rows) isa IndexLinear
|
| 563 | + @test IndexStyle(typeof(rows)) isa IndexLinear |
508 | 564 | @test all(t -> t.re >= 0, s)
|
509 | 565 | @test all(t -> t.re >= 0, rows)
|
510 | 566 | rows[13].re = -12
|
511 | 567 | rows[13].im = 0
|
512 | 568 | @test !all(t -> t.re >= 0, s)
|
513 | 569 | @test !all(t -> t.re >= 0, rows)
|
514 | 570 |
|
| 571 | + @test !all(t -> t.re >= 0, s) |
| 572 | + @test !all(t -> t.re >= 0, rows) |
515 | 573 | io = IOBuffer()
|
516 | 574 | show(io, rows[13])
|
517 | 575 | str = String(take!(io))
|
|
529 | 587 | s = StructArray((rand(10, 10), rand(10, 10)))
|
530 | 588 | rows = LazyRows(s)
|
531 | 589 | @test IndexStyle(rows) isa IndexLinear
|
| 590 | + @test IndexStyle(typeof(rows)) isa IndexLinear |
532 | 591 | @test all(t -> StructArrays._getproperty(t, 1) >= 0, s)
|
533 | 592 | @test all(t -> getproperty(t, 1) >= 0, rows)
|
534 | 593 | setproperty!(rows[13], 1, -12)
|
|
0 commit comments