oidmap.c 3.39 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_RAWSZ / 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 32
	}

33 34
	cl_git_pass(git_oidmap_new(&g_map));
}
35

36 37 38 39
void test_core_oidmap__cleanup(void)
{
	git_oidmap_free(g_map);
}
40

41 42 43
void test_core_oidmap__basic(void)
{
	size_t i;
44

45 46 47
	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]));
48 49
	}

50 51 52 53
	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]);
	}
54 55 56 57
}

void test_core_oidmap__hash_collision(void)
{
58
	size_t i;
59

60 61 62
	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]));
63 64
	}

65 66 67
	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]);
68 69
	}
}
70 71 72

void test_core_oidmap__get_succeeds_with_existing_keys(void)
{
73
	size_t i;
74

75 76
	for (i = 0; i < ARRAY_SIZE(test_oids); ++i)
		cl_git_pass(git_oidmap_set(g_map, &test_oids[i].oid, &test_oids[i]));
77

78 79
	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]);
80 81 82 83
}

void test_core_oidmap__get_fails_with_nonexisting_key(void)
{
84
	size_t i;
85 86

	/* Do _not_ add last OID to verify that we cannot look it up */
87 88
	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]));
89

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

void test_core_oidmap__setting_oid_persists(void)
{
	git_oid oids[] = {
	    {{ 0x01 }},
	    {{ 0x02 }},
	    {{ 0x03 }}
	};

101 102 103
	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"));
104

105 106 107
	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");
108 109 110 111 112 113 114 115 116 117
}

void test_core_oidmap__setting_existing_key_updates(void)
{
	git_oid oids[] = {
	    {{ 0x01 }},
	    {{ 0x02 }},
	    {{ 0x03 }}
	};

118 119 120 121
	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);
122

123 124
	cl_git_pass(git_oidmap_set(g_map, &oids[1], "other"));
	cl_assert_equal_i(git_oidmap_size(g_map), 3);
125

126
	cl_assert_equal_s(git_oidmap_get(g_map, &oids[1]), "other");
127
}