IntegerTests::testIsEven()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Domain\System\Tests\Units;
12
13
use Cubiche\Domain\System\Integer;
14
use Cubiche\Domain\System\Real;
15
16
/**
17
 * IntegerTests class.
18
 *
19
 * @author Ivannis Suárez Jerez <[email protected]>
20
 */
21
class IntegerTests extends NumberTestCase
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    protected function randomNativeNumber()
27
    {
28
        return rand(1, 10);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    protected function uniqueNativeNumber()
35
    {
36
        return 20;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    protected function negativeNativeNumber()
43
    {
44
        return rand(-1, -10);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    protected function invalidNativeNumber()
51
    {
52
        return 'text';
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    protected function fromNative($value)
59
    {
60
        return Integer::fromNative($value);
61
    }
62
63
    /*
64
     * Test sqrt.
65
     */
66
    public function testSqrt()
67
    {
68
        $this
69
            ->given(
70
                $native = $this->randomNativeNumber(),
71
                $invalidScale = 2.4,
72
                $a = $this->fromNative($native)
73
            )
74
            ->when($b = $a->sqrt())
75
            ->and()
76
            ->when($c = $a->sqrt(2))
77
            ->then()
78
                ->object($b)
79
                    ->isInstanceOf(Real::class)
80
                ->variable($b->toNative())
81
                    ->isEqualTo(\sqrt($native))
82
                ->variable($c->toNative())
83
                    ->isEqualTo(\bcsqrt($native, 2))
84
                ->exception(function () use ($a, $invalidScale) {
85
                    $a->sqrt($invalidScale);
86
                })->isInstanceOf(\InvalidArgumentException::class)
87
        ;
88
    }
89
90
    /*
91
     * Test inc.
92
     */
93
    public function testInc()
94
    {
95
        $this
96
            ->given(
97
                $native = $this->randomNativeNumber(),
98
                $one = $this->fromNative(1),
99
                $a = $this->fromNative($native)
100
            )
101
            ->when($b = $a->inc())
102
            ->then
103
                ->boolean($b->equals($a->add($one)))
104
                ->isTrue()
105
        ;
106
    }
107
108
    /*
109
     * Test dec.
110
     */
111
    public function testDec()
112
    {
113
        $this
114
            ->given(
115
                $native = $this->randomNativeNumber(),
116
                $one = $this->fromNative(1),
117
                $a = $this->fromNative($native)
118
            )
119
            ->when($b = $a->dec())
120
            ->then
121
                ->boolean($b->equals($a->sub($one)))
122
                ->isTrue()
123
        ;
124
    }
125
126
    /*
127
     * Test mod.
128
     */
129
    public function testMod()
130
    {
131
        $this
132
            ->given(
133
                $native1 = $this->randomNativeNumber(),
134
                $native2 = $this->randomNativeNumber(),
135
                $number1 = $this->fromNative($native1),
136
                $number2 = $this->fromNative($native2)
137
            )
138
            ->when($c = $number1->mod($number2))
139
            ->then
140
                ->variable($c->toNative())
141
                    ->isEqualTo($native1 % $native2)
142
        ;
143
    }
144
145
    /*
146
     * Test isEven.
147
     */
148 View Code Duplication
    public function testIsEven()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
149
    {
150
        $this
151
            ->given(
152
                $number1 = $this->fromNative(2),
153
                $number2 = $this->fromNative(3)
154
            )
155
            ->then
156
                ->boolean($number1->isEven())
157
                    ->isTrue()
158
                ->boolean($number2->isEven())
159
                    ->isFalse()
160
        ;
161
    }
162
163
    /*
164
     * Test isOdd.
165
     */
166 View Code Duplication
    public function testIsOdd()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
167
    {
168
        $this
169
            ->given(
170
                $number1 = $this->fromNative(2),
171
                $number2 = $this->fromNative(3)
172
            )
173
            ->then
174
                ->boolean($number1->isOdd())
175
                    ->isFalse()
176
                ->boolean($number2->isOdd())
177
                    ->isTrue()
178
        ;
179
    }
180
}
181