Annotate

tests/test_util_cache.lua @ 7456:76a8456cf1e2

Merge 0.10->trunk
author Kim Alvefur <zash@zash.se>
date Sun, 12 Jun 2016 20:06:30 +0200
parent 7291:688a7f5d3624
child 7484:d962ccf90735
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13
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());
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32
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());
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49
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());
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
71
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
74
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
82
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
85
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
94
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
97
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
106
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);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
109
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
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
120 local keys = { "nine", "four", "eight", "seven", "six" };
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
121 local values = { 9, 4, 8, 7, 6 };
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
122 local i = 0;
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
123 for k, v in c:items() do
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
124 i = i + 1;
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
125 assert_equal(k, keys[i]);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
126 assert_equal(v, values[i]);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
127 end
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
128 assert_equal(i, 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
129
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
130 c:set("four", "2+2");
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
131 assert_equal(c:count(), 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
132
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
133 assert_equal(c:get("one"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
134 assert_equal(c:get("two"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
135 assert_equal(c:get("three"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
136 assert_equal(c:get("four"), "2+2");
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
137 assert_equal(c:get("five"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
138 assert_equal(c:get("six"), 6);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
139 assert_equal(c:get("seven"), 7);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
140 assert_equal(c:get("eight"), 8);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
141 assert_equal(c:get("nine"), 9);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
142
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
143 local keys = { "four", "nine", "eight", "seven", "six" };
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
144 local values = { "2+2", 9, 8, 7, 6 };
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
145 local i = 0;
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
146 for k, v in c:items() do
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
147 i = i + 1;
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
148 assert_equal(k, keys[i]);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
149 assert_equal(v, values[i]);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
150 end
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
151 assert_equal(i, 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
152
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
153 c:set("foo", nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
154 assert_equal(c:count(), 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
155
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
156 assert_equal(c:get("one"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
157 assert_equal(c:get("two"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
158 assert_equal(c:get("three"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
159 assert_equal(c:get("four"), "2+2");
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
160 assert_equal(c:get("five"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
161 assert_equal(c:get("six"), 6);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
162 assert_equal(c:get("seven"), 7);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
163 assert_equal(c:get("eight"), 8);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
164 assert_equal(c:get("nine"), 9);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
165
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
166 local keys = { "four", "nine", "eight", "seven", "six" };
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
167 local values = { "2+2", 9, 8, 7, 6 };
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
168 local i = 0;
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
169 for k, v in c:items() do
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
170 i = i + 1;
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
171 assert_equal(k, keys[i]);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
172 assert_equal(v, values[i]);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
173 end
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
174 assert_equal(i, 5);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
175
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
176 c:set("four", nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
177
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
178 assert_equal(c:get("one"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
179 assert_equal(c:get("two"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
180 assert_equal(c:get("three"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
181 assert_equal(c:get("four"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
182 assert_equal(c:get("five"), nil);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
183 assert_equal(c:get("six"), 6);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
184 assert_equal(c:get("seven"), 7);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
185 assert_equal(c:get("eight"), 8);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
186 assert_equal(c:get("nine"), 9);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
187
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
188 local keys = { "nine", "eight", "seven", "six" };
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
189 local values = { 9, 8, 7, 6 };
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
190 local i = 0;
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
191 for k, v in c:items() do
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
192 i = i + 1;
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
193 assert_equal(k, keys[i]);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
194 assert_equal(v, values[i]);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
195 end
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
196 assert_equal(i, 4);
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
197
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
198 local evicted_key, evicted_value;
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
199 local c = new(3, function (_key, _value)
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
200 evicted_key, evicted_value = _key, _value;
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
201 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
202 local function set(k, v, should_evict_key, should_evict_value)
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
203 evicted_key, evicted_value = nil, nil;
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
204 c:set(k, v);
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
205 assert_equal(evicted_key, should_evict_key);
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
206 assert_equal(evicted_value, should_evict_value);
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
207 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
208 set("a", 1)
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
209 set("a", 1)
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
210 set("a", 1)
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
211 set("a", 1)
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
212 set("a", 1)
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
213
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
214 set("b", 2)
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
215 set("c", 3)
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
216 set("b", 2)
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
217 set("d", 4, "a", 1)
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
218 set("e", 5, "c", 3)
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
219
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
220
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
221 local evicted_key, evicted_value;
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
222 local c3 = new(1, function (_key, _value, c3)
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
223 evicted_key, evicted_value = _key, _value;
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
224 if _key == "a" then
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
225 -- Sanity check for what we're evicting
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
226 assert_equal(_key, "a");
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
227 assert_equal(_value, 1);
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
228 -- We're going to block eviction of this key/value, so set to nil...
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
229 evicted_key, evicted_value = nil, nil;
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
230 -- Returning false to block eviction
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
231 return false
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
232 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
233 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
234 local function set(k, v, should_evict_key, should_evict_value)
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
235 evicted_key, evicted_value = nil, nil;
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
236 local ret = c3:set(k, v);
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
237 assert_equal(evicted_key, should_evict_key);
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
238 assert_equal(evicted_value, 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
239 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
240 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
241 set("a", 1)
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
242 set("a", 1)
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
243 set("a", 1)
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
244 set("a", 1)
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
245 set("a", 1)
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
246
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
247 -- Our on_evict prevents "a" from being evicted, causing this to fail...
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
248 assert_equal(set("b", 2), false, "Failed to prevent eviction, or signal result");
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
249
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
250 expect_kv("a", 1, c3: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
251 expect_kv("a", 1, c3: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
252
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
253 -- Check the final state is what we expect
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
254 assert_equal(c3:get("a"), 1);
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
255 assert_equal(c3:get("b"), nil);
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
256 assert_equal(c3:count(), 1);
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
257
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
258
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
259 local c4 = new(3, 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
260
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
261 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
262 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
263 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
264 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
265 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
266 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
267 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
268 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
269 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
270
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
271 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
272 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
273
7291
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
274 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
275 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
276 return nil;
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
277 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
278 return true;
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
279 end
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
280 return false;
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
281 end);
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
282
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
283 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
284 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
285 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
286 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
287 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
288 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
289 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
290 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
291 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
292 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
293 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
294
688a7f5d3624 tests: util.cache: Tests for different return values of on_evict
Matthew Wild <mwild1@gmail.com>
parents: 7290
diff changeset
295 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
296 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
297
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
298 end