Software /
code /
prosody
Comparison
tests/test_util_cache.lua @ 6946:31fb9eb9edce
tests: Add tests for util.cache
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 26 Nov 2015 00:12:51 +0000 |
child | 7016:e0a0af42b09f |
comparison
equal
deleted
inserted
replaced
6945:d779c55058c6 | 6946:31fb9eb9edce |
---|---|
1 | |
2 function new(new) | |
3 local c = new(5); | |
4 | |
5 assert_equal(c:count(), 0); | |
6 | |
7 c:set("one", 1) | |
8 assert_equal(c:count(), 1); | |
9 c:set("two", 2) | |
10 c:set("three", 3) | |
11 c:set("four", 4) | |
12 c:set("five", 5); | |
13 assert_equal(c:count(), 5); | |
14 | |
15 c:set("foo", nil); | |
16 assert_equal(c:count(), 5); | |
17 | |
18 assert_equal(c:get("one"), 1); | |
19 assert_equal(c:get("two"), 2); | |
20 assert_equal(c:get("three"), 3); | |
21 assert_equal(c:get("four"), 4); | |
22 assert_equal(c:get("five"), 5); | |
23 | |
24 assert_equal(c:get("foo"), nil); | |
25 assert_equal(c:get("bar"), nil); | |
26 | |
27 c:set("six", 6); | |
28 assert_equal(c:count(), 5); | |
29 | |
30 assert_equal(c:get("one"), nil); | |
31 assert_equal(c:get("two"), 2); | |
32 assert_equal(c:get("three"), 3); | |
33 assert_equal(c:get("four"), 4); | |
34 assert_equal(c:get("five"), 5); | |
35 assert_equal(c:get("six"), 6); | |
36 | |
37 c:set("three", nil); | |
38 assert_equal(c:count(), 4); | |
39 | |
40 assert_equal(c:get("one"), nil); | |
41 assert_equal(c:get("two"), 2); | |
42 assert_equal(c:get("three"), nil); | |
43 assert_equal(c:get("four"), 4); | |
44 assert_equal(c:get("five"), 5); | |
45 assert_equal(c:get("six"), 6); | |
46 | |
47 c:set("seven", 7); | |
48 assert_equal(c:count(), 5); | |
49 | |
50 assert_equal(c:get("one"), nil); | |
51 assert_equal(c:get("two"), 2); | |
52 assert_equal(c:get("three"), nil); | |
53 assert_equal(c:get("four"), 4); | |
54 assert_equal(c:get("five"), 5); | |
55 assert_equal(c:get("six"), 6); | |
56 assert_equal(c:get("seven"), 7); | |
57 | |
58 c:set("eight", 8); | |
59 assert_equal(c:count(), 5); | |
60 | |
61 assert_equal(c:get("one"), nil); | |
62 assert_equal(c:get("two"), nil); | |
63 assert_equal(c:get("three"), nil); | |
64 assert_equal(c:get("four"), 4); | |
65 assert_equal(c:get("five"), 5); | |
66 assert_equal(c:get("six"), 6); | |
67 assert_equal(c:get("seven"), 7); | |
68 assert_equal(c:get("eight"), 8); | |
69 | |
70 c:set("four", 4); | |
71 assert_equal(c:count(), 5); | |
72 | |
73 assert_equal(c:get("one"), nil); | |
74 assert_equal(c:get("two"), nil); | |
75 assert_equal(c:get("three"), nil); | |
76 assert_equal(c:get("four"), 4); | |
77 assert_equal(c:get("five"), 5); | |
78 assert_equal(c:get("six"), 6); | |
79 assert_equal(c:get("seven"), 7); | |
80 assert_equal(c:get("eight"), 8); | |
81 | |
82 c:set("nine", 9); | |
83 assert_equal(c:count(), 5); | |
84 | |
85 assert_equal(c:get("one"), nil); | |
86 assert_equal(c:get("two"), nil); | |
87 assert_equal(c:get("three"), nil); | |
88 assert_equal(c:get("four"), 4); | |
89 assert_equal(c:get("five"), nil); | |
90 assert_equal(c:get("six"), 6); | |
91 assert_equal(c:get("seven"), 7); | |
92 assert_equal(c:get("eight"), 8); | |
93 assert_equal(c:get("nine"), 9); | |
94 | |
95 local keys = { "nine", "four", "eight", "seven", "six" }; | |
96 local values = { 9, 4, 8, 7, 6 }; | |
97 local i = 0; | |
98 for k, v in c:items() do | |
99 i = i + 1; | |
100 assert_equal(k, keys[i]); | |
101 assert_equal(v, values[i]); | |
102 end | |
103 assert_equal(i, 5); | |
104 | |
105 c:set("four", "2+2"); | |
106 assert_equal(c:count(), 5); | |
107 | |
108 assert_equal(c:get("one"), nil); | |
109 assert_equal(c:get("two"), nil); | |
110 assert_equal(c:get("three"), nil); | |
111 assert_equal(c:get("four"), "2+2"); | |
112 assert_equal(c:get("five"), nil); | |
113 assert_equal(c:get("six"), 6); | |
114 assert_equal(c:get("seven"), 7); | |
115 assert_equal(c:get("eight"), 8); | |
116 assert_equal(c:get("nine"), 9); | |
117 | |
118 local keys = { "four", "nine", "eight", "seven", "six" }; | |
119 local values = { "2+2", 9, 8, 7, 6 }; | |
120 local i = 0; | |
121 for k, v in c:items() do | |
122 i = i + 1; | |
123 assert_equal(k, keys[i]); | |
124 assert_equal(v, values[i]); | |
125 end | |
126 assert_equal(i, 5); | |
127 | |
128 c:set("foo", nil); | |
129 assert_equal(c:count(), 5); | |
130 | |
131 assert_equal(c:get("one"), nil); | |
132 assert_equal(c:get("two"), nil); | |
133 assert_equal(c:get("three"), nil); | |
134 assert_equal(c:get("four"), "2+2"); | |
135 assert_equal(c:get("five"), nil); | |
136 assert_equal(c:get("six"), 6); | |
137 assert_equal(c:get("seven"), 7); | |
138 assert_equal(c:get("eight"), 8); | |
139 assert_equal(c:get("nine"), 9); | |
140 | |
141 local keys = { "four", "nine", "eight", "seven", "six" }; | |
142 local values = { "2+2", 9, 8, 7, 6 }; | |
143 local i = 0; | |
144 for k, v in c:items() do | |
145 i = i + 1; | |
146 assert_equal(k, keys[i]); | |
147 assert_equal(v, values[i]); | |
148 end | |
149 assert_equal(i, 5); | |
150 | |
151 c:set("four", nil); | |
152 | |
153 assert_equal(c:get("one"), nil); | |
154 assert_equal(c:get("two"), nil); | |
155 assert_equal(c:get("three"), nil); | |
156 assert_equal(c:get("four"), nil); | |
157 assert_equal(c:get("five"), nil); | |
158 assert_equal(c:get("six"), 6); | |
159 assert_equal(c:get("seven"), 7); | |
160 assert_equal(c:get("eight"), 8); | |
161 assert_equal(c:get("nine"), 9); | |
162 | |
163 local keys = { "nine", "eight", "seven", "six" }; | |
164 local values = { 9, 8, 7, 6 }; | |
165 local i = 0; | |
166 for k, v in c:items() do | |
167 i = i + 1; | |
168 assert_equal(k, keys[i]); | |
169 assert_equal(v, values[i]); | |
170 end | |
171 assert_equal(i, 4); | |
172 | |
173 | |
174 end |