Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 135 lines (103 sloc) 5.02 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
<?php

require_once __DIR__ . '/../FunSets.php';

class FunSetsTest extends PHPUnit_Framework_TestCase {

private $funSets;

protected function setUp() {
$this->funSets = new FunSets();
}

function testContainsIsImplemented() {
// We caracterize a set by it's contains function. It is the basic function of a set.

$set = function ($element) {
return true;
};
$this->assertTrue($this->funSets->contains($set, 100));
}

function testSingletonSetContainsSingleElement() {
// A singleton set is characterize by a function which passed to contains will return true for the single element
// passed as it's parameter. In other words, a singleton is a set with a single element.

$singleton = $this->funSets->singletonSet(1);
$this->assertTrue($this->funSets->contains($singleton, 1));
}

function testUnionContainsAllElements() {
// A union is characterized by a function which gets 2 sets as parameters and contains all the provided sets
// We can only create singletons at this point, so we create 2 singletons and unite them
$s1 = $this->funSets->singletonSet(1);
$s2 = $this->funSets->singletonSet(2);
$union = $this->funSets->union($s1, $s2);

// Now, check that both 1 and 2 are part of the union
$this->assertTrue($this->funSets->contains($union, 1));
$this->assertTrue($this->funSets->contains($union, 2));
// ... and that it does not contain 3
$this->assertFalse($this->funSets->contains($union, 3));
}

function testIntersectionContainsOnlyCommonElements() {
$u12 = $this->createUnionWithElements(1, 2);
$u23 = $this->createUnionWithElements(2, 3);
$intersection = $this->funSets->intersect($u12, $u23);

// Verify intersection has common elements
$this->assertTrue($this->funSets->contains($intersection, 2));
// Check intersection does not have unique elements
$this->assertFalse($this->funSets->contains($intersection, 1));
$this->assertFalse($this->funSets->contains($intersection, 3));
}

function testDiffContainsOnlyUniqueElementsFromTheFirstArray() {
$u12 = $this->createUnionWithElements(1, 2);
$u23 = $this->createUnionWithElements(2, 3);
$difference = $this->funSets->diff($u12, $u23);

// Verify difference has the unique element from the first set
$this->assertTrue($this->funSets->contains($difference, 1));
// Check difference does not have the common element
$this->assertFalse($this->funSets->contains($difference, 2));
// Check difference does not have the element form the second set
$this->assertFalse($this->funSets->contains($difference, 3));
}

function testFilterContainsOnlyElementsThatMatchConditionFunction() {
$u123 = $this->createUnionWith123();

// Filtering rule, find elements greater than 1 (meaning 2 and 3)
$condition = function($elem) { return $elem > 1; };

// Filtered set
$filteredSet = $this->funSets->filter($u123, $condition);

// Verify filtered set does not contain 1
$this->assertFalse($this->funSets->contains($filteredSet, 1), "Should not contain 1");
// Check it contains 2 and 3
$this->assertTrue($this->funSets->contains($filteredSet, 2), "Should contain 2");
$this->assertTrue($this->funSets->contains($filteredSet, 3), "Should contain 3");
}

function testForAllCorrectlyTellsIfAllElementsSatisfyCondition() {
$u123 = $this->createUnionWith123();

$higherThanZero = function($elem) { return $elem > 0; };
$higherThanOne = function($elem) { return $elem > 1; };
$higherThanTwo = function($elem) { return $elem > 2; };

$this->assertTrue($this->funSets->forall($u123, $higherThanZero));
$this->assertFalse($this->funSets->forall($u123, $higherThanOne));
$this->assertFalse($this->funSets->forall($u123, $higherThanTwo));
}

function testExistsCorrectlyTellsIfAnElementsSatisfiesCondition() {
$u123 = $this->createUnionWith123();

$higherThanZero = function($elem) { return $elem > 0; };
$higherThanOne = function($elem) { return $elem > 1; };
$higherThanTwo = function($elem) { return $elem > 2; };
$higherThanThree = function($elem) { return $elem > 3; };

$this->assertTrue($this->funSets->exists($u123, $higherThanZero));
$this->assertTrue($this->funSets->exists($u123, $higherThanOne));
$this->assertTrue($this->funSets->exists($u123, $higherThanTwo));
$this->assertFalse($this->funSets->exists($u123, $higherThanThree));
}

function testMapAppliesFunctionToAllElements() {
$u12 = $this->createUnionWithElements(1, 2);

$double = function($elem) { return $elem * 2; };
$mapped = $this->funSets->map($u12, $double);

$this->assertTrue($this->funSets->contains($mapped, 2));
$this->assertTrue($this->funSets->contains($mapped, 4));
}

private function createUnionWithElements($elem1, $elem2) {
$s1 = $this->funSets->singletonSet($elem1);
$s2 = $this->funSets->singletonSet($elem2);
return $this->funSets->union($s1, $s2);
}

public function createUnionWith123() {
$u12 = $this->createUnionWithElements(1, 2);
return $this->funSets->union($u12, $this->funSets->singletonSet(3));
}

}

?>
Something went wrong with that request. Please try again.