Last active
August 29, 2015 14:11
-
-
Save notbenh/866b95cbea06a5d50b4f to your computer and use it in GitHub Desktop.
An alternate solution for https://gist.github.com/anonymous/3f696ec2b94d4e360da1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Test::More; | |
use Test::Differences; | |
use List::MoreUtils qw{uniq}; | |
sub pp { | |
my %lookup; | |
my $i; | |
$lookup{$_} = $i++ for sort( uniq( map{@$_} @_ )); | |
my @blank = map{undef} keys %lookup; | |
return [ map{ my @a = @blank; | |
$a[$lookup{$_}] = $_ for @$_; | |
\@a; | |
} @_ | |
]; | |
} | |
eq_or_diff pp( [qw{ b c f }] | |
, [qw{ a d }] | |
, [qw{ c d e }] | |
) | |
, [ [ undef, 'b', 'c', undef, undef, 'f' ] | |
, [ 'a', undef, undef, 'd', undef, undef ] | |
, [ undef, undef, 'c', 'd', 'e', undef ] | |
] | |
, 'example from blog'; | |
eq_or_diff pp( [qw{ f c b }] | |
, [qw{ a d d}] | |
, [qw{ c c d e }] | |
) | |
, [ [ undef, 'b', 'c', undef, undef, 'f' ] | |
, [ 'a', undef, undef, 'd', undef, undef ] | |
, [ undef, undef, 'c', 'd', 'e', undef ] | |
] | |
, 'shows that this will eat duplicates and alter order'; | |
eq_or_diff pp( [qw{ a b c f j}] | |
, [qw{ i j }] | |
, [] | |
) | |
, [ [ 'a', 'b', 'c', 'f', undef, 'j' ] | |
, [ undef, undef, undef, undef, 'i', 'j' ] | |
, [ undef, undef, undef, undef, undef, undef ] | |
] | |
, 'test for the j-alignment bug and show that it correctly handles null input'; | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment