Skip to content
This repository was archived by the owner on Dec 27, 2023. It is now read-only.

Commit ab3983b

Browse files
committed
Added tests on InfiniteDecimal, fixed bug in InfiniteDecimal::asFloat
1 parent 29a567f commit ab3983b

13 files changed

+214
-98
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
composer.lock
22
vendor
3-
vendor/
4-
vendor/*
3+
.idea
4+
coverage_report

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"require": {
1818
"php": ">=5.3.0",
1919
"ext-bcmath": "*",
20-
"litipk/php-exceptions": ">=0.1.2"
20+
"litipk/php-exceptions": ">=0.1.3"
2121
},
2222
"require-dev": {
2323
"phpunit/phpunit": "4.5.*",

src/Decimal.php

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -754,6 +754,45 @@ public function hasSameSign(Decimal $b) {
754754
return $this->isPositive() && $b->isPositive() || $this->isNegative() && $b->isNegative();
755755
}
756756

757+
/**
758+
* Return value as a float
759+
*
760+
* @return float
761+
*/
762+
public function asFloat()
763+
{
764+
return floatval($this->value);
765+
}
766+
767+
/**
768+
* Return value as a integer
769+
*
770+
* @return float
771+
*/
772+
public function asInteger()
773+
{
774+
return intval($this->value);
775+
}
776+
777+
/**
778+
* Return the inner representation of the class
779+
* use with caution
780+
*
781+
* @return number
782+
*/
783+
public function _innerValue()
784+
{
785+
return $this->value;
786+
}
787+
788+
/**
789+
* @return string
790+
*/
791+
public function __toString()
792+
{
793+
return $this->value;
794+
}
795+
757796
/**
758797
* "Rounds" the decimal string to have at most $scale digits after the point
759798
*
@@ -938,43 +977,4 @@ private static function countSignificativeDigits(Decimal $val, Decimal $abs)
938977
($abs->comp($one) === -1) ? 2 : max($val->scale, 1)
939978
) - ($val->isNegative() ? 1 : 0);
940979
}
941-
942-
/**
943-
* Return value as a float
944-
*
945-
* @return float
946-
*/
947-
public function asFloat()
948-
{
949-
return floatval($this->value);
950-
}
951-
952-
/**
953-
* Return value as a integer
954-
*
955-
* @return float
956-
*/
957-
public function asInteger()
958-
{
959-
return intval($this->value);
960-
}
961-
962-
/**
963-
* Return the inner representation of the class
964-
* use with caution
965-
*
966-
* @return number
967-
*/
968-
public function _innerValue()
969-
{
970-
return $this->value;
971-
}
972-
973-
/**
974-
* @return string
975-
*/
976-
public function __toString()
977-
{
978-
return $this->value;
979-
}
980980
}

src/InfiniteDecimal.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Litipk\BigNumbers;
44

55
use Litipk\BigNumbers\Decimal as Decimal;
6+
use Litipk\Exceptions\InvalidCastException;
67

7-
/**
8+
/**
89
* Immutable object that represents an infinite number
910
*
1011
* @author Andreu Correa Casablanca <[email protected]>
@@ -287,4 +288,24 @@ public function isInfinite()
287288
{
288289
return true;
289290
}
291+
292+
/**
293+
* Return value as a float
294+
*
295+
* @return float
296+
*/
297+
public function asFloat()
298+
{
299+
return ($this === self::$pInf) ? INF : -INF;
300+
}
301+
302+
/**
303+
* Return value as a integer
304+
*
305+
* @return float
306+
*/
307+
public function asInteger()
308+
{
309+
throw new InvalidCastException("InfiniteDecimal", "int", "PHP integers can't represent infinite values.");
310+
}
290311
}

tests/Decimal/DecimalAbsTest.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,10 @@
88

99
class DecimalAbsTest extends PHPUnit_Framework_TestCase
1010
{
11-
public function testFiniteAbs()
11+
public function testAbs()
1212
{
1313
$this->assertTrue(Decimal::fromInteger(0)->abs()->equals(Decimal::fromInteger(0)));
1414
$this->assertTrue(Decimal::fromInteger(5)->abs()->equals(Decimal::fromInteger(5)));
1515
$this->assertTrue(Decimal::fromInteger(-5)->abs()->equals(Decimal::fromInteger(5)));
1616
}
17-
18-
public function testInfiniteAbs()
19-
{
20-
$this->assertTrue(
21-
Decimal::getPositiveInfinite()->abs()->equals(Decimal::getPositiveInfinite())
22-
);
23-
24-
$this->assertTrue(
25-
Decimal::getNegativeInfinite()->abs()->equals(Decimal::getPositiveInfinite())
26-
);
27-
}
2817
}

tests/Decimal/DecimalAddTest.php

Lines changed: 14 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,58 +17,30 @@ public function testZeroAdd()
1717
$this->assertTrue($n->add($z)->equals($n));
1818
}
1919

20-
public function testFiniteInfiniteAdd()
20+
public function testPositivePositiveDecimalAdd()
2121
{
22-
$pInf = Decimal::getPositiveInfinite();
23-
$nInf = Decimal::getNegativeInfinite();
24-
25-
$pTen = Decimal::fromInteger(10);
26-
$nTen = Decimal::fromInteger(-10);
27-
28-
$this->assertTrue($pInf->add($pTen)->equals($pInf));
29-
$this->assertTrue($nInf->add($pTen)->equals($nInf));
30-
31-
$this->assertTrue($pInf->add($nTen)->equals($pInf));
32-
$this->assertTrue($nInf->add($nTen)->equals($nInf));
33-
34-
$this->assertTrue($pTen->add($pInf)->equals($pInf));
35-
$this->assertTrue($pTen->add($nInf)->equals($nInf));
22+
$n1 = Decimal::fromString('3.45');
23+
$n2 = Decimal::fromString('7.67');
3624

37-
$this->assertTrue($nTen->add($pInf)->equals($pInf));
38-
$this->assertTrue($nTen->add($nInf)->equals($nInf));
25+
$this->assertTrue($n1->add($n2)->equals(Decimal::fromString('11.12')));
26+
$this->assertTrue($n2->add($n1)->equals(Decimal::fromString('11.12')));
3927
}
4028

41-
public function testInfiniteInfiniteAdd()
29+
public function testNegativenegativeDecimalAdd()
4230
{
43-
$pInf = Decimal::getPositiveInfinite();
44-
$nInf = Decimal::getNegativeInfinite();
45-
46-
$this->assertTrue($pInf->add($pInf)->equals($pInf));
47-
$this->assertTrue($nInf->add($nInf)->equals($nInf));
31+
$n1 = Decimal::fromString('-3.45');
32+
$n2 = Decimal::fromString('-7.67');
4833

49-
$catched = false;
50-
try {
51-
$pInf->add($nInf);
52-
} catch (\DomainException $e) {
53-
$catched = true;
54-
}
55-
$this->assertTrue($catched);
56-
57-
$catched = false;
58-
try {
59-
$nInf->add($pInf);
60-
} catch (\DomainException $e) {
61-
$catched = true;
62-
}
63-
$this->assertTrue($catched);
34+
$this->assertTrue($n1->add($n2)->equals(Decimal::fromString('-11.12')));
35+
$this->assertTrue($n2->add($n1)->equals(Decimal::fromString('-11.12')));
6436
}
6537

66-
public function testPositivePositiveDecimalAdd()
38+
public function testPositiveNegativeDecimalAdd()
6739
{
6840
$n1 = Decimal::fromString('3.45');
69-
$n2 = Decimal::fromString('7.67');
41+
$n2 = Decimal::fromString('-7.67');
7042

71-
$this->assertTrue($n1->add($n2)->equals(Decimal::fromString('11.12')));
72-
$this->assertTrue($n2->add($n1)->equals(Decimal::fromString('11.12')));
43+
$this->assertTrue($n1->add($n2)->equals(Decimal::fromString('-4.22')));
44+
$this->assertTrue($n2->add($n1)->equals(Decimal::fromString('-4.22')));
7345
}
7446
}

tests/Decimal/DecimalAsFloatTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
class DecimalAsFloatTest extends PHPUnit_Framework_TestCase
1010
{
11-
public function testFloat()
11+
public function testAsFloat()
1212
{
1313
$this->assertEquals(1.0, Decimal::fromString('1.0')->asFloat());
1414
$this->assertTrue(is_float(Decimal::fromString('1.0')->asFloat()));

tests/Decimal/DecimalCreateTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public function testCreateFromFloat()
4343
$this->assertTrue(Decimal::create(-35.125)->equals(Decimal::fromFloat(-35.125)));
4444
$this->assertTrue(Decimal::create(0.0)->equals(Decimal::fromFloat(0.0)));
4545
$this->assertTrue(Decimal::create(35.125)->equals(Decimal::fromFloat(35.125)));
46+
47+
$this->assertTrue(Decimal::create(INF)->equals(Decimal::getPositiveInfinite()));
48+
$this->assertTrue(Decimal::create(-INF)->equals(Decimal::getNegativeInfinite()));
4649
}
4750

4851
public function testCreateFromString()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\Decimal as Decimal;
4+
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
5+
6+
7+
date_default_timezone_set('UTC');
8+
9+
10+
class DecimalGetInfiniteTest extends PHPUnit_Framework_TestCase
11+
{
12+
public function testGetInfinite()
13+
{
14+
$this->assertTrue(Decimal::getPositiveInfinite()->equals(InfiniteDecimal::getPositiveInfinite()));
15+
$this->assertTrue(Decimal::getNegativeInfinite()->equals(InfiniteDecimal::getNegativeInfinite()));
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
4+
5+
6+
date_default_timezone_set('UTC');
7+
8+
9+
class InfiniteDecimalAbsTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function testAbs()
12+
{
13+
$this->assertTrue(
14+
InfiniteDecimal::getPositiveInfinite()->abs()->equals(InfiniteDecimal::getPositiveInfinite())
15+
);
16+
17+
$this->assertTrue(
18+
InfiniteDecimal::getNegativeInfinite()->abs()->equals(InfiniteDecimal::getPositiveInfinite())
19+
);
20+
}
21+
}

0 commit comments

Comments
 (0)