-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6658d47
commit d177286
Showing
6 changed files
with
253 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package funsets | ||
|
||
/* | ||
import org.scalatest.PropSpec | ||
import org.scalatest.prop.PropertyChecks | ||
import org.scalatest.matchers.ShouldMatchers | ||
class Fraction(n: Int, d: Int) { | ||
require(d != 0) | ||
require(d != Integer.MIN_VALUE) | ||
require(n != Integer.MIN_VALUE) | ||
val numer = if (d < 0) -1 * n else n | ||
val denom = d.abs | ||
override def toString = numer + " / " + denom | ||
} | ||
class FractionSpec extends PropSpec with PropertyChecks with ShouldMatchers { | ||
property("Fraction constructor normalizes numerator and denominator") { | ||
forAll { (n: Int, d: Int) => | ||
whenever(d != 0 && d != Integer.MIN_VALUE && n != Integer.MIN_VALUE) { | ||
val f = new Fraction(n, d) | ||
if (n < 0 && d < 0 || n > 0 && d > 0) | ||
f.numer should be > 0 | ||
else if (n != 0) | ||
f.numer should be < 0 | ||
else | ||
f.numer should be === 0 | ||
f.denom should be > 0 | ||
} | ||
} | ||
} | ||
property("Fraction constructor throws IAE on bad data.") { | ||
val invalidCombos = | ||
Table( | ||
("n", "d"), | ||
(Integer.MIN_VALUE, Integer.MIN_VALUE), | ||
(1, Integer.MIN_VALUE), | ||
(Integer.MIN_VALUE, 1), | ||
(Integer.MIN_VALUE, 0), | ||
(1, 0)) | ||
forAll(invalidCombos) { (n: Int, d: Int) => | ||
evaluating { | ||
new Fraction(n, d) | ||
} should produce[IllegalArgumentException] | ||
} | ||
} | ||
} | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package funsets | ||
|
||
import FunSets._ | ||
import scala.language.implicitConversions | ||
import scala.collection.immutable | ||
|
||
object SetExtension { | ||
def toFunSet(l: immutable.Set[Int]): Set = (x => l.contains(x)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package funsets | ||
|
||
import org.scalatest.FunSuite | ||
|
||
import org.scalatest.PropSpec | ||
import org.scalatest.prop.PropertyChecks | ||
import org.scalatest.matchers.ShouldMatchers | ||
|
||
class SetsSpec extends PropSpec with PropertyChecks with ShouldMatchers { | ||
import FunSets._ | ||
import SetExtension._ | ||
import scala.collection.immutable | ||
|
||
val emptySet: Set = (x => false) | ||
val universalSet: Set = (x => true) | ||
|
||
property("union -- from definition") { | ||
forAll { (l1: immutable.Set[Int], l2: immutable.Set[Int]) => | ||
val unionSet: Set = FunSets.union(toFunSet(l1), toFunSet(l2)) | ||
(l1 forall (contains(unionSet, _))) should be(true) | ||
(l2 forall (contains(unionSet, _))) should be(true) | ||
((l1 ++ l2) forall (contains(unionSet, _))) should be(true) | ||
} | ||
} | ||
|
||
property("union -- no additional elements") { | ||
forAll { (n: Int, l: immutable.Set[Int]) => | ||
whenever(!(l contains n)) { | ||
val unionSet: Set = FunSets.union(toFunSet(l), toFunSet(l)) | ||
(!contains(unionSet, n)) should be(true) | ||
} | ||
} | ||
} | ||
|
||
property("union -- with empty set") { | ||
forAll { (l: immutable.Set[Int]) => | ||
{ | ||
val unionSet: Set = FunSets.union(emptySet, toFunSet(l)) | ||
(l forall (contains(unionSet, _))) should be(true) | ||
} | ||
} | ||
} | ||
|
||
property("intersect -- from definition") { | ||
forAll { (l1: immutable.Set[Int], l2: immutable.Set[Int]) => | ||
val intersectSet: Set = FunSets.intersect(toFunSet(l1), toFunSet(l2)) | ||
((l1 intersect l2) forall (contains(intersectSet, _))) should be(true) | ||
((l1 diff l2) forall (!contains(intersectSet, _))) should be(true) | ||
((l2 diff l1) forall (!contains(intersectSet, _))) should be(true) | ||
} | ||
} | ||
|
||
property("intersect -- no additional elements") { | ||
forAll { (n: Int, l: immutable.Set[Int]) => | ||
whenever(!(l contains n)) { | ||
val unionSet: Set = FunSets.union(toFunSet(l), toFunSet(l)) | ||
(contains(unionSet, n)) should be(false) | ||
} | ||
} | ||
} | ||
|
||
property("intersect -- with universal set") { | ||
forAll { (l: immutable.Set[Int]) => | ||
{ | ||
val intersectSet: Set = FunSets.intersect(universalSet, toFunSet(l)) | ||
(l forall (contains(intersectSet, _))) should be(true) | ||
} | ||
} | ||
} | ||
|
||
property("diff") { | ||
forAll { (s1: immutable.Set[Int], s2: immutable.Set[Int]) => | ||
{ | ||
val diffSet: Set = FunSets.diff(toFunSet(s1), toFunSet(s2)) | ||
((s1 diff s2) forall (contains(diffSet, _))) should be(true) | ||
((s2 diff s1) forall (!contains(diffSet, _))) should be(true) | ||
} | ||
} | ||
} | ||
|
||
property("filter") { | ||
forAll { (s: immutable.Set[Int]) => | ||
{ | ||
def even(x: Int) = x % 2 == 0 | ||
val filterSet: Set = FunSets.filter(toFunSet(s), even) | ||
((s filter even) forall (contains(filterSet, _))) should be(true) | ||
((s filter (x => !even(x))) forall (!contains(filterSet, _))) should be(true) | ||
} | ||
} | ||
} | ||
|
||
property("map") { | ||
forAll { (s1: immutable.Set[Int]) => | ||
{ | ||
val s = s1.map(x => x % 300) | ||
def double(x: Int) = 2 * x | ||
val mappedSet: Set = FunSets.map(toFunSet(s), double) | ||
((s map double) forall (contains(mappedSet, _))) should be(true) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.