-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDict.java
More file actions
241 lines (215 loc) · 6.55 KB
/
Copy pathTestDict.java
File metadata and controls
241 lines (215 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import java.io.*;
public class TestDict {
public static void main(String[] args) {
final String TEXT = "text";
OrderedDictionary dictionary = new OrderedDictionary();
Record rec;
Pair key;
String words[] = {"homework", "course", "class", "computer", "four"};
String definitions[] = {"Very enjoyable work that students need to complete outside the classroom.",
"A series of talks or lessons. For example, CS210.",
"A set of students taught together,",
"An electronic machine frequently used by Computer Science students.",
"One more than three"};
Pair[] keys = new Pair[5];
Record[] records = new Record[5];
for (int i = 0; i < 5; ++i) {
keys[i] = new Pair(words[i],TEXT);
records[i] = new Record(keys[i],definitions[i]);
}
// Insert one word and then find it
try {
dictionary.put(records[0]);
rec = dictionary.get(keys[0]);
if ((rec.getData()).compareTo(definitions[0]) == 0)
System.out.println("Test 1 passed");
else System.out.println("Test 1 failed");
}
catch(Exception e) {
System.out.println("Test 1 failed");
}
// Try to find an inexistent word
try {
rec = dictionary.get(keys[1]);
if (rec == null) System.out.println("Test 2 passed");
else System.out.println("Test 2 failed");
}
catch(Exception e) {
System.out.println("Test 2 failed");
}
// Try to insert the same word again
try {
dictionary.put(records[0]);
System.out.println("Test 3 failed");
}
catch(DictionaryException e) {
System.out.println("Test 3 passed");
}
catch (Exception e) {
System.out.println("Test 3 failed");
}
// Insert and remove a word
try {
dictionary.put(records[1]);
dictionary.remove(keys[1]);
rec = dictionary.get(keys[1]);
if (rec == null) System.out.println("Test 4 passed");
else System.out.println("Test 4 failed");
}
catch(DictionaryException e) {
System.out.println("Test 4 failed");
}
// Remove a word not in the dictionary
try {
dictionary.remove(keys[3]);
System.out.println("Test 5 failed");
}
catch(DictionaryException e) {
System.out.println("Test 5 passed");
}
catch (Exception e) {
System.out.println("Test 5 failed");
}
// Insert 5 words in the dictionary and test the successor method
try {
dictionary.remove(keys[0]);
dictionary.put(records[1]);
dictionary.put(records[0]);
for (int i = 2; i < 5; ++i)
dictionary.put(records[i]);
rec = dictionary.successor(keys[3]);
if ((rec.getKey().getWord()).compareTo(words[1]) == 0)
System.out.println("Test 6 passed");
else System.out.println("Test 6 failed");
}
catch (Exception e) {
System.out.println("Test 6 failed");
}
// Test the successor method
try {
rec = dictionary.successor(keys[2]);
if ((rec.getKey().getWord()).compareTo(words[3]) == 0)
System.out.println("Test 7 passed");
else System.out.println("Test 7 failed");
}
catch (Exception e) {
System.out.println("Test 7 failed");
}
//Test the predecessor method
try {
rec = dictionary.predecessor(keys[0]);
if ((rec.getKey().getWord()).compareTo(words[4]) == 0)
System.out.println("Test 8 passed");
else System.out.println("Test 8 failed");
}
catch (Exception e) {
System.out.println("Test 8 failed");
}
// Test the predecessor method
try {
rec = dictionary.predecessor(keys[4]);
if ((rec.getKey().getWord()).compareTo(words[1]) == 0)
System.out.println("Test 9 passed");
else System.out.println("Test 9 failed");
}
catch (Exception e) {
System.out.println("Test 9 failed");
}
// Create a new empty dictionary
dictionary = new OrderedDictionary();
try {
// Insert a large number of words in the dictionary
BufferedReader in = new BufferedReader(new FileReader("large.txt"));
String word = in.readLine();
String definition;
boolean test10 = true;
try {
while (word != null) {
try {
definition = in.readLine();
dictionary.put(new Record(new Pair(word,TEXT),definition));
word = in.readLine();
}
catch (Exception e) {
test10 = false;
}
}
if (test10) {
// Now, try to find the word "practic"
rec = dictionary.get(new Pair("practic",TEXT));
if ((rec.getData()).indexOf("Artful; deceitful; skillful.") == -1)
System.out.println("Test 10 failed");
else System.out.println("Test 10 passed");
}
else System.out.println("Test 10 failed");
}
catch (Exception e) {
System.out.println("Test 10 failed");
}
// Try to find a word that is not in the dictionary
try {
rec = dictionary.get(new Pair("schnell",TEXT));
if (rec != null) System.out.println("Test 11 failed");
else System.out.println("Test 11 passed");
}
catch (Exception e) {
System.out.println("Test 11 failed");
}
// Test the remove method
try {
dictionary.remove(new Pair("practic",TEXT));
rec = dictionary.get(new Pair("practic",TEXT));
if (rec == null) System.out.println("Test 12 passed");
else System.out.println("Test 12 failed");
}
catch (Exception e) {
System.out.println("Test 12 failed");
}
// Try to insert a word that is already in the dictionary
try {
dictionary.put(new Record(new Pair("pool",TEXT),"Body of water"));
System.out.println("Test 13 failed");
}
catch(DictionaryException e) {
System.out.println("Test 13 passed");
}
catch (Exception e) {
System.out.println("Test 13 failed");
}
// Test the predecessor method
try {
rec = dictionary.predecessor(new Pair("pony",TEXT));
if ((rec.getKey().getWord()).compareTo("ponvolant") == 0)
System.out.println("Test 14 passed");
else System.out.println("Test 14 failed");
}
catch (Exception e) {
System.out.println("Test 13 failed");
}
// Test the successor method
try {
rec = dictionary.successor(new Pair("reel",TEXT));
if ((rec.getKey().getWord()).compareTo("reem") == 0)
System.out.println("Test 15 passed");
else System.out.println("Test 15 failed");
}
catch (Exception e) {
System.out.println("Test 15 failed");
}
// Test removing a word and the using successor
try {
dictionary.remove(new Pair("langate",TEXT));
rec = dictionary.successor(new Pair("land",TEXT));
if ((rec.getKey().getWord()).compareTo("laniary") == 0)
System.out.println("Test 16 passed");
else System.out.println("Test 16 failed");
}
catch (Exception e) {
System.out.println("Test 16 failed");
}
}
catch (IOException e) {
System.out.println("Cannot open file: large.txt");
}
}
}