Blame tests/core/strmap.c

Packit Service 20376f
#include "clar_libgit2.h"
Packit Service 20376f
#include "strmap.h"
Packit Service 20376f
Packit Service 20376f
git_strmap *g_table;
Packit Service 20376f
Packit Service 20376f
void test_core_strmap__initialize(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_git_pass(git_strmap_alloc(&g_table));
Packit Service 20376f
	cl_assert(g_table != NULL);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_strmap__cleanup(void)
Packit Service 20376f
{
Packit Service 20376f
	git_strmap_free(g_table);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_strmap__0(void)
Packit Service 20376f
{
Packit Service 20376f
	cl_assert(git_strmap_num_entries(g_table) == 0);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
static void insert_strings(git_strmap *table, int count)
Packit Service 20376f
{
Packit Service 20376f
	int i, j, over, err;
Packit Service 20376f
	char *str;
Packit Service 20376f
Packit Service 20376f
	for (i = 0; i < count; ++i) {
Packit Service 20376f
		str = malloc(10);
Packit Service 20376f
		for (j = 0; j < 10; ++j)
Packit Service 20376f
			str[j] = 'a' + (i % 26);
Packit Service 20376f
		str[9] = '\0';
Packit Service 20376f
Packit Service 20376f
		/* if > 26, then encode larger value in first letters */
Packit Service 20376f
		for (j = 0, over = i / 26; over > 0; j++, over = over / 26)
Packit Service 20376f
			str[j] = 'A' + (over % 26);
Packit Service 20376f
Packit Service 20376f
		git_strmap_insert(table, str, str, &err;;
Packit Service 20376f
		cl_assert(err >= 0);
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	cl_assert((int)git_strmap_num_entries(table) == count);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_strmap__1(void)
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
	char *str;
Packit Service 20376f
Packit Service 20376f
	insert_strings(g_table, 20);
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_strmap_exists(g_table, "aaaaaaaaa"));
Packit Service 20376f
	cl_assert(git_strmap_exists(g_table, "ggggggggg"));
Packit Service 20376f
	cl_assert(!git_strmap_exists(g_table, "aaaaaaaab"));
Packit Service 20376f
	cl_assert(!git_strmap_exists(g_table, "abcdefghi"));
Packit Service 20376f
Packit Service 20376f
	i = 0;
Packit Service 20376f
	git_strmap_foreach_value(g_table, str, { i++; free(str); });
Packit Service 20376f
	cl_assert(i == 20);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_strmap__2(void)
Packit Service 20376f
{
Packit Service 20376f
	khiter_t pos;
Packit Service 20376f
	int i;
Packit Service 20376f
	char *str;
Packit Service 20376f
Packit Service 20376f
	insert_strings(g_table, 20);
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_strmap_exists(g_table, "aaaaaaaaa"));
Packit Service 20376f
	cl_assert(git_strmap_exists(g_table, "ggggggggg"));
Packit Service 20376f
	cl_assert(!git_strmap_exists(g_table, "aaaaaaaab"));
Packit Service 20376f
	cl_assert(!git_strmap_exists(g_table, "abcdefghi"));
Packit Service 20376f
Packit Service 20376f
	cl_assert(git_strmap_exists(g_table, "bbbbbbbbb"));
Packit Service 20376f
	pos = git_strmap_lookup_index(g_table, "bbbbbbbbb");
Packit Service 20376f
	cl_assert(git_strmap_valid_index(g_table, pos));
Packit Service 20376f
	cl_assert_equal_s(git_strmap_value_at(g_table, pos), "bbbbbbbbb");
Packit Service 20376f
	free(git_strmap_value_at(g_table, pos));
Packit Service 20376f
	git_strmap_delete_at(g_table, pos);
Packit Service 20376f
Packit Service 20376f
	cl_assert(!git_strmap_exists(g_table, "bbbbbbbbb"));
Packit Service 20376f
Packit Service 20376f
	i = 0;
Packit Service 20376f
	git_strmap_foreach_value(g_table, str, { i++; free(str); });
Packit Service 20376f
	cl_assert(i == 19);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void test_core_strmap__3(void)
Packit Service 20376f
{
Packit Service 20376f
	int i;
Packit Service 20376f
	char *str;
Packit Service 20376f
Packit Service 20376f
	insert_strings(g_table, 10000);
Packit Service 20376f
Packit Service 20376f
	i = 0;
Packit Service 20376f
	git_strmap_foreach_value(g_table, str, { i++; free(str); });
Packit Service 20376f
	cl_assert(i == 10000);
Packit Service 20376f
}