From ba69da82ed7e2f85d11a990fa535e302b6cacf38 Mon Sep 17 00:00:00 2001 From: Theo van Hoesel Date: Thu, 25 Jul 2013 02:33:22 +0200 Subject: [PATCH] * Fixed CPAN RT bug #85332 Changes is 0 bytes (this file) * Fixed CPAN RT bug #87221 Division by Zero * t/15_zeroes.t: Added tests for divisions by zero * t/16_decimals.t: Added tests for unsupported non-integer arguments --- Changes | 136 +++++++++++++++++++++++++++++++++++++++++ lib/Number/Fraction.pm | 10 ++- t/15_zeroes.t | 17 ++++++ t/16_decimals.t | 26 ++++++++ 4 files changed, 186 insertions(+), 3 deletions(-) create mode 100644 Changes create mode 100644 t/15_zeroes.t create mode 100644 t/16_decimals.t diff --git a/Changes b/Changes new file mode 100644 index 0000000..ccad76a --- /dev/null +++ b/Changes @@ -0,0 +1,136 @@ +2013-07-25 Theo van Hoesel + + * Fixed CPAN RT bug #85332 Changes is 0 bytes (this file) + + * Fixed CPAN RT bug #87221 Division by Zero + + * t/15_zeroes.t: Added tests for divisions by zero + + * t/16_decimals.t: Added tests for unsupported non-integer arguments + +2013-05-14 Dave Cross + + * Version 2: Now With Added Moose + +2011-11-26 Dave Cross + + * lib/Number/Fraction.pm: Fix typo in docs. + + * lib/Number/Fraction.pm: Bump version number. + + * MANIFEST, t/14_abs.t: Added more tests for 'abs'. Added META.json to + MANIFEST. + +2011-11-24 Dave Cross + + * lib/Number/Fraction.pm: Implemented the 'abs' function. Tests now pass. + + * t/14_abs.t: Added test for the 'abs' function. + +2010-03-21 Dave Cross + + * lib/Number/Fraction.pm: Added documentation for the exponentiation support. + + * lib/Number/Fraction.pm, t/13_exp.t: Added simple support for + exponentiation. + +2010-03-15 Dave Cross + + * t/13_exp.t: Added tests for exponentiation. And they all pass without any + code changes! + +2010-02-09 Dave Cross + + * lib/Number/Fraction.pm: Fixed a couple of typos in the Pod. Thanks to + Kornel Umann for spotting them. + +2009-06-15 Dave Cross + + * Build.PL, lib/Number/Fraction.pm, t/12_invalid.t: Fixed CPAN RT bug #46961 + (and added tests). Move VERSION setting into Number/Fraction.pm. + +2009-02-21 Dave Cross + + * : Removed execute bits on tests. + +2008-03-09 Dave Cross + + * Build.PL: Fiddled with dependencies. Incremented version number. + +2008-03-08 Dave Cross + + * MANIFEST: Added Build.PL and META.yml + + * lib/Number/Fraction.pm: Changed email address and copyright date + + * Build.PL, Makefile.PL: Moved from Makefile.PL to Build.PL. + + * lib/Number/Fraction.pm: Use svn revision number. Remove old cvs logs. + + * MANIFEST: Make non-executable + + * t/01_load.t, t/02_create.t, t/03_create.t, t/04_add.t, t/05_subtract.t, + t/06_multiply.t, t/07_divide.t, t/08_compare.t, t/09_neg.t: Switch to using + Test::More throughout. Remove old Test.pm black magic comments. + + * : Make non-executable + +2006-03-02 Dave Cross + + * t/05_subtract.t: fixed one test that failed after applying previous fixes. + + * lib/Number/Fraction.pm, t/02_create.t: A couple of patches supplied by + David Westbrook. + +2005-10-22 Dave Cross + + * lib/Number/Fraction.pm: Added new tests. + + * MANIFEST: Added pod coverage tests. + + * t/11_pod_coverage.t: Added Pod coverage tests. + +2004-10-23 Dave Cross + + * lib/Number/Fraction.pm: Improved test coverage (to 100% - Go Me!) + +2004-05-23 Dave Cross + + * Makefile.PL, lib/Number/Fraction.pm, t/10_pod.t: Changed pod tests. Updated + my email address in Makefile.PL + + * Makefile.PL: Changed references to Fraction.pm + + * MANIFEST: Added t/10_pod.t + + * MANIFEST: Moved Fraction.pm to lib/Number + + * Changes: Removed Changes (now autogenerated) + +2004-05-22 Dave Cross + + * lib/Number/Fraction.pm, t/02_create.t, t/03_create.t, t/04_add.t, + t/05_subtract.t, t/06_multiply.t, t/07_divide.t, t/10_pod.t: Added more + tests. Fixed a couple of bugs that they uncovered. + +2004-04-28 Dave Cross + + * MANIFEST, lib/Number/Fraction.pm: Added negative tests to MANIFEST + +2004-04-27 Dave Cross + + * lib/Number/Fraction.pm: Added support for negative numbers. + + * t/09_neg.t: Added tests for negative numbers + +2003-02-19 Dave Cross + + * t/08_compare.t: Corrected invalid string tests. '1/2' lt '1/4' (when + considered as strings) + + * lib/Number/Fraction.pm: Correct '+0' to '0+'. Added "fallback" - which + allowed me to remove cmp and ncmp. + + * Changes, MANIFEST, Makefile.PL, README, lib/Number/Fraction.pm, + t/01_load.t, t/02_create.t, t/03_create.t, t/04_add.t, t/05_subtract.t, + t/06_multiply.t, t/07_divide.t, t/08_compare.t: Initial revision diff --git a/lib/Number/Fraction.pm b/lib/Number/Fraction.pm index 812748b..06b8475 100755 --- a/lib/Number/Fraction.pm +++ b/lib/Number/Fraction.pm @@ -126,7 +126,7 @@ use warnings; use Carp; use Moose; -our $VERSION = '2.00'; +our $VERSION = '2.01'; use overload q("") => 'to_string', @@ -221,7 +221,7 @@ around BUILDARGS => sub { my $class = shift; if (@_ >= 2) { - die unless $_[0] =~ /^-?[0-9]+\z/ and $_[1] =~ /^-?[0-9]+\z/; + die "numinator and denominator both need to be integers" unless $_[0] =~ /^-?[0-9]+\z/ and $_[1] =~ /^-?[0-9]+\z/; return $class->$orig({ num => $_[0], den => $_[1] }); } elsif (@_ == 1) { @@ -232,7 +232,7 @@ around BUILDARGS => sub { die "Can't make a $class from a ", ref $_[0]; } } else { - die unless $_[0] =~ m|^(-?[0-9]+)(?:/(-?[0-9]+))?\z|; + die "numinator and denominator both need to be integers" unless $_[0] =~ m|^(-?[0-9]+)(?:/(-?[0-9]+))?\z|; return $class->$orig({ num => $1, den => ( defined $2 ? $2 : 1) }); } @@ -250,6 +250,7 @@ normalised format. sub BUILD { my $self = shift; + die "Denominator can\'t be equal to \'zero\'" if $self->{den} == 0; $self->_normalise; } @@ -403,6 +404,7 @@ sub div { if (ref $r) { if (UNIVERSAL::isa($r, ref $l)) { + die "FATAL ERROR: Division by zero" if $r->{num} == 0; return (ref $l)->new($l->{num} * $r->{den}, $l->{den} * $r->{num}); } else { @@ -477,6 +479,8 @@ sub _hcf { return $x; } +__PACKAGE__->meta->make_immutable; + 1; __END__ diff --git a/t/15_zeroes.t b/t/15_zeroes.t new file mode 100644 index 0000000..e2e3abb --- /dev/null +++ b/t/15_zeroes.t @@ -0,0 +1,17 @@ +use Test::More 'no_plan'; +use Number::Fraction; + +my $f = eval { Number::Fraction->new('1/0') }; +ok($@, "Denominator of zero should not allowed in string" ); + +$f = eval { Number::Fraction->new(1 , 0 ) }; +ok($@, "Denominator of zero should not allowed in two ints" ); + +my $zero = Number::Fraction->new (0); +cmp_ok($zero, '==', 0, "created a \'zero\' fraction"); + +my $qrtr = Number::Fraction->new('1/4'); +cmp_ok($qrtr, '==', 0.25, "Created 1/4"); + +my $divz = eval { $qrtr / $zero }; +ok($@, "Division by zero should cause FATAL ERROR"); diff --git a/t/16_decimals.t b/t/16_decimals.t new file mode 100644 index 0000000..1e2852a --- /dev/null +++ b/t/16_decimals.t @@ -0,0 +1,26 @@ +use Test::More 'no_plan'; +use Number::Fraction ':constants'; + +my $fract = undef; + +$fract = eval { Number::Fraction->new(3.0 , 4) }; +cmp_ok($fract, 'eq' , '3/4', "fraction from round decimal numinator as number"); + +$fract = eval {Number::Fraction->new(3 , 4.0) }; +cmp_ok($fract, 'eq' , '3/4', "fraction from round decimal denominator as number"); + +$fract = eval {Number::Fraction->new('3', '4' ) }; +ok($@, "fraction from two healty strings not supported, use numbers instead"); + +$fract = eval {Number::Fraction->new('3.0', '4' ) }; +ok($@, "fraction from round decimal numinator as string not supported"); + +$fract = eval {Number::Fraction->new('3' , '4.0') }; +ok($@, "fraction from round decimal denominator as string not supported"); + +$fract = eval {Number::Fraction->new(3.5, 4 ) }; +ok($@, "Numinator can\'t be a decimal"); + +$fract = eval {Number::Fraction->new(3 , 4.5) }; +ok($@, "Denominator can\'t be a decimal"); +