summaryrefslogtreecommitdiff
path: root/t/unit-tests
diff options
context:
space:
mode:
Diffstat (limited to 't/unit-tests')
-rwxr-xr-xt/unit-tests/generate-clar-decls.sh1
-rw-r--r--t/unit-tests/t-example-decorate.c74
-rw-r--r--t/unit-tests/t-mem-pool.c31
-rw-r--r--t/unit-tests/t-prio-queue.c91
-rw-r--r--t/unit-tests/t-reftable-basics.c2
-rw-r--r--t/unit-tests/t-reftable-readwrite.c8
-rw-r--r--t/unit-tests/t-reftable-record.c19
-rw-r--r--t/unit-tests/t-reftable-stack.c54
-rw-r--r--t/unit-tests/t-strbuf.c122
-rw-r--r--t/unit-tests/t-strcmp-offset.c35
-rw-r--r--t/unit-tests/u-example-decorate.c64
-rw-r--r--t/unit-tests/u-hash.c (renamed from t/unit-tests/t-hash.c)77
-rw-r--r--t/unit-tests/u-hashmap.c (renamed from t/unit-tests/t-hashmap.c)226
-rw-r--r--t/unit-tests/u-mem-pool.c25
-rw-r--r--t/unit-tests/u-prio-queue.c94
-rw-r--r--t/unit-tests/u-reftable-tree.c (renamed from t/unit-tests/t-reftable-tree.c)30
-rw-r--r--t/unit-tests/u-strbuf.c119
-rw-r--r--t/unit-tests/u-strcmp-offset.c45
18 files changed, 596 insertions, 521 deletions
diff --git a/t/unit-tests/generate-clar-decls.sh b/t/unit-tests/generate-clar-decls.sh
index 3b315c64b3..abf6a2ea2a 100755
--- a/t/unit-tests/generate-clar-decls.sh
+++ b/t/unit-tests/generate-clar-decls.sh
@@ -14,6 +14,7 @@ do
suite_name=$(basename "$suite")
suite_name=${suite_name%.c}
suite_name=${suite_name#u-}
+ suite_name=$(echo "$suite_name" | tr '-' '_')
sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\)$/extern \1;/p" "$suite" ||
exit 1
done >"$OUTPUT"
diff --git a/t/unit-tests/t-example-decorate.c b/t/unit-tests/t-example-decorate.c
deleted file mode 100644
index bfc776e223..0000000000
--- a/t/unit-tests/t-example-decorate.c
+++ /dev/null
@@ -1,74 +0,0 @@
-#define USE_THE_REPOSITORY_VARIABLE
-
-#include "test-lib.h"
-#include "object.h"
-#include "decorate.h"
-#include "repository.h"
-
-struct test_vars {
- struct object *one, *two, *three;
- struct decoration n;
- int decoration_a, decoration_b;
-};
-
-static void t_add(struct test_vars *vars)
-{
- void *ret = add_decoration(&vars->n, vars->one, &vars->decoration_a);
-
- check(ret == NULL);
- ret = add_decoration(&vars->n, vars->two, NULL);
- check(ret == NULL);
-}
-
-static void t_readd(struct test_vars *vars)
-{
- void *ret = add_decoration(&vars->n, vars->one, NULL);
-
- check(ret == &vars->decoration_a);
- ret = add_decoration(&vars->n, vars->two, &vars->decoration_b);
- check(ret == NULL);
-}
-
-static void t_lookup(struct test_vars *vars)
-{
- void *ret = lookup_decoration(&vars->n, vars->one);
-
- check(ret == NULL);
- ret = lookup_decoration(&vars->n, vars->two);
- check(ret == &vars->decoration_b);
- ret = lookup_decoration(&vars->n, vars->three);
- check(ret == NULL);
-}
-
-static void t_loop(struct test_vars *vars)
-{
- int objects_noticed = 0;
-
- for (size_t i = 0; i < vars->n.size; i++) {
- if (vars->n.entries[i].base)
- objects_noticed++;
- }
- check_int(objects_noticed, ==, 2);
-}
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } };
- struct test_vars vars = { 0 };
-
- vars.one = lookup_unknown_object(the_repository, &one_oid);
- vars.two = lookup_unknown_object(the_repository, &two_oid);
- vars.three = lookup_unknown_object(the_repository, &three_oid);
-
- TEST(t_add(&vars),
- "Add 2 objects, one with a non-NULL decoration and one with a NULL decoration.");
- TEST(t_readd(&vars),
- "When re-adding an already existing object, the old decoration is returned.");
- TEST(t_lookup(&vars),
- "Lookup returns the added declarations, or NULL if the object was never added.");
- TEST(t_loop(&vars), "The user can also loop through all entries.");
-
- clear_decoration(&vars.n, NULL);
-
- return test_done();
-}
diff --git a/t/unit-tests/t-mem-pool.c b/t/unit-tests/t-mem-pool.c
deleted file mode 100644
index fe500c704b..0000000000
--- a/t/unit-tests/t-mem-pool.c
+++ /dev/null
@@ -1,31 +0,0 @@
-#include "test-lib.h"
-#include "mem-pool.h"
-
-static void setup_static(void (*f)(struct mem_pool *), size_t block_alloc)
-{
- struct mem_pool pool = { .block_alloc = block_alloc };
- f(&pool);
- mem_pool_discard(&pool, 0);
-}
-
-static void t_calloc_100(struct mem_pool *pool)
-{
- size_t size = 100;
- char *buffer = mem_pool_calloc(pool, 1, size);
- for (size_t i = 0; i < size; i++)
- check_int(buffer[i], ==, 0);
- if (!check(pool->mp_block != NULL))
- return;
- check(pool->mp_block->next_free != NULL);
- check(pool->mp_block->end != NULL);
-}
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- TEST(setup_static(t_calloc_100, 1024 * 1024),
- "mem_pool_calloc returns 100 zeroed bytes with big block");
- TEST(setup_static(t_calloc_100, 1),
- "mem_pool_calloc returns 100 zeroed bytes with tiny block");
-
- return test_done();
-}
diff --git a/t/unit-tests/t-prio-queue.c b/t/unit-tests/t-prio-queue.c
deleted file mode 100644
index a053635000..0000000000
--- a/t/unit-tests/t-prio-queue.c
+++ /dev/null
@@ -1,91 +0,0 @@
-#include "test-lib.h"
-#include "prio-queue.h"
-
-static int intcmp(const void *va, const void *vb, void *data UNUSED)
-{
- const int *a = va, *b = vb;
- return *a - *b;
-}
-
-
-#define MISSING -1
-#define DUMP -2
-#define STACK -3
-#define GET -4
-#define REVERSE -5
-
-static int show(int *v)
-{
- return v ? *v : MISSING;
-}
-
-static void test_prio_queue(int *input, size_t input_size,
- int *result, size_t result_size)
-{
- struct prio_queue pq = { intcmp };
- int j = 0;
-
- for (size_t i = 0; i < input_size; i++) {
- void *peek, *get;
- switch(input[i]) {
- case GET:
- peek = prio_queue_peek(&pq);
- get = prio_queue_get(&pq);
- if (!check(peek == get))
- return;
- if (!check_uint(j, <, result_size))
- break;
- if (!check_int(result[j], ==, show(get)))
- test_msg(" j: %d", j);
- j++;
- break;
- case DUMP:
- while ((peek = prio_queue_peek(&pq))) {
- get = prio_queue_get(&pq);
- if (!check(peek == get))
- return;
- if (!check_uint(j, <, result_size))
- break;
- if (!check_int(result[j], ==, show(get)))
- test_msg(" j: %d", j);
- j++;
- }
- break;
- case STACK:
- pq.compare = NULL;
- break;
- case REVERSE:
- prio_queue_reverse(&pq);
- break;
- default:
- prio_queue_put(&pq, &input[i]);
- break;
- }
- }
- check_uint(j, ==, result_size);
- clear_prio_queue(&pq);
-}
-
-#define TEST_INPUT(input, result) \
- test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result))
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- TEST(TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }),
- ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })),
- "prio-queue works for basic input");
- TEST(TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }),
- ((int []){ 2, 3, 4, 1, 5, 6 })),
- "prio-queue works for mixed put & get commands");
- TEST(TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }),
- ((int []){ 1, 2, MISSING, 1, 2, MISSING })),
- "prio-queue works when queue is empty");
- TEST(TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }),
- ((int []){ 3, 2, 6, 4, 5, 1, 8 })),
- "prio-queue works when used as a LIFO stack");
- TEST(TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }),
- ((int []){ 1, 2, 3, 4, 5, 6 })),
- "prio-queue works when LIFO stack is reversed");
-
- return test_done();
-}
diff --git a/t/unit-tests/t-reftable-basics.c b/t/unit-tests/t-reftable-basics.c
index 1d640b280f..9ba7eb05ad 100644
--- a/t/unit-tests/t-reftable-basics.c
+++ b/t/unit-tests/t-reftable-basics.c
@@ -120,7 +120,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
for (size_t i = 0; i < ARRAY_SIZE(cases); i++) {
check(!reftable_buf_addstr(&a, cases[i].a));
check(!reftable_buf_addstr(&b, cases[i].b));
- check_int(common_prefix_size(&a, &b), ==, cases[i].want);
+ check_uint(common_prefix_size(&a, &b), ==, cases[i].want);
reftable_buf_reset(&a);
reftable_buf_reset(&b);
}
diff --git a/t/unit-tests/t-reftable-readwrite.c b/t/unit-tests/t-reftable-readwrite.c
index 6b75a419b9..c9626831da 100644
--- a/t/unit-tests/t-reftable-readwrite.c
+++ b/t/unit-tests/t-reftable-readwrite.c
@@ -108,8 +108,8 @@ static void t_log_buffer_size(void)
hash, to ensure that the compressed part is larger than the original.
*/
for (i = 0; i < REFTABLE_HASH_SIZE_SHA1; i++) {
- log.value.update.old_hash[i] = (uint8_t)(git_rand() % 256);
- log.value.update.new_hash[i] = (uint8_t)(git_rand() % 256);
+ log.value.update.old_hash[i] = (uint8_t)(git_rand(0) % 256);
+ log.value.update.new_hash[i] = (uint8_t)(git_rand(0) % 256);
}
reftable_writer_set_limits(w, update_index, update_index);
err = reftable_writer_add_log(w, &log);
@@ -325,7 +325,7 @@ static void t_log_zlib_corruption(void)
};
for (i = 0; i < sizeof(message) - 1; i++)
- message[i] = (uint8_t)(git_rand() % 64 + ' ');
+ message[i] = (uint8_t)(git_rand(0) % 64 + ' ');
reftable_writer_set_limits(w, 1, 1);
@@ -643,7 +643,7 @@ static void t_write_empty_table(void)
check_int(err, ==, REFTABLE_EMPTY_TABLE_ERROR);
reftable_writer_free(w);
- check_int(buf.len, ==, header_size(1) + footer_size(1));
+ check_uint(buf.len, ==, header_size(1) + footer_size(1));
block_source_from_buf(&source, &buf);
diff --git a/t/unit-tests/t-reftable-record.c b/t/unit-tests/t-reftable-record.c
index 42bc64cec8..d49d2a2729 100644
--- a/t/unit-tests/t-reftable-record.c
+++ b/t/unit-tests/t-reftable-record.c
@@ -58,9 +58,25 @@ static void t_varint_roundtrip(void)
}
}
+static void t_varint_overflow(void)
+{
+ unsigned char buf[] = {
+ 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0x00,
+ };
+ struct string_view view = {
+ .buf = buf,
+ .len = sizeof(buf),
+ };
+ uint64_t value;
+ int err = get_var_int(&value, &view);
+ check_int(err, ==, -1);
+}
+
static void set_hash(uint8_t *h, int j)
{
- for (int i = 0; i < hash_size(REFTABLE_HASH_SHA1); i++)
+ for (size_t i = 0; i < hash_size(REFTABLE_HASH_SHA1); i++)
h[i] = (j >> i) & 0xff;
}
@@ -544,6 +560,7 @@ int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
TEST(t_reftable_log_record_roundtrip(), "record operations work on log record");
TEST(t_reftable_ref_record_roundtrip(), "record operations work on ref record");
TEST(t_varint_roundtrip(), "put_var_int and get_var_int work");
+ TEST(t_varint_overflow(), "get_var_int notices an integer overflow");
TEST(t_key_roundtrip(), "reftable_encode_key and reftable_decode_key work");
TEST(t_reftable_obj_record_roundtrip(), "record operations work on obj record");
TEST(t_reftable_index_record_roundtrip(), "record operations work on index record");
diff --git a/t/unit-tests/t-reftable-stack.c b/t/unit-tests/t-reftable-stack.c
index aeec195b2b..c3f0059c34 100644
--- a/t/unit-tests/t-reftable-stack.c
+++ b/t/unit-tests/t-reftable-stack.c
@@ -103,7 +103,8 @@ static void t_read_file(void)
static int write_test_ref(struct reftable_writer *wr, void *arg)
{
struct reftable_ref_record *ref = arg;
- reftable_writer_set_limits(wr, ref->update_index, ref->update_index);
+ check(!reftable_writer_set_limits(wr, ref->update_index,
+ ref->update_index));
return reftable_writer_add_ref(wr, ref);
}
@@ -143,7 +144,8 @@ static int write_test_log(struct reftable_writer *wr, void *arg)
{
struct write_log_arg *wla = arg;
- reftable_writer_set_limits(wr, wla->update_index, wla->update_index);
+ check(!reftable_writer_set_limits(wr, wla->update_index,
+ wla->update_index));
return reftable_writer_add_log(wr, wla->log);
}
@@ -961,7 +963,7 @@ static void t_reflog_expire(void)
static int write_nothing(struct reftable_writer *wr, void *arg UNUSED)
{
- reftable_writer_set_limits(wr, 1, 1);
+ check(!reftable_writer_set_limits(wr, 1, 1));
return 0;
}
@@ -1369,11 +1371,57 @@ static void t_reftable_stack_reload_with_missing_table(void)
clear_dir(dir);
}
+static int write_limits_after_ref(struct reftable_writer *wr, void *arg)
+{
+ struct reftable_ref_record *ref = arg;
+ check(!reftable_writer_set_limits(wr, ref->update_index, ref->update_index));
+ check(!reftable_writer_add_ref(wr, ref));
+ return reftable_writer_set_limits(wr, ref->update_index, ref->update_index);
+}
+
+static void t_reftable_invalid_limit_updates(void)
+{
+ struct reftable_ref_record ref = {
+ .refname = (char *) "HEAD",
+ .update_index = 1,
+ .value_type = REFTABLE_REF_SYMREF,
+ .value.symref = (char *) "master",
+ };
+ struct reftable_write_options opts = {
+ .default_permissions = 0660,
+ };
+ struct reftable_addition *add = NULL;
+ char *dir = get_tmp_dir(__LINE__);
+ struct reftable_stack *st = NULL;
+ int err;
+
+ err = reftable_new_stack(&st, dir, &opts);
+ check(!err);
+
+ reftable_addition_destroy(add);
+
+ err = reftable_stack_new_addition(&add, st, 0);
+ check(!err);
+
+ /*
+ * write_limits_after_ref also updates the update indexes after adding
+ * the record. This should cause an err to be returned, since the limits
+ * must be set at the start.
+ */
+ err = reftable_addition_add(add, write_limits_after_ref, &ref);
+ check_int(err, ==, REFTABLE_API_ERROR);
+
+ reftable_addition_destroy(add);
+ reftable_stack_destroy(st);
+ clear_dir(dir);
+}
+
int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
{
TEST(t_empty_add(), "empty addition to stack");
TEST(t_read_file(), "read_lines works");
TEST(t_reflog_expire(), "expire reflog entries");
+ TEST(t_reftable_invalid_limit_updates(), "prevent limit updates after adding records");
TEST(t_reftable_stack_add(), "add multiple refs and logs to stack");
TEST(t_reftable_stack_add_one(), "add a single ref record to stack");
TEST(t_reftable_stack_add_performs_auto_compaction(), "addition to stack triggers auto-compaction");
diff --git a/t/unit-tests/t-strbuf.c b/t/unit-tests/t-strbuf.c
deleted file mode 100644
index 3f4044d697..0000000000
--- a/t/unit-tests/t-strbuf.c
+++ /dev/null
@@ -1,122 +0,0 @@
-#include "test-lib.h"
-#include "strbuf.h"
-
-/* wrapper that supplies tests with an empty, initialized strbuf */
-static void setup(void (*f)(struct strbuf*, const void*),
- const void *data)
-{
- struct strbuf buf = STRBUF_INIT;
-
- f(&buf, data);
- strbuf_release(&buf);
- check_uint(buf.len, ==, 0);
- check_uint(buf.alloc, ==, 0);
-}
-
-/* wrapper that supplies tests with a populated, initialized strbuf */
-static void setup_populated(void (*f)(struct strbuf*, const void*),
- const char *init_str, const void *data)
-{
- struct strbuf buf = STRBUF_INIT;
-
- strbuf_addstr(&buf, init_str);
- check_uint(buf.len, ==, strlen(init_str));
- f(&buf, data);
- strbuf_release(&buf);
- check_uint(buf.len, ==, 0);
- check_uint(buf.alloc, ==, 0);
-}
-
-static int assert_sane_strbuf(struct strbuf *buf)
-{
- /* Initialized strbufs should always have a non-NULL buffer */
- if (!check(!!buf->buf))
- return 0;
- /* Buffers should always be NUL-terminated */
- if (!check_char(buf->buf[buf->len], ==, '\0'))
- return 0;
- /*
- * Freshly-initialized strbufs may not have a dynamically allocated
- * buffer
- */
- if (buf->len == 0 && buf->alloc == 0)
- return 1;
- /* alloc must be at least one byte larger than len */
- return check_uint(buf->len, <, buf->alloc);
-}
-
-static void t_static_init(void)
-{
- struct strbuf buf = STRBUF_INIT;
-
- check_uint(buf.len, ==, 0);
- check_uint(buf.alloc, ==, 0);
- check_char(buf.buf[0], ==, '\0');
-}
-
-static void t_dynamic_init(void)
-{
- struct strbuf buf;
-
- strbuf_init(&buf, 1024);
- check(assert_sane_strbuf(&buf));
- check_uint(buf.len, ==, 0);
- check_uint(buf.alloc, >=, 1024);
- check_char(buf.buf[0], ==, '\0');
- strbuf_release(&buf);
-}
-
-static void t_addch(struct strbuf *buf, const void *data)
-{
- const char *p_ch = data;
- const char ch = *p_ch;
- size_t orig_alloc = buf->alloc;
- size_t orig_len = buf->len;
-
- if (!check(assert_sane_strbuf(buf)))
- return;
- strbuf_addch(buf, ch);
- if (!check(assert_sane_strbuf(buf)))
- return;
- if (!(check_uint(buf->len, ==, orig_len + 1) &&
- check_uint(buf->alloc, >=, orig_alloc)))
- return; /* avoid de-referencing buf->buf */
- check_char(buf->buf[buf->len - 1], ==, ch);
- check_char(buf->buf[buf->len], ==, '\0');
-}
-
-static void t_addstr(struct strbuf *buf, const void *data)
-{
- const char *text = data;
- size_t len = strlen(text);
- size_t orig_alloc = buf->alloc;
- size_t orig_len = buf->len;
-
- if (!check(assert_sane_strbuf(buf)))
- return;
- strbuf_addstr(buf, text);
- if (!check(assert_sane_strbuf(buf)))
- return;
- if (!(check_uint(buf->len, ==, orig_len + len) &&
- check_uint(buf->alloc, >=, orig_alloc) &&
- check_uint(buf->alloc, >, orig_len + len) &&
- check_char(buf->buf[orig_len + len], ==, '\0')))
- return;
- check_str(buf->buf + orig_len, text);
-}
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- if (!TEST(t_static_init(), "static initialization works"))
- test_skip_all("STRBUF_INIT is broken");
- TEST(t_dynamic_init(), "dynamic initialization works");
- TEST(setup(t_addch, "a"), "strbuf_addch adds char");
- TEST(setup(t_addch, ""), "strbuf_addch adds NUL char");
- TEST(setup_populated(t_addch, "initial value", "a"),
- "strbuf_addch appends to initial value");
- TEST(setup(t_addstr, "hello there"), "strbuf_addstr adds string");
- TEST(setup_populated(t_addstr, "initial value", "hello there"),
- "strbuf_addstr appends string to initial value");
-
- return test_done();
-}
diff --git a/t/unit-tests/t-strcmp-offset.c b/t/unit-tests/t-strcmp-offset.c
deleted file mode 100644
index 6880f21161..0000000000
--- a/t/unit-tests/t-strcmp-offset.c
+++ /dev/null
@@ -1,35 +0,0 @@
-#include "test-lib.h"
-#include "read-cache-ll.h"
-
-static void check_strcmp_offset(const char *string1, const char *string2,
- int expect_result, uintmax_t expect_offset)
-{
- size_t offset;
- int result = strcmp_offset(string1, string2, &offset);
-
- /*
- * Because different CRTs behave differently, only rely on signs of the
- * result values.
- */
- result = (result < 0 ? -1 :
- result > 0 ? 1 :
- 0);
-
- check_int(result, ==, expect_result);
- check_uint((uintmax_t)offset, ==, expect_offset);
-}
-
-#define TEST_STRCMP_OFFSET(string1, string2, expect_result, expect_offset) \
- TEST(check_strcmp_offset(string1, string2, expect_result, \
- expect_offset), \
- "strcmp_offset(%s, %s) works", #string1, #string2)
-
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
-{
- TEST_STRCMP_OFFSET("abc", "abc", 0, 3);
- TEST_STRCMP_OFFSET("abc", "def", -1, 0);
- TEST_STRCMP_OFFSET("abc", "abz", -1, 2);
- TEST_STRCMP_OFFSET("abc", "abcdef", -1, 3);
-
- return test_done();
-}
diff --git a/t/unit-tests/u-example-decorate.c b/t/unit-tests/u-example-decorate.c
new file mode 100644
index 0000000000..9b1d1ce753
--- /dev/null
+++ b/t/unit-tests/u-example-decorate.c
@@ -0,0 +1,64 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
+#include "unit-test.h"
+#include "object.h"
+#include "decorate.h"
+#include "repository.h"
+
+struct test_vars {
+ struct object *one, *two, *three;
+ struct decoration n;
+ int decoration_a, decoration_b;
+};
+
+static struct test_vars vars;
+
+void test_example_decorate__initialize(void)
+{
+ struct object_id one_oid = { { 1 } }, two_oid = { { 2 } }, three_oid = { { 3 } };
+
+ vars.one = lookup_unknown_object(the_repository, &one_oid);
+ vars.two = lookup_unknown_object(the_repository, &two_oid);
+ vars.three = lookup_unknown_object(the_repository, &three_oid);
+}
+
+void test_example_decorate__cleanup(void)
+{
+ clear_decoration(&vars.n, NULL);
+}
+
+void test_example_decorate__add(void)
+{
+ cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL);
+ cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL);
+}
+
+void test_example_decorate__readd(void)
+{
+ cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL);
+ cl_assert_equal_p(add_decoration(&vars.n, vars.two, NULL), NULL);
+ cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), &vars.decoration_a);
+ cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL);
+}
+
+void test_example_decorate__lookup(void)
+{
+ cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL);
+ cl_assert_equal_p(add_decoration(&vars.n, vars.one, NULL), NULL);
+ cl_assert_equal_p(lookup_decoration(&vars.n, vars.two), &vars.decoration_b);
+ cl_assert_equal_p(lookup_decoration(&vars.n, vars.one), NULL);
+}
+
+void test_example_decorate__loop(void)
+{
+ int objects_noticed = 0;
+
+ cl_assert_equal_p(add_decoration(&vars.n, vars.one, &vars.decoration_a), NULL);
+ cl_assert_equal_p(add_decoration(&vars.n, vars.two, &vars.decoration_b), NULL);
+
+ for (size_t i = 0; i < vars.n.size; i++)
+ if (vars.n.entries[i].base)
+ objects_noticed++;
+
+ cl_assert_equal_i(objects_noticed, 2);
+}
diff --git a/t/unit-tests/t-hash.c b/t/unit-tests/u-hash.c
index e62647019b..bd4ac6a6e1 100644
--- a/t/unit-tests/t-hash.c
+++ b/t/unit-tests/u-hash.c
@@ -1,84 +1,109 @@
-#include "test-lib.h"
+#include "unit-test.h"
#include "hex.h"
#include "strbuf.h"
static void check_hash_data(const void *data, size_t data_length,
const char *expected_hashes[])
{
- if (!check(data != NULL)) {
- test_msg("BUG: NULL data pointer provided");
- return;
- }
+ cl_assert(data != NULL);
for (size_t i = 1; i < ARRAY_SIZE(hash_algos); i++) {
- git_hash_ctx ctx;
+ struct git_hash_ctx ctx;
unsigned char hash[GIT_MAX_HEXSZ];
const struct git_hash_algo *algop = &hash_algos[i];
algop->init_fn(&ctx);
- algop->update_fn(&ctx, data, data_length);
- algop->final_fn(hash, &ctx);
+ git_hash_update(&ctx, data, data_length);
+ git_hash_final(hash, &ctx);
- if (!check_str(hash_to_hex_algop(hash, algop), expected_hashes[i - 1]))
- test_msg("result does not match with the expected for %s\n", hash_algos[i].name);
+ cl_assert_equal_s(hash_to_hex_algop(hash,algop), expected_hashes[i - 1]);
}
}
/* Works with a NUL terminated string. Doesn't work if it should contain a NUL character. */
#define TEST_HASH_STR(data, expected_sha1, expected_sha256) do { \
const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \
- TEST(check_hash_data(data, strlen(data), expected_hashes), \
- "SHA1 and SHA256 (%s) works", #data); \
+ check_hash_data(data, strlen(data), expected_hashes); \
} while (0)
/* Only works with a literal string, useful when it contains a NUL character. */
#define TEST_HASH_LITERAL(literal, expected_sha1, expected_sha256) do { \
const char *expected_hashes[] = { expected_sha1, expected_sha256 }; \
- TEST(check_hash_data(literal, (sizeof(literal) - 1), expected_hashes), \
- "SHA1 and SHA256 (%s) works", #literal); \
+ check_hash_data(literal, (sizeof(literal) - 1), expected_hashes); \
} while (0)
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
+void test_hash__empty_string(void)
{
- struct strbuf aaaaaaaaaa_100000 = STRBUF_INIT;
- struct strbuf alphabet_100000 = STRBUF_INIT;
-
- strbuf_addstrings(&aaaaaaaaaa_100000, "aaaaaaaaaa", 100000);
- strbuf_addstrings(&alphabet_100000, "abcdefghijklmnopqrstuvwxyz", 100000);
-
TEST_HASH_STR("",
"da39a3ee5e6b4b0d3255bfef95601890afd80709",
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855");
+}
+
+void test_hash__single_character(void)
+{
TEST_HASH_STR("a",
"86f7e437faa5a7fce15d1ddcb9eaeaea377667b8",
"ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb");
+}
+
+void test_hash__multi_character(void)
+{
TEST_HASH_STR("abc",
"a9993e364706816aba3e25717850c26c9cd0d89d",
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
+}
+
+void test_hash__message_digest(void)
+{
TEST_HASH_STR("message digest",
"c12252ceda8be8994d5fa0290a47231c1d16aae3",
"f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650");
+}
+
+void test_hash__alphabet(void)
+{
TEST_HASH_STR("abcdefghijklmnopqrstuvwxyz",
"32d10c7b8cf96570ca04ce37f2a19d84240d3a89",
"71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73");
+}
+
+void test_hash__aaaaaaaaaa_100000(void)
+{
+ struct strbuf aaaaaaaaaa_100000 = STRBUF_INIT;
+ strbuf_addstrings(&aaaaaaaaaa_100000, "aaaaaaaaaa", 100000);
TEST_HASH_STR(aaaaaaaaaa_100000.buf,
"34aa973cd4c4daa4f61eeb2bdbad27316534016f",
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
+ strbuf_release(&aaaaaaaaaa_100000);
+}
+
+void test_hash__alphabet_100000(void)
+{
+ struct strbuf alphabet_100000 = STRBUF_INIT;
+ strbuf_addstrings(&alphabet_100000, "abcdefghijklmnopqrstuvwxyz", 100000);
TEST_HASH_STR(alphabet_100000.buf,
"e7da7c55b3484fdf52aebec9cbe7b85a98f02fd4",
"e406ba321ca712ad35a698bf0af8d61fc4dc40eca6bdcea4697962724ccbde35");
+ strbuf_release(&alphabet_100000);
+}
+
+void test_hash__zero_blob_literal(void)
+{
TEST_HASH_LITERAL("blob 0\0",
"e69de29bb2d1d6434b8b29ae775ad8c2e48c5391",
"473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813");
+}
+
+void test_hash__three_blob_literal(void)
+{
TEST_HASH_LITERAL("blob 3\0abc",
"f2ba8f84ab5c1bce84a7b441cb1959cfc7093b7f",
"c1cf6e465077930e88dc5136641d402f72a229ddd996f627d60e9639eaba35a6");
+}
+
+void test_hash__zero_tree_literal(void)
+{
TEST_HASH_LITERAL("tree 0\0",
"4b825dc642cb6eb9a060e54bf8d69288fbee4904",
"6ef19b41225c5369f1c104d45d8d85efa9b057b53b14b4b9b939dd74decc5321");
-
- strbuf_release(&aaaaaaaaaa_100000);
- strbuf_release(&alphabet_100000);
-
- return test_done();
}
diff --git a/t/unit-tests/t-hashmap.c b/t/unit-tests/u-hashmap.c
index 83b79dff39..eb80aa1348 100644
--- a/t/unit-tests/t-hashmap.c
+++ b/t/unit-tests/u-hashmap.c
@@ -1,4 +1,4 @@
-#include "test-lib.h"
+#include "unit-test.h"
#include "hashmap.h"
#include "strbuf.h"
@@ -83,23 +83,23 @@ static void t_replace(struct hashmap *map, unsigned int ignore_case)
struct test_entry *entry;
entry = alloc_test_entry("key1", "value1", ignore_case);
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
entry = alloc_test_entry(ignore_case ? "Key1" : "key1", "value2",
ignore_case);
entry = hashmap_put_entry(map, entry, ent);
- if (check(entry != NULL))
- check_str(get_value(entry), "value1");
+ cl_assert(entry != NULL);
+ cl_assert_equal_s(get_value(entry), "value1");
free(entry);
entry = alloc_test_entry("fooBarFrotz", "value3", ignore_case);
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
entry = alloc_test_entry(ignore_case ? "FOObarFrotz" : "fooBarFrotz",
"value4", ignore_case);
entry = hashmap_put_entry(map, entry, ent);
- if (check(entry != NULL))
- check_str(get_value(entry), "value3");
+ cl_assert(entry != NULL);
+ cl_assert_equal_s(get_value(entry), "value3");
free(entry);
}
@@ -122,20 +122,18 @@ static void t_get(struct hashmap *map, unsigned int ignore_case)
for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
entry = alloc_test_entry(key_val[i][0], key_val[i][1],
ignore_case);
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
}
for (size_t i = 0; i < ARRAY_SIZE(query); i++) {
entry = get_test_entry(map, query[i][0], ignore_case);
- if (check(entry != NULL))
- check_str(get_value(entry), query[i][1]);
- else
- test_msg("query key: %s", query[i][0]);
+ cl_assert(entry != NULL);
+ cl_assert_equal_s(get_value(entry), query[i][1]);
}
- check_pointer_eq(get_test_entry(map, "notInMap", ignore_case), NULL);
- check_int(map->tablesize, ==, 64);
- check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val));
+ cl_assert_equal_p(get_test_entry(map, "notInMap", ignore_case), NULL);
+ cl_assert_equal_i(map->tablesize, 64);
+ cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val));
}
static void t_add(struct hashmap *map, unsigned int ignore_case)
@@ -165,39 +163,19 @@ static void t_add(struct hashmap *map, unsigned int ignore_case)
hashmap_for_each_entry_from(map, entry, ent)
{
- int ret;
- if (!check_int((ret = key_val_contains(
- key_val, seen,
- ARRAY_SIZE(key_val), entry)),
- ==, 0)) {
- switch (ret) {
- case 1:
- test_msg("found entry was not given in the input\n"
- " key: %s\n value: %s",
- entry->key, get_value(entry));
- break;
- case 2:
- test_msg("duplicate entry detected\n"
- " key: %s\n value: %s",
- entry->key, get_value(entry));
- break;
- }
- } else {
- count++;
- }
+ int ret = key_val_contains(key_val, seen,
+ ARRAY_SIZE(key_val), entry);
+ cl_assert_equal_i(ret, 0);
+ count++;
}
- check_int(count, ==, 2);
+ cl_assert_equal_i(count, 2);
}
- for (size_t i = 0; i < ARRAY_SIZE(seen); i++) {
- if (!check_int(seen[i], ==, 1))
- test_msg("following key-val pair was not iterated over:\n"
- " key: %s\n value: %s",
- key_val[i][0], key_val[i][1]);
- }
+ for (size_t i = 0; i < ARRAY_SIZE(seen); i++)
+ cl_assert_equal_i(seen[i], 1);
- check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val));
- check_pointer_eq(get_test_entry(map, "notInMap", ignore_case), NULL);
+ cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val));
+ cl_assert_equal_p(get_test_entry(map, "notInMap", ignore_case), NULL);
}
static void t_remove(struct hashmap *map, unsigned int ignore_case)
@@ -211,24 +189,25 @@ static void t_remove(struct hashmap *map, unsigned int ignore_case)
for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
entry = alloc_test_entry(key_val[i][0], key_val[i][1], ignore_case);
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
}
for (size_t i = 0; i < ARRAY_SIZE(remove); i++) {
entry = alloc_test_entry(remove[i][0], "", ignore_case);
removed = hashmap_remove_entry(map, entry, ent, remove[i][0]);
- if (check(removed != NULL))
- check_str(get_value(removed), remove[i][1]);
+ cl_assert(removed != NULL);
+ cl_assert_equal_s(get_value(removed), remove[i][1]);
free(entry);
free(removed);
}
entry = alloc_test_entry("notInMap", "", ignore_case);
- check_pointer_eq(hashmap_remove_entry(map, entry, ent, "notInMap"), NULL);
+ cl_assert_equal_p(hashmap_remove_entry(map, entry, ent, "notInMap"), NULL);
free(entry);
- check_int(map->tablesize, ==, 64);
- check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val) - ARRAY_SIZE(remove));
+ cl_assert_equal_i(map->tablesize, 64);
+ cl_assert_equal_i(hashmap_get_size(map),
+ ARRAY_SIZE(key_val) - ARRAY_SIZE(remove));
}
static void t_iterate(struct hashmap *map, unsigned int ignore_case)
@@ -242,38 +221,21 @@ static void t_iterate(struct hashmap *map, unsigned int ignore_case)
for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
entry = alloc_test_entry(key_val[i][0], key_val[i][1], ignore_case);
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
}
hashmap_for_each_entry(map, &iter, entry, ent /* member name */)
{
- int ret;
- if (!check_int((ret = key_val_contains(key_val, seen,
- ARRAY_SIZE(key_val),
- entry)), ==, 0)) {
- switch (ret) {
- case 1:
- test_msg("found entry was not given in the input\n"
- " key: %s\n value: %s",
- entry->key, get_value(entry));
- break;
- case 2:
- test_msg("duplicate entry detected\n"
- " key: %s\n value: %s",
- entry->key, get_value(entry));
- break;
- }
- }
+ int ret = key_val_contains(key_val, seen,
+ ARRAY_SIZE(key_val),
+ entry);
+ cl_assert(ret == 0);
}
- for (size_t i = 0; i < ARRAY_SIZE(seen); i++) {
- if (!check_int(seen[i], ==, 1))
- test_msg("following key-val pair was not iterated over:\n"
- " key: %s\n value: %s",
- key_val[i][0], key_val[i][1]);
- }
+ for (size_t i = 0; i < ARRAY_SIZE(seen); i++)
+ cl_assert_equal_i(seen[i], 1);
- check_int(hashmap_get_size(map), ==, ARRAY_SIZE(key_val));
+ cl_assert_equal_i(hashmap_get_size(map), ARRAY_SIZE(key_val));
}
static void t_alloc(struct hashmap *map, unsigned int ignore_case)
@@ -284,17 +246,17 @@ static void t_alloc(struct hashmap *map, unsigned int ignore_case)
char *key = xstrfmt("key%d", i);
char *value = xstrfmt("value%d", i);
entry = alloc_test_entry(key, value, ignore_case);
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
free(key);
free(value);
}
- check_int(map->tablesize, ==, 64);
- check_int(hashmap_get_size(map), ==, 51);
+ cl_assert_equal_i(map->tablesize, 64);
+ cl_assert_equal_i(hashmap_get_size(map), 51);
entry = alloc_test_entry("key52", "value52", ignore_case);
- check_pointer_eq(hashmap_put_entry(map, entry, ent), NULL);
- check_int(map->tablesize, ==, 256);
- check_int(hashmap_get_size(map), ==, 52);
+ cl_assert_equal_p(hashmap_put_entry(map, entry, ent), NULL);
+ cl_assert_equal_i(map->tablesize, 256);
+ cl_assert_equal_i(hashmap_get_size(map), 52);
for (int i = 1; i <= 12; i++) {
char *key = xstrfmt("key%d", i);
@@ -302,27 +264,27 @@ static void t_alloc(struct hashmap *map, unsigned int ignore_case)
entry = alloc_test_entry(key, "", ignore_case);
removed = hashmap_remove_entry(map, entry, ent, key);
- if (check(removed != NULL))
- check_str(value, get_value(removed));
+ cl_assert(removed != NULL);
+ cl_assert_equal_s(value, get_value(removed));
free(key);
free(value);
free(entry);
free(removed);
}
- check_int(map->tablesize, ==, 256);
- check_int(hashmap_get_size(map), ==, 40);
+ cl_assert_equal_i(map->tablesize, 256);
+ cl_assert_equal_i(hashmap_get_size(map), 40);
entry = alloc_test_entry("key40", "", ignore_case);
removed = hashmap_remove_entry(map, entry, ent, "key40");
- if (check(removed != NULL))
- check_str("value40", get_value(removed));
- check_int(map->tablesize, ==, 64);
- check_int(hashmap_get_size(map), ==, 39);
+ cl_assert(removed != NULL);
+ cl_assert_equal_s("value40", get_value(removed));
+ cl_assert_equal_i(map->tablesize, 64);
+ cl_assert_equal_i(hashmap_get_size(map), 39);
free(entry);
free(removed);
}
-static void t_intern(void)
+void test_hashmap__intern(void)
{
const char *values[] = { "value1", "Value1", "value2", "value2" };
@@ -330,32 +292,68 @@ static void t_intern(void)
const char *i1 = strintern(values[i]);
const char *i2 = strintern(values[i]);
- if (!check(!strcmp(i1, values[i])))
- test_msg("strintern(%s) returns %s\n", values[i], i1);
- else if (!check(i1 != values[i]))
- test_msg("strintern(%s) returns input pointer\n",
- values[i]);
- else if (!check_pointer_eq(i1, i2))
- test_msg("address('%s') != address('%s'), so strintern('%s') != strintern('%s')",
- i1, i2, values[i], values[i]);
- else
- check_str(i1, values[i]);
+ cl_assert_equal_s(i1, values[i]);
+ cl_assert(i1 != values[i]);
+ cl_assert_equal_p(i1, i2);
}
}
-int cmd_main(int argc UNUSED, const char **argv UNUSED)
+void test_hashmap__replace_case_sensitive(void)
+{
+ setup(t_replace, 0);
+}
+
+void test_hashmap__replace_case_insensitive(void)
+{
+ setup(t_replace, 1);
+}
+
+void test_hashmap__get_case_sensitive(void)
+{
+ setup(t_get, 0);
+}
+
+void test_hashmap__get_case_insensitive(void)
+{
+ setup(t_get, 1);
+}
+
+void test_hashmap__add_case_sensitive(void)
+{
+ setup(t_add, 0);
+}
+
+void test_hashmap__add_case_insensitive(void)
+{
+ setup(t_add, 1);
+}
+
+void test_hashmap__remove_case_sensitive(void)
+{
+ setup(t_remove, 0);
+}
+
+void test_hashmap__remove_case_insensitive(void)
+{
+ setup(t_remove, 1);
+}
+
+void test_hashmap__iterate_case_sensitive(void)
+{
+ setup(t_iterate, 0);
+}
+
+void test_hashmap__iterate_case_insensitive(void)
+{
+ setup(t_iterate, 1);
+}
+
+void test_hashmap__alloc_case_sensitive(void)
+{
+ setup(t_alloc, 0);
+}
+
+void test_hashmap__alloc_case_insensitive(void)
{
- TEST(setup(t_replace, 0), "replace works");
- TEST(setup(t_replace, 1), "replace (case insensitive) works");
- TEST(setup(t_get, 0), "get works");
- TEST(setup(t_get, 1), "get (case insensitive) works");
- TEST(setup(t_add, 0), "add works");
- TEST(setup(t_add, 1), "add (case insensitive) works");
- TEST(setup(t_remove, 0), "remove works");
- TEST(setup(t_remove, 1), "remove (case insensitive) works");
- TEST(setup(t_iterate, 0), "iterate works");
- TEST(setup(t_iterate, 1), "iterate (case insensitive) works");
- TEST(setup(t_alloc, 0), "grow / shrink works");
- TEST(t_intern(), "string interning works");
- return test_done();
+ setup(t_alloc, 1);
}
diff --git a/t/unit-tests/u-mem-pool.c b/t/unit-tests/u-mem-pool.c
new file mode 100644
index 0000000000..2bc2493b7e
--- /dev/null
+++ b/t/unit-tests/u-mem-pool.c
@@ -0,0 +1,25 @@
+#include "unit-test.h"
+#include "mem-pool.h"
+
+static void test_many_pool_allocations(size_t block_alloc)
+{
+ struct mem_pool pool = { .block_alloc = block_alloc };
+ size_t size = 100;
+ char *buffer = mem_pool_calloc(&pool, 1, size);
+ for (size_t i = 0; i < size; i++)
+ cl_assert_equal_i(0, buffer[i]);
+ cl_assert(pool.mp_block != NULL);
+ cl_assert(pool.mp_block->next_free != NULL);
+ cl_assert(pool.mp_block->end != NULL);
+ mem_pool_discard(&pool, 0);
+}
+
+void test_mem_pool__big_block(void)
+{
+ test_many_pool_allocations(1024 * 1024);
+}
+
+void test_mem_pool__tiny_block(void)
+{
+ test_many_pool_allocations(1);
+}
diff --git a/t/unit-tests/u-prio-queue.c b/t/unit-tests/u-prio-queue.c
new file mode 100644
index 0000000000..145e689c9c
--- /dev/null
+++ b/t/unit-tests/u-prio-queue.c
@@ -0,0 +1,94 @@
+#include "unit-test.h"
+#include "prio-queue.h"
+
+static int intcmp(const void *va, const void *vb, void *data UNUSED)
+{
+ const int *a = va, *b = vb;
+ return *a - *b;
+}
+
+
+#define MISSING -1
+#define DUMP -2
+#define STACK -3
+#define GET -4
+#define REVERSE -5
+
+static int show(int *v)
+{
+ return v ? *v : MISSING;
+}
+
+static void test_prio_queue(int *input, size_t input_size,
+ int *result, size_t result_size)
+{
+ struct prio_queue pq = { intcmp };
+ size_t j = 0;
+
+ for (size_t i = 0; i < input_size; i++) {
+ void *peek, *get;
+ switch(input[i]) {
+ case GET:
+ peek = prio_queue_peek(&pq);
+ get = prio_queue_get(&pq);
+ cl_assert(peek == get);
+ cl_assert(j < result_size);
+ cl_assert_equal_i(result[j], show(get));
+ j++;
+ break;
+ case DUMP:
+ while ((peek = prio_queue_peek(&pq))) {
+ get = prio_queue_get(&pq);
+ cl_assert(peek == get);
+ cl_assert(j < result_size);
+ cl_assert_equal_i(result[j], show(get));
+ j++;
+ }
+ break;
+ case STACK:
+ pq.compare = NULL;
+ break;
+ case REVERSE:
+ prio_queue_reverse(&pq);
+ break;
+ default:
+ prio_queue_put(&pq, &input[i]);
+ break;
+ }
+ }
+ cl_assert_equal_i(j, result_size);
+ clear_prio_queue(&pq);
+}
+
+#define TEST_INPUT(input, result) \
+ test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result))
+
+void test_prio_queue__basic(void)
+{
+ TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }),
+ ((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 }));
+}
+
+void test_prio_queue__mixed(void)
+{
+ TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }),
+ ((int []){ 2, 3, 4, 1, 5, 6 }));
+}
+
+void test_prio_queue__empty(void)
+{
+ TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }),
+ ((int []){ 1, 2, MISSING, 1, 2, MISSING }));
+}
+
+void test_prio_queue__stack(void)
+{
+ TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }),
+ ((int []){ 3, 2, 6, 4, 5, 1, 8 }));
+}
+
+void test_prio_queue__reverse_stack(void)
+{
+ TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }),
+ ((int []){ 1, 2, 3, 4, 5, 6 }));
+}
diff --git a/t/unit-tests/t-reftable-tree.c b/t/unit-tests/u-reftable-tree.c
index 79b175a45a..bcf9061071 100644
--- a/t/unit-tests/t-reftable-tree.c
+++ b/t/unit-tests/u-reftable-tree.c
@@ -6,7 +6,7 @@ license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
-#include "test-lib.h"
+#include "unit-test.h"
#include "reftable/tree.h"
static int t_compare(const void *a, const void *b)
@@ -25,7 +25,7 @@ static void store(void *arg, void *key)
c->arr[c->len++] = key;
}
-static void t_tree_search(void)
+void test_reftable_tree__tree_search(void)
{
struct tree_node *root = NULL;
void *values[11] = { 0 };
@@ -38,20 +38,20 @@ static void t_tree_search(void)
*/
do {
nodes[i] = tree_insert(&root, &values[i], &t_compare);
- check(nodes[i] != NULL);
+ cl_assert(nodes[i] != NULL);
i = (i * 7) % 11;
} while (i != 1);
for (i = 1; i < ARRAY_SIZE(nodes); i++) {
- check_pointer_eq(&values[i], nodes[i]->key);
- check_pointer_eq(nodes[i], tree_search(root, &values[i], &t_compare));
+ cl_assert_equal_p(&values[i], nodes[i]->key);
+ cl_assert_equal_p(nodes[i], tree_search(root, &values[i], &t_compare));
}
- check(!tree_search(root, values, t_compare));
+ cl_assert(tree_search(root, values, t_compare) == NULL);
tree_free(root);
}
-static void t_infix_walk(void)
+void test_reftable_tree__infix_walk(void)
{
struct tree_node *root = NULL;
void *values[11] = { 0 };
@@ -64,23 +64,15 @@ static void t_infix_walk(void)
do {
struct tree_node *node = tree_insert(&root, &values[i], t_compare);
- check(node != NULL);
+ cl_assert(node != NULL);
i = (i * 7) % 11;
count++;
} while (i != 1);
infix_walk(root, &store, &c);
for (i = 1; i < ARRAY_SIZE(values); i++)
- check_pointer_eq(&values[i], out[i - 1]);
- check(!out[i - 1]);
- check_int(c.len, ==, count);
+ cl_assert_equal_p(&values[i], out[i - 1]);
+ cl_assert(out[i - 1] == NULL);
+ cl_assert_equal_i(c.len, count);
tree_free(root);
}
-
-int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
-{
- TEST(t_tree_search(), "tree_search works");
- TEST(t_infix_walk(), "infix_walk works");
-
- return test_done();
-}
diff --git a/t/unit-tests/u-strbuf.c b/t/unit-tests/u-strbuf.c
new file mode 100644
index 0000000000..caa5d78aa3
--- /dev/null
+++ b/t/unit-tests/u-strbuf.c
@@ -0,0 +1,119 @@
+#include "unit-test.h"
+#include "strbuf.h"
+
+/* wrapper that supplies tests with an empty, initialized strbuf */
+static void setup(void (*f)(struct strbuf*, const void*),
+ const void *data)
+{
+ struct strbuf buf = STRBUF_INIT;
+
+ f(&buf, data);
+ strbuf_release(&buf);
+ cl_assert_equal_i(buf.len, 0);
+ cl_assert_equal_i(buf.alloc, 0);
+}
+
+/* wrapper that supplies tests with a populated, initialized strbuf */
+static void setup_populated(void (*f)(struct strbuf*, const void*),
+ const char *init_str, const void *data)
+{
+ struct strbuf buf = STRBUF_INIT;
+
+ strbuf_addstr(&buf, init_str);
+ cl_assert_equal_i(buf.len, strlen(init_str));
+ f(&buf, data);
+ strbuf_release(&buf);
+ cl_assert_equal_i(buf.len, 0);
+ cl_assert_equal_i(buf.alloc, 0);
+}
+
+static void assert_sane_strbuf(struct strbuf *buf)
+{
+ /* Initialized strbufs should always have a non-NULL buffer */
+ cl_assert(buf->buf != NULL);
+ /* Buffers should always be NUL-terminated */
+ cl_assert(buf->buf[buf->len] == '\0');
+ /*
+ * In case the buffer contains anything, `alloc` must alloc must
+ * be at least one byte larger than `len`.
+ */
+ if (buf->len)
+ cl_assert(buf->len < buf->alloc);
+}
+
+void test_strbuf__static_init(void)
+{
+ struct strbuf buf = STRBUF_INIT;
+
+ cl_assert_equal_i(buf.len, 0);
+ cl_assert_equal_i(buf.alloc, 0);
+ cl_assert(buf.buf[0] == '\0');
+}
+
+void test_strbuf__dynamic_init(void)
+{
+ struct strbuf buf;
+
+ strbuf_init(&buf, 1024);
+ assert_sane_strbuf(&buf);
+ cl_assert_equal_i(buf.len, 0);
+ cl_assert(buf.alloc >= 1024);
+ cl_assert(buf.buf[0] == '\0');
+ strbuf_release(&buf);
+}
+
+static void t_addch(struct strbuf *buf, const void *data)
+{
+ const char *p_ch = data;
+ const char ch = *p_ch;
+ size_t orig_alloc = buf->alloc;
+ size_t orig_len = buf->len;
+
+ assert_sane_strbuf(buf);
+ strbuf_addch(buf, ch);
+ assert_sane_strbuf(buf);
+ cl_assert_equal_i(buf->len, orig_len + 1);
+ cl_assert(buf->alloc >= orig_alloc);
+ cl_assert(buf->buf[buf->len] == '\0');
+}
+
+static void t_addstr(struct strbuf *buf, const void *data)
+{
+ const char *text = data;
+ size_t len = strlen(text);
+ size_t orig_alloc = buf->alloc;
+ size_t orig_len = buf->len;
+
+ assert_sane_strbuf(buf);
+ strbuf_addstr(buf, text);
+ assert_sane_strbuf(buf);
+ cl_assert_equal_i(buf->len, orig_len + len);
+ cl_assert(buf->alloc >= orig_alloc);
+ cl_assert(buf->buf[buf->len] == '\0');
+ cl_assert_equal_s(buf->buf + orig_len, text);
+}
+
+void test_strbuf__add_single_char(void)
+{
+ setup(t_addch, "a");
+}
+
+void test_strbuf__add_empty_char(void)
+{
+ setup(t_addch, "");
+}
+
+void test_strbuf__add_append_char(void)
+{
+ setup_populated(t_addch, "initial value", "a");
+}
+
+void test_strbuf__add_single_str(void)
+{
+ setup(t_addstr, "hello there");
+}
+
+void test_strbuf__add_append_str(void)
+{
+ setup_populated(t_addstr, "initial value", "hello there");
+}
diff --git a/t/unit-tests/u-strcmp-offset.c b/t/unit-tests/u-strcmp-offset.c
new file mode 100644
index 0000000000..7e8e9acf3c
--- /dev/null
+++ b/t/unit-tests/u-strcmp-offset.c
@@ -0,0 +1,45 @@
+#include "unit-test.h"
+#include "read-cache-ll.h"
+
+static void check_strcmp_offset(const char *string1, const char *string2,
+ int expect_result, uintmax_t expect_offset)
+{
+ size_t offset;
+ int result = strcmp_offset(string1, string2, &offset);
+
+ /*
+ * Because different CRTs behave differently, only rely on signs of the
+ * result values.
+ */
+ result = (result < 0 ? -1 :
+ result > 0 ? 1 :
+ 0);
+
+ cl_assert_equal_i(result, expect_result);
+ cl_assert_equal_i((uintmax_t)offset, expect_offset);
+}
+
+void test_strcmp_offset__empty(void)
+{
+ check_strcmp_offset("", "", 0, 0);
+}
+
+void test_strcmp_offset__equal(void)
+{
+ check_strcmp_offset("abc", "abc", 0, 3);
+}
+
+void test_strcmp_offset__different(void)
+{
+ check_strcmp_offset("abc", "def", -1, 0);
+}
+
+void test_strcmp_offset__mismatch(void)
+{
+ check_strcmp_offset("abc", "abz", -1, 2);
+}
+
+void test_strcmp_offset__different_length(void)
+{
+ check_strcmp_offset("abc", "abcdef", -1, 3);
+}