use DAKKAR::p 'test';
use List::Util 'shuffle';
use Enigmatic::Rotor;
use Enigmatic::RotorBox;
sub test_the_rotor {
my ($r,$out,$name) = @_;
my @in = 'A'..'Z';
my %map;@map{@in}=@$out;
for my $c ('A' .. 'Z') {
is($r->map($c),
$map{$c},
"$name on $c");
}
}
subtest 'identity rotor' => sub {
my @in = 'A'..'Z';
my $r = Enigmatic::Rotor->new(@in->join);
test_the_rotor($r,\@in,'identity');
};
subtest 'scramble rotor' => sub {
my @out = shuffle 'A'..'Z';
my $r = Enigmatic::Rotor->new(@out->join);
test_the_rotor($r,\@out,'shuffle');
};
subtest 'identity rotor, non-default ring setting' => sub {
my @out = (('B'..'Z'),'A');
my $r = Enigmatic::Rotor->new(
wiring => ['A'..'Z']->join,
ring_setting => 1,
);
test_the_rotor($r,\@out,'ring=1');
};
subtest 'constraints' => sub {
my @bad_params = (
'ABCD',
'ABCDEFGHIJKLMNOPQRSTUVWXYZABCD',
'AACDEFGHIJKLMNOPQRSTUVWXYZ',
'12CDEFGHIJKLMNOPQRSTUVWXYZABCD',
);
for my $bad_param (@bad_params) {
dies_ok {
Enigmatic::Rotor->new($bad_param);
} "bad parameter $bad_param";
}
};
subtest 'rotor box' => sub {
my %rotors = (
I => 'EKMFLGDQVZNTOWYHXUSPAIBRCJ',
II => 'AJDKSIRUXBLHWTMCQGZNPYFVOE',
III => 'BDFHJLCPRTXVZNYEIWGAKMUSQO',
IV => 'ESOVPZJAYQUIRHXLNFTGKDCMWB',
V => 'VZBRGITYUPSDNHLXAWMJQOFECK',
VI => 'JPGVOUMFYQBENHZRDKASXLICTW',
VII => 'NZJHGRCXMYSWBOUFAIVLPEKQDT',
VIII => 'FKQHTLXOCBJSPDZRAMEWNIUYGV',
Beta => 'LEYJVCNIXWPBQMDRTAKZGFUHOS',
Gamma => 'FSOKANUERHMBTIYCWLQPZXVGJD',
);
my $box = Enigmatic::RotorBox->new();
%rotors->each(sub {
my ($rotor,$wiring) = @_;
my $r = $box->get($rotor);
my $out = $wiring->split(qr//);
test_the_rotor($r,$out,
"rotor $rotor from box");
});
};
done_testing();