@@ -10,124 +10,15 @@ import (
1010)
1111
1212func TestLRU (t * testing.T ) {
13- cache := LRU {Size : 1 }
13+ cache := & LRU {Size : 1 }
1414
15- id1 := ids.ID {1 }
16- if _ , found := cache .Get (id1 ); found {
17- t .Fatalf ("Retrieved value when none exists" )
18- }
19-
20- expectedValue1 := 1
21- cache .Put (id1 , expectedValue1 )
22- if value , found := cache .Get (id1 ); ! found {
23- t .Fatalf ("Failed to retrieve value when one exists" )
24- } else if value != expectedValue1 {
25- t .Fatalf ("Failed to retrieve correct value when one exists" )
26- }
27-
28- cache .Put (id1 , expectedValue1 )
29- if value , found := cache .Get (id1 ); ! found {
30- t .Fatalf ("Failed to retrieve value when one exists" )
31- } else if value != expectedValue1 {
32- t .Fatalf ("Failed to retrieve correct value when one exists" )
33- }
34-
35- cache .Put (id1 , expectedValue1 )
36- if value , found := cache .Get (id1 ); ! found {
37- t .Fatalf ("Failed to retrieve value when one exists" )
38- } else if value != expectedValue1 {
39- t .Fatalf ("Failed to retrieve correct value when one exists" )
40- }
41-
42- id2 := ids.ID {2 }
43-
44- expectedValue2 := 2
45- cache .Put (id2 , expectedValue2 )
46- if _ , found := cache .Get (id1 ); found {
47- t .Fatalf ("Retrieved value when none exists" )
48- }
49- if value , found := cache .Get (id2 ); ! found {
50- t .Fatalf ("Failed to retrieve value when one exists" )
51- } else if value != expectedValue2 {
52- t .Fatalf ("Failed to retrieve correct value when one exists" )
53- }
15+ TestBasic (t , cache )
5416}
5517
5618func TestLRUEviction (t * testing.T ) {
57- cache := LRU {Size : 2 }
58-
59- id1 := ids.ID {1 }
60- id2 := ids.ID {2 }
61- id3 := ids.ID {3 }
62-
63- cache .Put (id1 , 1 )
64- cache .Put (id2 , 2 )
65-
66- if val , found := cache .Get (id1 ); ! found {
67- t .Fatalf ("Failed to retrieve value when one exists" )
68- } else if val != 1 {
69- t .Fatalf ("Retrieved wrong value" )
70- } else if val , found := cache .Get (id2 ); ! found {
71- t .Fatalf ("Failed to retrieve value when one exists" )
72- } else if val != 2 {
73- t .Fatalf ("Retrieved wrong value" )
74- } else if _ , found := cache .Get (id3 ); found {
75- t .Fatalf ("Retrieve value when none exists" )
76- }
77-
78- cache .Put (id3 , 3 )
79-
80- if _ , found := cache .Get (id1 ); found {
81- t .Fatalf ("Retrieve value when none exists" )
82- } else if val , found := cache .Get (id2 ); ! found {
83- t .Fatalf ("Failed to retrieve value when one exists" )
84- } else if val != 2 {
85- t .Fatalf ("Retrieved wrong value" )
86- } else if val , found := cache .Get (id3 ); ! found {
87- t .Fatalf ("Failed to retrieve value when one exists" )
88- } else if val != 3 {
89- t .Fatalf ("Retrieved wrong value" )
90- }
91-
92- cache .Get (id2 )
93- cache .Put (id1 , 1 )
94-
95- if val , found := cache .Get (id1 ); ! found {
96- t .Fatalf ("Failed to retrieve value when one exists" )
97- } else if val != 1 {
98- t .Fatalf ("Retrieved wrong value" )
99- } else if val , found := cache .Get (id2 ); ! found {
100- t .Fatalf ("Failed to retrieve value when one exists" )
101- } else if val != 2 {
102- t .Fatalf ("Retrieved wrong value" )
103- } else if _ , found := cache .Get (id3 ); found {
104- t .Fatalf ("Retrieved value when none exists" )
105- }
106-
107- cache .Evict (id2 )
108- cache .Put (id3 , 3 )
109-
110- if val , found := cache .Get (id1 ); ! found {
111- t .Fatalf ("Failed to retrieve value when one exists" )
112- } else if val != 1 {
113- t .Fatalf ("Retrieved wrong value" )
114- } else if _ , found := cache .Get (id2 ); found {
115- t .Fatalf ("Retrieved value when none exists" )
116- } else if val , found := cache .Get (id3 ); ! found {
117- t .Fatalf ("Failed to retrieve value when one exists" )
118- } else if val != 3 {
119- t .Fatalf ("Retrieved wrong value" )
120- }
121-
122- cache .Flush ()
19+ cache := & LRU {Size : 2 }
12320
124- if _ , found := cache .Get (id1 ); found {
125- t .Fatalf ("Retrieved value when none exists" )
126- } else if _ , found := cache .Get (id2 ); found {
127- t .Fatalf ("Retrieved value when none exists" )
128- } else if _ , found := cache .Get (id3 ); found {
129- t .Fatalf ("Retrieved value when none exists" )
130- }
21+ TestEviction (t , cache )
13122}
13223
13324func TestLRUResize (t * testing.T ) {
0 commit comments