-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDict.java
More file actions
264 lines (237 loc) · 7.2 KB
/
TestDict.java
File metadata and controls
264 lines (237 loc) · 7.2 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import java.io.*;
public class TestDict {
public static void main(String[] args) {
final int TEXT = 2;
BSTDictionary dictionary = new BSTDictionary();
Record rec;
Key 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"};
Key[] keys = new Key[5];
Record[] records = new Record[5];
boolean alltests = true;
int test = -1;
if (args.length > 0) {
test = Integer.parseInt(args[0]);
alltests = false;
}
for (int i = 0; i < 5; ++i) {
keys[i] = new Key(words[i],TEXT);
records[i] = new Record(keys[i],definitions[i]);
}
// Insert one word and then find it
if (alltests || test == 1 || test == 3 || test == 6 || test == 7 || test == 8 || test == 9)
try {
dictionary.put(records[0]);
rec = dictionary.get(keys[0]);
if ((rec.getDataItem()).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
if (alltests || test == 2)
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
if (alltests || test == 3)
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
if (alltests || test == 4)
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
if (alltests || test == 5)
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
if (alltests || test == 6 || test == 7 || test == 8 || test == 9)
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().getLabel()).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
if (alltests || test == 7)
try {
rec = dictionary.successor(keys[2]);
if ((rec.getKey().getLabel()).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
if (alltests || test == 8)
try {
rec = dictionary.predecessor(keys[0]);
if ((rec.getKey().getLabel()).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
if (alltests || test == 9)
try {
rec = dictionary.predecessor(keys[4]);
if ((rec.getKey().getLabel()).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 BSTDictionary();
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;
if (alltests || test >= 10)
try {
while (word != null) {
try {
definition = in.readLine();
dictionary.put(new Record(new Key(word,TEXT),definition));
word = in.readLine();
}
catch (Exception e) {
test10 = false;
}
}
if (test10) {
// Now, try to find the word "practic"
rec = dictionary.get(new Key("practic",TEXT));
if ((rec.getDataItem()).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");
}
if (alltests || test == 11)
// Try to find a word that is not in the dictionary
try {
rec = dictionary.get(new Key("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");
}
if (alltests || test == 12)
// Test the remove method
try {
dictionary.remove(new Key("practic",TEXT));
rec = dictionary.get(new Key("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");
}
if (alltests || test == 13)
// Try to insert a word that is already in the dictionary
try {
dictionary.put(new Record(new Key("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");
}
if (alltests || test == 14)
// Test the predecessor method
try {
rec = dictionary.predecessor(new Key("pony",TEXT));
if ((rec.getKey().getLabel()).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");
}
if (alltests || test == 15)
// Test the successor method
try {
rec = dictionary.successor(new Key("reel",TEXT));
if ((rec.getKey().getLabel()).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");
}
if (alltests || test == 16)
// Test removing a word and the using successor
try {
dictionary.remove(new Key("langate",TEXT));
rec = dictionary.successor(new Key("land",TEXT));
if ((rec.getKey().getLabel()).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");
}
}
}