Skip to content

Commit ff7652c

Browse files
authored
Merge pull request libgit2#5098 from pks-t/pks/clar-data-driven
Data-driven tests
2 parents fd734f7 + 1f47efc commit ff7652c

File tree

2 files changed

+86
-82
lines changed

2 files changed

+86
-82
lines changed

tests/generate.py

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class DeclarationTemplate(Template):
2424
def render(self):
2525
out = "\n".join("extern %s;" % cb['declaration'] for cb in self.module.callbacks) + "\n"
2626

27-
if self.module.initialize:
28-
out += "extern %s;\n" % self.module.initialize['declaration']
27+
for initializer in self.module.initializers:
28+
out += "extern %s;\n" % initializer['declaration']
2929

3030
if self.module.cleanup:
3131
out += "extern %s;\n" % self.module.cleanup['declaration']
@@ -41,22 +41,37 @@ def render(self):
4141

4242
class InfoTemplate(Template):
4343
def render(self):
44-
return Template(
44+
templates = []
45+
46+
initializers = self.module.initializers
47+
if len(initializers) == 0:
48+
initializers = [ None ]
49+
50+
for initializer in initializers:
51+
name = self.module.clean_name()
52+
if initializer and initializer['short_name'].startswith('initialize_'):
53+
variant = initializer['short_name'][len('initialize_'):]
54+
name += " (%s)" % variant.replace('_', ' ')
55+
56+
template = Template(
4557
r"""
4658
{
4759
"${clean_name}",
4860
${initialize},
4961
${cleanup},
5062
${cb_ptr}, ${cb_count}, ${enabled}
5163
}"""
52-
).substitute(
53-
clean_name = self.module.clean_name(),
54-
initialize = self._render_callback(self.module.initialize),
55-
cleanup = self._render_callback(self.module.cleanup),
56-
cb_ptr = "_clar_cb_%s" % self.module.name,
57-
cb_count = len(self.module.callbacks),
58-
enabled = int(self.module.enabled)
59-
)
64+
).substitute(
65+
clean_name = name,
66+
initialize = self._render_callback(initializer),
67+
cleanup = self._render_callback(self.module.cleanup),
68+
cb_ptr = "_clar_cb_%s" % self.module.name,
69+
cb_count = len(self.module.callbacks),
70+
enabled = int(self.module.enabled)
71+
)
72+
templates.append(template)
73+
74+
return ','.join(templates)
6075

6176
def __init__(self, name):
6277
self.name = name
@@ -86,7 +101,7 @@ def parse(self, contents):
86101
regex = re.compile(TEST_FUNC_REGEX % self.name, re.MULTILINE)
87102

88103
self.callbacks = []
89-
self.initialize = None
104+
self.initializers = []
90105
self.cleanup = None
91106

92107
for (declaration, symbol, short_name) in regex.findall(contents):
@@ -96,8 +111,8 @@ def parse(self, contents):
96111
"symbol" : symbol
97112
}
98113

99-
if short_name == 'initialize':
100-
self.initialize = data
114+
if short_name.startswith('initialize'):
115+
self.initializers.append(data)
101116
elif short_name == 'cleanup':
102117
self.cleanup = data
103118
else:

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)