Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import org.scalatest.FunSuite
- import org.junit.runner.RunWith
- import org.scalatest.junit.JUnitRunner
- @RunWith(classOf[JUnitRunner])
- class TweetSetSuite extends FunSuite {
- trait TestSets {
- val set1 = new Empty
- val set2 = set1.incl(new Tweet("a", "a body", 20))
- val set3 = set2.incl(new Tweet("b", "b body", 20))
- val c = new Tweet("c", "c body", 7)
- val d = new Tweet("d", "d body", 9)
- val set4c = set3.incl(c)
- val set4d = set3.incl(d)
- val set5 = set4c.incl(d)
- val set6 = set5.incl(new Tweet("x", "e body", 12))
- val set7 = set6.incl(new Tweet("xsad", "f body", 12))
- val set8 = set7.incl(new Tweet("xg", "g body", 123))
- val set9 = set8.incl(new Tweet("xgdf", "h baaaaody", 111))
- val set10 = set9.incl(new Tweet("xuty", "i bxxxody", 1534))
- val set11 = set10.incl(new Tweet("xiyu", "j boqqqdy", 163))
- val set12 = set11.incl(new Tweet("nmx", "k bqqqqody", 634))
- val set13 = set12.incl(new Tweet("nvbx", "l bodaadsdsady", 6342))
- val set14 = set13.incl(new Tweet("vcxx", "m badasdsody", 1342))
- val set15 = set14.incl(new Tweet("sdfx", "n bdsdaody", 862))
- val set16 = set15.incl(new Tweet("vxcx", "osad body", 8672))
- val set17 = set16.incl(new Tweet("werx", "p body", 1))
- val set18 = set17.incl(new Tweet("vxcx", "radx body", 1978))
- val set19 = set18.incl(new Tweet("bvbx", "s body", 9))
- def setN(n: Int): TweetSet = {
- def go(x: Int, acc: TweetSet): TweetSet =
- if(x == 0) acc
- else go(x - 1, acc.incl(new Tweet("T" * x, "B" * x, x)))
- go(n, new Empty)
- }
- }
- def asSet(tweets: TweetSet): Set[Tweet] = {
- var res = Set[Tweet]()
- tweets.foreach(res += _)
- res
- }
- def size(set: TweetSet): Int = asSet(set).size
- test("filter: on empty set") {
- new TestSets {
- assert(size(set1.filter(tw => tw.user == "a")) === 0)
- }
- }
- test("filter: a on set5") {
- new TestSets {
- assert(size(set5.filter(tw => tw.user == "a")) === 1)
- }
- }
- test("filter: 20 on set5") {
- new TestSets {
- assert(size(set5.filter(tw => tw.retweets == 20)) === 2)
- }
- }
- test("filter true on set5") {
- new TestSets {
- assert(size(set5.filter(_ => true)) === 4)
- }
- }
- test("filter true on set19") {
- new TestSets {
- assert(size(set19.filter(_ => true)) === 18)
- }
- }
- test("filter: true on set50") {
- new TestSets {
- assert(size(setN(50).filter(_ => true)) === 50)
- }
- }
- test("filter: true on set80") {
- new TestSets {
- assert(size(setN(80).filter(_ => true)) === 80)
- }
- }
- test("union: set4c and set4d") {
- new TestSets {
- assert(size(set4c.union(set4d)) === 4)
- }
- }
- test("union: with empty set (1)") {
- new TestSets {
- assert(size(set5.union(set1)) === 4)
- }
- }
- test("union: with empty set (2)") {
- new TestSets {
- assert(size(set1.union(set5)) === 4)
- }
- }
- test("max on set19") {
- new TestSets {
- assert(set19.mostRetweeted.retweets === 8672)
- }
- }
- test("descending") {
- new TestSets {
- val tr = set19.descendingByRetweet
- assert(!tr.isEmpty)
- def go(last: Int, xs: TweetList): Unit = {
- if(!xs.isEmpty)
- assert(last > xs.head.retweets)
- go(xs.head.retweets, xs.tail)
- }
- }
- }
- test("descending: set5") {
- new TestSets {
- val trends = set5.descendingByRetweet
- assert(!trends.isEmpty)
- assert(trends.head.user == "a" || trends.head.user == "b")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement