X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;ds=sidebyside;f=Assignment_06%2Fsrc%2Ftest%2Fscala%2FAnagramsTests.scala;fp=Assignment_06%2Fsrc%2Ftest%2Fscala%2FAnagramsTests.scala;h=6efda032e0a9db1da5936f8a3b778be1ad2bcc94;hb=6c413508bbba63f2421fd57d87eba4266c5beca0;hp=0000000000000000000000000000000000000000;hpb=3caf6fced99f7fe7d910dfdc3baa9ade89145848;p=Scala.git diff --git a/Assignment_06/src/test/scala/AnagramsTests.scala b/Assignment_06/src/test/scala/AnagramsTests.scala new file mode 100644 index 0000000..6efda03 --- /dev/null +++ b/Assignment_06/src/test/scala/AnagramsTests.scala @@ -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