1
1
2
- using ChainRules: unzip_broadcast, unzip # , unzip_map
2
+ using ChainRules: unzip_broadcast, unzip, unzip_map, unzip_map_reversed
3
3
4
4
@testset " unzipped.jl" begin
5
- @testset " basics: $(sprint (show, fun)) " for fun in [unzip_broadcast, unzip∘ map, unzip∘ broadcast] # unzip_map,
5
+ @testset " basics: $(sprint (show, fun)) " for fun in [unzip_broadcast, unzip∘ map, unzip∘ broadcast, unzip_map, unzip_map_reversed]
6
6
@test_throws Exception fun (sqrt, 1 : 3 )
7
7
8
- @test fun (tuple, 1 : 3 , 4 : 6 ) == ([1 , 2 , 3 ], [4 , 5 , 6 ])
9
- @test fun (tuple, [1 , 10 , 100 ]) == ([1 , 10 , 100 ],)
10
- @test fun (tuple, 1 : 3 , fill (nothing , 3 )) == (1 : 3 , fill (nothing , 3 ))
11
- @test fun (tuple, [1 , 10 , 100 ], fill (nothing , 3 )) == ([1 , 10 , 100 ], fill (nothing , 3 ))
12
- @test fun (tuple, fill (nothing , 3 ), fill (nothing , 3 )) == (fill (nothing , 3 ), fill (nothing , 3 ))
8
+ @test @inferred ( fun (tuple, 1 : 3 , 4 : 6 ) ) == ([1 , 2 , 3 ], [4 , 5 , 6 ])
9
+ @test @inferred ( fun (tuple, [1 , 10 , 100 ]) ) == ([1 , 10 , 100 ],)
10
+ @test @inferred ( fun (tuple, 1 : 3 , fill (nothing , 3 ) )) == (1 : 3 , fill (nothing , 3 ))
11
+ @test @inferred ( fun (tuple, [1 , 10 , 100 ], fill (nothing , 3 ) )) == ([1 , 10 , 100 ], fill (nothing , 3 ))
12
+ @test @inferred ( fun (tuple, fill (nothing , 3 ), fill (nothing , 3 ) )) == (fill (nothing , 3 ), fill (nothing , 3 ))
13
13
14
14
if contains (string (fun), " map" )
15
- @test fun (tuple, 1 : 3 , 4 : 999 ) == ([1 , 2 , 3 ], [4 , 5 , 6 ])
15
+ @test @inferred ( fun (tuple, 1 : 3 , 4 : 999 ) ) == ([1 , 2 , 3 ], [4 , 5 , 6 ])
16
16
else
17
- @test fun (tuple, [1 ,2 ,3 ], [4 5 ]) == ([1 1 ; 2 2 ; 3 3 ], [4 5 ; 4 5 ; 4 5 ])
17
+ @test @inferred (fun (tuple, [1 ,2 ,3 ], [4 5 ])) == ([1 1 ; 2 2 ; 3 3 ], [4 5 ; 4 5 ; 4 5 ])
18
+ @test @inferred (fun (tuple, [1 ,2 ,3 ], 6 )) == ([1 , 2 , 3 ], [6 , 6 , 6 ])
18
19
end
19
20
20
21
if contains (string (fun), " map" )
@@ -24,7 +25,24 @@ using ChainRules: unzip_broadcast, unzip #, unzip_map
24
25
@test fun (tuple, (1 ,2 ,3 ), (7 ,)) == ((1 , 2 , 3 ), (7 , 7 , 7 ))
25
26
@test fun (tuple, (1 ,2 ,3 ), 8 ) == ((1 , 2 , 3 ), (8 , 8 , 8 ))
26
27
end
27
- @test fun (tuple, (1 ,2 ,3 ), [4 ,5 ,6 ]) == ([1 , 2 , 3 ], [4 , 5 , 6 ]) # mix tuple & vector
28
+ @test @inferred (fun (tuple, (1 ,2 ,3 ), [4 ,5 ,6 ])) == ([1 , 2 , 3 ], [4 , 5 , 6 ]) # mix tuple & vector
29
+ end
30
+
31
+ @testset " zip behaviour: $unzip_map " for unzip_map in [unzip_map, unzip_map_reversed]
32
+ check (f, args... ) = @inferred (unzip_map (f, args... )) == unzip (map (f, args... ))
33
+ @test check (tuple, [1 2 ; 3 4 ], [5 ,6 ,7 ,8 ]) # makes a vector
34
+ @test check (tuple, [1 2 ; 3 4 ], [5 ,6 ,7 ])
35
+ @test check (tuple, [1 2 ; 3 4 ], [5 ,6 ,7 ,8 ,9 ,10 ])
36
+ end
37
+
38
+ @testset " unzip_map_reversed" begin
39
+ cnt (x, y) = (x, y) .+ (CNT[] += 1 )
40
+ CNT = Ref (0 )
41
+ @test unzip_map_reversed (cnt, [10 , 20 ], [30 , 40 , 50 ]) == ([12 , 21 ], [32 , 41 ])
42
+ @test CNT[] == 2
43
+
44
+ CNT = Ref (0 )
45
+ @test unzip_map_reversed (cnt, (10 , 20 , 99 ), (30 , 40 )) == ((12 , 21 ), (32 , 41 ))
28
46
end
29
47
30
48
@testset " rrules" begin
0 commit comments