Files
zombie-game/tests/Unit/Game/Vector2Test.php
T
2026-09-07 20:37:40 +02:00

126 lines
2.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Unit\Game;
use App\Game\Vector2;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Attributes\Test;
final class Vector2Test extends TestCase
{
#[Test]
public function addV(): void
{
$a = new Vector2(1.0, 2.0);
$b = new Vector2(3.0, -1.0);
$c = $a->addV($b);
$this->assertSame(4.0, $c->x);
$this->assertSame(1.0, $c->y);
}
#[Test]
public function subV(): void
{
$a = new Vector2(5.0, 7.0);
$b = new Vector2(2.0, 3.0);
$c = $a->subV($b);
$this->assertSame(3.0, $c->x);
$this->assertSame(4.0, $c->y);
}
#[Test]
public function mulS(): void
{
$v = new Vector2(2.0, -3.0);
$r = $v->mulS(4.0);
$this->assertSame(8.0, $r->x);
$this->assertSame(-12.0, $r->y);
}
#[Test]
public function addS(): void
{
$v = new Vector2(2.0, 3.0);
$r = $v->addS(5.0);
$this->assertSame(7.0, $r->x);
$this->assertSame(8.0, $r->y);
}
#[Test]
public function divS(): void
{
$v = new Vector2(6.0, -4.0);
$r = $v->divS(2.0);
$this->assertSame(3.0, $r->x);
$this->assertSame(-2.0, $r->y);
}
#[Test]
public function lengthSquared(): void
{
$v = new Vector2(3.0, 4.0);
$this->assertSame(25.0, $v->lengthSquared());
}
#[Test]
public function length(): void
{
$v = new Vector2(3.0, 4.0);
$this->assertSame(5.0, $v->length());
}
#[Test]
public function normalizeUnitVectorUnchanged(): void
{
$v = new Vector2(3.0, 0.0);
$n = $v->normalize();
$this->assertSame(1.0, $n->x);
$this->assertSame(0.0, $n->y);
}
#[Test]
public function normalizeNormalizesVector(): void
{
$v = new Vector2(3.0, 4.0);
$n = $v->normalize();
$this->assertEqualsWithDelta(0.6, $n->x, 1e-9);
$this->assertEqualsWithDelta(0.8, $n->y, 1e-9);
}
#[Test]
public function normalizeZeroVectorReturnsZero(): void
{
$v = new Vector2(0.0, 0.0);
$n = $v->normalize();
$this->assertSame(0.0, $n->x);
$this->assertSame(0.0, $n->y);
}
#[Test]
public function zeroReturnsOrigin(): void
{
$v = Vector2::zero();
$this->assertSame(0.0, $v->x);
$this->assertSame(0.0, $v->y);
}
#[Test]
public function rotate90DegreesYAxis(): void
{
$v = new Vector2(1.0, 0.0);
$r = $v->rotate(M_PI / 2.0);
$this->assertEqualsWithDelta(0.0, $r->x, 1e-6);
$this->assertEqualsWithDelta(1.0, $r->y, 1e-6);
}
#[Test]
public function rotate360ReturnsSameDirection(): void
{
$v = new Vector2(2.0, -1.0);
$r = $v->rotate(2.0 * M_PI);
$this->assertEqualsWithDelta($v->x, $r->x, 1e-6);
$this->assertEqualsWithDelta($v->y, $r->y, 1e-6);
}
}