Comparison

tests/test_util_cache.lua @ 7505:3d950ee0de35

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Thu, 14 Jul 2016 13:41:02 +0200
parent 7504:b43cbbbb806f
child 7961:ff556d010225
comparison
equal deleted inserted replaced
7497:22942eda53f8 7505:3d950ee0de35
115 assert_equal(c:get("six"), 6); 115 assert_equal(c:get("six"), 6);
116 assert_equal(c:get("seven"), 7); 116 assert_equal(c:get("seven"), 7);
117 assert_equal(c:get("eight"), 8); 117 assert_equal(c:get("eight"), 8);
118 assert_equal(c:get("nine"), 9); 118 assert_equal(c:get("nine"), 9);
119 119
120 local keys = { "nine", "four", "eight", "seven", "six" }; 120 do
121 local values = { 9, 4, 8, 7, 6 }; 121 local keys = { "nine", "four", "eight", "seven", "six" };
122 local i = 0; 122 local values = { 9, 4, 8, 7, 6 };
123 for k, v in c:items() do 123 local i = 0;
124 i = i + 1; 124 for k, v in c:items() do
125 assert_equal(k, keys[i]); 125 i = i + 1;
126 assert_equal(v, values[i]); 126 assert_equal(k, keys[i]);
127 end 127 assert_equal(v, values[i]);
128 assert_equal(i, 5); 128 end
129 129 assert_equal(i, 5);
130 c:set("four", "2+2"); 130
131 assert_equal(c:count(), 5); 131 c:set("four", "2+2");
132 132 assert_equal(c:count(), 5);
133 assert_equal(c:get("one"), nil); 133
134 assert_equal(c:get("two"), nil); 134 assert_equal(c:get("one"), nil);
135 assert_equal(c:get("three"), nil); 135 assert_equal(c:get("two"), nil);
136 assert_equal(c:get("four"), "2+2"); 136 assert_equal(c:get("three"), nil);
137 assert_equal(c:get("five"), nil); 137 assert_equal(c:get("four"), "2+2");
138 assert_equal(c:get("six"), 6); 138 assert_equal(c:get("five"), nil);
139 assert_equal(c:get("seven"), 7); 139 assert_equal(c:get("six"), 6);
140 assert_equal(c:get("eight"), 8); 140 assert_equal(c:get("seven"), 7);
141 assert_equal(c:get("nine"), 9); 141 assert_equal(c:get("eight"), 8);
142 142 assert_equal(c:get("nine"), 9);
143 local keys = { "four", "nine", "eight", "seven", "six" }; 143 end
144 local values = { "2+2", 9, 8, 7, 6 }; 144
145 local i = 0; 145 do
146 for k, v in c:items() do 146 local keys = { "four", "nine", "eight", "seven", "six" };
147 i = i + 1; 147 local values = { "2+2", 9, 8, 7, 6 };
148 assert_equal(k, keys[i]); 148 local i = 0;
149 assert_equal(v, values[i]); 149 for k, v in c:items() do
150 end 150 i = i + 1;
151 assert_equal(i, 5); 151 assert_equal(k, keys[i]);
152 152 assert_equal(v, values[i]);
153 c:set("foo", nil); 153 end
154 assert_equal(c:count(), 5); 154 assert_equal(i, 5);
155 155
156 assert_equal(c:get("one"), nil); 156 c:set("foo", nil);
157 assert_equal(c:get("two"), nil); 157 assert_equal(c:count(), 5);
158 assert_equal(c:get("three"), nil); 158
159 assert_equal(c:get("four"), "2+2"); 159 assert_equal(c:get("one"), nil);
160 assert_equal(c:get("five"), nil); 160 assert_equal(c:get("two"), nil);
161 assert_equal(c:get("six"), 6); 161 assert_equal(c:get("three"), nil);
162 assert_equal(c:get("seven"), 7); 162 assert_equal(c:get("four"), "2+2");
163 assert_equal(c:get("eight"), 8); 163 assert_equal(c:get("five"), nil);
164 assert_equal(c:get("nine"), 9); 164 assert_equal(c:get("six"), 6);
165 165 assert_equal(c:get("seven"), 7);
166 local keys = { "four", "nine", "eight", "seven", "six" }; 166 assert_equal(c:get("eight"), 8);
167 local values = { "2+2", 9, 8, 7, 6 }; 167 assert_equal(c:get("nine"), 9);
168 local i = 0; 168 end
169 for k, v in c:items() do 169
170 i = i + 1; 170 do
171 assert_equal(k, keys[i]); 171 local keys = { "four", "nine", "eight", "seven", "six" };
172 assert_equal(v, values[i]); 172 local values = { "2+2", 9, 8, 7, 6 };
173 end 173 local i = 0;
174 assert_equal(i, 5); 174 for k, v in c:items() do
175 175 i = i + 1;
176 c:set("four", nil); 176 assert_equal(k, keys[i]);
177 177 assert_equal(v, values[i]);
178 assert_equal(c:get("one"), nil); 178 end
179 assert_equal(c:get("two"), nil); 179 assert_equal(i, 5);
180 assert_equal(c:get("three"), nil); 180
181 assert_equal(c:get("four"), nil); 181 c:set("four", nil);
182 assert_equal(c:get("five"), nil); 182
183 assert_equal(c:get("six"), 6); 183 assert_equal(c:get("one"), nil);
184 assert_equal(c:get("seven"), 7); 184 assert_equal(c:get("two"), nil);
185 assert_equal(c:get("eight"), 8); 185 assert_equal(c:get("three"), nil);
186 assert_equal(c:get("nine"), 9); 186 assert_equal(c:get("four"), nil);
187 187 assert_equal(c:get("five"), nil);
188 local keys = { "nine", "eight", "seven", "six" }; 188 assert_equal(c:get("six"), 6);
189 local values = { 9, 8, 7, 6 }; 189 assert_equal(c:get("seven"), 7);
190 local i = 0; 190 assert_equal(c:get("eight"), 8);
191 for k, v in c:items() do 191 assert_equal(c:get("nine"), 9);
192 i = i + 1; 192 end
193 assert_equal(k, keys[i]); 193
194 assert_equal(v, values[i]); 194 do
195 end 195 local keys = { "nine", "eight", "seven", "six" };
196 assert_equal(i, 4); 196 local values = { 9, 8, 7, 6 };
197 197 local i = 0;
198 local evicted_key, evicted_value; 198 for k, v in c:items() do
199 local c2 = new(3, function (_key, _value) 199 i = i + 1;
200 evicted_key, evicted_value = _key, _value; 200 assert_equal(k, keys[i]);
201 end); 201 assert_equal(v, values[i]);
202 local function set(k, v, should_evict_key, should_evict_value) 202 end
203 evicted_key, evicted_value = nil, nil; 203 assert_equal(i, 4);
204 c2:set(k, v); 204 end
205 assert_equal(evicted_key, should_evict_key); 205
206 assert_equal(evicted_value, should_evict_value); 206 do
207 end 207 local evicted_key, evicted_value;
208 set("a", 1) 208 local c2 = new(3, function (_key, _value)
209 set("a", 1) 209 evicted_key, evicted_value = _key, _value;
210 set("a", 1) 210 end);
211 set("a", 1) 211 local function set(k, v, should_evict_key, should_evict_value)
212 set("a", 1)
213
214 set("b", 2)
215 set("c", 3)
216 set("b", 2)
217 set("d", 4, "a", 1)
218 set("e", 5, "c", 3)
219
220
221 local evicted_key, evicted_value;
222 local c3 = new(1, function (_key, _value)
223 evicted_key, evicted_value = _key, _value;
224 if _key == "a" then
225 -- Sanity check for what we're evicting
226 assert_equal(_key, "a");
227 assert_equal(_value, 1);
228 -- We're going to block eviction of this key/value, so set to nil...
229 evicted_key, evicted_value = nil, nil; 212 evicted_key, evicted_value = nil, nil;
230 -- Returning false to block eviction 213 c2:set(k, v);
231 return false 214 assert_equal(evicted_key, should_evict_key);
232 end 215 assert_equal(evicted_value, should_evict_value);
233 end); 216 end
234 local function set(k, v, should_evict_key, should_evict_value) 217 set("a", 1)
235 evicted_key, evicted_value = nil, nil; 218 set("a", 1)
236 local ret = c3:set(k, v); 219 set("a", 1)
237 assert_equal(evicted_key, should_evict_key); 220 set("a", 1)
238 assert_equal(evicted_value, should_evict_value); 221 set("a", 1)
239 return ret; 222
240 end 223 set("b", 2)
241 set("a", 1) 224 set("c", 3)
242 set("a", 1) 225 set("b", 2)
243 set("a", 1) 226 set("d", 4, "a", 1)
244 set("a", 1) 227 set("e", 5, "c", 3)
245 set("a", 1) 228 end
246 229
247 -- Our on_evict prevents "a" from being evicted, causing this to fail... 230 do
248 assert_equal(set("b", 2), false, "Failed to prevent eviction, or signal result"); 231 local evicted_key, evicted_value;
249 232 local c3 = new(1, function (_key, _value)
250 expect_kv("a", 1, c3:head()); 233 evicted_key, evicted_value = _key, _value;
251 expect_kv("a", 1, c3:tail()); 234 if _key == "a" then
252 235 -- Sanity check for what we're evicting
253 -- Check the final state is what we expect 236 assert_equal(_key, "a");
254 assert_equal(c3:get("a"), 1); 237 assert_equal(_value, 1);
255 assert_equal(c3:get("b"), nil); 238 -- We're going to block eviction of this key/value, so set to nil...
256 assert_equal(c3:count(), 1); 239 evicted_key, evicted_value = nil, nil;
240 -- Returning false to block eviction
241 return false
242 end
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;
250 end
251 set("a", 1)
252 set("a", 1)
253 set("a", 1)
254 set("a", 1)
255 set("a", 1)
256
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");
259
260 expect_kv("a", 1, c3:head());
261 expect_kv("a", 1, c3:tail());
262
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);
267 end
257 268
258 269
259 local c4 = new(3, false); 270 local c4 = new(3, false);
260 271
261 assert_equal(c4:set("a", 1), true); 272 assert_equal(c4:set("a", 1), true);