|
| 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 "test-lib.h" |
| 10 | +#include "reftable/constants.h" |
| 11 | +#include "reftable/pq.h" |
| 12 | + |
| 13 | +static void merged_iter_pqueue_check(const struct merged_iter_pqueue *pq) |
| 14 | +{ |
| 15 | + for (size_t i = 1; i < pq->len; i++) { |
| 16 | + size_t parent = (i - 1) / 2; |
| 17 | + check(pq_less(&pq->heap[parent], &pq->heap[i])); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +static int pq_entry_equal(struct pq_entry *a, struct pq_entry *b) |
| 22 | +{ |
| 23 | + return !reftable_record_cmp(a->rec, b->rec) && (a->index == b->index); |
| 24 | +} |
| 25 | + |
| 26 | +static void t_pq_record(void) |
| 27 | +{ |
| 28 | + struct merged_iter_pqueue pq = { 0 }; |
| 29 | + struct reftable_record recs[54]; |
| 30 | + size_t N = ARRAY_SIZE(recs) - 1, i; |
| 31 | + char *last = NULL; |
| 32 | + |
| 33 | + for (i = 0; i < N; i++) { |
| 34 | + reftable_record_init(&recs[i], BLOCK_TYPE_REF); |
| 35 | + recs[i].u.ref.refname = xstrfmt("%02"PRIuMAX, (uintmax_t)i); |
| 36 | + } |
| 37 | + |
| 38 | + i = 1; |
| 39 | + do { |
| 40 | + struct pq_entry e = { |
| 41 | + .rec = &recs[i], |
| 42 | + }; |
| 43 | + |
| 44 | + merged_iter_pqueue_add(&pq, &e); |
| 45 | + merged_iter_pqueue_check(&pq); |
| 46 | + i = (i * 7) % N; |
| 47 | + } while (i != 1); |
| 48 | + |
| 49 | + while (!merged_iter_pqueue_is_empty(pq)) { |
| 50 | + struct pq_entry top = merged_iter_pqueue_top(pq); |
| 51 | + struct pq_entry e = merged_iter_pqueue_remove(&pq); |
| 52 | + merged_iter_pqueue_check(&pq); |
| 53 | + |
| 54 | + check(pq_entry_equal(&top, &e)); |
| 55 | + check(reftable_record_type(e.rec) == BLOCK_TYPE_REF); |
| 56 | + if (last) |
| 57 | + check_int(strcmp(last, e.rec->u.ref.refname), <, 0); |
| 58 | + last = e.rec->u.ref.refname; |
| 59 | + } |
| 60 | + |
| 61 | + for (i = 0; i < N; i++) |
| 62 | + reftable_record_release(&recs[i]); |
| 63 | + merged_iter_pqueue_release(&pq); |
| 64 | +} |
| 65 | + |
| 66 | +static void t_pq_index(void) |
| 67 | +{ |
| 68 | + struct merged_iter_pqueue pq = { 0 }; |
| 69 | + struct reftable_record recs[13]; |
| 70 | + char *last = NULL; |
| 71 | + size_t N = ARRAY_SIZE(recs), i; |
| 72 | + |
| 73 | + for (i = 0; i < N; i++) { |
| 74 | + reftable_record_init(&recs[i], BLOCK_TYPE_REF); |
| 75 | + recs[i].u.ref.refname = (char *) "refs/heads/master"; |
| 76 | + } |
| 77 | + |
| 78 | + i = 1; |
| 79 | + do { |
| 80 | + struct pq_entry e = { |
| 81 | + .rec = &recs[i], |
| 82 | + .index = i, |
| 83 | + }; |
| 84 | + |
| 85 | + merged_iter_pqueue_add(&pq, &e); |
| 86 | + merged_iter_pqueue_check(&pq); |
| 87 | + i = (i * 7) % N; |
| 88 | + } while (i != 1); |
| 89 | + |
| 90 | + for (i = N - 1; i > 0; i--) { |
| 91 | + struct pq_entry top = merged_iter_pqueue_top(pq); |
| 92 | + struct pq_entry e = merged_iter_pqueue_remove(&pq); |
| 93 | + merged_iter_pqueue_check(&pq); |
| 94 | + |
| 95 | + check(pq_entry_equal(&top, &e)); |
| 96 | + check(reftable_record_type(e.rec) == BLOCK_TYPE_REF); |
| 97 | + check_int(e.index, ==, i); |
| 98 | + if (last) |
| 99 | + check_str(last, e.rec->u.ref.refname); |
| 100 | + last = e.rec->u.ref.refname; |
| 101 | + } |
| 102 | + |
| 103 | + merged_iter_pqueue_release(&pq); |
| 104 | +} |
| 105 | + |
| 106 | +static void t_merged_iter_pqueue_top(void) |
| 107 | +{ |
| 108 | + struct merged_iter_pqueue pq = { 0 }; |
| 109 | + struct reftable_record recs[13]; |
| 110 | + size_t N = ARRAY_SIZE(recs), i; |
| 111 | + |
| 112 | + for (i = 0; i < N; i++) { |
| 113 | + reftable_record_init(&recs[i], BLOCK_TYPE_REF); |
| 114 | + recs[i].u.ref.refname = (char *) "refs/heads/master"; |
| 115 | + } |
| 116 | + |
| 117 | + i = 1; |
| 118 | + do { |
| 119 | + struct pq_entry e = { |
| 120 | + .rec = &recs[i], |
| 121 | + .index = i, |
| 122 | + }; |
| 123 | + |
| 124 | + merged_iter_pqueue_add(&pq, &e); |
| 125 | + merged_iter_pqueue_check(&pq); |
| 126 | + i = (i * 7) % N; |
| 127 | + } while (i != 1); |
| 128 | + |
| 129 | + for (i = N - 1; i > 0; i--) { |
| 130 | + struct pq_entry top = merged_iter_pqueue_top(pq); |
| 131 | + struct pq_entry e = merged_iter_pqueue_remove(&pq); |
| 132 | + |
| 133 | + merged_iter_pqueue_check(&pq); |
| 134 | + check(pq_entry_equal(&top, &e)); |
| 135 | + check(reftable_record_equal(top.rec, &recs[i], GIT_SHA1_RAWSZ)); |
| 136 | + for (size_t j = 0; i < pq.len; j++) { |
| 137 | + check(pq_less(&top, &pq.heap[j])); |
| 138 | + check_int(top.index, >, j); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + merged_iter_pqueue_release(&pq); |
| 143 | +} |
| 144 | + |
| 145 | +int cmd_main(int argc, const char *argv[]) |
| 146 | +{ |
| 147 | + TEST(t_pq_record(), "pq works with record-based comparison"); |
| 148 | + TEST(t_pq_index(), "pq works with index-based comparison"); |
| 149 | + TEST(t_merged_iter_pqueue_top(), "merged_iter_pqueue_top works"); |
| 150 | + |
| 151 | + return test_done(); |
| 152 | +} |
0 commit comments