buffer.c 33.1 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "buffer.h"
3
#include "buf_text.h"
4
#include "git2/sys/hashsig.h"
5
#include "fileops.h"
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

#define TESTSTR "Have you seen that? Have you seeeen that??"
const char *test_string = TESTSTR;
const char *test_string_x2 = TESTSTR TESTSTR;

#define TESTSTR_4096 REP1024("1234")
#define TESTSTR_8192 REP1024("12341234")
const char *test_4096 = TESTSTR_4096;
const char *test_8192 = TESTSTR_8192;

/* test basic data concatenation */
void test_core_buffer__0(void)
{
	git_buf buf = GIT_BUF_INIT;

	cl_assert(buf.size == 0);

	git_buf_puts(&buf, test_string);
	cl_assert(git_buf_oom(&buf) == 0);
25
	cl_assert_equal_s(test_string, git_buf_cstr(&buf));
26 27 28

	git_buf_puts(&buf, test_string);
	cl_assert(git_buf_oom(&buf) == 0);
29
	cl_assert_equal_s(test_string_x2, git_buf_cstr(&buf));
30 31 32 33 34 35 36 37 38 39 40

	git_buf_free(&buf);
}

/* test git_buf_printf */
void test_core_buffer__1(void)
{
	git_buf buf = GIT_BUF_INIT;

	git_buf_printf(&buf, "%s %s %d ", "shoop", "da", 23);
	cl_assert(git_buf_oom(&buf) == 0);
41
	cl_assert_equal_s("shoop da 23 ", git_buf_cstr(&buf));
42 43 44

	git_buf_printf(&buf, "%s %d", "woop", 42);
	cl_assert(git_buf_oom(&buf) == 0);
45
	cl_assert_equal_s("shoop da 23 woop 42", git_buf_cstr(&buf));
46 47 48 49 50 51 52 53 54

	git_buf_free(&buf);
}

/* more thorough test of concatenation options */
void test_core_buffer__2(void)
{
	git_buf buf = GIT_BUF_INIT;
	int i;
55
	char data[128];
56 57 58 59 60 61 62 63 64

	cl_assert(buf.size == 0);

	/* this must be safe to do */
	git_buf_free(&buf);
	cl_assert(buf.size == 0);
	cl_assert(buf.asize == 0);

	/* empty buffer should be empty string */
65
	cl_assert_equal_s("", git_buf_cstr(&buf));
66
	cl_assert(buf.size == 0);
67
	/* cl_assert(buf.asize == 0); -- should not assume what git_buf does */
68 69 70 71 72 73 74 75 76

	/* free should set us back to the beginning */
	git_buf_free(&buf);
	cl_assert(buf.size == 0);
	cl_assert(buf.asize == 0);

	/* add letter */
	git_buf_putc(&buf, '+');
	cl_assert(git_buf_oom(&buf) == 0);
77
	cl_assert_equal_s("+", git_buf_cstr(&buf));
78 79 80 81

	/* add letter again */
	git_buf_putc(&buf, '+');
	cl_assert(git_buf_oom(&buf) == 0);
82
	cl_assert_equal_s("++", git_buf_cstr(&buf));
83 84 85 86 87 88

	/* let's try that a few times */
	for (i = 0; i < 16; ++i) {
		git_buf_putc(&buf, '+');
		cl_assert(git_buf_oom(&buf) == 0);
	}
89
	cl_assert_equal_s("++++++++++++++++++", git_buf_cstr(&buf));
90 91 92 93 94 95

	git_buf_free(&buf);

	/* add data */
	git_buf_put(&buf, "xo", 2);
	cl_assert(git_buf_oom(&buf) == 0);
96
	cl_assert_equal_s("xo", git_buf_cstr(&buf));
97 98 99 100

	/* add letter again */
	git_buf_put(&buf, "xo", 2);
	cl_assert(git_buf_oom(&buf) == 0);
101
	cl_assert_equal_s("xoxo", git_buf_cstr(&buf));
102 103 104 105 106 107

	/* let's try that a few times */
	for (i = 0; i < 16; ++i) {
		git_buf_put(&buf, "xo", 2);
		cl_assert(git_buf_oom(&buf) == 0);
	}
108
	cl_assert_equal_s("xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo",
109 110 111 112 113 114 115
					   git_buf_cstr(&buf));

	git_buf_free(&buf);

	/* set to string */
	git_buf_sets(&buf, test_string);
	cl_assert(git_buf_oom(&buf) == 0);
116
	cl_assert_equal_s(test_string, git_buf_cstr(&buf));
117 118 119 120

	/* append string */
	git_buf_puts(&buf, test_string);
	cl_assert(git_buf_oom(&buf) == 0);
121
	cl_assert_equal_s(test_string_x2, git_buf_cstr(&buf));
122 123 124 125

	/* set to string again (should overwrite - not append) */
	git_buf_sets(&buf, test_string);
	cl_assert(git_buf_oom(&buf) == 0);
126
	cl_assert_equal_s(test_string, git_buf_cstr(&buf));
127 128 129

	/* test clear */
	git_buf_clear(&buf);
130
	cl_assert_equal_s("", git_buf_cstr(&buf));
131 132

	git_buf_free(&buf);
133 134 135 136 137

	/* test extracting data into buffer */
	git_buf_puts(&buf, REP4("0123456789"));
	cl_assert(git_buf_oom(&buf) == 0);

138
	git_buf_copy_cstr(data, sizeof(data), &buf);
139
	cl_assert_equal_s(REP4("0123456789"), data);
140
	git_buf_copy_cstr(data, 11, &buf);
141
	cl_assert_equal_s("0123456789", data);
142
	git_buf_copy_cstr(data, 3, &buf);
143
	cl_assert_equal_s("01", data);
144
	git_buf_copy_cstr(data, 1, &buf);
145
	cl_assert_equal_s("", data);
146

147
	git_buf_copy_cstr(data, sizeof(data), &buf);
148
	cl_assert_equal_s(REP4("0123456789"), data);
149 150 151 152

	git_buf_sets(&buf, REP256("x"));
	git_buf_copy_cstr(data, sizeof(data), &buf);
	/* since sizeof(data) == 128, only 127 bytes should be copied */
153
	cl_assert_equal_s(REP4(REP16("x")) REP16("x") REP16("x")
154
					   REP16("x") "xxxxxxxxxxxxxxx", data);
155 156 157

	git_buf_free(&buf);

158
	git_buf_copy_cstr(data, sizeof(data), &buf);
159
	cl_assert_equal_s("", data);
160 161 162 163 164 165 166 167 168 169
}

/* let's do some tests with larger buffers to push our limits */
void test_core_buffer__3(void)
{
	git_buf buf = GIT_BUF_INIT;

	/* set to string */
	git_buf_set(&buf, test_4096, 4096);
	cl_assert(git_buf_oom(&buf) == 0);
170
	cl_assert_equal_s(test_4096, git_buf_cstr(&buf));
171 172 173 174

	/* append string */
	git_buf_puts(&buf, test_4096);
	cl_assert(git_buf_oom(&buf) == 0);
175
	cl_assert_equal_s(test_8192, git_buf_cstr(&buf));
176 177 178 179

	/* set to string again (should overwrite - not append) */
	git_buf_set(&buf, test_4096, 4096);
	cl_assert(git_buf_oom(&buf) == 0);
180
	cl_assert_equal_s(test_4096, git_buf_cstr(&buf));
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197

	git_buf_free(&buf);
}

/* let's try some producer/consumer tests */
void test_core_buffer__4(void)
{
	git_buf buf = GIT_BUF_INIT;
	int i;

	for (i = 0; i < 10; ++i) {
		git_buf_puts(&buf, "1234"); /* add 4 */
		cl_assert(git_buf_oom(&buf) == 0);
		git_buf_consume(&buf, buf.ptr + 2); /* eat the first two */
		cl_assert(strlen(git_buf_cstr(&buf)) == (size_t)((i + 1) * 2));
	}
	/* we have appended 1234 10x and removed the first 20 letters */
198
	cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
199 200

	git_buf_consume(&buf, NULL);
201
	cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
202 203

	git_buf_consume(&buf, "invalid pointer");
204
	cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
205 206

	git_buf_consume(&buf, buf.ptr);
207
	cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
208 209

	git_buf_consume(&buf, buf.ptr + 1);
210
	cl_assert_equal_s("2341234123412341234", git_buf_cstr(&buf));
211 212

	git_buf_consume(&buf, buf.ptr + buf.size);
213
	cl_assert_equal_s("", git_buf_cstr(&buf));
214 215 216 217 218 219 220 221 222 223

	git_buf_free(&buf);
}


static void
check_buf_append(
	const char* data_a,
	const char* data_b,
	const char* expected_data,
224 225
	size_t expected_size,
	size_t expected_asize)
226 227 228 229 230 231 232
{
	git_buf tgt = GIT_BUF_INIT;

	git_buf_sets(&tgt, data_a);
	cl_assert(git_buf_oom(&tgt) == 0);
	git_buf_puts(&tgt, data_b);
	cl_assert(git_buf_oom(&tgt) == 0);
233
	cl_assert_equal_s(expected_data, git_buf_cstr(&tgt));
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	cl_assert(tgt.size == expected_size);
	if (expected_asize > 0)
		cl_assert(tgt.asize == expected_asize);

	git_buf_free(&tgt);
}

static void
check_buf_append_abc(
	const char* buf_a,
	const char* buf_b,
	const char* buf_c,
	const char* expected_ab,
	const char* expected_abc,
	const char* expected_abca,
	const char* expected_abcab,
	const char* expected_abcabc)
{
	git_buf buf = GIT_BUF_INIT;

	git_buf_sets(&buf, buf_a);
	cl_assert(git_buf_oom(&buf) == 0);
256
	cl_assert_equal_s(buf_a, git_buf_cstr(&buf));
257 258 259

	git_buf_puts(&buf, buf_b);
	cl_assert(git_buf_oom(&buf) == 0);
260
	cl_assert_equal_s(expected_ab, git_buf_cstr(&buf));
261 262 263

	git_buf_puts(&buf, buf_c);
	cl_assert(git_buf_oom(&buf) == 0);
264
	cl_assert_equal_s(expected_abc, git_buf_cstr(&buf));
265 266 267

	git_buf_puts(&buf, buf_a);
	cl_assert(git_buf_oom(&buf) == 0);
268
	cl_assert_equal_s(expected_abca, git_buf_cstr(&buf));
269 270 271

	git_buf_puts(&buf, buf_b);
	cl_assert(git_buf_oom(&buf) == 0);
272
	cl_assert_equal_s(expected_abcab, git_buf_cstr(&buf));
273 274 275

	git_buf_puts(&buf, buf_c);
	cl_assert(git_buf_oom(&buf) == 0);
276
	cl_assert_equal_s(expected_abcabc, git_buf_cstr(&buf));
277 278 279 280 281 282 283

	git_buf_free(&buf);
}

/* more variations on append tests */
void test_core_buffer__5(void)
{
284 285
	check_buf_append("", "", "", 0, 0);
	check_buf_append("a", "", "a", 1, 0);
286
	check_buf_append("", "a", "a", 1, 8);
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
	check_buf_append("", "a", "a", 1, 8);
	check_buf_append("a", "b", "ab", 2, 8);
	check_buf_append("", "abcdefgh", "abcdefgh", 8, 16);
	check_buf_append("abcdefgh", "", "abcdefgh", 8, 16);

	/* buffer with starting asize will grow to:
	 *  1 ->  2,  2 ->  3,  3 ->  5,  4 ->  6,  5 ->  8,  6 ->  9,
	 *  7 -> 11,  8 -> 12,  9 -> 14, 10 -> 15, 11 -> 17, 12 -> 18,
	 * 13 -> 20, 14 -> 21, 15 -> 23, 16 -> 24, 17 -> 26, 18 -> 27,
	 * 19 -> 29, 20 -> 30, 21 -> 32, 22 -> 33, 23 -> 35, 24 -> 36,
	 * ...
	 * follow sequence until value > target size,
	 * then round up to nearest multiple of 8.
	 */

	check_buf_append("abcdefgh", "/", "abcdefgh/", 9, 16);
303
	check_buf_append("abcdefgh", "ijklmno", "abcdefghijklmno", 15, 16);
304 305 306 307 308 309
	check_buf_append("abcdefgh", "ijklmnop", "abcdefghijklmnop", 16, 24);
	check_buf_append("0123456789", "0123456789",
					 "01234567890123456789", 20, 24);
	check_buf_append(REP16("x"), REP16("o"),
					 REP16("x") REP16("o"), 32, 40);

310 311
	check_buf_append(test_4096, "", test_4096, 4096, 4104);
	check_buf_append(test_4096, test_4096, test_8192, 8192, 9240);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

	/* check sequences of appends */
	check_buf_append_abc("a", "b", "c",
						 "ab", "abc", "abca", "abcab", "abcabc");
	check_buf_append_abc("a1", "b2", "c3",
						 "a1b2", "a1b2c3", "a1b2c3a1",
						 "a1b2c3a1b2", "a1b2c3a1b2c3");
	check_buf_append_abc("a1/", "b2/", "c3/",
						 "a1/b2/", "a1/b2/c3/", "a1/b2/c3/a1/",
						 "a1/b2/c3/a1/b2/", "a1/b2/c3/a1/b2/c3/");
}

/* test swap */
void test_core_buffer__6(void)
{
	git_buf a = GIT_BUF_INIT;
	git_buf b = GIT_BUF_INIT;

	git_buf_sets(&a, "foo");
	cl_assert(git_buf_oom(&a) == 0);
	git_buf_sets(&b, "bar");
	cl_assert(git_buf_oom(&b) == 0);

335 336
	cl_assert_equal_s("foo", git_buf_cstr(&a));
	cl_assert_equal_s("bar", git_buf_cstr(&b));
337 338 339

	git_buf_swap(&a, &b);

340 341
	cl_assert_equal_s("bar", git_buf_cstr(&a));
	cl_assert_equal_s("foo", git_buf_cstr(&b));
342 343 344 345 346 347

	git_buf_free(&a);
	git_buf_free(&b);
}


348
/* test detach/attach data */
349 350
void test_core_buffer__7(void)
{
351
	const char *fun = "This is fun";
352 353 354 355 356
	git_buf a = GIT_BUF_INIT;
	char *b = NULL;

	git_buf_sets(&a, "foo");
	cl_assert(git_buf_oom(&a) == 0);
357
	cl_assert_equal_s("foo", git_buf_cstr(&a));
358

359
	b = git_buf_detach(&a);
360

361 362
	cl_assert_equal_s("foo", b);
	cl_assert_equal_s("", a.ptr);
363 364
	git__free(b);

365
	b = git_buf_detach(&a);
366

367 368
	cl_assert_equal_s(NULL, b);
	cl_assert_equal_s("", a.ptr);
369 370

	git_buf_free(&a);
371 372 373 374

	b = git__strdup(fun);
	git_buf_attach(&a, b, 0);

375
	cl_assert_equal_s(fun, a.ptr);
376 377
	cl_assert(a.size == strlen(fun));
	cl_assert(a.asize == strlen(fun) + 1);
378 379 380 381 382 383

	git_buf_free(&a);

	b = git__strdup(fun);
	git_buf_attach(&a, b, strlen(fun) + 1);

384
	cl_assert_equal_s(fun, a.ptr);
385 386
	cl_assert(a.size == strlen(fun));
	cl_assert(a.asize == strlen(fun) + 1);
387 388

	git_buf_free(&a);
389 390 391 392
}


static void
393 394 395 396 397 398 399 400 401 402
check_joinbuf_2(
	const char *a,
	const char *b,
	const char *expected)
{
	char sep = '/';
	git_buf buf = GIT_BUF_INIT;

	git_buf_join(&buf, sep, a, b);
	cl_assert(git_buf_oom(&buf) == 0);
403
	cl_assert_equal_s(expected, git_buf_cstr(&buf));
404 405 406 407
	git_buf_free(&buf);
}

static void
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
check_joinbuf_overlapped(
	const char *oldval,
	int ofs_a,
	const char *b,
	const char *expected)
{
	char sep = '/';
	git_buf buf = GIT_BUF_INIT;

	git_buf_sets(&buf, oldval);
	git_buf_join(&buf, sep, buf.ptr + ofs_a, b);
	cl_assert(git_buf_oom(&buf) == 0);
	cl_assert_equal_s(expected, git_buf_cstr(&buf));
	git_buf_free(&buf);
}

static void
425
check_joinbuf_n_2(
426 427 428 429 430 431 432 433 434 435
	const char *a,
	const char *b,
	const char *expected)
{
	char sep = '/';
	git_buf buf = GIT_BUF_INIT;

	git_buf_sets(&buf, a);
	cl_assert(git_buf_oom(&buf) == 0);

436
	git_buf_join_n(&buf, sep, 1, b);
437
	cl_assert(git_buf_oom(&buf) == 0);
438
	cl_assert_equal_s(expected, git_buf_cstr(&buf));
439 440 441 442 443

	git_buf_free(&buf);
}

static void
444
check_joinbuf_n_4(
445 446 447 448 449 450 451 452
	const char *a,
	const char *b,
	const char *c,
	const char *d,
	const char *expected)
{
	char sep = ';';
	git_buf buf = GIT_BUF_INIT;
453
	git_buf_join_n(&buf, sep, 4, a, b, c, d);
454
	cl_assert(git_buf_oom(&buf) == 0);
455
	cl_assert_equal_s(expected, git_buf_cstr(&buf));
456 457 458 459 460 461 462 463
	git_buf_free(&buf);
}

/* test join */
void test_core_buffer__8(void)
{
	git_buf a = GIT_BUF_INIT;

464
	git_buf_join_n(&a, '/', 1, "foo");
465
	cl_assert(git_buf_oom(&a) == 0);
466
	cl_assert_equal_s("foo", git_buf_cstr(&a));
467

468
	git_buf_join_n(&a, '/', 1, "bar");
469
	cl_assert(git_buf_oom(&a) == 0);
470
	cl_assert_equal_s("foo/bar", git_buf_cstr(&a));
471

472
	git_buf_join_n(&a, '/', 1, "baz");
473
	cl_assert(git_buf_oom(&a) == 0);
474
	cl_assert_equal_s("foo/bar/baz", git_buf_cstr(&a));
475 476 477

	git_buf_free(&a);

478 479 480
	check_joinbuf_2(NULL, "", "");
	check_joinbuf_2(NULL, "a", "a");
	check_joinbuf_2(NULL, "/a", "/a");
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
	check_joinbuf_2("", "", "");
	check_joinbuf_2("", "a", "a");
	check_joinbuf_2("", "/a", "/a");
	check_joinbuf_2("a", "", "a/");
	check_joinbuf_2("a", "/", "a/");
	check_joinbuf_2("a", "b", "a/b");
	check_joinbuf_2("/", "a", "/a");
	check_joinbuf_2("/", "", "/");
	check_joinbuf_2("/a", "/b", "/a/b");
	check_joinbuf_2("/a", "/b/", "/a/b/");
	check_joinbuf_2("/a/", "b/", "/a/b/");
	check_joinbuf_2("/a/", "/b/", "/a/b/");
	check_joinbuf_2("/a/", "//b/", "/a/b/");
	check_joinbuf_2("/abcd", "/defg", "/abcd/defg");
	check_joinbuf_2("/abcd", "/defg/", "/abcd/defg/");
	check_joinbuf_2("/abcd/", "defg/", "/abcd/defg/");
	check_joinbuf_2("/abcd/", "/defg/", "/abcd/defg/");

499 500 501 502 503 504 505 506 507 508 509 510 511 512
	check_joinbuf_overlapped("abcd", 0, "efg", "abcd/efg");
	check_joinbuf_overlapped("abcd", 1, "efg", "bcd/efg");
	check_joinbuf_overlapped("abcd", 2, "efg", "cd/efg");
	check_joinbuf_overlapped("abcd", 3, "efg", "d/efg");
	check_joinbuf_overlapped("abcd", 4, "efg", "efg");
	check_joinbuf_overlapped("abc/", 2, "efg", "c/efg");
	check_joinbuf_overlapped("abc/", 3, "efg", "/efg");
	check_joinbuf_overlapped("abc/", 4, "efg", "efg");
	check_joinbuf_overlapped("abcd", 3, "", "d/");
	check_joinbuf_overlapped("abcd", 4, "", "");
	check_joinbuf_overlapped("abc/", 2, "", "c/");
	check_joinbuf_overlapped("abc/", 3, "", "/");
	check_joinbuf_overlapped("abc/", 4, "", "");

513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	check_joinbuf_n_2("", "", "");
	check_joinbuf_n_2("", "a", "a");
	check_joinbuf_n_2("", "/a", "/a");
	check_joinbuf_n_2("a", "", "a/");
	check_joinbuf_n_2("a", "/", "a/");
	check_joinbuf_n_2("a", "b", "a/b");
	check_joinbuf_n_2("/", "a", "/a");
	check_joinbuf_n_2("/", "", "/");
	check_joinbuf_n_2("/a", "/b", "/a/b");
	check_joinbuf_n_2("/a", "/b/", "/a/b/");
	check_joinbuf_n_2("/a/", "b/", "/a/b/");
	check_joinbuf_n_2("/a/", "/b/", "/a/b/");
	check_joinbuf_n_2("/abcd", "/defg", "/abcd/defg");
	check_joinbuf_n_2("/abcd", "/defg/", "/abcd/defg/");
	check_joinbuf_n_2("/abcd/", "defg/", "/abcd/defg/");
	check_joinbuf_n_2("/abcd/", "/defg/", "/abcd/defg/");

	check_joinbuf_n_4("", "", "", "", "");
	check_joinbuf_n_4("", "a", "", "", "a;");
	check_joinbuf_n_4("a", "", "", "", "a;");
	check_joinbuf_n_4("", "", "", "a", "a");
	check_joinbuf_n_4("a", "b", "", ";c;d;", "a;b;c;d;");
	check_joinbuf_n_4("a", "b", "", ";c;d", "a;b;c;d");
	check_joinbuf_n_4("abcd", "efgh", "ijkl", "mnop", "abcd;efgh;ijkl;mnop");
	check_joinbuf_n_4("abcd;", "efgh;", "ijkl;", "mnop;", "abcd;efgh;ijkl;mnop;");
	check_joinbuf_n_4(";abcd;", ";efgh;", ";ijkl;", ";mnop;", ";abcd;efgh;ijkl;mnop;");
539 540
}

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
void test_core_buffer__9(void)
{
	git_buf buf = GIT_BUF_INIT;

	/* just some exhaustive tests of various separator placement */
	char *a[] = { "", "-", "a-", "-a", "-a-" };
	char *b[] = { "", "-", "b-", "-b", "-b-" };
	char sep[] = { 0, '-', '/' };
	char *expect_null[] = { "",    "-",     "a-",     "-a",     "-a-",
							"-",   "--",    "a--",    "-a-",    "-a--",
							"b-",  "-b-",   "a-b-",   "-ab-",   "-a-b-",
							"-b",  "--b",   "a--b",   "-a-b",   "-a--b",
							"-b-", "--b-",  "a--b-",  "-a-b-",  "-a--b-" };
	char *expect_dash[] = { "",    "-",     "a-",     "-a-",    "-a-",
							"-",   "-",     "a-",     "-a-",    "-a-",
							"b-",  "-b-",   "a-b-",   "-a-b-",  "-a-b-",
							"-b",  "-b",    "a-b",    "-a-b",   "-a-b",
							"-b-", "-b-",   "a-b-",   "-a-b-",  "-a-b-" };
	char *expect_slas[] = { "",    "-/",    "a-/",    "-a/",    "-a-/",
							"-",   "-/-",   "a-/-",   "-a/-",   "-a-/-",
							"b-",  "-/b-",  "a-/b-",  "-a/b-",  "-a-/b-",
							"-b",  "-/-b",  "a-/-b",  "-a/-b",  "-a-/-b",
							"-b-", "-/-b-", "a-/-b-", "-a/-b-", "-a-/-b-" };
	char **expect_values[] = { expect_null, expect_dash, expect_slas };
	char separator, **expect;
	unsigned int s, i, j;

	for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {
		separator = sep[s];
		expect = expect_values[s];

		for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {
			for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {
				git_buf_join(&buf, separator, a[i], b[j]);
575
				cl_assert_equal_s(*expect, buf.ptr);
576 577 578 579 580 581 582
				expect++;
			}
		}
	}

	git_buf_free(&buf);
}
583 584 585 586 587 588

void test_core_buffer__10(void)
{
	git_buf a = GIT_BUF_INIT;

	cl_git_pass(git_buf_join_n(&a, '/', 1, "test"));
589
	cl_assert_equal_s(a.ptr, "test");
590
	cl_git_pass(git_buf_join_n(&a, '/', 1, "string"));
591
	cl_assert_equal_s(a.ptr, "test/string");
592 593
	git_buf_clear(&a);
	cl_git_pass(git_buf_join_n(&a, '/', 3, "test", "string", "join"));
594
	cl_assert_equal_s(a.ptr, "test/string/join");
595
	cl_git_pass(git_buf_join_n(&a, '/', 2, a.ptr, "more"));
596
	cl_assert_equal_s(a.ptr, "test/string/join/test/string/join/more");
597 598 599

	git_buf_free(&a);
}
600

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
void test_core_buffer__join3(void)
{
	git_buf a = GIT_BUF_INIT;

	cl_git_pass(git_buf_join3(&a, '/', "test", "string", "join"));
	cl_assert_equal_s("test/string/join", a.ptr);
	cl_git_pass(git_buf_join3(&a, '/', "test/", "string", "join"));
	cl_assert_equal_s("test/string/join", a.ptr);
	cl_git_pass(git_buf_join3(&a, '/', "test/", "/string", "join"));
	cl_assert_equal_s("test/string/join", a.ptr);
	cl_git_pass(git_buf_join3(&a, '/', "test/", "/string/", "join"));
	cl_assert_equal_s("test/string/join", a.ptr);
	cl_git_pass(git_buf_join3(&a, '/', "test/", "/string/", "/join"));
	cl_assert_equal_s("test/string/join", a.ptr);

	cl_git_pass(git_buf_join3(&a, '/', "", "string", "join"));
	cl_assert_equal_s("string/join", a.ptr);
	cl_git_pass(git_buf_join3(&a, '/', "", "string/", "join"));
	cl_assert_equal_s("string/join", a.ptr);
	cl_git_pass(git_buf_join3(&a, '/', "", "string/", "/join"));
	cl_assert_equal_s("string/join", a.ptr);

	cl_git_pass(git_buf_join3(&a, '/', "string", "", "join"));
	cl_assert_equal_s("string/join", a.ptr);
	cl_git_pass(git_buf_join3(&a, '/', "string/", "", "join"));
	cl_assert_equal_s("string/join", a.ptr);
	cl_git_pass(git_buf_join3(&a, '/', "string/", "", "/join"));
	cl_assert_equal_s("string/join", a.ptr);

	git_buf_free(&a);
}

633 634 635 636 637 638 639 640 641 642 643 644 645 646
void test_core_buffer__11(void)
{
	git_buf a = GIT_BUF_INIT;
	git_strarray t;
	char *t1[] = { "nothing", "in", "common" };
	char *t2[] = { "something", "something else", "some other" };
	char *t3[] = { "something", "some fun", "no fun" };
	char *t4[] = { "happy", "happier", "happiest" };
	char *t5[] = { "happiest", "happier", "happy" };
	char *t6[] = { "no", "nope", "" };
	char *t7[] = { "", "doesn't matter" };

	t.strings = t1;
	t.count = 3;
647
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
648 649 650 651
	cl_assert_equal_s(a.ptr, "");

	t.strings = t2;
	t.count = 3;
652
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
653 654 655 656
	cl_assert_equal_s(a.ptr, "some");

	t.strings = t3;
	t.count = 3;
657
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
658 659 660 661
	cl_assert_equal_s(a.ptr, "");

	t.strings = t4;
	t.count = 3;
662
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
663 664 665 666
	cl_assert_equal_s(a.ptr, "happ");

	t.strings = t5;
	t.count = 3;
667
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
668 669 670 671
	cl_assert_equal_s(a.ptr, "happ");

	t.strings = t6;
	t.count = 3;
672
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
673 674 675 676
	cl_assert_equal_s(a.ptr, "");

	t.strings = t7;
	t.count = 3;
677
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
678 679 680 681
	cl_assert_equal_s(a.ptr, "");

	git_buf_free(&a);
}
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703

void test_core_buffer__rfind_variants(void)
{
	git_buf a = GIT_BUF_INIT;
	ssize_t len;

	cl_git_pass(git_buf_sets(&a, "/this/is/it/"));

	len = (ssize_t)git_buf_len(&a);

	cl_assert(git_buf_rfind(&a, '/') == len - 1);
	cl_assert(git_buf_rfind_next(&a, '/') == len - 4);

	cl_assert(git_buf_rfind(&a, 'i') == len - 3);
	cl_assert(git_buf_rfind_next(&a, 'i') == len - 3);

	cl_assert(git_buf_rfind(&a, 'h') == 2);
	cl_assert(git_buf_rfind_next(&a, 'h') == 2);

	cl_assert(git_buf_rfind(&a, 'q') == -1);
	cl_assert(git_buf_rfind_next(&a, 'q') == -1);

Russell Belfer committed
704
	git_buf_free(&a);
705 706 707 708 709 710 711
}

void test_core_buffer__puts_escaped(void)
{
	git_buf a = GIT_BUF_INIT;

	git_buf_clear(&a);
712
	cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "", ""));
713 714 715
	cl_assert_equal_s("this is a test", a.ptr);

	git_buf_clear(&a);
716
	cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "t", "\\"));
717 718 719
	cl_assert_equal_s("\\this is a \\tes\\t", a.ptr);

	git_buf_clear(&a);
720
	cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "i ", "__"));
721 722 723
	cl_assert_equal_s("th__is__ __is__ a__ test", a.ptr);

	git_buf_clear(&a);
724
	cl_git_pass(git_buf_text_puts_escape_regex(&a, "^match\\s*[A-Z]+.*"));
725 726 727 728
	cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a.ptr);

	git_buf_free(&a);
}
729 730 731 732 733

static void assert_unescape(char *expected, char *to_unescape) {
	git_buf buf = GIT_BUF_INIT;

	cl_git_pass(git_buf_sets(&buf, to_unescape));
734
	git_buf_text_unescape(&buf);
735
	cl_assert_equal_s(expected, buf.ptr);
736
	cl_assert_equal_sz(strlen(expected), buf.size);
737 738 739 740 741 742 743 744 745 746 747 748

	git_buf_free(&buf);
}

void test_core_buffer__unescape(void)
{
	assert_unescape("Escaped\\", "Es\\ca\\ped\\");
	assert_unescape("Es\\caped\\", "Es\\\\ca\\ped\\\\");
	assert_unescape("\\", "\\");
	assert_unescape("\\", "\\\\");
	assert_unescape("", "");
}
749

750
void test_core_buffer__encode_base64(void)
751 752 753 754 755 756 757 758 759 760
{
	git_buf buf = GIT_BUF_INIT;

	/*     t  h  i  s
	 * 0x 74 68 69 73
     * 0b 01110100 01101000 01101001 01110011
	 * 0b 011101 000110 100001 101001 011100 110000
	 * 0x 1d 06 21 29 1c 30
	 *     d  G  h  p  c  w
	 */
761
	cl_git_pass(git_buf_encode_base64(&buf, "this", 4));
762 763 764
	cl_assert_equal_s("dGhpcw==", buf.ptr);

	git_buf_clear(&buf);
765
	cl_git_pass(git_buf_encode_base64(&buf, "this!", 5));
766 767 768
	cl_assert_equal_s("dGhpcyE=", buf.ptr);

	git_buf_clear(&buf);
769
	cl_git_pass(git_buf_encode_base64(&buf, "this!\n", 6));
770 771 772 773
	cl_assert_equal_s("dGhpcyEK", buf.ptr);

	git_buf_free(&buf);
}
774

775
void test_core_buffer__decode_base64(void)
776 777 778
{
	git_buf buf = GIT_BUF_INIT;

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
	cl_git_pass(git_buf_decode_base64(&buf, "dGhpcw==", 8));
	cl_assert_equal_s("this", buf.ptr);

	git_buf_clear(&buf);
	cl_git_pass(git_buf_decode_base64(&buf, "dGhpcyE=", 8));
	cl_assert_equal_s("this!", buf.ptr);

	git_buf_clear(&buf);
	cl_git_pass(git_buf_decode_base64(&buf, "dGhpcyEK", 8));
	cl_assert_equal_s("this!\n", buf.ptr);

	cl_git_fail(git_buf_decode_base64(&buf, "This is not a valid base64 string!!!", 36));
	cl_assert_equal_s("this!\n", buf.ptr);

	git_buf_free(&buf);
}

void test_core_buffer__encode_base85(void)
{
	git_buf buf = GIT_BUF_INIT;

	cl_git_pass(git_buf_encode_base85(&buf, "this", 4));
801 802 803
	cl_assert_equal_s("bZBXF", buf.ptr);
	git_buf_clear(&buf);

804
	cl_git_pass(git_buf_encode_base85(&buf, "two rnds", 8));
805 806 807
	cl_assert_equal_s("ba!tca&BaE", buf.ptr);
	git_buf_clear(&buf);

808
	cl_git_pass(git_buf_encode_base85(&buf, "this is base 85 encoded",
809 810 811 812 813 814 815
		strlen("this is base 85 encoded")));
	cl_assert_equal_s("bZBXFAZc?TVqtS-AUHK3Wo~0{WMyOk", buf.ptr);
	git_buf_clear(&buf);

	git_buf_free(&buf);
}

816 817 818 819 820 821 822 823
void test_core_buffer__classify_with_utf8(void)
{
	char *data0 = "Simple text\n";
	size_t data0len = 12;
	char *data1 = "Is that UTF-8 data I see…\nYep!\n";
	size_t data1len = 31;
	char *data2 = "Internal NUL!!!\000\n\nI see you!\n";
	size_t data2len = 29;
824 825
	char *data3 = "\xef\xbb\xbfThis is UTF-8 with a BOM.\n";
	size_t data3len = 20;
826 827 828 829 830 831 832
	git_buf b;

	b.ptr = data0; b.size = b.asize = data0len;
	cl_assert(!git_buf_text_is_binary(&b));
	cl_assert(!git_buf_text_contains_nul(&b));

	b.ptr = data1; b.size = b.asize = data1len;
833
	cl_assert(!git_buf_text_is_binary(&b));
834 835 836 837 838
	cl_assert(!git_buf_text_contains_nul(&b));

	b.ptr = data2; b.size = b.asize = data2len;
	cl_assert(git_buf_text_is_binary(&b));
	cl_assert(git_buf_text_contains_nul(&b));
839 840 841 842

	b.ptr = data3; b.size = b.asize = data3len;
	cl_assert(!git_buf_text_is_binary(&b));
	cl_assert(!git_buf_text_contains_nul(&b));
843
}
844

845
#define SIMILARITY_TEST_DATA_1 \
846 847 848 849 850
	"000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
	"010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
	"020\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
	"030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
	"040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
851

852 853
void test_core_buffer__similarity_metric(void)
{
854
	git_hashsig *a, *b;
855 856 857 858 859
	git_buf buf = GIT_BUF_INIT;
	int sim;

	/* in the first case, we compare data to itself and expect 100% match */

860
	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
861 862
	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
	cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
863

864
	cl_assert_equal_i(100, git_hashsig_compare(a, b));
865

866 867
	git_hashsig_free(a);
	git_hashsig_free(b);
868

869
	/* if we change just a single byte, how much does that change magnify? */
870

871
	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
872
	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
873
	cl_git_pass(git_buf_sets(&buf,
874 875 876 877 878 879
		"000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
		"010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
		"x020x\n021\n022\n023\n024\n025\n026\n027\n028\n029\n" \
		"030\n031\n032\n033\n034\n035\n036\n037\n038\n039\n" \
		"040\n041\n042\n043\n044\n045\n046\n047\n048\n049\n"
		));
880
	cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
881

882
	sim = git_hashsig_compare(a, b);
883

884
	cl_assert_in_range(95, sim, 100); /* expect >95% similarity */
885

886 887
	git_hashsig_free(a);
	git_hashsig_free(b);
888

889
	/* let's try comparing data to a superset of itself */
890

891
	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
892
	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
893
	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1
894
		"050\n051\n052\n053\n054\n055\n056\n057\n058\n059\n"));
895
	cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
896

897
	sim = git_hashsig_compare(a, b);
898
	/* 20% lines added ~= 10% lines changed */
899

900
	cl_assert_in_range(85, sim, 95); /* expect similarity around 90% */
901

902 903 904 905 906 907
	git_hashsig_free(a);
	git_hashsig_free(b);

	/* what if we keep about half the original data and add half new */

	cl_git_pass(git_buf_sets(&buf, SIMILARITY_TEST_DATA_1));
908
	cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
909
	cl_git_pass(git_buf_sets(&buf,
910 911 912 913 914 915 916
		"000\n001\n002\n003\n004\n005\n006\n007\n008\n009\n" \
		"010\n011\n012\n013\n014\n015\n016\n017\n018\n019\n" \
		"020x\n021\n022\n023\n024\n" \
		"x25\nx26\nx27\nx28\nx29\n" \
		"x30\nx31\nx32\nx33\nx34\nx35\nx36\nx37\nx38\nx39\n" \
		"x40\nx41\nx42\nx43\nx44\nx45\nx46\nx47\nx48\nx49\n"
		));
917
	cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, GIT_HASHSIG_NORMAL));
918

919
	sim = git_hashsig_compare(a, b);
920
	/* 50% lines changed */
921

922
	cl_assert_in_range(40, sim, 60); /* expect in the 40-60% similarity range */
923

924 925
	git_hashsig_free(a);
	git_hashsig_free(b);
926 927 928

	/* lastly, let's check that we can hash file content as well */

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 932

	cl_git_pass(git_futils_mkdir("scratch", NULL, 0755, GIT_MKDIR_PATH));
933 934 935
	cl_git_mkfile("scratch/testdata", SIMILARITY_TEST_DATA_1);
	cl_git_pass(git_hashsig_create_fromfile(
		&b, "scratch/testdata", GIT_HASHSIG_NORMAL));
936

937
	cl_assert_equal_i(100, git_hashsig_compare(a, b));
938

939 940
	git_hashsig_free(a);
	git_hashsig_free(b);
941 942 943 944

	git_buf_free(&buf);
	git_futils_rmdir_r("scratch", NULL, GIT_RMDIR_REMOVE_FILES);
}
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999


void test_core_buffer__similarity_metric_whitespace(void)
{
	git_hashsig *a, *b;
	git_buf buf = GIT_BUF_INIT;
	int sim, i, j;
	git_hashsig_option_t opt;
	const char *tabbed =
		"	for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
		"		separator = sep[s];\n"
		"		expect = expect_values[s];\n"
		"\n"
		"		for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
		"			for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
		"				git_buf_join(&buf, separator, a[i], b[j]);\n"
		"				cl_assert_equal_s(*expect, buf.ptr);\n"
		"				expect++;\n"
		"			}\n"
		"		}\n"
		"	}\n";
	const char *spaced =
		"   for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\n"
		"       separator = sep[s];\n"
		"       expect = expect_values[s];\n"
		"\n"
		"       for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\n"
		"           for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\n"
		"               git_buf_join(&buf, separator, a[i], b[j]);\n"
		"               cl_assert_equal_s(*expect, buf.ptr);\n"
		"               expect++;\n"
		"           }\n"
		"       }\n"
		"   }\n";
	const char *crlf_spaced2 =
		"  for (s = 0; s < sizeof(sep) / sizeof(char); ++s) {\r\n"
		"    separator = sep[s];\r\n"
		"    expect = expect_values[s];\r\n"
		"\r\n"
		"    for (j = 0; j < sizeof(b) / sizeof(char*); ++j) {\r\n"
		"      for (i = 0; i < sizeof(a) / sizeof(char*); ++i) {\r\n"
		"        git_buf_join(&buf, separator, a[i], b[j]);\r\n"
		"        cl_assert_equal_s(*expect, buf.ptr);\r\n"
		"        expect++;\r\n"
		"      }\r\n"
		"    }\r\n"
		"  }\r\n";
	const char *text[3] = { tabbed, spaced, crlf_spaced2 };

	/* let's try variations of our own code with whitespace changes */

	for (opt = GIT_HASHSIG_NORMAL; opt <= GIT_HASHSIG_SMART_WHITESPACE; ++opt) {
		for (i = 0; i < 3; ++i) {
			for (j = 0; j < 3; ++j) {
				cl_git_pass(git_buf_sets(&buf, text[i]));
1000
				cl_git_pass(git_hashsig_create(&a, buf.ptr, buf.size, opt));
1001 1002

				cl_git_pass(git_buf_sets(&buf, text[j]));
1003
				cl_git_pass(git_hashsig_create(&b, buf.ptr, buf.size, opt));
1004 1005 1006 1007 1008 1009 1010

				sim = git_hashsig_compare(a, b);

				if (opt == GIT_HASHSIG_NORMAL) {
					if (i == j)
						cl_assert_equal_i(100, sim);
					else
1011
						cl_assert_in_range(0, sim, 30); /* pretty different */
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
				} else {
					cl_assert_equal_i(100, sim);
				}

				git_hashsig_free(a);
				git_hashsig_free(b);
			}
		}
	}

	git_buf_free(&buf);
}
1024

1025 1026
#include "../filter/crlf.h"

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041
#define check_buf(expected,buf) do { \
	cl_assert_equal_s(expected, buf.ptr); \
	cl_assert_equal_sz(strlen(expected), buf.size); } while (0)

void test_core_buffer__lf_and_crlf_conversions(void)
{
	git_buf src = GIT_BUF_INIT, tgt = GIT_BUF_INIT;

	/* LF source */

	git_buf_sets(&src, "lf\nlf\nlf\nlf\n");

	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf("lf\r\nlf\r\nlf\r\nlf\r\n", tgt);

1042 1043
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf(src.ptr, tgt);
1044 1045 1046 1047 1048 1049

	git_buf_sets(&src, "\nlf\nlf\nlf\nlf\nlf");

	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf("\r\nlf\r\nlf\r\nlf\r\nlf\r\nlf", tgt);

1050 1051
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf(src.ptr, tgt);
1052 1053 1054 1055 1056

	/* CRLF source */

	git_buf_sets(&src, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n");

1057 1058 1059 1060
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf("crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n", tgt);

	git_buf_sets(&src, "crlf\r\ncrlf\r\ncrlf\r\ncrlf\r\n");
1061 1062 1063 1064 1065 1066

	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf("crlf\ncrlf\ncrlf\ncrlf\n", tgt);

	git_buf_sets(&src, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf");

1067 1068 1069 1070
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf("\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf", tgt);

	git_buf_sets(&src, "\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf\r\ncrlf");
1071 1072 1073 1074 1075 1076 1077 1078

	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf("\ncrlf\ncrlf\ncrlf\ncrlf\ncrlf", tgt);

	/* CRLF in LF text */

	git_buf_sets(&src, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n");

1079 1080 1081 1082 1083
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf("\r\nlf\r\nlf\r\ncrlf\r\nlf\r\nlf\r\ncrlf\r\n", tgt);

	git_buf_sets(&src, "\nlf\nlf\ncrlf\r\nlf\nlf\ncrlf\r\n");

1084 1085 1086 1087 1088 1089 1090
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf("\nlf\nlf\ncrlf\nlf\nlf\ncrlf\n", tgt);

	/* LF in CRLF text */

	git_buf_sets(&src, "\ncrlf\r\ncrlf\r\nlf\ncrlf\r\ncrlf");

1091 1092 1093
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf("\r\ncrlf\r\ncrlf\r\nlf\r\ncrlf\r\ncrlf", tgt);

1094 1095 1096 1097 1098 1099 1100
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf("\ncrlf\ncrlf\nlf\ncrlf\ncrlf", tgt);

	/* bare CR test */

	git_buf_sets(&src, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r");

1101 1102 1103 1104 1105
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf("\rcrlf\r\nlf\r\nlf\r\ncr\rcrlf\r\nlf\r\ncr\r", tgt);

	git_buf_sets(&src, "\rcrlf\r\nlf\nlf\ncr\rcrlf\r\nlf\ncr\r");

1106 1107 1108 1109
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf("\rcrlf\nlf\nlf\ncr\rcrlf\nlf\ncr\r", tgt);

	git_buf_sets(&src, "\rcr\r");
1110 1111
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf(src.ptr, tgt);
1112 1113 1114 1115 1116
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf("\rcr\r", tgt);

	git_buf_free(&src);
	git_buf_free(&tgt);
1117 1118 1119 1120

	/* blob correspondence tests */

	git_buf_sets(&src, ALL_CRLF_TEXT_RAW);
1121 1122 1123
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf(ALL_CRLF_TEXT_AS_CRLF, tgt);
	git_buf_sets(&src, ALL_CRLF_TEXT_RAW);
1124 1125 1126 1127 1128 1129 1130 1131
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf(ALL_CRLF_TEXT_AS_LF, tgt);
	git_buf_free(&src);
	git_buf_free(&tgt);

	git_buf_sets(&src, ALL_LF_TEXT_RAW);
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf(ALL_LF_TEXT_AS_CRLF, tgt);
1132
	git_buf_sets(&src, ALL_LF_TEXT_RAW);
1133 1134 1135 1136 1137 1138
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf(ALL_LF_TEXT_AS_LF, tgt);
	git_buf_free(&src);
	git_buf_free(&tgt);

	git_buf_sets(&src, MORE_CRLF_TEXT_RAW);
1139 1140 1141
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf(MORE_CRLF_TEXT_AS_CRLF, tgt);
	git_buf_sets(&src, MORE_CRLF_TEXT_RAW);
1142 1143 1144 1145 1146 1147
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf(MORE_CRLF_TEXT_AS_LF, tgt);
	git_buf_free(&src);
	git_buf_free(&tgt);

	git_buf_sets(&src, MORE_LF_TEXT_RAW);
1148 1149 1150
	cl_git_pass(git_buf_text_lf_to_crlf(&tgt, &src));
	check_buf(MORE_LF_TEXT_AS_CRLF, tgt);
	git_buf_sets(&src, MORE_LF_TEXT_RAW);
1151 1152 1153 1154
	cl_git_pass(git_buf_text_crlf_to_lf(&tgt, &src));
	check_buf(MORE_LF_TEXT_AS_LF, tgt);
	git_buf_free(&src);
	git_buf_free(&tgt);
1155
}
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166

void test_core_buffer__dont_grow_borrowed(void)
{
	const char *somestring = "blah blah";
	git_buf buf = GIT_BUF_INIT;

	git_buf_attach_notowned(&buf, somestring, strlen(somestring) + 1);
	cl_assert_equal_p(somestring, buf.ptr);
	cl_assert_equal_i(0, buf.asize);
	cl_assert_equal_i(strlen(somestring) + 1, buf.size);

1167
	cl_git_fail_with(GIT_EINVALID, git_buf_grow(&buf, 1024));
1168
}