|
@@ -1,230 +1,230 @@
|
1
|
|
-//package org.zipcoder.store;
|
2
|
|
-//
|
3
|
|
-//import org.junit.Assert;
|
4
|
|
-//import org.junit.Before;
|
5
|
|
-//import org.junit.Test;
|
6
|
|
-//
|
7
|
|
-//import java.util.List;
|
8
|
|
-//
|
9
|
|
-//public class MyHashMapTest {
|
10
|
|
-// private MyMap myMap;
|
11
|
|
-//
|
12
|
|
-// @Before
|
13
|
|
-// public void setup(){
|
14
|
|
-// myMap = new MyHashMap();
|
15
|
|
-// }
|
16
|
|
-//
|
17
|
|
-// @Test
|
18
|
|
-// public void testPutAndGet_WithOneItem(){
|
19
|
|
-// User user = new User("Tiana");
|
20
|
|
-// Cart cart = new Cart(1);
|
21
|
|
-//
|
22
|
|
-// // When
|
23
|
|
-// myMap.put(user, cart);
|
24
|
|
-//
|
25
|
|
-// // Then
|
26
|
|
-// Cart actualCart = myMap.get(user);
|
27
|
|
-// Assert.assertEquals(cart, actualCart);
|
28
|
|
-// }
|
29
|
|
-//
|
30
|
|
-// @Test
|
31
|
|
-// public void testPutAndGet_WithTwoItems(){
|
32
|
|
-// User tiana = new User("Tiana");
|
33
|
|
-// Cart tianasCart = new Cart(1134);
|
34
|
|
-//
|
35
|
|
-// User john = new User("John");
|
36
|
|
-// Cart johnsCart = new Cart(42);
|
37
|
|
-//
|
38
|
|
-// // When
|
39
|
|
-// myMap.put(john, johnsCart);
|
40
|
|
-// myMap.put(tiana, tianasCart);
|
41
|
|
-//
|
42
|
|
-// // Then
|
43
|
|
-// Cart actualJohnsCart = myMap.get(john);
|
44
|
|
-// Assert.assertEquals(johnsCart, actualJohnsCart);
|
45
|
|
-//
|
46
|
|
-// Cart actualTianasCart = myMap.get(tiana);
|
47
|
|
-// Assert.assertEquals(tianasCart, actualTianasCart);
|
48
|
|
-// }
|
49
|
|
-//
|
50
|
|
-// @Test
|
51
|
|
-// public void testPutAndGet_UpdateCart(){
|
52
|
|
-// User tiana = new User("Tiana");
|
53
|
|
-// Cart tianasCart = new Cart(11);
|
54
|
|
-// myMap.put(tiana, tianasCart);
|
55
|
|
-//
|
56
|
|
-// // When
|
57
|
|
-// Cart updatedCart = new Cart(99);
|
58
|
|
-// myMap.put(tiana, updatedCart);
|
59
|
|
-//
|
60
|
|
-// // Then
|
61
|
|
-// Cart actualCart = myMap.get(tiana);
|
62
|
|
-// Assert.assertEquals(updatedCart, actualCart);
|
63
|
|
-// }
|
64
|
|
-//
|
65
|
|
-// @Test
|
66
|
|
-// public void testGetWithoutPut(){
|
67
|
|
-// // Given
|
68
|
|
-// User user = new User("Tiana");
|
69
|
|
-//
|
70
|
|
-// // When
|
71
|
|
-// Cart actualCart = myMap.get(user);
|
72
|
|
-//
|
73
|
|
-// // Then
|
74
|
|
-// Assert.assertNull(actualCart);
|
75
|
|
-// }
|
76
|
|
-//
|
77
|
|
-// @Test
|
78
|
|
-// public void testPutAndGet_WithDifferentKeyInstance(){
|
79
|
|
-// // Given
|
80
|
|
-// String name = "Tiana";
|
81
|
|
-// User tiana = new User(name);
|
82
|
|
-// Cart cart = new Cart(76);
|
83
|
|
-//
|
84
|
|
-// // When
|
85
|
|
-// myMap.put(tiana, cart);
|
86
|
|
-// User cloneTiana = new User(name);
|
87
|
|
-// Cart actualCart = myMap.get(cloneTiana);
|
88
|
|
-//
|
89
|
|
-// // Then
|
90
|
|
-// Assert.assertEquals(cart, actualCart);
|
91
|
|
-// }
|
92
|
|
-//
|
93
|
|
-// @Test
|
94
|
|
-// public void testSize_WithNoEntry(){
|
95
|
|
-// int expectedSize = 0;
|
96
|
|
-// Assert.assertEquals(expectedSize, myMap.size());
|
97
|
|
-// }
|
98
|
|
-//
|
99
|
|
-// @Test
|
100
|
|
-// public void testSize_WithEntries(){
|
101
|
|
-// // Given
|
102
|
|
-// User user1 = new User("Monica");
|
103
|
|
-// Cart cart1 = new Cart(45);
|
104
|
|
-//
|
105
|
|
-// User user2 = new User("Quincy");
|
106
|
|
-// Cart cart2 = new Cart(78);
|
107
|
|
-//
|
108
|
|
-// // When
|
109
|
|
-// myMap.put(user1, cart1);
|
110
|
|
-// myMap.put(user2, cart2);
|
111
|
|
-//
|
112
|
|
-// // Then
|
113
|
|
-// int expectedSize = 2;
|
114
|
|
-// Assert.assertEquals(expectedSize, myMap.size());
|
115
|
|
-// }
|
116
|
|
-//
|
117
|
|
-// @Test
|
118
|
|
-// public void testIsEmpty_WithoutEntry(){
|
119
|
|
-// Assert.assertTrue(myMap.isEmptry());
|
120
|
|
-// }
|
121
|
|
-//
|
122
|
|
-// @Test
|
123
|
|
-// public void testisEmpty_WithEntries(){
|
124
|
|
-// // Given
|
125
|
|
-// User user1 = new User("Monica");
|
126
|
|
-// Cart cart1 = new Cart(45);
|
127
|
|
-//
|
128
|
|
-// // When
|
129
|
|
-// myMap.put(user1, cart1);
|
130
|
|
-//
|
131
|
|
-// // Then
|
132
|
|
-// Assert.assertFalse(myMap.isEmptry());
|
133
|
|
-// }
|
134
|
|
-//
|
135
|
|
-// @Test
|
136
|
|
-// public void testGetKeys(){
|
137
|
|
-// // Given
|
138
|
|
-// User user1 = new User("Zeke");
|
139
|
|
-// Cart cart1 = new Cart(7);
|
140
|
|
-//
|
141
|
|
-// User user2 = new User("Nona");
|
142
|
|
-// Cart cart2 = new Cart(29);
|
143
|
|
-//
|
144
|
|
-// // When
|
145
|
|
-// myMap.put(user1, cart1);
|
146
|
|
-// myMap.put(user2, cart2);
|
147
|
|
-//
|
148
|
|
-// // Then
|
149
|
|
-// List<User> keys = myMap.getKeys();
|
150
|
|
-// int expectedKeySize = 2;
|
151
|
|
-// Assert.assertEquals(expectedKeySize, keys.size());
|
152
|
|
-// Assert.assertTrue(keys.contains(user1));
|
153
|
|
-// Assert.assertTrue(keys.contains(user2));
|
154
|
|
-// }
|
155
|
|
-//
|
156
|
|
-// @Test
|
157
|
|
-// public void testGetValues(){
|
158
|
|
-// // Given
|
159
|
|
-// User user1 = new User("Camille");
|
160
|
|
-// Cart cart1 = new Cart(433);
|
161
|
|
-//
|
162
|
|
-// User user2 = new User("Nathan");
|
163
|
|
-// Cart cart2 = new Cart(71);
|
164
|
|
-//
|
165
|
|
-// // When
|
166
|
|
-// myMap.put(user1, cart1);
|
167
|
|
-// myMap.put(user2, cart2);
|
168
|
|
-//
|
169
|
|
-// // Then
|
170
|
|
-// List<Cart> values = myMap.getValues();
|
171
|
|
-// int expectedKeySize = 2;
|
172
|
|
-// Assert.assertEquals(expectedKeySize, values.size());
|
173
|
|
-// Assert.assertTrue(values.contains(cart1));
|
174
|
|
-// Assert.assertTrue(values.contains(cart2));
|
175
|
|
-// }
|
|
1
|
+package org.zipcoder.store;
|
|
2
|
+
|
|
3
|
+import org.junit.Assert;
|
|
4
|
+import org.junit.Before;
|
|
5
|
+import org.junit.Test;
|
|
6
|
+
|
|
7
|
+import java.util.List;
|
|
8
|
+
|
|
9
|
+public class MyHashMapTest {
|
|
10
|
+ private MyMap myMap;
|
|
11
|
+
|
|
12
|
+ @Before
|
|
13
|
+ public void setup(){
|
|
14
|
+ myMap = new MyHashMap();
|
|
15
|
+ }
|
|
16
|
+
|
|
17
|
+ @Test
|
|
18
|
+ public void testPutAndGet_WithOneItem(){
|
|
19
|
+ User user = new User("Tiana");
|
|
20
|
+ Cart cart = new Cart(1);
|
|
21
|
+
|
|
22
|
+ // When
|
|
23
|
+ myMap.put(user, cart);
|
|
24
|
+
|
|
25
|
+ // Then
|
|
26
|
+ Cart actualCart = myMap.get(user);
|
|
27
|
+ Assert.assertEquals(cart, actualCart);
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ @Test
|
|
31
|
+ public void testPutAndGet_WithTwoItems(){
|
|
32
|
+ User tiana = new User("Tiana");
|
|
33
|
+ Cart tianasCart = new Cart(1134);
|
|
34
|
+
|
|
35
|
+ User john = new User("John");
|
|
36
|
+ Cart johnsCart = new Cart(42);
|
|
37
|
+
|
|
38
|
+ // When
|
|
39
|
+ myMap.put(john, johnsCart);
|
|
40
|
+ myMap.put(tiana, tianasCart);
|
|
41
|
+
|
|
42
|
+ // Then
|
|
43
|
+ Cart actualJohnsCart = myMap.get(john);
|
|
44
|
+ Assert.assertEquals(johnsCart, actualJohnsCart);
|
|
45
|
+
|
|
46
|
+ Cart actualTianasCart = myMap.get(tiana);
|
|
47
|
+ Assert.assertEquals(tianasCart, actualTianasCart);
|
|
48
|
+ }
|
|
49
|
+
|
|
50
|
+ @Test
|
|
51
|
+ public void testPutAndGet_UpdateCart(){
|
|
52
|
+ User tiana = new User("Tiana");
|
|
53
|
+ Cart tianasCart = new Cart(11);
|
|
54
|
+ myMap.put(tiana, tianasCart);
|
|
55
|
+
|
|
56
|
+ // When
|
|
57
|
+ Cart updatedCart = new Cart(99);
|
|
58
|
+ myMap.put(tiana, updatedCart);
|
|
59
|
+
|
|
60
|
+ // Then
|
|
61
|
+ Cart actualCart = myMap.get(tiana);
|
|
62
|
+ Assert.assertEquals(updatedCart, actualCart);
|
|
63
|
+ }
|
|
64
|
+
|
|
65
|
+ @Test
|
|
66
|
+ public void testGetWithoutPut(){
|
|
67
|
+ // Given
|
|
68
|
+ User user = new User("Tiana");
|
|
69
|
+
|
|
70
|
+ // When
|
|
71
|
+ Cart actualCart = myMap.get(user);
|
|
72
|
+
|
|
73
|
+ // Then
|
|
74
|
+ Assert.assertNull(actualCart);
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+ @Test
|
|
78
|
+ public void testPutAndGet_WithDifferentKeyInstance(){
|
|
79
|
+ // Given
|
|
80
|
+ String name = "Tiana";
|
|
81
|
+ User tiana = new User(name);
|
|
82
|
+ Cart cart = new Cart(76);
|
|
83
|
+
|
|
84
|
+ // When
|
|
85
|
+ myMap.put(tiana, cart);
|
|
86
|
+ User cloneTiana = new User(name);
|
|
87
|
+ Cart actualCart = myMap.get(cloneTiana);
|
|
88
|
+
|
|
89
|
+ // Then
|
|
90
|
+ Assert.assertEquals(cart, actualCart);
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+ @Test
|
|
94
|
+ public void testSize_WithNoEntry(){
|
|
95
|
+ int expectedSize = 0;
|
|
96
|
+ Assert.assertEquals(expectedSize, myMap.size());
|
|
97
|
+ }
|
|
98
|
+
|
|
99
|
+ @Test
|
|
100
|
+ public void testSize_WithEntries(){
|
|
101
|
+ // Given
|
|
102
|
+ User user1 = new User("Monica");
|
|
103
|
+ Cart cart1 = new Cart(45);
|
|
104
|
+
|
|
105
|
+ User user2 = new User("Quincy");
|
|
106
|
+ Cart cart2 = new Cart(78);
|
|
107
|
+
|
|
108
|
+ // When
|
|
109
|
+ myMap.put(user1, cart1);
|
|
110
|
+ myMap.put(user2, cart2);
|
|
111
|
+
|
|
112
|
+ // Then
|
|
113
|
+ int expectedSize = 2;
|
|
114
|
+ Assert.assertEquals(expectedSize, myMap.size());
|
|
115
|
+ }
|
|
116
|
+
|
|
117
|
+ @Test
|
|
118
|
+ public void testIsEmpty_WithoutEntry(){
|
|
119
|
+ Assert.assertTrue(myMap.isEmptry());
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ @Test
|
|
123
|
+ public void testisEmpty_WithEntries(){
|
|
124
|
+ // Given
|
|
125
|
+ User user1 = new User("Monica");
|
|
126
|
+ Cart cart1 = new Cart(45);
|
|
127
|
+
|
|
128
|
+ // When
|
|
129
|
+ myMap.put(user1, cart1);
|
|
130
|
+
|
|
131
|
+ // Then
|
|
132
|
+ Assert.assertFalse(myMap.isEmptry());
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ @Test
|
|
136
|
+ public void testGetKeys(){
|
|
137
|
+ // Given
|
|
138
|
+ User user1 = new User("Zeke");
|
|
139
|
+ Cart cart1 = new Cart(7);
|
|
140
|
+
|
|
141
|
+ User user2 = new User("Nona");
|
|
142
|
+ Cart cart2 = new Cart(29);
|
|
143
|
+
|
|
144
|
+ // When
|
|
145
|
+ myMap.put(user1, cart1);
|
|
146
|
+ myMap.put(user2, cart2);
|
|
147
|
+
|
|
148
|
+ // Then
|
|
149
|
+ List<User> keys = myMap.getKeys();
|
|
150
|
+ int expectedKeySize = 2;
|
|
151
|
+ Assert.assertEquals(expectedKeySize, keys.size());
|
|
152
|
+ Assert.assertTrue(keys.contains(user1));
|
|
153
|
+ Assert.assertTrue(keys.contains(user2));
|
|
154
|
+ }
|
|
155
|
+
|
|
156
|
+ @Test
|
|
157
|
+ public void testGetValues(){
|
|
158
|
+ // Given
|
|
159
|
+ User user1 = new User("Camille");
|
|
160
|
+ Cart cart1 = new Cart(433);
|
|
161
|
+
|
|
162
|
+ User user2 = new User("Nathan");
|
|
163
|
+ Cart cart2 = new Cart(71);
|
|
164
|
+
|
|
165
|
+ // When
|
|
166
|
+ myMap.put(user1, cart1);
|
|
167
|
+ myMap.put(user2, cart2);
|
|
168
|
+
|
|
169
|
+ // Then
|
|
170
|
+ List<Cart> values = myMap.getValues();
|
|
171
|
+ int expectedKeySize = 2;
|
|
172
|
+ Assert.assertEquals(expectedKeySize, values.size());
|
|
173
|
+ Assert.assertTrue(values.contains(cart1));
|
|
174
|
+ Assert.assertTrue(values.contains(cart2));
|
|
175
|
+ }
|
176
|
176
|
|
177
|
177
|
// I commented this out because it takes a long time to run. Uncomment it when implement your put method, uncomment this test
|
178
|
|
-// @Test
|
179
|
|
-// public void testPutAndGet_WithManyItems(){
|
180
|
|
-// // Given
|
181
|
|
-// int numberOfEntries = 70000;
|
182
|
|
-//
|
183
|
|
-// for (int i = 0; i < numberOfEntries; i++) {
|
184
|
|
-// myMap.put(new User(i), new Cart(i));
|
185
|
|
-// }
|
186
|
|
-//
|
187
|
|
-//
|
188
|
|
-// for (int i = 0; i < numberOfEntries; i++) {
|
189
|
|
-// // When
|
190
|
|
-// Cart actualCart = myMap.get(new User(i));
|
191
|
|
-// Cart expectedCart = new Cart(i);
|
192
|
|
-//
|
193
|
|
-// // Then
|
194
|
|
-// Assert.assertEquals(actualCart, expectedCart);
|
195
|
|
-// }
|
196
|
|
-// }
|
197
|
|
-
|
198
|
|
-// @Test
|
199
|
|
-// public void testRemove(){
|
200
|
|
-// // Given
|
201
|
|
-// User user1 = new User("Lena");
|
202
|
|
-// Cart cart1 = new Cart(11);
|
203
|
|
-//
|
204
|
|
-// User user2 = new User("Jamal");
|
205
|
|
-// Cart cart2 = new Cart(21);
|
206
|
|
-//
|
207
|
|
-// User user3 = new User("Kelvin");
|
208
|
|
-// Cart cart3 = new Cart(31);
|
209
|
|
-//
|
210
|
|
-// // When
|
211
|
|
-// myMap.put(user1, cart1);
|
212
|
|
-// myMap.put(user2, cart2);
|
213
|
|
-// myMap.put(user3, cart3);
|
214
|
|
-//
|
215
|
|
-// // Then
|
216
|
|
-// Cart actualCart = myMap.remove(user2);
|
217
|
|
-//
|
218
|
|
-// Assert.assertEquals(cart2, actualCart);
|
219
|
|
-// Assert.assertNull(myMap.get(user2));
|
220
|
|
-// }
|
221
|
|
-//
|
222
|
|
-// @Test
|
223
|
|
-// public void testRemove_CartNotInTheMap(){
|
224
|
|
-// // Given
|
225
|
|
-// User user1 = new User("Lena");
|
226
|
|
-//
|
227
|
|
-// // When & Then
|
228
|
|
-// Assert.assertNull(myMap.remove(user1));
|
229
|
|
-// }
|
|
178
|
+ @Test
|
|
179
|
+ public void testPutAndGet_WithManyItems(){
|
|
180
|
+ // Given
|
|
181
|
+ int numberOfEntries = 70000;
|
|
182
|
+
|
|
183
|
+ for (int i = 0; i < numberOfEntries; i++) {
|
|
184
|
+ myMap.put(new User(i), new Cart(i));
|
|
185
|
+ }
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+ for (int i = 0; i < numberOfEntries; i++) {
|
|
189
|
+ // When
|
|
190
|
+ Cart actualCart = myMap.get(new User(i));
|
|
191
|
+ Cart expectedCart = new Cart(i);
|
|
192
|
+
|
|
193
|
+ // Then
|
|
194
|
+ Assert.assertEquals(actualCart, expectedCart);
|
|
195
|
+ }
|
|
196
|
+ }
|
|
197
|
+
|
|
198
|
+ @Test
|
|
199
|
+ public void testRemove(){
|
|
200
|
+ // Given
|
|
201
|
+ User user1 = new User("Lena");
|
|
202
|
+ Cart cart1 = new Cart(11);
|
|
203
|
+
|
|
204
|
+ User user2 = new User("Jamal");
|
|
205
|
+ Cart cart2 = new Cart(21);
|
|
206
|
+
|
|
207
|
+ User user3 = new User("Kelvin");
|
|
208
|
+ Cart cart3 = new Cart(31);
|
|
209
|
+
|
|
210
|
+ // When
|
|
211
|
+ myMap.put(user1, cart1);
|
|
212
|
+ myMap.put(user2, cart2);
|
|
213
|
+ myMap.put(user3, cart3);
|
|
214
|
+
|
|
215
|
+ // Then
|
|
216
|
+ Cart actualCart = myMap.remove(user2);
|
|
217
|
+
|
|
218
|
+ Assert.assertEquals(cart2, actualCart);
|
|
219
|
+ Assert.assertNull(myMap.get(user2));
|
|
220
|
+ }
|
|
221
|
+
|
|
222
|
+ @Test
|
|
223
|
+ public void testRemove_CartNotInTheMap(){
|
|
224
|
+ // Given
|
|
225
|
+ User user1 = new User("Lena");
|
|
226
|
+
|
|
227
|
+ // When & Then
|
|
228
|
+ Assert.assertNull(myMap.remove(user1));
|
|
229
|
+ }
|
230
|
230
|
}
|