|
| 1 | +#include "test-lib.h" |
| 2 | +#include "oidset.h" |
| 3 | +#include "lib-oid.h" |
| 4 | +#include "hex.h" |
| 5 | +#include "strbuf.h" |
| 6 | + |
| 7 | +static const char *const hex_input[] = { "00", "11", "22", "33", "aa", "cc" }; |
| 8 | + |
| 9 | +static void strbuf_test_data_path(struct strbuf *buf, int hash_algo) |
| 10 | +{ |
| 11 | + strbuf_getcwd(buf); |
| 12 | + strbuf_strip_suffix(buf, "/unit-tests/bin"); |
| 13 | + strbuf_addf(buf, "/unit-tests/t-oidset/%s", |
| 14 | + hash_algo == GIT_HASH_SHA1 ? "sha1-oids" : "sha256-oids"); |
| 15 | +} |
| 16 | + |
| 17 | +static void setup(void (*f)(struct oidset *st)) |
| 18 | +{ |
| 19 | + struct oidset st = OIDSET_INIT; |
| 20 | + struct object_id oid; |
| 21 | + int ret = 0; |
| 22 | + |
| 23 | + if (!check_int(oidset_size(&st), ==, 0)) { |
| 24 | + test_skip_all("OIDSET_INIT is broken"); |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + for (size_t i = 0; i < ARRAY_SIZE(hex_input); i++) { |
| 29 | + if ((ret = get_oid_arbitrary_hex(hex_input[i], &oid))) |
| 30 | + break; |
| 31 | + if (!check_int((ret = oidset_insert(&st, &oid)), ==, 0)) |
| 32 | + break; |
| 33 | + } |
| 34 | + |
| 35 | + if (!ret && check_int(oidset_size(&st), ==, ARRAY_SIZE(hex_input))) |
| 36 | + f(&st); |
| 37 | + |
| 38 | + oidset_clear(&st); |
| 39 | +} |
| 40 | + |
| 41 | +static void t_contains(struct oidset *st) |
| 42 | +{ |
| 43 | + struct object_id oid; |
| 44 | + |
| 45 | + for (size_t i = 0; i < ARRAY_SIZE(hex_input); i++) { |
| 46 | + if (!get_oid_arbitrary_hex(hex_input[i], &oid)) { |
| 47 | + if (!check_int(oidset_contains(st, &oid), ==, 1)) |
| 48 | + test_msg("oid: %s", oid_to_hex(&oid)); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + if (!get_oid_arbitrary_hex("55", &oid)) |
| 53 | + check_int(oidset_contains(st, &oid), ==, 0); |
| 54 | +} |
| 55 | + |
| 56 | +static void t_insert_dup(struct oidset *st) |
| 57 | +{ |
| 58 | + struct object_id oid; |
| 59 | + |
| 60 | + if (!get_oid_arbitrary_hex("11", &oid)) |
| 61 | + check_int(oidset_insert(st, &oid), ==, 1); |
| 62 | + |
| 63 | + if (!get_oid_arbitrary_hex("aa", &oid)) |
| 64 | + check_int(oidset_insert(st, &oid), ==, 1); |
| 65 | + |
| 66 | + check_int(oidset_size(st), ==, ARRAY_SIZE(hex_input)); |
| 67 | +} |
| 68 | + |
| 69 | +static void t_insert_from_set(struct oidset *st_src) |
| 70 | +{ |
| 71 | + struct oidset st_dest = OIDSET_INIT; |
| 72 | + struct oidset_iter iter_src, iter_dest; |
| 73 | + struct object_id *oid_src, *oid_dest; |
| 74 | + struct object_id oid; |
| 75 | + size_t count = 0; |
| 76 | + |
| 77 | + oidset_insert_from_set(&st_dest, st_src); |
| 78 | + check_int(oidset_size(st_src), ==, ARRAY_SIZE(hex_input)); |
| 79 | + check_int(oidset_size(&st_dest), ==, oidset_size(st_src)); |
| 80 | + |
| 81 | + oidset_iter_init(st_src, &iter_src); |
| 82 | + oidset_iter_init(&st_dest, &iter_dest); |
| 83 | + |
| 84 | + /* check that oidset_insert_from_set() makes a copy of the object_ids */ |
| 85 | + while ((oid_src = oidset_iter_next(&iter_src)) && |
| 86 | + (oid_dest = oidset_iter_next(&iter_dest))) { |
| 87 | + check(oid_src != oid_dest); |
| 88 | + count++; |
| 89 | + } |
| 90 | + check_int(count, ==, ARRAY_SIZE(hex_input)); |
| 91 | + |
| 92 | + for (size_t i = 0; i < ARRAY_SIZE(hex_input); i++) { |
| 93 | + if (!get_oid_arbitrary_hex(hex_input[i], &oid)) { |
| 94 | + if (!check_int(oidset_contains(&st_dest, &oid), ==, 1)) |
| 95 | + test_msg("oid: %s", oid_to_hex(&oid)); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + if (!get_oid_arbitrary_hex("55", &oid)) |
| 100 | + check_int(oidset_contains(&st_dest, &oid), ==, 0); |
| 101 | + oidset_clear(&st_dest); |
| 102 | +} |
| 103 | + |
| 104 | +static void t_remove(struct oidset *st) |
| 105 | +{ |
| 106 | + struct object_id oid; |
| 107 | + |
| 108 | + if (!get_oid_arbitrary_hex("55", &oid)) { |
| 109 | + check_int(oidset_remove(st, &oid), ==, 0); |
| 110 | + check_int(oidset_size(st), ==, ARRAY_SIZE(hex_input)); |
| 111 | + } |
| 112 | + |
| 113 | + if (!get_oid_arbitrary_hex("22", &oid)) { |
| 114 | + check_int(oidset_remove(st, &oid), ==, 1); |
| 115 | + check_int(oidset_size(st), ==, ARRAY_SIZE(hex_input) - 1); |
| 116 | + check_int(oidset_contains(st, &oid), ==, 0); |
| 117 | + } |
| 118 | + |
| 119 | + if (!get_oid_arbitrary_hex("cc", &oid)) { |
| 120 | + check_int(oidset_remove(st, &oid), ==, 1); |
| 121 | + check_int(oidset_size(st), ==, ARRAY_SIZE(hex_input) - 2); |
| 122 | + check_int(oidset_contains(st, &oid), ==, 0); |
| 123 | + } |
| 124 | + |
| 125 | + if (!get_oid_arbitrary_hex("00", &oid)) |
| 126 | + { |
| 127 | + /* remove a value inserted more than once */ |
| 128 | + check_int(oidset_insert(st, &oid), ==, 1); |
| 129 | + check_int(oidset_remove(st, &oid), ==, 1); |
| 130 | + check_int(oidset_size(st), ==, ARRAY_SIZE(hex_input) - 3); |
| 131 | + check_int(oidset_contains(st, &oid), ==, 0); |
| 132 | + } |
| 133 | + |
| 134 | + if (!get_oid_arbitrary_hex("22", &oid)) |
| 135 | + check_int(oidset_remove(st, &oid), ==, 0); |
| 136 | +} |
| 137 | + |
| 138 | +static int input_contains(struct object_id *oid, char *seen) |
| 139 | +{ |
| 140 | + for (size_t i = 0; i < ARRAY_SIZE(hex_input); i++) { |
| 141 | + struct object_id oid_input; |
| 142 | + if (get_oid_arbitrary_hex(hex_input[i], &oid_input)) |
| 143 | + return -1; |
| 144 | + if (oideq(&oid_input, oid)) { |
| 145 | + if (seen[i]) |
| 146 | + return 2; |
| 147 | + seen[i] = 1; |
| 148 | + return 0; |
| 149 | + } |
| 150 | + } |
| 151 | + return 1; |
| 152 | +} |
| 153 | + |
| 154 | +static void t_iterate(struct oidset *st) |
| 155 | +{ |
| 156 | + struct oidset_iter iter; |
| 157 | + struct object_id *oid; |
| 158 | + char seen[ARRAY_SIZE(hex_input)] = { 0 }; |
| 159 | + int count = 0; |
| 160 | + |
| 161 | + oidset_iter_init(st, &iter); |
| 162 | + while ((oid = oidset_iter_next(&iter))) { |
| 163 | + int ret; |
| 164 | + if (!check_int((ret = input_contains(oid, seen)), ==, 0)) { |
| 165 | + switch (ret) { |
| 166 | + case -1: |
| 167 | + break; /* handled by get_oid_arbitrary_hex() */ |
| 168 | + case 1: |
| 169 | + test_msg("obtained object_id was not given in the input\n" |
| 170 | + " object_id: %s", oid_to_hex(oid)); |
| 171 | + break; |
| 172 | + case 2: |
| 173 | + test_msg("duplicate object_id detected\n" |
| 174 | + " object_id: %s", oid_to_hex(oid)); |
| 175 | + break; |
| 176 | + } |
| 177 | + } else { |
| 178 | + count++; |
| 179 | + } |
| 180 | + } |
| 181 | + check_int(count, ==, ARRAY_SIZE(hex_input)); |
| 182 | + check_int(oidset_size(st), ==, ARRAY_SIZE(hex_input)); |
| 183 | +} |
| 184 | + |
| 185 | +static void t_parse_file(void) |
| 186 | +{ |
| 187 | + struct strbuf path = STRBUF_INIT; |
| 188 | + struct oidset st = OIDSET_INIT; |
| 189 | + struct object_id oid; |
| 190 | + int hash_algo = init_hash_algo(); |
| 191 | + |
| 192 | + if (!check_int(hash_algo, !=, GIT_HASH_UNKNOWN)) |
| 193 | + return; |
| 194 | + |
| 195 | + strbuf_test_data_path(&path, hash_algo); |
| 196 | + oidset_parse_file(&st, path.buf, &hash_algos[hash_algo]); |
| 197 | + check_int(oidset_size(&st), ==, 6); |
| 198 | + |
| 199 | + if (!get_oid_arbitrary_hex("00", &oid)) |
| 200 | + check_int(oidset_contains(&st, &oid), ==, 1); |
| 201 | + if (!get_oid_arbitrary_hex("44", &oid)) |
| 202 | + check_int(oidset_contains(&st, &oid), ==, 1); |
| 203 | + if (!get_oid_arbitrary_hex("cc", &oid)) |
| 204 | + check_int(oidset_contains(&st, &oid), ==, 1); |
| 205 | + |
| 206 | + if (!get_oid_arbitrary_hex("11", &oid)) |
| 207 | + check_int(oidset_contains(&st, &oid), ==, 0); |
| 208 | + |
| 209 | + oidset_clear(&st); |
| 210 | + strbuf_release(&path); |
| 211 | +} |
| 212 | + |
| 213 | +int cmd_main(int argc UNUSED, const char **argv UNUSED) |
| 214 | +{ |
| 215 | + TEST(setup(t_contains), "contains works"); |
| 216 | + TEST(setup(t_insert_dup), "insert an already inserted value works"); |
| 217 | + TEST(setup(t_insert_from_set), "insert from one set to another works"); |
| 218 | + TEST(setup(t_remove), "remove works"); |
| 219 | + TEST(setup(t_iterate), "iteration works"); |
| 220 | + TEST(t_parse_file(), "parsing from file works"); |
| 221 | + return test_done(); |
| 222 | +} |
0 commit comments