Annotate

tests/test_util_cache.lua @ 7289:42e7545d5ae3

util.cache: Add head() and tail() methods (and tests)
author Matthew Wild <mwild1@gmail.com>
date Thu, 17 Mar 2016 19:07:40 +0000
parent 7016:e0a0af42b09f
child 7290:70ab13e81cf5
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 -- Put it back in...
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 -- Check that the newest key/value was set before on_evict was called
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(c3:get("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
228 -- 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
229 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
230 assert_equal(_value, 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
231 -- Re-insert the evicted key (causes this evict function to run again with "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
232 c3:set(_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
233 assert_equal(c3:get(_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
234 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
235 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
236 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
237 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
238 c3: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
239 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
240 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
241 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
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 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
247
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
248 -- The evict handler re-inserts "a"->1, so "b" gets evicted:
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
249 set("b", 2, "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
250 -- 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
251 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
252 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
253 assert_equal(c3:count(), 1);
6946
31fb9eb9edce tests: Add tests for util.cache
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
254 end