Skip to content

Commit

Permalink
Add .succ and .pred.
Browse files Browse the repository at this point in the history
  • Loading branch information
colomon committed Jan 29, 2011
1 parent 6cc43b1 commit 2cfe839
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/Math/BigInt.pm
Expand Up @@ -3,6 +3,8 @@ use NativeCall;
sub bdNew() returns OpaquePointer is native("libbd") { ... }
sub bdFreeSol(OpaquePointer $bd) is native("libbd") { ... }
sub bdConvFromDecimal(OpaquePointer $bd, Str $digits) returns Int is native("libbd") { ... }
sub bdIncrement(OpaquePointer $w) returns Int is native("libbd") { ... }
sub bdDecrement(OpaquePointer $w) returns Int is native("libbd") { ... }
sub bdAdd(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
sub bdSubtract(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
sub bdMultiply(OpaquePointer $w, OpaquePointer $u, OpaquePointer $v) returns Int is native("libbd") { ... }
Expand Down Expand Up @@ -45,6 +47,9 @@ class Math::BigInt does Real {
+self.Str;
}

method succ(Math::BigInt $x:) { bdIncrement($x.bd); self; }
method pred(Math::BigInt $x:) { bdDecrement($x.bd); self; }

multi sub infix:<+>(Math::BigInt $a, Math::BigInt $b) is export(:DEFAULT) {
my $result = Math::BigInt.new("1");
bdAdd($result.bd, $a.bd, $b.bd);
Expand Down
18 changes: 18 additions & 0 deletions t/01-basic.t
Expand Up @@ -20,4 +20,22 @@ plan *;
is_approx +$a, 1234567890098765432100123456789, "Numifies properly";
}

{
my $a = Math::BigInt.new("21");
isa_ok $a, Math::BigInt, "We successfully made a BigInt";
ok $a ~~ Numeric & Real, "It's Numeric and Real, too";
is +$a, 21, "Starts with proper value";
my $b = $a.succ;
is +$b, 22, ".succ works";
$a = $b.pred;
is +$a, 21, ".pred works";

$a++;
is +$a, 22, "Increment works";
isa_ok $a, Math::BigInt, "It's still a BigInt";
$a--;
is +$a, 21, "Decrement works";
isa_ok $a, Math::BigInt, "It's still a BigInt";
}

done;

0 comments on commit 2cfe839

Please sign in to comment.