Git fork
at reftables-rust 107 lines 2.8 kB view raw
1#include "unit-test.h" 2#include "lib-oid.h" 3#include "oidtree.h" 4#include "hash.h" 5#include "hex.h" 6#include "strvec.h" 7 8static struct oidtree ot; 9 10#define FILL_TREE(tree, ...) \ 11 do { \ 12 const char *hexes[] = { __VA_ARGS__ }; \ 13 if (fill_tree_loc(tree, hexes, ARRAY_SIZE(hexes))) \ 14 return; \ 15 } while (0) 16 17static int fill_tree_loc(struct oidtree *ot, const char *hexes[], size_t n) 18{ 19 for (size_t i = 0; i < n; i++) { 20 struct object_id oid; 21 cl_parse_any_oid(hexes[i], &oid); 22 oidtree_insert(ot, &oid); 23 } 24 return 0; 25} 26 27static void check_contains(struct oidtree *ot, const char *hex, int expected) 28{ 29 struct object_id oid; 30 31 cl_parse_any_oid(hex, &oid); 32 cl_assert_equal_i(oidtree_contains(ot, &oid), expected); 33} 34 35struct expected_hex_iter { 36 size_t i; 37 struct strvec expected_hexes; 38 const char *query; 39}; 40 41static enum cb_next check_each_cb(const struct object_id *oid, void *data) 42{ 43 struct expected_hex_iter *hex_iter = data; 44 struct object_id expected; 45 46 cl_assert(hex_iter->i < hex_iter->expected_hexes.nr); 47 48 cl_parse_any_oid(hex_iter->expected_hexes.v[hex_iter->i], 49 &expected); 50 cl_assert_equal_s(oid_to_hex(oid), oid_to_hex(&expected)); 51 hex_iter->i += 1; 52 return CB_CONTINUE; 53} 54 55LAST_ARG_MUST_BE_NULL 56static void check_each(struct oidtree *ot, const char *query, ...) 57{ 58 struct object_id oid; 59 struct expected_hex_iter hex_iter = { .expected_hexes = STRVEC_INIT, 60 .query = query }; 61 const char *arg; 62 va_list hex_args; 63 64 va_start(hex_args, query); 65 while ((arg = va_arg(hex_args, const char *))) 66 strvec_push(&hex_iter.expected_hexes, arg); 67 va_end(hex_args); 68 69 cl_parse_any_oid(query, &oid); 70 oidtree_each(ot, &oid, strlen(query), check_each_cb, &hex_iter); 71 72 if (hex_iter.i != hex_iter.expected_hexes.nr) 73 cl_failf("error: could not find some 'object_id's for query ('%s')", query); 74 75 strvec_clear(&hex_iter.expected_hexes); 76} 77 78void test_oidtree__initialize(void) 79{ 80 oidtree_init(&ot); 81} 82 83void test_oidtree__cleanup(void) 84{ 85 oidtree_clear(&ot); 86} 87 88void test_oidtree__contains(void) 89{ 90 FILL_TREE(&ot, "444", "1", "2", "3", "4", "5", "a", "b", "c", "d", "e"); 91 check_contains(&ot, "44", 0); 92 check_contains(&ot, "441", 0); 93 check_contains(&ot, "440", 0); 94 check_contains(&ot, "444", 1); 95 check_contains(&ot, "4440", 1); 96 check_contains(&ot, "4444", 0); 97} 98 99void test_oidtree__each(void) 100{ 101 FILL_TREE(&ot, "f", "9", "8", "123", "321", "320", "a", "b", "c", "d", "e"); 102 check_each(&ot, "12300", "123", NULL); 103 check_each(&ot, "3211", NULL); /* should not reach callback */ 104 check_each(&ot, "3210", "321", NULL); 105 check_each(&ot, "32100", "321", NULL); 106 check_each(&ot, "32", "320", "321", NULL); 107}