aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_util_cache.lua
diff options
context:
space:
mode:
authorMatthew Wild <mwild1@gmail.com>2015-11-26 00:12:51 +0000
committerMatthew Wild <mwild1@gmail.com>2015-11-26 00:12:51 +0000
commit0ad40f774a7fd8334289f9ef256e4ff082ce762d (patch)
tree41e1601b9d58d0af9eed0a688cee922085789318 /tests/test_util_cache.lua
parent7dc2548d04d3e9088ba669dffa102c6715a9dbb2 (diff)
downloadprosody-0ad40f774a7fd8334289f9ef256e4ff082ce762d.tar.gz
prosody-0ad40f774a7fd8334289f9ef256e4ff082ce762d.zip
tests: Add tests for util.cache
Diffstat (limited to 'tests/test_util_cache.lua')
-rw-r--r--tests/test_util_cache.lua174
1 files changed, 174 insertions, 0 deletions
diff --git a/tests/test_util_cache.lua b/tests/test_util_cache.lua
new file mode 100644
index 00000000..a174eea7
--- /dev/null
+++ b/tests/test_util_cache.lua
@@ -0,0 +1,174 @@
+
+function new(new)
+ local c = new(5);
+
+ assert_equal(c:count(), 0);
+
+ c:set("one", 1)
+ assert_equal(c:count(), 1);
+ c:set("two", 2)
+ c:set("three", 3)
+ c:set("four", 4)
+ c:set("five", 5);
+ assert_equal(c:count(), 5);
+
+ c:set("foo", nil);
+ assert_equal(c:count(), 5);
+
+ assert_equal(c:get("one"), 1);
+ assert_equal(c:get("two"), 2);
+ assert_equal(c:get("three"), 3);
+ assert_equal(c:get("four"), 4);
+ assert_equal(c:get("five"), 5);
+
+ assert_equal(c:get("foo"), nil);
+ assert_equal(c:get("bar"), nil);
+
+ c:set("six", 6);
+ assert_equal(c:count(), 5);
+
+ assert_equal(c:get("one"), nil);
+ assert_equal(c:get("two"), 2);
+ assert_equal(c:get("three"), 3);
+ assert_equal(c:get("four"), 4);
+ assert_equal(c:get("five"), 5);
+ assert_equal(c:get("six"), 6);
+
+ c:set("three", nil);
+ assert_equal(c:count(), 4);
+
+ assert_equal(c:get("one"), nil);
+ assert_equal(c:get("two"), 2);
+ assert_equal(c:get("three"), nil);
+ assert_equal(c:get("four"), 4);
+ assert_equal(c:get("five"), 5);
+ assert_equal(c:get("six"), 6);
+
+ c:set("seven", 7);
+ assert_equal(c:count(), 5);
+
+ assert_equal(c:get("one"), nil);
+ assert_equal(c:get("two"), 2);
+ assert_equal(c:get("three"), nil);
+ assert_equal(c:get("four"), 4);
+ assert_equal(c:get("five"), 5);
+ assert_equal(c:get("six"), 6);
+ assert_equal(c:get("seven"), 7);
+
+ c:set("eight", 8);
+ assert_equal(c:count(), 5);
+
+ assert_equal(c:get("one"), nil);
+ assert_equal(c:get("two"), nil);
+ assert_equal(c:get("three"), nil);
+ assert_equal(c:get("four"), 4);
+ assert_equal(c:get("five"), 5);
+ assert_equal(c:get("six"), 6);
+ assert_equal(c:get("seven"), 7);
+ assert_equal(c:get("eight"), 8);
+
+ c:set("four", 4);
+ assert_equal(c:count(), 5);
+
+ assert_equal(c:get("one"), nil);
+ assert_equal(c:get("two"), nil);
+ assert_equal(c:get("three"), nil);
+ assert_equal(c:get("four"), 4);
+ assert_equal(c:get("five"), 5);
+ assert_equal(c:get("six"), 6);
+ assert_equal(c:get("seven"), 7);
+ assert_equal(c:get("eight"), 8);
+
+ c:set("nine", 9);
+ assert_equal(c:count(), 5);
+
+ assert_equal(c:get("one"), nil);
+ assert_equal(c:get("two"), nil);
+ assert_equal(c:get("three"), nil);
+ assert_equal(c:get("four"), 4);
+ assert_equal(c:get("five"), nil);
+ assert_equal(c:get("six"), 6);
+ assert_equal(c:get("seven"), 7);
+ assert_equal(c:get("eight"), 8);
+ assert_equal(c:get("nine"), 9);
+
+ local keys = { "nine", "four", "eight", "seven", "six" };
+ local values = { 9, 4, 8, 7, 6 };
+ local i = 0;
+ for k, v in c:items() do
+ i = i + 1;
+ assert_equal(k, keys[i]);
+ assert_equal(v, values[i]);
+ end
+ assert_equal(i, 5);
+
+ c:set("four", "2+2");
+ assert_equal(c:count(), 5);
+
+ assert_equal(c:get("one"), nil);
+ assert_equal(c:get("two"), nil);
+ assert_equal(c:get("three"), nil);
+ assert_equal(c:get("four"), "2+2");
+ assert_equal(c:get("five"), nil);
+ assert_equal(c:get("six"), 6);
+ assert_equal(c:get("seven"), 7);
+ assert_equal(c:get("eight"), 8);
+ assert_equal(c:get("nine"), 9);
+
+ local keys = { "four", "nine", "eight", "seven", "six" };
+ local values = { "2+2", 9, 8, 7, 6 };
+ local i = 0;
+ for k, v in c:items() do
+ i = i + 1;
+ assert_equal(k, keys[i]);
+ assert_equal(v, values[i]);
+ end
+ assert_equal(i, 5);
+
+ c:set("foo", nil);
+ assert_equal(c:count(), 5);
+
+ assert_equal(c:get("one"), nil);
+ assert_equal(c:get("two"), nil);
+ assert_equal(c:get("three"), nil);
+ assert_equal(c:get("four"), "2+2");
+ assert_equal(c:get("five"), nil);
+ assert_equal(c:get("six"), 6);
+ assert_equal(c:get("seven"), 7);
+ assert_equal(c:get("eight"), 8);
+ assert_equal(c:get("nine"), 9);
+
+ local keys = { "four", "nine", "eight", "seven", "six" };
+ local values = { "2+2", 9, 8, 7, 6 };
+ local i = 0;
+ for k, v in c:items() do
+ i = i + 1;
+ assert_equal(k, keys[i]);
+ assert_equal(v, values[i]);
+ end
+ assert_equal(i, 5);
+
+ c:set("four", nil);
+
+ assert_equal(c:get("one"), nil);
+ assert_equal(c:get("two"), nil);
+ assert_equal(c:get("three"), nil);
+ assert_equal(c:get("four"), nil);
+ assert_equal(c:get("five"), nil);
+ assert_equal(c:get("six"), 6);
+ assert_equal(c:get("seven"), 7);
+ assert_equal(c:get("eight"), 8);
+ assert_equal(c:get("nine"), 9);
+
+ local keys = { "nine", "eight", "seven", "six" };
+ local values = { 9, 8, 7, 6 };
+ local i = 0;
+ for k, v in c:items() do
+ i = i + 1;
+ assert_equal(k, keys[i]);
+ assert_equal(v, values[i]);
+ end
+ assert_equal(i, 4);
+
+
+end