22
33import org .junit .Test ;
44
5- import static org .hamcrest .CoreMatchers .is ;
6- import static org .hamcrest .CoreMatchers .nullValue ;
7- import static org .hamcrest .MatcherAssert .assertThat ;
5+ import static org .assertj .core .api .Assertions .assertThat ;
86
97public class LFUCacheUsingLinkedHashSetTest {
108
119 @ Test
1210 public void testCacheWithZeroCapacity () {
1311 LFUCacheUsingLinkedHashSet <Integer , Integer > cache = new LFUCacheUsingLinkedHashSet <>(0 );
1412
15- assertThat (cache .get (2 ), nullValue () );
13+ assertThat (cache .get (2 )). isNull ( );
1614 cache .put (2 , 67 );
17- assertThat (cache .get (2 ), nullValue () );
15+ assertThat (cache .get (2 )). isNull ( );
1816 }
1917
2018 @ Test
2119 public void testCache () {
2220 LFUCacheUsingLinkedHashSet <Integer , Integer > cache = new LFUCacheUsingLinkedHashSet <>(2 );
2321 cache .put (1 , 10 );
2422 cache .put (2 , 20 );
25- assertThat (cache .get (1 ), is (10 ) );
23+ assertThat (cache .get (1 )). isEqualTo (10 );
2624
2725 cache .put (3 , 30 ); //evicts key 2
2826
29- assertThat (cache .get (2 ), nullValue () );
30- assertThat (cache .get (3 ), is (30 ) );
27+ assertThat (cache .get (2 )). isNull ( );
28+ assertThat (cache .get (3 )). isEqualTo (30 );
3129
3230 cache .put (4 , 40 ); //evicts key 1
3331
34- assertThat (cache .get (1 ), nullValue () );
35- assertThat (cache .get (3 ), is (30 ) );
36- assertThat (cache .get (4 ), is (40 ) );
32+ assertThat (cache .get (1 )). isNull ( );
33+ assertThat (cache .get (3 )). isEqualTo (30 );
34+ assertThat (cache .get (4 )). isEqualTo (40 );
3735 }
3836
3937 @ Test
@@ -42,29 +40,29 @@ public void testCacheComplex() {
4240 cache .put (1 , 10 );
4341 cache .put (2 , 20 );
4442 cache .put (3 , 30 );
45- assertThat (cache .get (1 ), is (10 ) );
46- assertThat (cache .get (3 ), is (30 ) );
47- assertThat (cache .get (1 ), is (10 ) );
48- assertThat (cache .get (1 ), is (10 ) ); // 1 -> 3 times used
49- assertThat (cache .get (3 ), is (30 ) ); // 3 -> 2 times used
50- assertThat (cache .get (2 ), is (20 ) ); // 2 -> 1 times used
43+ assertThat (cache .get (1 )). isEqualTo (10 );
44+ assertThat (cache .get (3 )). isEqualTo (30 );
45+ assertThat (cache .get (1 )). isEqualTo (10 );
46+ assertThat (cache .get (1 )). isEqualTo (10 ); // 1 -> 3 times used
47+ assertThat (cache .get (3 )). isEqualTo (30 ); // 3 -> 2 times used
48+ assertThat (cache .get (2 )). isEqualTo (20 ); // 2 -> 1 times used
5149
5250 cache .put (4 , 40 ); // evicts key 2 (less used)
5351
54- assertThat (cache .get (1 ), is (10 ) ); // 1 -> 4 times used
55- assertThat (cache .get (2 ), nullValue () );
56- assertThat (cache .get (4 ), is (40 ) );
57- assertThat (cache .get (4 ), is (40 ) );
58- assertThat (cache .get (4 ), is (40 ) ); // 4 -> 3 times used
59- assertThat (cache .get (3 ), is (30 ) ); // 3 -> 3 times used
52+ assertThat (cache .get (1 )). isEqualTo (10 ); // 1 -> 4 times used
53+ assertThat (cache .get (2 )). isNull ( );
54+ assertThat (cache .get (4 )). isEqualTo (40 );
55+ assertThat (cache .get (4 )). isEqualTo (40 );
56+ assertThat (cache .get (4 )). isEqualTo (40 ); // 4 -> 3 times used
57+ assertThat (cache .get (3 )). isEqualTo (30 ); // 3 -> 3 times used
6058
6159 cache .put (5 , 50 ); // evicts key 4 (same used as 3, but 3 is last used)
6260
63- assertThat (cache .get (1 ), is (10 ) );
64- assertThat (cache .get (2 ), nullValue () );
65- assertThat (cache .get (3 ), is (30 ) );
66- assertThat (cache .get (4 ), nullValue () );
67- assertThat (cache .get (5 ), is (50 ) );
61+ assertThat (cache .get (1 )). isEqualTo (10 );
62+ assertThat (cache .get (2 )). isNull ( );
63+ assertThat (cache .get (3 )). isEqualTo (30 );
64+ assertThat (cache .get (4 )). isNull ( );
65+ assertThat (cache .get (5 )). isEqualTo (50 );
6866 }
6967
7068 @ Test
@@ -74,23 +72,23 @@ public void testCacheLeetCode_updateValue() {
7472 cache .put (2 , 1 );
7573 cache .put (2 , 2 ); // overwrites value for key 2
7674 cache .put (4 , 4 ); // evicts key 3
77- assertThat (cache .get (2 ), is ( 2 ) );
75+ assertThat (cache .get (2 )). isEqualTo ( 2 );
7876 }
7977
8078 @ Test
8179 public void determineKeyToDelete () {
8280 var cache = new LFUCacheUsingLinkedHashSet <Integer , Integer >(2 );
8381 cache .put (2 , 100 );
8482 cache .put (3 , 200 );
85- assertThat (cache .determineKeyToDelete (), is ( 2 ) );
83+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 2 );
8684
8785 cache .get (2 );
88- assertThat (cache .determineKeyToDelete (), is ( 3 ) );
86+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 3 );
8987 cache .get (3 );
90- assertThat (cache .determineKeyToDelete (), is ( 2 ) );
88+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 2 );
9189 cache .put (4 , 500 );
92- assertThat (cache .determineKeyToDelete (), is ( 4 ) );
90+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 4 );
9391 cache .get (4 );
94- assertThat (cache .determineKeyToDelete (), is ( 3 ) );
92+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 3 );
9593 }
9694}
0 commit comments