DecimalSqrtTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 0
cbo 2
dl 0
loc 27
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testIntegerSqrt() 0 9 1
A testNearZeroSqrt() 0 5 1
A testFiniteNegativeSqrt() 0 4 1
1
<?php
2
3
use Litipk\BigNumbers\Decimal as Decimal;
4
use PHPUnit\Framework\TestCase;
5
6
date_default_timezone_set('UTC');
7
8
class DecimalSqrtTest extends TestCase
9
{
10
    public function testIntegerSqrt()
11
    {
12
        $this->assertTrue(Decimal::fromInteger(0)->sqrt()->equals(Decimal::fromInteger(0)));
13
        $this->assertTrue(Decimal::fromInteger(1)->sqrt()->equals(Decimal::fromInteger(1)));
14
        $this->assertTrue(Decimal::fromInteger(4)->sqrt()->equals(Decimal::fromInteger(2)));
15
        $this->assertTrue(Decimal::fromInteger(9)->sqrt()->equals(Decimal::fromInteger(3)));
16
        $this->assertTrue(Decimal::fromInteger(16)->sqrt()->equals(Decimal::fromInteger(4)));
17
        $this->assertTrue(Decimal::fromInteger(25)->sqrt()->equals(Decimal::fromInteger(5)));
18
    }
19
20
    public function testNearZeroSqrt()
21
    {
22
        $this->assertTrue(Decimal::fromString('0.01')->sqrt()->equals(Decimal::fromString('0.1')));
23
        $this->assertTrue(Decimal::fromString('0.0001')->sqrt()->equals(Decimal::fromString('0.01')));
24
    }
25
26
    /**
27
     * @expectedException \DomainException
28
     * @expectedExceptionMessage Decimal can't handle square roots of negative numbers (it's only for real numbers).
29
     */
30
    public function testFiniteNegativeSqrt()
31
    {
32
        Decimal::fromInteger(-1)->sqrt();
33
    }
34
}
35