t/t9*: avoid redundant uses of cat
[git.git] / reftable / pq_test.c
blobb7d3c80cc7260298749800696e4a8bcc5be3284c
1 /*
2 Copyright 2020 Google LLC
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 */
9 #include "system.h"
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"
18 void merged_iter_pqueue_check(struct merged_iter_pqueue pq)
20 int i;
21 for (i = 1; i < pq.len; i++) {
22 int parent = (i - 1) / 2;
24 EXPECT(pq_less(&pq.heap[parent], &pq.heap[i]));
28 static void test_pq(void)
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;
35 for (i = 0; i < N; i++) {
36 struct strbuf refname = STRBUF_INIT;
37 strbuf_addf(&refname, "%02d", i);
39 reftable_record_init(&recs[i], BLOCK_TYPE_REF);
40 recs[i].u.ref.refname = strbuf_detach(&refname, NULL);
43 i = 1;
44 do {
45 struct pq_entry e = {
46 .rec = &recs[i],
49 merged_iter_pqueue_add(&pq, &e);
50 merged_iter_pqueue_check(pq);
52 i = (i * 7) % N;
53 } while (i != 1);
55 while (!merged_iter_pqueue_is_empty(pq)) {
56 struct pq_entry e = merged_iter_pqueue_remove(&pq);
57 merged_iter_pqueue_check(pq);
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;
65 for (i = 0; i < N; i++)
66 reftable_record_release(&recs[i]);
67 merged_iter_pqueue_release(&pq);
70 int pq_test_main(int argc, const char *argv[])
72 RUN_TEST(test_pq);
73 return 0;