Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 5.04 KB | None | 0 0
  1. package funsets
  2.  
  3. import org.scalatest.FunSuite
  4.  
  5.  
  6. import org.junit.runner.RunWith
  7. import org.scalatest.junit.JUnitRunner
  8.  
  9. /**
  10.  * This class is a test suite for the methods in object FunSets. To run
  11.  * the test suite, you can either:
  12.  *  - run the "test" command in the SBT console
  13.  *  - right-click the file in eclipse and chose "Run As" - "JUnit Test"
  14.  */
  15. @RunWith(classOf[JUnitRunner])
  16. class FunSetSuite extends FunSuite {
  17.  
  18.   /**
  19.    * Link to the scaladoc - very clear and detailed tutorial of FunSuite
  20.    *
  21.    * http://doc.scalatest.org/1.9.1/index.html#org.scalatest.FunSuite
  22.    *
  23.    * Operators
  24.    *  - test
  25.    *  - ignore
  26.    *  - pending
  27.    */
  28.  
  29.   /**
  30.    * Tests are written using the "test" operator and the "assert" method.
  31.    */
  32.   // test("string take") {
  33.   //   val message = "hello, world"
  34.   //   assert(message.take(5) == "hello")
  35.   // }
  36.  
  37.   /**
  38.    * For ScalaTest tests, there exists a special equality operator "===" that
  39.    * can be used inside "assert". If the assertion fails, the two values will
  40.    * be printed in the error message. Otherwise, when using "==", the test
  41.    * error message will only say "assertion failed", without showing the values.
  42.    *
  43.    * Try it out! Change the values so that the assertion fails, and look at the
  44.    * error message.
  45.    */
  46.   // test("adding ints") {
  47.   //   assert(1 + 2 === 3)
  48.   // }
  49.  
  50.  
  51.   import FunSets._
  52.  
  53.   test("contains is implemented") {
  54.     assert(contains(x => true, 100))
  55.   }
  56.  
  57.   /**
  58.    * When writing tests, one would often like to re-use certain values for multiple
  59.    * tests. For instance, we would like to create an Int-set and have multiple test
  60.    * about it.
  61.    *
  62.    * Instead of copy-pasting the code for creating the set into every test, we can
  63.    * store it in the test class using a val:
  64.    *
  65.    *   val s1 = singletonSet(1)
  66.    *
  67.    * However, what happens if the method "singletonSet" has a bug and crashes? Then
  68.    * the test methods are not even executed, because creating an instance of the
  69.    * test class fails!
  70.    *
  71.    * Therefore, we put the shared values into a separate trait (traits are like
  72.    * abstract classes), and create an instance inside each test method.
  73.    *
  74.    */
  75.  
  76.   trait TestSets {
  77.     val s1 = singletonSet(1)
  78.     val s2 = singletonSet(2)
  79.     val s3 = singletonSet(3)
  80.     val s1us2 = union(s1, s2)
  81.     val s2us3 = union(s2, s3)
  82.     val s3us1 = union(s3, s1)
  83.     val s1us2us3 = union(s1us2, s3)
  84.   }
  85.  
  86.   /**
  87.    * This test is currently disabled (by using "ignore") because the method
  88.    * "singletonSet" is not yet implemented and the test would fail.
  89.    *
  90.    * Once you finish your implementation of "singletonSet", exchange the
  91.    * function "ignore" by "test".
  92.    */
  93.   test("singletonSet(1) contains 1") {
  94.  
  95.     /**
  96.      * We create a new instance of the "TestSets" trait, this gives us access
  97.      * to the values "s1" to "s3".
  98.      */
  99.     new TestSets {
  100.       /**
  101.        * The string argument of "assert" is a message that is printed in case
  102.        * the test fails. This helps identifying which assertion failed.
  103.        */
  104.       assert(contains(s1, 1), "Singleton")
  105.     }
  106.   }
  107.  
  108.   test("union contains all elements of each set") {
  109.     new TestSets {
  110.       val s = union(s1, s2)
  111.       assert(contains(s, 1), "Union 1")
  112.       assert(contains(s, 2), "Union 2")
  113.       assert(!contains(s, 3), "Union 3")
  114.     }
  115.   }
  116.  
  117.   test("intersect contains common elements of each set") {
  118.     new TestSets {
  119.       val s = intersect(s1us2, s2us3)
  120.  
  121.       assert(contains(s, 2), "Intersect 2")
  122.       assert(!contains(s, 1), "!Intersect 1")
  123.       assert(!contains(s, 3), "!Intersect 3")
  124.     }
  125.   }
  126.  
  127.   test("diff contains elements exclusive elements of first set") {
  128.     new TestSets {
  129.       val s = diff(s1us2, s1)
  130.  
  131.       assert(!contains(s, 1), "!Diff 1")
  132.       assert(contains(s, 2), "Diff 2")
  133.     }
  134.   }
  135.  
  136.   test("filter contains only those elements of set that satisfy the predicate") {
  137.     new TestSets {
  138.       val notOne = (x: Int) => x != 1
  139.       val s = filter(s1us2, notOne)
  140.  
  141.       assert(!contains(s, 1), "!Filter 1")
  142.       assert(contains(s, 2), "Filter 2")
  143.  
  144.     }
  145.   }
  146.  
  147.   test("forAll checks all elements in set satisfy the predicate") {
  148.     new TestSets {
  149.       val notFour = (x: Int) => x != 4
  150.       val notOne = (x: Int) => x != 1
  151.  
  152.       assert(!forall(s1us2us3, notOne), "!forAll one")
  153.       assert(forall(s1us2us3, notFour), "forAll four")
  154.     }
  155.   }
  156.  
  157.   test("exists returns if at least one element of set satisfies the predicate") {
  158.     new TestSets {
  159.       val isOne = (x: Int) => x == 1
  160.  
  161.       assert(exists(s1us2us3, isOne), "exists one")
  162.     }
  163.   }
  164.  
  165.   test("contains on a `mapped` set checks if supplied value exists as a result of mapped function in x") {
  166.     new TestSets {
  167.       val trf = (x: Int) => x * 2
  168.       val s = map(s1us2us3, trf)
  169.  
  170.       assert(contains(s, 2), "Map contains 2")
  171.       assert(contains(s, 4), "Map contains 4")
  172.       assert(contains(s, 6), "Map contains 6")
  173.       assert(!contains(s, 1), "!Map contains 1")
  174.       assert(!contains(s, 3), "!Map contains 3")
  175.     }
  176.   }
  177.  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement