|
| 1 | +using Test |
| 2 | +using HybridVariationalInference |
| 3 | +using HybridVariationalInference: HybridVariationalInference as CM |
| 4 | +using ComponentArrays: ComponentArrays as CA |
| 5 | + |
| 6 | +@testset "ComponentArrayInterpreter vector" begin |
| 7 | + component_counts = comp_cnts = (; P=2, M=3, Unc=5) |
| 8 | + m = ComponentArrayInterpreter(; comp_cnts...) |
| 9 | + testm = (m) -> begin |
| 10 | + @test CM._get_ComponentArrayInterpreter_axes(m) == (CA.Axis(P=1:2, M=3:5, Unc=6:10),) |
| 11 | + @test length(m) == 10 |
| 12 | + v = 1:length(m) |
| 13 | + cv = m(v) |
| 14 | + @test cv.Unc == 6:10 |
| 15 | + end |
| 16 | + testm(m) |
| 17 | + testm(get_concrete(m)) |
| 18 | + Base.isconcretetype(typeof(m)) |
| 19 | +end; |
| 20 | + |
| 21 | +# () -> begin |
| 22 | +# # test generate code for length |
| 23 | +# @code_llvm length(m) |
| 24 | +# mc = get_concrete(m) |
| 25 | +# @code_llvm length(mc) |
| 26 | +# v = 1:length(m) |
| 27 | +# @code_llvm as_ca(v,m) |
| 28 | +# @code_llvm as_ca(v,mc) |
| 29 | +# end |
| 30 | + |
| 31 | +@testset "ComponentArrayInterpreter matrix in vector" begin |
| 32 | + component_shapes = (; P=2, M=(2, 3), Unc=5) |
| 33 | + m = ComponentArrayInterpreter(; component_shapes...) |
| 34 | + testm = (m) -> begin |
| 35 | + @test length(m) == 13 |
| 36 | + a = 1:length(m) |
| 37 | + cv = m(a) |
| 38 | + @test cv.M == 2 .+ [1 3 5; 2 4 6] |
| 39 | + end |
| 40 | + testm(m) |
| 41 | + testm(get_concrete(m)) |
| 42 | +end; |
| 43 | + |
| 44 | +@testset "ComponentArrayInterpreter matrix and array" begin |
| 45 | + mv = ComponentArrayInterpreter(; c1=2, c2=3) |
| 46 | + cv = mv(1:length(mv)) |
| 47 | + n_col = 4 |
| 48 | + mm = ComponentArrayInterpreter(cv, (n_col,)) # 1-tuple |
| 49 | + testm = (m) -> begin |
| 50 | + @test length(mm) == length(cv) * n_col |
| 51 | + cm = mm(1:length(mm)) |
| 52 | + #cm[:c1,:] |
| 53 | + @test cm[:c1, 2] == 6:7 |
| 54 | + end |
| 55 | + testm(mm) |
| 56 | + mmc = get_concrete(mm) |
| 57 | + testm(mmc) |
| 58 | + # |
| 59 | + n_z = 3 |
| 60 | + mm = ComponentArrayInterpreter(cv, (n_col, n_z)) |
| 61 | + testm = (m) -> begin |
| 62 | + @test length(mm) == length(cv) * n_col * n_z |
| 63 | + cm = mm(1:length(mm)) |
| 64 | + @test cm[:c1, 2, 2] == 26:27 |
| 65 | + end |
| 66 | + testm(mm) |
| 67 | + testm(get_concrete(mm)) |
| 68 | + # |
| 69 | + n_row = 3 |
| 70 | + mm = ComponentArrayInterpreter((n_row,), cv) |
| 71 | + testm = (m) -> begin |
| 72 | + @test length(mm) == n_row * length(cv) |
| 73 | + cm = mm(1:length(mm)) |
| 74 | + @test cm[2, :c1] == [2, 5] |
| 75 | + end |
| 76 | + testm(mm) |
| 77 | + testm(get_concrete(mm)) |
| 78 | +end; |
| 79 | + |
| 80 | +@testset "empty ComponentVector" begin |
| 81 | + x = CA.ComponentVector{Float32}() |
| 82 | + int1 = ComponentArrayInterpreter(x) |
| 83 | + @test int1(CA.getdata(x)) == x |
| 84 | + int2 = ComponentArrayInterpreter(x, ()) |
| 85 | + @test int2 == int1 |
| 86 | + int3 = ComponentArrayInterpreter((), x) |
| 87 | + @test int3 == int1 |
| 88 | +end; |
| 89 | + |
| 90 | + |
| 91 | + |
0 commit comments