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
8 static 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
17 static 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, NULL);
23 }
24 return 0;
25 }
26
27 static void check_contains(struct oidtree *ot, const char *hex, bool 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
35 struct expected_hex_iter {
36 size_t i;
37 struct strvec expected_hexes;
38 const char *query;
39 };
40
41 static int check_each_cb(const struct object_id *oid, void *node_data UNUSED, void *cb_data)
42 {
43 struct expected_hex_iter *hex_iter = cb_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 0;
53 }
54
55 LAST_ARG_MUST_BE_NULL
56 static 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
78 void test_oidtree__initialize(void)
79 {
80 oidtree_init(&ot);
81 }
82
83 void test_oidtree__cleanup(void)
84 {
85 oidtree_clear(&ot);
86 }
87
88 void 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", false);
92 check_contains(&ot, "441", false);
93 check_contains(&ot, "440", false);
94 check_contains(&ot, "444", true);
95 check_contains(&ot, "4440", true);
96 check_contains(&ot, "4444", false);
97 }
98
99 void 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 }
108
109 void test_oidtree__insert_overwrites_data(void)
110 {
111 struct object_id oid;
112 struct oidtree ot;
113 int a, b;
114
115 cl_parse_any_oid("1", &oid);
116
117 oidtree_init(&ot);
118
119 oidtree_insert(&ot, &oid, NULL);
120 cl_assert_equal_p(oidtree_get(&ot, &oid), NULL);
121 oidtree_insert(&ot, &oid, &a);
122 cl_assert_equal_p(oidtree_get(&ot, &oid), &a);
123 oidtree_insert(&ot, &oid, &b);
124 cl_assert_equal_p(oidtree_get(&ot, &oid), &b);
125
126 oidtree_clear(&ot);
127 }