summaryrefslogtreecommitdiff
path: root/tests/core/oidmap.c
blob: ec4b5e7756455cd6a56e66abf0a07d9465cce4d8 (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
#include "clar_libgit2.h"
#include "oidmap.h"

GIT__USE_OIDMAP;

typedef struct {
	git_oid oid;
	size_t extra;
} oidmap_item;

#define NITEMS 0x0fff

void test_core_oidmap__basic(void)
{
	git_oidmap *map;
	oidmap_item items[NITEMS];
	uint32_t i, j;

	for (i = 0; i < NITEMS; ++i) {
		items[i].extra = i;
		for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
			items[i].oid.id[j * 4    ] = (unsigned char)i;
			items[i].oid.id[j * 4 + 1] = (unsigned char)(i >> 8);
			items[i].oid.id[j * 4 + 2] = (unsigned char)(i >> 16);
			items[i].oid.id[j * 4 + 3] = (unsigned char)(i >> 24);
		}
	}

	map = git_oidmap_alloc();
	cl_assert(map != NULL);

	for (i = 0; i < NITEMS; ++i) {
		khiter_t pos;
		int ret;

		pos = kh_get(oid, map, &items[i].oid);
		cl_assert(pos == kh_end(map));

		pos = kh_put(oid, map, &items[i].oid, &ret);
		cl_assert(ret != 0);

		kh_val(map, pos) = &items[i];
	}


	for (i = 0; i < NITEMS; ++i) {
		khiter_t pos;

		pos = kh_get(oid, map, &items[i].oid);
		cl_assert(pos != kh_end(map));

		cl_assert_equal_p(kh_val(map, pos), &items[i]);
	}

	git_oidmap_free(map);
}

void test_core_oidmap__hash_collision(void)
{
	git_oidmap *map;
	oidmap_item items[NITEMS];
	uint32_t i, j;

	for (i = 0; i < NITEMS; ++i) {
		uint32_t segment = i / 8;
		int modi = i - (segment * 8);

		items[i].extra = i;

		for (j = 0; j < GIT_OID_RAWSZ / 4; ++j) {
			items[i].oid.id[j * 4    ] = (unsigned char)modi;
			items[i].oid.id[j * 4 + 1] = (unsigned char)(modi >> 8);
			items[i].oid.id[j * 4 + 2] = (unsigned char)(modi >> 16);
			items[i].oid.id[j * 4 + 3] = (unsigned char)(modi >> 24);
		}

		items[i].oid.id[ 8] = (unsigned char)i;
		items[i].oid.id[ 9] = (unsigned char)(i >> 8);
		items[i].oid.id[10] = (unsigned char)(i >> 16);
		items[i].oid.id[11] = (unsigned char)(i >> 24);
	}

	map = git_oidmap_alloc();
	cl_assert(map != NULL);

	for (i = 0; i < NITEMS; ++i) {
		khiter_t pos;
		int ret;

		pos = kh_get(oid, map, &items[i].oid);
		cl_assert(pos == kh_end(map));

		pos = kh_put(oid, map, &items[i].oid, &ret);
		cl_assert(ret != 0);

		kh_val(map, pos) = &items[i];
	}


	for (i = 0; i < NITEMS; ++i) {
		khiter_t pos;

		pos = kh_get(oid, map, &items[i].oid);
		cl_assert(pos != kh_end(map));

		cl_assert_equal_p(kh_val(map, pos), &items[i]);
	}

	git_oidmap_free(map);
}