Skip to content

Commit 6af8018

Browse files
authored
Merge pull request #64 from marc-mabe/enumset_isequal_issubset_issuperset
EnumSet: added isEqual(), isSubset() and isSuperset()
2 parents 86417b6 + 065579a commit 6af8018

File tree

2 files changed

+177
-0
lines changed

2 files changed

+177
-0
lines changed

src/EnumSet.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,47 @@ public function count()
199199
return $cnt;
200200
}
201201

202+
/**
203+
* Check if this EnumSet is the same as other
204+
* @param EnumSet $other
205+
* @return bool
206+
*/
207+
public function isEqual(EnumSet $other)
208+
{
209+
return $this->getEnumeration() === $other->getEnumeration()
210+
&& $this->getBinaryBitsetLe() === $other->getBinaryBitsetLe();
211+
}
212+
213+
/**
214+
* Check if this EnumSet is a subset of other
215+
* @param EnumSet $other
216+
* @return bool
217+
*/
218+
public function isSubset(EnumSet $other)
219+
{
220+
if ($this->getEnumeration() !== $other->getEnumeration()) {
221+
return false;
222+
}
223+
224+
$thisBitset = $this->getBinaryBitsetLe();
225+
return ($thisBitset & $other->getBinaryBitsetLe()) === $thisBitset;
226+
}
227+
228+
/**
229+
* Check if this EnumSet is a superset of other
230+
* @param EnumSet $other
231+
* @return bool
232+
*/
233+
public function isSuperset(EnumSet $other)
234+
{
235+
if ($this->getEnumeration() !== $other->getEnumeration()) {
236+
return false;
237+
}
238+
239+
$thisBitset = $this->getBinaryBitsetLe();
240+
return ($thisBitset | $other->getBinaryBitsetLe()) === $thisBitset;
241+
}
242+
202243
/**
203244
* Get binary bitset in little-endian order
204245
*

tests/MabeEnumTest/EnumSetTest.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,4 +430,140 @@ public function testCountingEmptyEnumEmptySet()
430430
$set = new EnumSet('MabeEnumTest\TestAsset\EmptyEnum');
431431
$this->assertSame(0, $set->count());
432432
}
433+
434+
public function testIsEqual()
435+
{
436+
$set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
437+
$set2 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
438+
$this->assertTrue($set1->isEqual($set2));
439+
440+
foreach (EnumBasic::getEnumerators() as $enumerator) {
441+
$set1->attach($enumerator);
442+
$this->assertFalse($set1->isEqual($set2));
443+
444+
$set2->attach($enumerator);
445+
$this->assertTrue($set1->isEqual($set2));
446+
}
447+
}
448+
449+
public function testIsEqualWrongInstance()
450+
{
451+
$set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
452+
$set2 = new EnumSet('MabeEnumTest\TestAsset\EnumInheritance');
453+
$this->assertFalse($set1->isEqual($set2));
454+
455+
foreach (EnumBasic::getEnumerators() as $enumerator) {
456+
$set1->attach($enumerator);
457+
$this->assertFalse($set1->isEqual($set2));
458+
459+
$set2->attach($enumerator->getValue());
460+
$this->assertFalse($set1->isEqual($set2));
461+
}
462+
}
463+
464+
/**
465+
* if $A->isEqual($B) is true then $A->isSubsetOf($B) is also true
466+
*/
467+
public function testIsSubsetEqual()
468+
{
469+
$set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
470+
$set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
471+
$this->assertTrue($set1->isSubset($set2));
472+
473+
foreach (Enum32::getEnumerators() as $enumerator) {
474+
$set1->attach($enumerator);
475+
$set2->attach($enumerator);
476+
$this->assertTrue($set1->isSubset($set2));
477+
}
478+
}
479+
480+
public function testIsSubsetFull()
481+
{
482+
$set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
483+
$set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
484+
485+
foreach (Enum32::getEnumerators() as $enumerator) {
486+
$set2->attach($enumerator);
487+
$this->assertTrue($set1->isSubset($set2));
488+
}
489+
}
490+
491+
public function testIsSubsetFalse()
492+
{
493+
$set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
494+
$set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
495+
496+
foreach (Enum32::getEnumerators() as $enumerator) {
497+
$set1->attach($enumerator);
498+
$this->assertFalse($set1->isSubset($set2));
499+
}
500+
}
501+
502+
public function testIsSubsetWrongInstance()
503+
{
504+
$set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
505+
$set2 = new EnumSet('MabeEnumTest\TestAsset\EnumInheritance');
506+
$this->assertFalse($set1->isSubset($set2));
507+
508+
foreach (EnumBasic::getEnumerators() as $enumerator) {
509+
$set1->attach($enumerator);
510+
$this->assertFalse($set1->isSubset($set2));
511+
512+
$set2->attach($enumerator->getValue());
513+
$this->assertFalse($set1->isSubset($set2));
514+
}
515+
}
516+
517+
/**
518+
* if $A->isEqual($B) is true then $A->isSuperset($B) is also true
519+
*/
520+
public function testIsSsetEqual()
521+
{
522+
$set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
523+
$set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
524+
$this->assertTrue($set1->isSuperset($set2));
525+
526+
foreach (Enum32::getEnumerators() as $enumerator) {
527+
$set1->attach($enumerator);
528+
$set2->attach($enumerator);
529+
$this->assertTrue($set1->isSuperset($set2));
530+
}
531+
}
532+
533+
public function testIsSupersetFull()
534+
{
535+
$set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
536+
$set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
537+
538+
foreach (Enum32::getEnumerators() as $enumerator) {
539+
$set1->attach($enumerator);
540+
$this->assertTrue($set1->isSuperset($set2));
541+
}
542+
}
543+
544+
public function testIsSupersetFalse()
545+
{
546+
$set1 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
547+
$set2 = new EnumSet('MabeEnumTest\TestAsset\Enum32');
548+
549+
foreach (Enum32::getEnumerators() as $enumerator) {
550+
$set2->attach($enumerator);
551+
$this->assertFalse($set1->isSuperset($set2));
552+
}
553+
}
554+
555+
public function testIsSupersetWrongInstance()
556+
{
557+
$set1 = new EnumSet('MabeEnumTest\TestAsset\EnumBasic');
558+
$set2 = new EnumSet('MabeEnumTest\TestAsset\EnumInheritance');
559+
$this->assertFalse($set1->isSuperset($set2));
560+
561+
foreach (EnumBasic::getEnumerators() as $enumerator) {
562+
$set1->attach($enumerator);
563+
$this->assertFalse($set1->isSuperset($set2));
564+
565+
$set2->attach($enumerator->getValue());
566+
$this->assertFalse($set1->isSuperset($set2));
567+
}
568+
}
433569
}

0 commit comments

Comments
 (0)