ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
BigintTest.php
Go to the documentation of this file.
1<?php
2declare(strict_types=1);
3
4namespace BigintTest;
5
6use OverflowException;
7use PHPUnit\Framework\TestCase;
9
10class BigintTest extends TestCase
11{
12 public function testConstruct(): void
13 {
14 $bigint = new Bigint(0x12345678);
15 $this->assertSame('0x0000000012345678', $bigint->getHex64());
16 $this->assertSame(0x12345678, $bigint->getLow32());
17 $this->assertSame(0, $bigint->getHigh32());
18 }
19
20 public function testConstructLarge(): void
21 {
22 $bigint = new Bigint(0x87654321);
23 $this->assertSame('0x0000000087654321', $bigint->getHex64());
24 $this->assertSame('87654321', bin2hex(pack('N', $bigint->getLow32())));
25 $this->assertSame(0, $bigint->getHigh32());
26 }
27
28 public function testAddSmallValue(): void
29 {
30 $bigint = new Bigint(1);
31 $bigint = $bigint->add(Bigint::init(2));
32 $this->assertSame(3, $bigint->getLow32());
33 $this->assertFalse($bigint->isOver32());
34 $this->assertTrue($bigint->isOver32(true));
35 $this->assertSame($bigint->getLowFF(), (float)$bigint->getLow32());
36 $this->assertSame($bigint->getLowFF(true), (float)0xFFFFFFFF);
37 }
38
39 public function testAddWithOverflowAtLowestByte(): void
40 {
41 $bigint = new Bigint(0xFF);
42 $bigint = $bigint->add(Bigint::init(0x01));
43 $this->assertSame(0x100, $bigint->getLow32());
44 }
45
46 public function testAddWithOverflowAtInteger32(): void
47 {
48 $bigint = new Bigint(0xFFFFFFFE);
49 $this->assertFalse($bigint->isOver32());
50 $bigint = $bigint->add(Bigint::init(0x01));
51 $this->assertTrue($bigint->isOver32());
52 $bigint = $bigint->add(Bigint::init(0x01));
53 $this->assertSame('0x0000000100000000', $bigint->getHex64());
54 $this->assertTrue($bigint->isOver32());
55 $this->assertSame((float)0xFFFFFFFF, $bigint->getLowFF());
56 }
57
58 public function testAddWithOverflowAtInteger64(): void
59 {
60 $bigint = Bigint::fromLowHigh(0xFFFFFFFF, 0xFFFFFFFF);
61 $this->assertSame('0xFFFFFFFFFFFFFFFF', $bigint->getHex64());
62 $this->expectException(OverflowException::class);
63 $bigint->add(Bigint::init(1));
64 }
65}
An exception for terminatinating execution or to throw for unit testing.
static fromLowHigh(int $low, int $high)
Fill bytes from low to high.
Definition: Bigint.php:59
static init(int $value=0)
Get an instance.
Definition: Bigint.php:47