1
exercism fetch perl5 crypto-square

crypto.t

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env perl
use strict;
use warnings;

use Test::More;
use FindBin;
my $dir;
BEGIN { $dir = $FindBin::Bin . '/' };
use lib $dir;

my @cases = (
    {
        plain    => 's#$%^&plunk',
        expected => 'splunk',
        sub      => 'normalize_plaintext'
    },
    {
        plain    => '1, 2, 3 GO!',
        expected => '123go',
        sub      => 'normalize_plaintext'
    },
    {
        plain    => '1234',
        expected => 2,
        sub      => 'size'
    },
    {
        plain    => '123456789',
        expected => 3,
        sub      => 'size'
    },
    {
        plain    => '123456789abc',
        expected => 4,
        sub      => 'size'
    },
    {
        plain    => 'Never vex thine heart with idle woes',
        expected => [qw(neverv exthin eheart withid lewoes)],
        sub      => 'plaintext_segments'
    },
    {
        plain    => 'ZOMG! ZOMBIES!!!',
        expected => [qw(zomg zomb ies)],
        sub      => 'plaintext_segments'
    },
    {
        plain    => 'Time is an illusion. Lunchtime doubly so.',
        expected => 'tasneyinicdsmiohooelntuillibsuuml',
        sub      => 'ciphertext'
    },
    {
        plain    => 'We all know interspecies romance is weird.',
        expected => 'wneiaweoreneawssciliprerlneoidktcms',
        sub      => 'ciphertext'
    },
    {
        plain    => 'Madness, and then illumination',
        expected => 'msemo aanin dnin ndla etlt shui',
        sub      => 'normalize_ciphertext'
    },
    {
        plain    => 'Vampires are people too!',
        expected => 'vrel aepe mset paoo irpo',
        sub      => 'normalize_ciphertext'
    },
    {
        sub      => 'normalize_ciphertext',
        plain    => 'All human beings are born free and equal in dignity and rights.
            They are endowed with reason and conscience and should act towards one another
            in a spirit of brotherhood.',
        expected => (join ' ', qw/
            ageihdsednsh lsagtoonaepe lannswnccair hrditeaetnrh ueethdnatoio mbqyewdnotto aouayicdwhod
            nranatosaef bnldrhnhrrb efirersodir irnieecusno nedgnailoat/)
    }

);

my $module = $ENV{EXERCISM} ? 'Example' : 'Crypto';
my @subs = qw(new normalize_ciphertext normalize_plaintext plaintext_segments ciphertext size);
plan tests => 2 + @subs + @cases;

ok -e "${dir}${module}.pm", "Missing $module.pm"
            or BAIL_OUT "You need to create file: $module.pm";

eval "use $module";

ok !$@, "Cannot load $module"
            or BAIL_OUT "Cannot load $module. Does it compile? Does it end with 1;?";

foreach my $sub (@subs) {
    can_ok $module, $sub or BAIL_OUT "Missing package $module; or missing sub $sub()";
}

foreach my $c (@cases) {
    
    my $crypto = $module->new($c->{plain});
    my $sub = $c->{sub};

    if (ref $c->{expected} eq 'ARRAY') {
        is_deeply $crypto->$sub, $c->{expected}, $c->{sub} . " $c->{plain} to @{$c->{expected}}";
    } else {
        is $crypto->$sub, $c->{expected}, $c->{sub} . " $c->{plain} to $c->{expected}";
    }
}