pool.c 1.85 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#include "clar_libgit2.h"
#include "pool.h"
#include "git2/oid.h"

void test_core_pool__0(void)
{
	int i;
	git_pool p;
	void *ptr;

11
	git_pool_init(&p, 1);
12 13

	for (i = 1; i < 10000; i *= 2) {
14
		ptr = git_pool_malloc(&p, i);
15 16 17 18 19 20 21 22 23 24 25 26 27
		cl_assert(ptr != NULL);
		cl_assert(git_pool__ptr_in_pool(&p, ptr));
		cl_assert(!git_pool__ptr_in_pool(&p, &i));
	}

	git_pool_clear(&p);
}

void test_core_pool__1(void)
{
	int i;
	git_pool p;

28 29
	git_pool_init(&p, 1);
	p.page_size = 4000;
30 31

	for (i = 2010; i > 0; i--)
32
		cl_assert(git_pool_malloc(&p, i) != NULL);
33

34
#ifndef GIT_DEBUG_POOL
35
	/* with fixed page size, allocation must end up with these values */
36
	cl_assert_equal_i(591, git_pool__open_pages(&p));
37
#endif
38 39
	git_pool_clear(&p);

40 41
	git_pool_init(&p, 1);
	p.page_size = 4120;
42 43

	for (i = 2010; i > 0; i--)
44
		cl_assert(git_pool_malloc(&p, i) != NULL);
45

46
#ifndef GIT_DEBUG_POOL
47
	/* with fixed page size, allocation must end up with these values */
48
	cl_assert_equal_i(sizeof(void *) == 8 ? 575 : 573, git_pool__open_pages(&p));
49
#endif
50 51 52 53 54 55 56 57 58 59 60 61 62 63
	git_pool_clear(&p);
}

static char to_hex[] = "0123456789abcdef";

void test_core_pool__2(void)
{
	git_pool p;
	char oid_hex[GIT_OID_HEXSZ];
	git_oid *oid;
	int i, j;

	memset(oid_hex, '0', sizeof(oid_hex));

64 65
	git_pool_init(&p, sizeof(git_oid));
	p.page_size = 4000;
66 67

	for (i = 1000; i < 10000; i++) {
68 69 70
		oid = git_pool_malloc(&p, 1);
		cl_assert(oid != NULL);

71 72 73 74 75
		for (j = 0; j < 8; j++)
			oid_hex[j] = to_hex[(i >> (4 * j)) & 0x0f];
		cl_git_pass(git_oid_fromstr(oid, oid_hex));
	}

76
#ifndef GIT_DEBUG_POOL
77
	/* with fixed page size, allocation must end up with these values */
78
	cl_assert_equal_i(sizeof(void *) == 8 ? 55 : 45, git_pool__open_pages(&p));
79
#endif
80 81
	git_pool_clear(&p);
}
82 83 84 85 86

void test_core_pool__strndup_limit(void)
{
	git_pool p;

87
	git_pool_init(&p, 1);
88 89
	/* ensure 64 bit doesn't overflow */
	cl_assert(git_pool_strndup(&p, "foo", (size_t)-1) == NULL);
90 91 92
	git_pool_clear(&p);
}