Completed
Push — master ( b9f967...7a222d )
by Andreu
9s
created

DecimalSqrtTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 5
c 5
b 0
f 1
lcom 1
cbo 2
dl 0
loc 43
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testIntegerSqrt() 0 9 1
A testNearZeroSqrt() 0 5 1
A testFiniteNegativeSqrt() 0 4 1
A testPositiveInfiniteSqrt() 0 5 1
A testNegativeInfiniteSqrt() 0 5 1
1
<?php
2
3
use Litipk\BigNumbers\Decimal as Decimal;
4
5
6
date_default_timezone_set('UTC');
7
8
9
class DecimalSqrtTest extends PHPUnit_Framework_TestCase
10
{
11
    public function testIntegerSqrt()
12
    {
13
        $this->assertTrue(Decimal::fromInteger(0)->sqrt()->equals(Decimal::fromInteger(0)));
14
        $this->assertTrue(Decimal::fromInteger(1)->sqrt()->equals(Decimal::fromInteger(1)));
15
        $this->assertTrue(Decimal::fromInteger(4)->sqrt()->equals(Decimal::fromInteger(2)));
16
        $this->assertTrue(Decimal::fromInteger(9)->sqrt()->equals(Decimal::fromInteger(3)));
17
        $this->assertTrue(Decimal::fromInteger(16)->sqrt()->equals(Decimal::fromInteger(4)));
18
        $this->assertTrue(Decimal::fromInteger(25)->sqrt()->equals(Decimal::fromInteger(5)));
19
    }
20
21
    public function testNearZeroSqrt()
22
    {
23
        $this->assertTrue(Decimal::fromString('0.01')->sqrt()->equals(Decimal::fromString('0.1')));
24
        $this->assertTrue(Decimal::fromString('0.0001')->sqrt()->equals(Decimal::fromString('0.01')));
25
    }
26
27
    /**
28
     * @expectedException \DomainException
29
     * @expectedExceptionMessage Decimal can't handle square roots of negative numbers (it's only for real numbers).
30
     */
31
    public function testFiniteNegativeSqrt()
32
    {
33
        Decimal::fromInteger(-1)->sqrt();
34
    }
35
36
    public function testPositiveInfiniteSqrt()
37
    {
38
        $pInf = Decimal::getPositiveInfinite();
39
        $this->assertTrue($pInf->sqrt()->equals($pInf));
40
    }
41
42
    /**
43
     * @expectedException \DomainException
44
     * @expectedExceptionMessage Decimal can't handle square roots of negative numbers (it's only for real numbers).
45
     */
46
    public function testNegativeInfiniteSqrt()
47
    {
48
        $nInf = Decimal::getNegativeInfinite();
49
        $nInf->sqrt();
50
    }
51
}
52