$ cat math.pm package math; use strict; use warnings; # Perl Cookbook, 2.1. Checking Whether a String Is a Valid Number # Return 1 if input is a C float, otherwise undef. sub is_float { ($_[0] && $_[0] =~ /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$ +/) ? return 1 : return; } sub addition { my $sum; my (@mynumbers) = @_; for (@mynumbers) { die "invalid argument" if !is_float($_); } for (@mynumbers) { $sum += $_; } return $sum; } 1; __END__ $ cat math.t use strict; use warnings; use Test::More qw(no_plan); use Test::Exception; use lib qw(.); BEGIN { use_ok('math'); } # ------ some negative tests throws_ok { math::addition(undef); } qr/invalid argument/, q{caught invalid argument ok}; throws_ok { math::addition(q{a}); } qr/invalid argument/, q{caught invalid argument ok}; throws_ok { math::addition(1, 2, 3, q{-}); } qr/invalid argument/, q{caught invalid argument ok}; # ------ some positive tests cmp_ok(math::addition(1, 2, 3), q{==}, 6, q{Returns 6}); ok(math::is_float(math::addition(1e2, 2.5, 3.14)), q{Returns a float}) +; cmp_ok(math::addition(1.5, 2.5, 3.5), q{==}, 7.5, q{Returns 7.5}); __END__ $ prove math.t math....ok All tests successful. Files=1, Tests=7, 0 wallclock secs ( 0.04 cusr + 0.01 csys = 0.05 C +PU)
In reply to Re: cheesy comparison program not working!!
by andreas1234567
in thread cheesy comparison program not working!!
by navinjathan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |