File: BrickMathCalculatorTest.php

package info (click to toggle)
php-ramsey-uuid 4.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,124 kB
  • sloc: php: 13,359; xml: 194; python: 54; makefile: 16
file content (116 lines) | stat: -rw-r--r-- 3,289 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php

declare(strict_types=1);

namespace Ramsey\Uuid\Test\Math;

use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Math\BrickMathCalculator;
use Ramsey\Uuid\Math\RoundingMode;
use Ramsey\Uuid\Test\TestCase;
use Ramsey\Uuid\Type\Hexadecimal;
use Ramsey\Uuid\Type\Integer as IntegerObject;

class BrickMathCalculatorTest extends TestCase
{
    public function testAdd(): void
    {
        $int1 = new IntegerObject(5);
        $int2 = new IntegerObject(6);
        $int3 = new IntegerObject(7);

        $calculator = new BrickMathCalculator();

        $result = $calculator->add($int1, $int2, $int3);

        $this->assertSame('18', $result->toString());
    }

    public function testSubtract(): void
    {
        $int1 = new IntegerObject(5);
        $int2 = new IntegerObject(6);
        $int3 = new IntegerObject(7);

        $calculator = new BrickMathCalculator();

        $result = $calculator->subtract($int1, $int2, $int3);

        $this->assertSame('-8', $result->toString());
    }

    public function testMultiply(): void
    {
        $int1 = new IntegerObject(5);
        $int2 = new IntegerObject(6);
        $int3 = new IntegerObject(7);

        $calculator = new BrickMathCalculator();

        $result = $calculator->multiply($int1, $int2, $int3);

        $this->assertSame('210', $result->toString());
    }

    public function testDivide(): void
    {
        $int1 = new IntegerObject(1023);
        $int2 = new IntegerObject(6);
        $int3 = new IntegerObject(7);

        $calculator = new BrickMathCalculator();

        $result = $calculator->divide(RoundingMode::HALF_UP, 0, $int1, $int2, $int3);

        $this->assertSame('24', $result->toString());
    }

    public function testFromBase(): void
    {
        $calculator = new BrickMathCalculator();

        $result = $calculator->fromBase('ffffffffffffffffffff', 16);

        $this->assertInstanceOf(IntegerObject::class, $result);
        $this->assertSame('1208925819614629174706175', $result->toString());
    }

    public function testToBase(): void
    {
        $intValue = new IntegerObject('1208925819614629174706175');
        $calculator = new BrickMathCalculator();

        $this->assertSame('ffffffffffffffffffff', $calculator->toBase($intValue, 16));
    }

    public function testToHexadecimal(): void
    {
        $intValue = new IntegerObject('1208925819614629174706175');
        $calculator = new BrickMathCalculator();

        $result = $calculator->toHexadecimal($intValue);

        $this->assertInstanceOf(Hexadecimal::class, $result);
        $this->assertSame('ffffffffffffffffffff', $result->toString());
    }

    public function testFromBaseThrowsException(): void
    {
        $calculator = new BrickMathCalculator();

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('"o" is not a valid character in base 16');

        $calculator->fromBase('foobar', 16);
    }

    public function testToBaseThrowsException(): void
    {
        $calculator = new BrickMathCalculator();

        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('Base 1024 is out of range [2, 36]');

        $calculator->toBase(new IntegerObject(42), 1024);
    }
}