]>
Commit | Line | Data |
---|---|---|
0b7cdc02 RB |
1 | #include "clar_libgit2.h" |
2 | #include "sortedcache.h" | |
3 | ||
4 | static int name_only_cmp(const void *a, const void *b) | |
5 | { | |
6 | return strcmp(a, b); | |
7 | } | |
8 | ||
9 | void test_core_sortedcache__name_only(void) | |
10 | { | |
11 | git_sortedcache *sc; | |
12 | void *item; | |
a4977169 | 13 | size_t pos; |
0b7cdc02 RB |
14 | |
15 | cl_git_pass(git_sortedcache_new( | |
16 | &sc, 0, NULL, NULL, name_only_cmp, NULL)); | |
17 | ||
8d9a85d4 | 18 | cl_git_pass(git_sortedcache_wlock(sc)); |
0b7cdc02 RB |
19 | cl_git_pass(git_sortedcache_upsert(&item, sc, "aaa")); |
20 | cl_git_pass(git_sortedcache_upsert(&item, sc, "bbb")); | |
21 | cl_git_pass(git_sortedcache_upsert(&item, sc, "zzz")); | |
22 | cl_git_pass(git_sortedcache_upsert(&item, sc, "mmm")); | |
23 | cl_git_pass(git_sortedcache_upsert(&item, sc, "iii")); | |
8d9a85d4 | 24 | git_sortedcache_wunlock(sc); |
0b7cdc02 RB |
25 | |
26 | cl_assert_equal_sz(5, git_sortedcache_entrycount(sc)); | |
27 | ||
28 | cl_assert((item = git_sortedcache_lookup(sc, "aaa")) != NULL); | |
29 | cl_assert_equal_s("aaa", item); | |
30 | cl_assert((item = git_sortedcache_lookup(sc, "mmm")) != NULL); | |
31 | cl_assert_equal_s("mmm", item); | |
32 | cl_assert((item = git_sortedcache_lookup(sc, "zzz")) != NULL); | |
33 | cl_assert_equal_s("zzz", item); | |
34 | cl_assert(git_sortedcache_lookup(sc, "qqq") == NULL); | |
35 | ||
36 | cl_assert((item = git_sortedcache_entry(sc, 0)) != NULL); | |
37 | cl_assert_equal_s("aaa", item); | |
38 | cl_assert((item = git_sortedcache_entry(sc, 1)) != NULL); | |
39 | cl_assert_equal_s("bbb", item); | |
40 | cl_assert((item = git_sortedcache_entry(sc, 2)) != NULL); | |
41 | cl_assert_equal_s("iii", item); | |
42 | cl_assert((item = git_sortedcache_entry(sc, 3)) != NULL); | |
43 | cl_assert_equal_s("mmm", item); | |
44 | cl_assert((item = git_sortedcache_entry(sc, 4)) != NULL); | |
45 | cl_assert_equal_s("zzz", item); | |
46 | cl_assert(git_sortedcache_entry(sc, 5) == NULL); | |
47 | ||
a4977169 RB |
48 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "aaa")); |
49 | cl_assert_equal_sz(0, pos); | |
50 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "iii")); | |
51 | cl_assert_equal_sz(2, pos); | |
52 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "zzz")); | |
53 | cl_assert_equal_sz(4, pos); | |
54 | cl_assert_equal_i( | |
55 | GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "abc")); | |
56 | ||
c25aa7cd | 57 | cl_git_pass(git_sortedcache_clear(sc, true)); |
0b7cdc02 RB |
58 | |
59 | cl_assert_equal_sz(0, git_sortedcache_entrycount(sc)); | |
60 | cl_assert(git_sortedcache_entry(sc, 0) == NULL); | |
61 | cl_assert(git_sortedcache_lookup(sc, "aaa") == NULL); | |
62 | cl_assert(git_sortedcache_entry(sc, 0) == NULL); | |
63 | ||
64 | git_sortedcache_free(sc); | |
65 | } | |
66 | ||
67 | typedef struct { | |
68 | int value; | |
69 | char smaller_value; | |
70 | char path[GIT_FLEX_ARRAY]; | |
71 | } sortedcache_test_struct; | |
72 | ||
73 | static int sortedcache_test_struct_cmp(const void *a_, const void *b_) | |
74 | { | |
75 | const sortedcache_test_struct *a = a_, *b = b_; | |
76 | return strcmp(a->path, b->path); | |
77 | } | |
78 | ||
79 | static void sortedcache_test_struct_free(void *payload, void *item_) | |
80 | { | |
81 | sortedcache_test_struct *item = item_; | |
82 | int *count = payload; | |
83 | (*count)++; | |
84 | item->smaller_value = 0; | |
85 | } | |
86 | ||
87 | void test_core_sortedcache__in_memory(void) | |
88 | { | |
89 | git_sortedcache *sc; | |
90 | sortedcache_test_struct *item; | |
91 | int free_count = 0; | |
92 | ||
93 | cl_git_pass(git_sortedcache_new( | |
94 | &sc, offsetof(sortedcache_test_struct, path), | |
95 | sortedcache_test_struct_free, &free_count, | |
96 | sortedcache_test_struct_cmp, NULL)); | |
97 | ||
8d9a85d4 | 98 | cl_git_pass(git_sortedcache_wlock(sc)); |
0b7cdc02 RB |
99 | cl_git_pass(git_sortedcache_upsert((void **)&item, sc, "aaa")); |
100 | item->value = 10; | |
101 | item->smaller_value = 1; | |
102 | cl_git_pass(git_sortedcache_upsert((void **)&item, sc, "bbb")); | |
103 | item->value = 20; | |
104 | item->smaller_value = 2; | |
105 | cl_git_pass(git_sortedcache_upsert((void **)&item, sc, "zzz")); | |
106 | item->value = 30; | |
107 | item->smaller_value = 26; | |
108 | cl_git_pass(git_sortedcache_upsert((void **)&item, sc, "mmm")); | |
109 | item->value = 40; | |
110 | item->smaller_value = 14; | |
111 | cl_git_pass(git_sortedcache_upsert((void **)&item, sc, "iii")); | |
112 | item->value = 50; | |
113 | item->smaller_value = 9; | |
8d9a85d4 | 114 | git_sortedcache_wunlock(sc); |
0b7cdc02 RB |
115 | |
116 | cl_assert_equal_sz(5, git_sortedcache_entrycount(sc)); | |
117 | ||
8d9a85d4 RB |
118 | cl_git_pass(git_sortedcache_rlock(sc)); |
119 | ||
0b7cdc02 RB |
120 | cl_assert((item = git_sortedcache_lookup(sc, "aaa")) != NULL); |
121 | cl_assert_equal_s("aaa", item->path); | |
122 | cl_assert_equal_i(10, item->value); | |
123 | cl_assert((item = git_sortedcache_lookup(sc, "mmm")) != NULL); | |
124 | cl_assert_equal_s("mmm", item->path); | |
125 | cl_assert_equal_i(40, item->value); | |
126 | cl_assert((item = git_sortedcache_lookup(sc, "zzz")) != NULL); | |
127 | cl_assert_equal_s("zzz", item->path); | |
128 | cl_assert_equal_i(30, item->value); | |
129 | cl_assert(git_sortedcache_lookup(sc, "abc") == NULL); | |
130 | ||
972bb689 RB |
131 | /* not on Windows: |
132 | * cl_git_pass(git_sortedcache_rlock(sc)); -- grab more than one | |
133 | */ | |
8d9a85d4 | 134 | |
0b7cdc02 RB |
135 | cl_assert((item = git_sortedcache_entry(sc, 0)) != NULL); |
136 | cl_assert_equal_s("aaa", item->path); | |
137 | cl_assert_equal_i(10, item->value); | |
138 | cl_assert((item = git_sortedcache_entry(sc, 1)) != NULL); | |
139 | cl_assert_equal_s("bbb", item->path); | |
140 | cl_assert_equal_i(20, item->value); | |
141 | cl_assert((item = git_sortedcache_entry(sc, 2)) != NULL); | |
142 | cl_assert_equal_s("iii", item->path); | |
143 | cl_assert_equal_i(50, item->value); | |
144 | cl_assert((item = git_sortedcache_entry(sc, 3)) != NULL); | |
145 | cl_assert_equal_s("mmm", item->path); | |
146 | cl_assert_equal_i(40, item->value); | |
147 | cl_assert((item = git_sortedcache_entry(sc, 4)) != NULL); | |
148 | cl_assert_equal_s("zzz", item->path); | |
149 | cl_assert_equal_i(30, item->value); | |
150 | cl_assert(git_sortedcache_entry(sc, 5) == NULL); | |
151 | ||
8d9a85d4 | 152 | git_sortedcache_runlock(sc); |
972bb689 | 153 | /* git_sortedcache_runlock(sc); */ |
8d9a85d4 | 154 | |
0b7cdc02 RB |
155 | cl_assert_equal_i(0, free_count); |
156 | ||
c25aa7cd | 157 | cl_git_pass(git_sortedcache_clear(sc, true)); |
0b7cdc02 RB |
158 | |
159 | cl_assert_equal_i(5, free_count); | |
160 | ||
161 | cl_assert_equal_sz(0, git_sortedcache_entrycount(sc)); | |
162 | cl_assert(git_sortedcache_entry(sc, 0) == NULL); | |
163 | cl_assert(git_sortedcache_lookup(sc, "aaa") == NULL); | |
164 | cl_assert(git_sortedcache_entry(sc, 0) == NULL); | |
165 | ||
166 | free_count = 0; | |
167 | ||
8d9a85d4 | 168 | cl_git_pass(git_sortedcache_wlock(sc)); |
0b7cdc02 RB |
169 | cl_git_pass(git_sortedcache_upsert((void **)&item, sc, "testing")); |
170 | item->value = 10; | |
171 | item->smaller_value = 3; | |
172 | cl_git_pass(git_sortedcache_upsert((void **)&item, sc, "again")); | |
173 | item->value = 20; | |
174 | item->smaller_value = 1; | |
175 | cl_git_pass(git_sortedcache_upsert((void **)&item, sc, "final")); | |
176 | item->value = 30; | |
177 | item->smaller_value = 2; | |
8d9a85d4 | 178 | git_sortedcache_wunlock(sc); |
0b7cdc02 RB |
179 | |
180 | cl_assert_equal_sz(3, git_sortedcache_entrycount(sc)); | |
181 | ||
182 | cl_assert((item = git_sortedcache_lookup(sc, "testing")) != NULL); | |
183 | cl_assert_equal_s("testing", item->path); | |
184 | cl_assert_equal_i(10, item->value); | |
185 | cl_assert((item = git_sortedcache_lookup(sc, "again")) != NULL); | |
186 | cl_assert_equal_s("again", item->path); | |
187 | cl_assert_equal_i(20, item->value); | |
188 | cl_assert((item = git_sortedcache_lookup(sc, "final")) != NULL); | |
189 | cl_assert_equal_s("final", item->path); | |
190 | cl_assert_equal_i(30, item->value); | |
191 | cl_assert(git_sortedcache_lookup(sc, "zzz") == NULL); | |
192 | ||
193 | cl_assert((item = git_sortedcache_entry(sc, 0)) != NULL); | |
194 | cl_assert_equal_s("again", item->path); | |
195 | cl_assert_equal_i(20, item->value); | |
196 | cl_assert((item = git_sortedcache_entry(sc, 1)) != NULL); | |
197 | cl_assert_equal_s("final", item->path); | |
198 | cl_assert_equal_i(30, item->value); | |
199 | cl_assert((item = git_sortedcache_entry(sc, 2)) != NULL); | |
200 | cl_assert_equal_s("testing", item->path); | |
201 | cl_assert_equal_i(10, item->value); | |
202 | cl_assert(git_sortedcache_entry(sc, 3) == NULL); | |
203 | ||
a4977169 RB |
204 | { |
205 | size_t pos; | |
206 | ||
8d9a85d4 RB |
207 | cl_git_pass(git_sortedcache_wlock(sc)); |
208 | ||
a4977169 RB |
209 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "again")); |
210 | cl_assert_equal_sz(0, pos); | |
8d9a85d4 | 211 | cl_git_pass(git_sortedcache_remove(sc, pos)); |
a4977169 RB |
212 | cl_assert_equal_i( |
213 | GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "again")); | |
214 | ||
215 | cl_assert_equal_sz(2, git_sortedcache_entrycount(sc)); | |
216 | ||
217 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "testing")); | |
218 | cl_assert_equal_sz(1, pos); | |
8d9a85d4 | 219 | cl_git_pass(git_sortedcache_remove(sc, pos)); |
a4977169 RB |
220 | cl_assert_equal_i( |
221 | GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "testing")); | |
222 | ||
223 | cl_assert_equal_sz(1, git_sortedcache_entrycount(sc)); | |
224 | ||
225 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "final")); | |
226 | cl_assert_equal_sz(0, pos); | |
8d9a85d4 | 227 | cl_git_pass(git_sortedcache_remove(sc, pos)); |
a4977169 RB |
228 | cl_assert_equal_i( |
229 | GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "final")); | |
230 | ||
231 | cl_assert_equal_sz(0, git_sortedcache_entrycount(sc)); | |
8d9a85d4 RB |
232 | |
233 | git_sortedcache_wunlock(sc); | |
a4977169 RB |
234 | } |
235 | ||
0b7cdc02 RB |
236 | git_sortedcache_free(sc); |
237 | ||
238 | cl_assert_equal_i(3, free_count); | |
239 | } | |
240 | ||
241 | static void sortedcache_test_reload(git_sortedcache *sc) | |
242 | { | |
243 | int count = 0; | |
e579e0f7 | 244 | git_str buf = GIT_STR_INIT; |
0b7cdc02 RB |
245 | char *scan, *after; |
246 | sortedcache_test_struct *item; | |
247 | ||
248 | cl_assert(git_sortedcache_lockandload(sc, &buf) > 0); | |
249 | ||
c25aa7cd | 250 | cl_git_pass(git_sortedcache_clear(sc, false)); /* clear once we already have lock */ |
0b7cdc02 RB |
251 | |
252 | for (scan = buf.ptr; *scan; scan = after + 1) { | |
253 | int val = strtol(scan, &after, 0); | |
254 | cl_assert(after > scan); | |
255 | scan = after; | |
256 | ||
257 | for (scan = after; git__isspace(*scan); ++scan) /* find start */; | |
258 | for (after = scan; *after && *after != '\n'; ++after) /* find eol */; | |
259 | *after = '\0'; | |
260 | ||
261 | cl_git_pass(git_sortedcache_upsert((void **)&item, sc, scan)); | |
262 | ||
263 | item->value = val; | |
264 | item->smaller_value = (char)(count++); | |
265 | } | |
266 | ||
8d9a85d4 | 267 | git_sortedcache_wunlock(sc); |
0b7cdc02 | 268 | |
e579e0f7 | 269 | git_str_dispose(&buf); |
0b7cdc02 RB |
270 | } |
271 | ||
272 | void test_core_sortedcache__on_disk(void) | |
273 | { | |
274 | git_sortedcache *sc; | |
275 | sortedcache_test_struct *item; | |
276 | int free_count = 0; | |
a4977169 | 277 | size_t pos; |
0b7cdc02 RB |
278 | |
279 | cl_git_mkfile("cacheitems.txt", "10 abc\n20 bcd\n30 cde\n"); | |
280 | ||
281 | cl_git_pass(git_sortedcache_new( | |
282 | &sc, offsetof(sortedcache_test_struct, path), | |
283 | sortedcache_test_struct_free, &free_count, | |
284 | sortedcache_test_struct_cmp, "cacheitems.txt")); | |
285 | ||
286 | /* should need to reload the first time */ | |
287 | ||
288 | sortedcache_test_reload(sc); | |
289 | ||
290 | /* test what we loaded */ | |
291 | ||
292 | cl_assert_equal_sz(3, git_sortedcache_entrycount(sc)); | |
293 | ||
294 | cl_assert((item = git_sortedcache_lookup(sc, "abc")) != NULL); | |
295 | cl_assert_equal_s("abc", item->path); | |
296 | cl_assert_equal_i(10, item->value); | |
297 | cl_assert((item = git_sortedcache_lookup(sc, "cde")) != NULL); | |
298 | cl_assert_equal_s("cde", item->path); | |
299 | cl_assert_equal_i(30, item->value); | |
300 | cl_assert(git_sortedcache_lookup(sc, "aaa") == NULL); | |
301 | ||
302 | cl_assert((item = git_sortedcache_entry(sc, 0)) != NULL); | |
303 | cl_assert_equal_s("abc", item->path); | |
304 | cl_assert_equal_i(10, item->value); | |
305 | cl_assert((item = git_sortedcache_entry(sc, 1)) != NULL); | |
306 | cl_assert_equal_s("bcd", item->path); | |
307 | cl_assert_equal_i(20, item->value); | |
308 | cl_assert(git_sortedcache_entry(sc, 3) == NULL); | |
309 | ||
310 | /* should not need to reload this time */ | |
311 | ||
312 | cl_assert_equal_i(0, git_sortedcache_lockandload(sc, NULL)); | |
313 | ||
314 | /* rewrite ondisk file and reload */ | |
315 | ||
316 | cl_assert_equal_i(0, free_count); | |
317 | ||
318 | cl_git_rewritefile( | |
319 | "cacheitems.txt", "100 abc\n200 zzz\n500 aaa\n10 final\n"); | |
320 | sortedcache_test_reload(sc); | |
321 | ||
322 | cl_assert_equal_i(3, free_count); | |
323 | ||
324 | /* test what we loaded */ | |
325 | ||
326 | cl_assert_equal_sz(4, git_sortedcache_entrycount(sc)); | |
327 | ||
328 | cl_assert((item = git_sortedcache_lookup(sc, "abc")) != NULL); | |
329 | cl_assert_equal_s("abc", item->path); | |
330 | cl_assert_equal_i(100, item->value); | |
331 | cl_assert((item = git_sortedcache_lookup(sc, "final")) != NULL); | |
332 | cl_assert_equal_s("final", item->path); | |
333 | cl_assert_equal_i(10, item->value); | |
334 | cl_assert(git_sortedcache_lookup(sc, "cde") == NULL); | |
335 | ||
336 | cl_assert((item = git_sortedcache_entry(sc, 0)) != NULL); | |
337 | cl_assert_equal_s("aaa", item->path); | |
338 | cl_assert_equal_i(500, item->value); | |
339 | cl_assert((item = git_sortedcache_entry(sc, 2)) != NULL); | |
340 | cl_assert_equal_s("final", item->path); | |
341 | cl_assert_equal_i(10, item->value); | |
342 | cl_assert((item = git_sortedcache_entry(sc, 3)) != NULL); | |
343 | cl_assert_equal_s("zzz", item->path); | |
344 | cl_assert_equal_i(200, item->value); | |
345 | ||
a4977169 RB |
346 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "aaa")); |
347 | cl_assert_equal_sz(0, pos); | |
348 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "abc")); | |
349 | cl_assert_equal_sz(1, pos); | |
350 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "final")); | |
351 | cl_assert_equal_sz(2, pos); | |
352 | cl_git_pass(git_sortedcache_lookup_index(&pos, sc, "zzz")); | |
353 | cl_assert_equal_sz(3, pos); | |
354 | cl_assert_equal_i( | |
355 | GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "missing")); | |
356 | cl_assert_equal_i( | |
357 | GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "cde")); | |
358 | ||
0b7cdc02 RB |
359 | git_sortedcache_free(sc); |
360 | ||
361 | cl_assert_equal_i(7, free_count); | |
362 | } | |
363 |