Interesting problem. As others have written, I may have doubts about the basic idea, but I, also, couldn't resist...
Here's what I've been able to come up with. (Is this the proper place to post? Maybe my scratchpad...?)
# InferWhatever.t 20oct08waw
use warnings;
use strict;
use Test::More
'no_plan',
# tests => ??,
;
use InferWhatever qw(series);
use constant {
NO => 0,
YES => 1,
};
my @tests = (
# for the given the do we expect
# sequence, first n, completion?
[ [ 1, 1, 1, 1, ], 1, NO, ],
[ [ 1, 1, 1, 1, ], 2, YES, ],
[ [ 1, 1, 1, 1, ], 3, YES, ],
[ [ 0, 0, 0, 0, ], 1, NO, ],
[ [ 0, 0, 0, 0, ], 2, YES, ],
[ [ 0, 0, 0, 0, ], 3, YES, ],
[ [ 9, 9, 9, 9, 9, 9, 9, ], 3, YES, ],
[ [ 1, 2, 3, 4, 5, 6, ], 2, NO, ],
[ [ 1, 2, 3, 4, 5, 6, ], 3, YES, ],
[ [ 13, 11, 9, 7, 5, 3, ], 3, YES, ],
[ [ 1, 0, -1, -2, -3, ], 2, NO, ],
[ [ 1, 0, -1, -2, -3, ], 3, YES, ],
[ [ 1, 4, 9, 16, 25, 36, 49, 64, ], 2, NO, ],
[ [ 1, 4, 9, 16, 25, 36, 49, 64, ], 3, NO, ],
[ [ 1, 4, 9, 16, 25, 36, 49, 64, ], 4, YES, ],
[ [ 64, 49, 36, 25, 16, 9, 4, 1, ], 4, YES, ],
[ [ 34, 19, 6, -5, -14, -21, -26, -29, ], 4, YES, ],
[ [ 35, 24, 15, 8, 3, 0, ], 4, YES, ],
[ [ 81, 100, 121, 144, 169, 196, ], 2, NO, ],
[ [ 81, 100, 121, 144, 169, 196, ], 3, NO, ],
[ [ 81, 100, 121, 144, 169, 196, ], 4, YES, ],
[ [ 196, 169, 144, 121, 100, 81, ], 2, NO, ],
[ [ 196, 169, 144, 121, 100, 81, ], 3, NO, ],
[ [ 196, 169, 144, 121, 100, 81, ], 4, YES, ],
# [ [1, 2, 3], 4],
# [ [1, 2, 4], 8], # powers
# [ [2, 4, 8], 16],
# [ [1, 3, 9], 27],
# [ [1, -1, 1, -1], 1], # alternations
# [ [-1, 1, -1, 1], -1],
# [ [1, 0, 1, 0], 1],
# [ [0, 1, 0, 1], 0],
# [ [1, 1, 2, 3, 5], 8], # fibonacci
# [ [0, 1, 1, 2, 3], 8],
# [ [1, 2, 3, 5, 8], 13],
# [ [1, 2, 6, 24, 120], 720], # factorials
# [ [1, 0, 0, 1], undef],
# [ [1, 2, 3, 1], undef],
# [ [], ],
);
for my $ar_test (@tests) {
my @sequence = @{ $ar_test->[0] }; # full sequence
my $given = $ar_test->[1]; # initial stimulus
my $finish = $ar_test->[2]; # completion expected?
my @hint = @sequence[ 0 .. $given - 1 ];
my $iter = series(@hint);
my @generated = @{ $finish ? \@hint : [] };
my @expected = @{ $finish ? \@sequence : [] };
if ($iter) { # check iterator if one was generated
push @generated, $iter->() for 0 .. $#sequence - $given;
}
my $expected = "(@expected)";
my $got = "(@generated)";
my $hint = "(@hint)";
my $desc = "from $hint, expected $expected, got $got";
is ($expected, $got, $desc);
}