Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package patmat
- import org.scalatest.FunSuite
- import org.junit.runner.RunWith
- import org.scalatest.junit.JUnitRunner
- import patmat.Huffman._
- @RunWith(classOf[JUnitRunner])
- class HuffmanSuite extends FunSuite {
- trait TestTrees {
- val t1 = Fork(Leaf('a',2), Leaf('b',3), List('a','b'), 5)
- val t2 = Fork(Fork(Leaf('a',2), Leaf('b',3), List('a','b'), 5), Leaf('d',4), List('a','b','d'), 9)
- }
- test("weight of a larger tree") {
- new TestTrees {
- assert(weight(t1) === 5)
- }
- }
- test("chars of a larger tree") {
- new TestTrees {
- assert(chars(t2) === List('a','b','d'))
- }
- }
- test("string2chars(\"hello, world\")") {
- assert(string2Chars("hello, world") === List('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd'))
- }
- test("times a char is in a list - using pack and the exercise version of encode") {
- val cs: List[Char] = List('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd')
- assert(times(cs) === List((' ', 1), (',', 1), ('d', 1), ('e', 1), ('h', 1), ('l', 3), ('o', 2), ('r', 1), ('w',1)))
- }
- test("makeOrderedLeafList for some frequency table") {
- assert(makeOrderedLeafList(List(('t', 2), ('e', 1), ('x', 3))) === List(Leaf('e',1), Leaf('t',2), Leaf('x',3)))
- }
- test("test trees singleton?") {
- assert(singleton(List(Leaf('e', 1))))
- assert(!singleton(List(Leaf('e', 1), Leaf('t', 2), Leaf('x', 4))))
- }
- test("combine of some leaf list with 3 leafs, Codetree's") {
- val leaflist = List(Leaf('e', 1), Leaf('t', 2), Leaf('x', 4))
- assert(combine(leaflist) === List(Fork(Leaf('e',1),Leaf('t',2),List('e', 't'),3), Leaf('x',4)))
- }
- test("combine of some leaf list with 4 leafs, Codetree's") {
- val leaflist = List(Leaf('e', 1), Leaf('t', 2), Leaf('x', 4), Leaf('z', 5))
- assert(combine(leaflist) === List(Fork(Leaf('e',1),Leaf('t',2),List('e', 't'),3), Leaf('x', 4), Leaf('z', 5)))
- }
- test("combine of some leaf list w. 2 leafs, Codetree's which should remain the same") {
- val leaflist = List(Leaf('e', 1), Leaf('t', 2))
- assert(combine(leaflist) === List(Leaf('e', 1), Leaf('t', 2)))
- }
- test("integration test using times | makeOrderedLeafList | combine on 'hello, world'") {
- val cs: List[Char] = List('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd')
- assert(combine(makeOrderedLeafList(times(cs))) ===
- 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)))
- }
- test("until some singleton leaf list ") {
- val leaflist = List(Leaf('e', 1), Leaf('t', 2), Leaf('x', 4))
- assert(until(singleton, combine)(leaflist) === List(Fork(Leaf('e',1),Leaf('t',2),List('e', 't'),3), Leaf('x',4)))
- }
- ignore("until some singleton leaf list 2") {
- val leaflist = List(Leaf('e', 1), Leaf('t', 2))
- assert(until(singleton, combine)(leaflist) === List(Leaf('t', 2)))
- }
- test("createCodeTree") {
- val cs: List[Char] = List('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd')
- val codeTree: CodeTree = createCodeTree(cs)
- printCodeTree(codeTree)
- assert(codeTree === Leaf('l',3))
- // well had expected head to be Fork(....)?
- // Fork(Leaf('e',1), Leaf('t',2), List('e', 't'),3)
- }
- ignore("decode and encode a very short text should be identity") {
- new TestTrees {
- assert(decode(t1, encode(t1)("ab".toList)) === "ab".toList)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement