summaryrefslogtreecommitdiff
path: root/t/reflectors.t
diff options
context:
space:
mode:
Diffstat (limited to 't/reflectors.t')
-rw-r--r--t/reflectors.t71
1 files changed, 71 insertions, 0 deletions
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();