Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Rigid testing - Division by Zero and more #1

Merged
merged 2 commits into from 12 months ago

2 participants

Theo van Hoesel Dave Cross
Theo van Hoesel

Hi Dave,

I've made my first edits and fixed some bugs

  * 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
Theo van Hoesel added some commits
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
ba69da8
Th.J.v.Hoesel * MANIFEST includes t/15_zeroes.t and t/16_decimals.t 4b980f1
Dave Cross davorg merged commit 9b498dc into from
Dave Cross davorg closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Jul 25, 2013
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
ba69da8
Th.J.v.Hoesel * MANIFEST includes t/15_zeroes.t and t/16_decimals.t 4b980f1
This page is out of date. Refresh to see the latest.
136 Changes
... ... @@ -0,0 +1,136 @@
  1 +2013-07-25 Theo van Hoesel <Th.J.v.Hoesel@gmail.com>
  2 +
  3 + * Fixed CPAN RT bug #85332 Changes is 0 bytes (this file)
  4 +
  5 + * Fixed CPAN RT bug #87221 Division by Zero
  6 +
  7 + * t/15_zeroes.t: Added tests for divisions by zero
  8 +
  9 + * t/16_decimals.t: Added tests for unsupported non-integer arguments
  10 +
  11 +2013-05-14 Dave Cross <dave@dave.org.uk>
  12 +
  13 + * Version 2: Now With Added Moose
  14 +
  15 +2011-11-26 Dave Cross <dave@dave.org.uk>
  16 +
  17 + * lib/Number/Fraction.pm: Fix typo in docs.
  18 +
  19 + * lib/Number/Fraction.pm: Bump version number.
  20 +
  21 + * MANIFEST, t/14_abs.t: Added more tests for 'abs'. Added META.json to
  22 + MANIFEST.
  23 +
  24 +2011-11-24 Dave Cross <dave@dave.org.uk>
  25 +
  26 + * lib/Number/Fraction.pm: Implemented the 'abs' function. Tests now pass.
  27 +
  28 + * t/14_abs.t: Added test for the 'abs' function.
  29 +
  30 +2010-03-21 Dave Cross <dave@angel.mag-sol.com>
  31 +
  32 + * lib/Number/Fraction.pm: Added documentation for the exponentiation support.
  33 +
  34 + * lib/Number/Fraction.pm, t/13_exp.t: Added simple support for
  35 + exponentiation.
  36 +
  37 +2010-03-15 Dave Cross <dave@angel.mag-sol.com>
  38 +
  39 + * t/13_exp.t: Added tests for exponentiation. And they all pass without any
  40 + code changes!
  41 +
  42 +2010-02-09 Dave Cross <dave@dacross.(none)>
  43 +
  44 + * lib/Number/Fraction.pm: Fixed a couple of typos in the Pod. Thanks to
  45 + Kornel Umann for spotting them.
  46 +
  47 +2009-06-15 Dave Cross <dave@dave.org.uk>
  48 +
  49 + * Build.PL, lib/Number/Fraction.pm, t/12_invalid.t: Fixed CPAN RT bug #46961
  50 + (and added tests). Move VERSION setting into Number/Fraction.pm.
  51 +
  52 +2009-02-21 Dave Cross <dave@dave.org.uk>
  53 +
  54 + * : Removed execute bits on tests.
  55 +
  56 +2008-03-09 Dave Cross <dave@dave.org.uk>
  57 +
  58 + * Build.PL: Fiddled with dependencies. Incremented version number.
  59 +
  60 +2008-03-08 Dave Cross <dave@dave.org.uk>
  61 +
  62 + * MANIFEST: Added Build.PL and META.yml
  63 +
  64 + * lib/Number/Fraction.pm: Changed email address and copyright date
  65 +
  66 + * Build.PL, Makefile.PL: Moved from Makefile.PL to Build.PL.
  67 +
  68 + * lib/Number/Fraction.pm: Use svn revision number. Remove old cvs logs.
  69 +
  70 + * MANIFEST: Make non-executable
  71 +
  72 + * t/01_load.t, t/02_create.t, t/03_create.t, t/04_add.t, t/05_subtract.t,
  73 + t/06_multiply.t, t/07_divide.t, t/08_compare.t, t/09_neg.t: Switch to using
  74 + Test::More throughout. Remove old Test.pm black magic comments.
  75 +
  76 + * : Make non-executable
  77 +
  78 +2006-03-02 Dave Cross <dave@dave.org.uk>
  79 +
  80 + * t/05_subtract.t: fixed one test that failed after applying previous fixes.
  81 +
  82 + * lib/Number/Fraction.pm, t/02_create.t: A couple of patches supplied by
  83 + David Westbrook.
  84 +
  85 +2005-10-22 Dave Cross <dave@dave.org.uk>
  86 +
  87 + * lib/Number/Fraction.pm: Added new tests.
  88 +
  89 + * MANIFEST: Added pod coverage tests.
  90 +
  91 + * t/11_pod_coverage.t: Added Pod coverage tests.
  92 +
  93 +2004-10-23 Dave Cross <dave@dave.org.uk>
  94 +
  95 + * lib/Number/Fraction.pm: Improved test coverage (to 100% - Go Me!)
  96 +
  97 +2004-05-23 Dave Cross <dave@dave.org.uk>
  98 +
  99 + * Makefile.PL, lib/Number/Fraction.pm, t/10_pod.t: Changed pod tests. Updated
  100 + my email address in Makefile.PL
  101 +
  102 + * Makefile.PL: Changed references to Fraction.pm
  103 +
  104 + * MANIFEST: Added t/10_pod.t
  105 +
  106 + * MANIFEST: Moved Fraction.pm to lib/Number
  107 +
  108 + * Changes: Removed Changes (now autogenerated)
  109 +
  110 +2004-05-22 Dave Cross <dave@dave.org.uk>
  111 +
  112 + * lib/Number/Fraction.pm, t/02_create.t, t/03_create.t, t/04_add.t,
  113 + t/05_subtract.t, t/06_multiply.t, t/07_divide.t, t/10_pod.t: Added more
  114 + tests. Fixed a couple of bugs that they uncovered.
  115 +
  116 +2004-04-28 Dave Cross <dave@dave.org.uk>
  117 +
  118 + * MANIFEST, lib/Number/Fraction.pm: Added negative tests to MANIFEST
  119 +
  120 +2004-04-27 Dave Cross <dave@dave.org.uk>
  121 +
  122 + * lib/Number/Fraction.pm: Added support for negative numbers.
  123 +
  124 + * t/09_neg.t: Added tests for negative numbers
  125 +
  126 +2003-02-19 Dave Cross <dave@dave.org.uk>
  127 +
  128 + * t/08_compare.t: Corrected invalid string tests. '1/2' lt '1/4' (when
  129 + considered as strings)
  130 +
  131 + * lib/Number/Fraction.pm: Correct '+0' to '0+'. Added "fallback" - which
  132 + allowed me to remove cmp and ncmp.
  133 +
  134 + * Changes, MANIFEST, Makefile.PL, README, lib/Number/Fraction.pm,
  135 + t/01_load.t, t/02_create.t, t/03_create.t, t/04_add.t, t/05_subtract.t,
  136 + t/06_multiply.t, t/07_divide.t, t/08_compare.t: Initial revision
2  MANIFEST
@@ -18,5 +18,7 @@ t/11_pod_coverage.t
18 18 t/12_invalid.t
19 19 t/13_exp.t
20 20 t/14_abs.t
  21 +t/15_zeroes.t
  22 +t/16_decimals.t
21 23 META.yml
22 24 META.json
10 lib/Number/Fraction.pm
@@ -126,7 +126,7 @@ use warnings;
126 126 use Carp;
127 127 use Moose;
128 128
129   -our $VERSION = '2.00';
  129 +our $VERSION = '2.01';
130 130
131 131 use overload
132 132 q("") => 'to_string',
@@ -221,7 +221,7 @@ around BUILDARGS => sub {
221 221 my $class = shift;
222 222
223 223 if (@_ >= 2) {
224   - die unless $_[0] =~ /^-?[0-9]+\z/ and $_[1] =~ /^-?[0-9]+\z/;
  224 + die "numinator and denominator both need to be integers" unless $_[0] =~ /^-?[0-9]+\z/ and $_[1] =~ /^-?[0-9]+\z/;
225 225
226 226 return $class->$orig({ num => $_[0], den => $_[1] });
227 227 } elsif (@_ == 1) {
@@ -232,7 +232,7 @@ around BUILDARGS => sub {
232 232 die "Can't make a $class from a ", ref $_[0];
233 233 }
234 234 } else {
235   - die unless $_[0] =~ m|^(-?[0-9]+)(?:/(-?[0-9]+))?\z|;
  235 + die "numinator and denominator both need to be integers" unless $_[0] =~ m|^(-?[0-9]+)(?:/(-?[0-9]+))?\z|;
236 236
237 237 return $class->$orig({ num => $1, den => ( defined $2 ? $2 : 1) });
238 238 }
@@ -250,6 +250,7 @@ normalised format.
250 250
251 251 sub BUILD {
252 252 my $self = shift;
  253 + die "Denominator can\'t be equal to \'zero\'" if $self->{den} == 0;
253 254 $self->_normalise;
254 255 }
255 256
@@ -403,6 +404,7 @@ sub div {
403 404
404 405 if (ref $r) {
405 406 if (UNIVERSAL::isa($r, ref $l)) {
  407 + die "FATAL ERROR: Division by zero" if $r->{num} == 0;
406 408 return (ref $l)->new($l->{num} * $r->{den},
407 409 $l->{den} * $r->{num});
408 410 } else {
@@ -477,6 +479,8 @@ sub _hcf {
477 479 return $x;
478 480 }
479 481
  482 +__PACKAGE__->meta->make_immutable;
  483 +
480 484 1;
481 485 __END__
482 486
17 t/15_zeroes.t
... ... @@ -0,0 +1,17 @@
  1 +use Test::More 'no_plan';
  2 +use Number::Fraction;
  3 +
  4 +my $f = eval { Number::Fraction->new('1/0') };
  5 +ok($@, "Denominator of zero should not allowed in string" );
  6 +
  7 +$f = eval { Number::Fraction->new(1 , 0 ) };
  8 +ok($@, "Denominator of zero should not allowed in two ints" );
  9 +
  10 +my $zero = Number::Fraction->new (0);
  11 +cmp_ok($zero, '==', 0, "created a \'zero\' fraction");
  12 +
  13 +my $qrtr = Number::Fraction->new('1/4');
  14 +cmp_ok($qrtr, '==', 0.25, "Created 1/4");
  15 +
  16 +my $divz = eval { $qrtr / $zero };
  17 +ok($@, "Division by zero should cause FATAL ERROR");
26 t/16_decimals.t
... ... @@ -0,0 +1,26 @@
  1 +use Test::More 'no_plan';
  2 +use Number::Fraction ':constants';
  3 +
  4 +my $fract = undef;
  5 +
  6 +$fract = eval { Number::Fraction->new(3.0 , 4) };
  7 +cmp_ok($fract, 'eq' , '3/4', "fraction from round decimal numinator as number");
  8 +
  9 +$fract = eval {Number::Fraction->new(3 , 4.0) };
  10 +cmp_ok($fract, 'eq' , '3/4', "fraction from round decimal denominator as number");
  11 +
  12 +$fract = eval {Number::Fraction->new('3', '4' ) };
  13 +ok($@, "fraction from two healty strings not supported, use numbers instead");
  14 +
  15 +$fract = eval {Number::Fraction->new('3.0', '4' ) };
  16 +ok($@, "fraction from round decimal numinator as string not supported");
  17 +
  18 +$fract = eval {Number::Fraction->new('3' , '4.0') };
  19 +ok($@, "fraction from round decimal denominator as string not supported");
  20 +
  21 +$fract = eval {Number::Fraction->new(3.5, 4 ) };
  22 +ok($@, "Numinator can\'t be a decimal");
  23 +
  24 +$fract = eval {Number::Fraction->new(3 , 4.5) };
  25 +ok($@, "Denominator can\'t be a decimal");
  26 +

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.