diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..cb563f1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +language: php + +php: + - 7.2 + - nightly + +matrix: + allow_failures: + - php: nightly + +before_script: + - composer install --no-interaction + +script: + - ./vendor/bin/phpunit diff --git a/composer.json b/composer.json index 1c2c0ae..30ce90a 100644 --- a/composer.json +++ b/composer.json @@ -6,10 +6,18 @@ "require": { "php": ">=7.2.0", "php-64bit": "*" - }, + }, + "require-dev": { + "phpunit/phpunit": "^7.0" + }, "autoload": { "psr-4": { "pocketmine\\math\\": "src/" } - } + }, + "autoload-dev": { + "psr-4": { + "pocketmine\\math\\tests\\": "tests/" + } + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..f788c9b --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,12 @@ + + + + tests + + + + + src + + + diff --git a/tests/AxisAlignedBBTest.php b/tests/AxisAlignedBBTest.php new file mode 100644 index 0000000..5599f33 --- /dev/null +++ b/tests/AxisAlignedBBTest.php @@ -0,0 +1,253 @@ +assertInstanceOf(AxisAlignedBB::class, $axis->setBounds(1.2, 1.3, 1.4, 2.5, 2.6, 2.7)); + $this->assertSame('AxisAlignedBB(1.2, 1.3, 1.4, 2.5, 2.6, 2.7)', (string) $axis); + } + + public function addCoordProvider(){ + return [ + [-1.1, -1.2, -1.3, 'AxisAlignedBB(0, 0, 0, 2.1, 2.2, 2.3)'], + [1.1, 1.2, 1.3, 'AxisAlignedBB(1.1, 1.2, 1.3, 3.2, 3.4, 3.6)'], + ]; + } + + /** + * @dataProvider addCoordProvider + */ + public function testAddCoord($x, $y, $z, $expectedValue){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $result = $axis->addCoord($x, $y, $z); + + $this->assertInstanceOf(AxisAlignedBB::class, $result); + $this->assertSame($expectedValue, (string) $result); + } + + public function testGrow(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $result = $axis->grow(2.1, 2.2, 2.3); + + $this->assertInstanceOf(AxisAlignedBB::class, $result); + $this->assertSame('AxisAlignedBB(-1, -1, -1, 4.2, 4.4, 4.6)', (string) $result); + } + + public function testExpand(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $result = $axis->expand(2.1, 2.2, 2.3); + + $this->assertInstanceOf(AxisAlignedBB::class, $result); + $this->assertSame('AxisAlignedBB(-1, -1, -1, 4.2, 4.4, 4.6)', (string) $result); + $this->assertSame('AxisAlignedBB(-1, -1, -1, 4.2, 4.4, 4.6)', (string) $axis); + } + + public function testOffset(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $result = $axis->offset(2.1, 2.2, 2.3); + + $this->assertInstanceOf(AxisAlignedBB::class, $result); + $this->assertSame('AxisAlignedBB(3.2, 3.4, 3.6, 4.2, 4.4, 4.6)', (string) $result); + $this->assertSame('AxisAlignedBB(3.2, 3.4, 3.6, 4.2, 4.4, 4.6)', (string) $axis); + } + + public function testShrink(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $result = $axis->shrink(2.1, 2.2, 2.3); + + $this->assertInstanceOf(AxisAlignedBB::class, $result); + $this->assertSame('AxisAlignedBB(3.2, 3.4, 3.6, 0, 0, 0)', (string) $result); + } + + public function testContract(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $result = $axis->contract(2.1, 2.2, 2.3); + + $this->assertInstanceOf(AxisAlignedBB::class, $result); + $this->assertSame('AxisAlignedBB(3.2, 3.4, 3.6, 0, 0, 0)', (string) $result); + $this->assertSame('AxisAlignedBB(3.2, 3.4, 3.6, 0, 0, 0)', (string) $axis); + } + + public function testSetBB(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $bb = new AxisAlignedBB(1.0, 1.1, 1.2, 2.0, 2.1, 2.2); + $result = $axis->setBB($bb); + + $this->assertInstanceOf(AxisAlignedBB::class, $result); + $this->assertSame('AxisAlignedBB(1, 1.1, 1.2, 2, 2.1, 2.2)', (string) $result); + $this->assertSame('AxisAlignedBB(1, 1.1, 1.2, 2, 2.1, 2.2)', (string) $axis); + } + + public function testGetOffsetBoundingBox(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $result = $axis->getOffsetBoundingBox(1.1, 1.2, 1.3); + + $this->assertInstanceOf(AxisAlignedBB::class, $result); + $this->assertSame('AxisAlignedBB(2.2, 2.4, 2.6, 3.2, 3.4, 3.6)', (string) $result); + } + + public function calculateXOffsetProvider(){ + return [ + [new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3), 1.1, 1.1], + [new AxisAlignedBB(1.1, 1.2, 1.5, 2.1, 1.2, 2.3), 1.1, 1.1], + [new AxisAlignedBB(1.1, 1.2, 1.5, 2.1, 1.2, 2.4), 1.1, 1.1], + [new AxisAlignedBB(1.1, 2.1, 1.3, 2.1, 1.4, 1.3), 1.1, 1.1], + [new AxisAlignedBB(1.1, 2.0, 1.3, 1.1, 1.4, 1.5), 1.1, 0.0], + [new AxisAlignedBB(2.1, 2.0, 1.3, 1.1, 1.4, 1.5), -1.1, 0.0], + ]; + } + + /** + * @dataProvider calculateXOffsetProvider + */ + public function testCalculateXOffset($bb, $offsetX, $expectedValue){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + + $this->assertSame($expectedValue, $axis->calculateXOffset($bb, $offsetX)); + } + + public function calculateYOffsetProvider(){ + return [ + [new AxisAlignedBB(1.1, 1.2, 1.3, 1.1, 2.2, 2.3), 1.1, 1.1], + [new AxisAlignedBB(1.1, 1.2, 1.5, 2.1, 1.2, 2.3), 1.1, 0.0], + [new AxisAlignedBB(1.1, 0.1, 2.3, 3.1, 2.2, 2.3), 1.1, 1.1], + [new AxisAlignedBB(1.1, 2.2, 0.3, 3.1, 2.2, 1.4), -1.1, 0.0], + ]; + } + + /** + * @dataProvider calculateYOffsetProvider + */ + public function testCalculateYOffset($bb, $offsetY, $expectedValue){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + + $this->assertSame($expectedValue, $axis->calculateYOffset($bb, $offsetY)); + } + + public function calculateZOffsetProvider(){ + return [ + [new AxisAlignedBB(1.1, 1.2, 1.3, 1.1, 2.2, 2.3), 1.1, 1.1], + [new AxisAlignedBB(1.1, 1.1, 1.5, 2.1, 1.2, 2.3), 1.1, 1.1], + [new AxisAlignedBB(1.1, 0.1, 2.3, 3.1, 2.2, 2.3), 1.1, 1.1], + [new AxisAlignedBB(1.1, 0.1, 2.3, 3.1, 2.2, 1.3), 1.1, 0.0], + [new AxisAlignedBB(1.1, 0.1, 2.3, 3.1, 2.2, 1.3), -1.1, 0.0], + ]; + } + + /** + * @dataProvider calculateZOffsetProvider + */ + public function testCalculateZOffset($bb, $offsetZ, $expectedValue){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + + $this->assertSame($expectedValue, $axis->calculateZOffset($bb, $offsetZ)); + } + + public function intersectsWithProvider(){ + return [ + [new AxisAlignedBB(1.1, 1.2, 1.3, 1.5, 2.2, 2.3), 0.00001, true], + [new AxisAlignedBB(1.1, 1.2, 1.3, 1.1, 2.2, 2.3), 1.1, false], + ]; + } + + /** + * @dataProvider intersectsWithProvider + */ + public function testIntersectsWith($bb, $epsilon, $expectedValue){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + + $this->assertSame($expectedValue, $axis->intersectsWith($bb, $epsilon)); + } + + public function testGetAverageEdgeLength(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + + $this->assertSame(1.0, $axis->getAverageEdgeLength()); + } + + public function testIsVectorInside(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + + $this->assertFalse($axis->isVectorInside(new Vector3(1.1, 1.2, 1.3))); + $this->assertFalse($axis->isVectorInside(new Vector3(1.5, 1.2, 1.3))); + $this->assertFalse($axis->isVectorInside(new Vector3(1.5, 2.0, 1.3))); + $this->assertTrue($axis->isVectorInside(new Vector3(1.5, 2.0, 1.4))); + } + + public function testIsVectorInYZ(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 1.2, 1.3); + + $this->assertFalse($axis->isVectorInYZ(new Vector3(1.1, 0.2, 1.3))); + $this->assertTrue($axis->isVectorInYZ(new Vector3(1.5, 1.2, 1.3))); + } + + public function testIsVectorInXZ(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 1.2, 1.3); + + $this->assertFalse($axis->isVectorInXZ(new Vector3(0.2, 1.1, 1.3))); + $this->assertTrue($axis->isVectorInXZ(new Vector3(1.2, 1.5, 1.3))); + } + + public function testIsVectorInXY(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 1.2, 1.3); + + $this->assertFalse($axis->isVectorInXY(new Vector3(0.2, 1.1, 1.3))); + $this->assertTrue($axis->isVectorInXY(new Vector3(2.1, 1.2, 1.5))); + } + + public function calculateInterceptProvider(){ + return[ + [new Vector3(0.0, 1.3, 1.5), new Vector3(0.0, 0.3, 0.5)], + [new Vector3(1.0, 2.3, -2.5), new Vector3(1.1, -2.3, -2.5)] + ]; + } + + /** + * @dataProvider calculateInterceptProvider + */ + public function testCalculateIntercept($pos1, $pos2){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 1.2, 1.3); + + $this->assertNull($axis->calculateIntercept($pos1, $pos2)); + } + + public function testToString(){ + $axis = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + + $this->assertSame('AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3)', (string) $axis); + } +} diff --git a/tests/MathTest.php b/tests/MathTest.php new file mode 100644 index 0000000..67d6b87 --- /dev/null +++ b/tests/MathTest.php @@ -0,0 +1,59 @@ +assertSame(1, Math::floorFloat(1.2)); + $this->assertSame(-2, Math::floorFloat(-1.5)); + } + + public function testCeilFloat(){ + $this->assertSame(1, Math::ceilFloat(1.2)); + $this->assertSame(-1, Math::ceilFloat(-1.2)); + } + + public function solveQuadraticProvider(){ + return [ + [2, 3, 4, []], + [1, 5, 4, [-1.0, -4.0]], + [1, 0, 0, [0.0]], + ]; + } + + /** + * @dataProvider solveQuadraticProvider + */ + public function testSolveQuadratic($a, $b, $c, $expectedArray){ + $this->assertSame($expectedArray, Math::solveQuadratic($a, $b, $c)); + } +} diff --git a/tests/MatrixTest.php b/tests/MatrixTest.php new file mode 100644 index 0000000..35ce77a --- /dev/null +++ b/tests/MatrixTest.php @@ -0,0 +1,242 @@ +assertSame(1, $matrix->getRows()); + $this->assertSame(2, $matrix->getColumns()); + } + + public function testOffsetExists(){ + $matrix = new Matrix(1, 2); + + $this->assertTrue($matrix->offsetExists(0)); + $this->assertFalse($matrix->offsetExists(1)); + } + + public function testOffsetGet(){ + $matrix = new Matrix(1, 2); + + $this->assertSame([0, 0],$matrix->offsetGet(0)); + } + + public function testOffsetSet(){ + $matrix = new Matrix(1, 2); + $matrix->offsetSet(0, [1, 1]); + + $this->assertSame([1, 1], $matrix->offsetGet(0)); + } + + public function testOffsetUnset(){ + $matrix = new Matrix(1, 2); + + $this->assertNull($matrix->offsetUnset(0)); + } + + public function testSet(){ + $matrix = new Matrix(1, 2); + $matrix->set([1, 1]); + + $this->assertSame([0, 0], $matrix->offsetGet(0)); + } + + public function testGetRows(){ + $matrix = new Matrix(1, 2); + + $this->assertSame(1, $matrix->getRows()); + } + + public function testGetColumns(){ + $matrix = new Matrix(1, 2); + + $this->assertSame(2, $matrix->getColumns()); + } + + public function testSetElement(){ + $matrix = new Matrix(1, 2); + + $this->assertTrue($matrix->setElement(0, 1, 3)); + $this->assertFalse($matrix->setElement(1, 3, 3)); + } + + public function testGetElement(){ + $matrix = new Matrix(1, 2); + $matrix->setElement(0, 1, 3); + + $this->assertSame(3, $matrix->getElement(0, 1)); + } + + public function testGetElementOnInvalidRowColumn(){ + $matrix = new Matrix(1, 2); + + $this->assertFalse($matrix->getElement(-1, -1)); + } + + public function testIsSquare(){ + $matrix = new Matrix(2, 2); + + $this->assertTrue($matrix->isSquare()); + } + + public function testIsNotSquare(){ + $matrix = new Matrix(1, 2); + + $this->assertFalse($matrix->isSquare()); + } + + public function testAdd(){ + $matrix = new Matrix(1, 2); + $matrix2 = new Matrix(1, 2); + $matrix2->setElement(0, 1, 3); + $result = $matrix->add($matrix2); + + $this->assertSame([0, 3], $result->offsetGet(0)); + } + + public function testAddOnRowColumnAreNotEqual(){ + $matrix = new Matrix(1, 2); + $matrix2 = new Matrix(2, 1); + + $this->assertFalse($matrix->add($matrix2)); + } + + public function testSubtract(){ + $matrix = new Matrix(1, 2); + $matrix2 = new Matrix(1, 2); + $matrix2->setElement(0, 1, 3); + $result = $matrix->subtract($matrix2); + + $this->assertSame([0, -3], $result->offsetGet(0)); + } + + public function testSubtractOnRowColumnAreNotEqual(){ + $matrix = new Matrix(1, 2); + $matrix2 = new Matrix(2, 1); + + $this->assertFalse($matrix->subtract($matrix2)); + } + + public function testMultiplyScalar(){ + $matrix = new Matrix(1, 2); + $matrix->setElement(0, 1, 3); + $matrix->setElement(0, 0, 4); + $result = $matrix->multiplyScalar(2); + + $this->assertSame([8, 6], $result->offsetGet(0)); + } + + public function testDivideScalar(){ + $matrix = new Matrix(1, 2); + $matrix->setElement(0, 0, 8); + $matrix->setElement(0, 1, 6); + $result = $matrix->divideScalar(2); + + $this->assertSame([4, 3], $result->offsetGet(0)); + } + + public function testTranspose(){ + $matrix = new Matrix(2, 3); + $result = $matrix->transpose(); + + $this->assertSame([0, 0], $result->offsetGet(0)); + } + + public function testProductOnNonNaiveMatrix(){ + $matrix = new Matrix(2, 3); + $matrix2 = new Matrix(2, 3); + $matrix2->setElement(0, 0, 2); + $matrix2->setElement(0, 1, 3); + $result = $matrix->product($matrix2); + + $this->assertFalse($result); + } + + public function testProduct(){ + $matrix = new Matrix(2, 2); + $matrix->setElement(0, 0, 3); + $matrix->setElement(0, 1, 4); + $matrix2 = new Matrix(2, 2); + $matrix2->setElement(0, 0, 2); + $matrix2->setElement(0, 1, 3); + $result = $matrix->product($matrix2); + + $this->assertSame([6, 9], $result->offsetGet(0)); + $this->assertSame([0, 0], $result->offsetGet(1)); + } + + public function determinantProvider(){ + return [ + [1, 1, 0], + ]; + } + + public function testDeterminantOnNonDeterminantMatrix(){ + $matrix = new Matrix(1, 2); + $matrix2 = new Matrix(4, 4); + + $this->assertFalse($matrix->determinant()); + $this->assertFalse($matrix2->determinant()); + } + + public function testDeterminant(){ + $matrix = new Matrix(1, 1); + $matrix2 = new Matrix(2, 2); + $matrix2->setElement(0, 0, 1); + $matrix2->setElement(0, 1, 2); + $matrix2->setElement(1, 0, 3); + $matrix2->setElement(1, 1, 4); + $matrix3 = new Matrix(3, 3); + $matrix3->setElement(0, 0, 1); + $matrix3->setElement(0, 1, 2); + $matrix3->setElement(0, 2, 3); + $matrix3->setElement(1, 0, 4); + $matrix3->setElement(1, 1, 6); + $matrix3->setElement(1, 2, 6); + $matrix3->setElement(2, 0, 7); + $matrix3->setElement(2, 1, 8); + $matrix3->setElement(2, 2, 9); + + $this->assertSame(0, $matrix->determinant()); + $this->assertSame(-2, $matrix2->determinant()); + $this->assertSame(-12, $matrix3->determinant()); + } + + public function testToString(){ + $matrix = new Matrix(1, 2); + $matrix->setElement(0, 0, 2); + + $this->assertSame('Matrix(1x2;2,0)', (string) $matrix); + } +} diff --git a/tests/RayTraceResultTest.php b/tests/RayTraceResultTest.php new file mode 100644 index 0000000..af83539 --- /dev/null +++ b/tests/RayTraceResultTest.php @@ -0,0 +1,62 @@ +assertSame('AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3)', (string) $rayTraceResult->getBoundingBox()); + } + + public function testGetHitFace(){ + $bb = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $hitVector = new Vector3(1.2, 1.3, 1.5); + $rayTraceResult = new RayTraceResult($bb, 1, $hitVector); + + $this->assertSame(1, $rayTraceResult->getHitFace()); + } + + public function testGetHitVector(){ + $bb = new AxisAlignedBB(1.1, 1.2, 1.3, 2.1, 2.2, 2.3); + $hitVector = new Vector3(1.2, 1.3, 1.5); + $rayTraceResult = new RayTraceResult($bb, 1, $hitVector); + + $this->assertSame(1.2, $rayTraceResult->getHitVector()->getX()); + $this->assertSame(1.3, $rayTraceResult->getHitVector()->getY()); + $this->assertSame(1.5, $rayTraceResult->getHitVector()->getZ()); + } +} diff --git a/tests/Vector2Test.php b/tests/Vector2Test.php new file mode 100644 index 0000000..d8fc4c9 --- /dev/null +++ b/tests/Vector2Test.php @@ -0,0 +1,179 @@ +assertSame(1.2, $vector2->getX()); + $this->assertSame(1.5, $vector2->getY()); + } + + public function testGetFloorVector(){ + $vector2 = new Vector2(1.2, 1.5); + + $this->assertSame(1, $vector2->getFloorX()); + $this->assertSame(1, $vector2->getFloorY()); + } + + public function testAdd(){ + $vector2 = new Vector2(1.2, 1.5); + $result = $vector2->add(1.2, 1.2); + $resultOnVector = $vector2->add(new Vector2(1.2, 1.5)); + + $this->assertSame(2.4, $result->getX()); + $this->assertSame(2.7, $result->getY()); + $this->assertSame(2.4, $resultOnVector->getX()); + $this->assertSame(3.0, $resultOnVector->getY()); + } + + public function testSubtract(){ + $vector2 = new Vector2(1.2, 1.5); + $result = $vector2->subtract(1.2, 1.2); + $resultOnVector = $vector2->subtract(new Vector2(1.2, 1.5)); + + $this->assertSame(0.0, $result->getX()); + $this->assertSame(0.3, $result->getY()); + $this->assertSame(0.0, $resultOnVector->getX()); + $this->assertSame(0.0, $resultOnVector->getY()); + } + + public function testCeil(){ + $vector2 = new Vector2(1.2, 1.5); + $result = $vector2->ceil(); + + $this->assertSame(2.0, $result->getX()); + $this->assertSame(2.0, $result->getY()); + } + + public function testFloor(){ + $vector2 = new Vector2(1.2, 1.5); + $result = $vector2->floor(); + + $this->assertSame(1.0, $result->getX()); + $this->assertSame(1.0, $result->getY()); + } + + public function testRound(){ + $vector2 = new Vector2(1.2, 1.5); + $result = $vector2->round(); + + $this->assertSame(1.0, $result->getX()); + $this->assertSame(2.0, $result->getY()); + } + + public function testAbs(){ + $vector2 = new Vector2(-1.2, -1.5); + $result = $vector2->abs(); + + $this->assertSame(1.2, $result->getX()); + $this->assertSame(1.5, $result->getY()); + } + + public function testMultiply(){ + $vector2 = new Vector2(-1.2, -1.5); + $result = $vector2->multiply(1.2); + + $this->assertSame(-1.44, $result->getX()); + $this->assertSame(-1.80, $result->getY()); + } + + public function testDivide(){ + $vector2 = new Vector2(-1.2, -1.5); + $result = $vector2->divide(2); + + $this->assertSame(-0.6, $result->getX()); + $this->assertSame(-0.75, $result->getY()); + } + + public function testDistance(){ + $vector2 = new Vector2(1.2, 1.5); + $result = $vector2->distance(0.2, 1.5); + $resultOnVector = $vector2->distance(new Vector2(1.2, 0.5)); + + $this->assertSame(1.0, $result); + $this->assertSame(1.0, $resultOnVector); + } + + public function testDistanceSquared(){ + $vector2 = new Vector2(1.2, 1.5); + $result = $vector2->distanceSquared(0.2, 1.5); + $resultOnVector = $vector2->distanceSquared(new Vector2(1.2, 0.5)); + + $this->assertSame(1.0, $result); + $this->assertSame(1.0, $resultOnVector); + } + + public function testLength(){ + $vector2 = new Vector2(1.0, 0.0); + + $this->assertSame(1.0, $vector2->length()); + } + + public function testLengthSquared(){ + $vector2 = new Vector2(2.0, 1.0); + + $this->assertSame(5.0, $vector2->lengthSquared()); + } + + public function normalizeProvider(){ + return [ + [1.0, 0.0, 1.0, 0.0], + [0.0, 0.0, 0.0, 0.0], + ]; + } + + /** + * @dataProvider normalizeProvider + */ + public function testNormalize($x, $y, $expectedValueX, $expectedValueY){ + $vector2 = new Vector2($x, $y); + $result = $vector2->normalize(); + + $this->assertSame($expectedValueX, $result->getX()); + $this->assertSame($expectedValueY, $result->getY()); + } + + public function testDot(){ + $vector2 = new Vector2(1.2, 1.3); + $v = new Vector2(2.0, 3.0); + + $this->assertSame(6.3, $vector2->dot($v)); + } + + public function testToString(){ + $vector2 = new Vector2(1.2, 1.5); + + $this->assertSame('Vector2(x=1.2,y=1.5)', (string) $vector2); + } +} diff --git a/tests/Vector3Test.php b/tests/Vector3Test.php new file mode 100644 index 0000000..aad1464 --- /dev/null +++ b/tests/Vector3Test.php @@ -0,0 +1,331 @@ +assertSame(1.2, $vector3->getX()); + $this->assertSame(1.5, $vector3->getY()); + $this->assertSame(1.3, $vector3->getZ()); + } + + public function testGetFloorVector(){ + $vector3 = new Vector3(1.2, 1.5, 1.3); + + $this->assertSame(1, $vector3->getFloorX()); + $this->assertSame(1, $vector3->getFloorY()); + $this->assertSame(1, $vector3->getFloorZ()); + } + + public function testAdd(){ + $vector3 = new Vector3(1.2, 1.5, 1.3); + $result = $vector3->add(1.2, 1.2, 1.3); + $resultOnVector = $vector3->add(new Vector3(1.2, 1.5)); + + $this->assertSame(2.4, $result->getX()); + $this->assertSame(2.7, $result->getY()); + $this->assertSame(2.6, $result->getZ()); + $this->assertSame(2.4, $resultOnVector->getX()); + $this->assertSame(3.0, $resultOnVector->getY()); + $this->assertSame(1.3, $resultOnVector->getZ()); + } + + public function testSubtract(){ + $vector3 = new Vector3(1.2, 1.5, 1.3); + $result = $vector3->subtract(1.2, 1.2, 1.2); + $resultOnVector = $vector3->subtract(new Vector3(1.2, 1.5)); + + $this->assertSame(0.0, $result->getX()); + $this->assertSame(0.3, $result->getY()); + $this->assertSame(0.1, $result->getZ()); + $this->assertSame(0.0, $resultOnVector->getX()); + $this->assertSame(0.0, $resultOnVector->getY()); + $this->assertSame(1.3, $resultOnVector->getZ()); + } + + public function testCeil(){ + $vector3 = new Vector3(1.2, 1.5, 1.3); + $result = $vector3->ceil(); + + $this->assertSame(2, $result->getX()); + $this->assertSame(2, $result->getY()); + $this->assertSame(2, $result->getZ()); + } + + public function testFloor(){ + $vector3 = new Vector3(1.2, 1.5, 1.3); + $result = $vector3->floor(); + + $this->assertSame(1, $result->getX()); + $this->assertSame(1, $result->getY()); + $this->assertSame(1, $result->getZ()); + } + + public function testRound(){ + $vector3 = new Vector3(1.2, 1.5, 1.3); + $result = $vector3->round(); + $resultOnPrecision = $vector3->round(1); + + $this->assertSame(1, $result->getX()); + $this->assertSame(2, $result->getY()); + $this->assertSame(1, $result->getZ()); + $this->assertSame(1.2, $resultOnPrecision->getX()); + $this->assertSame(1.5, $resultOnPrecision->getY()); + $this->assertSame(1.3, $resultOnPrecision->getZ()); + } + + public function testAbs(){ + $vector3 = new Vector3(-1.2, -1.5, 1.3); + $result = $vector3->abs(); + + $this->assertSame(1.2, $result->getX()); + $this->assertSame(1.5, $result->getY()); + $this->assertSame(1.3, $result->getZ()); + } + + public function getSideProvider(){ + return [ + [0, 1, -1.2, -2.5, 1.3], + [1, 1, -1.2, -0.5, 1.3], + [2, 1, -1.2, -1.5, 0.3], + [3, 1, -1.2, -1.5, 2.3], + [4, 1, -2.2, -1.5, 1.3], + [5, 1, -0.2, -1.5, 1.3], + ]; + } + + /** + * @dataProvider getSideProvider + */ + public function testGetSide($side, $step, $expectedValueX, $expectedValueY, $expectedValueZ){ + $vector3 = new Vector3(-1.2, -1.5, 1.3); + $result = $vector3->getSide($side, $step); + + $this->assertSame($expectedValueX, $result->getX()); + $this->assertSame($expectedValueY, $result->getY()); + $this->assertSame($expectedValueZ, $result->getZ()); + } + + public function testGetSideOnInvalidSide(){ + $vector3 = new Vector3(-1.2, -1.5, 1.3); + + $this->assertInstanceOf(Vector3::class, $vector3->getSide(6)); + } + + public function testAsVector3(){ + $vector3 = new Vector3(-1.2, -1.5, 1.3); + $asVector3 = $vector3->asVector3(); + + $this->assertInstanceOf(Vector3::class, $asVector3); + $this->assertSame(-1.2, $asVector3->getX()); + $this->assertSame(-1.5, $asVector3->getY()); + $this->assertSame(1.3, $asVector3->getZ()); + } + + public function testGetOppositeSide(){ + $vector3 = new Vector3(-1.2, -1.5, 1.3); + $getOppositeResult = $vector3->getOppositeSide(0); + + $this->assertSame(1, $getOppositeResult); + } + + public function testGetOppositeSideOnInvalidSide(){ + $vector3 = new Vector3(-1.2, -1.5, 1.3); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid side 6 given to getOppositeSide'); + $getOppositeResult = $vector3->getOppositeSide(6); + } + + public function testMultiply(){ + $vector3 = new Vector3(-1.2, -1.5, 1.3); + $result = $vector3->multiply(1.2); + + $this->assertSame(-1.44, $result->getX()); + $this->assertSame(-1.80, $result->getY()); + $this->assertSame(1.56, $result->getZ()); + } + + public function testDivide(){ + $vector3 = new Vector3(-1.2, -1.5, 1.3); + $result = $vector3->divide(2); + + $this->assertSame(-0.6, $result->getX()); + $this->assertSame(-0.75, $result->getY()); + $this->assertSame(0.65, $result->getZ()); + } + + public function testDistance(){ + $vector3 = new Vector3(1.2, 1.5, 2.3); + $result = $vector3->distance(new Vector3(1.2, 1.5, 0.3)); + + $this->assertSame(2.0, $result); + } + + public function testDistanceSquared(){ + $vector3 = new Vector3(1.2, 1.5, 2.3); + $result = $vector3->distanceSquared(new Vector3(1.2, 1.5, 0.3)); + + $this->assertSame(4.0, $result); + } + + public function maxPlainDistanceProvider(){ + return [ + [new Vector3(1.1, 1.2, 1.3), 0.2, 0.2], + [new Vector2(1.2, 1.3), 0, 0.2], + ]; + } + + /** + * @dataProvider maxPlainDistanceProvider + */ + public function testMaxPlainDistance($vector, $z, $expectedValue){ + $vector3 = new Vector3(1.2, 1.3, 1.5); + + $this->assertSame($expectedValue, $vector3->maxPlainDistance($vector, $z)); + } + + public function testLength(){ + $vector3 = new Vector3(1.0, 0.0, 0.0); + + $this->assertSame(1.0, $vector3->length()); + } + + public function testLengthSquared(){ + $vector3 = new Vector3(1.2, 1.3, 1.5); + + $this->assertSame(5.38, $vector3->lengthSquared()); + } + + public function normalizeProvider(){ + return [ + [1.0, 0.0, 0.0, 1.0, 0.0, 0.0], + [0, 0, 0, 0, 0, 0], + ]; + } + + /** + * @dataProvider normalizeProvider + */ + public function testNormalize($x, $y, $z, $expectedValueX, $expectedValueY, $expectedValueZ){ + $vector3 = new Vector3($x, $y); + $result = $vector3->normalize(); + + $this->assertSame($expectedValueX, $result->getX()); + $this->assertSame($expectedValueY, $result->getY()); + $this->assertSame($expectedValueZ, $result->getZ()); + } + + public function testDot(){ + $vector3 = new Vector3(1.2, 1.3, 1.5); + $v = new Vector3(2.0, 3.0, 4.0); + + $this->assertSame(12.3, $vector3->dot($v)); + } + + public function testCross(){ + $vector3 = new Vector3(1.2, 1.3, 1.5); + $v = new Vector3(2.0, 3.0, 4.0); + $result = $vector3->cross($v); + + $this->assertSame(0.7, $result->getX()); + $this->assertSame(-1.8, $result->getY()); + $this->assertSame(1.0, $result->getZ()); + } + + public function testEquals(){ + $vector3 = new Vector3(1.2, 1.3, 1.5); + $v1 = new Vector3(1.2, 1.3, 1.5); + $v2 = new Vector3(1.2, 1.3, 1.6); + + $this->assertTrue($vector3->equals($v1)); + $this->assertFalse($vector3->equals($v2)); + } + + public function testGetIntermediateWithXValue(){ + $vector3 = new Vector3(0.0, 1.3, 1.5); + $v1 = new Vector3(0.00000001, 1.2, 1.3); + $v2 = new Vector3(1.1, 1.2, 1.3); + $result = $vector3->getIntermediateWithXValue($v2, 0.0); + + $this->assertNull($vector3->getIntermediateWithXValue($v1, 0.0)); + $this->assertNull($vector3->getIntermediateWithXValue($v2, -10.0)); + $this->assertSame(0.0, $result->getX()); + $this->assertSame(1.3, $result->getY()); + $this->assertSame(1.5, $result->getZ()); + } + + public function testGetIntermediateWithYValue(){ + $vector3 = new Vector3(1.2, 0.0, 1.5); + $v1 = new Vector3(1.2, 0.00000001, 1.3); + $v2 = new Vector3(1.1, 1.2, 1.3); + $result = $vector3->getIntermediateWithYValue($v2, 0.0); + + $this->assertNull($vector3->getIntermediateWithYValue($v1, 0.0)); + $this->assertNull($vector3->getIntermediateWithYValue($v2, -10.0)); + $this->assertSame(1.2, $result->getX()); + $this->assertSame(0.0, $result->getY()); + $this->assertSame(1.5, $result->getZ()); + } + + public function testGetIntermediateWithZValue(){ + $vector3 = new Vector3(1.2, 1.5, 0.0); + $v1 = new Vector3(1.2, 1.3, 0.00000001); + $v2 = new Vector3(1.1, 1.2, 1.3); + $result = $vector3->getIntermediateWithZValue($v2, 0.0); + + $this->assertNull($vector3->getIntermediateWithZValue($v1, 0.0)); + $this->assertNull($vector3->getIntermediateWithZValue($v2, -10.0)); + $this->assertSame(1.2, $result->getX()); + $this->assertSame(1.5, $result->getY()); + $this->assertSame(0.0, $result->getZ()); + } + + public function testSetComponents(){ + $vector3 = new Vector3(1.2, 1.3, 1.5); + $result = $vector3->setComponents(1.1, 1.2, 1.3); + + $this->assertInstanceOf(Vector3::class, $result); + $this->assertSame(1.1, $result->getX()); + $this->assertSame(1.2, $result->getY()); + $this->assertSame(1.3, $result->getZ()); + } + + public function testToString(){ + $vector3 = new Vector3(1.2, 1.3, 1.5); + + $this->assertSame('Vector3(x=1.2,y=1.3,z=1.5)', (string) $vector3); + } +} diff --git a/tests/VectorMathTest.php b/tests/VectorMathTest.php new file mode 100644 index 0000000..c5898c1 --- /dev/null +++ b/tests/VectorMathTest.php @@ -0,0 +1,41 @@ +assertSame(1.0, $vector->getX()); + $this->assertSame(0.0, $vector->getY()); + } +} \ No newline at end of file diff --git a/tests/VoxelRayTraceTest.php b/tests/VoxelRayTraceTest.php new file mode 100644 index 0000000..4f7d3b5 --- /dev/null +++ b/tests/VoxelRayTraceTest.php @@ -0,0 +1,49 @@ +assertSame('Vector3(x=1,y=1,z=4)', (string) $result->current()); + } + + public function testInDirectionThrowsInvalidArgumentException(){ + $result = VoxelRayTrace::inDirection(new Vector3(0.0, 0.0, 0.0), new Vector3(0.0, 0.0, 0.0), 1.2); + + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Start and end points are the same, giving a zero direction vector'); + $this->assertSame('Vector3(x=1,y=1,z=4)', (string) $result->current()); + } +} \ No newline at end of file