} } echo "Matrix 3x3:\n"; print_r(map(methodCaller('toArray'), $matrix->toArray())); // casting default array with all nested default arrays to PHP array // 3. Set example $set = set(1, 2); $set->add('hello'); $set[] = 'world'; $set->update([3, 4], ['answer', 42]); echo "Set:\n"; print_r($set->toArray()); foreach (['hello', 3, 4, 'answer', 42] as $element) { $set->delete($element); } echo "Set:\n"; print_r($set->toArray()); $array = [1, 2, 3]; $intersection = $set->intersection($array); echo "Intersection:\n"; print_r($intersection->toArray()); $anotherSet = Set::fromArray([1, 2, 3]); $difference = $set->difference($anotherSet); echo "Difference:\n"; print_r($difference->toArray()); $iterator = new \ArrayIterator([1, 2, 3]); $union = $set->union($iterator); echo "Union:\n"; print_r($union->toArray()); $isSubset = $set->isSubset([1, 2, 'hello', 'world']); $isSuperset = $set->isSuperset([1, 2]);
public function testSet() { $set = new Set(1, 2); $this->assertCount(2, $set); $this->assertTrue($set->contains(1)); $this->assertTrue($set->contains(2)); $set->add('hello'); $this->assertCount(3, $set); $this->assertTrue($set->contains('hello')); $set[] = 'world'; $this->assertCount(4, $set); $this->assertTrue($set->contains('world')); $set->update(['hello']); $this->assertCount(4, $set); $set->delete('hello'); $this->assertCount(3, $set); $this->assertFalse($set->contains('hello')); $difference = $set->difference([1, 2, 3]); $this->assertEquals(['world'], $difference->toArray()); $difference = $set->difference(Set::fromArray([1, 2, 3])); $this->assertEquals(['world'], $difference->toArray()); $intersection = $set->intersection([1, 2, 3]); $this->assertEquals([1, 2], $intersection->toArray()); $intersection = $set->intersection(Set::fromArray([1, 2, 3])); $this->assertEquals([1, 2], $intersection->toArray()); $union = $set->union([1, 2, 3]); $this->assertEquals([1, 2, 3, 'world'], $union->toArray(), '', 0, 10, true); $union = $set->union(Set::fromArray([1, 2, 3])); $this->assertEquals([1, 2, 3, 'world'], $union->toArray(), '', 0, 10, true); $this->assertTrue($set->isSubset($union)); $this->assertFalse($set->isSubset([1, 2, 3, 4])); $this->assertFalse($set->isSubset(Set::fromArray([1, 2, 3, 4]))); $this->assertTrue($set->isSuperset([1, 2])); $this->assertFalse($set->isSuperset(['hello', 'world'])); $this->assertTrue($set->isSuperset(Set::fromArray([1, 2]))); $this->assertFalse($set->isSuperset(Set::fromArray(['hello', 'world']))); $this->assertFalse($set->isEmpty()); $set->clear(); $this->assertTrue($set->isEmpty()); $set = set(1, 2); $this->assertCount(2, $set); $this->assertTrue($set->contains(1)); $this->assertTrue($set->contains(2)); $set[] = 2; $set[] = 3; $this->assertCount(3, $set); $this->assertTrue($set->contains(1)); $this->assertTrue($set->contains(2)); $this->assertTrue($set->contains(3)); $set = set(); $this->assertEquals($set->toArray(), array()); }
/** * Returns new Set object * * @return Set */ function set() { return Set::fromArray(func_get_args()); }