diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2013-04-28 00:33:06 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2016-11-10 18:03:23 +0100 |
commit | 3e2f88773c61e2d0020b57161d0e901bec596c96 (patch) | |
tree | fe5c381e4a731600a60b4ac9862af672b5b7aa12 /Scala/patmat/src/test | |
parent | 12ef23e6da7b7186bec20d426c087d757260659d (diff) | |
download | coursera-3e2f88773c61e2d0020b57161d0e901bec596c96.zip coursera-3e2f88773c61e2d0020b57161d0e901bec596c96.tar.gz |
Scala : add patmat
Diffstat (limited to 'Scala/patmat/src/test')
-rw-r--r-- | Scala/patmat/src/test/scala/patmat/HuffmanSuite.scala | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Scala/patmat/src/test/scala/patmat/HuffmanSuite.scala b/Scala/patmat/src/test/scala/patmat/HuffmanSuite.scala new file mode 100644 index 0000000..3881871 --- /dev/null +++ b/Scala/patmat/src/test/scala/patmat/HuffmanSuite.scala @@ -0,0 +1,47 @@ +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("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("combine of some leaf list") { + 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("decode and encode a very short text should be identity") { + new TestTrees { + assert(decode(t1, encode(t1)("ab".toList)) === "ab".toList) + } + } +} |