]> git.ipfire.org Git - thirdparty/git.git/blob - reftable/pq_test.c
Merge branch 'ds/fetch-config-parse-microfix'
[thirdparty/git.git] / reftable / pq_test.c
1 /*
2 Copyright 2020 Google LLC
3
4 Use of this source code is governed by a BSD-style
5 license that can be found in the LICENSE file or at
6 https://developers.google.com/open-source/licenses/bsd
7 */
8
9 #include "system.h"
10
11 #include "basics.h"
12 #include "constants.h"
13 #include "pq.h"
14 #include "record.h"
15 #include "reftable-tests.h"
16 #include "test_framework.h"
17
18 void merged_iter_pqueue_check(struct merged_iter_pqueue pq)
19 {
20 int i;
21 for (i = 1; i < pq.len; i++) {
22 int parent = (i - 1) / 2;
23
24 EXPECT(pq_less(&pq.heap[parent], &pq.heap[i]));
25 }
26 }
27
28 static void test_pq(void)
29 {
30 struct merged_iter_pqueue pq = { NULL };
31 struct reftable_record recs[54];
32 int N = ARRAY_SIZE(recs) - 1, i;
33 char *last = NULL;
34
35 for (i = 0; i < N; i++) {
36 struct strbuf refname = STRBUF_INIT;
37 strbuf_addf(&refname, "%02d", i);
38
39 reftable_record_init(&recs[i], BLOCK_TYPE_REF);
40 recs[i].u.ref.refname = strbuf_detach(&refname, NULL);
41 }
42
43 i = 1;
44 do {
45 struct pq_entry e = {
46 .rec = &recs[i],
47 };
48
49 merged_iter_pqueue_add(&pq, &e);
50 merged_iter_pqueue_check(pq);
51
52 i = (i * 7) % N;
53 } while (i != 1);
54
55 while (!merged_iter_pqueue_is_empty(pq)) {
56 struct pq_entry e = merged_iter_pqueue_remove(&pq);
57 merged_iter_pqueue_check(pq);
58
59 EXPECT(reftable_record_type(e.rec) == BLOCK_TYPE_REF);
60 if (last)
61 EXPECT(strcmp(last, e.rec->u.ref.refname) < 0);
62 last = e.rec->u.ref.refname;
63 }
64
65 for (i = 0; i < N; i++)
66 reftable_record_release(&recs[i]);
67 merged_iter_pqueue_release(&pq);
68 }
69
70 int pq_test_main(int argc, const char *argv[])
71 {
72 RUN_TEST(test_pq);
73 return 0;
74 }