oidmap.c 3.6 KB
Newer Older
1 2 3
#include "clar_libgit2.h"
#include "oidmap.h"

4
static struct {
5 6
	git_oid oid;
	size_t extra;
7
} test_oids[0x0FFF];
8

9
static git_oidmap *g_map;
10

11
void test_core_oidmap__initialize(void)
12 13
{
	uint32_t i, j;
14 15 16 17 18
	for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
		uint32_t segment = i / 8;
		int modi = i - (segment * 8);

		test_oids[i].extra = i;
19

20
		for (j = 0; j < GIT_OID_SHA1_SIZE / 4; ++j) {
21 22 23 24
			test_oids[i].oid.id[j * 4    ] = (unsigned char)modi;
			test_oids[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8);
			test_oids[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16);
			test_oids[i].oid.id[j * 4 + 3] = (unsigned char)(modi >> 24);
25 26
		}

27 28 29 30
		test_oids[i].oid.id[ 8] = (unsigned char)i;
		test_oids[i].oid.id[ 9] = (unsigned char)(i >> 8);
		test_oids[i].oid.id[10] = (unsigned char)(i >> 16);
		test_oids[i].oid.id[11] = (unsigned char)(i >> 24);
31
#ifdef GIT_EXPERIMENTAL_SHA256
Edward Thomson committed
32
		test_oids[i].oid.type = GIT_OID_SHA1;
33
#endif
34 35
	}

36 37
	cl_git_pass(git_oidmap_new(&g_map));
}
38

39 40 41 42
void test_core_oidmap__cleanup(void)
{
	git_oidmap_free(g_map);
}
43

44 45 46
void test_core_oidmap__basic(void)
{
	size_t i;
47

48 49 50
	for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
		cl_assert(!git_oidmap_exists(g_map, &test_oids[i].oid));
		cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
51 52
	}

53 54 55 56
	for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
		cl_assert(git_oidmap_exists(g_map, &test_oids[i].oid));
		cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[i]);
	}
57 58 59 60
}

void test_core_oidmap__hash_collision(void)
{
61
	size_t i;
62

63 64 65
	for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
		cl_assert(!git_oidmap_exists(g_map, &test_oids[i].oid));
		cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
66 67
	}

68 69 70
	for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
		cl_assert(git_oidmap_exists(g_map, &test_oids[i].oid));
		cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[i]);
71 72
	}
}
73 74 75

void test_core_oidmap__get_succeeds_with_existing_keys(void)
{
76
	size_t i;
77

78 79
	for (i = 0; i < ARRAY_SIZE(test_oids); ++i)
		cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
80

81 82
	for (i = 0; i < ARRAY_SIZE(test_oids); ++i)
		cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[i].oid), &test_oids[i]);
83 84 85 86
}

void test_core_oidmap__get_fails_with_nonexisting_key(void)
{
87
	size_t i;
88 89

	/* Do _not_ add last OID to verify that we cannot look it up */
90 91
	for (i = 0; i < ARRAY_SIZE(test_oids) - 1; ++i)
		cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
92

93
	cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[ARRAY_SIZE(test_oids) - 1].oid), NULL);
94
}
95 96 97 98

void test_core_oidmap__setting_oid_persists(void)
{
	git_oid oids[] = {
99 100 101
		GIT_OID_INIT(GIT_OID_SHA1, { 0x01 }),
		GIT_OID_INIT(GIT_OID_SHA1, { 0x02 }),
		GIT_OID_INIT(GIT_OID_SHA1, { 0x03 })
102 103
	};

104 105 106
	cl_git_pass(git_oidmap_set(g_map, &oids[0], "one"));
	cl_git_pass(git_oidmap_set(g_map, &oids[1], "two"));
	cl_git_pass(git_oidmap_set(g_map, &oids[2], "three"));
107

108 109 110
	cl_assert_equal_s(git_oidmap_get(g_map, &oids[0]), "one");
	cl_assert_equal_s(git_oidmap_get(g_map, &oids[1]), "two");
	cl_assert_equal_s(git_oidmap_get(g_map, &oids[2]), "three");
111 112 113 114 115
}

void test_core_oidmap__setting_existing_key_updates(void)
{
	git_oid oids[] = {
116 117 118
		GIT_OID_INIT(GIT_OID_SHA1, { 0x01 }),
		GIT_OID_INIT(GIT_OID_SHA1, { 0x02 }),
		GIT_OID_INIT(GIT_OID_SHA1, { 0x03 })
119 120
	};

121 122 123 124
	cl_git_pass(git_oidmap_set(g_map, &oids[0], "one"));
	cl_git_pass(git_oidmap_set(g_map, &oids[1], "two"));
	cl_git_pass(git_oidmap_set(g_map, &oids[2], "three"));
	cl_assert_equal_i(git_oidmap_size(g_map), 3);
125

126 127
	cl_git_pass(git_oidmap_set(g_map, &oids[1], "other"));
	cl_assert_equal_i(git_oidmap_size(g_map), 3);
128

129
	cl_assert_equal_s(git_oidmap_get(g_map, &oids[1]), "other");
130
}