-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
/** | ||
* This file is part of the SimplexCalculator 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 class Vector implements \Countable | ||
{ | ||
|
||
/** @var Fraction[] $values */ | ||
private readonly array $values; | ||
|
||
private readonly 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 function ($value): Fraction { | ||
return 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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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(); |