Assignment 6 (work in progress).
[Scala.git] / Assignment_06 / src / test / scala / AnagramsTests.scala
diff --git a/Assignment_06/src/test/scala/AnagramsTests.scala b/Assignment_06/src/test/scala/AnagramsTests.scala
new file mode 100644 (file)
index 0000000..6efda03
--- /dev/null
@@ -0,0 +1,137 @@
+package tests
+
+import org.scalatest.FunSuite
+import org.junit.runner.RunWith
+import org.scalatest.junit.JUnitRunner
+import anagrams.Anagrams._
+
+@RunWith(classOf[JUnitRunner])
+class AnagramsTests extends FunSuite {
+       /**
+        * Test suite for question 1
+        */
+       test("wordOccurrences: empty string") {
+               assert(wordOccurrences("") === List())
+       }
+
+       test("wordOccurrences: \"abcd\"") {
+               assert(wordOccurrences("abcd") === List(('a', 1), ('b', 1), ('c', 1), ('d', 1)))
+       }
+
+       test("wordOccurrences: \"aabcdeee\"") {
+               assert(wordOccurrences("aabcdeee") === List(('a', 2), ('b', 1), ('c', 1), ('d', 1), ('e', 3)))
+       }
+
+       test("wordOccurrences: \"Robert\"") {
+               assert(wordOccurrences("Robert") === List(('b', 1), ('e', 1), ('o', 1), ('r', 2), ('t', 1)))
+       }
+       
+       /***
+        * Test suite for question 2
+        */
+       test("""dictionaryByOccurrences for "eat" """) {
+               assert(dictionaryByOccurrences.get(List(('a', 1), ('e', 1), ('t', 1))).map(_.toSet) === Some(Set("ate", "eat", "tea")))
+       }
+       
+       /***
+        * Test suite for question 3
+        */
+       test("""wordAnagrams for "married" """) {
+               assert(wordAnagrams("married").toSet === Set("married", "admirer"))
+       }
+
+       test("""wordAnagrams for "mushroom" """) {
+               assert(wordAnagrams("mushroom").toSet === Set("mushroom"))
+       }
+
+       test("""wordAnagrams for "player" """) {
+               assert(wordAnagrams("player").toSet === Set("parley", "pearly", "player", "replay"))
+       }
+       
+       /**
+        * Test suite for question 4
+        */
+       test("combinations: empty combination []") {
+               assert(combinations(Nil) === List(Nil))
+       }       
+
+       test("combinations: \"aa\"") {
+               val aa = List(('a', 2))
+               val aacomb = List(
+                       List(),
+                       List(('a', 1)),
+                       List(('a', 2))
+                       )
+               assert(combinations(aa).toSet === aacomb.toSet)
+       }
+       
+       test("combinations: \"abba\"") {
+               val abba = List(('a', 2), ('b', 2))
+               val abbacomb = List(
+                       List(),
+                       List(('a', 1)),
+                       List(('a', 2)),
+                       List(('b', 1)),
+                       List(('a', 1), ('b', 1)),
+                       List(('a', 2), ('b', 1)),
+                       List(('b', 2)),
+                       List(('a', 1), ('b', 2)),
+                       List(('a', 2), ('b', 2)))
+               assert(combinations(abba).toSet === abbacomb.toSet)
+       }
+       
+       /**
+        * Test suite for question 5
+        */
+       test("subtract: \"lard\" - \"r\"") {
+               val lard = List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
+               val r = List(('r', 1))
+               val lad = List(('a', 1), ('d', 1), ('l', 1))
+               assert(subtract(lard, r) === lad)
+       }
+       
+       test("subtract: \"lard\" - \"\"") {
+               val lard = List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
+               val empty = List()              
+               assert(subtract(lard, empty) === lard)
+       }
+       
+       test("subtract: \"lard\" - \"lard\"") {
+               val lard = List(('a', 1), ('d', 1), ('l', 1), ('r', 1))                         
+               assert(subtract(lard, lard) === List())
+       }
+       
+       /**
+        * Test suite for question 6
+        */
+       test("sentence anagrams: []") {
+               val sentence = List()
+               assert(sentenceAnagrams(sentence) === List(Nil))
+       }
+
+       test("sentence anagrams: Linux rulez") {
+               val sentence = List("Linux", "rulez")
+               val anas = List(
+                       List("Rex", "Lin", "Zulu"),
+                       List("nil", "Zulu", "Rex"),
+                       List("Rex", "nil", "Zulu"),
+                       List("Zulu", "Rex", "Lin"),
+                       List("null", "Uzi", "Rex"),
+                       List("Rex", "Zulu", "Lin"),
+                       List("Uzi", "null", "Rex"),
+                       List("Rex", "null", "Uzi"),
+                       List("null", "Rex", "Uzi"),
+                       List("Lin", "Rex", "Zulu"),
+                       List("nil", "Rex", "Zulu"),
+                       List("Rex", "Uzi", "null"),
+                       List("Rex", "Zulu", "nil"),
+                       List("Zulu", "Rex", "nil"),
+                       List("Zulu", "Lin", "Rex"),
+                       List("Lin", "Zulu", "Rex"),
+                       List("Uzi", "Rex", "null"),
+                       List("Zulu", "nil", "Rex"),
+                       List("rulez", "Linux"),
+                       List("Linux", "rulez"))
+               assert(sentenceAnagrams(sentence).toSet === anas.toSet)
+       }
+}
\ No newline at end of file