summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
Diffstat (limited to 't')
-rw-r--r--t/lib/Test/Enigmatic.pm15
-rw-r--r--t/reflectors.t71
-rw-r--r--t/rotors.t26
3 files changed, 93 insertions, 19 deletions
diff --git a/t/lib/Test/Enigmatic.pm b/t/lib/Test/Enigmatic.pm
new file mode 100644
index 0000000..923ede1
--- /dev/null
+++ b/t/lib/Test/Enigmatic.pm
@@ -0,0 +1,15 @@
+package Test::Enigmatic;
+use DAKKAR::p 'test';
+
+sub test_static_map {
+ my ($mapper,$out,$name) = @_;
+
+ my @in = 'A'..'Z';
+ my %map;@map{@in}=@$out;
+
+ for my $c ('A' .. 'Z') {
+ is($mapper->map($c),
+ $map{$c},
+ "$name on $c");
+ }
+}
diff --git a/t/reflectors.t b/t/reflectors.t
new file mode 100644
index 0000000..107ff9f
--- /dev/null
+++ b/t/reflectors.t
@@ -0,0 +1,71 @@
+#!perl
+use DAKKAR::p 'test';
+use List::Util 'shuffle';
+use Test::Enigmatic;
+
+use Enigmatic::Reflector;
+use Enigmatic::ReflectorBox;
+
+subtest 'identity reflector' => sub {
+ my @in = 'A'..'Z';
+ my $r = Enigmatic::Reflector->new(@in->join);
+ Test::Enigmatic::test_static_map($r,\@in,'identity');
+};
+
+subtest 'scramble reflector' => sub {
+ my %map;my @letters='A'..'Z';
+ my %unused;@unused{@letters}=();
+ for my $letter (@letters) {
+ next if not exists $unused{$letter};
+ my @usable = keys %unused;
+ my $image = @usable[rand @usable];
+ $map{$letter} = $image;
+ $map{$image} = $letter;
+ delete $unused{$letter};
+ delete $unused{$image};
+ }
+
+ my $wiring = join '',@map{@letters};
+ note "scramble reflector: $wiring";
+
+ my $r = Enigmatic::Reflector->new($wiring);
+ Test::Enigmatic::test_static_map($r,[@map{@letters}],'shuffle');
+};
+
+subtest 'constraints' => sub {
+ my @bad_params = (
+ 'ABCD',
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZABCD',
+ 'AACDEFGHIJKLMNOPQRSTUVWXYZ',
+ '12CDEFGHIJKLMNOPQRSTUVWXYZABCD',
+ 'EKMFLGDQVZNTOWYHXUSPAIBRCJ',
+ );
+
+ for my $bad_param (@bad_params) {
+ dies_ok {
+ Enigmatic::Reflector->new($bad_param);
+ } "bad parameter $bad_param";
+ }
+};
+
+subtest 'reflector box' => sub {
+ my %reflectors = (
+ B => 'YRUHQSLDPXNGOKMIEBFZCWVJAT',
+ C => 'FVPJIAOYEDRZXWGCTKUQSBNMHL',
+ 'B_thin' => 'ENKQAUYWJICOPBLMDXZVFTHRGS',
+ 'C_thin' => 'RDOBJNTKVEHMLFCWZAXGYIPSUQ',
+ );
+
+ my $box = Enigmatic::ReflectorBox->new();
+
+ %reflectors->each(sub {
+ my ($reflector,$wiring) = @_;
+ my $r = $box->get($reflector);
+ my $out = $wiring->split(qr//);
+ Test::Enigmatic::test_static_map(
+ $r,$out,
+ "reflector $reflector from box");
+ });
+};
+
+done_testing();
diff --git a/t/rotors.t b/t/rotors.t
index fb53fe4..90ce6ce 100644
--- a/t/rotors.t
+++ b/t/rotors.t
@@ -1,35 +1,22 @@
#!perl
use DAKKAR::p 'test';
use List::Util 'shuffle';
+use Test::Enigmatic;
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');
+ Test::Enigmatic::test_static_map($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');
+ Test::Enigmatic::test_static_map($r,\@out,'shuffle');
};
subtest 'identity rotor, non-default ring setting' => sub {
@@ -39,7 +26,7 @@ subtest 'identity rotor, non-default ring setting' => sub {
wiring => ['A'..'Z']->join,
ring_setting => 1,
);
- test_the_rotor($r,\@out,'ring=1');
+ Test::Enigmatic::test_static_map($r,\@out,'ring=1');
};
subtest 'constraints' => sub {
@@ -77,8 +64,9 @@ subtest 'rotor box' => sub {
my ($rotor,$wiring) = @_;
my $r = $box->get($rotor);
my $out = $wiring->split(qr//);
- test_the_rotor($r,$out,
- "rotor $rotor from box");
+ Test::Enigmatic::test_static_map(
+ $r,$out,
+ "rotor $rotor from box");
});
};