Skip to content

Commit 5173a73

Browse files
committed
Fix issue with Set proper subset.
1 parent 65b41c9 commit 5173a73

File tree

2 files changed

+111
-11
lines changed

2 files changed

+111
-11
lines changed

src/SetTheory/Set.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* $set = new Set([$array1, $array2]);
3838
*
3939
* The set will have only one element, because the arrays are equal.
40-
* $array2 === $array2 evaluates to true.
40+
* $array1 === $array2 evaluates to true.
4141
*
4242
* Example (different objects):
4343
* $object1 = new \StdClass();
@@ -333,7 +333,7 @@ public function isProperSubset(Set $B): bool
333333
$A∩B = \array_intersect_key($this->A, $B_array);
334334
$A∖B = \array_diff_key($this->A, $B_array);
335335

336-
return (\count($A∩B) === \count($this->A)) && (empty($A∖B)) && (\count($this->A) === \count($B));
336+
return (\count($A∩B) === \count($this->A)) && (empty($A∖B)) && ($this != $B);
337337
}
338338

339339
/**
@@ -358,7 +358,7 @@ public function isSuperset(Set $B): bool
358358
/**
359359
* Superset (A ⊇ B & A ≠ B)
360360
* Is the set a superset of the other set?
361-
* In other words, does the the set contain all the elements of the other set,
361+
* In other words, does the set contain all the elements of the other set,
362362
* and the set is not the same set as the other set?
363363
*
364364
* @param Set $B
@@ -469,10 +469,10 @@ public function symmetricDifference(Set $B): Set
469469
{
470470
$B_array = $B->asArray();
471471

472-
$AB = \array_intersect_key($this->A, $B_array);
472+
$AB = \array_intersect_key($this->A, $B_array);
473473

474-
$A∖B = \array_diff_key($this->A, $AB);
475-
$B∖A = \array_diff_key($B_array, $AB);
474+
$A∖B = \array_diff_key($this->A, $AB);
475+
$B∖A = \array_diff_key($B_array, $AB);
476476

477477
return new Set($A∖B + $B∖A);
478478
}

tests/SetTheory/SetOperationsTest.php

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,7 @@ public function dataProviderForIsNotSubset(): array
958958

959959
/**
960960
* @test
961-
* @dataProvider dataProviderForIsProperSet
961+
* @dataProvider dataProviderForIsProperSubset
962962
*/
963963
public function testIsProperSubset(array $A, array $B)
964964
{
@@ -970,21 +970,121 @@ public function testIsProperSubset(array $A, array $B)
970970
$this->assertTrue($setA->isProperSubset($setB));
971971
}
972972

973+
public function dataProviderForIsProperSubset(): array
974+
{
975+
return [
976+
[
977+
[],
978+
[1],
979+
],
980+
[
981+
[1],
982+
[1, 2],
983+
],
984+
[
985+
[1, 2],
986+
[1, 2, 3],
987+
],
988+
[
989+
[1, 3, 2],
990+
[1, 2, 3, 4],
991+
],
992+
[
993+
[1, 2,'a', 4.5, new Set([1, 2])],
994+
[1, 2, 3, 'a', 4.5, new Set([1, 2])],
995+
],
996+
[
997+
[1, 3, 'a', 4.5, new Set([1, 2])],
998+
[1, 2, 3, 'a', 4.5, new Set([1, 2])],
999+
],
1000+
[
1001+
[1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 3.5],
1002+
[1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 2.4, 3.5],
1003+
],
1004+
];
1005+
}
1006+
1007+
1008+
/**
1009+
* @test
1010+
* @dataProvider dataProviderForIsSameSets
1011+
* @dataProvider dataProviderForIsProperSuperset
1012+
*/
1013+
public function testIsNotProperSubset(array $A, array $B)
1014+
{
1015+
// Given
1016+
$setA = new Set($A);
1017+
$setB = new Set($B);
1018+
1019+
// Then
1020+
$this->assertFalse($setA->isProperSubset($setB));
1021+
}
1022+
9731023
/**
9741024
* @test
975-
* @dataProvider dataProviderForIsProperSet
1025+
* @dataProvider dataProviderForIsProperSuperset
9761026
*/
9771027
public function testIsProperSuperset(array $A, array $B)
9781028
{
9791029
// Given
980-
$setA = new Set($B);
981-
$setB = new Set($A);
1030+
$setA = new Set($A);
1031+
$setB = new Set($B);
1032+
1033+
// Then
1034+
$this->assertTrue($setA->isProperSuperset($setB));
1035+
}
1036+
1037+
public function dataProviderForIsProperSuperset(): array
1038+
{
1039+
return [
1040+
[
1041+
[1],
1042+
[],
1043+
],
1044+
[
1045+
[1, 2],
1046+
[1],
1047+
],
1048+
[
1049+
[1, 2, 3],
1050+
[1, 2],
1051+
],
1052+
[
1053+
[1, 2, 3, 4],
1054+
[1, 3, 2],
1055+
],
1056+
[
1057+
[1, 2, 3, 'a', 4.5, new Set([1, 2])],
1058+
[1, 2,'a', 4.5, new Set([1, 2])],
1059+
],
1060+
[
1061+
[1, 2, 3, 'a', 4.5, new Set([1, 2])],
1062+
[1, 3, 'a', 4.5, new Set([1, 2])],
1063+
],
1064+
[
1065+
[1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 2.4, 3.5],
1066+
[1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 3.5],
1067+
],
1068+
];
1069+
}
1070+
1071+
1072+
/**
1073+
* @test
1074+
* @dataProvider dataProviderForIsSameSets
1075+
* @dataProvider dataProviderForIsProperSubset
1076+
*/
1077+
public function testIsNotProperSuperset(array $A, array $B)
1078+
{
1079+
// Given
1080+
$setA = new Set($A);
1081+
$setB = new Set($B);
9821082

9831083
// Then
9841084
$this->assertFalse($setA->isProperSuperset($setB));
9851085
}
9861086

987-
public function dataProviderForIsProperSet(): array
1087+
public function dataProviderForIsSameSets(): array
9881088
{
9891089
return [
9901090
[

0 commit comments

Comments
 (0)