Skip to content

Commit

Permalink
Unit test coverage, fixed bug with BCMATH driver
Browse files Browse the repository at this point in the history
  • Loading branch information
CloCkWeRX committed Oct 2, 2010
1 parent d0c14e3 commit f7f751f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Math/Integer/bcmath.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function fact() {/*{{{*/
$fact = 1; $fact = 1;
$val = $this->getValue(); $val = $this->getValue();
while (bccomp($val, 1) != 0) { while (bccomp($val, 1) != 0) {
$fact = bcmul($fac, $val); $fact = bcmul($fact, $val);
$val = bcsub($val, 1); $val = bcsub($val, 1);
} }
$this->setValue($fact); $this->setValue($fact);
Expand Down
123 changes: 71 additions & 52 deletions tests/Math_IntegerTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -9,43 +9,58 @@ public function setUp() {
if (!extension_loaded('gmp')) { if (!extension_loaded('gmp')) {
$this->markTestSkipped( "Missing gmp extension"); $this->markTestSkipped( "Missing gmp extension");
} }

$this->int = Math_Integer::create('22331242343213222231423234234234234234234111232123.1', MATH_INTEGER_GMP, true);
$this->hint = Math_Integer::create('efaaffadadfadad11234dfaed9002123346678878', MATH_INTEGER_GMP, true);
}

public function testToIntegerString1() {
$this->assertSame("5124168876805615", Math_Integer::_toIntegerString('012346789abcdef'));
}

public function testToIntegerString2() {
$this->assertSame("21892195098298702144417118276090206103627679369336", Math_Integer::_toIntegerString('efaaffadadfadad11234dfaed9002123346678878'));
}

public function testToIntegerString3() {
$this->assertSame('22331242343213222231423234234234234234234111232123', Math_Integer::_toIntegerString('22331242343213222231423234234234234234234111232123.0000000', MATH_INTEGER_AUTO, true));

}

public function testToIntegerString4() {
$result = Math_Integer::_toIntegerString('22331242343213222231423234234234234234234111232123.1');

$this->assertTrue(PEAR::isError($result));
}

public function testToIntegerString5() {
$this->assertSame('22331242343213222231423234234234234234234111232123', Math_Integer::_toIntegerString('22331242343213222231423234234234234234234111232123.1', MATH_INTEGER_AUTO, true));
}

public function testToString1() {
$this->assertSame('22331242343213222231423234234234234234234111232123', $this->int->toString());
}

public function testToString2() {
$this->assertSame('21892195098298702144417118276090206103627679369336', $this->hint->toString());
}

public function testAdd1() {
$this->int->add($this->hint);

$this->assertSame("44223437441511924375840352510324440337861790601459", $this->int->toString(), "Failed to correctly add");
}

public function testAdd2() {
$result = $this->int->add($foo = "1234");

$this->assertTrue(PEAR::isError($result));
} }




public function test() { public function testGcd() {
// Old test coverage - who knows what the expected results are.
$h = '012346789abcdef';
echo "INPUT: $h\n";
echo Math_Integer::_toIntegerString($h)."\n\n";

$h = 'efaaffadadfadad11234dfaed9002123346678878';
echo "INPUT: $h\n";
echo Math_Integer::_toIntegerString($h)."\n\n";

$f = '22331242343213222231423234234234234234234111232123.0000000';
echo "INPUT: $f\n";
echo Math_Integer::_toIntegerString($f, MATH_INTEGER_AUTO, true)."\n\n";

$f = '22331242343213222231423234234234234234234111232123.1';
echo "INPUT: $f\n";
print_r(Math_Integer::_toIntegerString($f));
echo Math_Integer::_toIntegerString($f, MATH_INTEGER_AUTO, true)."\n";

$int = Math_Integer::create($f, MATH_INTEGER_GMP, true);
$hint = Math_Integer::create($h, MATH_INTEGER_GMP, true);
print_r($int);
echo 'Math_Integer_GMP: '.$int->toString()."\n";
print_r($hint);
echo 'Math_Integer_GMP: '.$hint->toString()."\n";
$foo = $int->add($hint);
var_dump($foo);
echo 'Adding int to hint: '.$int->toString()."\n";
$bad = 1234;
$foo = $int->add($bad);
var_dump($foo);

$gcd = '12341123342312422313245'; $gcd = '12341123342312422313245';
echo "Fixed gcd: $gcd\n";
$tmp1 = Math_Integer::create($gcd); $tmp1 = Math_Integer::create($gcd);
$tmp2 = $tmp1->makeClone(); $tmp2 = $tmp1->makeClone();
$tmp1->mul(Math_Integer::create(3)); $tmp1->mul(Math_Integer::create(3));
Expand All @@ -55,37 +70,41 @@ public function test() {


$i1 = Math_Integer::create($v1, MATH_INTEGER_GMP); $i1 = Math_Integer::create($v1, MATH_INTEGER_GMP);
$i2 = Math_Integer::create($v2, MATH_INTEGER_GMP); $i2 = Math_Integer::create($v2, MATH_INTEGER_GMP);

$i3 = $i1->gcd($i2); $i3 = $i1->gcd($i2);
echo "GMP gcd($v1, $v2): ".$i3->toString()."\n"; $this->assertSame("12341123342312422313245", $i3->toString(), "GMP gcd($v1, $v2)");

$i3 = $i2->gcd($i1); $i3 = $i2->gcd($i1);
echo "GMP gcd($v2, $v1): ".$i3->toString()."\n"; $this->assertSame("12341123342312422313245", $i3->toString(), "GMP gcd($v2, $v1)");



$i1 = Math_Integer::create($v1, MATH_INTEGER_BCMATH); $i1 = Math_Integer::create($v1, MATH_INTEGER_BCMATH);
$i2 = Math_Integer::create($v2, MATH_INTEGER_BCMATH); $i2 = Math_Integer::create($v2, MATH_INTEGER_BCMATH);
$i3 = $i1->gcd($i2); $i3 = $i1->gcd($i2);
echo "BCMATH gcd($v1, $v2): ".$i3->toString()."\n"; $this->assertSame("12341123342312422313245", $i3->toString(), "BCMATH gcd($v1, $v2)");
$i3 = $i2->gcd($i1); $i3 = $i2->gcd($i1);
echo "BCMATH gcd($v2, $v1): ".$i3->toString()."\n"; $this->assertSame("12341123342312422313245", $i3->toString(), "BCMATH gcd($v2, $v1)");

}

public function testFact() {


$val = '30'; $val = '30';
$i1 = Math_Integer::create($val, MATH_INTEGER_GMP); $i1 = Math_Integer::create($val, MATH_INTEGER_GMP);
echo "GMP int = ".$i1->toString()."\n"; $this->assertSame('30', $i1->toString());

$err = $i1->fact(); $err = $i1->fact();
if($err == true) { $this->assertTrue($err);
$fact1 = $i1->toString(); $fact1 = $i1->toString();
echo "GMP int! = $fact1\n"; $this->assertSame("265252859812191058636308480000000", $fact1);
} else {
print_r($err);
} $i1 = Math_Integer::create($val, MATH_INTEGER_BCMATH);
$i1 = Math_Integer::create($val, MATH_INTEGER_GMP); $this->assertSame('30', $i1->toString());
echo "BCMATH int = ".$i1->toString()."\n";
$err = $i1->fact(); $err = $i1->fact();
if($err == true) { $this->assertTrue($err);
$fact2 = $i1->toString(); $fact2 = $i1->toString();
echo "BCMATH int! = $fact2\n"; $this->assertSame("265252859812191058636308480000000", $fact2);
} else {
print_r($err);
}


$this->assertSame($fact1, $fact2, "GMP and BCMATH gave different results"); $this->assertSame($fact1, $fact2, "GMP and BCMATH gave different results");
} }
Expand Down

0 comments on commit f7f751f

Please sign in to comment.