1
exercism fetch perl5 pascals-triangle

triangle.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
#!/usr/bin/env perl
use strict;
use warnings;

use Test::More;
use JSON::PP qw(decode_json);
use FindBin;
my $dir;
BEGIN { $dir = $FindBin::Bin . '/' };
use lib $dir;

my $module = 'Triangle';

my $cases;
{
    local $/ = undef;
    $cases = decode_json scalar <DATA>;
}

plan tests => 5 + @$cases;

ok -e "${dir}${module}.pm", "missing $module.pm"
    or BAIL_OUT("You need to create a class called $module.pm with a constructor called new.");

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

foreach my $sub (qw|triangle is_triangle row|) {
    can_ok($module, $sub) or BAIL_OUT("Missing package $module; or missing sub $sub()");
}

foreach my $c (@$cases) {
    no strict 'refs';
    my $sub = "${module}::" . $c->{sub};
    if ($c->{sub} eq "triangle") {
        is_deeply $sub->($c->{param}), $c->{expected}, $c->{name};
    } else {
        is $sub->($c->{param}), $c->{expected}, $c->{name};
    }
}

__DATA__
[
    {
        "sub"      : "triangle",
        "param"    : 4,
        "expected" : ["1", "1 1", "1 2 1", "1 3 3 1", "1 4 6 4 1"],
        "name"     : "pascals triangle of 0-4 rows"
    },
    {
        "sub"      : "triangle",
        "param"    : 6,
        "expected" : ["1", "1 1", "1 2 1", "1 3 3 1", "1 4 6 4 1", "1 5 10 10 5 1",
                       "1 6 15 20 15 6 1"],
        "name"     : "pascals triangle of 0-6 rows"
    },
    {
        "sub"      : "is_triangle",
        "param"    : ["1", "1 1", "1 2 1", "1 3 3 1", "1 4 6 4 1", "1 5 10 10 5 1"],
        "expected" : true,
        "name"     : "test a valid triangle"
    },
    {
        "sub"      : "is_triangle",
        "param"    : ["1", "1 1", "1 2 1", "1 4 4 1"],
        "expected" : false,
        "name"     : "test a NOT valid triangle"
    },
    {
        "sub"      : "row",
        "param"    : 0,
        "expected" : "1",
        "name"     : "row 0 of pascals triangle"
    },
    {
        "sub"      : "row",
        "param"    : 2,
        "expected" : "1 2 1",
        "name"     : "row 2 of pascals triangle"
    },
    {
        "sub"      : "row",
        "param"    : 7,
        "expected" : "1 7 21 35 35 21 7 1",
        "name"     : "row 7 of pascals triangle"
    }
]