/**
  * @test
  */
 public function whenIteratingOverSet_ThenReturnAllUniqueItems()
 {
     $item1 = 'A';
     $item2 = 'B';
     $set = new Set();
     $set->add($item1);
     $set->add($item2);
     $values = array();
     foreach ($set as $key => $value) {
         $values[$key] = $value;
     }
     $this->assertEquals(array(0 => 'A', 1 => 'B'), $values);
 }
Ejemplo n.º 2
0
 /**
  * Unsupported method
  * @throws Exception every time
  */
 public function add($const = '')
 {
     if ($this->inC) {
         return parent::add($const);
     }
     throw new Exception("You are not allowed to modify this set");
 }
Ejemplo n.º 3
0
 public function testMember()
 {
     $set = new Set(array(1, 2, 3));
     self::assertFalse($set->member(4));
     self::assertTrue($set->member(1));
     $set->add(4);
     self::assertTrue($set->member(4));
 }
Ejemplo n.º 4
0
 public function testRemove()
 {
     $set1 = new Set();
     $set2 = new Set([100, 200]);
     $set1->add(200);
     $set1->add(300);
     $set2->retainAll($set1);
     $this->assertEquals($set1->toArray(), [200, 300]);
     $this->assertEquals($set2->toArray(), [200]);
     $this->assertFalse($set1->equals($set2));
     $this->assertFalse($set2->equals($set1));
     $set1->remove(300);
     $this->assertTrue($set1->equals($set2));
     $this->assertTrue($set2->equals($set1));
     $set1->removeAll($set2);
     $this->assertTrue($set1->isEmpty());
     $this->assertFalse($set2->isEmpty());
 }
Ejemplo n.º 5
0
 public function difference(Set $other)
 {
     $difference_set = new Set();
     foreach ($this as $item) {
         if (!$other->contains($item)) {
             $difference_set->add($item);
         }
     }
     return $difference_set;
 }
Ejemplo n.º 6
0
 /**
  * @dataProvider sampleNumbers
  */
 public function testResumesIterationAfterConvertingToArray($numbers)
 {
     $set = new Set(\Cassandra::TYPE_INT);
     foreach ($numbers as $number) {
         $set->add($number);
     }
     $this->assertEquals(1, $set->current());
     $set->next();
     $this->assertEquals(2, $set->current());
     $set->next();
     $this->assertEquals(3, $set->current());
     $set->values();
     $set->next();
     $this->assertEquals(4, $set->current());
     $set->next();
     $this->assertEquals(5, $set->current());
     $set->next();
     $this->assertEquals(6, $set->current());
 }
 /**
  * User type using a partial user type value.
  *
  * This test will ensure that the PHP driver supports the user types. This
  * test uses a partial user type where some values will not be assigned.
  *
  * @test
  * @ticket PHP-57
  */
 public function testPartialUserType()
 {
     // Alias missing from a single number
     $phone = $this->getPhoneUserType();
     $phone->set("number", "000-000-0000");
     $numbers = new Set($phone->type());
     $numbers->add($phone);
     $address = UserTypeIntegrationTest::getAddressUserType();
     $address->set("street", "123 Missing Alias Street");
     $address->set("zip", 00);
     $address->set("phone_numbers", $numbers);
     $key = $this->insertAddress($address);
     $this->assertAddressValue($this->selectAddress($key), $address);
     // Missing/NULL values during insert
     $address = UserTypeIntegrationTest::getAddressUserType();
     $address->set("street", "1 Furzeground Way");
     $key = $this->insertAddress($address);
     $address->set("zip", null);
     // Add null 'zip' to assert properly (Server will default null values)
     $address->set("phone_numbers", null);
     // Add null 'phone_numbers' (same as above)
     $this->assertAddressValue($this->selectAddress($key), $address);
 }
Ejemplo n.º 8
0
 public function testSupportsRetrievingValues()
 {
     $values = array(new Varint('1'), new Varint('2'), new Varint('3'), new Varint('4'), new Varint('5'), new Varint('6'), new Varint('7'), new Varint('8'));
     $set = new Set(\Cassandra::TYPE_VARINT);
     for ($i = 0; $i < count($values); $i++) {
         $set->add($values[$i]);
     }
     $this->assertEquals($values, $set->values());
 }
Ejemplo n.º 9
0
 private function addIf(Set $to, Set $from, callable $f)
 {
     foreach ($from as $value) {
         if ($f($value)) {
             $to->add($value);
         }
     }
 }
Ejemplo n.º 10
0
 /**
  * Power set P(S)
  * The set of all subsets of S, including the empty set and S itself.
  *
  * Example:
  *  S = {x, y, z}
  *  P(S) = {Ø, {x}, {y}, {z}, {x,y}, {x,z}, {y,z}, {x,y,z}}
  *
  * Algorithm:
  *  Setup:
  *   - n:     size of the original set
  *   - 2ⁿ:    size of the power set
  *   - A:     original set as an array with numbered indices 0 to n - 1
  *   - P(S):  power set to be created
  *
  *  Iterative loop algorithm:
  *   - Loop i from 0 to < 2ⁿ
  *    - Create empty temporary Set
  *    - Loop j from 0 to < n
  *      - If the jᵗʰ bit of the i counter is set, add A[j] to temporary Set
  *    - Add temporary set to power set
  *
  * Time complexity: O(n2ⁿ)
  * Reference: http://www.geeksforgeeks.org/power-set/
  *
  * @return Set
  */
 public function powerSet() : Set
 {
     // Setup
     $n = count($this->A);
     // Size of the original set
     $2ⁿ = pow(2, $n);
     // Size of the power set
     $A = array_values($this->A);
     //  Original set as an array with numbered indices
     $P⟮S⟯ = new Set();
     //  Power set to be created
     // Populate power set
     for ($i = 0; $i < $2ⁿ; $i++) {
         $member_set = new Set();
         for ($j = 0; $j < $n; $j++) {
             if ($i & 1 << $j) {
                 $member_set->add($A[$j]);
             }
         }
         $P⟮S⟯->add($member_set);
     }
     return $P⟮S⟯;
 }
Ejemplo n.º 11
0
 public function testFirst()
 {
     $element = $this->items['alpha'];
     $this->object->add($element);
     $this->assertEquals($element, $this->object->first());
 }
Ejemplo n.º 12
0
 /**
  * @param ILogAppender $appender
  * @return ILoggerCategory
  */
 public function addAppender(ILogAppender $appender)
 {
     $this->appenders->add($appender);
     return $this;
 }
Ejemplo n.º 13
0
 /**
  * Returns the set of values that are in this set, excluding the values that
  * are also in the other set.
  *
  * @param Api\Set|Iterator|array $set
  *
  * @return Api\Set
  */
 public function difference($set)
 {
     $differenceSet = new Set();
     foreach ($this as $value) {
         if ($set->has($value)) {
             $differenceSet->add($value);
         }
     }
     return $differenceSet;
 }