6efda032e0a9db1da5936f8a3b778be1ad2bcc94
3 import org
.scalatest
.FunSuite
4 import org
.junit
.runner
.RunWith
5 import org
.scalatest
.junit
.JUnitRunner
6 import anagrams
.Anagrams
._
8 @RunWith(classOf
[JUnitRunner
])
9 class AnagramsTests
extends FunSuite
{
11 * Test suite for question 1
13 test("wordOccurrences: empty string") {
14 assert(wordOccurrences("") === List())
17 test("wordOccurrences: \"abcd\"") {
18 assert(wordOccurrences("abcd") === List(('a', 1), ('b', 1), ('c', 1), ('d', 1)))
21 test("wordOccurrences: \"aabcdeee\"") {
22 assert(wordOccurrences("aabcdeee") === List(('a', 2), ('b', 1), ('c', 1), ('d', 1), ('e', 3)))
25 test("wordOccurrences: \"Robert\"") {
26 assert(wordOccurrences("Robert") === List(('b', 1), ('e', 1), ('o', 1), ('r', 2), ('t', 1)))
30 * Test suite for question 2
32 test("""dictionaryByOccurrences for "eat" """) {
33 assert(dictionaryByOccurrences
.get(List(('a', 1), ('e', 1), ('t', 1))).map(_
.toSet
) === Some(Set("ate", "eat", "tea")))
37 * Test suite for question 3
39 test("""wordAnagrams for "married" """) {
40 assert(wordAnagrams("married").toSet
=== Set("married", "admirer"))
43 test("""wordAnagrams for "mushroom" """) {
44 assert(wordAnagrams("mushroom").toSet
=== Set("mushroom"))
47 test("""wordAnagrams for "player" """) {
48 assert(wordAnagrams("player").toSet
=== Set("parley", "pearly", "player", "replay"))
52 * Test suite for question 4
54 test("combinations: empty combination []") {
55 assert(combinations(Nil
) === List(Nil
))
58 test("combinations: \"aa\"") {
59 val aa
= List(('a', 2))
65 assert(combinations(aa
).toSet
=== aacomb
.toSet
)
68 test("combinations: \"abba\"") {
69 val abba
= List(('a', 2), ('b', 2))
75 List(('a', 1), ('b', 1)),
76 List(('a', 2), ('b', 1)),
78 List(('a', 1), ('b', 2)),
79 List(('a', 2), ('b', 2)))
80 assert(combinations(abba
).toSet
=== abbacomb
.toSet
)
84 * Test suite for question 5
86 test("subtract: \"lard\" - \"r\"") {
87 val lard
= List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
88 val r
= List(('r', 1))
89 val lad
= List(('a', 1), ('d', 1), ('l', 1))
90 assert(subtract(lard
, r
) === lad
)
93 test("subtract: \"lard\" - \"\"") {
94 val lard
= List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
96 assert(subtract(lard
, empty
) === lard
)
99 test("subtract: \"lard\" - \"lard\"") {
100 val lard
= List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
101 assert(subtract(lard
, lard
) === List())
105 * Test suite for question 6
107 test("sentence anagrams: []") {
108 val sentence
= List()
109 assert(sentenceAnagrams(sentence
) === List(Nil
))
112 test("sentence anagrams: Linux rulez") {
113 val sentence
= List("Linux", "rulez")
115 List("Rex", "Lin", "Zulu"),
116 List("nil", "Zulu", "Rex"),
117 List("Rex", "nil", "Zulu"),
118 List("Zulu", "Rex", "Lin"),
119 List("null", "Uzi", "Rex"),
120 List("Rex", "Zulu", "Lin"),
121 List("Uzi", "null", "Rex"),
122 List("Rex", "null", "Uzi"),
123 List("null", "Rex", "Uzi"),
124 List("Lin", "Rex", "Zulu"),
125 List("nil", "Rex", "Zulu"),
126 List("Rex", "Uzi", "null"),
127 List("Rex", "Zulu", "nil"),
128 List("Zulu", "Rex", "nil"),
129 List("Zulu", "Lin", "Rex"),
130 List("Lin", "Zulu", "Rex"),
131 List("Uzi", "Rex", "null"),
132 List("Zulu", "nil", "Rex"),
133 List("rulez", "Linux"),
134 List("Linux", "rulez"))
135 assert(sentenceAnagrams(sentence
).toSet
=== anas
.toSet
)