@@ -244,63 +244,54 @@ class LFUCache {
244
244
} // namespace others
245
245
246
246
/* *
247
- * @brief A simple test case
247
+ * @brief self test implementation
248
248
* @return void
249
249
*/
250
- void test1 () {
250
+ static void test () {
251
251
others::Cache::LFUCache<int , int > cache (5 );
252
+
253
+ // test the initial state of the cache
252
254
assert (cache.size () == 0 );
253
255
assert (cache.capacity () == 5 );
254
256
assert (cache.empty ());
257
+
258
+ // test insertion in the cache
255
259
cache.put (1 , 10 );
256
260
cache.put (-2 , 20 );
257
261
262
+ // test the state of cache after inserting some items
258
263
assert (cache.size () == 2 );
259
264
assert (cache.capacity () == 5 );
260
265
assert (!cache.empty ());
261
266
262
- cache.put (3 , -30 );
267
+ // test getting items from the cache
268
+ assert (cache.get (1 ) == 10 );
269
+ assert (cache.get (-2 ) == 20 );
270
+
271
+ cache.put (-3 , -30 );
263
272
cache.put (4 , 40 );
264
- cache.put (5 , 50 );
273
+ cache.put (5 , - 50 );
265
274
cache.put (6 , 60 );
266
275
276
+ // test the state after inserting more items than the capacity
267
277
assert (cache.size () == 5 );
268
278
assert (cache.capacity () == 5 );
269
279
assert (!cache.empty ());
270
280
271
- std::cout << " test1 - passed\n " ;
272
- }
281
+ // test retrieval of all items in the cache
282
+ assert (cache.get (1 ) == 10 );
283
+ assert (cache.get (-2 ) == 20 );
273
284
274
- /* *
275
- * @brief A simple test case
276
- * @return void
277
- */
278
- void test2 () {
279
- others::Cache::LFUCache<int , int > cache (5 );
280
- cache.put (-1 , -10 );
281
- cache.put (2 , 20 );
282
- cache.put (3 , 30 );
285
+ // fetching -3 throws runtime_error
286
+ // as -3 was evicted being the least frequently used
287
+ // when 6 was added
288
+ // assert(cache.get(-3) == -30);
283
289
284
- assert (cache.get (- 1 ) == - 10 );
285
- cache.get (2 );
286
- cache.get (3 );
290
+ assert (cache.get (4 ) == 40 );
291
+ assert ( cache.get (5 ) == - 50 );
292
+ assert ( cache.get (6 ) == 60 );
287
293
288
- cache.put (4 , 40 );
289
- cache.put (5 , 50 );
290
- cache.put (6 , 60 );
291
-
292
- assert (cache.get (5 ) == 50 );
293
-
294
- std::cout << " test2 - passed\n " ;
295
- }
296
-
297
- /* *
298
- * @brief self test implementation
299
- * @return void
300
- */
301
- void test () {
302
- test1 ();
303
- test2 ();
294
+ std::cout << " test - passed\n " ;
304
295
}
305
296
306
297
/* *
0 commit comments