AI-Prolog
view release on metacpan or search on metacpan
#!/usr/bin/perl
# '$Id: 80math.t,v 1.5 2005/08/06 23:28:40 ovid Exp $';
use warnings;
use strict;
use Test::More tests => 31;
#use Test::More qw/no_plan/;
use Clone qw/clone/;
BEGIN
{
chdir 't' if -d 't';
unshift @INC => '../lib';
}
use aliased 'AI::Prolog';
use aliased 'AI::Prolog::Engine';
use aliased 'AI::Prolog::KnowledgeBase';
#
# Math
#
Engine->formatted(1);
my $prolog = Prolog->new(<<'END_PROLOG');
value(rubies, 100).
value(paper, 1).
thief(badguy).
steals(PERP, STUFF) :-
value(STUFF, DOLLARS),
gt(DOLLARS, 50).
END_PROLOG
$prolog->query('is(X,7)');
is $prolog->results, 'is(7, 7)', 'is/2 should be able to bind a term to a var';
$prolog->query('is(X,-7)');
is $prolog->results, 'is(-7, -7)', '... and it should handle negative numbers';
$prolog->query('is(X,.7)');
is $prolog->results, 'is(.7, .7)', '... and number which begin with decimal points';
$prolog->query('is(X,-.7)');
is $prolog->results, 'is(-.7, -.7)', '... and negative numbers with decimal points';
$prolog->query('is(7,X)');
eval {$prolog->results};
like $@, qr/Tried to to get value of unbound term \(A\)/,
'... but trying to call is(7,X) with an unbound rhs should die';
$prolog->query('is(7,7)');
is $prolog->results, 'is(7, 7)', '... but it should succeed if both terms are bound and equal';
$prolog->query('is(5,7)');
ok ! defined $prolog->results, '... and it should fail if both terms are bound but unequal';
$prolog->query('gt(4,3)');
is $prolog->results, 'gt(4, 3)',
'gt(X,Y) should succeed if the first argument > the second argument.';
$prolog->query('gt(3,34)');
ok ! $prolog->results,
'... and it should fail if the first argument < the second argument.';
$prolog->query('gt(3,3)');
ok ! $prolog->results,
'... and it should fail if the first argument = the second argument.';
( run in 0.768 second using v1.01-cache-2.11-cpan-39bf76dae61 )