Assignment 6 (work in progress).
[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 /**
84 * Test suite for question 5
85 */
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)
91 }
92
93 test("subtract: \"lard\" - \"\"") {
94 val lard = List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
95 val empty = List()
96 assert(subtract(lard, empty) === lard)
97 }
98
99 test("subtract: \"lard\" - \"lard\"") {
100 val lard = List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
101 assert(subtract(lard, lard) === List())
102 }
103
104 /**
105 * Test suite for question 6
106 */
107 test("sentence anagrams: []") {
108 val sentence = List()
109 assert(sentenceAnagrams(sentence) === List(Nil))
110 }
111
112 test("sentence anagrams: Linux rulez") {
113 val sentence = List("Linux", "rulez")
114 val anas = List(
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)
136 }
137 }