1 #include "clar_libgit2.h"
3 #include "git2/sys/hashsig.h"
6 #define TESTSTR "Have you seen that? Have you seeeen that??"
7 const char *test_string
= TESTSTR
;
8 const char *test_string_x2
= TESTSTR TESTSTR
;
10 #define TESTSTR_4096 REP1024("1234")
11 #define TESTSTR_8192 REP1024("12341234")
12 const char *test_4096
= TESTSTR_4096
;
13 const char *test_8192
= TESTSTR_8192
;
15 /* test basic data concatenation */
16 void test_core_buffer__0(void)
18 git_buf buf
= GIT_BUF_INIT
;
20 cl_assert(buf
.size
== 0);
22 git_buf_puts(&buf
, test_string
);
23 cl_assert(git_buf_oom(&buf
) == 0);
24 cl_assert_equal_s(test_string
, git_buf_cstr(&buf
));
26 git_buf_puts(&buf
, test_string
);
27 cl_assert(git_buf_oom(&buf
) == 0);
28 cl_assert_equal_s(test_string_x2
, git_buf_cstr(&buf
));
30 git_buf_dispose(&buf
);
33 /* test git_buf_printf */
34 void test_core_buffer__1(void)
36 git_buf buf
= GIT_BUF_INIT
;
38 git_buf_printf(&buf
, "%s %s %d ", "shoop", "da", 23);
39 cl_assert(git_buf_oom(&buf
) == 0);
40 cl_assert_equal_s("shoop da 23 ", git_buf_cstr(&buf
));
42 git_buf_printf(&buf
, "%s %d", "woop", 42);
43 cl_assert(git_buf_oom(&buf
) == 0);
44 cl_assert_equal_s("shoop da 23 woop 42", git_buf_cstr(&buf
));
46 git_buf_dispose(&buf
);
49 /* more thorough test of concatenation options */
50 void test_core_buffer__2(void)
52 git_buf buf
= GIT_BUF_INIT
;
56 cl_assert(buf
.size
== 0);
58 /* this must be safe to do */
59 git_buf_dispose(&buf
);
60 cl_assert(buf
.size
== 0);
61 cl_assert(buf
.asize
== 0);
63 /* empty buffer should be empty string */
64 cl_assert_equal_s("", git_buf_cstr(&buf
));
65 cl_assert(buf
.size
== 0);
66 /* cl_assert(buf.asize == 0); -- should not assume what git_buf does */
68 /* free should set us back to the beginning */
69 git_buf_dispose(&buf
);
70 cl_assert(buf
.size
== 0);
71 cl_assert(buf
.asize
== 0);
74 git_buf_putc(&buf
, '+');
75 cl_assert(git_buf_oom(&buf
) == 0);
76 cl_assert_equal_s("+", git_buf_cstr(&buf
));
78 /* add letter again */
79 git_buf_putc(&buf
, '+');
80 cl_assert(git_buf_oom(&buf
) == 0);
81 cl_assert_equal_s("++", git_buf_cstr(&buf
));
83 /* let's try that a few times */
84 for (i
= 0; i
< 16; ++i
) {
85 git_buf_putc(&buf
, '+');
86 cl_assert(git_buf_oom(&buf
) == 0);
88 cl_assert_equal_s("++++++++++++++++++", git_buf_cstr(&buf
));
90 git_buf_dispose(&buf
);
93 git_buf_put(&buf
, "xo", 2);
94 cl_assert(git_buf_oom(&buf
) == 0);
95 cl_assert_equal_s("xo", git_buf_cstr(&buf
));
97 /* add letter again */
98 git_buf_put(&buf
, "xo", 2);
99 cl_assert(git_buf_oom(&buf
) == 0);
100 cl_assert_equal_s("xoxo", git_buf_cstr(&buf
));
102 /* let's try that a few times */
103 for (i
= 0; i
< 16; ++i
) {
104 git_buf_put(&buf
, "xo", 2);
105 cl_assert(git_buf_oom(&buf
) == 0);
107 cl_assert_equal_s("xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo",
110 git_buf_dispose(&buf
);
113 git_buf_sets(&buf
, test_string
);
114 cl_assert(git_buf_oom(&buf
) == 0);
115 cl_assert_equal_s(test_string
, git_buf_cstr(&buf
));
118 git_buf_puts(&buf
, test_string
);
119 cl_assert(git_buf_oom(&buf
) == 0);
120 cl_assert_equal_s(test_string_x2
, git_buf_cstr(&buf
));
122 /* set to string again (should overwrite - not append) */
123 git_buf_sets(&buf
, test_string
);
124 cl_assert(git_buf_oom(&buf
) == 0);
125 cl_assert_equal_s(test_string
, git_buf_cstr(&buf
));
129 cl_assert_equal_s("", git_buf_cstr(&buf
));
131 git_buf_dispose(&buf
);
133 /* test extracting data into buffer */
134 git_buf_puts(&buf
, REP4("0123456789"));
135 cl_assert(git_buf_oom(&buf
) == 0);
137 git_buf_copy_cstr(data
, sizeof(data
), &buf
);
138 cl_assert_equal_s(REP4("0123456789"), data
);
139 git_buf_copy_cstr(data
, 11, &buf
);
140 cl_assert_equal_s("0123456789", data
);
141 git_buf_copy_cstr(data
, 3, &buf
);
142 cl_assert_equal_s("01", data
);
143 git_buf_copy_cstr(data
, 1, &buf
);
144 cl_assert_equal_s("", data
);
146 git_buf_copy_cstr(data
, sizeof(data
), &buf
);
147 cl_assert_equal_s(REP4("0123456789"), data
);
149 git_buf_sets(&buf
, REP256("x"));
150 git_buf_copy_cstr(data
, sizeof(data
), &buf
);
151 /* since sizeof(data) == 128, only 127 bytes should be copied */
152 cl_assert_equal_s(REP4(REP16("x")) REP16("x") REP16("x")
153 REP16("x") "xxxxxxxxxxxxxxx", data
);
155 git_buf_dispose(&buf
);
157 git_buf_copy_cstr(data
, sizeof(data
), &buf
);
158 cl_assert_equal_s("", data
);
161 /* let's do some tests with larger buffers to push our limits */
162 void test_core_buffer__3(void)
164 git_buf buf
= GIT_BUF_INIT
;
167 git_buf_set(&buf
, test_4096
, 4096);
168 cl_assert(git_buf_oom(&buf
) == 0);
169 cl_assert_equal_s(test_4096
, git_buf_cstr(&buf
));
172 git_buf_puts(&buf
, test_4096
);
173 cl_assert(git_buf_oom(&buf
) == 0);
174 cl_assert_equal_s(test_8192
, git_buf_cstr(&buf
));
176 /* set to string again (should overwrite - not append) */
177 git_buf_set(&buf
, test_4096
, 4096);
178 cl_assert(git_buf_oom(&buf
) == 0);
179 cl_assert_equal_s(test_4096
, git_buf_cstr(&buf
));
181 git_buf_dispose(&buf
);
184 /* let's try some producer/consumer tests */
185 void test_core_buffer__4(void)
187 git_buf buf
= GIT_BUF_INIT
;
190 for (i
= 0; i
< 10; ++i
) {
191 git_buf_puts(&buf
, "1234"); /* add 4 */
192 cl_assert(git_buf_oom(&buf
) == 0);
193 git_buf_consume(&buf
, buf
.ptr
+ 2); /* eat the first two */
194 cl_assert(strlen(git_buf_cstr(&buf
)) == (size_t)((i
+ 1) * 2));
196 /* we have appended 1234 10x and removed the first 20 letters */
197 cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf
));
199 git_buf_consume(&buf
, NULL
);
200 cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf
));
202 git_buf_consume(&buf
, "invalid pointer");
203 cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf
));
205 git_buf_consume(&buf
, buf
.ptr
);
206 cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf
));
208 git_buf_consume(&buf
, buf
.ptr
+ 1);
209 cl_assert_equal_s("2341234123412341234", git_buf_cstr(&buf
));
211 git_buf_consume(&buf
, buf
.ptr
+ buf
.size
);
212 cl_assert_equal_s("", git_buf_cstr(&buf
));
214 git_buf_dispose(&buf
);
222 const char* expected_data
,
223 size_t expected_size
,
224 size_t expected_asize
)
226 git_buf tgt
= GIT_BUF_INIT
;
228 git_buf_sets(&tgt
, data_a
);
229 cl_assert(git_buf_oom(&tgt
) == 0);
230 git_buf_puts(&tgt
, data_b
);
231 cl_assert(git_buf_oom(&tgt
) == 0);
232 cl_assert_equal_s(expected_data
, git_buf_cstr(&tgt
));
233 cl_assert_equal_i(tgt
.size
, expected_size
);
234 if (expected_asize
> 0)
235 cl_assert_equal_i(tgt
.asize
, expected_asize
);
237 git_buf_dispose(&tgt
);
241 check_buf_append_abc(
245 const char* expected_ab
,
246 const char* expected_abc
,
247 const char* expected_abca
,
248 const char* expected_abcab
,
249 const char* expected_abcabc
)
251 git_buf buf
= GIT_BUF_INIT
;
253 git_buf_sets(&buf
, buf_a
);
254 cl_assert(git_buf_oom(&buf
) == 0);
255 cl_assert_equal_s(buf_a
, git_buf_cstr(&buf
));
257 git_buf_puts(&buf
, buf_b
);
258 cl_assert(git_buf_oom(&buf
) == 0);
259 cl_assert_equal_s(expected_ab
, git_buf_cstr(&buf
));
261 git_buf_puts(&buf
, buf_c
);
262 cl_assert(git_buf_oom(&buf
) == 0);
263 cl_assert_equal_s(expected_abc
, git_buf_cstr(&buf
));
265 git_buf_puts(&buf
, buf_a
);
266 cl_assert(git_buf_oom(&buf
) == 0);
267 cl_assert_equal_s(expected_abca
, git_buf_cstr(&buf
));
269 git_buf_puts(&buf
, buf_b
);
270 cl_assert(git_buf_oom(&buf
) == 0);
271 cl_assert_equal_s(expected_abcab
, git_buf_cstr(&buf
));
273 git_buf_puts(&buf
, buf_c
);
274 cl_assert(git_buf_oom(&buf
) == 0);
275 cl_assert_equal_s(expected_abcabc
, git_buf_cstr(&buf
));
277 git_buf_dispose(&buf
);
280 /* more variations on append tests */
281 void test_core_buffer__5(void)
283 check_buf_append("", "", "", 0, 0);
284 check_buf_append("a", "", "a", 1, 0);
285 check_buf_append("", "a", "a", 1, 8);
286 check_buf_append("", "a", "a", 1, 8);
287 check_buf_append("a", "b", "ab", 2, 8);
288 check_buf_append("", "abcdefgh", "abcdefgh", 8, 16);
289 check_buf_append("abcdefgh", "", "abcdefgh", 8, 16);
291 /* buffer with starting asize will grow to:
292 * 1 -> 2, 2 -> 3, 3 -> 5, 4 -> 6, 5 -> 8, 6 -> 9,
293 * 7 -> 11, 8 -> 12, 9 -> 14, 10 -> 15, 11 -> 17, 12 -> 18,
294 * 13 -> 20, 14 -> 21, 15 -> 23, 16 -> 24, 17 -> 26, 18 -> 27,
295 * 19 -> 29, 20 -> 30, 21 -> 32, 22 -> 33, 23 -> 35, 24 -> 36,
297 * follow sequence until value > target size,
298 * then round up to nearest multiple of 8.
301 check_buf_append("abcdefgh", "/", "abcdefgh/", 9, 16);
302 check_buf_append("abcdefgh", "ijklmno", "abcdefghijklmno", 15, 16);
303 check_buf_append("abcdefgh", "ijklmnop", "abcdefghijklmnop", 16, 24);
304 check_buf_append("0123456789", "0123456789",
305 "01234567890123456789", 20, 24);
306 check_buf_append(REP16("x"), REP16("o"),
307 REP16("x") REP16("o"), 32, 40);
309 check_buf_append(test_4096
, "", test_4096
, 4096, 4104);
310 check_buf_append(test_4096
, test_4096
, test_8192
, 8192, 8200);
312 /* check sequences of appends */
313 check_buf_append_abc("a", "b", "c",
314 "ab", "abc", "abca", "abcab", "abcabc");
315 check_buf_append_abc("a1", "b2", "c3",
316 "a1b2", "a1b2c3", "a1b2c3a1",
317 "a1b2c3a1b2", "a1b2c3a1b2c3");
318 check_buf_append_abc("a1/", "b2/", "c3/",
319 "a1/b2/", "a1/b2/c3/", "a1/b2/c3/a1/",
320 "a1/b2/c3/a1/b2/", "a1/b2/c3/a1/b2/c3/");
324 void test_core_buffer__6(void)
326 git_buf a
= GIT_BUF_INIT
;
327 git_buf b
= GIT_BUF_INIT
;
329 git_buf_sets(&a
, "foo");
330 cl_assert(git_buf_oom(&a
) == 0);
331 git_buf_sets(&b
, "bar");
332 cl_assert(git_buf_oom(&b
) == 0);
334 cl_assert_equal_s("foo", git_buf_cstr(&a
));
335 cl_assert_equal_s("bar", git_buf_cstr(&b
));
337 git_buf_swap(&a
, &b
);
339 cl_assert_equal_s("bar", git_buf_cstr(&a
));
340 cl_assert_equal_s("foo", git_buf_cstr(&b
));
347 /* test detach/attach data */
348 void test_core_buffer__7(void)
350 const char *fun
= "This is fun";
351 git_buf a
= GIT_BUF_INIT
;
354 git_buf_sets(&a
, "foo");
355 cl_assert(git_buf_oom(&a
) == 0);
356 cl_assert_equal_s("foo", git_buf_cstr(&a
));
358 b
= git_buf_detach(&a
);
360 cl_assert_equal_s("foo", b
);
361 cl_assert_equal_s("", a
.ptr
);
364 b
= git_buf_detach(&a
);
366 cl_assert_equal_s(NULL
, b
);
367 cl_assert_equal_s("", a
.ptr
);
371 b
= git__strdup(fun
);
372 git_buf_attach(&a
, b
, 0);
374 cl_assert_equal_s(fun
, a
.ptr
);
375 cl_assert(a
.size
== strlen(fun
));
376 cl_assert(a
.asize
== strlen(fun
) + 1);
380 b
= git__strdup(fun
);
381 git_buf_attach(&a
, b
, strlen(fun
) + 1);
383 cl_assert_equal_s(fun
, a
.ptr
);
384 cl_assert(a
.size
== strlen(fun
));
385 cl_assert(a
.asize
== strlen(fun
) + 1);
395 const char *expected
)
398 git_buf buf
= GIT_BUF_INIT
;
400 git_buf_join(&buf
, sep
, a
, b
);
401 cl_assert(git_buf_oom(&buf
) == 0);
402 cl_assert_equal_s(expected
, git_buf_cstr(&buf
));
403 git_buf_dispose(&buf
);
407 check_joinbuf_overlapped(
411 const char *expected
)
414 git_buf buf
= GIT_BUF_INIT
;
416 git_buf_sets(&buf
, oldval
);
417 git_buf_join(&buf
, sep
, buf
.ptr
+ ofs_a
, b
);
418 cl_assert(git_buf_oom(&buf
) == 0);
419 cl_assert_equal_s(expected
, git_buf_cstr(&buf
));
420 git_buf_dispose(&buf
);
427 const char *expected
)
430 git_buf buf
= GIT_BUF_INIT
;
432 git_buf_sets(&buf
, a
);
433 cl_assert(git_buf_oom(&buf
) == 0);
435 git_buf_join_n(&buf
, sep
, 1, b
);
436 cl_assert(git_buf_oom(&buf
) == 0);
437 cl_assert_equal_s(expected
, git_buf_cstr(&buf
));
439 git_buf_dispose(&buf
);
448 const char *expected
)
451 git_buf buf
= GIT_BUF_INIT
;
452 git_buf_join_n(&buf
, sep
, 4, a
, b
, c
, d
);
453 cl_assert(git_buf_oom(&buf
) == 0);
454 cl_assert_equal_s(expected
, git_buf_cstr(&buf
));
455 git_buf_dispose(&buf
);
459 void test_core_buffer__8(void)
461 git_buf a
= GIT_BUF_INIT
;
463 git_buf_join_n(&a
, '/', 1, "foo");
464 cl_assert(git_buf_oom(&a
) == 0);
465 cl_assert_equal_s("foo", git_buf_cstr(&a
));
467 git_buf_join_n(&a
, '/', 1, "bar");
468 cl_assert(git_buf_oom(&a
) == 0);
469 cl_assert_equal_s("foo/bar", git_buf_cstr(&a
));
471 git_buf_join_n(&a
, '/', 1, "baz");
472 cl_assert(git_buf_oom(&a
) == 0);
473 cl_assert_equal_s("foo/bar/baz", git_buf_cstr(&a
));
477 check_joinbuf_2(NULL
, "", "");
478 check_joinbuf_2(NULL
, "a", "a");
479 check_joinbuf_2(NULL
, "/a", "/a");
480 check_joinbuf_2("", "", "");
481 check_joinbuf_2("", "a", "a");
482 check_joinbuf_2("", "/a", "/a");
483 check_joinbuf_2("a", "", "a/");
484 check_joinbuf_2("a", "/", "a/");
485 check_joinbuf_2("a", "b", "a/b");
486 check_joinbuf_2("/", "a", "/a");
487 check_joinbuf_2("/", "", "/");
488 check_joinbuf_2("/a", "/b", "/a/b");
489 check_joinbuf_2("/a", "/b/", "/a/b/");
490 check_joinbuf_2("/a/", "b/", "/a/b/");
491 check_joinbuf_2("/a/", "/b/", "/a/b/");
492 check_joinbuf_2("/a/", "//b/", "/a/b/");
493 check_joinbuf_2("/abcd", "/defg", "/abcd/defg");
494 check_joinbuf_2("/abcd", "/defg/", "/abcd/defg/");
495 check_joinbuf_2("/abcd/", "defg/", "/abcd/defg/");
496 check_joinbuf_2("/abcd/", "/defg/", "/abcd/defg/");
498 check_joinbuf_overlapped("abcd", 0, "efg", "abcd/efg");
499 check_joinbuf_overlapped("abcd", 1, "efg", "bcd/efg");
500 check_joinbuf_overlapped("abcd", 2, "efg", "cd/efg");
501 check_joinbuf_overlapped("abcd", 3, "efg", "d/efg");
502 check_joinbuf_overlapped("abcd", 4, "efg", "efg");
503 check_joinbuf_overlapped("abc/", 2, "efg", "c/efg");
504 check_joinbuf_overlapped("abc/", 3, "efg", "/efg");
505 check_joinbuf_overlapped("abc/", 4, "efg", "efg");
506 check_joinbuf_overlapped("abcd", 3, "", "d/");
507 check_joinbuf_overlapped("abcd", 4, "", "");
508 check_joinbuf_overlapped("abc/", 2, "", "c/");
509 check_joinbuf_overlapped("abc/", 3, "", "/");
510 check_joinbuf_overlapped("abc/", 4, "", "");
512 check_joinbuf_n_2("", "", "");
513 check_joinbuf_n_2("", "a", "a");
514 check_joinbuf_n_2("", "/a", "/a");
515 check_joinbuf_n_2("a", "", "a/");
516 check_joinbuf_n_2("a", "/", "a/");
517 check_joinbuf_n_2("a", "b", "a/b");
518 check_joinbuf_n_2("/", "a", "/a");
519 check_joinbuf_n_2("/", "", "/");
520 check_joinbuf_n_2("/a", "/b", "/a/b");
521 check_joinbuf_n_2("/a", "/b/", "/a/b/");
522 check_joinbuf_n_2("/a/", "b/", "/a/b/");
523 check_joinbuf_n_2("/a/", "/b/", "/a/b/");
524 check_joinbuf_n_2("/abcd", "/defg", "/abcd/defg");
525 check_joinbuf_n_2("/abcd", "/defg/", "/abcd/defg/");
526 check_joinbuf_n_2("/abcd/", "defg/", "/abcd/defg/");
527 check_joinbuf_n_2("/abcd/", "/defg/", "/abcd/defg/");
529 check_joinbuf_n_4("", "", "", "", "");
530 check_joinbuf_n_4("", "a", "", "", "a;");
531 check_joinbuf_n_4("a", "", "", "", "a;");
532 check_joinbuf_n_4("", "", "", "a", "a");
533 check_joinbuf_n_4("a", "b", "", ";c;d;", "a;b;c;d;");
534 check_joinbuf_n_4("a", "b", "", ";c;d", "a;b;c;d");
535 check_joinbuf_n_4("abcd", "efgh", "ijkl", "mnop", "abcd;efgh;ijkl;mnop");
536 check_joinbuf_n_4("abcd;", "efgh;", "ijkl;", "mnop;", "abcd;efgh;ijkl;mnop;");
537 check_joinbuf_n_4(";abcd;", ";efgh;", ";ijkl;", ";mnop;", ";abcd;efgh;ijkl;mnop;");
540 void test_core_buffer__9(void)
542 git_buf buf
= GIT_BUF_INIT
;
544 /* just some exhaustive tests of various separator placement */
545 char *a
[] = { "", "-", "a-", "-a", "-a-" };
546 char *b
[] = { "", "-", "b-", "-b", "-b-" };
547 char sep
[] = { 0, '-', '/' };
548 char *expect_null
[] = { "", "-", "a-", "-a", "-a-",
549 "-", "--", "a--", "-a-", "-a--",
550 "b-", "-b-", "a-b-", "-ab-", "-a-b-",
551 "-b", "--b", "a--b", "-a-b", "-a--b",
552 "-b-", "--b-", "a--b-", "-a-b-", "-a--b-" };
553 char *expect_dash
[] = { "", "-", "a-", "-a-", "-a-",
554 "-", "-", "a-", "-a-", "-a-",
555 "b-", "-b-", "a-b-", "-a-b-", "-a-b-",
556 "-b", "-b", "a-b", "-a-b", "-a-b",
557 "-b-", "-b-", "a-b-", "-a-b-", "-a-b-" };
558 char *expect_slas
[] = { "", "-/", "a-/", "-a/", "-a-/",
559 "-", "-/-", "a-/-", "-a/-", "-a-/-",
560 "b-", "-/b-", "a-/b-", "-a/b-", "-a-/b-",
561 "-b", "-/-b", "a-/-b", "-a/-b", "-a-/-b",
562 "-b-", "-/-b-", "a-/-b-", "-a/-b-", "-a-/-b-" };
563 char **expect_values
[] = { expect_null
, expect_dash
, expect_slas
};
564 char separator
, **expect
;
565 unsigned int s
, i
, j
;
567 for (s
= 0; s
< sizeof(sep
) / sizeof(char); ++s
) {
569 expect
= expect_values
[s
];
571 for (j
= 0; j
< sizeof(b
) / sizeof(char*); ++j
) {
572 for (i
= 0; i
< sizeof(a
) / sizeof(char*); ++i
) {
573 git_buf_join(&buf
, separator
, a
[i
], b
[j
]);
574 cl_assert_equal_s(*expect
, buf
.ptr
);
580 git_buf_dispose(&buf
);
583 void test_core_buffer__10(void)
585 git_buf a
= GIT_BUF_INIT
;
587 cl_git_pass(git_buf_join_n(&a
, '/', 1, "test"));
588 cl_assert_equal_s(a
.ptr
, "test");
589 cl_git_pass(git_buf_join_n(&a
, '/', 1, "string"));
590 cl_assert_equal_s(a
.ptr
, "test/string");
592 cl_git_pass(git_buf_join_n(&a
, '/', 3, "test", "string", "join"));
593 cl_assert_equal_s(a
.ptr
, "test/string/join");
594 cl_git_pass(git_buf_join_n(&a
, '/', 2, a
.ptr
, "more"));
595 cl_assert_equal_s(a
.ptr
, "test/string/join/test/string/join/more");
600 void test_core_buffer__join3(void)
602 git_buf a
= GIT_BUF_INIT
;
604 cl_git_pass(git_buf_join3(&a
, '/', "test", "string", "join"));
605 cl_assert_equal_s("test/string/join", a
.ptr
);
606 cl_git_pass(git_buf_join3(&a
, '/', "test/", "string", "join"));
607 cl_assert_equal_s("test/string/join", a
.ptr
);
608 cl_git_pass(git_buf_join3(&a
, '/', "test/", "/string", "join"));
609 cl_assert_equal_s("test/string/join", a
.ptr
);
610 cl_git_pass(git_buf_join3(&a
, '/', "test/", "/string/", "join"));
611 cl_assert_equal_s("test/string/join", a
.ptr
);
612 cl_git_pass(git_buf_join3(&a
, '/', "test/", "/string/", "/join"));
613 cl_assert_equal_s("test/string/join", a
.ptr
);
615 cl_git_pass(git_buf_join3(&a
, '/', "", "string", "join"));
616 cl_assert_equal_s("string/join", a
.ptr
);
617 cl_git_pass(git_buf_join3(&a
, '/', "", "string/", "join"));
618 cl_assert_equal_s("string/join", a
.ptr
);
619 cl_git_pass(git_buf_join3(&a
, '/', "", "string/", "/join"));
620 cl_assert_equal_s("string/join", a
.ptr
);
622 cl_git_pass(git_buf_join3(&a
, '/', "string", "", "join"));
623 cl_assert_equal_s("string/join", a
.ptr
);
624 cl_git_pass(git_buf_join3(&a
, '/', "string/", "", "join"));
625 cl_assert_equal_s("string/join", a
.ptr
);
626 cl_git_pass(git_buf_join3(&a
, '/', "string/", "", "/join"));
627 cl_assert_equal_s("string/join", a
.ptr
);
632 void test_core_buffer__11(void)
634 git_buf a
= GIT_BUF_INIT
;
635 char *t1
[] = { "nothing", "in", "common" };
636 char *t2
[] = { "something", "something else", "some other" };
637 char *t3
[] = { "something", "some fun", "no fun" };
638 char *t4
[] = { "happy", "happier", "happiest" };
639 char *t5
[] = { "happiest", "happier", "happy" };
640 char *t6
[] = { "no", "nope", "" };
641 char *t7
[] = { "", "doesn't matter" };
643 cl_git_pass(git_buf_common_prefix(&a
, t1
, 3));
644 cl_assert_equal_s(a
.ptr
, "");
646 cl_git_pass(git_buf_common_prefix(&a
, t2
, 3));
647 cl_assert_equal_s(a
.ptr
, "some");
649 cl_git_pass(git_buf_common_prefix(&a
, t3
, 3));
650 cl_assert_equal_s(a
.ptr
, "");
652 cl_git_pass(git_buf_common_prefix(&a
, t4
, 3));
653 cl_assert_equal_s(a
.ptr
, "happ");
655 cl_git_pass(git_buf_common_prefix(&a
, t5
, 3));
656 cl_assert_equal_s(a
.ptr
, "happ");
658 cl_git_pass(git_buf_common_prefix(&a
, t6
, 3));
659 cl_assert_equal_s(a
.ptr
, "");
661 cl_git_pass(git_buf_common_prefix(&a
, t7
, 3));
662 cl_assert_equal_s(a
.ptr
, "");
667 void test_core_buffer__rfind_variants(void)
669 git_buf a
= GIT_BUF_INIT
;
672 cl_git_pass(git_buf_sets(&a
, "/this/is/it/"));
674 len
= (ssize_t
)git_buf_len(&a
);
676 cl_assert(git_buf_rfind(&a
, '/') == len
- 1);
677 cl_assert(git_buf_rfind_next(&a
, '/') == len
- 4);
679 cl_assert(git_buf_rfind(&a
, 'i') == len
- 3);
680 cl_assert(git_buf_rfind_next(&a
, 'i') == len
- 3);
682 cl_assert(git_buf_rfind(&a
, 'h') == 2);
683 cl_assert(git_buf_rfind_next(&a
, 'h') == 2);
685 cl_assert(git_buf_rfind(&a
, 'q') == -1);
686 cl_assert(git_buf_rfind_next(&a
, 'q') == -1);
691 void test_core_buffer__puts_escaped(void)
693 git_buf a
= GIT_BUF_INIT
;
696 cl_git_pass(git_buf_puts_escaped(&a
, "this is a test", "", ""));
697 cl_assert_equal_s("this is a test", a
.ptr
);
700 cl_git_pass(git_buf_puts_escaped(&a
, "this is a test", "t", "\\"));
701 cl_assert_equal_s("\\this is a \\tes\\t", a
.ptr
);
704 cl_git_pass(git_buf_puts_escaped(&a
, "this is a test", "i ", "__"));
705 cl_assert_equal_s("th__is__ __is__ a__ test", a
.ptr
);
708 cl_git_pass(git_buf_puts_escape_regex(&a
, "^match\\s*[A-Z]+.*"));
709 cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a
.ptr
);
714 static void assert_unescape(char *expected
, char *to_unescape
) {
715 git_buf buf
= GIT_BUF_INIT
;
717 cl_git_pass(git_buf_sets(&buf
, to_unescape
));
718 git_buf_unescape(&buf
);
719 cl_assert_equal_s(expected
, buf
.ptr
);
720 cl_assert_equal_sz(strlen(expected
), buf
.size
);
722 git_buf_dispose(&buf
);
725 void test_core_buffer__unescape(void)
727 assert_unescape("Escaped\\", "Es\\ca\\ped\\");
728 assert_unescape("Es\\caped\\", "Es\\\\ca\\ped\\\\");
729 assert_unescape("\\", "\\");
730 assert_unescape("\\", "\\\\");
731 assert_unescape("", "");
734 void test_core_buffer__encode_base64(void)
736 git_buf buf
= GIT_BUF_INIT
;
740 * 0b 01110100 01101000 01101001 01110011
741 * 0b 011101 000110 100001 101001 011100 110000
742 * 0x 1d 06 21 29 1c 30
745 cl_git_pass(git_buf_encode_base64(&buf
, "this", 4));
746 cl_assert_equal_s("dGhpcw==", buf
.ptr
);
749 cl_git_pass(git_buf_encode_base64(&buf
, "this!", 5));
750 cl_assert_equal_s("dGhpcyE=", buf
.ptr
);
753 cl_git_pass(git_buf_encode_base64(&buf
, "this!\n", 6));
754 cl_assert_equal_s("dGhpcyEK", buf
.ptr
);
756 git_buf_dispose(&buf
);
759 void test_core_buffer__decode_base64(void)
761 git_buf buf
= GIT_BUF_INIT
;
763 cl_git_pass(git_buf_decode_base64(&buf
, "dGhpcw==", 8));
764 cl_assert_equal_s("this", buf
.ptr
);
767 cl_git_pass(git_buf_decode_base64(&buf
, "dGhpcyE=", 8));
768 cl_assert_equal_s("this!", buf
.ptr
);
771 cl_git_pass(git_buf_decode_base64(&buf
, "dGhpcyEK", 8));
772 cl_assert_equal_s("this!\n", buf
.ptr
);
774 cl_git_fail(git_buf_decode_base64(&buf
, "This is not a valid base64 string!!!", 36));
775 cl_assert_equal_s("this!\n", buf
.ptr
);
777 git_buf_dispose(&buf
);
780 void test_core_buffer__encode_base85(void)
782 git_buf buf
= GIT_BUF_INIT
;
784 cl_git_pass(git_buf_encode_base85(&buf
, "this", 4));
785 cl_assert_equal_s("bZBXF", buf
.ptr
);
788 cl_git_pass(git_buf_encode_base85(&buf
, "two rnds", 8));
789 cl_assert_equal_s("ba!tca&BaE", buf
.ptr
);
792 cl_git_pass(git_buf_encode_base85(&buf
, "this is base 85 encoded",
793 strlen("this is base 85 encoded")));
794 cl_assert_equal_s("bZBXFAZc?TVqtS-AUHK3Wo~0{WMyOk", buf
.ptr
);
797 git_buf_dispose(&buf
);
800 void test_core_buffer__decode_base85(void)
802 git_buf buf
= GIT_BUF_INIT
;
804 cl_git_pass(git_buf_decode_base85(&buf
, "bZBXF", 5, 4));
805 cl_assert_equal_sz(4, buf
.size
);
806 cl_assert_equal_s("this", buf
.ptr
);
809 cl_git_pass(git_buf_decode_base85(&buf
, "ba!tca&BaE", 10, 8));
810 cl_assert_equal_sz(8, buf
.size
);
811 cl_assert_equal_s("two rnds", buf
.ptr
);
814 cl_git_pass(git_buf_decode_base85(&buf
, "bZBXFAZc?TVqtS-AUHK3Wo~0{WMyOk", 30, 23));
815 cl_assert_equal_sz(23, buf
.size
);
816 cl_assert_equal_s("this is base 85 encoded", buf
.ptr
);
819 git_buf_dispose(&buf
);
822 void test_core_buffer__decode_base85_fails_gracefully(void)
824 git_buf buf
= GIT_BUF_INIT
;
826 git_buf_puts(&buf
, "foobar");
828 cl_git_fail(git_buf_decode_base85(&buf
, "invalid charsZZ", 15, 42));
829 cl_git_fail(git_buf_decode_base85(&buf
, "invalidchars__ ", 15, 42));
830 cl_git_fail(git_buf_decode_base85(&buf
, "overflowZZ~~~~~", 15, 42));
831 cl_git_fail(git_buf_decode_base85(&buf
, "truncated", 9, 42));
832 cl_assert_equal_sz(6, buf
.size
);
833 cl_assert_equal_s("foobar", buf
.ptr
);
835 git_buf_dispose(&buf
);
838 void test_core_buffer__classify_with_utf8(void)
840 char *data0
= "Simple text\n";
841 size_t data0len
= 12;
842 char *data1
= "Is that UTF-8 data I see…\nYep!\n";
843 size_t data1len
= 31;
844 char *data2
= "Internal NUL!!!\000\n\nI see you!\n";
845 size_t data2len
= 29;
846 char *data3
= "\xef\xbb\xbfThis is UTF-8 with a BOM.\n";
847 size_t data3len
= 20;
850 b
.ptr
= data0
; b
.size
= b
.asize
= data0len
;
851 cl_assert(!git_buf_is_binary(&b
));
852 cl_assert(!git_buf_contains_nul(&b
));
854 b
.ptr
= data1
; b
.size
= b
.asize
= data1len
;
855 cl_assert(!git_buf_is_binary(&b
));
856 cl_assert(!git_buf_contains_nul(&b
));
858 b
.ptr
= data2
; b
.size
= b
.asize
= data2len
;
859 cl_assert(git_buf_is_binary(&b
));
860 cl_assert(git_buf_contains_nul(&b
));
862 b
.ptr
= data3
; b
.size
= b
.asize
= data3len
;
863 cl_assert(!git_buf_is_binary(&b
));
864 cl_assert(!git_buf_contains_nul(&b
));
867 #define SIMILARITY_TEST_DATA_1 \
868 "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
869 "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
870 "020\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
871 "030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
872 "040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
874 void test_core_buffer__similarity_metric(void)
877 git_buf buf
= GIT_BUF_INIT
;
880 /* in the first case, we compare data to itself and expect 100% match */
882 cl_git_pass(git_buf_sets(&buf
, SIMILARITY_TEST_DATA_1
));
883 cl_git_pass(git_hashsig_create(&a
, buf
.ptr
, buf
.size
, GIT_HASHSIG_NORMAL
));
884 cl_git_pass(git_hashsig_create(&b
, buf
.ptr
, buf
.size
, GIT_HASHSIG_NORMAL
));
886 cl_assert_equal_i(100, git_hashsig_compare(a
, b
));
891 /* if we change just a single byte, how much does that change magnify? */
893 cl_git_pass(git_buf_sets(&buf
, SIMILARITY_TEST_DATA_1
));
894 cl_git_pass(git_hashsig_create(&a
, buf
.ptr
, buf
.size
, GIT_HASHSIG_NORMAL
));
895 cl_git_pass(git_buf_sets(&buf
,
896 "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
897 "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
898 "x020x\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
899 "030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
900 "040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
902 cl_git_pass(git_hashsig_create(&b
, buf
.ptr
, buf
.size
, GIT_HASHSIG_NORMAL
));
904 sim
= git_hashsig_compare(a
, b
);
906 cl_assert_in_range(95, sim
, 100); /* expect >95% similarity */
911 /* let's try comparing data to a superset of itself */
913 cl_git_pass(git_buf_sets(&buf
, SIMILARITY_TEST_DATA_1
));
914 cl_git_pass(git_hashsig_create(&a
, buf
.ptr
, buf
.size
, GIT_HASHSIG_NORMAL
));
915 cl_git_pass(git_buf_sets(&buf
, SIMILARITY_TEST_DATA_1
916 "050\n051\n052\n053\n054\n055\n056\n057\n058\n059\n"));
917 cl_git_pass(git_hashsig_create(&b
, buf
.ptr
, buf
.size
, GIT_HASHSIG_NORMAL
));
919 sim
= git_hashsig_compare(a
, b
);
920 /* 20% lines added ~= 10% lines changed */
922 cl_assert_in_range(85, sim
, 95); /* expect similarity around 90% */
927 /* what if we keep about half the original data and add half new */
929 cl_git_pass(git_buf_sets(&buf
, SIMILARITY_TEST_DATA_1
));
930 cl_git_pass(git_hashsig_create(&a
, buf
.ptr
, buf
.size
, GIT_HASHSIG_NORMAL
));
931 cl_git_pass(git_buf_sets(&buf
,
932 "000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
933 "010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
934 "020x\n021\n022\n023\n024\n" \
935 "x25\nx26\nx27\nx28\nx29\n" \
936 "x30\nx31\nx32\nx33\nx34\nx35\nx36\nx37\nx38\nx39\n" \
937 "x40\nx41\nx42\nx43\nx44\nx45\nx46\nx47\nx48\nx49\n"
939 cl_git_pass(git_hashsig_create(&b
, buf
.ptr
, buf
.size
, GIT_HASHSIG_NORMAL
));
941 sim
= git_hashsig_compare(a
, b
);
942 /* 50% lines changed */
944 cl_assert_in_range(40, sim
, 60); /* expect in the 40-60% similarity range */
949 /* lastly, let's check that we can hash file content as well */
951 cl_git_pass(git_buf_sets(&buf
, SIMILARITY_TEST_DATA_1
));
952 cl_git_pass(git_hashsig_create(&a
, buf
.ptr
, buf
.size
, GIT_HASHSIG_NORMAL
));
954 cl_git_pass(git_futils_mkdir("scratch", 0755, GIT_MKDIR_PATH
));
955 cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1
);
956 cl_git_pass(git_hashsig_create_fromfile(
957 &b
, "scratch/testdata", GIT_HASHSIG_NORMAL
));
959 cl_assert_equal_i(100, git_hashsig_compare(a
, b
));
964 git_buf_dispose(&buf
);
965 git_futils_rmdir_r("scratch", NULL
, GIT_RMDIR_REMOVE_FILES
);
969 void test_core_buffer__similarity_metric_whitespace(void)
972 git_buf buf
= GIT_BUF_INIT
;
974 git_hashsig_option_t opt
;
976 " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
977 " separator = sep[s];\n"
978 " expect = expect_values[s];\n"
980 " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
981 " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
982 " git_buf_join(&buf, separator, a[i], b[j]);\n"
983 " cl_assert_equal_s(*expect, buf.ptr);\n"
989 " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
990 " separator = sep[s];\n"
991 " expect = expect_values[s];\n"
993 " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
994 " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
995 " git_buf_join(&buf, separator, a[i], b[j]);\n"
996 " cl_assert_equal_s(*expect, buf.ptr);\n"
1001 const char *crlf_spaced2
=
1002 " for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\r\n"
1003 " separator = sep[s];\r\n"
1004 " expect = expect_values[s];\r\n"
1006 " for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\r\n"
1007 " for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\r\n"
1008 " git_buf_join(&buf, separator, a[i], b[j]);\r\n"
1009 " cl_assert_equal_s(*expect, buf.ptr);\r\n"
1014 const char *text
[3] = { tabbed
, spaced
, crlf_spaced2
};
1016 /* let's try variations of our own code with whitespace changes */
1018 for (opt
= GIT_HASHSIG_NORMAL
; opt
<= GIT_HASHSIG_SMART_WHITESPACE
; ++opt
) {
1019 for (i
= 0; i
< 3; ++i
) {
1020 for (j
= 0; j
< 3; ++j
) {
1021 cl_git_pass(git_buf_sets(&buf
, text
[i
]));
1022 cl_git_pass(git_hashsig_create(&a
, buf
.ptr
, buf
.size
, opt
));
1024 cl_git_pass(git_buf_sets(&buf
, text
[j
]));
1025 cl_git_pass(git_hashsig_create(&b
, buf
.ptr
, buf
.size
, opt
));
1027 sim
= git_hashsig_compare(a
, b
);
1029 if (opt
== GIT_HASHSIG_NORMAL
) {
1031 cl_assert_equal_i(100, sim
);
1033 cl_assert_in_range(0, sim
, 30); /* pretty different */
1035 cl_assert_equal_i(100, sim
);
1038 git_hashsig_free(a
);
1039 git_hashsig_free(b
);
1044 git_buf_dispose(&buf
);
1047 #include "../filter/crlf.h"
1049 #define check_buf(expected,buf) do { \
1050 cl_assert_equal_s(expected, buf.ptr); \
1051 cl_assert_equal_sz(strlen(expected), buf.size); } while (0)
1053 void test_core_buffer__lf_and_crlf_conversions(void)
1055 git_buf src
= GIT_BUF_INIT
, tgt
= GIT_BUF_INIT
;
1059 git_buf_sets(&src
, "lf\nlf\nlf\nlf\n");
1061 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1062 check_buf("lf\r\nlf\r\nlf\r\nlf\r\n", tgt
);
1064 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1065 check_buf(src
.ptr
, tgt
);
1067 git_buf_sets(&src
, "\nlf\nlf\nlf\nlf\nlf");
1069 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1070 check_buf("\r\nlf\r\nlf\r\nlf\r\nlf\r\nlf", tgt
);
1072 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1073 check_buf(src
.ptr
, tgt
);
1077 git_buf_sets(&src
, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n");
1079 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1080 check_buf("crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n", tgt
);
1082 git_buf_sets(&src
, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n");
1084 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1085 check_buf("crlf\ncrlf\ncrlf\ncrlf\n", tgt
);
1087 git_buf_sets(&src
, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf");
1089 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1090 check_buf("\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf", tgt
);
1092 git_buf_sets(&src
, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf");
1094 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1095 check_buf("\ncrlf\ncrlf\ncrlf\ncrlf\ncrlf", tgt
);
1097 /* CRLF in LF text */
1099 git_buf_sets(&src
, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n");
1101 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1102 check_buf("\r\nlf\r\nlf\r\ncrlf\r\nlf\r\nlf\r\ncrlf\r\n", tgt
);
1104 git_buf_sets(&src
, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n");
1106 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1107 check_buf("\nlf\nlf\ncrlf\nlf\nlf\ncrlf\n", tgt
);
1109 /* LF in CRLF text */
1111 git_buf_sets(&src
, "\ncrlf\r\ncrlf\r\nlf\ncrlf\r\ncrlf");
1113 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1114 check_buf("\r\ncrlf\r\ncrlf\r\nlf\r\ncrlf\r\ncrlf", tgt
);
1116 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1117 check_buf("\ncrlf\ncrlf\nlf\ncrlf\ncrlf", tgt
);
1121 git_buf_sets(&src
, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r");
1123 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1124 check_buf("\rcrlf\r\nlf\r\nlf\r\ncr\rcrlf\r\nlf\r\ncr\r", tgt
);
1126 git_buf_sets(&src
, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r");
1128 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1129 check_buf("\rcrlf\nlf\nlf\ncr\rcrlf\nlf\ncr\r", tgt
);
1131 git_buf_sets(&src
, "\rcr\r");
1132 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1133 check_buf(src
.ptr
, tgt
);
1134 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1135 check_buf("\rcr\r", tgt
);
1137 git_buf_dispose(&src
);
1138 git_buf_dispose(&tgt
);
1140 /* blob correspondence tests */
1142 git_buf_sets(&src
, ALL_CRLF_TEXT_RAW
);
1143 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1144 check_buf(ALL_CRLF_TEXT_AS_CRLF
, tgt
);
1145 git_buf_sets(&src
, ALL_CRLF_TEXT_RAW
);
1146 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1147 check_buf(ALL_CRLF_TEXT_AS_LF
, tgt
);
1148 git_buf_dispose(&src
);
1149 git_buf_dispose(&tgt
);
1151 git_buf_sets(&src
, ALL_LF_TEXT_RAW
);
1152 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1153 check_buf(ALL_LF_TEXT_AS_CRLF
, tgt
);
1154 git_buf_sets(&src
, ALL_LF_TEXT_RAW
);
1155 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1156 check_buf(ALL_LF_TEXT_AS_LF
, tgt
);
1157 git_buf_dispose(&src
);
1158 git_buf_dispose(&tgt
);
1160 git_buf_sets(&src
, MORE_CRLF_TEXT_RAW
);
1161 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1162 check_buf(MORE_CRLF_TEXT_AS_CRLF
, tgt
);
1163 git_buf_sets(&src
, MORE_CRLF_TEXT_RAW
);
1164 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1165 check_buf(MORE_CRLF_TEXT_AS_LF
, tgt
);
1166 git_buf_dispose(&src
);
1167 git_buf_dispose(&tgt
);
1169 git_buf_sets(&src
, MORE_LF_TEXT_RAW
);
1170 cl_git_pass(git_buf_lf_to_crlf(&tgt
, &src
));
1171 check_buf(MORE_LF_TEXT_AS_CRLF
, tgt
);
1172 git_buf_sets(&src
, MORE_LF_TEXT_RAW
);
1173 cl_git_pass(git_buf_crlf_to_lf(&tgt
, &src
));
1174 check_buf(MORE_LF_TEXT_AS_LF
, tgt
);
1175 git_buf_dispose(&src
);
1176 git_buf_dispose(&tgt
);
1179 void test_core_buffer__dont_grow_borrowed(void)
1181 const char *somestring
= "blah blah";
1182 git_buf buf
= GIT_BUF_INIT
;
1184 git_buf_attach_notowned(&buf
, somestring
, strlen(somestring
) + 1);
1185 cl_assert_equal_p(somestring
, buf
.ptr
);
1186 cl_assert_equal_i(0, buf
.asize
);
1187 cl_assert_equal_i(strlen(somestring
) + 1, buf
.size
);
1189 cl_git_fail_with(GIT_EINVALID
, git_buf_grow(&buf
, 1024));
1192 void test_core_buffer__dont_hit_infinite_loop_when_resizing(void)
1194 git_buf buf
= GIT_BUF_INIT
;
1196 cl_git_pass(git_buf_puts(&buf
, "foobar"));
1198 * We do not care whether this succeeds or fails, which
1199 * would depend on platform-specific allocation
1200 * semantics. We only want to know that the function
1203 (void)git_buf_try_grow(&buf
, SIZE_MAX
, true);
1205 git_buf_dispose(&buf
);
1208 void test_core_buffer__avoid_printing_into_oom_buffer(void)
1210 git_buf buf
= GIT_BUF_INIT
;
1212 /* Emulate OOM situation with a previous allocation */
1214 buf
.ptr
= git_buf__oom
;
1217 * Print the same string again. As the buffer still has
1218 * an `asize` of 8 due to the previous print,
1219 * `ENSURE_SIZE` would not try to reallocate the array at
1220 * all. As it didn't explicitly check for `git_buf__oom`
1221 * in earlier versions, this would've resulted in it
1222 * returning successfully and thus `git_buf_puts` would
1223 * just print into the `git_buf__oom` array.
1225 cl_git_fail(git_buf_puts(&buf
, "foobar"));