|
| 1 | +using JLArrays: JLArray, JLMatrix, JLVector |
1 | 2 | using Test: @test, @test_throws, @test_broken, @testset |
2 | 3 | using TestExtras: @constinferred |
3 | 4 | using TypeParameterAccessors: |
4 | 5 | set_type_parameters, specify_type_parameters, type_parameters, unspecify_type_parameters |
5 | 6 |
|
6 | | -@testset "Get parameters" begin |
7 | | - @test @constinferred(type_parameters($(AbstractArray{Float64}), 1)) == Float64 |
8 | | - @test @constinferred(type_parameters($(AbstractArray{Float64}), eltype)) == Float64 |
9 | | - @test @constinferred(type_parameters($(AbstractMatrix{Float64}), ndims)) == 2 |
| 7 | +const anyarrayts = ( |
| 8 | + (arrayt=Array, matrixt=Matrix, vectort=Vector), |
| 9 | + (arrayt=JLArray, matrixt=JLMatrix, vectort=JLVector), |
| 10 | +) |
| 11 | +@testset "basics (arrayts=$anyarrayt)" for anyarrayt in anyarrayts |
| 12 | + (; arrayt, matrixt, vectort) = anyarrayt |
10 | 13 |
|
11 | | - @test @constinferred(type_parameters($(Array{Float64}), 1)) == Float64 |
12 | | - @test @constinferred(type_parameters($(Val{3}))) == (3,) |
| 14 | + @testset "Get parameters" begin |
| 15 | + @test @constinferred(type_parameters($(AbstractArray{Float64}), 1)) == Float64 |
| 16 | + @test @constinferred(type_parameters($(AbstractArray{Float64}), eltype)) == Float64 |
| 17 | + @test @constinferred(type_parameters($(AbstractMatrix{Float64}), ndims)) == 2 |
13 | 18 |
|
14 | | - # @test_throws ErrorException type_parameter(Array, 1) |
15 | | - @test @constinferred(type_parameters($(Array{Float64}), eltype)) == Float64 |
16 | | - @test @constinferred(type_parameters($(Matrix{Float64}), ndims)) == 2 |
17 | | - @test @constinferred(type_parameters($(Matrix{Float64}), (ndims, eltype))) == (2, Float64) |
18 | | - # TODO: Not inferrable without interpolating positions: |
19 | | - # https://github.com/ITensor/TypeParameterAccessors.jl/issues/21. |
20 | | - @test @constinferred(type_parameters($(Matrix{Float64}), $((2, eltype)))) == (2, Float64) |
21 | | - @test @constinferred(type_parameters($(Matrix{Float64}), (ndims, eltype))) == (2, Float64) |
22 | | - # @test_throws ErrorException type_parameters(Array{Float64}, ndims) == 2 |
23 | | - @test @constinferred(broadcast($type_parameters, $(Matrix{Float64}), $((2, eltype)))) == |
24 | | - (2, Float64) |
25 | | -end |
| 19 | + @test @constinferred(type_parameters($(arrayt{Float64}), 1)) == Float64 |
| 20 | + @test @constinferred(type_parameters($(Val{3}))) == (3,) |
26 | 21 |
|
27 | | -@testset "Set parameters" begin |
28 | | - @test @constinferred(set_type_parameters($Array, 1, $Float64)) == Array{Float64} |
29 | | - @test @constinferred(set_type_parameters($Array, 2, 2)) == Matrix |
30 | | - @test @constinferred(set_type_parameters($Array, $eltype, $Float32)) == Array{Float32} |
31 | | - @test @constinferred(set_type_parameters($Array, $((eltype, 2)), $((Float32, 3)))) == |
32 | | - Array{Float32,3} |
33 | | -end |
| 22 | + # @test_throws ErrorException type_parameter(arrayt, 1) |
| 23 | + @test @constinferred(type_parameters($(arrayt{Float64}), eltype)) == Float64 |
| 24 | + @test @constinferred(type_parameters($(matrixt{Float64}), ndims)) == 2 |
| 25 | + @test @constinferred(type_parameters($(matrixt{Float64}), (ndims, eltype))) == |
| 26 | + (2, Float64) |
| 27 | + # TODO: Not inferrable without interpolating positions: |
| 28 | + # https://github.com/ITensor/TypeParameterAccessors.jl/issues/21. |
| 29 | + @test @constinferred(type_parameters($(matrixt{Float64}), $((2, eltype)))) == |
| 30 | + (2, Float64) |
| 31 | + @test @constinferred(type_parameters($(matrixt{Float64}), (ndims, eltype))) == |
| 32 | + (2, Float64) |
| 33 | + # @test_throws ErrorException type_parameters(arrayt{Float64}, ndims) == 2 |
| 34 | + @test @constinferred( |
| 35 | + broadcast($type_parameters, $(matrixt{Float64}), $((2, eltype))) |
| 36 | + ) == (2, Float64) |
| 37 | + end |
34 | 38 |
|
35 | | -@testset "Specify parameters" begin |
36 | | - @test @constinferred(specify_type_parameters($Array, 1, $Float64)) == Array{Float64} |
37 | | - @test @constinferred(specify_type_parameters($Matrix, $((2, 1)), $((4, Float32)))) == |
38 | | - Matrix{Float32} |
39 | | - @test @constinferred(specify_type_parameters($Array, $((Float64, 2)))) == Matrix{Float64} |
40 | | - @test @constinferred(specify_type_parameters($Array, $eltype, $Float32)) == Array{Float32} |
41 | | - @test @constinferred(specify_type_parameters($Array, $((eltype, 2)), $((Float32, 3)))) == |
42 | | - Array{Float32,3} |
43 | | -end |
| 39 | + @testset "Set parameters" begin |
| 40 | + @test @constinferred(set_type_parameters($arrayt, 1, $Float64)) == arrayt{Float64} |
| 41 | + @test @constinferred(set_type_parameters($arrayt, 2, 2)) == matrixt |
| 42 | + @test @constinferred(set_type_parameters($arrayt, $eltype, $Float32)) == arrayt{Float32} |
| 43 | + @test @constinferred(set_type_parameters($arrayt, $((eltype, 2)), $((Float32, 3)))) == |
| 44 | + arrayt{Float32,3} |
| 45 | + end |
44 | 46 |
|
45 | | -@testset "Unspecify parameters" begin |
46 | | - @test @constinferred(unspecify_type_parameters($Vector, 2)) == Array |
47 | | - @test @constinferred(unspecify_type_parameters($(Vector{Float64}), eltype)) == Vector |
48 | | - @test @constinferred(unspecify_type_parameters($(Vector{Float64}))) == Array |
49 | | - @test @constinferred(unspecify_type_parameters($(Vector{Float64}), $((eltype, 2)))) == |
50 | | - Array |
51 | | -end |
| 47 | + @testset "Specify parameters" begin |
| 48 | + @test @constinferred(specify_type_parameters($arrayt, 1, $Float64)) == arrayt{Float64} |
| 49 | + @test @constinferred(specify_type_parameters($matrixt, $((2, 1)), $((4, Float32)))) == |
| 50 | + matrixt{Float32} |
| 51 | + @test @constinferred(specify_type_parameters($arrayt, $((Float64, 2)))) == |
| 52 | + matrixt{Float64} |
| 53 | + @test @constinferred(specify_type_parameters($arrayt, $eltype, $Float32)) == |
| 54 | + arrayt{Float32} |
| 55 | + @test @constinferred( |
| 56 | + specify_type_parameters($arrayt, $((eltype, 2)), $((Float32, 3))) |
| 57 | + ) == arrayt{Float32,3} |
| 58 | + end |
| 59 | + |
| 60 | + @testset "Unspecify parameters" begin |
| 61 | + @test @constinferred(unspecify_type_parameters($vectort, 2)) == arrayt |
| 62 | + @test @constinferred(unspecify_type_parameters($(vectort{Float64}), eltype)) == vectort |
| 63 | + @test @constinferred(unspecify_type_parameters($(vectort{Float64}))) == arrayt |
| 64 | + @test @constinferred(unspecify_type_parameters($(vectort{Float64}), $((eltype, 2)))) == |
| 65 | + arrayt |
| 66 | + end |
52 | 67 |
|
53 | | -@testset "On objects" begin |
54 | | - @test @constinferred(type_parameters($(Val{3}()))) == (3,) |
55 | | - @test @constinferred(type_parameters($(Val{Float32}()))) == (Float32,) |
56 | | - a = randn(Float32, (2, 2, 2)) |
57 | | - @test @constinferred(type_parameters(a, 1)) == Float32 |
58 | | - @test @constinferred(type_parameters(a, eltype)) == Float32 |
59 | | - @test @constinferred(type_parameters(a, 2)) == 3 |
60 | | - @test @constinferred(type_parameters(a, ndims)) == 3 |
61 | | - @test @constinferred(type_parameters(a)) == (Float32, 3) |
62 | | - @test @constinferred(broadcast($type_parameters, $(Ref(a)), $((2, eltype)))) == |
63 | | - (3, Float32) |
| 68 | + @testset "On objects" begin |
| 69 | + @test @constinferred(type_parameters($(Val{3}()))) == (3,) |
| 70 | + @test @constinferred(type_parameters($(Val{Float32}()))) == (Float32,) |
| 71 | + a = arrayt(randn(Float32, (2, 2, 2))) |
| 72 | + @test @constinferred(type_parameters(a, 1)) == Float32 |
| 73 | + @test @constinferred(type_parameters(a, eltype)) == Float32 |
| 74 | + @test @constinferred(type_parameters(a, 2)) == 3 |
| 75 | + @test @constinferred(type_parameters(a, ndims)) == 3 |
| 76 | + @test @constinferred(type_parameters(a)) == (Float32, 3) |
| 77 | + @test @constinferred(broadcast($type_parameters, $(Ref(a)), $((2, eltype)))) == |
| 78 | + (3, Float32) |
| 79 | + end |
64 | 80 | end |
0 commit comments