]> git.ipfire.org Git - thirdparty/git.git/blob - reftable/pq_test.c
c9bb05e37b717eab1527f5a1ac6b39fbec687207
[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 char *names[54] = { NULL };
31 int N = ARRAY_SIZE(names) - 1;
32
33 struct merged_iter_pqueue pq = { NULL };
34 const char *last = NULL;
35
36 int i = 0;
37 for (i = 0; i < N; i++) {
38 char name[100];
39 snprintf(name, sizeof(name), "%02d", i);
40 names[i] = xstrdup(name);
41 }
42
43 i = 1;
44 do {
45 struct reftable_record rec =
46 reftable_new_record(BLOCK_TYPE_REF);
47 struct pq_entry e = { 0 };
48
49 reftable_record_as_ref(&rec)->refname = names[i];
50 e.rec = rec;
51 merged_iter_pqueue_add(&pq, e);
52 merged_iter_pqueue_check(pq);
53 i = (i * 7) % N;
54 } while (i != 1);
55
56 while (!merged_iter_pqueue_is_empty(pq)) {
57 struct pq_entry e = merged_iter_pqueue_remove(&pq);
58 struct reftable_ref_record *ref =
59 reftable_record_as_ref(&e.rec);
60
61 merged_iter_pqueue_check(pq);
62
63 if (last) {
64 EXPECT(strcmp(last, ref->refname) < 0);
65 }
66 last = ref->refname;
67 ref->refname = NULL;
68 reftable_free(ref);
69 }
70
71 for (i = 0; i < N; i++) {
72 reftable_free(names[i]);
73 }
74
75 merged_iter_pqueue_release(&pq);
76 }
77
78 int pq_test_main(int argc, const char *argv[])
79 {
80 RUN_TEST(test_pq);
81 return 0;
82 }