Ejemplo n.º 1
0
Archivo: DSTest.php Proyecto: ihor/Nspl
 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());
 }
Ejemplo n.º 2
0
Archivo: ds.php Proyecto: ihor/Nspl
// 2. Multidimensional default array
// Note that we create nested default array with an anonymous function.
// Otherwise, default array object will be shared across all parent array fields.
$matrix = defaultarray(function () {
    return defaultarray(0);
});
for ($i = 0; $i < 3; ++$i) {
    for ($j = 0; $j < 3; ++$j) {
        ++$matrix[$i][$j];
    }
}
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]);