2
2
3
3
import org .junit .Test ;
4
4
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 ;
8
6
9
7
public class LFUCacheUsingLinkedHashSetTest {
10
8
11
9
@ Test
12
10
public void testCacheWithZeroCapacity () {
13
11
LFUCacheUsingLinkedHashSet <Integer , Integer > cache = new LFUCacheUsingLinkedHashSet <>(0 );
14
12
15
- assertThat (cache .get (2 ), nullValue () );
13
+ assertThat (cache .get (2 )). isNull ( );
16
14
cache .put (2 , 67 );
17
- assertThat (cache .get (2 ), nullValue () );
15
+ assertThat (cache .get (2 )). isNull ( );
18
16
}
19
17
20
18
@ Test
21
19
public void testCache () {
22
20
LFUCacheUsingLinkedHashSet <Integer , Integer > cache = new LFUCacheUsingLinkedHashSet <>(2 );
23
21
cache .put (1 , 10 );
24
22
cache .put (2 , 20 );
25
- assertThat (cache .get (1 ), is (10 ) );
23
+ assertThat (cache .get (1 )). isEqualTo (10 );
26
24
27
25
cache .put (3 , 30 ); //evicts key 2
28
26
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 );
31
29
32
30
cache .put (4 , 40 ); //evicts key 1
33
31
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 );
37
35
}
38
36
39
37
@ Test
@@ -42,29 +40,29 @@ public void testCacheComplex() {
42
40
cache .put (1 , 10 );
43
41
cache .put (2 , 20 );
44
42
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
51
49
52
50
cache .put (4 , 40 ); // evicts key 2 (less used)
53
51
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
60
58
61
59
cache .put (5 , 50 ); // evicts key 4 (same used as 3, but 3 is last used)
62
60
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 );
68
66
}
69
67
70
68
@ Test
@@ -74,23 +72,23 @@ public void testCacheLeetCode_updateValue() {
74
72
cache .put (2 , 1 );
75
73
cache .put (2 , 2 ); // overwrites value for key 2
76
74
cache .put (4 , 4 ); // evicts key 3
77
- assertThat (cache .get (2 ), is ( 2 ) );
75
+ assertThat (cache .get (2 )). isEqualTo ( 2 );
78
76
}
79
77
80
78
@ Test
81
79
public void determineKeyToDelete () {
82
80
var cache = new LFUCacheUsingLinkedHashSet <Integer , Integer >(2 );
83
81
cache .put (2 , 100 );
84
82
cache .put (3 , 200 );
85
- assertThat (cache .determineKeyToDelete (), is ( 2 ) );
83
+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 2 );
86
84
87
85
cache .get (2 );
88
- assertThat (cache .determineKeyToDelete (), is ( 3 ) );
86
+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 3 );
89
87
cache .get (3 );
90
- assertThat (cache .determineKeyToDelete (), is ( 2 ) );
88
+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 2 );
91
89
cache .put (4 , 500 );
92
- assertThat (cache .determineKeyToDelete (), is ( 4 ) );
90
+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 4 );
93
91
cache .get (4 );
94
- assertThat (cache .determineKeyToDelete (), is ( 3 ) );
92
+ assertThat (cache .determineKeyToDelete ()). isEqualTo ( 3 );
95
93
}
96
94
}
0 commit comments