2017-02-03 11:58:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
|
2018-01-06 12:09:33 +00:00
|
|
|
namespace Phpml\Tests\Math;
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
|
|
|
|
use Phpml\Math\Set;
|
2017-02-03 11:58:25 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
|
2017-02-03 11:58:25 +00:00
|
|
|
class SetTest extends TestCase
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testUnion(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$union = Set::union(new Set([3, 1]), new Set([3, 2, 2]));
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(new Set([1, 2, 3]), $union);
|
|
|
|
self::assertEquals(3, $union->cardinality());
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testIntersection(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$intersection = Set::intersection(new Set(['C', 'A']), new Set(['B', 'C']));
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(new Set(['C']), $intersection);
|
|
|
|
self::assertEquals(1, $intersection->cardinality());
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testDifference(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$difference = Set::difference(new Set(['C', 'A', 'B']), new Set(['A']));
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(new Set(['B', 'C']), $difference);
|
|
|
|
self::assertEquals(2, $difference->cardinality());
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testPower(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$power = Set::power(new Set(['A', 'B']));
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals([new Set(), new Set(['A']), new Set(['B']), new Set(['A', 'B'])], $power);
|
|
|
|
self::assertCount(4, $power);
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testCartesian(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$cartesian = Set::cartesian(new Set(['A']), new Set([1, 2]));
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals([new Set(['A', 1]), new Set(['A', 2])], $cartesian);
|
|
|
|
self::assertCount(2, $cartesian);
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testContains(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$set = new Set(['B', 'A', 2, 1]);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertTrue($set->contains('B'));
|
|
|
|
self::assertTrue($set->containsAll(['A', 'B']));
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertFalse($set->contains('C'));
|
|
|
|
self::assertFalse($set->containsAll(['A', 'B', 'C']));
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testRemove(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$set = new Set(['B', 'A', 2, 1]);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals((new Set([1, 2, 2, 2, 'B']))->toArray(), $set->remove('A')->toArray());
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testAdd(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$set = new Set(['B', 'A', 2, 1]);
|
|
|
|
$set->addAll(['foo', 'bar']);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(6, $set->cardinality());
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testEmpty(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$set = new Set([1, 2]);
|
|
|
|
$set->removeAll([2, 1]);
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals(new Set(), $set);
|
|
|
|
self::assertTrue($set->isEmpty());
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-11-14 20:21:23 +00:00
|
|
|
public function testToArray(): void
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
{
|
|
|
|
$set = new Set([1, 2, 2, 3, 'A', false, '', 1.1, -1, -10, 'B']);
|
|
|
|
|
2018-10-28 06:44:52 +00:00
|
|
|
self::assertEquals([-10, '', -1, 'A', 'B', 1, 1.1, 2, 3], $set->toArray());
|
Add new class Set for simple Set-theoretical operations
### Features
* Works only with primitive types int, float, string
* Implements set theortic operations union, intersection, complement
* Modifies set by adding, removing elements
* Implements \IteratorAggregate for use in loops
### Implementation details
Based on array functions:
* array_diff,
* array_merge,
* array_intersection,
* array_unique,
* array_values,
* sort.
### Drawbacks
* **Do not work with objects.**
* Power set and Cartesian product returning array of Set
2016-09-10 11:24:43 +00:00
|
|
|
}
|
|
|
|
}
|