Comparison

spec/util_cache_spec.lua @ 12802:4a8740e01813

Merge 0.12->trunk
author Kim Alvefur <zash@zash.se>
date Mon, 12 Dec 2022 07:10:54 +0100
parent 12771:e9fcc69ea508
child 13174:8ec7b7d6556f
comparison
equal deleted inserted replaced
12801:ebd6b4d8bf04 12802:4a8740e01813
2 local cache = require "util.cache"; 2 local cache = require "util.cache";
3 3
4 describe("util.cache", function() 4 describe("util.cache", function()
5 describe("#new()", function() 5 describe("#new()", function()
6 it("should work", function() 6 it("should work", function()
7 do
8 local c = cache.new(1);
9 assert.is_not_nil(c);
10
11 assert.has_error(function ()
12 cache.new(0);
13 end);
14 assert.has_error(function ()
15 cache.new(-1);
16 end);
17 assert.has_error(function ()
18 cache.new("foo");
19 end);
20 end
7 21
8 local c = cache.new(5); 22 local c = cache.new(5);
9 23
10 local function expect_kv(key, value, actual_key, actual_value) 24 local function expect_kv(key, value, actual_key, actual_value)
11 assert.are.equal(key, actual_key, "key incorrect"); 25 assert.are.equal(key, actual_key, "key incorrect");
312 expect_kv("e", 5, c5:head()); 326 expect_kv("e", 5, c5:head());
313 expect_kv("c", 3, c5:tail()); 327 expect_kv("c", 3, c5:tail());
314 328
315 end); 329 end);
316 330
317 (_VERSION=="Lua 5.1" and pending or it)(":table works", function () 331 it(":table works", function ()
318 local t = cache.new(3):table(); 332 local t = cache.new(3):table();
319 assert.is.table(t); 333 assert.is.table(t);
320 t["a"] = "1"; 334 t["a"] = "1";
321 assert.are.equal(t["a"], "1"); 335 assert.are.equal(t["a"], "1");
322 t["b"] = "2"; 336 t["b"] = "2";
334 assert.spy(i).was_called(); 348 assert.spy(i).was_called();
335 assert.spy(i).was_called_with("b", "2"); 349 assert.spy(i).was_called_with("b", "2");
336 assert.spy(i).was_called_with("c", "3"); 350 assert.spy(i).was_called_with("c", "3");
337 assert.spy(i).was_called_with("d", "4"); 351 assert.spy(i).was_called_with("d", "4");
338 end); 352 end);
353
354 local function vs(t)
355 local vs_ = {};
356 for v in t:values() do
357 vs_[#vs_+1] = v;
358 end
359 return vs_;
360 end
361
362 it(":values works", function ()
363 local t = cache.new(3);
364 t:set("k1", "v1");
365 t:set("k2", "v2");
366 assert.same({"v2", "v1"}, vs(t));
367 t:set("k3", "v3");
368 assert.same({"v3", "v2", "v1"}, vs(t));
369 t:set("k4", "v4");
370 assert.same({"v4", "v3", "v2"}, vs(t));
371 end);
372
373 it(":resize works", function ()
374 local c = cache.new(5);
375 for i = 1, 5 do
376 c:set(("k%d"):format(i), ("v%d"):format(i));
377 end
378 assert.same({"v5", "v4", "v3", "v2", "v1"}, vs(c));
379 assert.has_error(function ()
380 c:resize(-1);
381 end);
382 assert.has_error(function ()
383 c:resize(0);
384 end);
385 assert.has_error(function ()
386 c:resize("foo");
387 end);
388 c:resize(3);
389 assert.same({"v5", "v4", "v3"}, vs(c));
390 end);
339 end); 391 end);
340 end); 392 end);