4 local function expect_kv(key
, value
, actual_key
, actual_value
)
5 assert_equal(key
, actual_key
, "key incorrect");
6 assert_equal(value
, actual_value
, "value incorrect");
9 expect_kv(nil, nil, c
:head());
10 expect_kv(nil, nil, c
:tail());
12 assert_equal(c
:count(), 0);
15 assert_equal(c
:count(), 1);
16 expect_kv("one", 1, c
:head());
17 expect_kv("one", 1, c
:tail());
20 expect_kv("two", 2, c
:head());
21 expect_kv("one", 1, c
:tail());
24 expect_kv("three", 3, c
:head());
25 expect_kv("one", 1, c
:tail());
29 assert_equal(c
:count(), 5);
30 expect_kv("five", 5, c
:head());
31 expect_kv("one", 1, c
:tail());
34 assert_equal(c
:count(), 5);
35 expect_kv("five", 5, c
:head());
36 expect_kv("one", 1, c
:tail());
38 assert_equal(c
:get("one"), 1);
39 expect_kv("five", 5, c
:head());
40 expect_kv("one", 1, c
:tail());
42 assert_equal(c
:get("two"), 2);
43 assert_equal(c
:get("three"), 3);
44 assert_equal(c
:get("four"), 4);
45 assert_equal(c
:get("five"), 5);
47 assert_equal(c
:get("foo"), nil);
48 assert_equal(c
:get("bar"), nil);
51 assert_equal(c
:count(), 5);
52 expect_kv("six", 6, c
:head());
53 expect_kv("two", 2, c
:tail());
55 assert_equal(c
:get("one"), nil);
56 assert_equal(c
:get("two"), 2);
57 assert_equal(c
:get("three"), 3);
58 assert_equal(c
:get("four"), 4);
59 assert_equal(c
:get("five"), 5);
60 assert_equal(c
:get("six"), 6);
63 assert_equal(c
:count(), 4);
65 assert_equal(c
:get("one"), nil);
66 assert_equal(c
:get("two"), 2);
67 assert_equal(c
:get("three"), nil);
68 assert_equal(c
:get("four"), 4);
69 assert_equal(c
:get("five"), 5);
70 assert_equal(c
:get("six"), 6);
73 assert_equal(c
:count(), 5);
75 assert_equal(c
:get("one"), nil);
76 assert_equal(c
:get("two"), 2);
77 assert_equal(c
:get("three"), nil);
78 assert_equal(c
:get("four"), 4);
79 assert_equal(c
:get("five"), 5);
80 assert_equal(c
:get("six"), 6);
81 assert_equal(c
:get("seven"), 7);
84 assert_equal(c
:count(), 5);
86 assert_equal(c
:get("one"), nil);
87 assert_equal(c
:get("two"), nil);
88 assert_equal(c
:get("three"), nil);
89 assert_equal(c
:get("four"), 4);
90 assert_equal(c
:get("five"), 5);
91 assert_equal(c
:get("six"), 6);
92 assert_equal(c
:get("seven"), 7);
93 assert_equal(c
:get("eight"), 8);
96 assert_equal(c
:count(), 5);
98 assert_equal(c
:get("one"), nil);
99 assert_equal(c
:get("two"), nil);
100 assert_equal(c
:get("three"), nil);
101 assert_equal(c
:get("four"), 4);
102 assert_equal(c
:get("five"), 5);
103 assert_equal(c
:get("six"), 6);
104 assert_equal(c
:get("seven"), 7);
105 assert_equal(c
:get("eight"), 8);
108 assert_equal(c
:count(), 5);
110 assert_equal(c
:get("one"), nil);
111 assert_equal(c
:get("two"), nil);
112 assert_equal(c
:get("three"), nil);
113 assert_equal(c
:get("four"), 4);
114 assert_equal(c
:get("five"), nil);
115 assert_equal(c
:get("six"), 6);
116 assert_equal(c
:get("seven"), 7);
117 assert_equal(c
:get("eight"), 8);
118 assert_equal(c
:get("nine"), 9);
121 local keys
= { "nine", "four", "eight", "seven", "six" };
122 local values
= { 9, 4, 8, 7, 6 };
124 for k
, v
in c
:items() do
126 assert_equal(k
, keys
[i
]);
127 assert_equal(v
, values
[i
]);
131 c
:set("four", "2+2");
132 assert_equal(c
:count(), 5);
134 assert_equal(c
:get("one"), nil);
135 assert_equal(c
:get("two"), nil);
136 assert_equal(c
:get("three"), nil);
137 assert_equal(c
:get("four"), "2+2");
138 assert_equal(c
:get("five"), nil);
139 assert_equal(c
:get("six"), 6);
140 assert_equal(c
:get("seven"), 7);
141 assert_equal(c
:get("eight"), 8);
142 assert_equal(c
:get("nine"), 9);
146 local keys
= { "four", "nine", "eight", "seven", "six" };
147 local values
= { "2+2", 9, 8, 7, 6 };
149 for k
, v
in c
:items() do
151 assert_equal(k
, keys
[i
]);
152 assert_equal(v
, values
[i
]);
157 assert_equal(c
:count(), 5);
159 assert_equal(c
:get("one"), nil);
160 assert_equal(c
:get("two"), nil);
161 assert_equal(c
:get("three"), nil);
162 assert_equal(c
:get("four"), "2+2");
163 assert_equal(c
:get("five"), nil);
164 assert_equal(c
:get("six"), 6);
165 assert_equal(c
:get("seven"), 7);
166 assert_equal(c
:get("eight"), 8);
167 assert_equal(c
:get("nine"), 9);
171 local keys
= { "four", "nine", "eight", "seven", "six" };
172 local values
= { "2+2", 9, 8, 7, 6 };
174 for k
, v
in c
:items() do
176 assert_equal(k
, keys
[i
]);
177 assert_equal(v
, values
[i
]);
183 assert_equal(c
:get("one"), nil);
184 assert_equal(c
:get("two"), nil);
185 assert_equal(c
:get("three"), nil);
186 assert_equal(c
:get("four"), nil);
187 assert_equal(c
:get("five"), nil);
188 assert_equal(c
:get("six"), 6);
189 assert_equal(c
:get("seven"), 7);
190 assert_equal(c
:get("eight"), 8);
191 assert_equal(c
:get("nine"), 9);
195 local keys
= { "nine", "eight", "seven", "six" };
196 local values
= { 9, 8, 7, 6 };
198 for k
, v
in c
:items() do
200 assert_equal(k
, keys
[i
]);
201 assert_equal(v
, values
[i
]);
207 local evicted_key
, evicted_value
;
208 local c2
= new(3, function (_key
, _value
)
209 evicted_key
, evicted_value
= _key
, _value
;
211 local function set(k
, v
, should_evict_key
, should_evict_value
)
212 evicted_key
, evicted_value
= nil, nil;
214 assert_equal(evicted_key
, should_evict_key
);
215 assert_equal(evicted_value
, should_evict_value
);
231 local evicted_key
, evicted_value
;
232 local c3
= new(1, function (_key
, _value
)
233 evicted_key
, evicted_value
= _key
, _value
;
235 -- Sanity check for what we're evicting
236 assert_equal(_key
, "a");
237 assert_equal(_value
, 1);
238 -- We're going to block eviction of this key/value, so set to nil...
239 evicted_key
, evicted_value
= nil, nil;
240 -- Returning false to block eviction
244 local function set(k
, v
, should_evict_key
, should_evict_value
)
245 evicted_key
, evicted_value
= nil, nil;
246 local ret
= c3
:set(k
, v
);
247 assert_equal(evicted_key
, should_evict_key
);
248 assert_equal(evicted_value
, should_evict_value
);
257 -- Our on_evict prevents "a" from being evicted, causing this to fail...
258 assert_equal(set("b", 2), false, "Failed to prevent eviction, or signal result");
260 expect_kv("a", 1, c3
:head());
261 expect_kv("a", 1, c3
:tail());
263 -- Check the final state is what we expect
264 assert_equal(c3
:get("a"), 1);
265 assert_equal(c3
:get("b"), nil);
266 assert_equal(c3
:count(), 1);
270 local c4
= new(3, false);
272 assert_equal(c4
:set("a", 1), true);
273 assert_equal(c4
:set("a", 1), true);
274 assert_equal(c4
:set("a", 1), true);
275 assert_equal(c4
:set("a", 1), true);
276 assert_equal(c4
:set("b", 2), true);
277 assert_equal(c4
:set("c", 3), true);
278 assert_equal(c4
:set("d", 4), false);
279 assert_equal(c4
:set("d", 4), false);
280 assert_equal(c4
:set("d", 4), false);
282 expect_kv("c", 3, c4
:head());
283 expect_kv("a", 1, c4
:tail());
285 local c5
= new(3, function (k
, v
)
294 assert_equal(c5
:set("a", 1), true);
295 assert_equal(c5
:set("a", 1), true);
296 assert_equal(c5
:set("a", 1), true);
297 assert_equal(c5
:set("a", 1), true);
298 assert_equal(c5
:set("b", 2), true);
299 assert_equal(c5
:set("c", 3), true);
300 assert_equal(c5
:set("d", 4), true); -- "a" evicted (cb returned nil)
301 assert_equal(c5
:set("d", 4), true); -- nop
302 assert_equal(c5
:set("d", 4), true); -- nop
303 assert_equal(c5
:set("e", 5), true); -- "b" evicted (cb returned true)
304 assert_equal(c5
:set("f", 6), false); -- "c" won't evict (cb returned false)
306 expect_kv("e", 5, c5
:head());
307 expect_kv("c", 3, c5
:tail());