1 #include "clar_libgit2.h"
2 #include "sortedcache.h"
4 static int name_only_cmp(const void *a
, const void *b
)
9 void test_core_sortedcache__name_only(void)
15 cl_git_pass(git_sortedcache_new(
16 &sc
, 0, NULL
, NULL
, name_only_cmp
, NULL
));
18 cl_git_pass(git_sortedcache_wlock(sc
));
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"));
24 git_sortedcache_wunlock(sc
);
26 cl_assert_equal_sz(5, git_sortedcache_entrycount(sc
));
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
);
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
);
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
);
55 GIT_ENOTFOUND
, git_sortedcache_lookup_index(&pos
, sc
, "abc"));
57 cl_git_pass(git_sortedcache_clear(sc
, true));
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
);
64 git_sortedcache_free(sc
);
70 char path
[GIT_FLEX_ARRAY
];
71 } sortedcache_test_struct
;
73 static int sortedcache_test_struct_cmp(const void *a_
, const void *b_
)
75 const sortedcache_test_struct
*a
= a_
, *b
= b_
;
76 return strcmp(a
->path
, b
->path
);
79 static void sortedcache_test_struct_free(void *payload
, void *item_
)
81 sortedcache_test_struct
*item
= item_
;
84 item
->smaller_value
= 0;
87 void test_core_sortedcache__in_memory(void)
90 sortedcache_test_struct
*item
;
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
));
98 cl_git_pass(git_sortedcache_wlock(sc
));
99 cl_git_pass(git_sortedcache_upsert((void **)&item
, sc
, "aaa"));
101 item
->smaller_value
= 1;
102 cl_git_pass(git_sortedcache_upsert((void **)&item
, sc
, "bbb"));
104 item
->smaller_value
= 2;
105 cl_git_pass(git_sortedcache_upsert((void **)&item
, sc
, "zzz"));
107 item
->smaller_value
= 26;
108 cl_git_pass(git_sortedcache_upsert((void **)&item
, sc
, "mmm"));
110 item
->smaller_value
= 14;
111 cl_git_pass(git_sortedcache_upsert((void **)&item
, sc
, "iii"));
113 item
->smaller_value
= 9;
114 git_sortedcache_wunlock(sc
);
116 cl_assert_equal_sz(5, git_sortedcache_entrycount(sc
));
118 cl_git_pass(git_sortedcache_rlock(sc
));
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
);
132 * cl_git_pass(git_sortedcache_rlock(sc)); -- grab more than one
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
);
152 git_sortedcache_runlock(sc
);
153 /* git_sortedcache_runlock(sc); */
155 cl_assert_equal_i(0, free_count
);
157 cl_git_pass(git_sortedcache_clear(sc
, true));
159 cl_assert_equal_i(5, free_count
);
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
);
168 cl_git_pass(git_sortedcache_wlock(sc
));
169 cl_git_pass(git_sortedcache_upsert((void **)&item
, sc
, "testing"));
171 item
->smaller_value
= 3;
172 cl_git_pass(git_sortedcache_upsert((void **)&item
, sc
, "again"));
174 item
->smaller_value
= 1;
175 cl_git_pass(git_sortedcache_upsert((void **)&item
, sc
, "final"));
177 item
->smaller_value
= 2;
178 git_sortedcache_wunlock(sc
);
180 cl_assert_equal_sz(3, git_sortedcache_entrycount(sc
));
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
);
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
);
207 cl_git_pass(git_sortedcache_wlock(sc
));
209 cl_git_pass(git_sortedcache_lookup_index(&pos
, sc
, "again"));
210 cl_assert_equal_sz(0, pos
);
211 cl_git_pass(git_sortedcache_remove(sc
, pos
));
213 GIT_ENOTFOUND
, git_sortedcache_lookup_index(&pos
, sc
, "again"));
215 cl_assert_equal_sz(2, git_sortedcache_entrycount(sc
));
217 cl_git_pass(git_sortedcache_lookup_index(&pos
, sc
, "testing"));
218 cl_assert_equal_sz(1, pos
);
219 cl_git_pass(git_sortedcache_remove(sc
, pos
));
221 GIT_ENOTFOUND
, git_sortedcache_lookup_index(&pos
, sc
, "testing"));
223 cl_assert_equal_sz(1, git_sortedcache_entrycount(sc
));
225 cl_git_pass(git_sortedcache_lookup_index(&pos
, sc
, "final"));
226 cl_assert_equal_sz(0, pos
);
227 cl_git_pass(git_sortedcache_remove(sc
, pos
));
229 GIT_ENOTFOUND
, git_sortedcache_lookup_index(&pos
, sc
, "final"));
231 cl_assert_equal_sz(0, git_sortedcache_entrycount(sc
));
233 git_sortedcache_wunlock(sc
);
236 git_sortedcache_free(sc
);
238 cl_assert_equal_i(3, free_count
);
241 static void sortedcache_test_reload(git_sortedcache
*sc
)
244 git_buf buf
= GIT_BUF_INIT
;
246 sortedcache_test_struct
*item
;
248 cl_assert(git_sortedcache_lockandload(sc
, &buf
) > 0);
250 cl_git_pass(git_sortedcache_clear(sc
, false)); /* clear once we already have lock */
252 for (scan
= buf
.ptr
; *scan
; scan
= after
+ 1) {
253 int val
= strtol(scan
, &after
, 0);
254 cl_assert(after
> scan
);
257 for (scan
= after
; git__isspace(*scan
); ++scan
) /* find start */;
258 for (after
= scan
; *after
&& *after
!= '\n'; ++after
) /* find eol */;
261 cl_git_pass(git_sortedcache_upsert((void **)&item
, sc
, scan
));
264 item
->smaller_value
= (char)(count
++);
267 git_sortedcache_wunlock(sc
);
269 git_buf_dispose(&buf
);
272 void test_core_sortedcache__on_disk(void)
275 sortedcache_test_struct
*item
;
279 cl_git_mkfile("cacheitems.txt", "10 abc\n20 bcd\n30 cde\n");
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"));
286 /* should need to reload the first time */
288 sortedcache_test_reload(sc
);
290 /* test what we loaded */
292 cl_assert_equal_sz(3, git_sortedcache_entrycount(sc
));
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
);
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
);
310 /* should not need to reload this time */
312 cl_assert_equal_i(0, git_sortedcache_lockandload(sc
, NULL
));
314 /* rewrite ondisk file and reload */
316 cl_assert_equal_i(0, free_count
);
319 "cacheitems.txt", "100 abc\n200 zzz\n500 aaa\n10 final\n");
320 sortedcache_test_reload(sc
);
322 cl_assert_equal_i(3, free_count
);
324 /* test what we loaded */
326 cl_assert_equal_sz(4, git_sortedcache_entrycount(sc
));
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
);
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
);
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
);
355 GIT_ENOTFOUND
, git_sortedcache_lookup_index(&pos
, sc
, "missing"));
357 GIT_ENOTFOUND
, git_sortedcache_lookup_index(&pos
, sc
, "cde"));
359 git_sortedcache_free(sc
);
361 cl_assert_equal_i(7, free_count
);