Advertisement
pellekrogholt

Untitled

Oct 24th, 2012
743
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 3.49 KB | None | 0 0
  1. package patmat
  2.  
  3. import org.scalatest.FunSuite
  4.  
  5. import org.junit.runner.RunWith
  6. import org.scalatest.junit.JUnitRunner
  7.  
  8. import patmat.Huffman._
  9.  
  10. @RunWith(classOf[JUnitRunner])
  11. class HuffmanSuite extends FunSuite {
  12.   trait TestTrees {
  13.     val t1 = Fork(Leaf('a',2), Leaf('b',3), List('a','b'), 5)
  14.     val t2 = Fork(Fork(Leaf('a',2), Leaf('b',3), List('a','b'), 5), Leaf('d',4), List('a','b','d'), 9)
  15.   }
  16.  
  17.   test("weight of a larger tree") {
  18.     new TestTrees {
  19.       assert(weight(t1) === 5)
  20.     }
  21.   }
  22.  
  23.   test("chars of a larger tree") {
  24.     new TestTrees {
  25.       assert(chars(t2) === List('a','b','d'))
  26.     }
  27.   }
  28.  
  29.   test("string2chars(\"hello, world\")") {
  30.     assert(string2Chars("hello, world") === List('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd'))
  31.   }
  32.  
  33.  
  34.   test("times a char is in a list - using pack and the exercise version of encode") {
  35.     val cs: List[Char] = List('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd')
  36.     assert(times(cs) === List((' ', 1), (',', 1), ('d', 1), ('e', 1), ('h', 1), ('l', 3), ('o', 2), ('r', 1), ('w',1)))
  37.   }  
  38.  
  39.   test("makeOrderedLeafList for some frequency table") {
  40.     assert(makeOrderedLeafList(List(('t', 2), ('e', 1), ('x', 3))) === List(Leaf('e',1), Leaf('t',2), Leaf('x',3)))
  41.   }
  42.  
  43.  
  44.   test("test trees singleton?") {
  45.     assert(singleton(List(Leaf('e', 1))))    
  46.     assert(!singleton(List(Leaf('e', 1), Leaf('t', 2), Leaf('x', 4))))
  47.   }
  48.  
  49.  
  50.   test("combine of some leaf list with 3 leafs, Codetree's") {
  51.     val leaflist = List(Leaf('e', 1), Leaf('t', 2), Leaf('x', 4))
  52.     assert(combine(leaflist) === List(Fork(Leaf('e',1),Leaf('t',2),List('e', 't'),3), Leaf('x',4)))
  53.   }
  54.  
  55.  
  56.   test("combine of some leaf list with 4 leafs, Codetree's") {
  57.     val leaflist = List(Leaf('e', 1), Leaf('t', 2), Leaf('x', 4), Leaf('z', 5))
  58.     assert(combine(leaflist) === List(Fork(Leaf('e',1),Leaf('t',2),List('e', 't'),3), Leaf('x', 4), Leaf('z', 5)))
  59.   }
  60.  
  61.   test("combine of some leaf list w. 2 leafs, Codetree's which should remain the same") {
  62.     val leaflist = List(Leaf('e', 1), Leaf('t', 2))
  63.     assert(combine(leaflist) === List(Leaf('e', 1), Leaf('t', 2)))
  64.   }
  65.  
  66.   test("integration test using times | makeOrderedLeafList | combine  on 'hello, world'") {
  67.     val cs: List[Char] = List('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd')
  68.     assert(combine(makeOrderedLeafList(times(cs))) ===
  69.       List(Leaf('d',1), Leaf('e',1), Leaf('h',1), Leaf('r',1), Leaf('w',1), Fork(Leaf(' ',1),Leaf(',',1),List(' ', ','),2), Leaf('o',2), Leaf('l',3)))  
  70.  
  71.   }
  72.  
  73.   test("until some singleton leaf list ") {
  74.     val leaflist = List(Leaf('e', 1), Leaf('t', 2), Leaf('x', 4))
  75.     assert(until(singleton, combine)(leaflist) === List(Fork(Leaf('e',1),Leaf('t',2),List('e', 't'),3), Leaf('x',4)))
  76.   }
  77.  
  78.   ignore("until some singleton leaf list 2") {
  79.     val leaflist = List(Leaf('e', 1), Leaf('t', 2))
  80.     assert(until(singleton, combine)(leaflist) === List(Leaf('t', 2)))
  81.   }
  82.  
  83.  
  84.   test("createCodeTree") {
  85.     val cs: List[Char] = List('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd')
  86.    
  87.     val codeTree: CodeTree = createCodeTree(cs)
  88.  
  89.     printCodeTree(codeTree)
  90.    
  91.     assert(codeTree === Leaf('l',3))
  92.    
  93.     // well had expected head to be Fork(....)?
  94.     // Fork(Leaf('e',1), Leaf('t',2), List('e', 't'),3)
  95.   }
  96.  
  97.  
  98.  
  99.  
  100.   ignore("decode and encode a very short text should be identity") {
  101.     new TestTrees {
  102.       assert(decode(t1, encode(t1)("ab".toList)) === "ab".toList)
  103.     }
  104.   }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement