Skip to content

Commit b0d5220

Browse files
author
Pietro Vertechi
authored
Add tests (#80)
1 parent 6d08e4b commit b0d5220

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/lazy.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ end
3434
Base.parent(v::LazyRows) = getfield(v, 1)
3535
fieldarrays(v::LazyRows) = fieldarrays(parent(v))
3636

37+
Base.getproperty(s::LazyRows, key::Symbol) = getproperty(parent(s), key)
38+
Base.getproperty(s::LazyRows, key::Int) = getproperty(parent(s), key)
39+
Base.propertynames(c::LazyRows) = propertynames(parent(c))
40+
41+
staticschema(::Type{LazyRows{T, N, C, I}}) where {T, N, C, I} = staticschema(StructArray{T, N, C, I})
42+
3743
Base.size(v::LazyRows) = size(parent(v))
3844
Base.getindex(v::LazyRows{<:Any, <:Any, <:Any, Int}, i::Int) = LazyRow(parent(v), i)
3945
Base.getindex(v::LazyRows{<:Any, <:Any, <:Any, CartesianIndex{N}}, i::Vararg{Int, N}) where {N} = LazyRow(parent(v), CartesianIndex(i))

test/runtests.jl

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using StructArrays
2+
using StructArrays: staticschema, iscompatible, _promote_typejoin
23
using OffsetArrays: OffsetArray
34
import Tables, PooledArrays, WeakRefStrings
45
using Test
@@ -18,6 +19,28 @@ end
1819
@test StructArrays.propertynames(StructArrays.fieldarrays(t)) == (:a, :b)
1920
end
2021

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+
2144
@testset "indexstyle" begin
2245
@inferred IndexStyle(StructArray(a=rand(10,10), b=view(rand(100,100), 1:10, 1:10)))
2346
s = StructArray(a=rand(10,10), b=view(rand(100,100), 1:10, 1:10))
@@ -66,6 +89,13 @@ end
6689
@test StructArrays.roweq(strs, 1, 2)
6790
@test !StructArrays.roweq(strs, 1, 3)
6891
@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
6999
end
70100

71101
@testset "permute" begin
@@ -144,6 +174,9 @@ end
144174
a = [1, 2, 1, 1, 0, 9, -100]
145175
b = [-2, 12, 1, 1, 0, 11, 9]
146176
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}}
147180
s = StructArray(itr)
148181
as, bs = fieldarrays(s)
149182
@test as == [1:1, 1:0, 2:2, 3:5, 6:6, 7:7, 1:0, 1:0]
@@ -302,8 +335,11 @@ end
302335
@test Tables.columns(s).a == [1]
303336
@test Tables.columns(s).b == ["test"]
304337
@test Tables.istable(s)
338+
@test Tables.istable(typeof(s))
305339
@test Tables.rowaccess(s)
340+
@test Tables.rowaccess(typeof(s))
306341
@test Tables.columnaccess(s)
342+
@test Tables.columnaccess(typeof(s))
307343
end
308344

309345
struct S
@@ -502,16 +538,38 @@ end
502538
end
503539

504540
@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+
505555
s = StructArray(rand(ComplexF64, 10, 10))
506556
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}
507562
@test IndexStyle(rows) isa IndexLinear
563+
@test IndexStyle(typeof(rows)) isa IndexLinear
508564
@test all(t -> t.re >= 0, s)
509565
@test all(t -> t.re >= 0, rows)
510566
rows[13].re = -12
511567
rows[13].im = 0
512568
@test !all(t -> t.re >= 0, s)
513569
@test !all(t -> t.re >= 0, rows)
514570

571+
@test !all(t -> t.re >= 0, s)
572+
@test !all(t -> t.re >= 0, rows)
515573
io = IOBuffer()
516574
show(io, rows[13])
517575
str = String(take!(io))
@@ -529,6 +587,7 @@ end
529587
s = StructArray((rand(10, 10), rand(10, 10)))
530588
rows = LazyRows(s)
531589
@test IndexStyle(rows) isa IndexLinear
590+
@test IndexStyle(typeof(rows)) isa IndexLinear
532591
@test all(t -> StructArrays._getproperty(t, 1) >= 0, s)
533592
@test all(t -> getproperty(t, 1) >= 0, rows)
534593
setproperty!(rows[13], 1, -12)

0 commit comments

Comments
 (0)