| 1 | #include "unit-test.h" |
| 2 | #include "lib-reftable.h" |
| 3 | #include "reftable/blocksource.h" |
| 4 | #include "reftable/constants.h" |
| 5 | #include "reftable/iter.h" |
| 6 | #include "reftable/table.h" |
| 7 | #include "strbuf.h" |
| 8 | |
| 9 | void test_reftable_table__seek_once(void) |
| 10 | { |
| 11 | struct reftable_ref_record records[] = { |
| 12 | { |
| 13 | .refname = (char *) "refs/heads/main", |
| 14 | .value_type = REFTABLE_REF_VAL1, |
| 15 | .value.val1 = { 42 }, |
| 16 | }, |
| 17 | }; |
| 18 | struct reftable_block_source source = { 0 }; |
| 19 | struct reftable_ref_record ref = { 0 }; |
| 20 | struct reftable_iterator it = { 0 }; |
| 21 | struct reftable_table *table; |
| 22 | struct reftable_buf buf = REFTABLE_BUF_INIT; |
| 23 | int ret; |
| 24 | |
| 25 | cl_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records), NULL, 0, NULL); |
| 26 | block_source_from_buf(&source, &buf); |
| 27 | |
| 28 | ret = reftable_table_new(&table, &source, "name"); |
| 29 | cl_assert(!ret); |
| 30 | |
| 31 | reftable_table_init_ref_iterator(table, &it); |
| 32 | ret = reftable_iterator_seek_ref(&it, ""); |
| 33 | cl_assert(!ret); |
| 34 | ret = reftable_iterator_next_ref(&it, &ref); |
| 35 | cl_assert(!ret); |
| 36 | |
| 37 | ret = reftable_ref_record_equal(&ref, &records[0], |
| 38 | REFTABLE_HASH_SIZE_SHA1); |
| 39 | cl_assert_equal_i(ret, 1); |
| 40 | |
| 41 | ret = reftable_iterator_next_ref(&it, &ref); |
| 42 | cl_assert_equal_i(ret, 1); |
| 43 | |
| 44 | reftable_ref_record_release(&ref); |
| 45 | reftable_iterator_destroy(&it); |
| 46 | reftable_table_decref(table); |
| 47 | reftable_buf_release(&buf); |
| 48 | } |
| 49 | |
| 50 | void test_reftable_table__reseek(void) |
| 51 | { |
| 52 | struct reftable_ref_record records[] = { |
| 53 | { |
| 54 | .refname = (char *) "refs/heads/main", |
| 55 | .value_type = REFTABLE_REF_VAL1, |
| 56 | .value.val1 = { 42 }, |
| 57 | }, |
| 58 | }; |
| 59 | struct reftable_block_source source = { 0 }; |
| 60 | struct reftable_ref_record ref = { 0 }; |
| 61 | struct reftable_iterator it = { 0 }; |
| 62 | struct reftable_table *table; |
| 63 | struct reftable_buf buf = REFTABLE_BUF_INIT; |
| 64 | int ret; |
| 65 | |
| 66 | cl_reftable_write_to_buf(&buf, records, ARRAY_SIZE(records), |
| 67 | NULL, 0, NULL); |
| 68 | block_source_from_buf(&source, &buf); |
| 69 | |
| 70 | ret = reftable_table_new(&table, &source, "name"); |
| 71 | cl_assert(!ret); |
| 72 | |
| 73 | reftable_table_init_ref_iterator(table, &it); |
| 74 | |
| 75 | for (size_t i = 0; i < 5; i++) { |
| 76 | ret = reftable_iterator_seek_ref(&it, ""); |
| 77 | cl_assert(!ret); |
| 78 | ret = reftable_iterator_next_ref(&it, &ref); |
| 79 | cl_assert(!ret); |
| 80 | |
| 81 | ret = reftable_ref_record_equal(&ref, &records[0], REFTABLE_HASH_SIZE_SHA1); |
| 82 | cl_assert_equal_i(ret, 1); |
| 83 | |
| 84 | ret = reftable_iterator_next_ref(&it, &ref); |
| 85 | cl_assert_equal_i(ret, 1); |
| 86 | } |
| 87 | |
| 88 | reftable_ref_record_release(&ref); |
| 89 | reftable_iterator_destroy(&it); |
| 90 | reftable_table_decref(table); |
| 91 | reftable_buf_release(&buf); |
| 92 | } |
| 93 | |
| 94 | void test_reftable_table__block_iterator(void) |
| 95 | { |
| 96 | struct reftable_block_source source = { 0 }; |
| 97 | struct reftable_table_iterator it = { 0 }; |
| 98 | struct reftable_ref_record *records; |
| 99 | const struct reftable_block *block; |
| 100 | struct reftable_table *table; |
| 101 | struct reftable_buf buf = REFTABLE_BUF_INIT; |
| 102 | struct { |
| 103 | uint8_t block_type; |
| 104 | uint16_t header_off; |
| 105 | uint16_t restart_count; |
| 106 | uint16_t record_count; |
| 107 | } expected_blocks[] = { |
| 108 | { |
| 109 | .block_type = REFTABLE_BLOCK_TYPE_REF, |
| 110 | .header_off = 24, |
| 111 | .restart_count = 10, |
| 112 | .record_count = 158, |
| 113 | }, |
| 114 | { |
| 115 | .block_type = REFTABLE_BLOCK_TYPE_REF, |
| 116 | .restart_count = 10, |
| 117 | .record_count = 159, |
| 118 | }, |
| 119 | { |
| 120 | .block_type = REFTABLE_BLOCK_TYPE_REF, |
| 121 | .restart_count = 10, |
| 122 | .record_count = 159, |
| 123 | }, |
| 124 | { |
| 125 | .block_type = REFTABLE_BLOCK_TYPE_REF, |
| 126 | .restart_count = 2, |
| 127 | .record_count = 24, |
| 128 | }, |
| 129 | { |
| 130 | .block_type = REFTABLE_BLOCK_TYPE_INDEX, |
| 131 | .restart_count = 1, |
| 132 | .record_count = 4, |
| 133 | }, |
| 134 | { |
| 135 | .block_type = REFTABLE_BLOCK_TYPE_OBJ, |
| 136 | .restart_count = 1, |
| 137 | .record_count = 1, |
| 138 | }, |
| 139 | }; |
| 140 | const size_t nrecords = 500; |
| 141 | int ret; |
| 142 | |
| 143 | REFTABLE_CALLOC_ARRAY(records, nrecords); |
| 144 | for (size_t i = 0; i < nrecords; i++) { |
| 145 | records[i].value_type = REFTABLE_REF_VAL1; |
| 146 | records[i].refname = xstrfmt("refs/heads/branch-%03"PRIuMAX, |
| 147 | (uintmax_t) i); |
| 148 | } |
| 149 | |
| 150 | cl_reftable_write_to_buf(&buf, records, nrecords, NULL, 0, NULL); |
| 151 | block_source_from_buf(&source, &buf); |
| 152 | |
| 153 | ret = reftable_table_new(&table, &source, "name"); |
| 154 | cl_assert(!ret); |
| 155 | |
| 156 | ret = reftable_table_iterator_init(&it, table); |
| 157 | cl_assert(!ret); |
| 158 | |
| 159 | for (size_t i = 0; i < ARRAY_SIZE(expected_blocks); i++) { |
| 160 | struct reftable_iterator record_it = { 0 }; |
| 161 | struct reftable_record record = { |
| 162 | .type = expected_blocks[i].block_type, |
| 163 | }; |
| 164 | |
| 165 | ret = reftable_table_iterator_next(&it, &block); |
| 166 | cl_assert(!ret); |
| 167 | |
| 168 | cl_assert_equal_i(block->block_type, |
| 169 | expected_blocks[i].block_type); |
| 170 | cl_assert_equal_i(block->header_off, |
| 171 | expected_blocks[i].header_off); |
| 172 | cl_assert_equal_i(block->restart_count, |
| 173 | expected_blocks[i].restart_count); |
| 174 | |
| 175 | ret = reftable_block_init_iterator(block, &record_it); |
| 176 | cl_assert(!ret); |
| 177 | |
| 178 | for (size_t j = 0; ; j++) { |
| 179 | ret = iterator_next(&record_it, &record); |
| 180 | if (ret > 0) { |
| 181 | cl_assert_equal_i(j, |
| 182 | expected_blocks[i].record_count); |
| 183 | break; |
| 184 | } |
| 185 | cl_assert(!ret); |
| 186 | } |
| 187 | |
| 188 | reftable_iterator_destroy(&record_it); |
| 189 | reftable_record_release(&record); |
| 190 | } |
| 191 | |
| 192 | ret = reftable_table_iterator_next(&it, &block); |
| 193 | cl_assert_equal_i(ret, 1); |
| 194 | |
| 195 | for (size_t i = 0; i < nrecords; i++) |
| 196 | reftable_free(records[i].refname); |
| 197 | reftable_table_iterator_release(&it); |
| 198 | reftable_table_decref(table); |
| 199 | reftable_buf_release(&buf); |
| 200 | reftable_free(records); |
| 201 | } |