@@ -3,113 +3,132 @@ using Distributions, Test, Random, LinearAlgebra
3
3
4
4
rng = MersenneTwister (123456 )
5
5
6
- @testset " matrixreshaped.jl" begin
6
+ if VERSION >= v " 1.6.0-DEV.254"
7
+ _redirect_stderr (f, :: Base.DevNull ) = redirect_stderr (f, devnull )
8
+ else
9
+ function _redirect_stderr (f, :: Base.DevNull )
10
+ nulldev = @static Sys. iswindows () ? " NUL" : " /dev/null"
11
+ open (nulldev, " w" ) do io
12
+ redirect_stderr (f, io)
13
+ end
14
+ end
15
+ end
7
16
8
17
function test_matrixreshaped (rng, d1, sizes)
9
- x1 = rand (rng, d1)
10
- d1s = [@test_deprecated (MatrixReshaped (d1, s... )) for s in sizes]
18
+ @testset " MatrixReshaped $(nameof (typeof (d1))) tests" begin
19
+ x1 = rand (rng, d1)
20
+ d1s = [@test_deprecated (MatrixReshaped (d1, s... )) for s in sizes]
11
21
12
- @testset " MatrixReshaped $( nameof ( typeof (d1))) tests " begin
13
- @testset " MatrixReshaped constructor " begin
14
- for d in d1s
15
- @test d isa MatrixReshaped
22
+ @testset " MatrixReshaped constructor " begin
23
+ for d in d1s
24
+ @test d isa MatrixReshaped
25
+ end
16
26
end
17
- end
18
- @testset " MatrixReshaped constructor errors" begin
19
- @test_deprecated (@test_throws ArgumentError MatrixReshaped (d1, length (d1), 2 ))
20
- @test_deprecated (@test_throws ArgumentError MatrixReshaped (d1, length (d1)))
21
- @test_deprecated (@test_throws ArgumentError MatrixReshaped (d1, - length (d1), - 1 ))
22
- end
23
- @testset " MatrixReshaped size" begin
24
- for (d, s) in zip (d1s[1 : end - 1 ], sizes[1 : end - 1 ])
25
- @test size (d) == s
27
+ @testset " MatrixReshaped constructor errors" begin
28
+ @test_deprecated (@test_throws ArgumentError MatrixReshaped (d1, length (d1), 2 ))
29
+ @test_deprecated (@test_throws ArgumentError MatrixReshaped (d1, length (d1)))
30
+ @test_deprecated (@test_throws ArgumentError MatrixReshaped (d1, - length (d1), - 1 ))
26
31
end
27
- end
28
- @testset " MatrixReshaped length " begin
29
- for d in d1s
30
- @test length (d) == length (d1)
32
+ @testset " MatrixReshaped size " begin
33
+ for (d, s) in zip (d1s[ 1 : end - 1 ], sizes[ 1 : end - 1 ])
34
+ @test size (d) == s
35
+ end
31
36
end
32
- end
33
- @testset " MatrixReshaped rank " begin
34
- for (d, s) in zip (d1s, sizes )
35
- @test rank (d) == minimum (s)
37
+ @testset " MatrixReshaped length " begin
38
+ for d in d1s
39
+ @test length (d) == length (d1 )
40
+ end
36
41
end
37
- end
38
- @testset " MatrixReshaped insupport" begin
39
- for (i, d) in enumerate (d1s[1 : end - 1 ])
40
- for (j, s) in enumerate (sizes[1 : end - 1 ])
41
- @test (i == j) ⊻ ! insupport (d, reshape (x1, s))
42
+ @testset " MatrixReshaped rank" begin
43
+ for (d, s) in zip (d1s, sizes)
44
+ @test rank (d) == minimum (s)
42
45
end
43
46
end
44
- end
45
- @testset " MatrixReshaped mean" begin
46
- for (d, s) in zip (d1s[1 : end - 1 ], sizes[1 : end - 1 ])
47
- @test mean (d) == reshape (mean (d1), s)
47
+ @testset " MatrixReshaped insupport" begin
48
+ for (i, d) in enumerate (d1s[1 : end - 1 ])
49
+ for (j, s) in enumerate (sizes[1 : end - 1 ])
50
+ @test (i == j) ⊻ ! insupport (d, reshape (x1, s))
51
+ end
52
+ end
48
53
end
49
- end
50
- @testset " MatrixReshaped mode " begin
51
- for (d, s) in zip (d1s[ 1 : end - 1 ], sizes[ 1 : end - 1 ] )
52
- @test mode (d) == reshape ( mode (d1), s)
54
+ @testset " MatrixReshaped mean " begin
55
+ for (d, s) in zip (d1s[ 1 : end - 1 ], sizes[ 1 : end - 1 ])
56
+ @test mean (d) == reshape ( mean (d1), s )
57
+ end
53
58
end
54
- end
55
- @testset " MatrixReshaped covariance" begin
56
- for (d, (n, p)) in zip (d1s[1 : end - 1 ], sizes[1 : end - 1 ])
57
- @test cov (d) == cov (d1)
58
- @test cov (d, Val (false )) == reshape (cov (d1), n, p, n, p)
59
+ @testset " MatrixReshaped mode" begin
60
+ for (d, s) in zip (d1s[1 : end - 1 ], sizes[1 : end - 1 ])
61
+ @test mode (d) == reshape (mode (d1), s)
62
+ end
59
63
end
60
- end
61
- @testset " MatrixReshaped variance" begin
62
- for (d, s) in zip (d1s[1 : end - 1 ], sizes[1 : end - 1 ])
63
- @test var (d) == reshape (var (d1), s)
64
+ @testset " MatrixReshaped covariance" begin
65
+ for (d, (n, p)) in zip (d1s[1 : end - 1 ], sizes[1 : end - 1 ])
66
+ @test cov (d) == cov (d1)
67
+ @test cov (d, Val (false )) == reshape (cov (d1), n, p, n, p)
68
+ end
64
69
end
65
- end
66
- @testset " MatrixReshaped params " begin
67
- for (d, s) in zip (d1s[ 1 : end - 1 ], sizes[ 1 : end - 1 ] )
68
- @test params (d) == (d1, s)
70
+ @testset " MatrixReshaped variance " begin
71
+ for (d, s) in zip (d1s[ 1 : end - 1 ], sizes[ 1 : end - 1 ])
72
+ @test var (d) == reshape ( var (d1), s )
73
+ end
69
74
end
70
- end
71
- @testset " MatrixReshaped partype " begin
72
- for d in d1s
73
- @test partype (d) === partype (d1)
75
+ @testset " MatrixReshaped params " begin
76
+ for (d, s) in zip (d1s[ 1 : end - 1 ], sizes[ 1 : end - 1 ])
77
+ @test params (d) == (d1, s)
78
+ end
74
79
end
75
- end
76
- @testset " MatrixReshaped eltype " begin
77
- for d in d1s
78
- @test eltype (d) === eltype (d1)
80
+ @testset " MatrixReshaped partype " begin
81
+ for d in d1s
82
+ @test partype (d) === partype (d1)
83
+ end
79
84
end
80
- end
81
- @testset " MatrixReshaped logpdf" begin
82
- for (d, s) in zip (d1s[1 : end - 1 ], sizes[1 : end - 1 ])
83
- x = reshape (x1, s)
84
- @test logpdf (d, x) == logpdf (d1, x1)
85
+ @testset " MatrixReshaped eltype" begin
86
+ for d in d1s
87
+ @test eltype (d) === eltype (d1)
88
+ end
85
89
end
86
- end
87
- @testset " MatrixReshaped rand" begin
88
- for d in d1s
89
- x = rand (rng, d)
90
- @test insupport (d, x)
91
- @test insupport (d1, vec (x))
92
- @test logpdf (d, x) == logpdf (d1, vec (x))
90
+ @testset " MatrixReshaped logpdf" begin
91
+ for (d, s) in zip (d1s[1 : end - 1 ], sizes[1 : end - 1 ])
92
+ x = reshape (x1, s)
93
+ @test logpdf (d, x) == logpdf (d1, x1)
94
+ end
93
95
end
94
- end
95
- @testset " MatrixReshaped vec" begin
96
- for d in d1s
97
- @test vec (d) === d1
96
+ @testset " MatrixReshaped rand" begin
97
+ for d in d1s
98
+ x = rand (rng, d)
99
+ @test insupport (d, x)
100
+ @test insupport (d1, vec (x))
101
+ @test logpdf (d, x) == logpdf (d1, vec (x))
102
+ end
103
+ end
104
+ @testset " MatrixReshaped vec" begin
105
+ for d in d1s
106
+ @test vec (d) === d1
107
+ end
98
108
end
99
109
end
100
- end
101
110
end
102
111
103
- # MvNormal
104
- σ = rand (rng, 16 , 16 )
105
- μ = rand (rng, 16 )
106
- d1 = MvNormal (μ, σ * σ' )
107
- sizes = [(4 , 4 ), (8 , 2 ), (2 , 8 ), (1 , 16 ), (16 , 1 ), (4 ,)]
108
- test_matrixreshaped (rng, d1, sizes)
112
+ # Note: In contrast to `@deprecate`, `@deprecate_binding` can't be tested with `@test_deprecated`
113
+ # Ref: https://github.com/JuliaLang/julia/issues/38780
114
+ @testset " matrixreshaped.jl" begin
115
+ @testset " MvNormal" begin
116
+ σ = rand (rng, 16 , 16 )
117
+ μ = rand (rng, 16 )
118
+ d1 = MvNormal (μ, σ * σ' )
119
+ sizes = [(4 , 4 ), (8 , 2 ), (2 , 8 ), (1 , 16 ), (16 , 1 ), (4 ,)]
120
+ _redirect_stderr (devnull ) do
121
+ test_matrixreshaped (rng, d1, sizes)
122
+ end
123
+ end
109
124
110
125
# Dirichlet
111
- α = rand (rng, 36 ) .+ 1 # mode is only defined if all alpha > 1
112
- d1 = Dirichlet (α)
113
- sizes = [(6 , 6 ), (4 , 9 ), (9 , 4 ), (3 , 12 ), (12 , 3 ), (1 , 36 ), (36 , 1 ), (6 ,)]
114
- test_matrixreshaped (rng, d1, sizes)
126
+ @testset " Dirichlet" begin
127
+ α = rand (rng, 36 ) .+ 1 # mode is only defined if all alpha > 1
128
+ d1 = Dirichlet (α)
129
+ sizes = [(6 , 6 ), (4 , 9 ), (9 , 4 ), (3 , 12 ), (12 , 3 ), (1 , 36 ), (36 , 1 ), (6 ,)]
130
+ _redirect_stderr (devnull ) do
131
+ test_matrixreshaped (rng, d1, sizes)
132
+ end
133
+ end
115
134
end
0 commit comments