Annotate

tests/test_util_cache.lua @ 8122:e56a90168890

prosodyctl: Add cert import to short help
author Kim Alvefur <zash@zash.se>
date Mon, 24 Apr 2017 15:53:26 +0200
parent 7961:ff556d010225
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 function new(new)
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local c = new(5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
7289
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
4 local function expect_kv(key, value, actual_key, actual_value)
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
5 assert_equal(key, actual_key, "key incorrect");
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
6 assert_equal(value, actual_value, "value incorrect");
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
7 end
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
8
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
9 expect_kv(nil, nil, c:head());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
10 expect_kv(nil, nil, c:tail());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
11
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 assert_equal(c:count(), 0);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
13
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 c:set("one", 1)
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 assert_equal(c:count(), 1);
7289
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
16 expect_kv("one", 1, c:head());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
17 expect_kv("one", 1, c:tail());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
18
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 c:set("two", 2)
7289
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
20 expect_kv("two", 2, c:head());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
21 expect_kv("one", 1, c:tail());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
22
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 c:set("three", 3)
7289
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
24 expect_kv("three", 3, c:head());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
25 expect_kv("one", 1, c:tail());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
26
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 c:set("four", 4)
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 c:set("five", 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 assert_equal(c:count(), 5);
7289
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
30 expect_kv("five", 5, c:head());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
31 expect_kv("one", 1, c:tail());
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
32
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 c:set("foo", nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 assert_equal(c:count(), 5);
7289
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
35 expect_kv("five", 5, c:head());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
36 expect_kv("one", 1, c:tail());
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
37
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 assert_equal(c:get("one"), 1);
7289
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
39 expect_kv("five", 5, c:head());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
40 expect_kv("one", 1, c:tail());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
41
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 assert_equal(c:get("two"), 2);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 assert_equal(c:get("three"), 3);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 assert_equal(c:get("four"), 4);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 assert_equal(c:get("five"), 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 assert_equal(c:get("foo"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 assert_equal(c:get("bar"), nil);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
49
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 c:set("six", 6);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 assert_equal(c:count(), 5);
7289
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
52 expect_kv("six", 6, c:head());
42e7545d5ae3 util.cache: Add head() and tail() methods (and tests)
Matthew Wild <mwild1@gmail.com>
parents: 7016
diff changeset
53 expect_kv("two", 2, c:tail());
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
54
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 assert_equal(c:get("one"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 assert_equal(c:get("two"), 2);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 assert_equal(c:get("three"), 3);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 assert_equal(c:get("four"), 4);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 assert_equal(c:get("five"), 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 assert_equal(c:get("six"), 6);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
61
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 c:set("three", nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 assert_equal(c:count(), 4);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
64
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 assert_equal(c:get("one"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 assert_equal(c:get("two"), 2);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
67 assert_equal(c:get("three"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
68 assert_equal(c:get("four"), 4);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 assert_equal(c:get("five"), 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70 assert_equal(c:get("six"), 6);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
71
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
72 c:set("seven", 7);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
73 assert_equal(c:count(), 5);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
74
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
75 assert_equal(c:get("one"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
76 assert_equal(c:get("two"), 2);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
77 assert_equal(c:get("three"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
78 assert_equal(c:get("four"), 4);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
79 assert_equal(c:get("five"), 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
80 assert_equal(c:get("six"), 6);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
81 assert_equal(c:get("seven"), 7);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
82
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
83 c:set("eight", 8);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
84 assert_equal(c:count(), 5);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
85
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
86 assert_equal(c:get("one"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
87 assert_equal(c:get("two"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 assert_equal(c:get("three"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
89 assert_equal(c:get("four"), 4);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 assert_equal(c:get("five"), 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
91 assert_equal(c:get("six"), 6);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
92 assert_equal(c:get("seven"), 7);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
93 assert_equal(c:get("eight"), 8);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
94
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
95 c:set("four", 4);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
96 assert_equal(c:count(), 5);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
97
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
98 assert_equal(c:get("one"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
99 assert_equal(c:get("two"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
100 assert_equal(c:get("three"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
101 assert_equal(c:get("four"), 4);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
102 assert_equal(c:get("five"), 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
103 assert_equal(c:get("six"), 6);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
104 assert_equal(c:get("seven"), 7);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
105 assert_equal(c:get("eight"), 8);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
106
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
107 c:set("nine", 9);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
108 assert_equal(c:count(), 5);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
109
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
110 assert_equal(c:get("one"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
111 assert_equal(c:get("two"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
112 assert_equal(c:get("three"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
113 assert_equal(c:get("four"), 4);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
114 assert_equal(c:get("five"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
115 assert_equal(c:get("six"), 6);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
116 assert_equal(c:get("seven"), 7);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
117 assert_equal(c:get("eight"), 8);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
118 assert_equal(c:get("nine"), 9);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
119
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
120 do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
121 local keys = { "nine", "four", "eight", "seven", "six" };
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
122 local values = { 9, 4, 8, 7, 6 };
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
123 local i = 0;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
124 for k, v in c:items() do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
125 i = i + 1;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
126 assert_equal(k, keys[i]);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
127 assert_equal(v, values[i]);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
128 end
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
129 assert_equal(i, 5);
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
131 c:set("four", "2+2");
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
132 assert_equal(c:count(), 5);
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
134 assert_equal(c:get("one"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
135 assert_equal(c:get("two"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
136 assert_equal(c:get("three"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
137 assert_equal(c:get("four"), "2+2");
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
138 assert_equal(c:get("five"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
139 assert_equal(c:get("six"), 6);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
140 assert_equal(c:get("seven"), 7);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
141 assert_equal(c:get("eight"), 8);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
142 assert_equal(c:get("nine"), 9);
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 end
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
145 do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
146 local keys = { "four", "nine", "eight", "seven", "six" };
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
147 local values = { "2+2", 9, 8, 7, 6 };
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
148 local i = 0;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
149 for k, v in c:items() do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
150 i = i + 1;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
151 assert_equal(k, keys[i]);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
152 assert_equal(v, values[i]);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
153 end
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
154 assert_equal(i, 5);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
155
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
156 c:set("foo", nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
157 assert_equal(c:count(), 5);
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
159 assert_equal(c:get("one"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
160 assert_equal(c:get("two"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
161 assert_equal(c:get("three"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
162 assert_equal(c:get("four"), "2+2");
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
163 assert_equal(c:get("five"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
164 assert_equal(c:get("six"), 6);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
165 assert_equal(c:get("seven"), 7);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
166 assert_equal(c:get("eight"), 8);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
167 assert_equal(c:get("nine"), 9);
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 end
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
169
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
170 do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
171 local keys = { "four", "nine", "eight", "seven", "six" };
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
172 local values = { "2+2", 9, 8, 7, 6 };
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
173 local i = 0;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
174 for k, v in c:items() do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
175 i = i + 1;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
176 assert_equal(k, keys[i]);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
177 assert_equal(v, values[i]);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
178 end
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
179 assert_equal(i, 5);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
180
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
181 c:set("four", nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
182
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
183 assert_equal(c:get("one"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
184 assert_equal(c:get("two"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
185 assert_equal(c:get("three"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
186 assert_equal(c:get("four"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
187 assert_equal(c:get("five"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
188 assert_equal(c:get("six"), 6);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
189 assert_equal(c:get("seven"), 7);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
190 assert_equal(c:get("eight"), 8);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
191 assert_equal(c:get("nine"), 9);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
192 end
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
194 do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
195 local keys = { "nine", "eight", "seven", "six" };
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
196 local values = { 9, 8, 7, 6 };
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
197 local i = 0;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
198 for k, v in c:items() do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
199 i = i + 1;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
200 assert_equal(k, keys[i]);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
201 assert_equal(v, values[i]);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
202 end
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
203 assert_equal(i, 4);
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
204 end
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
205
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
206 do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
207 local evicted_key, evicted_value;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
208 local c2 = new(3, function (_key, _value)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
209 evicted_key, evicted_value = _key, _value;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
210 end);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
211 local function set(k, v, should_evict_key, should_evict_value)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
212 evicted_key, evicted_value = nil, nil;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
213 c2:set(k, v);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
214 assert_equal(evicted_key, should_evict_key);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
215 assert_equal(evicted_value, should_evict_value);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
216 end
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
217 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
218 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
219 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
220 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
221 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
222
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
223 set("b", 2)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
224 set("c", 3)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
225 set("b", 2)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
226 set("d", 4, "a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
227 set("e", 5, "c", 3)
7016
e0a0af42b09f util.cache (and tests): Call on_evict after insertion of the new key, so inside on_evict we can be more certain about the current state of the cache (i.e. full, new item added, old item removed)
Matthew Wild <mwild1@gmail.com>
parents: 6946
diff changeset
228 end
e0a0af42b09f util.cache (and tests): Call on_evict after insertion of the new key, so inside on_evict we can be more certain about the current state of the cache (i.e. full, new item added, old item removed)
Matthew Wild <mwild1@gmail.com>
parents: 6946
diff changeset
229
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
230 do
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
231 local evicted_key, evicted_value;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
232 local c3 = new(1, function (_key, _value)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
233 evicted_key, evicted_value = _key, _value;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
234 if _key == "a" then
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
235 -- Sanity check for what we're evicting
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
236 assert_equal(_key, "a");
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
237 assert_equal(_value, 1);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
238 -- We're going to block eviction of this key/value, so set to nil...
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
239 evicted_key, evicted_value = nil, nil;
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
240 -- Returning false to block eviction
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
241 return false
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
242 end
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
243 end);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
244 local function set(k, v, should_evict_key, should_evict_value)
7290
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
245 evicted_key, evicted_value = nil, nil;
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
246 local ret = c3:set(k, v);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
247 assert_equal(evicted_key, should_evict_key);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
248 assert_equal(evicted_value, should_evict_value);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
249 return ret;
7016
e0a0af42b09f util.cache (and tests): Call on_evict after insertion of the new key, so inside on_evict we can be more certain about the current state of the cache (i.e. full, new item added, old item removed)
Matthew Wild <mwild1@gmail.com>
parents: 6946
diff changeset
250 end
7504
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
251 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
252 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
253 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
254 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
255 set("a", 1)
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
256
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
257 -- Our on_evict prevents "a" from being evicted, causing this to fail...
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
258 assert_equal(set("b", 2), false, "Failed to prevent eviction, or signal result");
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
259
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
260 expect_kv("a", 1, c3:head());
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
261 expect_kv("a", 1, c3:tail());
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
262
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
263 -- Check the final state is what we expect
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
264 assert_equal(c3:get("a"), 1);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
265 assert_equal(c3:get("b"), nil);
b43cbbbb806f test_util_cache: wrap individual test blocks in do-end [luacheck]
Anton Shestakov <av6@dwimlabs.net>
parents: 7485
diff changeset
266 assert_equal(c3:count(), 1);
7016
e0a0af42b09f util.cache (and tests): Call on_evict after insertion of the new key, so inside on_evict we can be more certain about the current state of the cache (i.e. full, new item added, old item removed)
Matthew Wild <mwild1@gmail.com>
parents: 6946
diff changeset
267 end
7290
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
268
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
269
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
270 local c4 = new(3, false);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
271
7290
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
272 assert_equal(c4:set("a", 1), true);
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
273 assert_equal(c4:set("a", 1), true);
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
274 assert_equal(c4:set("a", 1), true);
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
275 assert_equal(c4:set("a", 1), true);
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
276 assert_equal(c4:set("b", 2), true);
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
277 assert_equal(c4:set("c", 3), true);
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
278 assert_equal(c4:set("d", 4), false);
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
279 assert_equal(c4:set("d", 4), false);
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
280 assert_equal(c4:set("d", 4), false);
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
281
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
282 expect_kv("c", 3, c4:head());
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
283 expect_kv("a", 1, c4:tail());
70ab13e81cf5 util.cache: Change behaviour of on_evict (and tests). Now accepts false instead of a function (never evict), or on_evict can return false to prevent eviction.
Matthew Wild <mwild1@gmail.com>
parents: 7289
diff changeset
284
7291
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
285 local c5 = new(3, function (k, v)
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
286 if k == "a" then
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
287 return nil;
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
288 elseif k == "b" then
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
289 return true;
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
290 end
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
291 return false;
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
292 end);
7961
ff556d010225 tests: Trim trailing whitespace
Kim Alvefur <zash@zash.se>
parents: 7504
diff changeset
293
7291
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
294 assert_equal(c5:set("a", 1), true);
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
295 assert_equal(c5:set("a", 1), true);
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
296 assert_equal(c5:set("a", 1), true);
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
297 assert_equal(c5:set("a", 1), true);
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
298 assert_equal(c5:set("b", 2), true);
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
299 assert_equal(c5:set("c", 3), true);
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
300 assert_equal(c5:set("d", 4), true); -- "a" evicted (cb returned nil)
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
301 assert_equal(c5:set("d", 4), true); -- nop
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
302 assert_equal(c5:set("d", 4), true); -- nop
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
303 assert_equal(c5:set("e", 5), true); -- "b" evicted (cb returned true)
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
304 assert_equal(c5:set("f", 6), false); -- "c" won't evict (cb returned false)
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
305
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
306 expect_kv("e", 5, c5:head());
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
307 expect_kv("c", 3, c5:tail());
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
308
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
309 end