aboutsummaryrefslogtreecommitdiffstats
path: root/spec/util_cache_spec.lua
diff options
context:
space:
mode:
Diffstat (limited to 'spec/util_cache_spec.lua')
-rw-r--r--spec/util_cache_spec.lua74
1 files changed, 73 insertions, 1 deletions
diff --git a/spec/util_cache_spec.lua b/spec/util_cache_spec.lua
index d57e25ac..ae7b1936 100644
--- a/spec/util_cache_spec.lua
+++ b/spec/util_cache_spec.lua
@@ -4,6 +4,20 @@ local cache = require "util.cache";
describe("util.cache", function()
describe("#new()", function()
it("should work", function()
+ do
+ local c = cache.new(1);
+ assert.is_not_nil(c);
+
+ assert.has_error(function ()
+ cache.new(0);
+ end);
+ assert.has_error(function ()
+ cache.new(-1);
+ end);
+ assert.has_error(function ()
+ cache.new("foo");
+ end);
+ end
local c = cache.new(5);
@@ -314,7 +328,7 @@ describe("util.cache", function()
end);
- (_VERSION=="Lua 5.1" and pending or it)(":table works", function ()
+ it(":table works", function ()
local t = cache.new(3):table();
assert.is.table(t);
t["a"] = "1";
@@ -336,5 +350,63 @@ describe("util.cache", function()
assert.spy(i).was_called_with("c", "3");
assert.spy(i).was_called_with("d", "4");
end);
+
+ local function vs(t)
+ local vs_ = {};
+ for v in t:values() do
+ vs_[#vs_+1] = v;
+ end
+ return vs_;
+ end
+
+ it(":values works", function ()
+ local t = cache.new(3);
+ t:set("k1", "v1");
+ t:set("k2", "v2");
+ assert.same({"v2", "v1"}, vs(t));
+ t:set("k3", "v3");
+ assert.same({"v3", "v2", "v1"}, vs(t));
+ t:set("k4", "v4");
+ assert.same({"v4", "v3", "v2"}, vs(t));
+ end);
+
+ it(":resize works", function ()
+ local c = cache.new(5);
+ for i = 1, 5 do
+ c:set(("k%d"):format(i), ("v%d"):format(i));
+ end
+ assert.same({"v5", "v4", "v3", "v2", "v1"}, vs(c));
+ assert.has_error(function ()
+ c:resize(-1);
+ end);
+ assert.has_error(function ()
+ c:resize(0);
+ end);
+ assert.has_error(function ()
+ c:resize("foo");
+ end);
+ c:resize(3);
+ assert.same({"v5", "v4", "v3"}, vs(c));
+ end);
+
+ it("eviction stuff", function ()
+ local c = cache.new(4, function(_k,_v,c)
+ if c.size < 10 then
+ c:resize(c.size*2);
+ end
+ end)
+ for i = 1,20 do
+ c:set(i,i)
+ end
+ assert.equal(16, c.size);
+ assert.is_nil(c:get(1))
+ assert.is_nil(c:get(4))
+ assert.equal(5, c:get(5))
+ assert.equal(20, c:get(20))
+ c:resize(4)
+ assert.equal(20, c:get(20))
+ assert.equal(17, c:get(17))
+ assert.is_nil(c:get(10))
+ end)
end);
end);