123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214 |
- <?php
- namespace MathPHP\Tests\SetTheory;
- use MathPHP\SetTheory\Set;
- use MathPHP\LinearAlgebra\Vector;
- use MathPHP\LinearAlgebra\NumericMatrix;
- class SetOperationsTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @test
- * @dataProvider dataProviderForAdd
- */
- public function testAdd(array $A, $x, array $R)
- {
- // Given
- $setA = new Set($A);
- $setR = new Set($R);
- // When
- $setA->add($x);
- // Then
- $this->assertEquals($setR, $setA);
- $this->assertEquals($setR->asArray(), $setA->asArray());
- }
- /**
- * @test
- * @dataProvider dataProviderForAdd
- */
- public function testAddTwiceDoesNothing(array $A, $x, array $R)
- {
- // Given
- $setA = new Set($A);
- $setR = new Set($R);
- // When
- $setA->add($x);
- $setA->add($x);
- // Then
- $this->assertEquals($setR, $setA);
- }
- public function dataProviderForAdd(): array
- {
- $vector = new Vector([1, 2, 3]);
- return [
- [
- [],
- null,
- [null],
- ],
- [
- [],
- new Set(),
- ['Ø' => new Set()],
- ],
- [
- [],
- 1,
- [1 => 1],
- ],
- [
- [1, 2, 3],
- 4,
- [1, 2, 3, 4],
- ],
- [
- [1, 2, 3],
- 1,
- [1, 2, 3],
- ],
- [
- [1, 2, 3],
- 'new',
- [1, 2, 3, 'new'],
- ],
- [
- [1, 2, 3],
- 3.1,
- [1, 2, 3, 3.1],
- ],
- [
- [1, 2, 3],
- new Set(),
- [1, 2, 3, 'Ø'],
- ],
- [
- [1, 2, 3],
- new Set([4, 5]),
- [1, 2, 3, 'Set{4, 5}'],
- ],
- [
- [1, 2, 3],
- new Set([1, 2]),
- [1, 2, 3, 'Set{1, 2}'],
- ],
- [
- [1, 2, 3],
- -3,
- [1, 2, 3, -3],
- ],
- [
- [1, 2, 3],
- $vector,
- [1, 2, 3, $vector],
- ],
- ];
- }
- /**
- * When adding objects to a set, the key becomes to the objects hash.
- * The object is stored as is as the value.
- */
- public function testAddWithObjects()
- {
- // Given
- $set = new Set([1, 2, 3]);
- $vector = new Vector([1, 2, 3]);
- $matrix = new NumericMatrix([[1,2,3],[2,3,4]]);
- // When
- $set->add($vector);
- $set->add($matrix);
- // Then
- $this->assertEquals(5, count($set));
- $this->assertEquals(5, count($set->asArray()));
- $objects = 0;
- foreach ($set as $key => $value) {
- if ($value instanceof \MathPHP\LinearAlgebra\Vector) {
- $objects++;
- $vector_key = \get_class($value) . '(' . spl_object_hash($vector) . ')';
- $this->assertEquals($vector_key, $key);
- $this->assertEquals($vector, $value);
- }
- if ($value instanceof \MathPHP\LinearAlgebra\NumericMatrix) {
- $objects++;
- $matrix_key = \get_class($value) . '(' . spl_object_hash($matrix) . ')';
- $this->assertEquals($matrix_key, $key);
- $this->assertEquals($matrix, $value);
- }
- }
- // There should have been two objects (vector and matrix)
- $this->assertEquals(2, $objects);
- }
- public function testAddWithMultipleObjects()
- {
- // Given
- $set = new Set([1, 2, 3]);
- $vector1 = new Vector([1, 2, 3]);
- $vector2 = new Vector([1, 2, 3]);
- $vector3 = new Vector([4, 5, 6]);
- $matrix = new NumericMatrix([[1,2,3],[2,3,4]]);
- $std1 = new \StdClass();
- $std2 = new \StdClass();
- $std3 = $std2; // Same object so this wont get added
- // When
- $set->add($vector1);
- $set->add($vector2);
- $set->add($vector3);
- $set->add($matrix);
- $set->add($std1);
- $set->add($std2);
- $set->add($std3);
- // Then
- $this->assertEquals(9, count($set));
- $this->assertEquals(9, count($set->asArray()));
- $objects = 0;
- foreach ($set as $key => $value) {
- if ($value instanceof \MathPHP\LinearAlgebra\Vector) {
- $objects++;
- $this->assertInstanceOf(\MathPHP\LinearAlgebra\Vector::class, $value);
- }
- if ($value instanceof \MathPHP\LinearAlgebra\NumericMatrix) {
- $objects++;
- $this->assertInstanceOf(\MathPHP\LinearAlgebra\NumericMatrix::class, $value);
- }
- if ($value instanceof \StdClass) {
- $objects++;
- $this->assertInstanceOf(\StdClass::class, $value);
- }
- }
- // There should have been four objects (3 vectors and 1 matrix)
- $this->assertEquals(6, $objects);
- }
- public function testAddWithDuplicateObjects()
- {
- // Given
- $set = new Set([1, 2, 3]);
- $vector = new Vector([1, 2, 3]);
- // When adding the same object twice.
- $set->add($vector);
- $set->add($vector);
- // Then
- $this->assertEquals(4, count($set));
- $this->assertEquals(4, count($set->asArray()));
- $objects = 0;
- foreach ($set as $key => $value) {
- if ($value instanceof \MathPHP\LinearAlgebra\Vector) {
- $objects++;
- $vector_key = \get_class($value) . '(' . spl_object_hash($vector) . ')';
- $this->assertEquals($vector_key, $key);
- $this->assertEquals($vector, $value);
- }
- }
- // There should have only been one vector object.
- $this->assertEquals(1, $objects);
- }
- /**
- * In this case, we add an array that contains arrays.
- * So each array element will be added, but with the implementation
- * detail that they will be converted into ArrayObjects.
- */
- public function testAddMultiWithArrayOfArrays()
- {
- // Given
- $set = new Set([1, 2, 3]);
- $array = [4, 5, [1, 2, 3]];
- // When
- $set->addMulti($array);
- // Then
- $this->assertEquals(6, count($set));
- $this->assertEquals(6, count($set->asArray()));
- $arrays = 0;
- foreach ($set as $key => $value) {
- if (\is_array($value)) {
- $arrays++;
- $this->assertEquals([1, 2, 3], $value);
- $this->assertEquals(3, count($value));
- $this->assertEquals(1, $value[0]);
- $this->assertEquals(1, $value[0]);
- $this->assertEquals(1, $value[0]);
- }
- }
- // There should have only been one array.
- $this->assertEquals(1, $arrays);
- }
- /**
- * In this case, we add an array that contains arrays.
- * So each array element will be added, but with the implementation
- * detail that they will be converted into ArrayObjects.
- */
- public function testAddMultiWithArrayOfArraysMultipleArraysAndDuplicates()
- {
- // Given
- $set = new Set([1, 2, 3]);
- $array = [4, 5, [1, 2, 3], [1, 2, 3], [5, 5, 5]];
- // When
- $set->addMulti($array);
- // Then, only 7, because [1, 2, 3] was in there twice.
- $this->assertEquals(7, count($set));
- $this->assertEquals(7, count($set->asArray()));
- $arrays = 0;
- foreach ($set as $key => $value) {
- if (\is_array($value)) {
- $arrays++;
- $this->assertEquals(3, count($value));
- }
- }
- // There should have been 2 arrays.
- $this->assertEquals(2, $arrays);
- }
- /**
- * When adding resources to a set, the key becomes to the resource ID.
- * The resource is stored as is as the value.
- */
- public function testAddWithResources()
- {
- // Given
- $set = new Set();
- $fh = fopen(__FILE__, 'r');
- // When
- $set->add($fh);
- $set->add($fh); // Should only get added once
- // Then
- $this->assertEquals(1, count($set));
- $this->assertEquals(1, count($set->asArray()));
- $resources = 0;
- foreach ($set as $key => $value) {
- if (\is_resource($value)) {
- $resources++;
- $vector_key = 'Resource(' . \strval($value) . ')';
- $this->assertEquals($vector_key, $key);
- $this->assertEquals($fh, $value);
- }
- }
- // There should have been one resource
- $this->assertEquals(1, $resources);
- }
- /**
- * @test
- * @dataProvider dataProviderForAddMulti
- */
- public function testAddMulti(array $A, array $x, array $R)
- {
- // Given
- $setA = new Set($A);
- $setR = new Set($R);
- // When
- $setA->addMulti($x);
- // Then
- $this->assertEquals($setR, $setA);
- $this->assertEquals($setR->asArray(), $setA->asArray());
- }
- public function dataProviderForAddMulti(): array
- {
- $vector = new Vector([1, 2, 3]);
- return [
- [
- [],
- [1],
- [1],
- ],
- [
- [],
- [1, 2],
- [1, 2],
- ],
- [
- [1, 2, 3],
- [],
- [1, 2, 3],
- ],
- [
- [1, 2, 3],
- [4],
- [1, 2, 3, 4],
- ],
- [
- [1, 2, 3],
- [4, 5],
- [1, 2, 3, 4, 5],
- ],
- [
- [1, 2, 3],
- [4, 5, 6],
- [1, 2, 3, 4, 5, 6],
- ],
- [
- [1, 2, 3],
- [1, 2, 3],
- [1, 2, 3],
- ],
- [
- [1, 2, 3],
- [1, 2, 3, 4],
- [1, 2, 3, 4],
- ],
- [
- [1, 2, 3],
- ['new', 4],
- [1, 2, 3, 'new', 4],
- ],
- [
- [1, 2, 3],
- [3.1, 4],
- [1, 2, 3, 3.1, 4],
- ],
- [
- [1, 2, 3],
- [new Set()],
- [1, 2, 3, 'Ø'],
- ],
- [
- [1, 2, 3],
- [new Set([4, 5])],
- [1, 2, 3, 'Set{4, 5}'],
- ],
- [
- [1, 2, 3],
- [new Set([1, 2]), 4],
- [1, 2, 3, 'Set{1, 2}', 4],
- ],
- [
- [1, 2, 3],
- [new Set([1, 2]), 6, 7, new Set([1, 2]), new Set([3, 4])],
- [1, 2, 3, 'Set{1, 2}', 6, 7, 'Set{3, 4}'],
- ],
- [
- [1, 2, 3],
- [-3],
- [1, 2, 3, -3],
- ],
- [
- [1, 2, 3],
- [4, $vector],
- [1, 2, 3, 4, $vector],
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForRemove
- */
- public function testRemove(array $A, $x, array $R)
- {
- // Given
- $setA = new Set($A);
- $setR = new Set($R);
- // When
- $setA->remove($x);
- // Then
- $this->assertEquals($setR, $setA);
- }
- public function dataProviderForRemove(): array
- {
- $vector = new Vector([1, 2, 3]);
- $fh = fopen(__FILE__, 'r');
- return [
- [
- [],
- null,
- [],
- ],
- [
- [null],
- null,
- [],
- ],
- [
- [1],
- 1,
- [],
- ],
- [
- [1, 2, 3],
- 1,
- [2, 3],
- ],
- [
- [1, 2, 3],
- 2,
- [1, 3],
- ],
- [
- [1, 2, 3],
- 3,
- [1, 2],
- ],
- [
- [1, 2, 3],
- 5,
- [1, 2, 3],
- ],
- [
- [1, 2, 3],
- -1,
- [1, 2, 3],
- ],
- [
- [1, 2, 3, -3],
- -3,
- [1, 2, 3],
- ],
- [
- [1, 2, 3, 4.5, 6.7],
- 4.5,
- [1, 2, 3, 6.7],
- ],
- [
- [1, 2, 3, 'a', 'b', 'see'],
- 'b',
- [1, 2, 3, 'a', 'see'],
- ],
- [
- [1, 2, 3, new Set([1, 2])],
- 1,
- [2, 3, 'Set{1, 2}'],
- ],
- [
- [1, 2, 3, new Set([1, 2])],
- new Set([1, 2]),
- [1, 2, 3],
- ],
- [
- [1, 2, 3, new Set([1, 2])],
- 'Set{1, 2}',
- [1, 2, 3],
- ],
- [
- [1, 2, 3, [1, 2, 3]],
- [1, 2, 3],
- [1, 2, 3],
- ],
- [
- [1, 2, 3, [1, 2, 3], [2, 3], [4, 5, 6]],
- [2, 3],
- [1, 2, 3, [1, 2, 3], [4, 5, 6]],
- ],
- [
- [1, 2, 3, [1, 2, 3], [2, 3], [4, 5, 6]],
- [4, 5, 6],
- [1, 2, 3, [1, 2, 3], [2, 3]],
- ],
- [
- [1, 2, 3, [1, 2, 3]],
- [6, 7, 3],
- [1, 2, 3, [1, 2, 3]],
- ],
- [
- [1, 2, 3, $vector],
- $vector,
- [1, 2, 3],
- ],
- [
- [1, 2, 3, $vector],
- [$vector], // Array containing vector
- [1, 2, 3, $vector],
- ],
- [
- [1, 2, 3, $vector],
- [1, $vector], // array containing 1 and vector
- [1, 2, 3, $vector],
- ],
- [
- [1, 2, 3, $fh],
- $fh,
- [1, 2, 3],
- ],
- [
- [1, 2, 3, $fh],
- [1, $fh], // array containing 1 and f1
- [1, 2, 3, $fh],
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForRemoveMulti
- */
- public function testRemoveMulti(array $A, array $x, array $R)
- {
- // Given
- $setA = new Set($A);
- $setR = new Set($R);
- // When
- $setA->removeMulti($x);
- // Then
- $this->assertEquals($setR, $setA);
- }
- public function dataProviderForRemoveMulti(): array
- {
- $vector = new Vector([1, 2, 3]);
- $fh = fopen(__FILE__, 'r');
- return [
- [
- [],
- [],
- [],
- ],
- [
- [],
- [null],
- [],
- ],
- [
- [null],
- [null],
- [],
- ],
- [
- [1],
- [1],
- [],
- ],
- [
- [1],
- [1],
- [],
- ],
- [
- [1, 2, 3],
- [1],
- [2, 3],
- ],
- [
- [1, 2, 3],
- [2],
- [1, 3],
- ],
- [
- [1, 2, 3],
- [3],
- [1, 2],
- ],
- [
- [1, 2, 3],
- [4],
- [1, 2, 3],
- ],
- [
- [1, 2, 3],
- [5, 6],
- [1, 2, 3],
- ],
- [
- [1, 2, 3],
- [3, 4, 5],
- [1, 2],
- ],
- [
- [1, 2, 3],
- [1, 2],
- [3],
- ],
- [
- [1, 2, 3],
- [2, 3],
- [1],
- ],
- [
- [1, 2, 3],
- [1, 3],
- [2],
- ],
- [
- [1, 2, 3],
- [5, 'a'],
- [1, 2, 3],
- ],
- [
- [1, 2, 3],
- [-1],
- [1, 2, 3],
- ],
- [
- [1, 2, 3, -3],
- [-3],
- [1, 2, 3],
- ],
- [
- [1, 2, 3, 4.5, 6.7],
- [4.5, 10],
- [1, 2, 3, 6.7],
- ],
- [
- [1, 2, 3, 'a', 'b', 'see'],
- ['b', 'z'],
- [1, 2, 3, 'a', 'see'],
- ],
- [
- [1, 2, 3, 'a', 'b', 'see'],
- ['b', 1, 'see', 5555],
- [ 2, 3, 'a'],
- ],
- [
- [1, 2, 3, new Set([1, 2])],
- [1],
- [2, 3, 'Set{1, 2}'],
- ],
- [
- [1, 2, 3, new Set([1, 2])],
- [new Set([1, 2])],
- [1, 2, 3],
- ],
- [
- [1, 2, 3, new Set([1, 2])],
- ['Set{1, 2}'],
- [1, 2, 3],
- ],
- [
- [1, 2, 3, [1, 2, 3]],
- [1, 2, 3],
- [[1, 2, 3]],
- ],
- [
- [1, 2, 3, [1, 2, 3], [2, 3], [4, 5, 6]],
- [2, 3],
- [1, [1, 2, 3], [2, 3], [4, 5, 6]],
- ],
- [
- [1, 2, 3, [1, 2, 3], [2, 3], [4, 5, 6]],
- [4, 5, 6],
- [1, 2, 3, [1, 2, 3], [2, 3], [4, 5, 6]],
- ],
- [
- [1, 2, 3, [1, 2, 3]],
- [[1, 2, 3]],
- [1, 2, 3],
- ],
- [
- [1, 2, 3, [1, 2, 3], [2, 3], [4, 5, 6]],
- [[2, 3]],
- [1, 2, 3, [1, 2, 3], [4, 5, 6]],
- ],
- [
- [1, 2, 3, [1, 2, 3], [2, 3], [4, 5, 6]],
- [[4, 5, 6]],
- [1, 2, 3, [1, 2, 3], [2, 3]],
- ],
- [
- [1, 2, 3, [1, 2, 3], [2, 3], [4, 5, 6]],
- [[1, 2, 3], [4, 5, 6]],
- [1, 2, 3, [2, 3]],
- ],
- [
- [1, 2, 3, [1, 2, 3], [2, 3], [4, 5, 6]],
- [1, [4, 5, 6], 3],
- [2, [1, 2, 3], [2, 3]],
- ],
- [
- [1, 2, 3, $vector],
- [$vector, 9],
- [1, 2, 3],
- ],
- [
- [1, 2, 3, $vector],
- [$vector],
- [1, 2, 3],
- ],
- [
- [1, 2, 3, $vector],
- [1, $vector],
- [2, 3],
- ],
- [
- [1, 2, 3, $fh],
- [1, $fh],
- [2, 3],
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForIsDisjoint
- */
- public function testIsDisjoint(array $A, array $B)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- // Then
- $this->assertTrue($setA->isDisjoint($setB));
- }
- public function dataProviderForIsDisjoint(): array
- {
- return [
- [
- [],
- [2],
- ],
- [
- [1],
- [],
- ],
- [
- [1],
- [2],
- ],
- [
- [1, 2, 3],
- [4, 5, 6],
- ],
- [
- [1, 2, 3,],
- [1.1, 2.2, -3],
- ],
- [
- [1, 2, 3, 'a', 'b'],
- [4, 5, 6, 'c', 'd'],
- ],
- [
- [1, 2, 3, new Set([1, 2])],
- [4, 5, 6],
- ],
- [
- [1, 2, 3, new Set([1, 2])],
- [4, 5, 6, new Set([2, 3])],
- ],
- [
- [new Set([1, 2])],
- [new Set([2, 3])],
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForNotDisjoint
- */
- public function testNotDisjoint(array $A, array $B)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- // Then
- $this->assertFalse($setA->isDisjoint($setB));
- }
- public function dataProviderForNotDisjoint(): array
- {
- return [
- [
- [1],
- [1],
- ],
- [
- [new Set()],
- [new Set()],
- ],
- [
- [new Set([1, 2])],
- [new Set([1, 2])],
- ],
- [
- [1, 2, 3],
- [3, 4, 5],
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForIsSubsetSuperset
- */
- public function testIsSubset(array $A, array $B)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- // Then
- $this->assertTrue($setA->isSubset($setB));
- }
- public function dataProviderForIsSubsetSuperset(): array
- {
- return [
- [
- [],
- [1],
- ],
- [
- [1],
- [1],
- ],
- [
- [1, 2],
- [1, 2],
- ],
- [
- [1, 2],
- [1, 2, 3],
- ],
- [
- [1, 2, 'a'],
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- ],
- [
- [1, 2, 'a', new Set([1, 2])],
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- ],
- [
- [1, 2, 'a', new Set([1, 2]), -1, 2.4],
- [1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 2.4, 3.5],
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForIsNotSubset
- */
- public function testIsNotSubset(array $A, array $B)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- // Then
- $this->assertFalse($setA->isSubset($setB));
- }
- public function dataProviderForIsNotSubset(): array
- {
- return [
- [
- [1],
- [],
- ],
- [
- [1, 2],
- [1],
- ],
- [
- [1, 2, 3],
- [1, 2],
- ],
- [
- [1, 2, 3, 4],
- [1, 2, 3],
- ],
- [
- [1, 2, 'b'],
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- ],
- [
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- [1, 2, 'a', new Set([1, 2])],
- ],
- [
- [1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 2.4, 3.5],
- [1, 2, 'a', new Set([1, 2]), -1, 2.4],
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForIsProperSet
- */
- public function testIsProperSubset(array $A, array $B)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- // Then
- $this->assertTrue($setA->isProperSubset($setB));
- }
- /**
- * @test
- * @dataProvider dataProviderForIsProperSet
- */
- public function testIsProperSuperset(array $A, array $B)
- {
- // Given
- $setA = new Set($B);
- $setB = new Set($A);
- // Then
- $this->assertFalse($setA->isProperSuperset($setB));
- }
- public function dataProviderForIsProperSet(): array
- {
- return [
- [
- [],
- [],
- ],
- [
- [1],
- [1],
- ],
- [
- [1, 2],
- [1, 2],
- ],
- [
- [1, 3, 2],
- [1, 2, 3],
- ],
- [
- [1, 2,'a', 3, 4.5, new Set([1, 2])],
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- ],
- [
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- ],
- [
- [1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 2.4, 3.5],
- [1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 2.4, 3.5],
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForIsSubsetSuperset
- */
- public function testIsSuperset(array $A, array $B)
- {
- // Given
- $setA = new Set($B);
- $setB = new Set($A);
- // Then
- $this->assertTrue($setA->isSuperset($setB));
- }
- /**
- * @test
- * @dataProvider dataProviderForUnion
- */
- public function testUnion(array $A, array $B, array $A∪B, Set $R)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- $expected = new Set($A∪B);
- // When
- $union = $setA->union($setB);
- $union_array = $union->asArray();
- // Then
- $this->assertEquals($R, $union);
- $this->assertEquals($expected, $union);
- $this->assertEquals(count($A∪B), count($union));
- foreach ($A∪B as $member) {
- $this->assertArrayHasKey("$member", $union_array);
- }
- foreach ($A∪B as $_ => $value) {
- if ($value instanceof Set) {
- $this->assertEquals($value, $union_array["$value"]);
- } else {
- $this->assertArrayHasKey((string) $value, $union_array);
- }
- }
- }
- public function dataProviderForUnion(): array
- {
- $setOneTwo = new Set([1, 2]);
- return [
- [
- [],
- [],
- [],
- new Set(),
- ],
- [
- [1],
- [],
- [1],
- new Set([1]),
- ],
- [
- [],
- [1],
- [1],
- new Set([1]),
- ],
- [
- [1],
- [1],
- [1],
- new Set([1]),
- ],
- [
- [1],
- [2],
- [1, 2],
- new Set([1, 2]),
- ],
- [
- [2],
- [1],
- [1, 2],
- new Set([1, 2]),
- ],
- [
- [1],
- [2],
- [2, 1],
- new Set([1, 2]),
- ],
- [
- [2],
- [1],
- [2, 1],
- new Set([1, 2]),
- ],
- [
- [1, 2, 3, 'a', 'b'],
- [1, 'a', 'k'],
- [1, 2, 3, 'a', 'b', 'k'],
- new Set([1, 2, 3, 'a', 'b', 'k']),
- ],
- [
- [1, 2, 3, 'a', 'b', $setOneTwo],
- [1, 'a', 'k'],
- [1, 2, 3, 'a', 'b', 'k', $setOneTwo],
- new Set([1, 2, 3, 'a', 'b', 'k', $setOneTwo]),
- ],
- [
- [1, 2, 3, 'a', 'b'],
- [1, 'a', 'k', $setOneTwo],
- [1, 2, 3, 'a', 'b', 'k', $setOneTwo],
- new Set([1, 2, 3, 'a', 'b', 'k', $setOneTwo]),
- ],
- [
- [1, 2, 3, 'a', 'b', new Set()],
- [1, 'a', 'k', $setOneTwo],
- [1, 2, 3, 'a', 'b', 'k', $setOneTwo, new Set()],
- new Set([1, 2, 3, 'a', 'b', 'k', $setOneTwo, new Set()]),
- ],
- [
- [1, 2, 3, 'a', 'b', $setOneTwo],
- [1, 'a', 'k', -2, '2.4', 3.5, $setOneTwo],
- [1, 2, 3, 'a', 'b', 'k', -2, '2.4', 3.5, $setOneTwo],
- new Set([1, 2, 3, 'a', 'b', 'k', -2, '2.4', 3.5, $setOneTwo]),
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForUnionMultipleSets
- */
- public function testUnionMultipleSets(array $A, array $B, array $C, array $A∪B∪C, Set $R)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- $setC = new Set($C);
- $expected = new Set($A∪B∪C);
- // When
- $union = $setA->union($setB, $setC);
- $union_array = $union->asArray();
- // Then
- $this->assertEquals($R, $union);
- $this->assertEquals($expected, $union);
- $this->assertEquals(count($A∪B∪C), count($union));
- foreach ($A∪B∪C as $member) {
- $this->assertArrayHasKey("$member", $union_array);
- }
- foreach ($A∪B∪C as $_ => $value) {
- if ($value instanceof Set) {
- $this->assertEquals($value, $union_array["$value"]);
- } else {
- $this->assertArrayHasKey((string) $value, $union_array);
- }
- }
- }
- public function dataProviderForUnionMultipleSets(): array
- {
- return [
- [
- [1, 2, 3],
- [2, 3, 4],
- [3, 4, 5],
- [1, 2, 3, 4, 5],
- new Set([1, 2, 3, 4, 5]),
- ],
- [
- [1, 2, 3, -3, 3.4],
- [2, 3, 4, new Set()],
- [3, 4, 5, new Set([1, 2])],
- [1, 2, 3, 4, 5, -3, 3.4, new Set(), new Set([1, 2])],
- new Set([1, 2, 3, 4, 5, -3, 3.4, new Set(), new Set([1, 2])]),
- ],
- ];
- }
- public function testUnionWithArrays()
- {
- // Given
- $A = new Set([1, 2, [1, 2, 3]]);
- $B = new Set([2, 3, [2, 3, 4]]);
- $expected = new Set([1, 2, [1, 2, 3], 3, [2, 3, 4]]);
- // When
- $A∪B = $A->union($B);
- // Then
- $this->assertEquals($expected, $A∪B);
- $this->assertEquals($expected->asArray(), $A∪B->asArray());
- }
- public function testUnionWithArrays2()
- {
- // Given
- $A = new Set([1, 2, [1, 2, 3]]);
- $B = new Set([2, 3, [2, 3, 4], [1, 2, 3]]);
- $expected = new Set([1, 2, [1, 2, 3], 3, [2, 3, 4]]);
- // When
- $A∪B = $A->union($B);
- // Then
- $this->assertEquals($expected, $A∪B);
- $this->assertEquals($expected->asArray(), $A∪B->asArray());
- }
- public function testUnionWithObjects()
- {
- // Given
- $vector1 = new Vector([1, 2, 3]);
- $vector2 = new Vector([1, 2, 3]);
- $A = new Set([1, 2, $vector1]);
- $B = new Set([2, 3, $vector2]);
- $expected = new Set([1, 2, $vector1, 3, $vector2]);
- // When
- $A∪B = $A->union($B);
- // Then
- $this->assertEquals($expected, $A∪B);
- $this->assertEquals($expected->asArray(), $A∪B->asArray());
- }
- public function testUnionWithObjects2()
- {
- // Given
- $vector1 = new Vector([1, 2, 3]);
- $vector2 = new Vector([1, 2, 3]);
- $A = new Set([1, 2, $vector1]);
- $B = new Set([2, 3, $vector2, $vector1]);
- $expected = new Set([1, 2, $vector1, 3, $vector2]);
- // When
- $A∪B = $A->union($B);
- // Then
- $this->assertEquals($expected, $A∪B);
- $this->assertEquals($expected->asArray(), $A∪B->asArray());
- }
- /**
- * @test
- * @dataProvider dataProviderForIntersect
- */
- public function testIntersect(array $A, array $B, array $A∩B, Set $R)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- $expected = new Set($A∩B);
- // When
- $intersection = $setA->intersect($setB);
- $intersection_array = $intersection->asArray();
- // Then
- $this->assertEquals($R, $intersection);
- $this->assertEquals($expected, $intersection);
- $this->assertEquals(count($A∩B), count($intersection));
- foreach ($A∩B as $member) {
- $this->assertArrayHasKey("$member", $intersection_array);
- $this->assertArrayHasKey("$member", $setA->asArray());
- $this->assertArrayHasKey("$member", $setB->asArray());
- $this->assertContains($member, $A);
- $this->assertContains($member, $B);
- }
- foreach ($A∩B as $_ => $value) {
- if ($value instanceof Set) {
- $this->assertEquals($value, $intersection_array["$value"]);
- } else {
- $this->assertContains($value, $intersection_array);
- }
- }
- }
- public function dataProviderForIntersect(): array
- {
- $setOneTwo = new Set([1, 2]);
- return [
- [
- [],
- [],
- [],
- new Set(),
- ],
- [
- [1],
- [],
- [],
- new Set(),
- ],
- [
- [],
- [1],
- [],
- new Set(),
- ],
- [
- [1],
- [1],
- [1],
- new Set([1]),
- ],
- [
- [1],
- [2],
- [],
- new Set(),
- ],
- [
- [2],
- [1],
- [],
- new Set(),
- ],
- [
- [2],
- [2],
- [2],
- new Set([2]),
- ],
- [
- [1, 2],
- [1, 2],
- [1, 2],
- new Set([1, 2]),
- ],
- [
- [1, 2],
- [2, 1],
- [1, 2],
- new Set([1, 2]),
- ],
- [
- [1, 2, 3, 'a', 'b'],
- [1, 'a', 'k'],
- [1, 'a'],
- new Set([1, 'a']),
- ],
- [
- [1, 2, 3, 'a', 'b', new Set([1, 2])],
- [1, 'a', 'k'],
- [1, 'a'],
- new Set([1, 'a']),
- ],
- [
- [1, 2, 3, 'a', 'b'],
- [1, 'a', 'k', new Set([1, 2])],
- [1, 'a'],
- new Set([1, 'a']),
- ],
- [
- [1, 2, 3, 'a', 'b', $setOneTwo],
- [1, 'a', 'k', $setOneTwo],
- [1, 'a', $setOneTwo],
- new Set([1, 'a', $setOneTwo]),
- ],
- [
- [1, 2, 3, 'a', 'b', new Set()],
- [1, 'a', 'k', $setOneTwo],
- [1, 'a'],
- new Set([1, 'a']),
- ],
- [
- [1, 2, 3, 'a', 'b', $setOneTwo],
- [1, 'a', 'k', -2, '2.4', 3.5, $setOneTwo],
- [1, 'a', $setOneTwo],
- new Set([1, 'a', $setOneTwo]),
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForIntersectMultipleSets
- */
- public function testIntersectMultipleSets(array $A, array $B, array $C, array $A∩B∩C, Set $R)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- $setC = new Set($C);
- $expected = new Set($A∩B∩C);
- // When
- $intersection = $setA->intersect($setB, $setC);
- $intersection_array = $intersection->asArray();
- // Then
- $this->assertEquals($R, $intersection);
- $this->assertEquals($expected, $intersection);
- $this->assertEquals(count($A∩B∩C), count($intersection));
- foreach ($A∩B∩C as $member) {
- $this->assertArrayHasKey("$member", $intersection_array);
- $this->assertArrayHasKey("$member", $setA->asArray());
- $this->assertArrayHasKey("$member", $setB->asArray());
- $this->assertArrayHasKey("$member", $setC->asArray());
- $this->assertContains($member, $A);
- $this->assertContains($member, $B);
- $this->assertContains($member, $C);
- }
- foreach ($A∩B∩C as $_ => $value) {
- if ($value instanceof Set) {
- $this->assertEquals($value, $intersection_array["$value"]);
- } else {
- $this->assertContains($value, $intersection_array);
- }
- }
- }
- public function dataProviderForIntersectMultipleSets(): array
- {
- $setOneTwo = new Set([1, 2]);
- return [
- [
- [1, 2, 3, 4],
- [2, 3, 4, 5],
- [3, 4, 5, 6],
- [3, 4],
- new Set([3, 4]),
- ],
- [
- [1, 2, 3, 4, $setOneTwo],
- [2, 3, 4, 5, $setOneTwo],
- [3, 4, 5, 6, $setOneTwo],
- [3, 4, $setOneTwo],
- new Set([3, 4, $setOneTwo]),
- ],
- ];
- }
- public function testIntersectWithArrays()
- {
- // Given
- $A = new Set([1, 2, [1, 2, 3]]);
- $B = new Set([2, 3, [2, 3, 4]]);
- $expected = new Set([2]);
- // When
- $A∩B = $A->intersect($B);
- // Then
- $this->assertEquals($expected, $A∩B);
- $this->assertEquals($expected->asArray(), $A∩B->asArray());
- }
- public function testIntersectWithArrays2()
- {
- // Given
- $A = new Set([1, 2, [1, 2, 3]]);
- $B = new Set([2, 3, [2, 3, 4], [1, 2, 3]]);
- $expected = new Set([2, [1, 2, 3]]);
- // When
- $A∩B = $A->intersect($B);
- // Then
- $this->assertEquals($expected, $A∩B);
- $this->assertEquals($expected->asArray(), $A∩B->asArray());
- }
- public function testIntersectWithObjects()
- {
- // Given
- $vector1 = new Vector([1, 2, 3]);
- $vector2 = new Vector([1, 2, 3]);
- $A = new Set([1, 2, $vector1]);
- $B = new Set([2, 3, $vector2]);
- $expected = new Set([2]);
- // When
- $A∩B = $A->intersect($B);
- // Then
- $this->assertEquals($expected, $A∩B);
- $this->assertEquals($expected->asArray(), $A∩B->asArray());
- }
- public function testIntersectWithObjects2()
- {
- // Given
- $vector1 = new Vector([1, 2, 3]);
- $vector2 = new Vector([1, 2, 3]);
- $A = new Set([1, 2, $vector1]);
- $B = new Set([2, 3, $vector2, $vector1]);
- $expected = new Set([2, $vector1]);
- // When
- $A∩B = $A->intersect($B);
- // Then
- $this->assertEquals($expected, $A∩B);
- $this->assertEquals($expected->asArray(), $A∩B->asArray());
- }
- /**
- * @test
- * @dataProvider dataProviderForDifference
- */
- public function testDifference(array $A, array $B, array $diff, Set $R)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- $expected = new Set($diff);
- // When
- $difference = $setA->difference($setB);
- $difference_array = $difference->asArray();
- // Then
- $this->assertEquals($R, $difference);
- $this->assertEquals($expected, $difference);
- $this->assertEquals(count($diff), count($difference));
- foreach ($diff as $member) {
- $this->assertArrayHasKey("$member", $difference_array);
- $this->assertArrayHasKey("$member", $setA->asArray());
- $this->assertArrayNotHasKey("$member", $setB->asArray());
- $this->assertContains($member, $A);
- $this->assertNotContains("$member", $B);
- }
- foreach ($diff as $_ => $value) {
- if ($value instanceof Set) {
- $this->assertEquals($value, $difference_array["$value"]);
- } else {
- $this->assertContains($value, $difference_array);
- }
- }
- }
- public function dataProviderForDifference(): array
- {
- $emptySet = new Set();
- $setOneTwo = new Set([1, 2]);
- return [
- [
- [],
- [],
- [],
- new Set(),
- ],
- [
- [1],
- [1],
- [],
- new Set(),
- ],
- [
- [1, 2],
- [1],
- [2],
- new Set([2]),
- ],
- [
- [1],
- [1, 2],
- [],
- new Set(),
- ],
- [
- [1, 2, 3, 4],
- [2, 3, 4, 5],
- [1],
- new Set([1]),
- ],
- [
- [1, 2, 3, 'a', 'b'],
- [1, 'a', 'k'],
- [2, 3, 'b'],
- new Set([2, 3, 'b']),
- ],
- [
- [1, 2, 3, 'a', 'b', $setOneTwo],
- [1, 'a', 'k'],
- [2, 3, 'b',$setOneTwo],
- new Set([2, 3, 'b', $setOneTwo]),
- ],
- [
- [1, 2, 3, 'a', 'b'],
- [1, 'a', 'k', new Set([1, 2])],
- [2, 3, 'b'],
- new Set([2, 3, 'b']),
- ],
- [
- [1, 2, 3, 'a', 'b', $emptySet],
- [1, 'a', 'k', new Set([1, 2])],
- [2, 3, 'b', $emptySet],
- new Set([2, 3, 'b', $emptySet]),
- ],
- [
- [1, 2, 3, 'a', 'b', new Set([1, 2])],
- [1, 'a', 'k', -2, '2.4', 3.5, new Set([1, 2])],
- [2, 3, 'b'],
- new Set([2, 3, 'b']),
- ],
- [
- [1, 2,'a', 3, 4.5, new Set([1, 2])],
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- [],
- new Set(),
- ],
- [
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- [1, 2, 3, 'a', 4.5, new Set([1, 2])],
- [],
- new Set(),
- ],
- [
- [1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 2.4, 3.5],
- [1, 2, 3, 'a', 4.5, new Set([1, 2]), -1, -2, 2.4, 3.5],
- [],
- new Set(),
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForDifferenceMultiSet
- */
- public function testDifferenceMultiSet(array $A, array $B, array $C, array $diff, Set $R)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- $setC = new Set($C);
- $expected = new Set($diff);
- // When
- $difference = $setA->difference($setB, $setC);
- $difference_array = $difference->asArray();
- // Then
- $this->assertEquals($R, $difference);
- $this->assertEquals($expected, $difference);
- $this->assertEquals(count($diff), count($difference));
- foreach ($diff as $member) {
- $this->assertArrayHasKey("$member", $difference_array);
- $this->assertArrayHasKey("$member", $setA->asArray());
- $this->assertArrayNotHasKey("$member", $setB->asArray());
- $this->assertArrayNotHasKey("$member", $setC->asArray());
- $this->assertContains($member, $A);
- $this->assertNotContains("$member", $B);
- $this->assertNotContains("$member", $C);
- }
- foreach ($diff as $_ => $value) {
- if ($value instanceof Set) {
- $this->assertEquals($value, $difference_array["$value"]);
- } else {
- $this->assertContains($value, $difference_array);
- }
- }
- }
- public function dataProviderForDifferenceMultiSet(): array
- {
- $setOneTwo = new Set([1, 2]);
- return [
- [
- ['1', '2', '3', '4'],
- ['2', '3', '4', '5'],
- ['3', '4', '5', '6'],
- ['1'],
- new Set([1]),
- ],
- [
- ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
- ['2', '4', '6', '8', '10'],
- ['5', '10'],
- ['1', '3', '7', '9'],
- new Set([1, 3, 7, 9]),
- ],
- [
- ['1', '2', '3', '4', $setOneTwo],
- ['2', '3', '4', '5'],
- ['3', '4', '5', '6'],
- ['1', $setOneTwo],
- new Set([1, $setOneTwo]),
- ],
- [
- ['1', '2', '3', '4', new Set([1, 2])],
- ['2', '3', '4', '5'],
- ['3', '4', '5', '6', new Set([1, 2])],
- ['1'],
- new Set([1]),
- ],
- ];
- }
- public function testDifferenceWithArrays()
- {
- // Given
- $A = new Set([1, 2, [1, 2, 3]]);
- $B = new Set([2, 3, [2, 3, 4]]);
- $expected = new Set([1, [1, 2, 3]]);
- // When
- $A∖B = $A->difference($B);
- // Then
- $this->assertEquals($expected, $A∖B);
- $this->assertEquals($expected->asArray(), $A∖B->asArray());
- }
- public function testDifferenceWithArrays2()
- {
- // Given
- $A = new Set([1, 2, [1, 2, 3]]);
- $B = new Set([2, 3, [2, 3, 4], [1, 2, 3]]);
- $expected = new Set([1]);
- // When
- $A∖B = $A->difference($B);
- // Then
- $this->assertEquals($expected, $A∖B);
- $this->assertEquals($expected->asArray(), $A∖B->asArray());
- }
- public function testDifferenceWithObjects()
- {
- // Given
- $vector1 = new Vector([1, 2, 3]);
- $vector2 = new Vector([1, 2, 3]);
- $A = new Set([1, 2, $vector1]);
- $B = new Set([2, 3, $vector2]);
- $expected = new Set([1, $vector1]);
- // When
- $A∖B = $A->difference($B);
- // Then
- $this->assertEquals($expected, $A∖B);
- $this->assertEquals($expected->asArray(), $A∖B->asArray());
- }
- public function testDifferenceWithObjects2()
- {
- // Given
- $vector1 = new Vector([1, 2, 3]);
- $vector2 = new Vector([1, 2, 3]);
- $A = new Set([1, 2, $vector1]);
- $B = new Set([2, 3, $vector2, $vector1]);
- $expected = new Set([1]);
- // When
- $A∖B = $A->difference($B);
- // Then
- $this->assertEquals($expected, $A∖B);
- $this->assertEquals($expected->asArray(), $A∖B->asArray());
- }
- /**
- * @test
- * @dataProvider dataProviderForSymmetricDifference
- */
- public function testSymmetricDifference(array $A, array $B, array $diff, Set $R)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- $expected = new Set($diff);
- // When
- $difference = $setA->symmetricDifference($setB);
- $difference_array = $difference->asArray();
- // Then
- $this->assertEquals($R, $difference);
- $this->assertEquals($expected, $difference);
- $this->assertEquals(count($diff), count($difference));
- foreach ($diff as $member) {
- $this->assertArrayHasKey("$member", $difference_array);
- }
- foreach ($diff as $_ => $value) {
- if ($value instanceof Set) {
- $this->assertEquals($value, $difference_array["$value"]);
- } else {
- $this->assertArrayHasKey((string) $value, $difference_array);
- }
- }
- }
- public function dataProviderForSymmetricDifference(): array
- {
- return [
- [
- [1, 2, 3],
- [2, 3, 4],
- [1, 4],
- new Set([1, 4]),
- ],
- [
- [1, 2, 3, new Set()],
- [2, 3, 4],
- [1, 4, new Set()],
- new Set([1, 4, new Set()]),
- ],
- [
- [1, 2, 3],
- [2, 3, 4, new Set()],
- [1, 4, new Set()],
- new Set([1, 4, new Set()]),
- ],
- [
- [1, 2, 3, new Set()],
- [2, 3, 4, new Set()],
- [1, 4],
- new Set([1, 4]),
- ],
- [
- [1, 3, 5, 7, 9],
- [2, 4, 6, 8, 10],
- [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
- new Set([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]),
- ],
- ];
- }
- public function testSymmetricDifferenceWithArrays()
- {
- // Given
- $A = new Set([1, 2, [1, 2, 3]]);
- $B = new Set([2, 3, [2, 3, 4]]);
- $expected = new Set([1, 3, [1, 2, 3], [2, 3, 4]]);
- // When
- $AΔB = $A->symmetricDifference($B);
- // Then
- $this->assertEquals($expected, $AΔB);
- $this->assertEquals($expected->asArray(), $AΔB->asArray());
- }
- public function testSymmetricDifferenceWithArrays2()
- {
- // Given
- $A = new Set([1, 2, [1, 2, 3]]);
- $B = new Set([2, 3, [2, 3, 4], [1, 2, 3]]);
- $expected = new Set([1, 3, [2, 3, 4]]);
- // When
- $AΔB = $A->symmetricDifference($B);
- // Then
- $this->assertEquals($expected, $AΔB);
- $this->assertEquals($expected->asArray(), $AΔB->asArray());
- }
- public function testSymmetricDifferenceWithObjects()
- {
- // Given
- $vector1 = new Vector([1, 2, 3]);
- $vector2 = new Vector([1, 2, 3]);
- $A = new Set([1, 2, $vector1]);
- $B = new Set([2, 3, $vector2]);
- $expected = new Set([1, 3, $vector1, $vector2]);
- // When
- $AΔB = $A->symmetricDifference($B);
- // Then
- $this->assertEquals($expected, $AΔB);
- $this->assertEquals($expected->asArray(), $AΔB->asArray());
- }
- public function testSymmetricDifferenceWithObjects2()
- {
- // Given
- $vector1 = new Vector([1, 2, 3]);
- $vector2 = new Vector([1, 2, 3]);
- $A = new Set([1, 2, $vector1]);
- $B = new Set([2, 3, $vector2, $vector1]);
- $expected = new Set([1, 3, $vector2]);
- // When
- $AΔB = $A->symmetricDifference($B);
- // Then
- $this->assertEquals($expected, $AΔB);
- $this->assertEquals($expected->asArray(), $AΔB->asArray());
- }
- /**
- * @test
- * @dataProvider dataProviderForSingleSet
- */
- public function testCopy(array $members)
- {
- // Given
- $set = new Set($members);
- $copy = $set->copy();
- // When
- $set_array = $set->asArray();
- $copy_array = $copy->asArray();
- // Then
- $this->assertEquals($set, $copy);
- $this->assertEquals($set_array, $copy_array);
- $this->assertEquals(count($set), count($copy));
- }
- /**
- * @test
- * @dataProvider dataProviderForSingleSet
- */
- public function testClear(array $members)
- {
- // Given
- $set = new Set($members);
- // When
- $set->clear();
- // Then
- $this->assertTrue($set->isEmpty());
- $this->assertEmpty($set->asArray());
- $this->assertEquals($set, new Set());
- }
- /**
- * @test
- * @dataProvider dataProviderForCartesianProduct
- */
- public function testCartesianProduct(array $A, array $B, array $A×B, Set $R)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- // When
- $setA×B = $setA->cartesianProduct($setB);
- $A×B_array = $setA×B->asArray();
- // Then
- $this->assertEquals($R, $setA×B);
- $this->assertEquals($A×B, $A×B_array);
- $this->assertEquals(count($setA×B), count($A×B));
- foreach ($setA×B as $key => $value) {
- $this->assertInstanceOf(Set::class, $value);
- $this->assertEquals(2, count($value));
- }
- foreach ($A×B_array as $key => $value) {
- $this->assertInstanceOf(Set::class, $value);
- $this->assertEquals(2, count($value));
- }
- }
- public function dataProviderForCartesianProduct(): array
- {
- return [
- [
- [1, 2],
- [3, 4],
- ['Set{1, 3}' => new Set([1, 3]), 'Set{1, 4}' => new Set([1, 4]), 'Set{2, 3}' => new Set([2, 3]), 'Set{2, 4}' => new Set([2, 4])],
- new Set([new Set([1, 3]), new Set([1, 4]), new Set([2, 3]), new Set([2, 4])]),
- ],
- [
- [1, 2],
- ['red', 'white'],
- ['Set{1, red}' => new Set([1, 'red']), 'Set{1, white}' => new Set([1, 'white']), 'Set{2, red}' => new Set([2, 'red']), 'Set{2, white}' => new Set([2, 'white'])],
- new Set([new Set([1, 'red']), new Set([1, 'white']), new Set([2, 'red']), new Set([2, 'white'])]),
- ],
- [
- [1, 2],
- [],
- [],
- new Set(),
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForNaryCartesianProduct
- */
- public function testNaryCartesianProduct(array $A, array $B, array $C, array $A×B×C, Set $R)
- {
- // Given
- $setA = new Set($A);
- $setB = new Set($B);
- $setC = new Set($C);
- // When
- $setA×B×C = $setA->cartesianProduct($setB, $setC);
- $A×B×C_array = $setA×B×C->asArray();
- // Then
- $this->assertEquals($R, $setA×B×C);
- $this->assertEquals($A×B×C, $A×B×C_array);
- $this->assertEquals(count($setA×B×C), count($A×B×C));
- $this->assertEquals(count($setA×B×C), count($setA) * count($setB) * count($setC));
- foreach ($setA×B×C as $key => $value) {
- $this->assertInstanceOf(Set::class, $value);
- $this->assertEquals(3, count($value));
- }
- foreach ($A×B×C_array as $key => $value) {
- $this->assertInstanceOf(Set::class, $value);
- $this->assertEquals(3, count($value));
- }
- }
- public function dataProviderForNaryCartesianProduct(): array
- {
- return [
- [
- [1, 2],
- [3, 4],
- [5, 6],
- [
- 'Set{1, 3, 5}' => new Set([1, 3, 5]),
- 'Set{1, 3, 6}' => new Set([1, 3, 6]),
- 'Set{1, 4, 5}' => new Set([1, 4, 5]),
- 'Set{1, 4, 6}' => new Set([1, 4, 6]),
- 'Set{2, 3, 5}' => new Set([2, 3, 5]),
- 'Set{2, 3, 6}' => new Set([2, 3, 6]),
- 'Set{2, 4, 5}' => new Set([2, 4, 5]),
- 'Set{2, 4, 6}' => new Set([2, 4, 6]),
- ],
- new Set([
- new Set([1, 3, 5]),
- new Set([1, 3, 6]),
- new Set([1, 4, 5]),
- new Set([1, 4, 6]),
- new Set([2, 3, 5]),
- new Set([2, 3, 6]),
- new Set([2, 4, 5]),
- new Set([2, 4, 6]),
- ]),
- ],
- [
- [1, 2],
- ['red', 'white'],
- ['A', 'B'],
- [
- 'Set{1, red, A}' => new Set([1, 'red', 'A']),
- 'Set{1, red, B}' => new Set([1, 'red', 'B']),
- 'Set{1, white, A}' => new Set([1, 'white', 'A']),
- 'Set{1, white, B}' => new Set([1, 'white', 'B']),
- 'Set{2, red, A}' => new Set([2, 'red', 'A']),
- 'Set{2, red, B}' => new Set([2, 'red', 'B']),
- 'Set{2, white, A}' => new Set([2, 'white', 'A']),
- 'Set{2, white, B}' => new Set([2, 'white', 'B']),
- ],
- new Set([
- new Set([1, 'red', 'A']),
- new Set([1, 'red', 'B']),
- new Set([1, 'white', 'A']),
- new Set([1, 'white', 'B']),
- new Set([2, 'red', 'A']),
- new Set([2, 'red', 'B']),
- new Set([2, 'white', 'A']),
- new Set([2, 'white', 'B']),
- ]),
- ],
- [
- [1, 2],
- [3],
- [],
- [],
- new Set(),
- ],
- ];
- }
- /**
- * @test
- * @dataProvider dataProviderForPowerSet
- */
- public function testPowerSet(Set $A, Set $expected)
- {
- // When
- $P⟮S⟯ = $A->powerSet();
- // Then
- $this->assertEquals($expected, $P⟮S⟯);
- $this->assertEquals($expected->asArray(), $P⟮S⟯->asArray());
- $this->assertEquals(count($expected), count($P⟮S⟯));
- }
- public function dataProviderForPowerSet(): array
- {
- return [
- // P({}) = {Ø}
- [
- new Set(),
- new Set([
- new Set(),
- ]),
- ],
- // P({1}) = {{Ø}, {1}}
- [
- new Set([1]),
- new Set([
- new Set(),
- new Set([1]),
- ]),
- ],
- // P({1, 2, 3}) = {Ø, {1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}
- [
- new Set([1, 2, 3]),
- new Set([
- new Set(),
- new Set([1]),
- new Set([2]),
- new Set([3]),
- new Set([1, 2]),
- new Set([1, 3]),
- new Set([2, 3]),
- new Set([1, 2, 3]),
- ]),
- ],
- // P({x, y, z}) = {Ø, {x}, {y}, {z}, {x,y}, {x,z}, {y,z}, {x,y,z}}
- [
- new Set(['x', 'y', 'z']),
- new Set([
- new Set(),
- new Set(['x']),
- new Set(['y']),
- new Set(['z']),
- new Set(['x', 'y']),
- new Set(['x', 'z']),
- new Set(['y', 'z']),
- new Set(['x', 'y', 'z']),
- ]),
- ],
- // P({1, [1, 2]}) = {Ø, {1}, {[1, 2]}, {1, [1, 2]}}
- [
- new Set([1, [1, 2]]),
- new Set([
- new Set(),
- new Set([1]),
- new Set([[1, 2]]),
- new Set([1, [1, 2]]),
- ]),
- ],
- ];
- }
- public function dataProviderForSingleSet(): array
- {
- $fh = fopen(__FILE__, 'r');
- $vector = new Vector([1, 2, 3]);
- $func = function ($x) {
- return $x * 2;
- };
- return [
- [[]],
- [[0]],
- [[1]],
- [[5]],
- [[-5]],
- [[1, 2]],
- [[1, 2, 3]],
- [[1, -2, 3]],
- [[1, 2, 3, 4, 5, 6]],
- [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]],
- [[1, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2, 2.01, 2.001, 2.15]],
- [['a']],
- [['a', 'b']],
- [['a', 'b', 'c', 'd', 'e']],
- [[1, 2, 'a', 'b', 3.14, 'hello', 'goodbye']],
- [[1, 2, 3, new Set([1, 2]), 'a', 'b']],
- [['a', 1, 'b', new Set([1, 'b'])]],
- [['a', 1, 'b', new Set([1, 'b']), '4', 5]],
- [['a', 1, 'b', new Set([1, 'b']), new Set([3, 4, 5]), '4', 5]],
- [[1, 2, 3, [1, 2], [2, 3, 4]]],
- [[1, 2, $fh, $vector, [4, 5], 6, 'a', $func, 12, new Set([4, 6, 7]), new Set(), 'sets']],
- ];
- }
- }
|