@@ -393,6 +393,74 @@ mod tests {
393
393
assert_eq ! ( flushed, [ ( 2 , 2 ) , ( 3 , 3 ) ] ) ;
394
394
}
395
395
396
+ #[ test]
397
+ fn test_lru_cache_capacity_one ( ) {
398
+ let mut cache = LruCache :: new ( 1 ) ;
399
+
400
+ cache. insert ( 1 , 1 ) . expect ( "cache corrupted" ) ;
401
+ assert_eq ! ( cache. get( & 1 ) . expect( "cache corrupted" ) , Some ( 1 ) ) ;
402
+
403
+ cache. insert ( 2 , 2 ) . expect ( "cache corrupted" ) ;
404
+ assert_eq ! ( cache. get( & 1 ) . expect( "cache corrupted" ) , None ) ;
405
+ assert_eq ! ( cache. get( & 2 ) . expect( "cache corrupted" ) , Some ( 2 ) ) ;
406
+ }
407
+
408
+ #[ test]
409
+ fn test_lru_cache_capacity_one_update ( ) {
410
+ let mut cache = LruCache :: new ( 1 ) ;
411
+
412
+ cache. insert ( 1 , 1 ) . expect ( "cache corrupted" ) ;
413
+ cache. insert ( 1 , 2 ) . expect ( "cache corrupted" ) ;
414
+ assert_eq ! ( cache. get( & 1 ) . expect( "cache corrupted" ) , Some ( 2 ) ) ;
415
+
416
+ cache. insert ( 2 , 3 ) . expect ( "cache corrupted" ) ;
417
+ assert_eq ! ( cache. get( & 1 ) . expect( "cache corrupted" ) , None ) ;
418
+ assert_eq ! ( cache. get( & 2 ) . expect( "cache corrupted" ) , Some ( 3 ) ) ;
419
+ }
420
+
421
+ #[ test]
422
+ fn test_lru_cache_capacity_one_eviction ( ) {
423
+ let mut cache = LruCache :: new ( 1 ) ;
424
+
425
+ assert ! ( cache. insert( 1 , 1 ) . expect( "cache corrupted" ) . is_none( ) ) ;
426
+ let evicted = cache
427
+ . insert ( 2 , 2 )
428
+ . expect ( "cache corrupted" )
429
+ . expect ( "expected eviction" ) ;
430
+ assert_eq ! ( evicted, ( 1 , 1 ) ) ;
431
+ }
432
+
433
+ #[ test]
434
+ fn test_lru_cache_capacity_one_flush ( ) {
435
+ let mut cache = LruCache :: new ( 1 ) ;
436
+
437
+ cache. insert ( 1 , 1 ) . expect ( "cache corrupted" ) ;
438
+
439
+ let mut flushed = Vec :: new ( ) ;
440
+ cache
441
+ . flush ( |k, v| {
442
+ flushed. push ( ( * k, v) ) ;
443
+ Ok :: < ( ) , ( ) > ( ( ) )
444
+ } )
445
+ . expect ( "cache corrupted" )
446
+ . expect ( "flush failed" ) ;
447
+
448
+ assert_eq ! ( flushed, vec![ ( 1 , 1 ) ] ) ;
449
+
450
+ cache. insert ( 2 , 2 ) . expect ( "cache corrupted" ) ;
451
+
452
+ let mut flushed = Vec :: new ( ) ;
453
+ cache
454
+ . flush ( |k, v| {
455
+ flushed. push ( ( * k, v) ) ;
456
+ Ok :: < ( ) , ( ) > ( ( ) )
457
+ } )
458
+ . expect ( "cache corrupted" )
459
+ . expect ( "flush failed" ) ;
460
+
461
+ assert_eq ! ( flushed, vec![ ( 2 , 2 ) ] ) ;
462
+ }
463
+
396
464
/// Simple LRU implementation for testing
397
465
pub struct SimpleLRU {
398
466
pub cache : Vec < Node < u32 , u32 > > ,
0 commit comments