summaryrefslogtreecommitdiff
path: root/t/unit-tests
diff options
context:
space:
mode:
Diffstat (limited to 't/unit-tests')
-rw-r--r--t/unit-tests/clar/clar/fs.h10
-rw-r--r--t/unit-tests/lib-oid.c32
-rw-r--r--t/unit-tests/lib-oid.h9
-rw-r--r--t/unit-tests/t-oid-array.c126
-rw-r--r--t/unit-tests/t-oidmap.c181
-rw-r--r--t/unit-tests/t-oidtree.c122
-rw-r--r--t/unit-tests/t-reftable-basics.c28
-rw-r--r--t/unit-tests/t-reftable-pq.c22
-rw-r--r--t/unit-tests/t-reftable-record.c42
-rw-r--r--t/unit-tests/t-trailer.c317
-rw-r--r--t/unit-tests/u-oid-array.c129
-rw-r--r--t/unit-tests/u-oidmap.c136
-rw-r--r--t/unit-tests/u-oidtree.c107
-rw-r--r--t/unit-tests/u-trailer.c320
-rw-r--r--t/unit-tests/u-urlmatch-normalization.c (renamed from t/unit-tests/t-urlmatch-normalization.c)60
-rw-r--r--t/unit-tests/unit-test.c2
16 files changed, 804 insertions, 839 deletions
diff --git a/t/unit-tests/clar/clar/fs.h b/t/unit-tests/clar/clar/fs.h
index 8b206179fc..2203743fb4 100644
--- a/t/unit-tests/clar/clar/fs.h
+++ b/t/unit-tests/clar/clar/fs.h
@@ -376,9 +376,12 @@ fs_copydir_helper(const char *source, const char *dest, int dest_mode)
mkdir(dest, dest_mode);
cl_assert_(source_dir = opendir(source), "Could not open source dir");
- while ((d = (errno = 0, readdir(source_dir))) != NULL) {
+ for (;;) {
char *child;
+ errno = 0;
+ if ((d = readdir(source_dir)) == NULL)
+ break;
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
@@ -479,9 +482,12 @@ fs_rmdir_helper(const char *path)
struct dirent *d;
cl_assert_(dir = opendir(path), "Could not open dir");
- while ((d = (errno = 0, readdir(dir))) != NULL) {
+ for (;;) {
char *child;
+ errno = 0;
+ if ((d = readdir(dir)) == NULL)
+ break;
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
diff --git a/t/unit-tests/lib-oid.c b/t/unit-tests/lib-oid.c
index 8f0ccac532..e0b3180f23 100644
--- a/t/unit-tests/lib-oid.c
+++ b/t/unit-tests/lib-oid.c
@@ -1,9 +1,9 @@
-#include "test-lib.h"
+#include "unit-test.h"
#include "lib-oid.h"
#include "strbuf.h"
#include "hex.h"
-int init_hash_algo(void)
+int cl_setup_hash_algo(void)
{
static int algo = -1;
@@ -11,42 +11,32 @@ int init_hash_algo(void)
const char *algo_name = getenv("GIT_TEST_DEFAULT_HASH");
algo = algo_name ? hash_algo_by_name(algo_name) : GIT_HASH_SHA1;
- if (!check(algo != GIT_HASH_UNKNOWN))
- test_msg("BUG: invalid GIT_TEST_DEFAULT_HASH value ('%s')",
- algo_name);
+ cl_assert(algo != GIT_HASH_UNKNOWN);
}
return algo;
}
-static int get_oid_arbitrary_hex_algop(const char *hex, struct object_id *oid,
+static void cl_parse_oid(const char *hex, struct object_id *oid,
const struct git_hash_algo *algop)
{
- int ret;
size_t sz = strlen(hex);
struct strbuf buf = STRBUF_INIT;
- if (!check(sz <= algop->hexsz)) {
- test_msg("BUG: hex string (%s) bigger than maximum allowed (%lu)",
- hex, (unsigned long)algop->hexsz);
- return -1;
- }
+ cl_assert(sz <= algop->hexsz);
strbuf_add(&buf, hex, sz);
strbuf_addchars(&buf, '0', algop->hexsz - sz);
- ret = get_oid_hex_algop(buf.buf, oid, algop);
- if (!check_int(ret, ==, 0))
- test_msg("BUG: invalid hex input (%s) provided", hex);
+ cl_assert_equal_i(get_oid_hex_algop(buf.buf, oid, algop), 0);
strbuf_release(&buf);
- return ret;
}
-int get_oid_arbitrary_hex(const char *hex, struct object_id *oid)
+
+void cl_parse_any_oid(const char *hex, struct object_id *oid)
{
- int hash_algo = init_hash_algo();
+ int hash_algo = cl_setup_hash_algo();
- if (!check_int(hash_algo, !=, GIT_HASH_UNKNOWN))
- return -1;
- return get_oid_arbitrary_hex_algop(hex, oid, &hash_algos[hash_algo]);
+ cl_assert(hash_algo != GIT_HASH_UNKNOWN);
+ cl_parse_oid(hex, oid, &hash_algos[hash_algo]);
}
diff --git a/t/unit-tests/lib-oid.h b/t/unit-tests/lib-oid.h
index 4e77c04bd2..4031775104 100644
--- a/t/unit-tests/lib-oid.h
+++ b/t/unit-tests/lib-oid.h
@@ -5,6 +5,7 @@
/*
* Convert arbitrary hex string to object_id.
+ *
* For example, passing "abc12" will generate
* "abc1200000000000000000000000000000000000" hex of length 40 for SHA-1 and
* create object_id with that.
@@ -12,14 +13,16 @@
* algo is not allowed. The hash algo is decided based on GIT_TEST_DEFAULT_HASH
* environment variable.
*/
-int get_oid_arbitrary_hex(const char *s, struct object_id *oid);
+
+void cl_parse_any_oid (const char *s, struct object_id *oid);
/*
* Returns one of GIT_HASH_{SHA1, SHA256, UNKNOWN} based on the value of
* GIT_TEST_DEFAULT_HASH environment variable. The fallback value in the
* absence of GIT_TEST_DEFAULT_HASH is GIT_HASH_SHA1. It also uses
- * check(algo != GIT_HASH_UNKNOWN) before returning to verify if the
+ * cl_assert(algo != GIT_HASH_UNKNOWN) before returning to verify if the
* GIT_TEST_DEFAULT_HASH's value is valid or not.
*/
-int init_hash_algo(void);
+
+int cl_setup_hash_algo(void);
#endif /* LIB_OID_H */
diff --git a/t/unit-tests/t-oid-array.c b/t/unit-tests/t-oid-array.c
deleted file mode 100644
index 45b59a2a51..0000000000
--- a/t/unit-tests/t-oid-array.c
+++ /dev/null
@@ -1,126 +0,0 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
-#include "test-lib.h"
-#include "lib-oid.h"
-#include "oid-array.h"
-#include "hex.h"
-
-static int fill_array(struct oid_array *array, const char *hexes[], size_t n)
-{
- for (size_t i = 0; i < n; i++) {
- struct object_id oid;
-
- if (!check_int(get_oid_arbitrary_hex(hexes[i], &oid), ==, 0))
- return -1;
- oid_array_append(array, &oid);
- }
- if (!check_uint(array->nr, ==, n))
- return -1;
- return 0;
-}
-
-static int add_to_oid_array(const struct object_id *oid, void *data)
-{
- struct oid_array *array = data;
-
- oid_array_append(array, oid);
- return 0;
-}
-
-static void t_enumeration(const char **input_args, size_t input_sz,
- const char **expect_args, size_t expect_sz)
-{
- struct oid_array input = OID_ARRAY_INIT, expect = OID_ARRAY_INIT,
- actual = OID_ARRAY_INIT;
- size_t i;
-
- if (fill_array(&input, input_args, input_sz))
- return;
- if (fill_array(&expect, expect_args, expect_sz))
- return;
-
- oid_array_for_each_unique(&input, add_to_oid_array, &actual);
- if (!check_uint(actual.nr, ==, expect.nr))
- return;
-
- for (i = 0; i < actual.nr; i++) {
- if (!check(oideq(&actual.oid[i], &expect.oid[i])))
- test_msg("expected: %s\n got: %s\n index: %" PRIuMAX,
- oid_to_hex(&expect.oid[i]), oid_to_hex(&actual.oid[i]),
- (uintmax_t)i);
- }
-
- oid_array_clear(&actual);
- oid_array_clear(&input);
- oid_array_clear(&expect);
-}
-
-#define TEST_ENUMERATION(input, expect, desc) \
- TEST(t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect)), \
- desc " works")
-
-static void t_lookup(const char **input_hexes, size_t n, const char *query_hex,
- int lower_bound, int upper_bound)
-{
- struct oid_array array = OID_ARRAY_INIT;
- struct object_id oid_query;
- int ret;
-
- if (!check_int(get_oid_arbitrary_hex(query_hex, &oid_query), ==, 0))
- return;
- if (fill_array(&array, input_hexes, n))
- return;
- ret = oid_array_lookup(&array, &oid_query);
-
- if (!check_int(ret, <=, upper_bound) ||
- !check_int(ret, >=, lower_bound))
- test_msg("oid query for lookup: %s", oid_to_hex(&oid_query));
-
- oid_array_clear(&array);
-}
-
-#define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound, desc) \
- TEST(t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \
- lower_bound, upper_bound), \
- desc " works")
-
-static void setup(void)
-{
- /* The hash algo is used by oid_array_lookup() internally */
- int algo = init_hash_algo();
- if (check_int(algo, !=, GIT_HASH_UNKNOWN))
- repo_set_hash_algo(the_repository, algo);
-}
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- const char *arr_input[] = { "88", "44", "aa", "55" };
- const char *arr_input_dup[] = { "88", "44", "aa", "55",
- "88", "44", "aa", "55",
- "88", "44", "aa", "55" };
- const char *res_sorted[] = { "44", "55", "88", "aa" };
- const char *nearly_55;
-
- if (!TEST(setup(), "setup"))
- test_skip_all("hash algo initialization failed");
-
- TEST_ENUMERATION(arr_input, res_sorted, "ordered enumeration");
- TEST_ENUMERATION(arr_input_dup, res_sorted,
- "ordered enumeration with duplicate suppression");
-
- TEST_LOOKUP(arr_input, "55", 1, 1, "lookup");
- TEST_LOOKUP(arr_input, "33", INT_MIN, -1, "lookup non-existent entry");
- TEST_LOOKUP(arr_input_dup, "55", 3, 5, "lookup with duplicates");
- TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1,
- "lookup non-existent entry with duplicates");
-
- nearly_55 = init_hash_algo() == GIT_HASH_SHA1 ?
- "5500000000000000000000000000000000000001" :
- "5500000000000000000000000000000000000000000000000000000000000001";
- TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0,
- "lookup with almost duplicate values");
- TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1,
- "lookup with single duplicate value");
-
- return test_done();
-}
diff --git a/t/unit-tests/t-oidmap.c b/t/unit-tests/t-oidmap.c
deleted file mode 100644
index b22e52d08b..0000000000
--- a/t/unit-tests/t-oidmap.c
+++ /dev/null
@@ -1,181 +0,0 @@
-#include "test-lib.h"
-#include "lib-oid.h"
-#include "oidmap.h"
-#include "hash.h"
-#include "hex.h"
-
-/*
- * Elements we will put in oidmap structs are made of a key: the entry.oid
- * field, which is of type struct object_id, and a value: the name field (could
- * be a refname for example).
- */
-struct test_entry {
- struct oidmap_entry entry;
- char name[FLEX_ARRAY];
-};
-
-static const char *const key_val[][2] = { { "11", "one" },
- { "22", "two" },
- { "33", "three" } };
-
-static void setup(void (*f)(struct oidmap *map))
-{
- struct oidmap map = OIDMAP_INIT;
- int ret = 0;
-
- for (size_t i = 0; i < ARRAY_SIZE(key_val); i++){
- struct test_entry *entry;
-
- FLEX_ALLOC_STR(entry, name, key_val[i][1]);
- if ((ret = get_oid_arbitrary_hex(key_val[i][0], &entry->entry.oid))) {
- free(entry);
- break;
- }
- entry = oidmap_put(&map, entry);
- if (!check(entry == NULL))
- free(entry);
- }
-
- if (!ret)
- f(&map);
- oidmap_free(&map, 1);
-}
-
-static void t_replace(struct oidmap *map)
-{
- struct test_entry *entry, *prev;
-
- FLEX_ALLOC_STR(entry, name, "un");
- if (get_oid_arbitrary_hex("11", &entry->entry.oid))
- return;
- prev = oidmap_put(map, entry);
- if (!check(prev != NULL))
- return;
- check_str(prev->name, "one");
- free(prev);
-
- FLEX_ALLOC_STR(entry, name, "deux");
- if (get_oid_arbitrary_hex("22", &entry->entry.oid))
- return;
- prev = oidmap_put(map, entry);
- if (!check(prev != NULL))
- return;
- check_str(prev->name, "two");
- free(prev);
-}
-
-static void t_get(struct oidmap *map)
-{
- struct test_entry *entry;
- struct object_id oid;
-
- if (get_oid_arbitrary_hex("22", &oid))
- return;
- entry = oidmap_get(map, &oid);
- if (!check(entry != NULL))
- return;
- check_str(entry->name, "two");
-
- if (get_oid_arbitrary_hex("44", &oid))
- return;
- check(oidmap_get(map, &oid) == NULL);
-
- if (get_oid_arbitrary_hex("11", &oid))
- return;
- entry = oidmap_get(map, &oid);
- if (!check(entry != NULL))
- return;
- check_str(entry->name, "one");
-}
-
-static void t_remove(struct oidmap *map)
-{
- struct test_entry *entry;
- struct object_id oid;
-
- if (get_oid_arbitrary_hex("11", &oid))
- return;
- entry = oidmap_remove(map, &oid);
- if (!check(entry != NULL))
- return;
- check_str(entry->name, "one");
- check(oidmap_get(map, &oid) == NULL);
- free(entry);
-
- if (get_oid_arbitrary_hex("22", &oid))
- return;
- entry = oidmap_remove(map, &oid);
- if (!check(entry != NULL))
- return;
- check_str(entry->name, "two");
- check(oidmap_get(map, &oid) == NULL);
- free(entry);
-
- if (get_oid_arbitrary_hex("44", &oid))
- return;
- check(oidmap_remove(map, &oid) == NULL);
-}
-
-static int key_val_contains(struct test_entry *entry, char seen[])
-{
- for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
- struct object_id oid;
-
- if (get_oid_arbitrary_hex(key_val[i][0], &oid))
- return -1;
-
- if (oideq(&entry->entry.oid, &oid)) {
- if (seen[i])
- return 2;
- seen[i] = 1;
- return 0;
- }
- }
- return 1;
-}
-
-static void t_iterate(struct oidmap *map)
-{
- struct oidmap_iter iter;
- struct test_entry *entry;
- char seen[ARRAY_SIZE(key_val)] = { 0 };
- int count = 0;
-
- oidmap_iter_init(map, &iter);
- while ((entry = oidmap_iter_next(&iter))) {
- int ret;
- if (!check_int((ret = key_val_contains(entry, seen)), ==, 0)) {
- switch (ret) {
- case -1:
- break; /* error message handled by get_oid_arbitrary_hex() */
- case 1:
- test_msg("obtained entry was not given in the input\n"
- " name: %s\n oid: %s\n",
- entry->name, oid_to_hex(&entry->entry.oid));
- break;
- case 2:
- test_msg("duplicate entry detected\n"
- " name: %s\n oid: %s\n",
- entry->name, oid_to_hex(&entry->entry.oid));
- break;
- default:
- test_msg("BUG: invalid return value (%d) from key_val_contains()",
- ret);
- break;
- }
- } else {
- count++;
- }
- }
- check_int(count, ==, ARRAY_SIZE(key_val));
- check_int(hashmap_get_size(&map->map), ==, ARRAY_SIZE(key_val));
-}
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- TEST(setup(t_replace), "replace works");
- TEST(setup(t_get), "get works");
- TEST(setup(t_remove), "remove works");
- TEST(setup(t_iterate), "iterate works");
- return test_done();
-}
diff --git a/t/unit-tests/t-oidtree.c b/t/unit-tests/t-oidtree.c
deleted file mode 100644
index a38754b066..0000000000
--- a/t/unit-tests/t-oidtree.c
+++ /dev/null
@@ -1,122 +0,0 @@
-#include "test-lib.h"
-#include "lib-oid.h"
-#include "oidtree.h"
-#include "hash.h"
-#include "hex.h"
-#include "strvec.h"
-
-#define FILL_TREE(tree, ...) \
- do { \
- const char *hexes[] = { __VA_ARGS__ }; \
- if (fill_tree_loc(tree, hexes, ARRAY_SIZE(hexes))) \
- return; \
- } while (0)
-
-static int fill_tree_loc(struct oidtree *ot, const char *hexes[], size_t n)
-{
- for (size_t i = 0; i < n; i++) {
- struct object_id oid;
- if (!check_int(get_oid_arbitrary_hex(hexes[i], &oid), ==, 0))
- return -1;
- oidtree_insert(ot, &oid);
- }
- return 0;
-}
-
-static void check_contains(struct oidtree *ot, const char *hex, int expected)
-{
- struct object_id oid;
-
- if (!check_int(get_oid_arbitrary_hex(hex, &oid), ==, 0))
- return;
- if (!check_int(oidtree_contains(ot, &oid), ==, expected))
- test_msg("oid: %s", oid_to_hex(&oid));
-}
-
-struct expected_hex_iter {
- size_t i;
- struct strvec expected_hexes;
- const char *query;
-};
-
-static enum cb_next check_each_cb(const struct object_id *oid, void *data)
-{
- struct expected_hex_iter *hex_iter = data;
- struct object_id expected;
-
- if (!check_int(hex_iter->i, <, hex_iter->expected_hexes.nr)) {
- test_msg("error: extraneous callback for query: ('%s'), object_id: ('%s')",
- hex_iter->query, oid_to_hex(oid));
- return CB_BREAK;
- }
-
- if (!check_int(get_oid_arbitrary_hex(hex_iter->expected_hexes.v[hex_iter->i],
- &expected), ==, 0))
- ; /* the data is bogus and cannot be used */
- else if (!check(oideq(oid, &expected)))
- test_msg("expected: %s\n got: %s\n query: %s",
- oid_to_hex(&expected), oid_to_hex(oid), hex_iter->query);
-
- hex_iter->i += 1;
- return CB_CONTINUE;
-}
-
-LAST_ARG_MUST_BE_NULL
-static void check_each(struct oidtree *ot, const char *query, ...)
-{
- struct object_id oid;
- struct expected_hex_iter hex_iter = { .expected_hexes = STRVEC_INIT,
- .query = query };
- const char *arg;
- va_list hex_args;
-
- va_start(hex_args, query);
- while ((arg = va_arg(hex_args, const char *)))
- strvec_push(&hex_iter.expected_hexes, arg);
- va_end(hex_args);
-
- if (!check_int(get_oid_arbitrary_hex(query, &oid), ==, 0))
- return;
- oidtree_each(ot, &oid, strlen(query), check_each_cb, &hex_iter);
-
- if (!check_int(hex_iter.i, ==, hex_iter.expected_hexes.nr))
- test_msg("error: could not find some 'object_id's for query ('%s')", query);
- strvec_clear(&hex_iter.expected_hexes);
-}
-
-static void setup(void (*f)(struct oidtree *ot))
-{
- struct oidtree ot;
-
- oidtree_init(&ot);
- f(&ot);
- oidtree_clear(&ot);
-}
-
-static void t_contains(struct oidtree *ot)
-{
- FILL_TREE(ot, "444", "1", "2", "3", "4", "5", "a", "b", "c", "d", "e");
- check_contains(ot, "44", 0);
- check_contains(ot, "441", 0);
- check_contains(ot, "440", 0);
- check_contains(ot, "444", 1);
- check_contains(ot, "4440", 1);
- check_contains(ot, "4444", 0);
-}
-
-static void t_each(struct oidtree *ot)
-{
- FILL_TREE(ot, "f", "9", "8", "123", "321", "320", "a", "b", "c", "d", "e");
- check_each(ot, "12300", "123", NULL);
- check_each(ot, "3211", NULL); /* should not reach callback */
- check_each(ot, "3210", "321", NULL);
- check_each(ot, "32100", "321", NULL);
- check_each(ot, "32", "320", "321", NULL);
-}
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- TEST(setup(t_contains), "oidtree insert and contains works");
- TEST(setup(t_each), "oidtree each works");
- return test_done();
-}
diff --git a/t/unit-tests/t-reftable-basics.c b/t/unit-tests/t-reftable-basics.c
index 9ba7eb05ad..c9e751e49e 100644
--- a/t/unit-tests/t-reftable-basics.c
+++ b/t/unit-tests/t-reftable-basics.c
@@ -128,12 +128,30 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
reftable_buf_release(&b);
}
- if_test ("put_be24 and get_be24 work") {
+ if_test ("reftable_put_be64 and reftable_get_be64 work") {
+ uint64_t in = 0x1122334455667788;
+ uint8_t dest[8];
+ uint64_t out;
+ reftable_put_be64(dest, in);
+ out = reftable_get_be64(dest);
+ check_int(in, ==, out);
+ }
+
+ if_test ("reftable_put_be32 and reftable_get_be32 work") {
+ uint32_t in = 0x11223344;
+ uint8_t dest[4];
+ uint32_t out;
+ reftable_put_be32(dest, in);
+ out = reftable_get_be32(dest);
+ check_int(in, ==, out);
+ }
+
+ if_test ("reftable_put_be24 and reftable_get_be24 work") {
uint32_t in = 0x112233;
uint8_t dest[3];
uint32_t out;
- put_be24(dest, in);
- out = get_be24(dest);
+ reftable_put_be24(dest, in);
+ out = reftable_get_be24(dest);
check_int(in, ==, out);
}
@@ -141,8 +159,8 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
uint32_t in = 0xfef1;
uint8_t dest[3];
uint32_t out;
- put_be16(dest, in);
- out = get_be16(dest);
+ reftable_put_be16(dest, in);
+ out = reftable_get_be16(dest);
check_int(in, ==, out);
}
diff --git a/t/unit-tests/t-reftable-pq.c b/t/unit-tests/t-reftable-pq.c
index f3f8a0cdf3..c128fe8616 100644
--- a/t/unit-tests/t-reftable-pq.c
+++ b/t/unit-tests/t-reftable-pq.c
@@ -21,7 +21,9 @@ static void merged_iter_pqueue_check(const struct merged_iter_pqueue *pq)
static int pq_entry_equal(struct pq_entry *a, struct pq_entry *b)
{
- return !reftable_record_cmp(a->rec, b->rec) && (a->index == b->index);
+ int cmp;
+ check(!reftable_record_cmp(a->rec, b->rec, &cmp));
+ return !cmp && (a->index == b->index);
}
static void t_pq_record(void)
@@ -32,7 +34,7 @@ static void t_pq_record(void)
char *last = NULL;
for (i = 0; i < N; i++) {
- reftable_record_init(&recs[i], BLOCK_TYPE_REF);
+ check(!reftable_record_init(&recs[i], BLOCK_TYPE_REF));
recs[i].u.ref.refname = xstrfmt("%02"PRIuMAX, (uintmax_t)i);
}
@@ -49,7 +51,9 @@ static void t_pq_record(void)
while (!merged_iter_pqueue_is_empty(pq)) {
struct pq_entry top = merged_iter_pqueue_top(pq);
- struct pq_entry e = merged_iter_pqueue_remove(&pq);
+ struct pq_entry e;
+
+ check(!merged_iter_pqueue_remove(&pq, &e));
merged_iter_pqueue_check(&pq);
check(pq_entry_equal(&top, &e));
@@ -72,7 +76,7 @@ static void t_pq_index(void)
size_t N = ARRAY_SIZE(recs), i;
for (i = 0; i < N; i++) {
- reftable_record_init(&recs[i], BLOCK_TYPE_REF);
+ check(!reftable_record_init(&recs[i], BLOCK_TYPE_REF));
recs[i].u.ref.refname = (char *) "refs/heads/master";
}
@@ -90,7 +94,9 @@ static void t_pq_index(void)
for (i = N - 1; i > 0; i--) {
struct pq_entry top = merged_iter_pqueue_top(pq);
- struct pq_entry e = merged_iter_pqueue_remove(&pq);
+ struct pq_entry e;
+
+ check(!merged_iter_pqueue_remove(&pq, &e));
merged_iter_pqueue_check(&pq);
check(pq_entry_equal(&top, &e));
@@ -111,7 +117,7 @@ static void t_merged_iter_pqueue_top(void)
size_t N = ARRAY_SIZE(recs), i;
for (i = 0; i < N; i++) {
- reftable_record_init(&recs[i], BLOCK_TYPE_REF);
+ check(!reftable_record_init(&recs[i], BLOCK_TYPE_REF));
recs[i].u.ref.refname = (char *) "refs/heads/master";
}
@@ -129,7 +135,9 @@ static void t_merged_iter_pqueue_top(void)
for (i = N - 1; i > 0; i--) {
struct pq_entry top = merged_iter_pqueue_top(pq);
- struct pq_entry e = merged_iter_pqueue_remove(&pq);
+ struct pq_entry e;
+
+ check(!merged_iter_pqueue_remove(&pq, &e));
merged_iter_pqueue_check(&pq);
check(pq_entry_equal(&top, &e));
diff --git a/t/unit-tests/t-reftable-record.c b/t/unit-tests/t-reftable-record.c
index d49d2a2729..5954966373 100644
--- a/t/unit-tests/t-reftable-record.c
+++ b/t/unit-tests/t-reftable-record.c
@@ -17,7 +17,7 @@ static void t_copy(struct reftable_record *rec)
uint8_t typ;
typ = reftable_record_type(rec);
- reftable_record_init(&copy, typ);
+ check(!reftable_record_init(&copy, typ));
reftable_record_copy_from(&copy, rec, REFTABLE_HASH_SIZE_SHA1);
/* do it twice to catch memory leaks */
reftable_record_copy_from(&copy, rec, REFTABLE_HASH_SIZE_SHA1);
@@ -100,16 +100,20 @@ static void t_reftable_ref_record_comparison(void)
.u.ref.value.symref = (char *) "refs/heads/master",
},
};
+ int cmp;
check(!reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));
- check(!reftable_record_cmp(&in[0], &in[1]));
+ check(!reftable_record_cmp(&in[0], &in[1], &cmp));
+ check(!cmp);
check(!reftable_record_equal(&in[1], &in[2], REFTABLE_HASH_SIZE_SHA1));
- check_int(reftable_record_cmp(&in[1], &in[2]), >, 0);
+ check(!reftable_record_cmp(&in[1], &in[2], &cmp));
+ check_int(cmp, >, 0);
in[1].u.ref.value_type = in[0].u.ref.value_type;
check(reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));
- check(!reftable_record_cmp(&in[0], &in[1]));
+ check(!reftable_record_cmp(&in[0], &in[1], &cmp));
+ check(!cmp);
}
static void t_reftable_ref_record_compare_name(void)
@@ -209,17 +213,20 @@ static void t_reftable_log_record_comparison(void)
.u.log.update_index = 22,
},
};
+ int cmp;
check(!reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));
check(!reftable_record_equal(&in[1], &in[2], REFTABLE_HASH_SIZE_SHA1));
- check_int(reftable_record_cmp(&in[1], &in[2]), >, 0);
+ check(!reftable_record_cmp(&in[1], &in[2], &cmp));
+ check_int(cmp, >, 0);
/* comparison should be reversed for equal keys, because
* comparison is now performed on the basis of update indices */
- check_int(reftable_record_cmp(&in[0], &in[1]), <, 0);
+ check(!reftable_record_cmp(&in[0], &in[1], &cmp));
+ check_int(cmp, <, 0);
in[1].u.log.update_index = in[0].u.log.update_index;
check(reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));
- check(!reftable_record_cmp(&in[0], &in[1]));
+ check(!reftable_record_cmp(&in[0], &in[1], &cmp));
}
static void t_reftable_log_record_compare_key(void)
@@ -396,16 +403,20 @@ static void t_reftable_obj_record_comparison(void)
.u.obj.hash_prefix_len = 5,
},
};
+ int cmp;
check(!reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));
- check(!reftable_record_cmp(&in[0], &in[1]));
+ check(!reftable_record_cmp(&in[0], &in[1], &cmp));
+ check(!cmp);
check(!reftable_record_equal(&in[1], &in[2], REFTABLE_HASH_SIZE_SHA1));
- check_int(reftable_record_cmp(&in[1], &in[2]), >, 0);
+ check(!reftable_record_cmp(&in[1], &in[2], &cmp));
+ check_int(cmp, >, 0);
in[1].u.obj.offset_len = in[0].u.obj.offset_len;
check(reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));
- check(!reftable_record_cmp(&in[0], &in[1]));
+ check(!reftable_record_cmp(&in[0], &in[1], &cmp));
+ check(!cmp);
}
static void t_reftable_obj_record_roundtrip(void)
@@ -486,19 +497,24 @@ static void t_reftable_index_record_comparison(void)
.u.idx.last_key = REFTABLE_BUF_INIT,
},
};
+ int cmp;
+
check(!reftable_buf_addstr(&in[0].u.idx.last_key, "refs/heads/master"));
check(!reftable_buf_addstr(&in[1].u.idx.last_key, "refs/heads/master"));
check(!reftable_buf_addstr(&in[2].u.idx.last_key, "refs/heads/branch"));
check(!reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));
- check(!reftable_record_cmp(&in[0], &in[1]));
+ check(!reftable_record_cmp(&in[0], &in[1], &cmp));
+ check(!cmp);
check(!reftable_record_equal(&in[1], &in[2], REFTABLE_HASH_SIZE_SHA1));
- check_int(reftable_record_cmp(&in[1], &in[2]), >, 0);
+ check(!reftable_record_cmp(&in[1], &in[2], &cmp));
+ check_int(cmp, >, 0);
in[1].u.idx.offset = in[0].u.idx.offset;
check(reftable_record_equal(&in[0], &in[1], REFTABLE_HASH_SIZE_SHA1));
- check(!reftable_record_cmp(&in[0], &in[1]));
+ check(!reftable_record_cmp(&in[0], &in[1], &cmp));
+ check(!cmp);
for (size_t i = 0; i < ARRAY_SIZE(in); i++)
reftable_record_release(&in[i]);
diff --git a/t/unit-tests/t-trailer.c b/t/unit-tests/t-trailer.c
deleted file mode 100644
index 184593e73d..0000000000
--- a/t/unit-tests/t-trailer.c
+++ /dev/null
@@ -1,317 +0,0 @@
-#define DISABLE_SIGN_COMPARE_WARNINGS
-
-#include "test-lib.h"
-#include "trailer.h"
-
-struct contents {
- const char *raw;
- const char *key;
- const char *val;
-};
-
-static void t_trailer_iterator(const char *msg, size_t num_expected,
- struct contents *contents)
-{
- struct trailer_iterator iter;
- size_t i = 0;
-
- trailer_iterator_init(&iter, msg);
- while (trailer_iterator_advance(&iter)) {
- if (num_expected) {
- check_str(iter.raw, contents[i].raw);
- check_str(iter.key.buf, contents[i].key);
- check_str(iter.val.buf, contents[i].val);
- }
- i++;
- }
- trailer_iterator_release(&iter);
-
- check_uint(i, ==, num_expected);
-}
-
-static void run_t_trailer_iterator(void)
-{
-
- static struct test_cases {
- const char *name;
- const char *msg;
- size_t num_expected;
- struct contents contents[10];
- } tc[] = {
- {
- "empty input",
- "",
- 0,
- {{0}},
- },
- {
- "no newline at beginning",
- "Fixes: x\n"
- "Acked-by: x\n"
- "Reviewed-by: x\n",
- 0,
- {{0}},
- },
- {
- "newline at beginning",
- "\n"
- "Fixes: x\n"
- "Acked-by: x\n"
- "Reviewed-by: x\n",
- 3,
- {
- {
- .raw = "Fixes: x\n",
- .key = "Fixes",
- .val = "x",
- },
- {
- .raw = "Acked-by: x\n",
- .key = "Acked-by",
- .val = "x",
- },
- {
- .raw = "Reviewed-by: x\n",
- .key = "Reviewed-by",
- .val = "x",
- },
- {
- 0
- },
- },
- },
- {
- "without body text",
- "subject: foo bar\n"
- "\n"
- "Fixes: x\n"
- "Acked-by: x\n"
- "Reviewed-by: x\n",
- 3,
- {
- {
- .raw = "Fixes: x\n",
- .key = "Fixes",
- .val = "x",
- },
- {
- .raw = "Acked-by: x\n",
- .key = "Acked-by",
- .val = "x",
- },
- {
- .raw = "Reviewed-by: x\n",
- .key = "Reviewed-by",
- .val = "x",
- },
- {
- 0
- },
- },
- },
- {
- "with body text, without divider",
- "my subject\n"
- "\n"
- "my body which is long\n"
- "and contains some special\n"
- "chars like : = ? !\n"
- "hello\n"
- "\n"
- "Fixes: x\n"
- "Acked-by: x\n"
- "Reviewed-by: x\n"
- "Signed-off-by: x\n",
- 4,
- {
- {
- .raw = "Fixes: x\n",
- .key = "Fixes",
- .val = "x",
- },
- {
- .raw = "Acked-by: x\n",
- .key = "Acked-by",
- .val = "x",
- },
- {
- .raw = "Reviewed-by: x\n",
- .key = "Reviewed-by",
- .val = "x",
- },
- {
- .raw = "Signed-off-by: x\n",
- .key = "Signed-off-by",
- .val = "x",
- },
- {
- 0
- },
- },
- },
- {
- "with body text, without divider (second trailer block)",
- "my subject\n"
- "\n"
- "my body which is long\n"
- "and contains some special\n"
- "chars like : = ? !\n"
- "hello\n"
- "\n"
- "Fixes: x\n"
- "Acked-by: x\n"
- "Reviewed-by: x\n"
- "Signed-off-by: x\n"
- "\n"
- /*
- * Because this is the last trailer block, it takes
- * precedence over the first one encountered above.
- */
- "Helped-by: x\n"
- "Signed-off-by: x\n",
- 2,
- {
- {
- .raw = "Helped-by: x\n",
- .key = "Helped-by",
- .val = "x",
- },
- {
- .raw = "Signed-off-by: x\n",
- .key = "Signed-off-by",
- .val = "x",
- },
- {
- 0
- },
- },
- },
- {
- "with body text, with divider",
- "my subject\n"
- "\n"
- "my body which is long\n"
- "and contains some special\n"
- "chars like : = ? !\n"
- "hello\n"
- "\n"
- "---\n"
- "\n"
- /*
- * This trailer still counts because the iterator
- * always ignores the divider.
- */
- "Signed-off-by: x\n",
- 1,
- {
- {
- .raw = "Signed-off-by: x\n",
- .key = "Signed-off-by",
- .val = "x",
- },
- {
- 0
- },
- },
- },
- {
- "with non-trailer lines in trailer block",
- "subject: foo bar\n"
- "\n"
- /*
- * Even though this trailer block has a non-trailer line
- * in it, it's still a valid trailer block because it's
- * at least 25% trailers and is Git-generated (see
- * git_generated_prefixes[] in trailer.c).
- */
- "not a trailer line\n"
- "not a trailer line\n"
- "not a trailer line\n"
- "Signed-off-by: x\n",
- /*
- * Even though there is only really 1 real "trailer"
- * (Signed-off-by), we still have 4 trailer objects
- * because we still want to iterate through the entire
- * block.
- */
- 4,
- {
- {
- .raw = "not a trailer line\n",
- .key = "not a trailer line",
- .val = "",
- },
- {
- .raw = "not a trailer line\n",
- .key = "not a trailer line",
- .val = "",
- },
- {
- .raw = "not a trailer line\n",
- .key = "not a trailer line",
- .val = "",
- },
- {
- .raw = "Signed-off-by: x\n",
- .key = "Signed-off-by",
- .val = "x",
- },
- {
- 0
- },
- },
- },
- {
- "with non-trailer lines (one too many) in trailer block",
- "subject: foo bar\n"
- "\n"
- /*
- * This block has only 20% trailers, so it's below the
- * 25% threshold.
- */
- "not a trailer line\n"
- "not a trailer line\n"
- "not a trailer line\n"
- "not a trailer line\n"
- "Signed-off-by: x\n",
- 0,
- {{0}},
- },
- {
- "with non-trailer lines (only 1) in trailer block, but no Git-generated trailers",
- "subject: foo bar\n"
- "\n"
- /*
- * This block has only 1 non-trailer out of 10 (IOW, 90%
- * trailers) but is not considered a trailer block
- * because the 25% threshold only applies to cases where
- * there was a Git-generated trailer.
- */
- "Reviewed-by: x\n"
- "Reviewed-by: x\n"
- "Reviewed-by: x\n"
- "Helped-by: x\n"
- "Helped-by: x\n"
- "Helped-by: x\n"
- "Acked-by: x\n"
- "Acked-by: x\n"
- "Acked-by: x\n"
- "not a trailer line\n",
- 0,
- {{0}},
- },
- };
-
- for (int i = 0; i < sizeof(tc) / sizeof(tc[0]); i++) {
- TEST(t_trailer_iterator(tc[i].msg,
- tc[i].num_expected,
- tc[i].contents),
- "%s", tc[i].name);
- }
-}
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- run_t_trailer_iterator();
- return test_done();
-}
diff --git a/t/unit-tests/u-oid-array.c b/t/unit-tests/u-oid-array.c
new file mode 100644
index 0000000000..e48a433f21
--- /dev/null
+++ b/t/unit-tests/u-oid-array.c
@@ -0,0 +1,129 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
+#include "unit-test.h"
+#include "lib-oid.h"
+#include "oid-array.h"
+#include "hex.h"
+
+static void fill_array(struct oid_array *array, const char *hexes[], size_t n)
+{
+ for (size_t i = 0; i < n; i++) {
+ struct object_id oid;
+
+ cl_parse_any_oid(hexes[i], &oid);
+ oid_array_append(array, &oid);
+ }
+ cl_assert_equal_i(array->nr, n);
+}
+
+static int add_to_oid_array(const struct object_id *oid, void *data)
+{
+ struct oid_array *array = data;
+
+ oid_array_append(array, oid);
+ return 0;
+}
+
+static void t_enumeration(const char **input_args, size_t input_sz,
+ const char **expect_args, size_t expect_sz)
+{
+ struct oid_array input = OID_ARRAY_INIT, expect = OID_ARRAY_INIT,
+ actual = OID_ARRAY_INIT;
+ size_t i;
+
+ fill_array(&input, input_args, input_sz);
+ fill_array(&expect, expect_args, expect_sz);
+
+ oid_array_for_each_unique(&input, add_to_oid_array, &actual);
+ cl_assert_equal_i(actual.nr, expect.nr);
+
+ for (i = 0; i < actual.nr; i++)
+ cl_assert(oideq(&actual.oid[i], &expect.oid[i]));
+
+ oid_array_clear(&actual);
+ oid_array_clear(&input);
+ oid_array_clear(&expect);
+}
+
+#define TEST_ENUMERATION(input, expect) \
+ t_enumeration(input, ARRAY_SIZE(input), expect, ARRAY_SIZE(expect));
+
+static void t_lookup(const char **input_hexes, size_t n, const char *query_hex,
+ int lower_bound, int upper_bound)
+{
+ struct oid_array array = OID_ARRAY_INIT;
+ struct object_id oid_query;
+ int ret;
+
+ cl_parse_any_oid(query_hex, &oid_query);
+ fill_array(&array, input_hexes, n);
+ ret = oid_array_lookup(&array, &oid_query);
+
+ cl_assert(ret <= upper_bound);
+ cl_assert(ret >= lower_bound);
+
+ oid_array_clear(&array);
+}
+
+#define TEST_LOOKUP(input_hexes, query, lower_bound, upper_bound) \
+ t_lookup(input_hexes, ARRAY_SIZE(input_hexes), query, \
+ lower_bound, upper_bound);
+
+void test_oid_array__initialize(void)
+{
+ /* The hash algo is used by oid_array_lookup() internally */
+ int algo = cl_setup_hash_algo();
+ repo_set_hash_algo(the_repository, algo);
+}
+
+static const char *arr_input[] = { "88", "44", "aa", "55" };
+static const char *arr_input_dup[] = { "88", "44", "aa", "55",
+ "88", "44", "aa", "55",
+ "88", "44", "aa", "55" };
+static const char *res_sorted[] = { "44", "55", "88", "aa" };
+
+void test_oid_array__enumerate_unique(void)
+{
+ TEST_ENUMERATION(arr_input, res_sorted);
+}
+
+void test_oid_array__enumerate_duplicate(void)
+{
+ TEST_ENUMERATION(arr_input_dup, res_sorted);
+}
+
+void test_oid_array__lookup(void)
+{
+ TEST_LOOKUP(arr_input, "55", 1, 1);
+}
+
+void test_oid_array__lookup_non_existent(void)
+{
+ TEST_LOOKUP(arr_input, "33", INT_MIN, -1);
+}
+
+void test_oid_array__lookup_duplicates(void)
+{
+ TEST_LOOKUP(arr_input_dup, "55", 3, 5);
+}
+
+void test_oid_array__lookup_non_existent_dup(void)
+{
+ TEST_LOOKUP(arr_input_dup, "66", INT_MIN, -1);
+}
+
+void test_oid_array__lookup_almost_dup(void)
+{
+ const char *nearly_55;
+
+ nearly_55 = cl_setup_hash_algo() == GIT_HASH_SHA1 ?
+ "5500000000000000000000000000000000000001" :
+ "5500000000000000000000000000000000000000000000000000000000000001";
+
+ TEST_LOOKUP(((const char *[]){ "55", nearly_55 }), "55", 0, 0);
+}
+
+void test_oid_array__lookup_single_dup(void)
+{
+ TEST_LOOKUP(((const char *[]){ "55", "55" }), "55", 0, 1);
+}
diff --git a/t/unit-tests/u-oidmap.c b/t/unit-tests/u-oidmap.c
new file mode 100644
index 0000000000..dc805b7e3c
--- /dev/null
+++ b/t/unit-tests/u-oidmap.c
@@ -0,0 +1,136 @@
+#include "unit-test.h"
+#include "lib-oid.h"
+#include "oidmap.h"
+#include "hash.h"
+#include "hex.h"
+
+/*
+ * Elements we will put in oidmap structs are made of a key: the entry.oid
+ * field, which is of type struct object_id, and a value: the name field (could
+ * be a refname for example).
+ */
+struct test_entry {
+ struct oidmap_entry entry;
+ char name[FLEX_ARRAY];
+};
+
+static const char *const key_val[][2] = { { "11", "one" },
+ { "22", "two" },
+ { "33", "three" } };
+
+static struct oidmap map;
+
+void test_oidmap__initialize(void)
+{
+ oidmap_init(&map, 0);
+
+ for (size_t i = 0; i < ARRAY_SIZE(key_val); i++){
+ struct test_entry *entry;
+
+ FLEX_ALLOC_STR(entry, name, key_val[i][1]);
+ cl_parse_any_oid(key_val[i][0], &entry->entry.oid);
+ cl_assert(oidmap_put(&map, entry) == NULL);
+ }
+}
+
+void test_oidmap__cleanup(void)
+{
+ oidmap_free(&map, 1);
+}
+
+void test_oidmap__replace(void)
+{
+ struct test_entry *entry, *prev;
+
+ FLEX_ALLOC_STR(entry, name, "un");
+ cl_parse_any_oid("11", &entry->entry.oid);
+ prev = oidmap_put(&map, entry);
+ cl_assert(prev != NULL);
+ cl_assert_equal_s(prev->name, "one");
+ free(prev);
+
+ FLEX_ALLOC_STR(entry, name, "deux");
+ cl_parse_any_oid("22", &entry->entry.oid);
+ prev = oidmap_put(&map, entry);
+ cl_assert(prev != NULL);
+ cl_assert_equal_s(prev->name, "two");
+ free(prev);
+}
+
+void test_oidmap__get(void)
+{
+ struct test_entry *entry;
+ struct object_id oid;
+
+ cl_parse_any_oid("22", &oid);
+ entry = oidmap_get(&map, &oid);
+ cl_assert(entry != NULL);
+ cl_assert_equal_s(entry->name, "two");
+
+ cl_parse_any_oid("44", &oid);
+ cl_assert(oidmap_get(&map, &oid) == NULL);
+
+ cl_parse_any_oid("11", &oid);
+ entry = oidmap_get(&map, &oid);
+ cl_assert(entry != NULL);
+ cl_assert_equal_s(entry->name, "one");
+}
+
+void test_oidmap__remove(void)
+{
+ struct test_entry *entry;
+ struct object_id oid;
+
+ cl_parse_any_oid("11", &oid);
+ entry = oidmap_remove(&map, &oid);
+ cl_assert(entry != NULL);
+ cl_assert_equal_s(entry->name, "one");
+ cl_assert(oidmap_get(&map, &oid) == NULL);
+ free(entry);
+
+ cl_parse_any_oid("22", &oid);
+ entry = oidmap_remove(&map, &oid);
+ cl_assert(entry != NULL);
+ cl_assert_equal_s(entry->name, "two");
+ cl_assert(oidmap_get(&map, &oid) == NULL);
+ free(entry);
+
+ cl_parse_any_oid("44", &oid);
+ cl_assert(oidmap_remove(&map, &oid) == NULL);
+}
+
+static int key_val_contains(struct test_entry *entry, char seen[])
+{
+ for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
+ struct object_id oid;
+
+ cl_parse_any_oid(key_val[i][0], &oid);
+
+ if (oideq(&entry->entry.oid, &oid)) {
+ if (seen[i])
+ return 2;
+ seen[i] = 1;
+ return 0;
+ }
+ }
+ return 1;
+}
+
+void test_oidmap__iterate(void)
+{
+ struct oidmap_iter iter;
+ struct test_entry *entry;
+ char seen[ARRAY_SIZE(key_val)] = { 0 };
+ int count = 0;
+
+ oidmap_iter_init(&map, &iter);
+ while ((entry = oidmap_iter_next(&iter))) {
+ if (key_val_contains(entry, seen) != 0) {
+ cl_failf("Unexpected entry: name = %s, oid = %s",
+ entry->name, oid_to_hex(&entry->entry.oid));
+ }
+ count++;
+ }
+ cl_assert_equal_i(count, ARRAY_SIZE(key_val));
+ cl_assert_equal_i(hashmap_get_size(&map.map), ARRAY_SIZE(key_val));
+}
diff --git a/t/unit-tests/u-oidtree.c b/t/unit-tests/u-oidtree.c
new file mode 100644
index 0000000000..e6eede2740
--- /dev/null
+++ b/t/unit-tests/u-oidtree.c
@@ -0,0 +1,107 @@
+#include "unit-test.h"
+#include "lib-oid.h"
+#include "oidtree.h"
+#include "hash.h"
+#include "hex.h"
+#include "strvec.h"
+
+static struct oidtree ot;
+
+#define FILL_TREE(tree, ...) \
+ do { \
+ const char *hexes[] = { __VA_ARGS__ }; \
+ if (fill_tree_loc(tree, hexes, ARRAY_SIZE(hexes))) \
+ return; \
+ } while (0)
+
+static int fill_tree_loc(struct oidtree *ot, const char *hexes[], size_t n)
+{
+ for (size_t i = 0; i < n; i++) {
+ struct object_id oid;
+ cl_parse_any_oid(hexes[i], &oid);
+ oidtree_insert(ot, &oid);
+ }
+ return 0;
+}
+
+static void check_contains(struct oidtree *ot, const char *hex, int expected)
+{
+ struct object_id oid;
+
+ cl_parse_any_oid(hex, &oid);
+ cl_assert_equal_i(oidtree_contains(ot, &oid), expected);
+}
+
+struct expected_hex_iter {
+ size_t i;
+ struct strvec expected_hexes;
+ const char *query;
+};
+
+static enum cb_next check_each_cb(const struct object_id *oid, void *data)
+{
+ struct expected_hex_iter *hex_iter = data;
+ struct object_id expected;
+
+ cl_assert(hex_iter->i < hex_iter->expected_hexes.nr);
+
+ cl_parse_any_oid(hex_iter->expected_hexes.v[hex_iter->i],
+ &expected);
+ cl_assert_equal_s(oid_to_hex(oid), oid_to_hex(&expected));
+ hex_iter->i += 1;
+ return CB_CONTINUE;
+}
+
+LAST_ARG_MUST_BE_NULL
+static void check_each(struct oidtree *ot, const char *query, ...)
+{
+ struct object_id oid;
+ struct expected_hex_iter hex_iter = { .expected_hexes = STRVEC_INIT,
+ .query = query };
+ const char *arg;
+ va_list hex_args;
+
+ va_start(hex_args, query);
+ while ((arg = va_arg(hex_args, const char *)))
+ strvec_push(&hex_iter.expected_hexes, arg);
+ va_end(hex_args);
+
+ cl_parse_any_oid(query, &oid);
+ oidtree_each(ot, &oid, strlen(query), check_each_cb, &hex_iter);
+
+ if (hex_iter.i != hex_iter.expected_hexes.nr)
+ cl_failf("error: could not find some 'object_id's for query ('%s')", query);
+
+ strvec_clear(&hex_iter.expected_hexes);
+}
+
+void test_oidtree__initialize(void)
+{
+ oidtree_init(&ot);
+}
+
+void test_oidtree__cleanup(void)
+{
+ oidtree_clear(&ot);
+}
+
+void test_oidtree__contains(void)
+{
+ FILL_TREE(&ot, "444", "1", "2", "3", "4", "5", "a", "b", "c", "d", "e");
+ check_contains(&ot, "44", 0);
+ check_contains(&ot, "441", 0);
+ check_contains(&ot, "440", 0);
+ check_contains(&ot, "444", 1);
+ check_contains(&ot, "4440", 1);
+ check_contains(&ot, "4444", 0);
+}
+
+void test_oidtree__each(void)
+{
+ FILL_TREE(&ot, "f", "9", "8", "123", "321", "320", "a", "b", "c", "d", "e");
+ check_each(&ot, "12300", "123", NULL);
+ check_each(&ot, "3211", NULL); /* should not reach callback */
+ check_each(&ot, "3210", "321", NULL);
+ check_each(&ot, "32100", "321", NULL);
+ check_each(&ot, "32", "320", "321", NULL);
+}
diff --git a/t/unit-tests/u-trailer.c b/t/unit-tests/u-trailer.c
new file mode 100644
index 0000000000..3d60ea1603
--- /dev/null
+++ b/t/unit-tests/u-trailer.c
@@ -0,0 +1,320 @@
+#define DISABLE_SIGN_COMPARE_WARNINGS
+
+#include "unit-test.h"
+#include "trailer.h"
+
+struct contents {
+ const char *raw;
+ const char *key;
+ const char *val;
+};
+
+static void t_trailer_iterator(const char *msg, size_t num_expected,
+ struct contents *contents)
+{
+ struct trailer_iterator iter;
+ size_t i = 0;
+
+ trailer_iterator_init(&iter, msg);
+ while (trailer_iterator_advance(&iter)) {
+ if (num_expected) {
+ cl_assert_equal_s(iter.raw, contents[i].raw);
+ cl_assert_equal_s(iter.key.buf, contents[i].key);
+ cl_assert_equal_s(iter.val.buf, contents[i].val);
+ }
+ i++;
+ }
+ trailer_iterator_release(&iter);
+
+ cl_assert_equal_i(i, num_expected);
+}
+
+void test_trailer__empty_input(void)
+{
+ struct contents expected_contents[] = { 0 };
+ t_trailer_iterator("", 0, expected_contents);
+}
+
+void test_trailer__no_newline_start(void)
+{
+ struct contents expected_contents[] = { 0 };
+
+ t_trailer_iterator("Fixes: x\n"
+ "Acked-by: x\n"
+ "Reviewed-by: x\n",
+ 0,
+ expected_contents);
+}
+
+void test_trailer__newline_start(void)
+{
+ struct contents expected_contents[] = {
+ {
+ .raw = "Fixes: x\n",
+ .key = "Fixes",
+ .val = "x",
+ },
+ {
+ .raw = "Acked-by: x\n",
+ .key = "Acked-by",
+ .val = "x",
+ },
+ {
+ .raw = "Reviewed-by: x\n",
+ .key = "Reviewed-by",
+ .val = "x",
+ },
+ {
+ 0
+ },
+ };
+
+ t_trailer_iterator("\n"
+ "Fixes: x\n"
+ "Acked-by: x\n"
+ "Reviewed-by: x\n",
+ 3,
+ expected_contents);
+}
+
+void test_trailer__no_body_text(void)
+{
+ struct contents expected_contents[] = {
+
+ {
+ .raw = "Fixes: x\n",
+ .key = "Fixes",
+ .val = "x",
+ },
+ {
+ .raw = "Acked-by: x\n",
+ .key = "Acked-by",
+ .val = "x",
+ },
+ {
+ .raw = "Reviewed-by: x\n",
+ .key = "Reviewed-by",
+ .val = "x",
+ },
+ {
+ 0
+ },
+ };
+
+ t_trailer_iterator("subject: foo bar\n"
+ "\n"
+ "Fixes: x\n"
+ "Acked-by: x\n"
+ "Reviewed-by: x\n",
+ 3,
+ expected_contents);
+}
+
+void test_trailer__body_text_no_divider(void)
+{
+ struct contents expected_contents[] = {
+ {
+ .raw = "Fixes: x\n",
+ .key = "Fixes",
+ .val = "x",
+ },
+ {
+ .raw = "Acked-by: x\n",
+ .key = "Acked-by",
+ .val = "x",
+ },
+ {
+ .raw = "Reviewed-by: x\n",
+ .key = "Reviewed-by",
+ .val = "x",
+ },
+ {
+ .raw = "Signed-off-by: x\n",
+ .key = "Signed-off-by",
+ .val = "x",
+ },
+ {
+ 0
+ },
+ };
+
+ t_trailer_iterator("my subject\n"
+ "\n"
+ "my body which is long\n"
+ "and contains some special\n"
+ "chars like : = ? !\n"
+ "hello\n"
+ "\n"
+ "Fixes: x\n"
+ "Acked-by: x\n"
+ "Reviewed-by: x\n"
+ "Signed-off-by: x\n",
+ 4,
+ expected_contents);
+}
+
+void test_trailer__body_no_divider_2nd_block(void)
+{
+ struct contents expected_contents[] = {
+ {
+ .raw = "Helped-by: x\n",
+ .key = "Helped-by",
+ .val = "x",
+ },
+ {
+ .raw = "Signed-off-by: x\n",
+ .key = "Signed-off-by",
+ .val = "x",
+ },
+ {
+ 0
+ },
+ };
+
+ t_trailer_iterator("my subject\n"
+ "\n"
+ "my body which is long\n"
+ "and contains some special\n"
+ "chars like : = ? !\n"
+ "hello\n"
+ "\n"
+ "Fixes: x\n"
+ "Acked-by: x\n"
+ "Reviewed-by: x\n"
+ "Signed-off-by: x\n"
+ "\n"
+ /*
+ * Because this is the last trailer block, it takes
+ * precedence over the first one encountered above.
+ */
+ "Helped-by: x\n"
+ "Signed-off-by: x\n",
+ 2,
+ expected_contents);
+}
+
+void test_trailer__body_and_divider(void)
+{
+ struct contents expected_contents[] = {
+ {
+ .raw = "Signed-off-by: x\n",
+ .key = "Signed-off-by",
+ .val = "x",
+ },
+ {
+ 0
+ },
+ };
+
+ t_trailer_iterator("my subject\n"
+ "\n"
+ "my body which is long\n"
+ "and contains some special\n"
+ "chars like : = ? !\n"
+ "hello\n"
+ "\n"
+ "---\n"
+ "\n"
+ /*
+ * This trailer still counts because the iterator
+ * always ignores the divider.
+ */
+ "Signed-off-by: x\n",
+ 1,
+ expected_contents);
+}
+
+void test_trailer__non_trailer_in_block(void)
+{
+ struct contents expected_contents[] = {
+ {
+ .raw = "not a trailer line\n",
+ .key = "not a trailer line",
+ .val = "",
+ },
+ {
+ .raw = "not a trailer line\n",
+ .key = "not a trailer line",
+ .val = "",
+ },
+ {
+ .raw = "not a trailer line\n",
+ .key = "not a trailer line",
+ .val = "",
+ },
+ {
+ .raw = "Signed-off-by: x\n",
+ .key = "Signed-off-by",
+ .val = "x",
+ },
+ {
+ 0
+ },
+ };
+
+ t_trailer_iterator("subject: foo bar\n"
+ "\n"
+ /*
+ * Even though this trailer block has a non-trailer line
+ * in it, it's still a valid trailer block because it's
+ * at least 25% trailers and is Git-generated (see
+ * git_generated_prefixes[] in trailer.c).
+ */
+ "not a trailer line\n"
+ "not a trailer line\n"
+ "not a trailer line\n"
+ "Signed-off-by: x\n",
+ /*
+ * Even though there is only really 1 real "trailer"
+ * (Signed-off-by), we still have 4 trailer objects
+ * because we still want to iterate through the entire
+ * block.
+ */
+ 4,
+ expected_contents);
+}
+
+void test_trailer__too_many_non_trailers(void)
+{
+ struct contents expected_contents[] = { 0 };
+
+ t_trailer_iterator("subject: foo bar\n"
+ "\n"
+ /*
+ * This block has only 20% trailers, so it's below the
+ * 25% threshold.
+ */
+ "not a trailer line\n"
+ "not a trailer line\n"
+ "not a trailer line\n"
+ "not a trailer line\n"
+ "Signed-off-by: x\n",
+ 0,
+ expected_contents);
+}
+
+void test_trailer__one_non_trailer_no_git_trailers(void)
+{
+ struct contents expected_contents[] = { 0 };
+
+ t_trailer_iterator("subject: foo bar\n"
+ "\n"
+ /*
+ * This block has only 1 non-trailer out of 10 (IOW, 90%
+ * trailers) but is not considered a trailer block
+ * because the 25% threshold only applies to cases where
+ * there was a Git-generated trailer.
+ */
+ "Reviewed-by: x\n"
+ "Reviewed-by: x\n"
+ "Reviewed-by: x\n"
+ "Helped-by: x\n"
+ "Helped-by: x\n"
+ "Helped-by: x\n"
+ "Acked-by: x\n"
+ "Acked-by: x\n"
+ "Acked-by: x\n"
+ "not a trailer line\n",
+ 0,
+ expected_contents);
+}
diff --git a/t/unit-tests/t-urlmatch-normalization.c b/t/unit-tests/u-urlmatch-normalization.c
index 1769c357b9..39f6e1ba26 100644
--- a/t/unit-tests/t-urlmatch-normalization.c
+++ b/t/unit-tests/u-urlmatch-normalization.c
@@ -1,12 +1,11 @@
-#include "test-lib.h"
+#include "unit-test.h"
#include "urlmatch.h"
static void check_url_normalizable(const char *url, unsigned int normalizable)
{
char *url_norm = url_normalize(url, NULL);
- if (!check_int(normalizable, ==, url_norm ? 1 : 0))
- test_msg("input url: %s", url);
+ cl_assert_equal_i(normalizable, url_norm ? 1 : 0);
free(url_norm);
}
@@ -14,8 +13,7 @@ static void check_normalized_url(const char *url, const char *expect)
{
char *url_norm = url_normalize(url, NULL);
- if (!check_str(url_norm, expect))
- test_msg("input url: %s", url);
+ cl_assert_equal_s(url_norm, expect);
free(url_norm);
}
@@ -26,13 +24,9 @@ static void compare_normalized_urls(const char *url1, const char *url2,
char *url2_norm = url_normalize(url2, NULL);
if (equal) {
- if (!check_str(url1_norm, url2_norm))
- test_msg("input url1: %s\n input url2: %s", url1,
- url2);
- } else if (!check_int(strcmp(url1_norm, url2_norm), !=, 0)) {
- test_msg(" normalized url1: %s\n normalized url2: %s\n"
- " input url1: %s\n input url2: %s",
- url1_norm, url2_norm, url1, url2);
+ cl_assert_equal_s(url1_norm, url2_norm);
+ } else {
+ cl_assert(strcmp(url1_norm, url2_norm) != 0);
}
free(url1_norm);
free(url2_norm);
@@ -43,14 +37,12 @@ static void check_normalized_url_length(const char *url, size_t len)
struct url_info info;
char *url_norm = url_normalize(url, &info);
- if (!check_int(info.url_len, ==, len))
- test_msg(" input url: %s\n normalized url: %s", url,
- url_norm);
+ cl_assert_equal_i(info.url_len, len);
free(url_norm);
}
/* Note that only "file:" URLs should be allowed without a host */
-static void t_url_scheme(void)
+void test_urlmatch_normalization__scheme(void)
{
check_url_normalizable("", 0);
check_url_normalizable("_", 0);
@@ -73,7 +65,7 @@ static void t_url_scheme(void)
check_normalized_url("AbCdeF://x.Y", "abcdef://x.y/");
}
-static void t_url_authority(void)
+void test_urlmatch_normalization__authority(void)
{
check_url_normalizable("scheme://user:pass@", 0);
check_url_normalizable("scheme://?", 0);
@@ -109,7 +101,7 @@ static void t_url_authority(void)
check_url_normalizable("scheme://invalid....:[", 0);
}
-static void t_url_port(void)
+void test_urlmatch_normalization__port(void)
{
check_url_normalizable("xyz://q@some.host:", 1);
check_url_normalizable("xyz://q@some.host:456/", 1);
@@ -139,7 +131,7 @@ static void t_url_port(void)
check_url_normalizable("xyz://[::1]:030f/", 0);
}
-static void t_url_port_normalization(void)
+void test_urlmatch_normalization__port_normalization(void)
{
check_normalized_url("http://x:800", "http://x:800/");
check_normalized_url("http://x:0800", "http://x:800/");
@@ -154,7 +146,7 @@ static void t_url_port_normalization(void)
check_normalized_url("https://x:000000443", "https://x/");
}
-static void t_url_general_escape(void)
+void test_urlmatch_normalization__general_escape(void)
{
check_url_normalizable("http://x.y?%fg", 0);
check_normalized_url("X://W/%7e%41^%3a", "x://w/~A%5E%3A");
@@ -164,7 +156,7 @@ static void t_url_general_escape(void)
check_normalized_url("X://W?!", "x://w/?!");
}
-static void t_url_high_bit(void)
+void test_urlmatch_normalization__high_bit(void)
{
check_normalized_url(
"x://q/\x01\x02\x03\x04\x05\x06\x07\x08\x0e\x0f\x10\x11\x12",
@@ -198,26 +190,26 @@ static void t_url_high_bit(void)
"x://q/%F0%F1%F2%F3%F4%F5%F6%F7%F8%F9%FA%FB%FC%FD%FE%FF");
}
-static void t_url_utf8_escape(void)
+void test_urlmatch_normalization__utf8_escape(void)
{
check_normalized_url(
"x://q/\xc2\x80\xdf\xbf\xe0\xa0\x80\xef\xbf\xbd\xf0\x90\x80\x80\xf0\xaf\xbf\xbd",
"x://q/%C2%80%DF%BF%E0%A0%80%EF%BF%BD%F0%90%80%80%F0%AF%BF%BD");
}
-static void t_url_username_pass(void)
+void test_urlmatch_normalization__username_pass(void)
{
check_normalized_url("x://%41%62(^):%70+d@foo", "x://Ab(%5E):p+d@foo/");
}
-static void t_url_length(void)
+void test_urlmatch_normalization__length(void)
{
check_normalized_url_length("Http://%4d%65:%4d^%70@The.Host", 25);
check_normalized_url_length("http://%41:%42@x.y/%61/", 17);
check_normalized_url_length("http://@x.y/^", 15);
}
-static void t_url_dots(void)
+void test_urlmatch_normalization__dots(void)
{
check_normalized_url("x://y/.", "x://y/");
check_normalized_url("x://y/./", "x://y/");
@@ -244,7 +236,7 @@ static void t_url_dots(void)
* "http://foo" specifies neither a user name nor a password.
* So they should not be equivalent.
*/
-static void t_url_equivalents(void)
+void test_urlmatch_normalization__equivalents(void)
{
compare_normalized_urls("httP://x", "Http://X/", 1);
compare_normalized_urls("Http://%4d%65:%4d^%70@The.Host", "hTTP://Me:%4D^p@the.HOST:80/", 1);
@@ -253,19 +245,3 @@ static void t_url_equivalents(void)
compare_normalized_urls("https://@x.y/^/../abc", "httpS://@x.y:0443/abc", 1);
compare_normalized_urls("https://@x.y/^/..", "httpS://@x.y:0443/", 1);
}
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- TEST(t_url_scheme(), "url scheme");
- TEST(t_url_authority(), "url authority");
- TEST(t_url_port(), "url port checks");
- TEST(t_url_port_normalization(), "url port normalization");
- TEST(t_url_general_escape(), "url general escapes");
- TEST(t_url_high_bit(), "url high-bit escapes");
- TEST(t_url_utf8_escape(), "url utf8 escapes");
- TEST(t_url_username_pass(), "url username/password escapes");
- TEST(t_url_length(), "url normalized lengths");
- TEST(t_url_dots(), "url . and .. segments");
- TEST(t_url_equivalents(), "url equivalents");
- return test_done();
-}
diff --git a/t/unit-tests/unit-test.c b/t/unit-tests/unit-test.c
index fa8818842a..5af645048a 100644
--- a/t/unit-tests/unit-test.c
+++ b/t/unit-tests/unit-test.c
@@ -1,5 +1,7 @@
#include "unit-test.h"
+#include "hex.h"
#include "parse-options.h"
+#include "strbuf.h"
#include "string-list.h"
#include "strvec.h"