1
exercism fetch perl5 list-ops

list-ops.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/env perl
use strict; 
use warnings;

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

my $module = $ENV{EXERCISM} ? 'Example' : 'ListOps';

my %test_cases = (
   map => {
      tests => [
         {  name     => 'square',
            input    => [qw(-1 2 -3 4 -5 6 -7 8 -9 10)],      
            function => sub { $_[0] * $_[0] },
            expected => [qw(1 4 9 16 25 36 49 64 81 100)],
         },
         {  name     => 'cube',
            function => sub { $_[0] * $_[0] * $_[0] },
            input    => [qw(-1 2 -3 4 -5 6 -7 8 -9 10)],      
            expected => [qw(-1 8 -27 64 -125 216 -343 512 -729 1000)],
         },
         {  name     => 'abs',
            function => sub { $_[0] > 0 ? $_[0] : -$_[0] },
            input    => [qw(-1 2 -3 4 -5 6 -7 8 -9 10)],      
            expected => [qw(1 2 3 4 5 6 7 8 9 10)],
         },
         {  name     => 'inc',
            function => sub { $_[0] + 1 },
            input    => [qw(-1 2 -3 4 -5 6 -7 8 -9 10)],      
            expected => [qw(0 3 -2 5 -4 7 -6 9 -8 11)],
         },
         {  name     => 'dec',
            function => sub { $_[0] - 1 },
            input    => [qw(-1 2 -3 4 -5 6 -7 8 -9 10)],      
            expected => [qw(-2 1 -4 3 -6 5 -8 7 -10 9)],
         },
      ], 
   },
   reduce => {
      tests => [
         {  name     => 'summation', 
            function => sub { $_[0] + $_[1] },
            input    => [qw(1 2 3 4 5 6)],
            expected => 21,
         },
         {  name     => 'product',
            function => sub { $_[0] * $_[1] },
            input    => [qw(1 2 3 4 5 6)],
            expected => 720,
         },
         {  name     => 'concatenation',
            function => sub { $_[0] . $_[1] },
            input    => [qw(1 2 3 4 5 6)],
            expected => '123456',
         },
      ],      
   },
   length => {
      tests => [
         {  name     => 'postive length',
            input    => [qw(-1 2 -3 4 -5 6 -7 8 -9 10)],
            expected => 10,
         },
         {  name     => 'empty length',
            input    => [],
            expected => 0,
         },
      ],
   },
);

my $num_test_cases;
foreach my $test_type ( keys %test_cases ) {
   $num_test_cases += @{ $test_cases{$test_type}->{tests} };
}

plan tests => 5 + $num_test_cases;

ok -e "${dir}${module}.pm", "missing $module.pm"
   or BAIL_OUT("You need to create a class called $module.pm with 3 functions: map, reduce, and length.");

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

can_ok($module, 'map')
   or BAIL_OUT("Missing package $module; or missing sub map()");

can_ok($module, 'reduce')
   or BAIL_OUT("Missing package $module; or missing sub reduce()");

can_ok($module, 'length')
   or BAIL_OUT("Missing package $module; or missing sub length()");

foreach my $test_type (keys %test_cases) {
   no strict 'refs';
   my $f = "${module}::$test_type";
   foreach my $test ( @{ $test_cases{$test_type}->{tests} } ) {
      if ( exists $test->{function} ) {
         is_deeply(
            $f->($test->{function}, $test->{input}),
            $test->{expected},
            $test->{name},
         );         
      }
      else { # length tests
         is_deeply(
            $f->($test->{input}),
            $test->{expected},
            $test->{name},
         ); 
      }
               
   }
}