-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from MSeeker1340/named-tuple
Named tuple conversion
- Loading branch information
Showing
7 changed files
with
248 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,71 @@ | ||
using LabelledArrays, Test, InteractiveUtils | ||
|
||
x = @LArray [1.0,2.0,3.0] (:a,:b,:c) | ||
y = @LVector Float64 (:a,:b,:c) | ||
y .= [1,2,3.] | ||
@test x == y | ||
@testset "Basic interface" begin | ||
x = @LArray [1.0,2.0,3.0] (:a,:b,:c) | ||
y = @LVector Float64 (:a,:b,:c) | ||
y .= [1,2,3.] | ||
@test x == y | ||
|
||
syms = (:a,:b,:c) | ||
syms = (:a,:b,:c) | ||
|
||
for (i,s) in enumerate(syms) | ||
@show i,s | ||
@test x[i] == x[s] | ||
for (i,s) in enumerate(syms) | ||
@show i,s | ||
@test x[i] == x[s] | ||
end | ||
|
||
x[:a] | ||
|
||
f(x) = x[1] | ||
g(x) = x.a | ||
@time f(x) | ||
@time f(x) | ||
@time g(x) | ||
@time g(x) | ||
|
||
#@code_warntype x.a | ||
#@inferred getindex(x,:a) | ||
@code_warntype g(x) | ||
@inferred g(x) | ||
|
||
x = @LArray [1,2,3] (:a,:b,:c) | ||
@test x .* x isa LArray | ||
@test x .+ 1 isa LArray | ||
@test x .+ 1. isa LArray | ||
z = x .+ ones(Int, 3) | ||
@test z isa LArray && eltype(z) === Int | ||
z = x .+ ones(Float64, 3) | ||
@test z isa LArray && eltype(z) === Float64 | ||
@test eltype(x .+ 1.) === Float64 | ||
|
||
z = @LArray Float64 (2,2) (:a,:b,:c,:d) | ||
w = rand(2,2) | ||
z .= w | ||
|
||
@test z[:a] == w[1,1] | ||
@test z[:b] == w[2,1] | ||
@test z[:c] == w[1,2] | ||
@test z[:d] == w[2,2] | ||
end | ||
|
||
x[:a] | ||
|
||
f(x) = x[1] | ||
g(x) = x.a | ||
@time f(x) | ||
@time f(x) | ||
@time g(x) | ||
@time g(x) | ||
|
||
#@code_warntype x.a | ||
#@inferred getindex(x,:a) | ||
@code_warntype g(x) | ||
@inferred g(x) | ||
|
||
x = @LArray [1,2,3] (:a,:b,:c) | ||
@test x .* x isa LArray | ||
@test x .+ 1 isa LArray | ||
@test x .+ 1. isa LArray | ||
z = x .+ ones(Int, 3) | ||
@test z isa LArray && eltype(z) === Int | ||
z = x .+ ones(Float64, 3) | ||
@test z isa LArray && eltype(z) === Float64 | ||
@test eltype(x .+ 1.) === Float64 | ||
|
||
z = @LArray Float64 (2,2) (:a,:b,:c,:d) | ||
w = rand(2,2) | ||
z .= w | ||
|
||
@test z[:a] == w[1,1] | ||
@test z[:b] == w[2,1] | ||
@test z[:c] == w[1,2] | ||
@test z[:d] == w[2,2] | ||
|
||
t = similar(z, String) # t's elements are uninitialized | ||
@test_throws UndefRefError t[1] | ||
copy(t) # should be ok | ||
deepcopy(t) # should also be ok | ||
@testset "NameTuple conversion" begin | ||
x_tup = (a=1, b=2) | ||
y_tup = (a=1, b=2, c=3, d=4) | ||
x = LVector(a=1, b=2) | ||
y = LArray((2,2); a=1, b=2, c=3, d=4) | ||
@test convert(NamedTuple, x) == x_tup | ||
@test convert(NamedTuple, y) == y_tup | ||
@test collect(pairs(x)) == collect(pairs(x_tup)) | ||
@test collect(pairs(y)) == collect(pairs(y_tup)) | ||
|
||
@code_warntype LArray((2,2); a=1, b=2, c=3, d=4) | ||
@code_warntype convert(NamedTuple, y) | ||
@code_warntype collect(pairs(y)) | ||
end | ||
|
||
@testset "undef copy" begin | ||
z = @LArray Float64 (2,2) (:a,:b,:c,:d) | ||
t = similar(z, String) # t's elements are uninitialized | ||
@test_throws UndefRefError t[1] | ||
copy(t) # should be ok | ||
deepcopy(t) # should also be ok | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,43 @@ | ||
using LabelledArrays | ||
using Test | ||
using LabelledArrays, StaticArrays | ||
using Test, InteractiveUtils | ||
|
||
ABC = @SLVector (:a,:b,:c) | ||
b = ABC(1,2,3) | ||
@testset "Basic interface" begin | ||
ABC = @SLVector (:a,:b,:c) | ||
b = ABC(1,2,3) | ||
|
||
@test b.a == 1 | ||
@test b.b == 2 | ||
@test b.c == 3 | ||
@test b[1] == b.a | ||
@test b[2] == b.b | ||
@test b[3] == b.c | ||
@test b.a == 1 | ||
@test b.b == 2 | ||
@test b.c == 3 | ||
@test b[1] == b.a | ||
@test b[2] == b.b | ||
@test b[3] == b.c | ||
|
||
@test_throws UndefVarError fill!(a,1) | ||
@test typeof(b.__x) == SVector{3,Int} | ||
@test_throws UndefVarError fill!(a,1) | ||
@test typeof(b.__x) == SVector{3,Int} | ||
|
||
# Type stability tests | ||
ABC_fl = @SLVector Float64 (:a, :b, :c) | ||
ABC_int = @SLVector Int (:a, :b, :c) | ||
@test similar_type(b, Float64) == ABC_fl | ||
@test typeof(copy(b)) == ABC_int | ||
@test typeof(Float64.(b)) == ABC_fl | ||
@test typeof(b .+ b) == ABC_int | ||
@test typeof(b .+ 1.0) == ABC_fl | ||
@test typeof(zero(b)) == ABC_int | ||
@test similar(b) isa MArray # similar should return a mutable copy | ||
# Type stability tests | ||
ABC_fl = @SLVector Float64 (:a, :b, :c) | ||
ABC_int = @SLVector Int (:a, :b, :c) | ||
@test similar_type(b, Float64) == ABC_fl | ||
@test typeof(copy(b)) == ABC_int | ||
@test typeof(Float64.(b)) == ABC_fl | ||
@test typeof(b .+ b) == ABC_int | ||
@test typeof(b .+ 1.0) == ABC_fl | ||
@test typeof(zero(b)) == ABC_int | ||
@test similar(b) isa MArray # similar should return a mutable copy | ||
end | ||
|
||
@testset "NamedTuple conversion" begin | ||
x_tup = (a=1, b=2) | ||
y_tup = (a=1, b=2, c=3, d=4) | ||
x = SLVector(a=1, b=2) | ||
y = SLArray{Tuple{2,2}}(a=1, b=2, c=3, d=4) | ||
@test convert(NamedTuple, x) == x_tup | ||
@test convert(NamedTuple, y) == y_tup | ||
@test collect(pairs(x)) == collect(pairs(x_tup)) | ||
@test collect(pairs(y)) == collect(pairs(y_tup)) | ||
|
||
@code_warntype SLArray{Tuple{2,2}}(a=1, b=2, c=3, d=4) | ||
@code_warntype convert(NamedTuple, y) | ||
@code_warntype collect(pairs(y)) | ||
end |