Suppose there is a PERL Module- fib.pm
Function- returns 1 if number is Fibonacci, otherwise return 0.
Range- 1 to 10
Location: C:\Perl\site\lib
Code(fib.pm):
use strict;
use warnings;
my @arr=(1,2,3,5);
sub fib{
my @a1=@_;
my $a=$a1[0];
my $c=0;
foreach(@arr)
{
if($_==$a)
{
$c++;
return 1;
last;
}
}
if($c==0)
{
return 0;
}
}
Function- returns 1 if number is Fibonacci, otherwise return 0.
Range- 1 to 10
Location: C:\Perl\site\lib
Code(fib.pm):
use strict;
use warnings;
my @arr=(1,2,3,5);
sub fib{
my @a1=@_;
my $a=$a1[0];
my $c=0;
foreach(@arr)
{
if($_==$a)
{
$c++;
return 1;
last;
}
}
if($c==0)
{
return 0;
}
}
Clearly we have a bug here:- This program will output 0 for 8.
Test Program(t.pl):
use strict;
use warnings;
use Test::Simple tests => 10;
use fib;
ok( fib(1) == 1 );
ok( fib(2) == 1 );
ok( fib(3) == 1 );
ok( fib(4) == 0 );
ok( fib(5) == 1 );
ok( fib(6) == 0 );
ok( fib(7) == 0 );
ok( fib(8) == 1 );
ok( fib(9) == 0 );
ok( fib(10) == 0 );
Output of t.pl

No comments:
Post a Comment