|
| 1 | +#define USE_THE_REPOSITORY_VARIABLE |
| 2 | + |
| 3 | +#include "test-lib.h" |
| 4 | +#include "lib-oid.h" |
| 5 | +#include "oid-array.h" |
| 6 | +#include "hex.h" |
| 7 | + |
| 8 | +static int fill_array(struct oid_array *array, const char *hexes[], size_t n) |
| 9 | +{ |
| 10 | + for (size_t i = 0; i < n; i++) { |
| 11 | + struct object_id oid; |
| 12 | + |
| 13 | + if (!check_int(get_oid_arbitrary_hex(hexes[i], &oid), ==, 0)) |
| 14 | + return -1; |
| 15 | + oid_array_append(array, &oid); |
| 16 | + } |
| 17 | + if (!check_uint(array->nr, ==, n)) |
| 18 | + return -1; |
| 19 | + return 0; |
| 20 | +} |
| 21 | + |
| 22 | +static int add_to_oid_array(const struct object_id *oid, void *data) |
| 23 | +{ |
| 24 | + struct oid_array *array = data; |
| 25 | + |
| 26 | + oid_array_append(array, oid); |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +static void t_enumeration(const char **input_args, size_t input_sz, |
| 31 | + const char **expect_args, size_t expect_sz) |
| 32 | +{ |
| 33 | + struct oid_array input = OID_ARRAY_INIT, expect = OID_ARRAY_INIT, |
| 34 | + actual = OID_ARRAY_INIT; |
| 35 | + size_t i; |
| 36 | + |
| 37 | + if (fill_array(&input, input_args, input_sz)) |
| 38 | + return; |
| 39 | + if (fill_array(&expect, expect_args, expect_sz)) |
| 40 | + return; |
| 41 | + |
| 42 | + oid_array_for_each_unique(&input, add_to_oid_array, &actual); |
| 43 | + if (!check_uint(actual.nr, ==, expect.nr)) |
| 44 | + return; |
| 45 | + |
| 46 | + for (i = 0; i < actual.nr; i++) { |
| 47 | + if (!check(oideq(&actual.oid[i], &expect.oid[i]))) |
| 48 | + test_msg("expected: %s\n got: %s\n index: %" PRIuMAX, |
| 49 | + oid_to_hex(&expect.oid[i]), oid_to_hex(&actual.oid[i]), |
| 50 | + (uintmax_t)i); |
| 51 | + } |
| 52 | + |
| 53 | + oid_array_clear(&actual); |
| 54 | + oid_array_clear(&input); |
| 55 | + oid_array_clear(&expect); |
| 56 | +} |
| 57 | + |
| 58 | +#define TEST_ENUMERATION(input, expect, desc) \ |
| 59 | + TEST(t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect)), \ |
| 60 | + desc " works") |
| 61 | + |
| 62 | +static void t_lookup(const char **input_hexes, size_t n, const char *query_hex, |
| 63 | + int lower_bound, int upper_bound) |
| 64 | +{ |
| 65 | + struct oid_array array = OID_ARRAY_INIT; |
| 66 | + struct object_id oid_query; |
| 67 | + int ret; |
| 68 | + |
| 69 | + if (!check_int(get_oid_arbitrary_hex(query_hex, &oid_query), ==, 0)) |
| 70 | + return; |
| 71 | + if (fill_array(&array, input_hexes, n)) |
| 72 | + return; |
| 73 | + ret = oid_array_lookup(&array, &oid_query); |
| 74 | + |
| 75 | + if (!check_int(ret, <=, upper_bound) || |
| 76 | + !check_int(ret, >=, lower_bound)) |
| 77 | + test_msg("oid query for lookup: %s", oid_to_hex(&oid_query)); |
| 78 | + |
| 79 | + oid_array_clear(&array); |
| 80 | +} |
| 81 | + |
| 82 | +#define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound, desc) \ |
| 83 | + TEST(t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \ |
| 84 | + lower_bound, upper_bound), \ |
| 85 | + desc " works") |
| 86 | + |
| 87 | +static void setup(void) |
| 88 | +{ |
| 89 | + /* The hash algo is used by oid_array_lookup() internally */ |
| 90 | + int algo = init_hash_algo(); |
| 91 | + if (check_int(algo, !=, GIT_HASH_UNKNOWN)) |
| 92 | + repo_set_hash_algo(the_repository, algo); |
| 93 | +} |
| 94 | + |
| 95 | +int cmd_main(int argc UNUSED, const char **argv UNUSED) |
| 96 | +{ |
| 97 | + const char *arr_input[] = { "88", "44", "aa", "55" }; |
| 98 | + const char *arr_input_dup[] = { "88", "44", "aa", "55", |
| 99 | + "88", "44", "aa", "55", |
| 100 | + "88", "44", "aa", "55" }; |
| 101 | + const char *res_sorted[] = { "44", "55", "88", "aa" }; |
| 102 | + const char *nearly_55; |
| 103 | + |
| 104 | + if (!TEST(setup(), "setup")) |
| 105 | + test_skip_all("hash algo initialization failed"); |
| 106 | + |
| 107 | + TEST_ENUMERATION(arr_input, res_sorted, "ordered enumeration"); |
| 108 | + TEST_ENUMERATION(arr_input_dup, res_sorted, |
| 109 | + "ordered enumeration with duplicate suppression"); |
| 110 | + |
| 111 | + TEST_LOOKUP(arr_input, "55", 1, 1, "lookup"); |
| 112 | + TEST_LOOKUP(arr_input, "33", INT_MIN, -1, "lookup non-existent entry"); |
| 113 | + TEST_LOOKUP(arr_input_dup, "55", 3, 5, "lookup with duplicates"); |
| 114 | + TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1, |
| 115 | + "lookup non-existent entry with duplicates"); |
| 116 | + |
| 117 | + nearly_55 = init_hash_algo() == GIT_HASH_SHA1 ? |
| 118 | + "5500000000000000000000000000000000000001" : |
| 119 | + "5500000000000000000000000000000000000000000000000000000000000001"; |
| 120 | + TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0, |
| 121 | + "lookup with almost duplicate values"); |
| 122 | + TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1, |
| 123 | + "lookup with single duplicate value"); |
| 124 | + |
| 125 | + return test_done(); |
| 126 | +} |
0 commit comments