summaryrefslogtreecommitdiff
path: root/tests/core/oidmap.c
blob: 7f98287a6729d680a461a358ddf064d4b7678520 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "clar_libgit2.h"
#include "oidmap.h"

static struct {
	git_oid oid;
	size_t extra;
} test_oids[0x0FFF];

static git_oidmap *g_map;

void test_core_oidmap__initialize(void)
{
	uint32_t i, j;
	for (i = 0; i < ARRAY_SIZE(test_oids); ++i) {
		uint32_t segment = i / 8;
		int modi = i - (segment * 8);

		test_oids[i].extra = i;

		for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
			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);
		}

		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);
	}

	cl_git_pass(git_oidmap_new(&g_map));
}

void test_core_oidmap__cleanup(void)
{
	git_oidmap_free(g_map);
}

void test_core_oidmap__basic(void)
{
	size_t i;

	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]));
	}

	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]);
	}
}

void test_core_oidmap__hash_collision(void)
{
	size_t i;

	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]));
	}

	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]);
	}
}

void test_core_oidmap__get_succeeds_with_existing_keys(void)
{
	size_t i;

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

	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]);
}

void test_core_oidmap__get_fails_with_nonexisting_key(void)
{
	size_t i;

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

	cl_assert_equal_p(git_oidmap_get(g_map, &test_oids[ARRAY_SIZE(test_oids) - 1].oid), NULL);
}

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

	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_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");
}

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

	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);

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

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