alloc.c 1.18 KB
Newer Older
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
#include "clar_libgit2.h"
#include "clar_libgit2_alloc.h"
#include "alloc.h"

void test_alloc__cleanup(void)
{
	cl_alloc_reset();
}

void test_alloc__oom(void)
{
	void *ptr = NULL;

	cl_alloc_limit(0);

	cl_assert(git__malloc(1) == NULL);
	cl_assert(git__calloc(1, 1) == NULL);
	cl_assert(git__realloc(ptr, 1) == NULL);
	cl_assert(git__strdup("test") == NULL);
	cl_assert(git__strndup("test", 4) == NULL);
}

void test_alloc__single_byte_is_exhausted(void)
{
	void *ptr;

	cl_alloc_limit(1);

	cl_assert(ptr = git__malloc(1));
	cl_assert(git__malloc(1) == NULL);
	git__free(ptr);
}

void test_alloc__free_replenishes_byte(void)
{
	void *ptr;

	cl_alloc_limit(1);

	cl_assert(ptr = git__malloc(1));
	cl_assert(git__malloc(1) == NULL);
	git__free(ptr);
	cl_assert(ptr = git__malloc(1));
	git__free(ptr);
}

void test_alloc__realloc(void)
{
	char *ptr = NULL;

	cl_alloc_limit(3);

	cl_assert(ptr = git__realloc(ptr, 1));
	*ptr = 'x';

	cl_assert(ptr = git__realloc(ptr, 1));
	cl_assert_equal_i(*ptr, 'x');

	cl_assert(ptr = git__realloc(ptr, 2));
	cl_assert_equal_i(*ptr, 'x');

	cl_assert(git__realloc(ptr, 2) == NULL);

	cl_assert(ptr = git__realloc(ptr, 1));
	cl_assert_equal_i(*ptr, 'x');

	git__free(ptr);
}