22using ChainRules: unzip_broadcast, unzip, unzip_map, unzip_map_reversed
33
44@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]
66 @test_throws Exception fun (sqrt, 1 : 3 )
77
88 @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
2727 end
2828 @test @inferred (fun (tuple, (1 ,2 ,3 ), [4 ,5 ,6 ])) == ([1 , 2 , 3 ], [4 , 5 , 6 ]) # mix tuple & vector
2929 end
30-
30+
3131 @testset " zip behaviour: $unzip_map " for unzip_map in [unzip_map, unzip_map_reversed]
3232 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+
3335 @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+
3440 @test check (tuple, [1 2 ; 3 4 ], [5 ,6 ,7 ])
3541 @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 ))
3646 end
3747
3848 @testset " unzip_map_reversed" begin
3949 cnt (x, y) = (x, y) .+ (CNT[] += 1 )
4050 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 ])
4252 @test CNT[] == 2
4353
4454 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 ))
4656 end
4757
4858 @testset " rrules" begin
@@ -76,6 +86,10 @@ using ChainRules: unzip_broadcast, unzip, unzip_map, unzip_map_reversed
7686 @test unzip ([(1 ,), (3 ,), (5 ,)])[1 ] isa Base. ReinterpretArray
7787
7888 @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 ]])
7993
8094 # 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
8195
0 commit comments