foreach.c 1.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#include "clar_libgit2.h"
#include "odb.h"
#include "git2/odb_backend.h"
#include "pack.h"

static git_odb *_odb;
static git_repository *_repo;

void test_odb_foreach__cleanup(void)
{
	git_odb_free(_odb);
	git_repository_free(_repo);
13 14 15

	_odb = NULL;
	_repo = NULL;
16 17
}

18
static int foreach_cb(const git_oid *oid, void *data)
19
{
20 21
	int *nobj = data;
	(*nobj)++;
22

23
	GIT_UNUSED(oid);
24 25 26 27

	return 0;
}

28
/*
29
 * $ git --git-dir tests/resources/testrepo.git count-objects --verbose
30 31
 * count: 47
 * size: 4
32 33 34 35 36 37
 * in-pack: 1640
 * packs: 3
 * size-pack: 425
 * prune-packable: 0
 * garbage: 0
 */
38 39
void test_odb_foreach__foreach(void)
{
40 41
	int nobj = 0;

42 43 44
	cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
	git_repository_odb(&_odb, _repo);

45
	cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
46
	cl_assert_equal_i(47 + 1640, nobj); /* count + in-pack */
47
}
48 49 50 51

void test_odb_foreach__one_pack(void)
{
	git_odb_backend *backend = NULL;
52
	int nobj = 0;
53 54 55 56 57 58

	cl_git_pass(git_odb_new(&_odb));
	cl_git_pass(git_odb_backend_one_pack(&backend, cl_fixture("testrepo.git/objects/pack/pack-a81e489679b7d3418f9ab594bda8ceb37dd4c695.idx")));
	cl_git_pass(git_odb_add_backend(_odb, backend, 1));
	_repo = NULL;

59
	cl_git_pass(git_odb_foreach(_odb, foreach_cb, &nobj));
60 61
	cl_assert(nobj == 1628);
}
62

63
static int foreach_stop_cb(const git_oid *oid, void *data)
64
{
65 66
	int *nobj = data;
	(*nobj)++;
67

68
	GIT_UNUSED(oid);
69

70
	return (*nobj == 1000) ? -321 : 0;
71 72 73 74
}

void test_odb_foreach__interrupt_foreach(void)
{
75 76
	int nobj = 0;

77 78 79
	cl_git_pass(git_repository_open(&_repo, cl_fixture("testrepo.git")));
	git_repository_odb(&_odb, _repo);

80
	cl_assert_equal_i(-321, git_odb_foreach(_odb, foreach_stop_cb, &nobj));
81 82
	cl_assert(nobj == 1000);
}