@@ -371,6 +371,78 @@ mod tests {
371371 assert_eq ! ( result, Ok ( 4 ) ) ;
372372 }
373373
374+ #[ test]
375+ fn test_retain_unsync ( ) {
376+ let mut cache = unsync:: Cache :: < u64 , u64 > :: new ( 100 ) ;
377+ let ranges = 0 ..10 ;
378+ for i in ranges. clone ( ) {
379+ let guard = cache. get_ref_or_guard ( & i) . unwrap_err ( ) ;
380+ guard. insert ( i) ;
381+ assert_eq ! ( cache. get_ref_or_guard( & i) . ok( ) . copied( ) , Some ( i) ) ;
382+ }
383+ let small = 3 ;
384+ cache. retain ( |& key, & val| val > small && key > small) ;
385+ for i in ranges. clone ( ) {
386+ let actual = cache. get ( & i) ;
387+ if i > small {
388+ assert ! ( actual. is_some( ) ) ;
389+ assert_eq ! ( * actual. unwrap( ) , i) ;
390+ } else {
391+ assert ! ( actual. is_none( ) ) ;
392+ }
393+ }
394+ let big = 7 ;
395+ cache. retain ( |& key, & val| val < big && key < big) ;
396+ for i in ranges {
397+ let actual = cache. get ( & i) ;
398+ if i > small && i < big {
399+ assert ! ( actual. is_some( ) ) ;
400+ assert_eq ! ( * actual. unwrap( ) , i) ;
401+ } else {
402+ assert ! ( actual. is_none( ) ) ;
403+ }
404+ }
405+ }
406+
407+ #[ tokio:: test]
408+ async fn test_retain_sync ( ) {
409+ use crate :: sync:: * ;
410+ let cache = Cache :: < u64 , u64 > :: new ( 100 ) ;
411+ let ranges = 0 ..10 ;
412+ for i in ranges. clone ( ) {
413+ let GuardResult :: Guard ( guard) = cache. get_value_or_guard ( & i, None ) else {
414+ panic ! ( ) ;
415+ } ;
416+ guard. insert ( i) . unwrap ( ) ;
417+ let GuardResult :: Value ( v) = cache. get_value_or_guard ( & i, None ) else {
418+ panic ! ( ) ;
419+ } ;
420+ assert_eq ! ( v, i) ;
421+ }
422+ let small = 4 ;
423+ cache. retain ( |& key, & val| val > small && key > small) ;
424+ for i in ranges. clone ( ) {
425+ let actual = cache. get ( & i) ;
426+ if i > small {
427+ assert ! ( actual. is_some( ) ) ;
428+ assert_eq ! ( actual. unwrap( ) , i) ;
429+ } else {
430+ assert ! ( actual. is_none( ) ) ;
431+ }
432+ }
433+ let big = 8 ;
434+ cache. retain ( |& key, & val| val < big && key < big) ;
435+ for i in ranges {
436+ let actual = cache. get ( & i) ;
437+ if i > small && i < big {
438+ assert ! ( actual. is_some( ) ) ;
439+ assert_eq ! ( actual. unwrap( ) , i) ;
440+ } else {
441+ assert ! ( actual. is_none( ) ) ;
442+ }
443+ }
444+ }
445+
374446 #[ test]
375447 #[ cfg_attr( miri, ignore) ]
376448 fn test_value_or_guard ( ) {
0 commit comments