Skip to content

Commit 1f47efc

Browse files
committed
tests: object: consolidate cache tests
The object::cache test module has two tests that do nearly the same thing: given a cache limit, load a certain set of objects and verify if those objects have been cached or not. Convert those tests to the new data-driven initializers to demonstrate how these are to be used. Furthermore, add some additional test data. This conversion is mainly done to show this new facility.
1 parent 394951a commit 1f47efc

File tree

1 file changed

+57
-68
lines changed

1 file changed

+57
-68
lines changed

tests/object/cache.c

Lines changed: 57 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,35 @@
22
#include "repository.h"
33

44
static git_repository *g_repo;
5+
static size_t cache_limit;
6+
static int object_type;
57

6-
void test_object_cache__initialize(void)
8+
void test_object_cache__initialize_cache_no_blobs(void)
79
{
810
g_repo = NULL;
11+
object_type = GIT_OBJECT_BLOB;
12+
cache_limit = 0;
13+
}
14+
15+
void test_object_cache__initialize_cache_tiny_blobs(void)
16+
{
17+
g_repo = NULL;
18+
object_type = GIT_OBJECT_BLOB;
19+
cache_limit = 10;
20+
}
21+
22+
void test_object_cache__initialize_cache_all_blobs(void)
23+
{
24+
g_repo = NULL;
25+
object_type = GIT_OBJECT_BLOB;
26+
cache_limit = 32767;
27+
}
28+
29+
void test_object_cache__initialize_cache_no_trees(void)
30+
{
31+
g_repo = NULL;
32+
object_type = GIT_OBJECT_TREE;
33+
cache_limit = 0;
934
}
1035

1136
void test_object_cache__cleanup(void)
@@ -14,47 +39,49 @@ void test_object_cache__cleanup(void)
1439
g_repo = NULL;
1540

1641
git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_BLOB, (size_t)0);
42+
git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_TREE, (size_t)4096);
43+
git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_COMMIT, (size_t)4096);
1744
}
1845

1946
static struct {
2047
git_object_t type;
2148
const char *sha;
49+
size_t size;
2250
} g_data[] = {
2351
/* HEAD */
24-
{ GIT_OBJECT_BLOB, "a8233120f6ad708f843d861ce2b7228ec4e3dec6" }, /* README */
25-
{ GIT_OBJECT_BLOB, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc" }, /* branch_file.txt */
26-
{ GIT_OBJECT_BLOB, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd" }, /* new.txt */
52+
{ GIT_OBJECT_BLOB, "a8233120f6ad708f843d861ce2b7228ec4e3dec6", 10 }, /* README */
53+
{ GIT_OBJECT_BLOB, "3697d64be941a53d4ae8f6a271e4e3fa56b022cc", 8 }, /* branch_file.txt */
54+
{ GIT_OBJECT_BLOB, "a71586c1dfe8a71c6cbf6c129f404c5642ff31bd", 12 }, /* new.txt */
2755

2856
/* refs/heads/subtrees */
29-
{ GIT_OBJECT_BLOB, "1385f264afb75a56a5bec74243be9b367ba4ca08" }, /* README */
30-
{ GIT_OBJECT_TREE, "f1425cef211cc08caa31e7b545ffb232acb098c3" }, /* ab */
31-
{ GIT_OBJECT_BLOB, "d6c93164c249c8000205dd4ec5cbca1b516d487f" }, /* ab/4.txt */
32-
{ GIT_OBJECT_TREE, "9a03079b8a8ee85a0bee58bf9be3da8b62414ed4" }, /* ab/c */
33-
{ GIT_OBJECT_BLOB, "270b8ea76056d5cad83af921837702d3e3c2924d" }, /* ab/c/3.txt */
34-
{ GIT_OBJECT_TREE, "b6361fc6a97178d8fc8639fdeed71c775ab52593" }, /* ab/de */
35-
{ GIT_OBJECT_BLOB, "e7b4ad382349ff96dd8199000580b9b1e2042eb0" }, /* ab/de/2.txt */
36-
{ GIT_OBJECT_TREE, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54" }, /* ab/de/fgh */
37-
{ GIT_OBJECT_BLOB, "1f67fc4386b2d171e0d21be1c447e12660561f9b" }, /* ab/de/fgh/1.txt */
38-
{ GIT_OBJECT_BLOB, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057" }, /* branch_file.txt */
39-
{ GIT_OBJECT_BLOB, "fa49b077972391ad58037050f2a75f74e3671e92" }, /* new.txt */
57+
{ GIT_OBJECT_BLOB, "1385f264afb75a56a5bec74243be9b367ba4ca08", 4 }, /* README */
58+
{ GIT_OBJECT_TREE, "f1425cef211cc08caa31e7b545ffb232acb098c3", 90 }, /* ab */
59+
{ GIT_OBJECT_BLOB, "d6c93164c249c8000205dd4ec5cbca1b516d487f", 6 }, /* ab/4.txt */
60+
{ GIT_OBJECT_TREE, "9a03079b8a8ee85a0bee58bf9be3da8b62414ed4", 33 }, /* ab/c */
61+
{ GIT_OBJECT_BLOB, "270b8ea76056d5cad83af921837702d3e3c2924d", 6 }, /* ab/c/3.txt */
62+
{ GIT_OBJECT_TREE, "b6361fc6a97178d8fc8639fdeed71c775ab52593", 63 }, /* ab/de */
63+
{ GIT_OBJECT_BLOB, "e7b4ad382349ff96dd8199000580b9b1e2042eb0", 6 }, /* ab/de/2.txt */
64+
{ GIT_OBJECT_TREE, "3259a6bd5b57fb9c1281bb7ed3167b50f224cb54", 33 }, /* ab/de/fgh */
65+
{ GIT_OBJECT_BLOB, "1f67fc4386b2d171e0d21be1c447e12660561f9b", 6 }, /* ab/de/fgh/1.txt */
66+
{ GIT_OBJECT_BLOB, "45b983be36b73c0788dc9cbcb76cbb80fc7bb057", 3 }, /* branch_file.txt */
67+
{ GIT_OBJECT_BLOB, "fa49b077972391ad58037050f2a75f74e3671e92", 9 }, /* new.txt */
4068

4169
/* refs/heads/chomped */
42-
{ GIT_OBJECT_BLOB, "0266163a49e280c4f5ed1e08facd36a2bd716bcf" }, /* readme.txt */
70+
{ GIT_OBJECT_BLOB, "0266163a49e280c4f5ed1e08facd36a2bd716bcf", 51 }, /* readme.txt */
4371

44-
{ 0, NULL },
45-
{ 0, NULL }
72+
{ 0, NULL, 0 },
73+
{ 0, NULL, 0 }
4674
};
4775

48-
void test_object_cache__cache_everything(void)
76+
void test_object_cache__cache_counts(void)
4977
{
50-
int i, start;
78+
int i, start, nonmatching = 0;
5179
git_oid oid;
5280
git_odb_object *odb_obj;
5381
git_object *obj;
5482
git_odb *odb;
5583

56-
git_libgit2_opts(
57-
GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_BLOB, (size_t)32767);
84+
git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, object_type, cache_limit);
5885

5986
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
6087
cl_git_pass(git_repository_odb(&odb, g_repo));
@@ -77,12 +104,16 @@ void test_object_cache__cache_everything(void)
77104
git_object_free(obj);
78105
}
79106

80-
cl_assert_equal_i(count + 1, (int)git_cache_size(&g_repo->objects));
107+
if ((g_data[i].type == object_type && g_data[i].size >= cache_limit) ||
108+
(g_data[i].type != object_type && g_data[i].type == GIT_OBJECT_BLOB))
109+
cl_assert_equal_i(count, (int)git_cache_size(&g_repo->objects));
110+
else {
111+
cl_assert_equal_i(count + 1, (int)git_cache_size(&g_repo->objects));
112+
nonmatching++;
113+
}
81114
}
82115

83-
cl_assert_equal_i(i, (int)git_cache_size(&g_repo->objects) - start);
84-
85-
git_odb_free(odb);
116+
cl_assert_equal_i(nonmatching, (int)git_cache_size(&g_repo->objects) - start);
86117

87118
for (i = 0; g_data[i].sha != NULL; ++i) {
88119
int count = (int)git_cache_size(&g_repo->objects);
@@ -94,48 +125,6 @@ void test_object_cache__cache_everything(void)
94125

95126
cl_assert_equal_i(count, (int)git_cache_size(&g_repo->objects));
96127
}
97-
}
98-
99-
void test_object_cache__cache_no_blobs(void)
100-
{
101-
int i, start, nonblobs = 0;
102-
git_oid oid;
103-
git_odb_object *odb_obj;
104-
git_object *obj;
105-
git_odb *odb;
106-
107-
git_libgit2_opts(GIT_OPT_SET_CACHE_OBJECT_LIMIT, (int)GIT_OBJECT_BLOB, (size_t)0);
108-
109-
cl_git_pass(git_repository_open(&g_repo, cl_fixture("testrepo.git")));
110-
cl_git_pass(git_repository_odb(&odb, g_repo));
111-
112-
start = (int)git_cache_size(&g_repo->objects);
113-
114-
for (i = 0; g_data[i].sha != NULL; ++i) {
115-
int count = (int)git_cache_size(&g_repo->objects);
116-
117-
cl_git_pass(git_oid_fromstr(&oid, g_data[i].sha));
118-
119-
/* alternate between loading raw and parsed objects */
120-
if ((i & 1) == 0) {
121-
cl_git_pass(git_odb_read(&odb_obj, odb, &oid));
122-
cl_assert(g_data[i].type == git_odb_object_type(odb_obj));
123-
git_odb_object_free(odb_obj);
124-
} else {
125-
cl_git_pass(git_object_lookup(&obj, g_repo, &oid, GIT_OBJECT_ANY));
126-
cl_assert(g_data[i].type == git_object_type(obj));
127-
git_object_free(obj);
128-
}
129-
130-
if (g_data[i].type == GIT_OBJECT_BLOB)
131-
cl_assert_equal_i(count, (int)git_cache_size(&g_repo->objects));
132-
else {
133-
cl_assert_equal_i(count + 1, (int)git_cache_size(&g_repo->objects));
134-
nonblobs++;
135-
}
136-
}
137-
138-
cl_assert_equal_i(nonblobs, (int)git_cache_size(&g_repo->objects) - start);
139128

140129
git_odb_free(odb);
141130
}

0 commit comments

Comments
 (0)