@@ -130,6 +130,82 @@ def test_reset(self):
130
130
cache .save ('cow' , 'crate' )
131
131
self .assertEqual (cache .lookup ('cow' ), 'crate' )
132
132
133
+ def test_remove_non_existent_key (self ):
134
+ cache = LRUCache (3 , 1000 )
135
+ cache .save ("1" , 100 )
136
+ cache .save ("2" , 200 )
137
+
138
+ cache .remove ("3" ) # Doesn't exist
139
+
140
+ self .assertEqual (cache .lookup ("1" ), 100 )
141
+ self .assertEqual (cache .lookup ("2" ), 200 )
142
+
143
+ def test_remove_existing_key (self ):
144
+ cache = LRUCache (3 , 1000 )
145
+
146
+ cache .save ("1" , 100 )
147
+ cache .save ("2" , 200 )
148
+ cache .save ("3" , 300 )
149
+
150
+ self .assertEqual (cache .lookup ("1" ), 100 )
151
+ self .assertEqual (cache .lookup ("2" ), 200 )
152
+ self .assertEqual (cache .lookup ("3" ), 300 )
153
+
154
+ cache .remove ("2" )
155
+
156
+ self .assertEqual (cache .lookup ("1" ), 100 )
157
+ self .assertIsNone (cache .lookup ("2" ))
158
+ self .assertEqual (cache .lookup ("3" ), 300 )
159
+
160
+ def test_remove_from_zero_sized_cache (self ):
161
+ cache = LRUCache (0 , 1000 )
162
+ cache .save ("1" , 100 )
163
+ cache .remove ("1" )
164
+
165
+ self .assertIsNone (cache .lookup ("1" ))
166
+
167
+ def test_remove_and_add_back (self ):
168
+ cache = LRUCache (3 , 1000 )
169
+ cache .save ("1" , 100 )
170
+ cache .save ("2" , 200 )
171
+ cache .save ("3" , 300 )
172
+
173
+ cache .remove ("2" )
174
+ cache .save ("2" , 201 )
175
+
176
+ self .assertEqual (cache .lookup ("1" ), 100 )
177
+ self .assertEqual (cache .lookup ("2" ), 201 )
178
+ self .assertEqual (cache .lookup ("3" ), 300 )
179
+
180
+ def test_thread_safety (self ):
181
+ import threading
182
+
183
+ max_size = 100
184
+ cache = LRUCache (max_size , 1000 )
185
+
186
+ for i in range (1 , max_size + 1 ):
187
+ cache .save (str (i ), i * 100 )
188
+
189
+ def remove_key (k ):
190
+ cache .remove (str (k ))
191
+
192
+ threads = []
193
+ for i in range (1 , (max_size // 2 ) + 1 ):
194
+ thread = threading .Thread (target = remove_key , args = (i ,))
195
+ threads .append (thread )
196
+ thread .start ()
197
+
198
+ for thread in threads :
199
+ thread .join ()
200
+
201
+ for i in range (1 , max_size + 1 ):
202
+ if i <= max_size // 2 :
203
+ self .assertIsNone (cache .lookup (str (i )))
204
+ else :
205
+ self .assertEqual (cache .lookup (str (i )), i * 100 )
206
+
207
+ self .assertEqual (len (cache .map ), max_size // 2 )
208
+
133
209
# type checker test
134
210
# confirm that LRUCache matches OptimizelySegmentsCache protocol
135
211
_ : OptimizelySegmentsCache = LRUCache (0 , 0 )
0 commit comments