Assignment 6 finished.
[Scala.git] / Assignment_06 / src / test / scala / AnagramsTests.scala
1 package tests
2
3 import org.scalatest.FunSuite
4 import org.junit.runner.RunWith
5 import org.scalatest.junit.JUnitRunner
6 import anagrams.Anagrams._
7
8 @RunWith(classOf[JUnitRunner])
9 class AnagramsTests extends FunSuite {
10 /**
11 * Test suite for question 1
12 */
13 test("wordOccurrences: empty string") {
14 assert(wordOccurrences("") === List())
15 }
16
17 test("wordOccurrences: \"abcd\"") {
18 assert(wordOccurrences("abcd") === List(('a', 1), ('b', 1), ('c', 1), ('d', 1)))
19 }
20
21 test("wordOccurrences: \"aabcdeee\"") {
22 assert(wordOccurrences("aabcdeee") === List(('a', 2), ('b', 1), ('c', 1), ('d', 1), ('e', 3)))
23 }
24
25 test("wordOccurrences: \"Robert\"") {
26 assert(wordOccurrences("Robert") === List(('b', 1), ('e', 1), ('o', 1), ('r', 2), ('t', 1)))
27 }
28
29 /***
30 * Test suite for question 2
31 */
32 test("""dictionaryByOccurrences for "eat" """) {
33 assert(dictionaryByOccurrences.get(List(('a', 1), ('e', 1), ('t', 1))).map(_.toSet) === Some(Set("ate", "eat", "tea")))
34 }
35
36 /***
37 * Test suite for question 3
38 */
39 test("""wordAnagrams for "married" """) {
40 assert(wordAnagrams("married").toSet === Set("married", "admirer"))
41 }
42
43 test("""wordAnagrams for "mushroom" """) {
44 assert(wordAnagrams("mushroom").toSet === Set("mushroom"))
45 }
46
47 test("""wordAnagrams for "player" """) {
48 assert(wordAnagrams("player").toSet === Set("parley", "pearly", "player", "replay"))
49 }
50
51 /**
52 * Test suite for question 4
53 */
54 test("combinations: empty combination []") {
55 assert(combinations(Nil) === List(Nil))
56 }
57
58 test("combinations: \"aa\"") {
59 val aa = List(('a', 2))
60 val aacomb = List(
61 List(),
62 List(('a', 1)),
63 List(('a', 2))
64 )
65 assert(combinations(aa).toSet === aacomb.toSet)
66 }
67
68 test("combinations: \"abba\"") {
69 val abba = List(('a', 2), ('b', 2))
70 val abbacomb = List(
71 List(),
72 List(('a', 1)),
73 List(('a', 2)),
74 List(('b', 1)),
75 List(('a', 1), ('b', 1)),
76 List(('a', 2), ('b', 1)),
77 List(('b', 2)),
78 List(('a', 1), ('b', 2)),
79 List(('a', 2), ('b', 2)))
80 assert(combinations(abba).toSet === abbacomb.toSet)
81 }
82
83 test("combinations: \"abbac\"") {
84 val abbac = List(('a', 2), ('b', 2), ('c', 1))
85 val abbaccomb = List(
86 List(),
87 List(('a', 1)),
88 List(('a', 2)),
89 List(('b', 1)),
90 List(('c', 1)),
91 List(('a', 1), ('b', 1)),
92 List(('a', 2), ('b', 1)),
93 List(('b', 2)),
94 List(('a', 1), ('b', 2)),
95 List(('a', 2), ('b', 2)),
96 List(('a', 1), ('c', 1)),
97 List(('a', 2), ('c', 1)),
98 List(('b', 1), ('c', 1)),
99 List(('b', 2), ('c', 1)),
100 List(('a', 1), ('b', 1), ('c', 1)),
101 List(('a', 2), ('b', 1), ('c', 1)),
102 List(('a', 1), ('b', 2), ('c', 1)),
103 List(('a', 2), ('b', 2), ('c', 1)))
104 assert(combinations(abbac).toSet === abbaccomb.toSet)
105 }
106
107 /**
108 * Test suite for question 5
109 */
110 test("subtract: \"lard\" - \"r\"") {
111 val lard = List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
112 val r = List(('r', 1))
113 val lad = List(('a', 1), ('d', 1), ('l', 1))
114 assert(subtract(lard, r) === lad)
115 }
116
117 test("subtract: \"lard\" - \"\"") {
118 val lard = List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
119 val empty = List()
120 assert(subtract(lard, empty) === lard)
121 }
122
123 test("subtract: \"lard\" - \"lard\"") {
124 val lard = List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
125 assert(subtract(lard, lard) === List())
126 }
127
128 /**
129 * Test suite for question 6
130 */
131 test("sentence anagrams: []") {
132 val sentence = List()
133 assert(sentenceAnagrams(sentence) === List(Nil))
134 }
135
136 test("sentence anagrams: Linux rulez") {
137 val sentence = List("Linux", "rulez")
138 val anas = List(
139 List("Rex", "Lin", "Zulu"),
140 List("nil", "Zulu", "Rex"),
141 List("Rex", "nil", "Zulu"),
142 List("Zulu", "Rex", "Lin"),
143 List("null", "Uzi", "Rex"),
144 List("Rex", "Zulu", "Lin"),
145 List("Uzi", "null", "Rex"),
146 List("Rex", "null", "Uzi"),
147 List("null", "Rex", "Uzi"),
148 List("Lin", "Rex", "Zulu"),
149 List("nil", "Rex", "Zulu"),
150 List("Rex", "Uzi", "null"),
151 List("Rex", "Zulu", "nil"),
152 List("Zulu", "Rex", "nil"),
153 List("Zulu", "Lin", "Rex"),
154 List("Lin", "Zulu", "Rex"),
155 List("Uzi", "Rex", "null"),
156 List("Zulu", "nil", "Rex"),
157 List("rulez", "Linux"),
158 List("Linux", "rulez"))
159 assert(sentenceAnagrams(sentence).toSet === anas.toSet)
160 }
161 }