-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestHashTable.java
More file actions
300 lines (289 loc) · 11.9 KB
/
TestHashTable.java
File metadata and controls
300 lines (289 loc) · 11.9 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// --== CS400 File Header Information ==--
// Name: Taylor Powers
// Email: tmpowers@wisc.edu
// Team: EB
// Role: Test Engineer
// TA: Keren Chen
// Lecturer: Gary Dahl
// Notes to Grader: None
import java.util.NoSuchElementException;
/**
* This class implements unit test methods to check the correctness of the
* HashTableMap class
*/
public class TestHashTable {
/*
* Tests that the put method accurately adds key-value pairs to the hash
* table
*
* @return True when the size of the HashTableMap is equal to the number of
* key-value pairs added to the hash table, and False when it is now
*/
public static boolean testPut() {
StateTable<String, String> myMap = new StateTable<String, String>();
myMap.insertInfo("United States",
"Geographic region: North America, Area: 9.83 million km^2, Capital: Washington D.C., "
+ "GDP: $20.5 trillion, Population: 327.2 million, GDP per capita: $62,869");
myMap.insertInfo("China",
"Geographic region: Asia, Area: 9.6 million km^2, Capital: Beijing, GDP: $13.6 trillion, "
+ "Population: 1.4 billion, GDP per capita: $18,116");
myMap.insertInfo("United Kingdom",
"Geographic region: Europe, Area: 243,610 km^2, Capital: London, GDP: $2.8 trillion, "
+ "Population: 66.5 million, GDP per capita: $45,741");
myMap.insertInfo("Russia",
"Geographic region: Eurasia, Area: 17.1 million km^2, Capital: Moscow, GDP: $1.7 trillion, "
+ "Population: 114.5 million, GDP per capita: $28,797");
myMap.insertInfo("Germany",
"Geographic region: Europe, Area: 357,022 km^2, Capital: Berlin, GDP: $4.0 trillion, "
+ "Population: 82.9 million, GDP per capita: $52,386");
if (myMap.size() != 5) {
return false;
}
return true;
}
/*
* Tests that the get method returns the correct value when passed a key. If
* passed a key that does not exist in the hash table, then it tests whether
* a NoSuchElementException is caught or not.
*
* @return True when the correct value is returned or an exception is
* caught, and False if an incorrect value is returned or an exception is
* not caught.
*/
public static boolean testGet() {
StateTable<String, String> myMap = new StateTable<String, String>();
myMap.insertInfo("United States",
"Geographic region: North America, Area: 9.83 million km^2, Capital: Washington D.C., "
+ "GDP: $20.5 trillion, Population: 327.2 million, GDP per capita: $62,869");
myMap.insertInfo("China",
"Geographic region: Asia, Area: 9.6 million km^2, Capital: Beijing, GDP: $13.6 trillion, "
+ "Population: 1.4 billion, GDP per capita: $18,116");
myMap.insertInfo("United Kingdom",
"Geographic region: Europe, Area: 243,610 km^2, Capital: London, GDP: $2.8 trillion, "
+ "Population: 66.5 million, GDP per capita: $45,741");
myMap.insertInfo("Russia",
"Geographic region: Eurasia, Area: 17.1 million km^2, Capital: Moscow, GDP: $1.7 trillion, "
+ "Population: 114.5 million, GDP per capita: $28,797");
myMap.insertInfo("Germany",
"Geographic region: Europe, Area: 357,022 km^2, Capital: Berlin, GDP: $4.0 trillion, "
+ "Population: 82.9 million, GDP per capita: $52,386");
if (myMap.getInfo(
"Russia") != "Geographic region: Eurasia, Area: 17.1 million km^2, Capital: Moscow, "
+ "GDP: $1.7 trillion, Population: 114.5 million, GDP per capita: $28,797") {
return false;
}
try {
myMap.getInfo("Japan");
System.out.println(
"Problem detected. Calling the get() method with an invalid key does"
+ "not throw a NoSuchElementException.");
return false;
} catch (NoSuchElementException e) {
return true;
}
}
/*
* Tests the remove method by attempting to remove a key and then verifying
* that the value returned is the correct value and that the hash table
* decreases in size. Also tests whether calling the get method on a key
* that has previously been removed correctly throws a
* NoSuchElementException
*
* @return True when the remove method returns the correct value and
* decreases the size of the hash table and False when the remove method
* does not
*/
public static boolean testRemove() {
StateTable<String, String> myMap = new StateTable<String, String>();
myMap.insertInfo("United States",
"Geographic region: North America, Area: 9.83 million km^2, Capital: Washington D.C., "
+ "GDP: $20.5 trillion, Population: 327.2 million, GDP per capita: $62,869");
myMap.insertInfo("China",
"Geographic region: Asia, Area: 9.6 million km^2, Capital: Beijing, GDP: $13.6 trillion, "
+ "Population: 1.4 billion, GDP per capita: $18,116");
myMap.insertInfo("United Kingdom",
"Geographic region: Europe, Area: 243,610 km^2, Capital: London, GDP: $2.8 trillion, "
+ "Population: 66.5 million, GDP per capita: $45,741");
myMap.insertInfo("Russia",
"Geographic region: Eurasia, Area: 17.1 million km^2, Capital: Moscow, GDP: $1.7 trillion, "
+ "Population: 114.5 million, GDP per capita: $28,797");
myMap.insertInfo("Germany",
"Geographic region: Europe, Area: 357,022 km^2, Capital: Berlin, GDP: $4.0 trillion, "
+ "Population: 82.9 million, GDP per capita: $52,386");
if (myMap.remove(
"United States") != "Geographic region: North America, Area: 9.83 million km^2, "
+ "Capital: Washington D.C., GDP: $20.5 trillion, Population: 327.2 million, GDP per capita: $62,869"
&& myMap.size() != 4) {
return false;
}
try {
myMap.getInfo("United States");
System.out.println(
"Problem detected. Calling the get() method with an invalid key does"
+ "not throw a NoSuchElementException.");
return false;
} catch (NoSuchElementException e) {
return true;
}
}
/*
* Tests that the HashTableMap correctly grows and rehashes when the load
* capacity reaches or exceeds 80%. This method adds key-value pairs to a
* hash table with a capacity of 10 and checks whether the new capacity of
* the hash table is 20 after adding the 9th key-value pair.
*
* @return True when the hash table correctly doubles and rehashes, and
* False otherwise
*/
public static boolean testLoadFactor() {
HashTableMap<String, String> myMap = new HashTableMap<>(5);
myMap.put("United States",
"Geographic region: North America, Area: 9.83 million km^2, Capital: Washington D.C., "
+ "GDP: $20.5 trillion, Population: 327.2 million, GDP per capita: $62,869");
myMap.put("China",
"Geographic region: Asia, Area: 9.6 million km^2, Capital: Beijing, GDP: $13.6 trillion, "
+ "Population: 1.4 billion, GDP per capita: $18,116");
myMap.put("United Kingdom",
"Geographic region: Europe, Area: 243,610 km^2, Capital: London, GDP: $2.8 trillion, "
+ "Population: 66.5 million, GDP per capita: $45,741");
myMap.put("Russia",
"Geographic region: Eurasia, Area: 17.1 million km^2, Capital: Moscow, GDP: $1.7 trillion, "
+ "Population: 114.5 million, GDP per capita: $28,797");
myMap.put("Germany",
"Geographic region: Europe, Area: 357,022 km^2, Capital: Berlin, GDP: $4.0 trillion, "
+ "Population: 82.9 million, GDP per capita: $52,386");
if (myMap.size() != 5) {
return false;
}
if (myMap.capacity() != 10) {
return false;
}
myMap.put("France",
"Geographic region: Europe, Area: 643,801 km^2, Capital: Paris, GDP: $2.8 trillion, "
+ "Population: 67.0 million, GDP per capita: $45,893");
myMap.put("Japan",
"Geographic region: Asia, Area: 377,915 km^2, Capital: Tokyo, GDP: $5.0 trillion, "
+ "Population: 126.5 million, GDP per capita: $44,246");
myMap.put("Italy",
"Geographic region: Europe, Area: 301.340 km^2, Capital: Rome, GDP: $2.1 trillion, "
+ "Population: 60.4 million, GDP per capita: $39,676");
if (myMap.capacity() != 20) {
return false;
}
return true;
}
/*
* Tests that the clear method correctly removes all key-value pairs from
* the hash table
*
* @return True when the size of the hash table is 0, and False otherwise
*/
public static boolean testClear() {
StateTable<String, String> myMap = new StateTable<String, String>();
myMap.insertInfo("United States",
"Geographic region: North America, Area: 9.83 million km^2, Capital: Washington D.C., "
+ "GDP: $20.5 trillion, Population: 327.2 million, GDP per capita: $62,869");
myMap.insertInfo("China",
"Geographic region: Asia, Area: 9.6 million km^2, Capital: Beijing, GDP: $13.6 trillion, "
+ "Population: 1.4 billion, GDP per capita: $18,116");
myMap.insertInfo("United Kingdom",
"Geographic region: Europe, Area: 243,610 km^2, Capital: London, GDP: $2.8 trillion, "
+ "Population: 66.5 million, GDP per capita: $45,741");
myMap.clear();
if (myMap.size() != 0) {
return false;
}
return true;
}
/*
* Tests that addInfo method correctly adds info to existing info
*
*@return True when the method correctly tests for a key, and False
* otherwise
*/
public static boolean testaddInfo() {
StateTable<String, String> myMap = new StateTable<String, String>();
myMap.insertInfo("Russia", "Geographic region: Eurasia");
myMap.addInfo("Russia", "Capital: Moscow");
if (!myMap.getInfo("Russia")
.equals("Geographic region: Eurasia\n" + "Capital: Moscow")) {
return false;
}
return true;
}
/*
* Tests that the contains method correctly return true or false depending
* on whether the key passed into the method is in the hash table or not
*
* @return True when the method correctly tests for a key, and False
* otherwise
*/
public static boolean testContains() {
HashTableMap<String, String> myMap = new HashTableMap<>(10);
myMap.put("France",
"Geographic region: Europe, Area: 643,801 km^2, Capital: Paris, GDP: $2.8 trillion, "
+ "Population: 67.0 million, GDP per capita: $45,893");
myMap.put("Japan",
"Geographic region: Asia, Area: 377,915 km^2, Capital: Tokyo, GDP: $5.0 trillion, "
+ "Population: 126.5 million, GDP per capita: $44,246");
myMap.put("Italy",
"Geographic region: Europe, Area: 301.340 km^2, Capital: Rome, GDP: $2.1 trillion, "
+ "Population: 60.4 million, GDP per capita: $39,676");
if (myMap.containsKey("Italy") != true) {
return false;
}
if (myMap.containsKey("United States") != false) {
return false;
}
return true;
}
/*
* Tests the MapData class that inputs all the countries and their
* respective data into a map. Makes sure the countries and info are
* retrievable when called with the correct line number from the
* countries.csv file.
*
* @return True when the method correctly tests for a key, and False
* otherwise
*/
public static boolean testData() {
MapData map = new MapData();
if (!map.getCountry(0).equals("United States")) {
return false;
}
if (!map.getCountry(72).equals("Latvia")) {
return false;
}
if (!map.getCountry(10).equals("South Korea")) {
return false;
}
if (!map.getInfo(10).equals(
"Geographic region: Asia\nArea: 99,720 km^2\nCapital: Seoul\nGDP: $1.6 trillion\n"
+ "Population: 51.6 million\nGDP per capita: $43,290.00\nOther info: \n")) {
return false;
}
if (!map.getInfo(0).equals(
"Geographic region: North America\nArea: 9.83 million km^2\nCapital: Washington, D.C.\n"
+ "GDP: $20.5 trillion\nPopulation: 327.2 million\nGDP per capita: $62,869.00\nOther info: \n")) {
return false;
}
return true;
}
/**
* This method should test the methods defined within this class and display
* their outputs
*
* @return true when this test verifies a correct functionality, and false
* otherwise
*/
public static void main(String[] args) {
System.out.println("testPut(): " + testPut());
System.out.println("testGet(): " + testGet());
System.out.println("testRemove(): " + testRemove());
System.out.println("testLoadFactor(): " + testLoadFactor());
System.out.println("testClear(): " + testClear());
System.out.println("testaddInfo(): " + testaddInfo());
System.out.println("testContains(): " + testContains());
System.out.println("testData(): " + testData());
}
}