|
| 1 | +#define USE_THE_REPOSITORY_VARIABLE |
| 2 | + |
| 3 | +#include "unit-test.h" |
| 4 | +#include "lib-oid.h" |
| 5 | +#include "oid-array.h" |
| 6 | +#include "hex.h" |
| 7 | + |
| 8 | +static void 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 | + cl_parse_any_oid(hexes[i], &oid); |
| 14 | + oid_array_append(array, &oid); |
| 15 | + } |
| 16 | + cl_assert_equal_i(array->nr, n); |
| 17 | +} |
| 18 | + |
| 19 | +static int add_to_oid_array(const struct object_id *oid, void *data) |
| 20 | +{ |
| 21 | + struct oid_array *array = data; |
| 22 | + |
| 23 | + oid_array_append(array, oid); |
| 24 | + return 0; |
| 25 | +} |
| 26 | + |
| 27 | +static void t_enumeration(const char **input_args, size_t input_sz, |
| 28 | + const char **expect_args, size_t expect_sz) |
| 29 | +{ |
| 30 | + struct oid_array input = OID_ARRAY_INIT, expect = OID_ARRAY_INIT, |
| 31 | + actual = OID_ARRAY_INIT; |
| 32 | + size_t i; |
| 33 | + |
| 34 | + fill_array(&input, input_args, input_sz); |
| 35 | + fill_array(&expect, expect_args, expect_sz); |
| 36 | + |
| 37 | + oid_array_for_each_unique(&input, add_to_oid_array, &actual); |
| 38 | + cl_assert_equal_i(actual.nr, expect.nr); |
| 39 | + |
| 40 | + for (i = 0; i < actual.nr; i++) |
| 41 | + cl_assert(oideq(&actual.oid[i], &expect.oid[i])); |
| 42 | + |
| 43 | + oid_array_clear(&actual); |
| 44 | + oid_array_clear(&input); |
| 45 | + oid_array_clear(&expect); |
| 46 | +} |
| 47 | + |
| 48 | +#define TEST_ENUMERATION(input, expect) \ |
| 49 | + t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect)); |
| 50 | + |
| 51 | +static void t_lookup(const char **input_hexes, size_t n, const char *query_hex, |
| 52 | + int lower_bound, int upper_bound) |
| 53 | +{ |
| 54 | + struct oid_array array = OID_ARRAY_INIT; |
| 55 | + struct object_id oid_query; |
| 56 | + int ret; |
| 57 | + |
| 58 | + cl_parse_any_oid(query_hex, &oid_query); |
| 59 | + fill_array(&array, input_hexes, n); |
| 60 | + ret = oid_array_lookup(&array, &oid_query); |
| 61 | + |
| 62 | + cl_assert(ret <= upper_bound); |
| 63 | + cl_assert(ret >= lower_bound); |
| 64 | + |
| 65 | + oid_array_clear(&array); |
| 66 | +} |
| 67 | + |
| 68 | +#define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound) \ |
| 69 | + t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \ |
| 70 | + lower_bound, upper_bound); |
| 71 | + |
| 72 | +void test_oid_array__initialize(void) |
| 73 | +{ |
| 74 | + /* The hash algo is used by oid_array_lookup() internally */ |
| 75 | + int algo = cl_setup_hash_algo(); |
| 76 | + repo_set_hash_algo(the_repository, algo); |
| 77 | +} |
| 78 | + |
| 79 | +static const char *arr_input[] = { "88", "44", "aa", "55" }; |
| 80 | +static const char *arr_input_dup[] = { "88", "44", "aa", "55", |
| 81 | + "88", "44", "aa", "55", |
| 82 | + "88", "44", "aa", "55" }; |
| 83 | +static const char *res_sorted[] = { "44", "55", "88", "aa" }; |
| 84 | + |
| 85 | +void test_oid_array__enumerate_unique(void) |
| 86 | +{ |
| 87 | + TEST_ENUMERATION(arr_input, res_sorted); |
| 88 | +} |
| 89 | + |
| 90 | +void test_oid_array__enumerate_duplicate(void) |
| 91 | +{ |
| 92 | + TEST_ENUMERATION(arr_input_dup, res_sorted); |
| 93 | +} |
| 94 | + |
| 95 | +void test_oid_array__lookup(void) |
| 96 | +{ |
| 97 | + TEST_LOOKUP(arr_input, "55", 1, 1); |
| 98 | +} |
| 99 | + |
| 100 | +void test_oid_array__lookup_non_existent(void) |
| 101 | +{ |
| 102 | + TEST_LOOKUP(arr_input, "33", INT_MIN, -1); |
| 103 | +} |
| 104 | + |
| 105 | +void test_oid_array__lookup_duplicates(void) |
| 106 | +{ |
| 107 | + TEST_LOOKUP(arr_input_dup, "55", 3, 5); |
| 108 | +} |
| 109 | + |
| 110 | +void test_oid_array__lookup_non_existent_dup(void) |
| 111 | +{ |
| 112 | + TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1); |
| 113 | +} |
| 114 | + |
| 115 | +void test_oid_array__lookup_almost_dup(void) |
| 116 | +{ |
| 117 | + const char *nearly_55; |
| 118 | + |
| 119 | + nearly_55 = cl_setup_hash_algo() == GIT_HASH_SHA1 ? |
| 120 | + "5500000000000000000000000000000000000001" : |
| 121 | + "5500000000000000000000000000000000000000000000000000000000000001"; |
| 122 | + |
| 123 | + TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0); |
| 124 | +} |
| 125 | + |
| 126 | +void test_oid_array__lookup_single_dup(void) |
| 127 | +{ |
| 128 | + TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1); |
| 129 | +} |
0 commit comments