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

Commit 3d1d245

Browse files
committed
Added and moved tests related to InfiniteDecimal
1 parent 3100787 commit 3d1d245

9 files changed

+197
-100
lines changed

src/InfiniteDecimal.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,16 +260,16 @@ public function floor($scale = 0)
260260
public function sin($scale = null)
261261
{
262262
throw new \DomainException(($this === self::$pInf) ?
263-
"Sinus function hasn't limit in the positive infinite." :
264-
"Sinus function hasn't limit in the negative infinite."
263+
"Sine function hasn't limit in the positive infinite." :
264+
"Sine function hasn't limit in the negative infinite."
265265
);
266266
}
267267

268268
public function cos($scale = null)
269269
{
270270
throw new \DomainException(($this === self::$pInf) ?
271-
"Cosinus function hasn't limit in the positive infinite." :
272-
"Cosinus function hasn't limit in the negative infinite."
271+
"Cosine function hasn't limit in the positive infinite." :
272+
"Cosine function hasn't limit in the negative infinite."
273273
);
274274
}
275275

tests/Decimal/DecimalDivTest.php

Lines changed: 0 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,6 @@ public function testZeroFiniteDiv()
2424
$this->assertTrue($zero->div($one)->equals($zero));
2525
}
2626

27-
public function testZeroInfiniteDiv()
28-
{
29-
$pInf = Decimal::getPositiveInfinite();
30-
$nInf = Decimal::getNegativeInfinite();
31-
$zero = Decimal::fromInteger(0);
32-
33-
$catched = false;
34-
try {
35-
$pInf->div($zero);
36-
} catch (\DomainException $e) {
37-
$catched = true;
38-
}
39-
$this->assertTrue($catched);
40-
41-
$catched = false;
42-
try {
43-
$nInf->div($zero);
44-
} catch (\DomainException $e) {
45-
$catched = true;
46-
}
47-
$this->assertTrue($catched);
48-
}
49-
5027
public function testOneDiv()
5128
{
5229
$one = Decimal::fromInteger(1);
@@ -55,65 +32,6 @@ public function testOneDiv()
5532
$this->assertTrue($two->div($one)->equals($two));
5633
}
5734

58-
public function testFiniteInfiniteDiv()
59-
{
60-
$pTen = Decimal::fromInteger(10);
61-
$nTen = Decimal::fromInteger(-10);
62-
63-
$pInf = Decimal::getPositiveInfinite();
64-
$nInf = Decimal::getNegativeInfinite();
65-
66-
$this->assertTrue($pInf->div($pTen)->equals($pInf));
67-
$this->assertTrue($pInf->div($nTen)->equals($nInf));
68-
69-
$this->assertTrue($nInf->div($pTen)->equals($nInf));
70-
$this->assertTrue($nInf->div($nTen)->equals($pInf));
71-
72-
$this->assertTrue($pTen->div($pInf)->isZero());
73-
$this->assertTrue($nTen->div($pInf)->isZero());
74-
75-
$this->assertTrue($pTen->div($nInf)->isZero());
76-
$this->assertTrue($nTen->div($nInf)->isZero());
77-
}
78-
79-
public function testInfiniteInfiniteDiv()
80-
{
81-
$pInf = Decimal::getPositiveInfinite();
82-
$nInf = Decimal::getNegativeInfinite();
83-
84-
$catched = false;
85-
try {
86-
$pInf->div($pInf);
87-
} catch (\DomainException $e) {
88-
$catched = true;
89-
}
90-
$this->assertTrue($catched);
91-
92-
$catched = false;
93-
try {
94-
$pInf->div($nInf);
95-
} catch (\DomainException $e) {
96-
$catched = true;
97-
}
98-
$this->assertTrue($catched);
99-
100-
$catched = false;
101-
try {
102-
$nInf->div($pInf);
103-
} catch (\DomainException $e) {
104-
$catched = true;
105-
}
106-
$this->assertTrue($catched);
107-
108-
$catched = false;
109-
try {
110-
$nInf->div($nInf);
111-
} catch (\DomainException $e) {
112-
$catched = true;
113-
}
114-
$this->assertTrue($catched);
115-
}
116-
11735
public function testBasicDiv()
11836
{
11937
$one = Decimal::fromInteger(1);

tests/Decimal/DecimalEqualsTest.php

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,55 @@
88

99
class DecimalEqualsTest extends PHPUnit_Framework_TestCase
1010
{
11-
public function testNotEquals()
11+
public function testSimpleEquals()
1212
{
13-
$this->assertTrue(!Decimal::fromInteger(1)->equals(Decimal::fromInteger(2)));
13+
# Transitivity & inter-types constructors compatibility
14+
$this->assertTrue(Decimal::fromInteger(1)->equals(Decimal::fromString("1")));
15+
$this->assertTrue(Decimal::fromString("1")->equals(Decimal::fromFloat(1.0)));
16+
$this->assertTrue(Decimal::fromInteger(1)->equals(Decimal::fromFloat(1.0)));
1417

15-
$this->assertTrue(!Decimal::fromInteger(1)->equals(Decimal::getPositiveInfinite()));
16-
$this->assertTrue(!Decimal::fromInteger(1)->equals(Decimal::getNegativeInfinite()));
18+
# Reflexivity
19+
$this->assertTrue(Decimal::fromInteger(1)->equals(Decimal::fromInteger(1)));
1720

18-
$this->assertTrue(!Decimal::getPositiveInfinite()->equals(Decimal::fromInteger(1)));
19-
$this->assertTrue(!Decimal::getNegativeInfinite()->equals(Decimal::fromInteger(1)));
21+
# Symmetry
22+
$this->assertTrue(Decimal::fromString("1")->equals(Decimal::fromInteger(1)));
23+
$this->assertTrue(Decimal::fromFloat(1.0)->equals(Decimal::fromString("1")));
24+
$this->assertTrue(Decimal::fromFloat(1.0)->equals(Decimal::fromInteger(1)));
25+
}
26+
27+
public function testSimpleNotEquals()
28+
{
29+
# Symmetry
30+
$this->assertFalse(Decimal::fromInteger(1)->equals(Decimal::fromInteger(2)));
31+
$this->assertFalse(Decimal::fromInteger(2)->equals(Decimal::fromInteger(1)));
32+
33+
$this->assertFalse(Decimal::fromFloat(1.01)->equals(Decimal::fromInteger(1)));
34+
$this->assertFalse(Decimal::fromInteger(1)->equals(Decimal::fromFloat(1.01)));
35+
}
36+
37+
public function testScaledEquals()
38+
{
39+
# Transitivity
40+
$this->assertTrue(Decimal::fromFloat(1.001)->equals(Decimal::fromFloat(1.01), 1));
41+
$this->assertTrue(Decimal::fromFloat(1.01)->equals(Decimal::fromFloat(1.004), 1));
42+
$this->assertTrue(Decimal::fromFloat(1.001)->equals(Decimal::fromFloat(1.004), 1));
43+
44+
# Reflexivity
45+
$this->assertTrue(Decimal::fromFloat(1.00525)->equals(Decimal::fromFloat(1.00525), 2));
46+
47+
# Symmetry
48+
$this->assertTrue(Decimal::fromFloat(1.01)->equals(Decimal::fromFloat(1.001), 1));
49+
$this->assertTrue(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.01), 1));
50+
$this->assertTrue(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.001), 1));
51+
52+
# Proper rounding
53+
$this->assertTrue(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.000), 2));
54+
$this->assertTrue(Decimal::fromFloat(1.005)->equals(Decimal::fromFloat(1.010), 2));
55+
}
56+
57+
public function testScaledNotEquals()
58+
{
59+
# Proper rounding
60+
$this->assertFalse(Decimal::fromFloat(1.004)->equals(Decimal::fromFloat(1.005), 2));
2061
}
2162
}

tests/Decimal/DecimalInnerValueTest.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ class DecimalInnerValueTest extends PHPUnit_Framework_TestCase
1010
{
1111
public function testInnerValue()
1212
{
13-
for($i=0;$i<100;$i++)
14-
{
15-
$this->assertEquals($i, Decimal::fromInteger($i)->_innerValue());
16-
}
13+
# We cannot make assumptions on the inner value!
14+
Decimal::fromInteger(3)->_innerValue();
1715
}
1816
}

tests/InfiniteDecimal/InfiniteDecimalCosTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class InfiniteDecimalCosTest extends PHPUnit_Framework_TestCase
99
{
1010
/**
1111
* @expectedException \DomainException
12-
* @expectedExceptionMessage Cosinus function hasn't limit in the positive infinite.
12+
* @expectedExceptionMessage Cosine function hasn't limit in the positive infinite.
1313
*/
1414
public function testFinitePositiveInfiniteCos()
1515
{
@@ -18,7 +18,7 @@ public function testFinitePositiveInfiniteCos()
1818

1919
/**
2020
* @expectedException \DomainException
21-
* @expectedExceptionMessage Cosinus function hasn't limit in the negative infinite.
21+
* @expectedExceptionMessage Cosine function hasn't limit in the negative infinite.
2222
*/
2323
public function testFiniteNegativeInfiniteCos()
2424
{
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
4+
use Litipk\BigNumbers\Decimal as Decimal;
5+
6+
7+
date_default_timezone_set('UTC');
8+
9+
10+
class InfiniteDecimalDivTest extends PHPUnit_Framework_TestCase
11+
{
12+
public function testZeroInfiniteDiv()
13+
{
14+
$pInf = InfiniteDecimal::getPositiveInfinite();
15+
$nInf = InfiniteDecimal::getNegativeInfinite();
16+
$zero = Decimal::fromInteger(0);
17+
18+
$catched = false;
19+
try {
20+
$pInf->div($zero);
21+
} catch (\DomainException $e) {
22+
$catched = true;
23+
}
24+
$this->assertTrue($catched);
25+
26+
$catched = false;
27+
try {
28+
$nInf->div($zero);
29+
} catch (\DomainException $e) {
30+
$catched = true;
31+
}
32+
$this->assertTrue($catched);
33+
}
34+
35+
public function testFiniteInfiniteDiv()
36+
{
37+
$pTen = Decimal::fromInteger(10);
38+
$nTen = Decimal::fromInteger(-10);
39+
40+
$pInf = InfiniteDecimal::getPositiveInfinite();
41+
$nInf = InfiniteDecimal::getNegativeInfinite();
42+
43+
$this->assertTrue($pInf->div($pTen)->equals($pInf));
44+
$this->assertTrue($pInf->div($nTen)->equals($nInf));
45+
46+
$this->assertTrue($nInf->div($pTen)->equals($nInf));
47+
$this->assertTrue($nInf->div($nTen)->equals($pInf));
48+
49+
$this->assertTrue($pTen->div($pInf)->isZero());
50+
$this->assertTrue($nTen->div($pInf)->isZero());
51+
52+
$this->assertTrue($pTen->div($nInf)->isZero());
53+
$this->assertTrue($nTen->div($nInf)->isZero());
54+
}
55+
56+
public function testInfiniteInfiniteDiv()
57+
{
58+
$pInf = InfiniteDecimal::getPositiveInfinite();
59+
$nInf = InfiniteDecimal::getNegativeInfinite();
60+
61+
$catched = false;
62+
try {
63+
$pInf->div($pInf);
64+
} catch (\DomainException $e) {
65+
$catched = true;
66+
}
67+
$this->assertTrue($catched);
68+
69+
$catched = false;
70+
try {
71+
$pInf->div($nInf);
72+
} catch (\DomainException $e) {
73+
$catched = true;
74+
}
75+
$this->assertTrue($catched);
76+
77+
$catched = false;
78+
try {
79+
$nInf->div($pInf);
80+
} catch (\DomainException $e) {
81+
$catched = true;
82+
}
83+
$this->assertTrue($catched);
84+
85+
$catched = false;
86+
try {
87+
$nInf->div($nInf);
88+
} catch (\DomainException $e) {
89+
$catched = true;
90+
}
91+
$this->assertTrue($catched);
92+
}
93+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
4+
5+
6+
date_default_timezone_set('UTC');
7+
8+
9+
class InfiniteDecimalEqualsTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function testEquals()
12+
{
13+
$this->assertTrue(InfiniteDecimal::getPositiveInfinite()->equals(InfiniteDecimal::getPositiveInfinite()));
14+
$this->assertTrue(InfiniteDecimal::getNegativeInfinite()->equals(InfiniteDecimal::getNegativeInfinite()));
15+
16+
$this->assertFalse(InfiniteDecimal::getPositiveInfinite()->equals(InfiniteDecimal::getNegativeInfinite()));
17+
$this->assertFalse(InfiniteDecimal::getNegativeInfinite()->equals(InfiniteDecimal::getPositiveInfinite()));
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Litipk\BigNumbers\InfiniteDecimal as InfiniteDecimal;
4+
5+
6+
date_default_timezone_set('UTC');
7+
8+
9+
class InfiniteDecimalPropertiesTest extends PHPUnit_Framework_TestCase
10+
{
11+
public function testIsZero()
12+
{
13+
$this->assertFalse(InfiniteDecimal::getPositiveInfinite()->isZero());
14+
$this->assertFalse(InfiniteDecimal::getNegativeInfinite()->isZero());
15+
}
16+
17+
public function testIsPositive()
18+
{
19+
$this->assertTrue(InfiniteDecimal::getPositiveInfinite()->isPositive());
20+
$this->assertFalse(InfiniteDecimal::getNegativeInfinite()->isPositive());
21+
}
22+
23+
public function testIsNegative()
24+
{
25+
$this->assertFalse(InfiniteDecimal::getPositiveInfinite()->isNegative());
26+
$this->assertTrue(InfiniteDecimal::getNegativeInfinite()->isNegative());
27+
}
28+
}

tests/InfiniteDecimal/InfiniteDecimalSinTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class InfiniteDecimalSinTest extends PHPUnit_Framework_TestCase
99
{
1010
/**
1111
* @expectedException \DomainException
12-
* @expectedExceptionMessage Sinus function hasn't limit in the positive infinite.
12+
* @expectedExceptionMessage Sine function hasn't limit in the positive infinite.
1313
*/
1414
public function testFinitePositiveInfiniteSin()
1515
{
@@ -18,7 +18,7 @@ public function testFinitePositiveInfiniteSin()
1818

1919
/**
2020
* @expectedException \DomainException
21-
* @expectedExceptionMessage Sinus function hasn't limit in the negative infinite.
21+
* @expectedExceptionMessage Sine function hasn't limit in the negative infinite.
2222
*/
2323
public function testFiniteNegativeInfiniteSin()
2424
{

0 commit comments

Comments
 (0)