ImmutableSetTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace MathPHP\Tests\SetTheory;
  3. use MathPHP\SetTheory\ImmutableSet;
  4. use MathPHP\SetTheory\Set;
  5. class ImmutableSetTest extends \PHPUnit\Framework\TestCase
  6. {
  7. /**
  8. * @test add does nothing
  9. */
  10. public function testAddDoesNothing()
  11. {
  12. // Given
  13. $A = new ImmutableSet([1, 2, 3, 4]);
  14. $B = $A->copy();
  15. // When
  16. $A->add(5);
  17. $A->add(6);
  18. $A->add([7, 8, 9]);
  19. $A->add(new Set(['a', 'b']));
  20. // Then
  21. $this->assertEquals($B, $A);
  22. $this->assertEquals($B->asArray(), $A->asArray());
  23. }
  24. /**
  25. * @test addMulti does nothing
  26. */
  27. public function testAddMultiDoesNothing()
  28. {
  29. // Given
  30. $A = new ImmutableSet([1, 2, 3, 4]);
  31. $B = $A->copy();
  32. // When
  33. $A->addMulti([5]);
  34. $A->addMulti([6, 7, 8]);
  35. $A->addMulti([7, 8, 9]);
  36. $A->addMulti([new Set(['a', 'b'])]);
  37. // Then
  38. $this->assertEquals($B, $A);
  39. $this->assertEquals($B->asArray(), $A->asArray());
  40. }
  41. /**
  42. * @test remove does nothing
  43. */
  44. public function testRemoveDoesNothing()
  45. {
  46. // Given
  47. $A = new ImmutableSet([1, 2, 3, 4]);
  48. $B = $A->copy();
  49. // When
  50. $A->remove(1);
  51. $A->remove([2, 3]);
  52. $this->assertEquals($B, $A);
  53. $this->assertEquals($B->asArray(), $A->asArray());
  54. }
  55. /**
  56. * @test removeMulti does nothing
  57. */
  58. public function testRemoveMultiDoesNothing()
  59. {
  60. // Given
  61. $A = new ImmutableSet([1, 2, 3, 4]);
  62. $B = $A->copy();
  63. // When
  64. $A->removeMulti([1]);
  65. $A->removeMulti([2, 3]);
  66. $this->assertEquals($B, $A);
  67. $this->assertEquals($B->asArray(), $A->asArray());
  68. }
  69. /**
  70. * @test clear does nothing
  71. */
  72. public function testClearDoesNothing()
  73. {
  74. // Given
  75. $A = new ImmutableSet([1, 2, 3, 4]);
  76. $B = $A->copy();
  77. // When
  78. $A->clear();
  79. $this->assertEquals($B, $A);
  80. $this->assertEquals($B->asArray(), $A->asArray());
  81. }
  82. /**
  83. * @test set
  84. */
  85. public function testIsASet()
  86. {
  87. // Given
  88. $A = new ImmutableSet([1, 2, 3, 4]);
  89. $this->assertInstanceOf(Set::class, $A);
  90. }
  91. /**
  92. * @test acts like a set
  93. */
  94. public function testActsLikeASet()
  95. {
  96. // Given
  97. $A = new ImmutableSet([1, 2, 3]);
  98. $B = new ImmutableSet([3, 4, 5]);
  99. $this->assertEquals(3, $A->length());
  100. $this->assertEquals(3, $B->length());
  101. $this->assertFalse($A->isEmpty());
  102. $this->assertFalse($B->isEmpty());
  103. $this->assertTrue($A->isMember(1));
  104. $this->assertTrue($A->isMember(2));
  105. $this->assertTrue($A->isMember(3));
  106. $this->assertTrue($B->isMember(3));
  107. $this->assertTrue($B->isMember(4));
  108. $this->assertTrue($B->isMember(5));
  109. $this->assertTrue($A->isNotMember(10));
  110. $this->assertTrue($B->isNotMember(10));
  111. $this->assertFalse($A->isDisjoint($B));
  112. $this->assertFalse($A->isSubset($B));
  113. $this->assertFalse($A->isProperSubset($B));
  114. $this->assertFalse($A->isSuperset($B));
  115. $this->assertFalse($A->isProperSuperset($B));
  116. $this->assertEquals(new Set([1, 2, 3, 4, 5]), $A->union($B));
  117. $this->assertEquals(new Set([3]), $A->intersect($B));
  118. $this->assertEquals(new Set([1, 2]), $A->difference($B));
  119. $this->assertEquals(new Set([1, 2, 4, 5]), $A->symmetricDifference($B));
  120. $this->assertEquals(new Set([
  121. new Set([1, 3]),
  122. new Set([1, 4]),
  123. new Set([1, 5]),
  124. new Set([2, 3]),
  125. new Set([2, 4]),
  126. new Set([2, 5]),
  127. new Set([3, 3]),
  128. new Set([3, 4]),
  129. new Set([3, 5]),
  130. ]), $A->cartesianProduct($B));
  131. $this->assertEquals($A, $A->copy());
  132. $this->assertEquals('Set{1, 2, 3}', \strval($A));
  133. $this->assertEquals(3, count($A));
  134. $this->assertEquals(3, count($B));
  135. $i = 0;
  136. foreach ($A as $x) {
  137. $i++;
  138. }
  139. $this->assertEquals(3, $i);
  140. }
  141. }