Skip to content

Commit e6d9361

Browse files
committed
refactor: move utility tests into util
1 parent 2b09b5d commit e6d9361

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+708
-776
lines changed

ci/test.sh

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,25 @@ fi
156156

157157
# Run the tests that do not require network connectivity.
158158

159+
if [ -z "$SKIP_UTILITY_TESTS" ]; then
160+
run_test util
161+
fi
162+
159163
if [ -z "$SKIP_OFFLINE_TESTS" ]; then
160164
echo ""
161165
echo "##############################################################################"
162-
echo "## Running (offline) tests"
166+
echo "## Running core tests"
163167
echo "##############################################################################"
164168

169+
echo ""
170+
echo "Running libgit2 integration (offline) tests"
171+
echo ""
165172
run_test offline
173+
174+
echo ""
175+
echo "Running utility tests"
176+
echo ""
177+
run_test util
166178
fi
167179

168180
if [ -n "$RUN_INVASIVE_TESTS" ]; then
@@ -186,7 +198,7 @@ if [ -z "$SKIP_ONLINE_TESTS" ]; then
186198

187199
echo ""
188200
echo "##############################################################################"
189-
echo "## Running (online) tests"
201+
echo "## Running networking (online) tests"
190202
echo "##############################################################################"
191203

192204
export GITTEST_REMOTE_REDIRECT_INITIAL="http://localhost:9000/initial-redirect/libgit2/TestGitRepository"
@@ -198,9 +210,9 @@ if [ -z "$SKIP_ONLINE_TESTS" ]; then
198210
# Run the online tests that immutably change global state separately
199211
# to avoid polluting the test environment.
200212
echo ""
201-
echo "##############################################################################"
202-
echo "## Running (online_customcert) tests"
203-
echo "##############################################################################"
213+
echo "Running custom certificate (online_customcert) tests"
214+
echo ""
215+
204216
run_test online_customcert
205217
fi
206218

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33

44
add_subdirectory(headertest)
55
add_subdirectory(libgit2)
6+
add_subdirectory(util)

tests/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ These are the unit and integration tests for the libgit2 projects.
1212
* `resources`
1313
These are the resources for the tests, including files and git
1414
repositories.
15+
* `util`
16+
These are tests of the common utility library.
1517

1618
## Writing tests for libgit2
1719

tests/libgit2/core/hashsig.c

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
#include "clar_libgit2.h"
2+
#include "git2/sys/hashsig.h"
3+
#include "futils.h"
4+
5+
#define SIMILARITY_TEST_DATA_1 \
6+
"000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
7+
"010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
8+
"020\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
9+
"030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
10+
"040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
11+
12+
void test_core_hashsig__similarity_metric(void)
13+
{
14+
git_hashsig *a, *b;
15+
git_str buf = GIT_STR_INIT;
16+
int sim;
17+
18+
/* in the first case, we compare data to itself and expect 100% match */
19+
20+
cl_git_pass(git_str_sets(&buf, SIMILARITY_TEST_DATA_1));
21+
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
22+
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
23+
24+
cl_assert_equal_i(100, git_hashsig_compare(a, b));
25+
26+
git_hashsig_free(a);
27+
git_hashsig_free(b);
28+
29+
/* if we change just a single byte, how much does that change magnify? */
30+
31+
cl_git_pass(git_str_sets(&buf, SIMILARITY_TEST_DATA_1));
32+
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
33+
cl_git_pass(git_str_sets(&buf,
34+
"000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
35+
"010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
36+
"x020x\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
37+
"030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
38+
"040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
39+
));
40+
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
41+
42+
sim = git_hashsig_compare(a, b);
43+
44+
cl_assert_in_range(95, sim, 100); /* expect >95% similarity */
45+
46+
git_hashsig_free(a);
47+
git_hashsig_free(b);
48+
49+
/* let's try comparing data to a superset of itself */
50+
51+
cl_git_pass(git_str_sets(&buf, SIMILARITY_TEST_DATA_1));
52+
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
53+
cl_git_pass(git_str_sets(&buf, SIMILARITY_TEST_DATA_1
54+
"050\n051\n052\n053\n054\n055\n056\n057\n058\n059\n"));
55+
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
56+
57+
sim = git_hashsig_compare(a, b);
58+
/* 20% lines added ~= 10% lines changed */
59+
60+
cl_assert_in_range(85, sim, 95); /* expect similarity around 90% */
61+
62+
git_hashsig_free(a);
63+
git_hashsig_free(b);
64+
65+
/* what if we keep about half the original data and add half new */
66+
67+
cl_git_pass(git_str_sets(&buf, SIMILARITY_TEST_DATA_1));
68+
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
69+
cl_git_pass(git_str_sets(&buf,
70+
"000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
71+
"010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
72+
"020x\n021\n022\n023\n024\n" \
73+
"x25\nx26\nx27\nx28\nx29\n" \
74+
"x30\nx31\nx32\nx33\nx34\nx35\nx36\nx37\nx38\nx39\n" \
75+
"x40\nx41\nx42\nx43\nx44\nx45\nx46\nx47\nx48\nx49\n"
76+
));
77+
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
78+
79+
sim = git_hashsig_compare(a, b);
80+
/* 50% lines changed */
81+
82+
cl_assert_in_range(40, sim, 60); /* expect in the 40-60% similarity range */
83+
84+
git_hashsig_free(a);
85+
git_hashsig_free(b);
86+
87+
/* lastly, let's check that we can hash file content as well */
88+
89+
cl_git_pass(git_str_sets(&buf, SIMILARITY_TEST_DATA_1));
90+
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
91+
92+
cl_git_pass(git_futils_mkdir("scratch", 0755, GIT_MKDIR_PATH));
93+
cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1);
94+
cl_git_pass(git_hashsig_create_fromfile(
95+
&b, "scratch/testdata", GIT_HASHSIG_NORMAL));
96+
97+
cl_assert_equal_i(100, git_hashsig_compare(a, b));
98+
99+
git_hashsig_free(a);
100+
git_hashsig_free(b);
101+
102+
git_str_dispose(&buf);
103+
git_futils_rmdir_r("scratch", NULL, GIT_RMDIR_REMOVE_FILES);
104+
}
105+
106+
void test_core_hashsig__similarity_metric_whitespace(void)
107+
{
108+
git_hashsig *a, *b;
109+
git_str buf = GIT_STR_INIT;
110+
int sim, i, j;
111+
git_hashsig_option_t opt;
112+
const char *tabbed =
113+
" for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
114+
" separator = sep[s];\n"
115+
" expect = expect_values[s];\n"
116+
"\n"
117+
" for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
118+
" for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
119+
" git_str_join(&buf, separator, a[i], b[j]);\n"
120+
" cl_assert_equal_s(*expect, buf.ptr);\n"
121+
" expect++;\n"
122+
" }\n"
123+
" }\n"
124+
" }\n";
125+
const char *spaced =
126+
" for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
127+
" separator = sep[s];\n"
128+
" expect = expect_values[s];\n"
129+
"\n"
130+
" for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
131+
" for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
132+
" git_str_join(&buf, separator, a[i], b[j]);\n"
133+
" cl_assert_equal_s(*expect, buf.ptr);\n"
134+
" expect++;\n"
135+
" }\n"
136+
" }\n"
137+
" }\n";
138+
const char *crlf_spaced2 =
139+
" for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\r\n"
140+
" separator = sep[s];\r\n"
141+
" expect = expect_values[s];\r\n"
142+
"\r\n"
143+
" for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\r\n"
144+
" for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\r\n"
145+
" git_str_join(&buf, separator, a[i], b[j]);\r\n"
146+
" cl_assert_equal_s(*expect, buf.ptr);\r\n"
147+
" expect++;\r\n"
148+
" }\r\n"
149+
" }\r\n"
150+
" }\r\n";
151+
const char *text[3] = { tabbed, spaced, crlf_spaced2 };
152+
153+
/* let's try variations of our own code with whitespace changes */
154+
155+
for (opt = GIT_HASHSIG_NORMAL; opt <= GIT_HASHSIG_SMART_WHITESPACE; ++opt) {
156+
for (i = 0; i < 3; ++i) {
157+
for (j = 0; j < 3; ++j) {
158+
cl_git_pass(git_str_sets(&buf, text[i]));
159+
cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, opt));
160+
161+
cl_git_pass(git_str_sets(&buf, text[j]));
162+
cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, opt));
163+
164+
sim = git_hashsig_compare(a, b);
165+
166+
if (opt == GIT_HASHSIG_NORMAL) {
167+
if (i == j)
168+
cl_assert_equal_i(100, sim);
169+
else
170+
cl_assert_in_range(0, sim, 30); /* pretty different */
171+
} else {
172+
cl_assert_equal_i(100, sim);
173+
}
174+
175+
git_hashsig_free(a);
176+
git_hashsig_free(b);
177+
}
178+
}
179+
}
180+
181+
git_str_dispose(&buf);
182+
}

tests/libgit2/core/pool.c

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,9 @@
22
#include "pool.h"
33
#include "git2/oid.h"
44

5-
void test_core_pool__0(void)
6-
{
7-
int i;
8-
git_pool p;
9-
void *ptr;
10-
11-
git_pool_init(&p, 1);
12-
13-
for (i = 1; i < 10000; i *= 2) {
14-
ptr = git_pool_malloc(&p, i);
15-
cl_assert(ptr != NULL);
16-
cl_assert(git_pool__ptr_in_pool(&p, ptr));
17-
cl_assert(!git_pool__ptr_in_pool(&p, &i));
18-
}
19-
20-
git_pool_clear(&p);
21-
}
22-
23-
void test_core_pool__1(void)
24-
{
25-
int i;
26-
git_pool p;
27-
28-
git_pool_init(&p, 1);
29-
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-
git_pool_clear(&p);
39-
40-
git_pool_init(&p, 1);
41-
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-
git_pool_clear(&p);
51-
}
52-
535
static char to_hex[] = "0123456789abcdef";
546

55-
void test_core_pool__2(void)
7+
void test_core_pool__oid(void)
568
{
579
git_pool p;
5810
char oid_hex[GIT_OID_HEXSZ];
@@ -79,14 +31,3 @@ void test_core_pool__2(void)
7931
#endif
8032
git_pool_clear(&p);
8133
}
82-
83-
void test_core_pool__strndup_limit(void)
84-
{
85-
git_pool p;
86-
87-
git_pool_init(&p, 1);
88-
/* ensure 64 bit doesn't overflow */
89-
cl_assert(git_pool_strndup(&p, "foo", (size_t)-1) == NULL);
90-
git_pool_clear(&p);
91-
}
92-

tests/libgit2/diff/userdiff.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "clar_libgit2.h"
2+
3+
#include "userdiff.h"
4+
5+
static git_regexp regex;
6+
7+
void test_diff_userdiff__cleanup(void)
8+
{
9+
git_regexp_dispose(&regex);
10+
}
11+
12+
void test_diff_userdiff__compile_userdiff_regexps(void)
13+
{
14+
size_t idx;
15+
16+
for (idx = 0; idx < ARRAY_SIZE(builtin_defs); ++idx) {
17+
git_diff_driver_definition ddef = builtin_defs[idx];
18+
19+
cl_git_pass(git_regexp_compile(&regex, ddef.fns, ddef.flags));
20+
git_regexp_dispose(&regex);
21+
22+
cl_git_pass(git_regexp_compile(&regex, ddef.words, 0));
23+
git_regexp_dispose(&regex);
24+
}
25+
}

0 commit comments

Comments
 (0)