Advertisement
Guest User

Untitled

a guest
Sep 28th, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.55 KB | None | 0 0
  1.  
  2. import org.scalatest.FunSuite
  3.  
  4. import org.junit.runner.RunWith
  5. import org.scalatest.junit.JUnitRunner
  6.  
  7. @RunWith(classOf[JUnitRunner])
  8. class TweetSetSuite extends FunSuite {
  9.   trait TestSets {
  10.     val set1 = new Empty
  11.     val set2 = set1.incl(new Tweet("a", "a body", 20))
  12.     val set3 = set2.incl(new Tweet("b", "b body", 20))
  13.     val c = new Tweet("c", "c body", 7)
  14.     val d = new Tweet("d", "d body", 9)
  15.     val set4c = set3.incl(c)
  16.     val set4d = set3.incl(d)
  17.     val set5 = set4c.incl(d)
  18.     val set6 = set5.incl(new Tweet("x", "e body", 12))
  19.     val set7 = set6.incl(new Tweet("xsad", "f body", 12))
  20.     val set8 = set7.incl(new Tweet("xg", "g body", 123))
  21.     val set9 = set8.incl(new Tweet("xgdf", "h baaaaody", 111))
  22.     val set10 = set9.incl(new Tweet("xuty", "i bxxxody", 1534))
  23.     val set11 = set10.incl(new Tweet("xiyu", "j boqqqdy", 163))
  24.     val set12 = set11.incl(new Tweet("nmx", "k bqqqqody", 634))
  25.     val set13 = set12.incl(new Tweet("nvbx", "l bodaadsdsady", 6342))
  26.     val set14 = set13.incl(new Tweet("vcxx", "m badasdsody", 1342))
  27.     val set15 = set14.incl(new Tweet("sdfx", "n bdsdaody", 862))
  28.     val set16 = set15.incl(new Tweet("vxcx", "osad body", 8672))
  29.     val set17 = set16.incl(new Tweet("werx", "p body", 1))
  30.     val set18 = set17.incl(new Tweet("vxcx", "radx body", 1978))
  31.     val set19 = set18.incl(new Tweet("bvbx", "s body", 9))
  32.  
  33.     def setN(n: Int): TweetSet = {
  34.       def go(x: Int, acc: TweetSet): TweetSet =
  35.         if(x == 0) acc
  36.         else go(x - 1, acc.incl(new Tweet("T" * x, "B" * x, x)))
  37.       go(n, new Empty)
  38.     }
  39.      
  40.   }
  41.  
  42.   def asSet(tweets: TweetSet): Set[Tweet] = {
  43.     var res = Set[Tweet]()
  44.     tweets.foreach(res += _)
  45.     res
  46.   }
  47.  
  48.   def size(set: TweetSet): Int = asSet(set).size
  49.  
  50.   test("filter: on empty set") {
  51.     new TestSets {
  52.       assert(size(set1.filter(tw => tw.user == "a")) === 0)
  53.     }
  54.   }
  55.  
  56.   test("filter: a on set5") {
  57.     new TestSets {
  58.       assert(size(set5.filter(tw => tw.user == "a")) === 1)
  59.     }
  60.   }
  61.  
  62.   test("filter: 20 on set5") {
  63.     new TestSets {
  64.       assert(size(set5.filter(tw => tw.retweets == 20)) === 2)
  65.     }
  66.   }
  67.  
  68.   test("filter true on set5") {
  69.     new TestSets {
  70.       assert(size(set5.filter(_ => true)) === 4)
  71.     }
  72.   }
  73.  
  74.   test("filter true on set19") {
  75.     new TestSets {
  76.       assert(size(set19.filter(_ => true)) === 18)
  77.     }
  78.   }
  79.  
  80.   test("filter: true on set50") {
  81.     new TestSets {
  82.       assert(size(setN(50).filter(_ => true)) === 50)
  83.     }
  84.   }
  85.  
  86.   test("filter: true on set80") {
  87.     new TestSets {
  88.       assert(size(setN(80).filter(_ => true)) === 80)
  89.     }
  90.   }
  91.  
  92.   test("union: set4c and set4d") {
  93.     new TestSets {
  94.       assert(size(set4c.union(set4d)) === 4)
  95.     }
  96.   }
  97.  
  98.   test("union: with empty set (1)") {
  99.     new TestSets {
  100.       assert(size(set5.union(set1)) === 4)
  101.     }
  102.   }
  103.  
  104.   test("union: with empty set (2)") {
  105.     new TestSets {
  106.       assert(size(set1.union(set5)) === 4)
  107.     }
  108.   }
  109.  
  110.   test("max on set19") {
  111.     new TestSets {
  112.       assert(set19.mostRetweeted.retweets === 8672)
  113.     }
  114.   }
  115.  
  116.   test("descending") {
  117.     new TestSets {
  118.       val tr = set19.descendingByRetweet
  119.       assert(!tr.isEmpty)
  120.       def go(last: Int, xs: TweetList): Unit = {
  121.         if(!xs.isEmpty)
  122.           assert(last > xs.head.retweets)
  123.           go(xs.head.retweets, xs.tail)
  124.       }
  125.     }
  126.   }
  127.  
  128.   test("descending: set5") {
  129.     new TestSets {
  130.       val trends = set5.descendingByRetweet
  131.       assert(!trends.isEmpty)
  132.       assert(trends.head.user == "a" || trends.head.user == "b")
  133.     }
  134.   }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement