Created
September 30, 2013 15:01
-
-
Save denen99/6765084 to your computer and use it in GitHub Desktop.
Coursera week 2 scala tests
This file contains hidden or 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
package funsets | |
import org.scalatest.FunSuite | |
import org.junit.runner.RunWith | |
import org.scalatest.junit.JUnitRunner | |
/** | |
* This class is a test suite for the methods in object FunSets. To run | |
* the test suite, you can either: | |
* - run the "test" command in the SBT console | |
* - right-click the file in eclipse and chose "Run As" - "JUnit Test" | |
*/ | |
@RunWith(classOf[JUnitRunner]) | |
class FunSetSuite extends FunSuite { | |
/** | |
* Link to the scaladoc - very clear and detailed tutorial of FunSuite | |
* | |
* http://doc.scalatest.org/1.9.1/index.html#org.scalatest.FunSuite | |
* | |
* Operators | |
* - test | |
* - ignore | |
* - pending | |
*/ | |
/** | |
* Tests are written using the "test" operator and the "assert" method. | |
*/ | |
test("string take") { | |
val message = "hello, world" | |
assert(message.take(5) == "hello") | |
} | |
/** | |
* For ScalaTest tests, there exists a special equality operator "===" that | |
* can be used inside "assert". If the assertion fails, the two values will | |
* be printed in the error message. Otherwise, when using "==", the test | |
* error message will only say "assertion failed", without showing the values. | |
* | |
* Try it out! Change the values so that the assertion fails, and look at the | |
* error message. | |
*/ | |
test("adding ints") { | |
assert(1 + 2 === 3) | |
} | |
import FunSets._ | |
test("contains is implemented") { | |
assert(contains(x => true, 100)) | |
} | |
test("contains even number") { | |
assert(contains(x => x % 2 == 0, 2)) | |
} | |
test("does not contain even numbers"){ | |
assert(!contains(x => x % 2 == 0, 3)) | |
} | |
test("contains with singletons") { | |
val s1 = singletonSet(1) | |
val s2 = singletonSet(2) | |
val s3 = singletonSet(3) | |
assert(contains(s1, 1), "Singleton 1") | |
assert(!contains(s1, 2), "Singleton !1") | |
assert(contains(s2, 2), "Singleton 2") | |
assert(contains(s3, 3), "Singleton 3") | |
} | |
/** | |
* When writing tests, one would often like to re-use certain values for multiple | |
* tests. For instance, we would like to create an Int-set and have multiple test | |
* about it. | |
* | |
* Instead of copy-pasting the code for creating the set into every test, we can | |
* store it in the test class using a val: | |
* | |
* val s1 = singletonSet(1) | |
* | |
* However, what happens if the method "singletonSet" has a bug and crashes? Then | |
* the test methods are not even executed, because creating an instance of the | |
* test class fails! | |
* | |
* Therefore, we put the shared values into a separate trait (traits are like | |
* abstract classes), and create an instance inside each test method. | |
* | |
*/ | |
trait TestSets { | |
val s1 = singletonSet(1) | |
val s2 = singletonSet(2) | |
val s3 = singletonSet(3) | |
val s3b = singletonSet(3) | |
} | |
/** | |
* This test is currently disabled (by using "ignore") because the method | |
* "singletonSet" is not yet implemented and the test would fail. | |
* | |
* Once you finish your implementation of "singletonSet", exchange the | |
* function "ignore" by "test". | |
*/ | |
test("singletonSet(1) contains 1") { | |
/** | |
* We create a new instance of the "TestSets" trait, this gives us access | |
* to the values "s1" to "s3". | |
*/ | |
new TestSets { | |
/** | |
* The string argument of "assert" is a message that is printed in case | |
* the test fails. This helps identifying which assertion failed. | |
*/ | |
assert(contains(s1, 1), "Singleton") | |
} | |
} | |
test("union contains all elements") { | |
new TestSets { | |
val s = union(s1, s2) | |
assert(contains(s, 1), "Union 1") | |
assert(contains(s, 2), "Union 2") | |
assert(!contains(s, 3), "Union 3") | |
} | |
} | |
test("intersect contains proper element") { | |
new TestSets { | |
val i = intersect(s3,s3b) | |
assert(contains(i,3),"Intersect contains 3") | |
assert(!contains(i,2),"Intersect does not contain 2") | |
} | |
} | |
test("diff contains proper element") { | |
new TestSets { | |
val d = diff(s1,s2) | |
assert(contains(d,1),"Diff contains 1") | |
assert(!contains(d,2),"Diff does not contain 2") | |
} | |
} | |
test("filter contains proper element") { | |
new TestSets { | |
val even = (x: Int) => x % 2 == 0 | |
val f = filter(s1,even) | |
val f2 = filter(s2,even) | |
val f3 = filter(singletonSet(100),even) | |
assert(!contains(f,1),"Filter does not contain 1") | |
assert(contains(f2,2),"Filter contains 2") | |
assert(contains(f3,100),"Filter contains 100") | |
} | |
} | |
test("forall contains proper element") { | |
new TestSets { | |
val even = (x: Int) => x % 2 == 0 | |
val all = forall(s1,even) | |
val all2 = forall(s2,even) | |
val by4 = (x: Int) => x % 4 == 0 | |
val s4 = (x:Int) => x == 4 || x== 8 || x == 12 || x == 16 | |
val all3 = forall(s4,by4) | |
assert(all == false , "S1 is not all even") | |
assert(all2 == true, "S2 is all even") | |
assert(all3 == true, "S3 is divisible by 4") | |
} | |
} | |
test("exists contains proper element") { | |
new TestSets { | |
val odd = (x: Int) => x % 2 != 0 | |
val even = (x: Int) => x % 2 == 0 | |
val underTen = (x: Int) => x < 10 | |
val random = (x:Int) => x == 2 || x == 5 || x == 20 | |
val e1 = exists(s1,odd) | |
val e2 = exists(s2,even) | |
val e3 = exists(s1,even) | |
val e4 = exists(random,underTen) | |
assert(e1 == true, "e1 is odd") | |
assert(e2 == true," e2 is even") | |
assert(e3 == false, " e3 does not have even numbers") | |
assert(e4 == true, "e4 is under Ten") | |
} | |
} | |
test("map contains proper element") { | |
new TestSets { | |
val dbl = (x: Int) => x * 2 | |
val result = map(s1,dbl) | |
val result2 = map(s2,dbl) | |
val bigSet = (x:Int) => x == 1 || x ==3 || x ==4 || x ==5 || x == 7 || x == 1000 | |
val minusOne = (x: Int) => x - 1 | |
val result3 = map(bigSet,minusOne) | |
assert(contains(result,2) == true, "Double function mapped") | |
assert(contains(result2,4) == true, "Double function mapped again") | |
assert(!contains(result,3) == true, "result does not contain 3") | |
assert(contains(result3,999) == true, "results should contain 999") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment