]>
Commit | Line | Data |
---|---|---|
3b34f636 HWN |
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 | { | |
3b34f636 | 30 | struct merged_iter_pqueue pq = { NULL }; |
bb2d6be4 PS |
31 | struct reftable_record recs[54]; |
32 | int N = ARRAY_SIZE(recs) - 1, i; | |
66c0daba | 33 | char *last = NULL; |
3b34f636 | 34 | |
3b34f636 | 35 | for (i = 0; i < N; i++) { |
bb2d6be4 PS |
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); | |
3b34f636 HWN |
41 | } |
42 | ||
43 | i = 1; | |
44 | do { | |
bb2d6be4 PS |
45 | struct pq_entry e = { |
46 | .rec = &recs[i], | |
47 | }; | |
48 | ||
c18eecbe | 49 | merged_iter_pqueue_add(&pq, &e); |
3b34f636 | 50 | merged_iter_pqueue_check(pq); |
bb2d6be4 | 51 | |
3b34f636 HWN |
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); | |
3b34f636 HWN |
57 | merged_iter_pqueue_check(pq); |
58 | ||
bb2d6be4 PS |
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; | |
3b34f636 HWN |
63 | } |
64 | ||
bb2d6be4 PS |
65 | for (i = 0; i < N; i++) |
66 | reftable_record_release(&recs[i]); | |
3b34f636 HWN |
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 | } |