2
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, unzip_map_reversed ]
5
+ @testset " basics: $(sprint (show, fun)) " for fun in [unzip_broadcast, unzip∘ map, unzip∘ broadcast, unzip_map]
6
6
@test_throws Exception fun (sqrt, 1 : 3 )
7
7
8
8
@test @inferred (fun (tuple, 1 : 3 , 4 : 6 )) == ([1 , 2 , 3 ], [4 , 5 , 6 ])
@@ -27,22 +27,32 @@ using ChainRules: unzip_broadcast, unzip, unzip_map, unzip_map_reversed
27
27
end
28
28
@test @inferred (fun (tuple, (1 ,2 ,3 ), [4 ,5 ,6 ])) == ([1 , 2 , 3 ], [4 , 5 , 6 ]) # mix tuple & vector
29
29
end
30
-
30
+
31
31
@testset " zip behaviour: $unzip_map " for unzip_map in [unzip_map, unzip_map_reversed]
32
32
check (f, args... ) = @inferred (unzip_map (f, args... )) == unzip (map (f, args... ))
33
+ check_no_inferr (f, args... ) = unzip_map (f, args... ) == unzip (map (f, args... ))
34
+
33
35
@test check (tuple, [1 2 ; 3 4 ], [5 ,6 ,7 ,8 ]) # makes a vector
36
+ @test check_no_inferr (tuple, [1 ,2 ,3 ], (5 ,6 ,7 ))
37
+
38
+ unzip_map == unzip_map_reversed && continue # does not handle unequal lengths.
39
+
34
40
@test check (tuple, [1 2 ; 3 4 ], [5 ,6 ,7 ])
35
41
@test check (tuple, [1 2 ; 3 4 ], [5 ,6 ,7 ,8 ,9 ,10 ])
42
+
43
+ @test check_no_inferr (tuple, [1 ,2 ,3 ], (5 ,6 ,7 ,8 ))
44
+ @test check_no_inferr (tuple, [1 ,2 ,3 ,4 ], (5 ,6 ,7 ))
45
+ @test check_no_inferr (tuple, [1 2 ;3 4 ], (5 ,6 ,7 ))
36
46
end
37
47
38
48
@testset " unzip_map_reversed" begin
39
49
cnt (x, y) = (x, y) .+ (CNT[] += 1 )
40
50
CNT = Ref (0 )
41
- @test unzip_map_reversed (cnt, [10 , 20 ], [30 , 40 , 50 ]) == ([12 , 21 ], [32 , 41 ])
51
+ @test unzip_map_reversed (cnt, [10 , 20 ], [30 , 40 ]) == ([12 , 21 ], [32 , 41 ])
42
52
@test CNT[] == 2
43
53
44
54
CNT = Ref (0 )
45
- @test unzip_map_reversed (cnt, (10 , 20 , 99 ), (30 , 40 )) == ((12 , 21 ), (32 , 41 ))
55
+ @test unzip_map_reversed (cnt, (10 , 20 ), (30 , 40 )) == ((12 , 21 ), (32 , 41 ))
46
56
end
47
57
48
58
@testset " rrules" begin
@@ -76,6 +86,10 @@ using ChainRules: unzip_broadcast, unzip, unzip_map, unzip_map_reversed
76
86
@test unzip ([(1 ,), (3 ,), (5 ,)])[1 ] isa Base. ReinterpretArray
77
87
78
88
@test unzip (((1 ,2 ), (3 ,4 ), (5 ,6 ))) == ((1 , 3 , 5 ), (2 , 4 , 6 ))
89
+
90
+ # Bug: these cases cannot be done by reinterpret
91
+ @test unzip ([([1 ,2 ],), ([3 ,4 ],)]) == ([[1 , 2 ], [3 , 4 ]],)
92
+ @test unzip ([(nothing , [1 ,2 ]), (nothing , [3 ,4 ])]) == ([nothing , nothing ], [[1 , 2 ], [3 , 4 ]])
79
93
80
94
# test_rrule(unzip, [(1,2), (3,4), (5.0,6.0)], check_inferred=false) # DimensionMismatch: second dimension of A, 6, does not match length of x, 2
81
95
0 commit comments