aboutsummaryrefslogtreecommitdiff
path: root/challenge-094/paulo-custodio/test.pl
blob: 43d13408023097fe62adc6d68169a09f658237b7 (plain)
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
#!/usr/bin/perl

use Modern::Perl;
use Test::More;
use Path::Tiny;

my $input = "input.txt";

compile("gcc -o   c/ch-1     c/ch-1.c");
compile("g++ -o cpp/ch-1   cpp/ch-1.cpp");
compile("fbc             basic/ch-1.bas");


for (["opt bat saw tab pot top was" => <<END],
[ ("bat", "tab"),
  ("saw", "was"),
  ("opt", "pot", "top") ]
END
     ["x" => <<END]) {
[ ("x") ]
END
    my($in, $out) = @$_;

    is capture("perl perl/ch-1.pl $in"), $out;
    is capture(        "c/ch-1    $in"), $out;
    is capture(      "cpp/ch-1    $in"), $out;
}

for (["opt bat saw tab pot top was" => <<END],
[ ("opt", "pot", "top"),
  ("bat", "tab"),
  ("saw", "was") ]
END
     ["x" => <<END]) {
[ ("x") ]
END
    my($in, $out) = @$_;

    is capture("python python/ch-1.py $in"), $out;
    is capture( "gforth forth/ch-1.fs $in"), $out;
    is capture(        "basic/ch-1    $in"), $out;
}


compile("gcc -o c/ch-2       c/ch-2.c");
compile("g++ -o cpp/ch-2   cpp/ch-2.cpp");
compile("fbc             basic/ch-2.bas");


for ([<<'END' => "1 -> 2 -> 4 -> 5 -> 6 -> 7 -> 3"],
        1
       / \
      2   3
     / \
    4   5
       / \
      6   7
END
     [<<'END' => "7 -> 5 -> 4 -> 6 -> 8 -> 9"]) {
        7
       / \
      5   8
     / \   \
    4   6   9
END
    my($in, $out) = @$_;
    path($input)->spew($in);

    is capture(    "perl perl/ch-2.pl < $input"),  "$out\n";
    is capture("python python/ch-2.py < $input"),  "$out\n";
    is capture( "gforth forth/ch-2.fs < $input"),  "$out\n";
    is capture(            "c/ch-2    < $input"),  "$out\n";
    is capture(          "cpp/ch-2    < $input"),  "$out\n";
    is capture(        "basic/ch-2    < $input"),  "$out\n";
}

unlink $input;
done_testing;

sub capture {
    my($cmd) = @_;
    my $out = `$cmd`;
    $out =~ s/[ \t\v\f\r]*\n/\n/g;
    return $out;
}

sub compile {
    my($cmd) = @_;
    0==system($cmd) or die "Compile failed: $cmd\n";
}