From cb3a3dbf898b852f92fd66254f8aecdd5136e42e Mon Sep 17 00:00:00 2001 From: PrInStPL Date: Sun, 30 Jun 2024 22:25:18 +0200 Subject: [PATCH] feat: object test --- src/Classes/ChainPlainClass802.php | 30 ++ src/Classes/PrinstChain802.php | 97 ++++ src/Classes/PrinstChain802Iterator.php | 300 ++++++++++++ src/Classes/Test.php | 448 ++++++++++++++++++ src/Classes/TestConfig.php | 103 ++++ .../ot-ArrayObject-of-objects-PlainClass.php | 64 +++ src/Tests/ot-ChainPlainClass802.php | 70 +++ .../ot-DsMap-seq-of-objects-PlainClass.php | 66 +++ .../ot-DsMap-string-of-objects-PlainClass.php | 67 +++ .../ot-DsVector-of-objects-PlainClass.php | 66 +++ ...ot-SplFixedArray-of-objects-PlainClass.php | 67 +++ ...SplObjectStorage-of-objects-PlainClass.php | 87 ++++ src/Tests/ot-array-seq-of-arrays.php | 44 ++ src/Tests/ot-array-string-of-arrays.php | 45 ++ src/test-obj.php | 36 ++ 15 files changed, 1590 insertions(+) create mode 100644 src/Classes/ChainPlainClass802.php create mode 100644 src/Classes/PrinstChain802.php create mode 100644 src/Classes/PrinstChain802Iterator.php create mode 100644 src/Classes/Test.php create mode 100644 src/Classes/TestConfig.php create mode 100644 src/Tests/ot-ArrayObject-of-objects-PlainClass.php create mode 100644 src/Tests/ot-ChainPlainClass802.php create mode 100644 src/Tests/ot-DsMap-seq-of-objects-PlainClass.php create mode 100644 src/Tests/ot-DsMap-string-of-objects-PlainClass.php create mode 100644 src/Tests/ot-DsVector-of-objects-PlainClass.php create mode 100644 src/Tests/ot-SplFixedArray-of-objects-PlainClass.php create mode 100644 src/Tests/ot-SplObjectStorage-of-objects-PlainClass.php create mode 100644 src/Tests/ot-array-seq-of-arrays.php create mode 100644 src/Tests/ot-array-string-of-arrays.php create mode 100644 src/test-obj.php diff --git a/src/Classes/ChainPlainClass802.php b/src/Classes/ChainPlainClass802.php new file mode 100644 index 0000000..0956505 --- /dev/null +++ b/src/Classes/ChainPlainClass802.php @@ -0,0 +1,30 @@ + $this->info, + 'first' => $this->first, + 'second' => $this->second, + 'object id' => spl_object_id($this), + 'previous object id' => $this->previous ? spl_object_id($this->previous) : null, + 'next object id' => $this->next ? spl_object_id($this->next) : null, + ]; + } +} diff --git a/src/Classes/PrinstChain802.php b/src/Classes/PrinstChain802.php new file mode 100644 index 0000000..227f8e5 --- /dev/null +++ b/src/Classes/PrinstChain802.php @@ -0,0 +1,97 @@ +previous?->setNext($this); + $this->iterator?->append($this); + } + + public function __destruct() + { + $this->previous?->setNext($this->next); + $this->next?->setPrevious($this->previous); + + if ($current = ($this->previous ?? $this->next)) { + $this->iterator?->recreate($current); + } + } + + /** + * @inheritDoc + */ + public function getIterator(): PrinstChain802Iterator + { + if (!$this->iterator) { + while ($element = ($element ?? $this)->getPrevious()) { + $this->iterator = $element->iterator; + + if ($this->iterator) { + return $this->iterator; + } + } + + while ($element = ($element ?? $this)->getNext()) { + $this->iterator = $element->iterator; + + if ($this->iterator) { + return $this->iterator; + } + } + + $this->iterator = new PrinstChain802Iterator($this); + } + + return $this->iterator; + } + + public function getNext(): ?static + { + return $this->next; + } + + public function setNext(?self $next, bool $chainThisToNextPrevious = true): ?static + { + $this->next = $next; + + if ($chainThisToNextPrevious && $this !== $next?->getPrevious()) { + $this->next?->setPrevious($this); + } + + // ToDo: chain old next to him second previous if exists + + return $this; + } + + public function getPrevious(): ?static + { + return $this->previous; + } + + public function setPrevious(?self $previous, bool $chainThisToPreviousNext = true): ?static + { + $this->previous = $previous; + + if ($chainThisToPreviousNext && $this !== $previous?->getNext()) { + $this->previous?->setNext($this); + } + + // ToDo: chain old previous to him second next if exists + + return $this; + } +} diff --git a/src/Classes/PrinstChain802Iterator.php b/src/Classes/PrinstChain802Iterator.php new file mode 100644 index 0000000..4037ad5 --- /dev/null +++ b/src/Classes/PrinstChain802Iterator.php @@ -0,0 +1,300 @@ +recreate($current); + parent::__construct($this->toArray()); + } + + /** + * @inheritDoc + */ + public function current(): ?PrinstChain802 + { + return $this->current; + } + + /** + * @inheritDoc + */ + public function next(): void + { + $this->current = $this->current?->getNext(); + $this->key++; + } + + /** + * @inheritDoc + */ + public function key(): ?int + { + return $this->key; + } + + /** + * @inheritDoc + */ + public function valid(): bool + { + return $this->current !== null; + } + + /** + * @inheritDoc + */ + public function rewind(): void + { + $this->current = $this->first; + $this->key = 0; + } + + /** + * @inheritDoc + */ + public function count(): int + { + return $this->count; + } + + /** + * @inheritDoc + */ + public function offsetExists(mixed $key): bool + { + if (is_int($key)) { + if (0 < $key && $this->count < $key) { + return true; + } + } elseif ($key instanceof PrinstChain802) { + return false !== $this->findKey($key); + } + + return false; + } + + /** + * @inheritDoc + */ + public function offsetGet(mixed $key): ?PrinstChain802 + { + if (!is_int($key)) { + throw new InvalidArgumentException('Offset must be an integer'); + } + + if (0 === $key) { + return $this->first; + } elseif ($this->key === $key) { + return $this->current; + } else { + $element = $this->first; + + for ($i = 0; $i <= $key; ++$i) { + $element = $element->getNext(); + } + + return $element; + } + } + + /** + * @inheritDoc + */ + public function offsetSet(mixed $key, mixed $value): void + { + if (!is_int($key)) { + throw new InvalidArgumentException('Offset must be an integer'); + } + + if (!($value instanceof PrinstChain802)) { + throw new InvalidArgumentException('Value must be an instance of ' . PrinstChain802::class); + } + } + + /** + * @inheritDoc + */ + public function offsetUnset(mixed $key): void + { + if (!is_int($key)) { + throw new InvalidArgumentException('Offset must be an integer'); + } + // TODO: Implement offsetUnset() method. + } + + public function getFirst(): PrinstChain802 + { + return $this->first; + } + + public function getFirstKey(): int + { + return 0; + } + + public function getLast(): PrinstChain802 + { + return $this->last; + } + + public function getLastKey(): int + { + return $this->key - 1; + } + + public function append(mixed $value): void + { + if (!($value instanceof PrinstChain802)) { + throw new InvalidArgumentException('Value must be an instance of ' . PrinstChain802::class); + } + + if (false !== $this->findKey($value)) { + throw new InvalidArgumentException('Element already exists in chain'); + } + + if ($this->last->getNext() !== $value) { + $this->last->setNext($value); + } + + if ($value->getPrevious() !== $this->last) { + $value->setPrevious($value); + } + + $this->count++; + $this->key++; + $this->last = $value; + } + + /** + * @param PrinstChain802 $any + * @param bool $asCurrentKet + * + * @return void + */ + public function recreate(PrinstChain802 $any, bool $asCurrentKet = true): void + { + $this->count = 1; + + $this->first = $any; + $element = $any; + $key = 0; + while ($element = $element->getPrevious()) { + $this->count++; + $this->first = $element; + $key--; + } + + if ($asCurrentKet) { + $this->current = $any; + $this->key = abs($key); + } else { + $this->current = $this->first; + $this->key = 0; + } + + $this->last = $any; + $element = $any; + while ($element = $element->getNext()) { + $this->count++; + $this->last = $element; + } + } + + /** + * @param PrinstChain802 $element + * + * @return int|false + */ + public function findKey(PrinstChain802 $element): int|false + { + $candidate = $this->first; + $key = 0; + + if ($candidate === $element) { + return $key; + } + + while ($candidate = $candidate->getNext()) { + $key++; + if ($candidate === $element) { + return $key; + } + } + + return false; + } + + /** + * @param int $key + * + * @return bool + */ + public function setCurrentKey(int $key): bool + { + if (!$this->offsetExists($key)) { + return false; + } + + if (0 === $key) { + $this->current = $this->first; + } elseif (($this->count - 1) === $key) { + $this->current = $this->last; + } else { + $current = $this->first; + for ($i = 1; $i <= $key; ++$i) { + $current = $current->getNext(); + } + $this->current = $current; + } + + $this->key = $key; + + return true; + } + + /** + * @return PrinstChain802[] + */ + public function toArray(): array + { + $array = [$element = $this->first]; + + while ($element = $element->getNext()) { + $array[] = $element; + } + + return $array; + } + + /** + * @return non-empty-array + */ + public function __debugInfo(): array + { + return [ + 'count' => $this->count, + 'first object id' => spl_object_id($this->first), + 'key' => $this->key, + 'current object id' => spl_object_id($this->current), + 'last object id' => spl_object_id($this->last), + ]; + } +} diff --git a/src/Classes/Test.php b/src/Classes/Test.php new file mode 100644 index 0000000..96e5fdd --- /dev/null +++ b/src/Classes/Test.php @@ -0,0 +1,448 @@ +executionSection($name); + } + + /** + * @param string $__function__ + * + * @return Measurement + */ + protected function measurementStart(string $__function__): Measurement + { + if (empty($this->measurements[$__function__])) { + $this->measurements[$__function__] = new Measurement(); + } + + return $this->measurements[$__function__]->start(); + } + + /** + * @param string $__function__ + * + * @return Measurement + */ + protected function measurementStop(string $__function__): Measurement + { + if (empty($this->measurements[$__function__])) { + $this->measurementStart($__function__); + } + + return $this->measurements[$__function__]->stop(); + } + + /** + * @return void + */ + public static function printResults(): void + { + echo PHP_EOL; + echo '-> PHP ver.: ' . PHP_VERSION . PHP_EOL; + echo '-> The number of elements: ' . TestConfig::getDataSetCount() . PHP_EOL; + echo '-> The number of repetitions: ' . TestConfig::getRepetitions() . PHP_EOL; + echo '-> Time taken (in seconds) by all tests: ' . Measurement::getTimeTakenGlobal() . PHP_EOL; + echo '-> Memory used (in bytes) by all tests: ' + . number_format(Measurement::getMemoryUsedGlobal(), 0, '', ' ') + . PHP_EOL + ; + + echo PHP_EOL; + /** @noinspection PhpRedundantOptionalArgumentInspection */ + StaticReport::printResult(StaticReport::PRINT_CLI, StaticReport::LAYOUT_SECTIONS_LEFT); + echo PHP_EOL; + } + + /** + * @return void + */ + protected function printTestExecutionDebugInfo(): void + { + if (TestConfig::getTestsExecutionDebugPrinting()) { + echo '.'; + + if (80 <= ++$this->debugEchoLength) { + echo PHP_EOL; + $this->debugEchoLength = 0; + } + } + } + + /** + * @return int + */ + protected function getTestsMemoryCalculated(): int + { + $memory = 0; + + foreach ($this->measurements as $measurement) { + $memory += $measurement->getMemoryUsed(); + } + + return $memory; + } + + /** + * @return float + */ + protected function getTestsTimeCalculated(): float + { + $time = 0.0; + + foreach ($this->measurements as $measurement) { + $time += $measurement->getTimeTaken(); + } + + return $time; + } + + /** + * @return $this + */ + public function printTestResults(): self + { + $textTime = '* test object time used (seconds): ' . $this->getTestsTimeCalculated(); + $textMemory + = '* test object memory used (seconds): ' + . number_format($this->getTestsMemoryCalculated(), 0, '', ' ') + ; + $coverLength + = ($textTimeLength = strlen($textTime)) > ($textMemoryLength = strlen($textMemory)) + ? $textTimeLength + : $textMemoryLength + ; + + echo str_repeat('-', $coverLength) . PHP_EOL + . $textTime . PHP_EOL + . $textMemory . PHP_EOL + . str_repeat('-', $coverLength) . PHP_EOL + ; + + return $this; + } + + /** + * @param string $section + * + * @return void + */ + protected function executionSection(string $section): void + { + if (TestConfig::getTestsExecutionInfoPrinting()) { + $section = 'TEST: ' . $section; + $sectionLength = strlen($section); + $sectionCover = str_repeat('-', $sectionLength); + + echo PHP_EOL + . $sectionCover . PHP_EOL + . $section . PHP_EOL + . $sectionCover . PHP_EOL + . '* PHP ver.: ' . PHP_VERSION . PHP_EOL + . '* data set length: ' . number_format(TestConfig::getDataSetCount(), 0, '', ' ') . PHP_EOL + . '* get/set repetitions: ' . number_format(TestConfig::getRepetitions(), 0, '', ' ') . PHP_EOL + . PHP_EOL + ; + + $this->debugEchoLength = 0; + } + + StaticReport::addSection($section); + } + + /** + * @param string $header + * @param int $repetitions + * @param string|null $__function__ + * + * @return void + */ + protected function executionHeader(string $header, int $repetitions, ?string $__function__ = null): void + { + if (TestConfig::getTestsExecutionInfoPrinting()) { + echo "-> $header [repetitions: " + . number_format($repetitions, 0, '', ' ') + . ']' + . PHP_EOL + ; + + $this->debugEchoLength = 0; + } + + StaticReport::addHeader($__function__ ?? $header); + StaticReport::addResult( + StaticReport::RESULT_TYPE_REPETITION, + number_format($repetitions, 0, '', ' ') + ); + } + + /** + * @param Measurement $measurement + * + * @return void + */ + protected function executionResult(Measurement $measurement): void + { + if (TestConfig::getTestsExecutionInfoPrinting()) { + if (TestConfig::getTestsExecutionDebugPrinting()) { + echo PHP_EOL; + } + + echo ' time taken (seconds): ' . $measurement->getTimeTaken() . PHP_EOL + . ' memory used (bytes): ' . number_format($measurement->getMemoryUsed(), 0, '', ' ') . PHP_EOL + . PHP_EOL + ; + + $this->debugEchoLength = 0; + } + + StaticReport::addResult( + StaticReport::RESULT_TYPE_TIME, + (string) $measurement->getTimeTaken() + ); + StaticReport::addResult( + StaticReport::RESULT_TYPE_MEMORY, + number_format($measurement->getMemoryUsed(), 0, '', ' ') + ); + } + + /** + * @param callable(array|object &$dataSet, non-negative-int $elementNr): void $dataSetCreation + * @param callable(): array|object|null $dataSetInitialization + * + * @return self + * @noinspection PhpDocSignatureInspection + */ + public function createDataSet(callable $dataSetCreation, callable $dataSetInitialization): self + { + $this->executionHeader(__FUNCTION__, TestConfig::getDataSetCount()); + + $this->measurementStart(__FUNCTION__); + + $this->dataSet = $dataSetInitialization(); + $this->printTestExecutionDebugInfo(); + + for ($i = 0; $i < TestConfig::getDataSetCount(); $i++) { + $dataSetCreation($this->dataSet, $i); + + $this->printTestExecutionDebugInfo(); + } + + $this->executionResult($this->measurementStop(__FUNCTION__)); + + return $this; + } + + /** + * @param callable(array|object &$element, int $elementIndex, int $repetitionNr): bool $arrayWalkCallable Have to + * return true to walk successfully + * @param null|callable(array|object &$dataSet, int $repetitionNr): array $dataSetAlternative + * + * @return self + */ + public function testArrayWalk( + callable $arrayWalkCallable, + ?callable $dataSetAlternative = null + ): self { + $this->executionHeader(__FUNCTION__, TestConfig::getRepetitions()); + + $this->measurementStart(__FUNCTION__); + + if (is_null($dataSetAlternative)) { + for ($i = 0; $i < TestConfig::getRepetitions(); $i++) { + array_walk( + $this->dataSet, + $arrayWalkCallable, + $i + ); + + $this->printTestExecutionDebugInfo(); + } + } else { + for ($i = 0; $i < TestConfig::getRepetitions(); $i++) { + $dataSet = $dataSetAlternative($this->dataSet); + + array_walk( + $dataSet, + $arrayWalkCallable, + $i + ); + + $this->printTestExecutionDebugInfo(); + } + } + + $this->executionResult($this->measurementStop(__FUNCTION__)); + + return $this; + } + + /** + * @param callable(array|object $element): array|object $arrayMapCallable + * @param null|callable(array|object &$dataSet, int $repetitionNr): array $dataSetAlternative + * @param null|callable(array &$mappedDataArray, array|object &$sourceDataSet): array|object $dataSetFinisher + * + * @return self + * @noinspection PhpDocSignatureInspection + */ + public function testArrayMap( + callable $arrayMapCallable, + ?callable $dataSetAlternative = null, + ?callable $dataSetFinisher = null + ): self { + global $repetitionNr; + + $this->executionHeader(__FUNCTION__, TestConfig::getRepetitions()); + + $this->measurementStart(__FUNCTION__); + + for ($repetitionNr = 0; $repetitionNr < TestConfig::getRepetitions(); $repetitionNr++) { + $dataSet = $dataSetAlternative + ? $dataSetAlternative($this->dataSet, $repetitionNr) + : null + ; + + $mappedDataSet = array_map( + $arrayMapCallable, + $dataSet ?? $this->dataSet + ); + + if ($dataSetFinisher) { + $this->dataSet = $dataSetFinisher($mappedDataSet, $this->dataSet); + } + $this->dataSet = $dataSetFinisher + ? $dataSetFinisher($mappedDataSet, $this->dataSet) + : $mappedDataSet + ; + + $this->printTestExecutionDebugInfo(); + } + + $this->executionResult($this->measurementStop(__FUNCTION__)); + + unset($repetitionNr); + + return $this; + } + + /** + * For unify values + * + * @param string $case + * @param int $i + * + * @return string + */ + public static function valueOfInfo(string $case, int $i): string { + return "Some $i information in $i $case repetition"; + } + + /** + * For unify values + * + * @param int $i + * + * @return string + */ + public static function valueOfFirst(int $i): string { + return sprintf('%015d', $i); + } + + /** + * The premise is to set element values by it storage key. + * + * @param callable(array|object &$dataSet, array|object $element, int|string $elementIndex, int $repetitionNr): void $callable + * + * @return $this + */ + public function testSetForeach(callable $callable): self + { + $this->executionHeader(__FUNCTION__, TestConfig::getRepetitions()); + + $this->measurementStart(__FUNCTION__); + + for ($repetitionNr = 0; $repetitionNr < TestConfig::getRepetitions(); $repetitionNr++) { + foreach ($this->dataSet as $key => $value) { + $callable($this->dataSet, $value, $key, $repetitionNr); + + $this->printTestExecutionDebugInfo(); + } + } + + $this->executionResult($this->measurementStop(__FUNCTION__)); + + return $this; + } + + /** + * @param callable(array|object $element, int|string $elementIndex, int $repetitionNr): array|object $setValuesCallable + * @param null|callable(array|object &$element, int|string $elementIndex, array|object &$dataSet): void $dataSetFinisher + * + * @return $this + * @noinspection PhpDocSignatureInspection + */ + public function testSetFor(callable $setValuesCallable, ?callable $dataSetFinisher = null): self + { + $this->executionHeader(__FUNCTION__, TestConfig::getRepetitions()); + + $this->measurementStart(__FUNCTION__); + + if (is_null($dataSetFinisher)) { + for ($repetitionNr = 0; $repetitionNr < TestConfig::getRepetitions(); $repetitionNr++) { + foreach ($this->dataSet as $key => $value) { + $setValuesCallable($value, $key, $repetitionNr); + + $this->printTestExecutionDebugInfo(); + } + } + } else { + for ($repetitionNr = 0; $repetitionNr < TestConfig::getRepetitions(); $repetitionNr++) { + foreach ($this->dataSet as $elementIndex => $element) { + $dataSetFinisher( + $setValuesCallable($element, $elementIndex, $repetitionNr), + $elementIndex, + $this->dataSet + ); + + $this->printTestExecutionDebugInfo(); + } + } + } + + $this->executionResult($this->measurementStop(__FUNCTION__)); + + return $this; + } + + /** + * @return $this + */ + public function unsetDataSet(): self + { + $this->dataSet = null; + + return $this; + } +} diff --git a/src/Classes/TestConfig.php b/src/Classes/TestConfig.php new file mode 100644 index 0000000..836ed99 --- /dev/null +++ b/src/Classes/TestConfig.php @@ -0,0 +1,103 @@ +createDataSet( + function(ArrayObject $data, int $elementNr) { + $element = new PlainClass(); + $element->info = Test::valueOfInfo('createDataSet', $elementNr); + $element->first = Test::valueOfFirst($elementNr); + $element->second = $elementNr; + $data[] = $element; + }, + function(): ArrayObject { + return new ArrayObject(); + } + ) + ->testSetForeach( + function(ArrayObject $dataSet, PlainClass $element, int $elementIndex, int $repetitionNr): void { + $dataSet[$elementIndex]->info = Test::valueOfInfo('testSetForeach', $repetitionNr); + $dataSet[$elementIndex]->first = Test::valueOfFirst($repetitionNr); + $dataSet[$elementIndex]->second = $repetitionNr; + }, + ) + ->testArrayMap( + function(PlainClass $element): PlainClass { + global $repetitionNr; + $element->info = Test::valueOfInfo('testArrayMap', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second += $repetitionNr; + return $element; + }, + function(ArrayObject $data): array { + return $data->getArrayCopy(); + }, + function(array $mappedData): ArrayObject { + return new ArrayObject($mappedData); + } + ) + ->testArrayWalk( + function(PlainClass $element, int $elementIndex, int $repetitionNr): bool { + $element->info = Test::valueOfInfo('testArrayWalk', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second = $repetitionNr; + return true; + }, + function(ArrayObject $data): array { + return $data->getArrayCopy(); + } + ) + ->unsetDataSet() + ->printTestResults() +; diff --git a/src/Tests/ot-ChainPlainClass802.php b/src/Tests/ot-ChainPlainClass802.php new file mode 100644 index 0000000..3915faf --- /dev/null +++ b/src/Tests/ot-ChainPlainClass802.php @@ -0,0 +1,70 @@ +createDataSet( + function(?ChainPlainClass802 &$dataSet, int $elementNr): void { + $dataSet = new ChainPlainClass802( + Test::valueOfInfo('createDataSet', $elementNr), + Test::valueOfFirst($elementNr), + $elementNr, + $dataSet + ); + }, + function(): null { + return null; + } + ) + ->testSetForeach( + function( + ChainPlainClass802 $dataSet, + ChainPlainClass802 $element, + int $elementIndex, + int $repetitionNr + ): void { + $dataSet->getIterator()->current()->info = Test::valueOfInfo('testSetForeach', $repetitionNr); + $dataSet->getIterator()->current()->first = Test::valueOfFirst($repetitionNr); + $dataSet->getIterator()->current()->second = $repetitionNr; + }, + ) + ->testArrayMap( + function(ChainPlainClass802 $element): ChainPlainClass802 { + global $repetitionNr; + $element->info = Test::valueOfInfo('testArrayMap', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second += $repetitionNr; + return $element; + }, + function(ChainPlainClass802 $dataSet): array { + return $dataSet->getIterator()->toArray(); + }, + function(array $mappedData): ChainPlainClass802 { + return array_pop($mappedData); + } + ) + ->testArrayWalk( + function(ChainPlainClass802 $element, int $elementIndex, int $repetitionNr): bool { + $element->info = Test::valueOfInfo('testArrayWalk', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second = $repetitionNr; + return true; + }, + function(ChainPlainClass802 $dataSet): array { + return $dataSet->getIterator()->toArray(); + } + ) + ->unsetDataSet() + ->printTestResults() +; diff --git a/src/Tests/ot-DsMap-seq-of-objects-PlainClass.php b/src/Tests/ot-DsMap-seq-of-objects-PlainClass.php new file mode 100644 index 0000000..82ac1ba --- /dev/null +++ b/src/Tests/ot-DsMap-seq-of-objects-PlainClass.php @@ -0,0 +1,66 @@ +createDataSet( + function(Map $data, int $elementNr) { + $element = new PlainClass(); + $element->info = Test::valueOfInfo('createDataSet', $elementNr); + $element->first = Test::valueOfFirst($elementNr); + $element->second = $elementNr; + $data->put($elementNr, $element); + }, + function(): Map { + return new Map(); + } + ) + ->testSetForeach( + function(Map $dataSet, PlainClass $element, int $elementIndex, int $repetitionNr): void { + $dataSet[$elementIndex]->info = Test::valueOfInfo('testSetForeach', $repetitionNr); + $dataSet[$elementIndex]->first = Test::valueOfFirst($repetitionNr); + $dataSet[$elementIndex]->second = $repetitionNr; + }, + ) + ->testArrayMap( + function(PlainClass $element): PlainClass { + global $repetitionNr; + $element->info = Test::valueOfInfo('testArrayMap', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second += $repetitionNr; + return $element; + }, + function(Map $data): array { + return $data->toArray(); + }, + function(array $mappedData): Map { + return new Map($mappedData); + } + ) + ->testArrayWalk( + function(PlainClass $element, int $elementIndex, int $repetitionNr): bool { + $element->info = Test::valueOfInfo('testArrayWalk', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second = $repetitionNr; + return true; + }, + function(Map $data): array { + return $data->toArray(); + } + ) + ->unsetDataSet() + ->printTestResults() +; diff --git a/src/Tests/ot-DsMap-string-of-objects-PlainClass.php b/src/Tests/ot-DsMap-string-of-objects-PlainClass.php new file mode 100644 index 0000000..50f65bb --- /dev/null +++ b/src/Tests/ot-DsMap-string-of-objects-PlainClass.php @@ -0,0 +1,67 @@ +createDataSet( + function(Map $data, int $elementNr) { + $valueInfo = Test::valueOfInfo('createDataSet', $elementNr); + $element = new PlainClass(); + $element->info = $valueInfo; + $element->first = Test::valueOfFirst($elementNr); + $element->second = $elementNr; + $data->put($valueInfo, $element); + }, + function(): Map { + return new Map(); + } + ) + ->testSetForeach( + function(Map $dataSet, PlainClass $element, string $elementIndex, int $repetitionNr): void { + $dataSet[$elementIndex]->info = Test::valueOfInfo('testSetForeach', $repetitionNr); + $dataSet[$elementIndex]->first = Test::valueOfFirst($repetitionNr); + $dataSet[$elementIndex]->second = $repetitionNr; + }, + ) + ->testArrayMap( + function(PlainClass $element): PlainClass { + global $repetitionNr; + $element->info = Test::valueOfInfo('testArrayMap', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second += $repetitionNr; + return $element; + }, + function(Map $data): array { + return $data->toArray(); + }, + function(array $mappedData): Map { + return new Map($mappedData); + } + ) + ->testArrayWalk( + function(PlainClass $element, string $elementIndex, int $repetitionNr): bool { + $element->info = Test::valueOfInfo('testArrayWalk', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second = $repetitionNr; + return true; + }, + function(Map $data): array { + return $data->toArray(); + } + ) + ->unsetDataSet() + ->printTestResults() +; diff --git a/src/Tests/ot-DsVector-of-objects-PlainClass.php b/src/Tests/ot-DsVector-of-objects-PlainClass.php new file mode 100644 index 0000000..bbd9734 --- /dev/null +++ b/src/Tests/ot-DsVector-of-objects-PlainClass.php @@ -0,0 +1,66 @@ +createDataSet( + function(Vector $data, int $elementNr) { + $element = new PlainClass(); + $element->info = Test::valueOfInfo('createDataSet', $elementNr); + $element->first = Test::valueOfFirst($elementNr); + $element->second = $elementNr; + $data->push($element); + }, + function(): Vector { + return new Vector(); + } + ) + ->testSetForeach( + function(Vector $dataSet, PlainClass $element, int $elementIndex, int $repetitionNr): void { + $dataSet[$elementIndex]->info = Test::valueOfInfo('testSetForeach', $repetitionNr); + $dataSet[$elementIndex]->first = Test::valueOfFirst($repetitionNr); + $dataSet[$elementIndex]->second = $repetitionNr; + }, + ) + ->testArrayMap( + function(PlainClass $element): PlainClass { + global $repetitionNr; + $element->info = Test::valueOfInfo('testArrayMap', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second += $repetitionNr; + return $element; + }, + function(Vector $data): array { + return $data->toArray(); + }, + function(array $mappedData): Vector { + return new Vector($mappedData); + } + ) + ->testArrayWalk( + function(PlainClass $element, int $elementIndex, int $repetitionNr): bool { + $element->info = Test::valueOfInfo('testArrayWalk', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second = $repetitionNr; + return true; + }, + function(Vector $data): array { + return $data->toArray(); + } + ) + ->unsetDataSet() + ->printTestResults() +; diff --git a/src/Tests/ot-SplFixedArray-of-objects-PlainClass.php b/src/Tests/ot-SplFixedArray-of-objects-PlainClass.php new file mode 100644 index 0000000..09942ba --- /dev/null +++ b/src/Tests/ot-SplFixedArray-of-objects-PlainClass.php @@ -0,0 +1,67 @@ +createDataSet( + function(SplFixedArray $data, int $elementNr) { + $element = new PlainClass(); + $element->info = Test::valueOfInfo('createDataSet', $elementNr); + $element->first = Test::valueOfFirst($elementNr); + $element->second = $elementNr; + $data[$elementNr] = $element; + }, + function(): SplFixedArray { + return new SplFixedArray(TestConfig::getDataSetCount()); + } + ) + ->testSetForeach( + function(SplFixedArray $dataSet, PlainClass $element, int $elementIndex, int $repetitionNr): void { + $dataSet[$elementIndex]->info = Test::valueOfInfo('testSetForeach', $repetitionNr); + $dataSet[$elementIndex]->first = Test::valueOfFirst($repetitionNr); + $dataSet[$elementIndex]->second = $repetitionNr; + }, + ) + ->testArrayMap( + function(PlainClass $element): PlainClass { + global $repetitionNr; + $element->info = Test::valueOfInfo('testArrayMap', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second += $repetitionNr; + return $element; + }, + function(SplFixedArray $data): array { + return $data->toArray(); + }, + function(array $mappedData): SplFixedArray { + return SplFixedArray::fromArray($mappedData); + } + ) + ->testArrayWalk( + function(PlainClass $element, int $elementIndex, int $repetitionNr): bool { + $element->info = Test::valueOfInfo('testArrayWalk', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second = $repetitionNr; + return true; + }, + function(SplFixedArray $data): array { + return $data->toArray(); + } + ) + ->unsetDataSet() + ->printTestResults() +; diff --git a/src/Tests/ot-SplObjectStorage-of-objects-PlainClass.php b/src/Tests/ot-SplObjectStorage-of-objects-PlainClass.php new file mode 100644 index 0000000..1b8d91e --- /dev/null +++ b/src/Tests/ot-SplObjectStorage-of-objects-PlainClass.php @@ -0,0 +1,87 @@ +createDataSet( + function(SplObjectStorage $data, int $elementNr) { + $element = new PlainClass(); + $element->info = Test::valueOfInfo('createDataSet', $elementNr); + $element->first = Test::valueOfFirst($elementNr); + $element->second = $elementNr; + $data->attach($element); + }, + function(): SplObjectStorage { + return new SplObjectStorage(); + } + ) + ->testSetForeach( + function(SplObjectStorage $dataSet, PlainClass $element, int $elementIndex, int $repetitionNr): void { + $dataSet->current()->info = Test::valueOfInfo('testSetForeach', $repetitionNr); + $dataSet->current()->first = Test::valueOfFirst($repetitionNr); + $dataSet->current()->second = $repetitionNr; + }, + ) + ->testArrayMap( + function(PlainClass $element): PlainClass { + global $repetitionNr; + $element->info = Test::valueOfInfo('testArrayMap', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second += $repetitionNr; + return $element; + }, + function(SplObjectStorage $dataSet): array { + $dataSet->rewind(); + $data = []; + while ($dataSet->valid()) { + $data[] = $dataSet->current(); + $dataSet->next(); + } + $dataSet->rewind(); + + return $data; + }, + function(array $mappedData): SplObjectStorage { + $newDataSet = new SplObjectStorage(); + foreach ($mappedData as $element) { + $newDataSet->attach($element); + } + + return $newDataSet; + } + ) + ->testArrayWalk( + function(PlainClass $element, int $elementIndex, int $repetitionNr): bool { + $element->info = Test::valueOfInfo('testArrayWalk', $repetitionNr); + $element->first = Test::valueOfFirst($repetitionNr); + $element->second = $repetitionNr; + return true; + }, + function(SplObjectStorage $dataSet): array { + $dataSet->rewind(); + $data = []; + while ($dataSet->valid()) { + $data[] = $dataSet->current(); + $dataSet->next(); + } + $dataSet->rewind(); + + return $data; + } + ) + ->unsetDataSet() + ->printTestResults() +; diff --git a/src/Tests/ot-array-seq-of-arrays.php b/src/Tests/ot-array-seq-of-arrays.php new file mode 100644 index 0000000..577680a --- /dev/null +++ b/src/Tests/ot-array-seq-of-arrays.php @@ -0,0 +1,44 @@ +createDataSet( + function(array &$data, int $elementNr) { + $data[] = [ + Test::valueOfInfo('createDataSet', $elementNr), + Test::valueOfFirst($elementNr), + $elementNr + ]; + }, + function(): array { + return []; + } + ) + ->testSetForeach( + function(array &$dataSet, array $element, int $elementIndex, int $repetitionNr): void { + $dataSet[$elementIndex][0] = Test::valueOfInfo('testSetForeach', $repetitionNr); + $dataSet[$elementIndex][1] = Test::valueOfFirst($repetitionNr); + $dataSet[$elementIndex][2] = $repetitionNr; + }, + ) + ->testArrayMap(function(array $element): array { + global $repetitionNr; + $element[0] = Test::valueOfInfo('testArrayMap', $repetitionNr); + $element[1] = Test::valueOfFirst($repetitionNr); + $element[2] += $repetitionNr; + return $element; + }) + ->testArrayWalk(function(array &$element, int $elementIndex, int $repetitionNr): bool { + $element[0] = Test::valueOfInfo('testArrayWalk', $repetitionNr); + $element[1] = Test::valueOfFirst($repetitionNr); + $element[2] = $repetitionNr; + return true; + }) + ->unsetDataSet() + ->printTestResults() +; diff --git a/src/Tests/ot-array-string-of-arrays.php b/src/Tests/ot-array-string-of-arrays.php new file mode 100644 index 0000000..2bd903a --- /dev/null +++ b/src/Tests/ot-array-string-of-arrays.php @@ -0,0 +1,45 @@ +createDataSet( + function(array &$data, int $elementNr) { + $valueInfo = Test::valueOfInfo('createDataSet', $elementNr); + $data[$valueInfo] = [ + $valueInfo, + Test::valueOfFirst($elementNr), + $elementNr + ]; + }, + function(): array { + return []; + } + ) + ->testSetForeach( + function(array &$dataSet, array $element, string $elementIndex, int $repetitionNr): void { + $dataSet[$elementIndex][0] = Test::valueOfInfo('testSetForeach', $repetitionNr); + $dataSet[$elementIndex][1] = Test::valueOfFirst($repetitionNr); + $dataSet[$elementIndex][2] = $repetitionNr; + }, + ) + ->testArrayMap(function(array $element): array { + global $repetitionNr; + $element[0] = Test::valueOfInfo('testArrayMap', $repetitionNr); + $element[1] = Test::valueOfFirst($repetitionNr); + $element[2] += $repetitionNr; + return $element; + }) + ->testArrayWalk(function(array &$element, string $elementIndex, int $repetitionNr): bool { + $element[0] = Test::valueOfInfo('testArrayWalk', $repetitionNr); + $element[1] = Test::valueOfFirst($repetitionNr); + $element[2] = $repetitionNr; + return true; + }) + ->unsetDataSet() + ->printTestResults() +; diff --git a/src/test-obj.php b/src/test-obj.php new file mode 100644 index 0000000..59175b7 --- /dev/null +++ b/src/test-obj.php @@ -0,0 +1,36 @@ +