-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestDict.java
More file actions
164 lines (140 loc) · 4.23 KB
/
Copy pathTestDict.java
File metadata and controls
164 lines (140 loc) · 4.23 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
package assignment2;
public class TestDict {
/*
** Test program for the Dictionary class.
** This program WILL NOT COMPILE if your insert or remove
** methods do not throw a DictionaryException.
*/
public static void main(String[] args) {
Dictionary dict = new Dictionary(9887);
ConfigData pair;
boolean[] test = new boolean[11];
int i,j;
if (args.length == 0)
for (i = 0; i < 11; ++i) test[i] = true; // Perform all tests
else {
if (args[0].equals("?")) {
System.out.println("Usage: java TestDict, or java TestDict n1 n2 n3 ... ");
System.out.println("where each ni has value 1 - 10; with the second invocation");
System.out.println("only the specified tests will be run");
System.exit(0);
}
for (i = 0; i < 11; ++i) test[i] = false;
for (i = 0; i < args.length; ++i) {
j = Integer.parseInt(args[i]);
if (j >= 1 && j <= 10) test[j] = true; // Perform only specified tests
}
}
// Test 1: insert a data item in the dictionary.
// Should not throw an exception.
if (test[1])
try {
dict.insert(new ConfigData("answer", 42));
System.out.println(" Test 1 succeeded");
}
catch (DictionaryException e) {
System.out.println("***Test 1 failed");
}
// Test 2: try to insert another data item with the same key.
// Should throw an exception.
if (test[2])
try {
dict.insert(new ConfigData("answer", 56));
System.out.println("***Test 2 failed");
}
catch (DictionaryException e) {
System.out.println(" Test 2 succeeded");
}
// Test 3: find a key in the table.
if (test[3])
if (dict.find("answer") == -1)
System.out.println("***Test 3 failed");
else System.out.println(" Test 3 succeeded");
// Test 4: look for an inexistent key
if (test[4])
if (dict.find("chicken") != -1)
System.out.println("***Test 4 failed");
else System.out.println(" Test 4 succeeded");
// Test 5: try to delete a nonexistent entry.
// Should throw an exception.
if (test[5])
try {
dict.remove("chicken");
System.out.println("***Test 5 failed");
}
catch (DictionaryException e) {
System.out.println(" Test 5 succeeded");
}
// Test 6: delete an actual entry.
// Should not throw an exception.
if (test[6])
try {
dict.remove("answer");
System.out.println(" Test 6 succeeded");
}
catch (DictionaryException e) {
System.out.println("***Test 6 failed");
}
int collisions = 0;
String s;
// Test 7: insert 10000 different values into the Dictionary
if (test[7])
try {
for (i = 0; i < 10000; ++i) {
s = (new Integer(i)).toString();
for (j = 0; j < 5; ++j) s += s;
collisions += dict.insert(new ConfigData(s,i));
}
System.out.println(" Test 7 succeeded");
}
catch (DictionaryException e) {
System.out.println("***Test 7 failed");
}
// Test 8: check that all inserted values are in the Dictionary
boolean pass = true;
if (test[8]) {
for (i = 0; i < 10000; ++i) {
s = (new Integer(i)).toString();
for (j = 0; j < 5; ++j) s += s;
if (dict.find(s) == -1) {
System.out.println("***Test 8 failed");
pass = false;
break;
}
}
if (pass) System.out.println(" Test 8 succeeded");
}
// Test 9: Remove the first 1000 data items and verify that the rest
// are in the dictionary
pass = true;
if (test[9])
try {
for (i = 0; i < 1000; ++i) {
s = (new Integer(i)).toString();
for (j = 0; j < 5; ++j) s += s;
dict.remove(s);
}
for (i = 1000; i < 10000; ++i) {
s = (new Integer(i)).toString();
for (j = 0; j < 5; ++j) s += s;
if (dict.find(s) == -1) {
System.out.println("***Test 9 failed");
pass = false;
break;
}
}
if (pass) System.out.println(" Test 9 succeeded");
}
catch (DictionaryException e) {
System.out.println("***Test 9 failed");
}
//Test 10: Number of collisions
if (test[10])
if (collisions >= 5000) {
System.out.println("***Test 10 failed");
System.out.println("Too many collisions: "+collisions);
}
else
System.out.println(" Test 10 succeeded");
}
}