CHANGES: Update for release
[prosody.git] / tests / test_util_cache.lua
blob4240c43301f5e5f5b0a0c7c9094e66fee7169015
1 function new(new)
2 local c = new(5);
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");
7 end
9 expect_kv(nil, nil, c:head());
10 expect_kv(nil, nil, c:tail());
12 assert_equal(c:count(), 0);
14 c:set("one", 1)
15 assert_equal(c:count(), 1);
16 expect_kv("one", 1, c:head());
17 expect_kv("one", 1, c:tail());
19 c:set("two", 2)
20 expect_kv("two", 2, c:head());
21 expect_kv("one", 1, c:tail());
23 c:set("three", 3)
24 expect_kv("three", 3, c:head());
25 expect_kv("one", 1, c:tail());
27 c:set("four", 4)
28 c:set("five", 5);
29 assert_equal(c:count(), 5);
30 expect_kv("five", 5, c:head());
31 expect_kv("one", 1, c:tail());
33 c:set("foo", nil);
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);
50 c:set("six", 6);
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);
62 c:set("three", nil);
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);
72 c:set("seven", 7);
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);
83 c:set("eight", 8);
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);
95 c:set("four", 4);
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);
107 c:set("nine", 9);
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 };
123 local i = 0;
124 for k, v in c:items() do
125 i = i + 1;
126 assert_equal(k, keys[i]);
127 assert_equal(v, values[i]);
129 assert_equal(i, 5);
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 };
148 local i = 0;
149 for k, v in c:items() do
150 i = i + 1;
151 assert_equal(k, keys[i]);
152 assert_equal(v, values[i]);
154 assert_equal(i, 5);
156 c:set("foo", nil);
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 };
173 local i = 0;
174 for k, v in c:items() do
175 i = i + 1;
176 assert_equal(k, keys[i]);
177 assert_equal(v, values[i]);
179 assert_equal(i, 5);
181 c:set("four", nil);
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 };
197 local i = 0;
198 for k, v in c:items() do
199 i = i + 1;
200 assert_equal(k, keys[i]);
201 assert_equal(v, values[i]);
203 assert_equal(i, 4);
207 local evicted_key, evicted_value;
208 local c2 = new(3, function (_key, _value)
209 evicted_key, evicted_value = _key, _value;
210 end);
211 local function set(k, v, should_evict_key, should_evict_value)
212 evicted_key, evicted_value = nil, nil;
213 c2:set(k, v);
214 assert_equal(evicted_key, should_evict_key);
215 assert_equal(evicted_value, should_evict_value);
217 set("a", 1)
218 set("a", 1)
219 set("a", 1)
220 set("a", 1)
221 set("a", 1)
223 set("b", 2)
224 set("c", 3)
225 set("b", 2)
226 set("d", 4, "a", 1)
227 set("e", 5, "c", 3)
231 local evicted_key, evicted_value;
232 local c3 = new(1, function (_key, _value)
233 evicted_key, evicted_value = _key, _value;
234 if _key == "a" then
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
241 return false
243 end);
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);
249 return ret;
251 set("a", 1)
252 set("a", 1)
253 set("a", 1)
254 set("a", 1)
255 set("a", 1)
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)
286 if k == "a" then
287 return nil;
288 elseif k == "b" then
289 return true;
291 return false;
292 end);
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());