Skip to content

Commit

Permalink
added Vector
Browse files Browse the repository at this point in the history
  • Loading branch information
uestla committed Oct 11, 2024
1 parent 53305e1 commit 904a610
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Simplex/Math/Vector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types = 1);

/**
* This file is part of the Simplex-Calculator library
*
* Copyright (c) 2014 Petr Kessler (https://kesspess.cz)
*
* @license MIT
* @link https://github.com/uestla/Simplex-Calculator
*/

namespace Simplex\Math;

use Simplex\EmptyVectorException;


final readonly class Vector implements \Countable
{
/** @var Fraction[] $values */
private array $values;

private int $size;


/** @param array<int, Fraction|string|int|float> $values */
public function __construct(array $values)
{
if ($values === []) {
throw new EmptyVectorException;
}

$this->values = array_map(
static fn ($value): Fraction => Fraction::create($value),
array_values($values),
);

$this->size = count($this->values);
}


/** @param Vector|array<int, Fraction|string|int|float> $values */
public static function create(self|array $values): self
{
return $values instanceof self ? $values : new self($values);
}


/** @return Fraction[] */
public function toArray(): array
{
return $this->values;
}


public function count(): int
{
return $this->size;
}
}
9 changes: 9 additions & 0 deletions Simplex/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,14 @@ public function __construct()
}


final class EmptyVectorException extends SimplexException
{
public function __construct()
{
parent::__construct('Vector must have at least one value.');
}
}


abstract class SimplexException extends \Exception
{}
1 change: 1 addition & 0 deletions Simplex/simplex.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@
require_once __DIR__ . '/exceptions.php';
require_once __DIR__ . '/Math/math.php';
require_once __DIR__ . '/Math/Fraction.php';
require_once __DIR__ . '/Math/Vector.php';
69 changes: 69 additions & 0 deletions tests/VectorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types = 1);

namespace Simplex\Tests;

use Tester\Assert;
use Tester\TestCase;
use Simplex\Math\Vector;
use Simplex\Math\Fraction;
use Simplex\EmptyVectorException;


require_once __DIR__ . '/bootstrap.php';


final class VectorTest extends TestCase
{
/**
* @param array<int|float|string|Fraction> $values
* @param Fraction[] $expectedValues
* @dataProvider provideCreationData
*/
public function testCreation(array $values, array $expectedValues, int $expectedCount): void
{
$vectorConstructor = new Vector($values);
Assert::equal($expectedValues, $vectorConstructor->toArray());
Assert::count($expectedCount, $vectorConstructor);

$vectorFactory = Vector::create($values);
Assert::equal($expectedValues, $vectorFactory->toArray());
Assert::count($expectedCount, $vectorFactory);
}


/** @return array<int, array{array<int|float|string|Fraction>, Fraction[], int}> */
public function provideCreationData(): array
{
return [
[
[1, '2', 3.0, new Fraction('4')],
[new Fraction('1'), new Fraction('2'), new Fraction('3'), new Fraction('4')],
4,
],
[
['a' => 1, 'b' => '2', 3.0, 'xyz' => new Fraction('4')],
[new Fraction('1'), new Fraction('2'), new Fraction('3'), new Fraction('4')],
4,
],
];
}


public function testEmptyVector(): void
{
Assert::exception(static function (): void {
new Vector([]);

}, EmptyVectorException::class, 'Vector must have at least one value.');

Assert::exception(static function (): void {
Vector::create([]);

}, EmptyVectorException::class, 'Vector must have at least one value.');
}
}


(new VectorTest)->run();

0 comments on commit 904a610

Please sign in to comment.