buffer.c 19.4 KB
Newer Older
1
#include "clar_libgit2.h"
2
#include "buffer.h"
3
#include "buf_text.h"
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

#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);
23
	cl_assert_equal_s(test_string, git_buf_cstr(&buf));
24 25 26

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

	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);
39
	cl_assert_equal_s("shoop da 23 ", git_buf_cstr(&buf));
40 41 42

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

	git_buf_free(&buf);
}

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

	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 */
63
	cl_assert_equal_s("", git_buf_cstr(&buf));
64
	cl_assert(buf.size == 0);
65
	/* cl_assert(buf.asize == 0); -- should not assume what git_buf does */
66 67 68 69 70 71 72 73 74

	/* 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);
75
	cl_assert_equal_s("+", git_buf_cstr(&buf));
76 77 78 79

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

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

	git_buf_free(&buf);

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

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

	/* 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);
	}
106
	cl_assert_equal_s("xoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxoxo",
107 108 109 110 111 112 113
					   git_buf_cstr(&buf));

	git_buf_free(&buf);

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

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

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

	/* test clear */
	git_buf_clear(&buf);
128
	cl_assert_equal_s("", git_buf_cstr(&buf));
129 130

	git_buf_free(&buf);
131 132 133 134 135

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

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

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

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

	git_buf_free(&buf);

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

/* 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);
168
	cl_assert_equal_s(test_4096, git_buf_cstr(&buf));
169 170 171 172

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

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

	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 */
196
	cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
197 198

	git_buf_consume(&buf, NULL);
199
	cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
200 201

	git_buf_consume(&buf, "invalid pointer");
202
	cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
203 204

	git_buf_consume(&buf, buf.ptr);
205
	cl_assert_equal_s("12341234123412341234", git_buf_cstr(&buf));
206 207

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

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

	git_buf_free(&buf);
}


static void
check_buf_append(
	const char* data_a,
	const char* data_b,
	const char* expected_data,
222 223
	size_t expected_size,
	size_t expected_asize)
224 225 226 227 228 229 230
{
	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);
231
	cl_assert_equal_s(expected_data, git_buf_cstr(&tgt));
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	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);
254
	cl_assert_equal_s(buf_a, git_buf_cstr(&buf));
255 256 257

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

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

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

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

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

	git_buf_free(&buf);
}

/* more variations on append tests */
void test_core_buffer__5(void)
{
	check_buf_append("", "", "", 0, 8);
283 284
	check_buf_append("a", "", "a", 1, 8);
	check_buf_append("", "a", "a", 1, 8);
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
	check_buf_append("", "a", "a", 1, 8);
	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);
302
	check_buf_append("abcdefgh", "ijklmno", "abcdefghijklmno", 15, 16);
303 304 305 306 307 308
	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);

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

	/* 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);

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

	git_buf_swap(&a, &b);

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

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


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

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

358
	b = git_buf_detach(&a);
359

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

364
	b = git_buf_detach(&a);
365

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

	git_buf_free(&a);
370 371 372 373

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

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

	git_buf_free(&a);

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

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

	git_buf_free(&a);
388 389 390 391
}


static void
392 393 394 395 396 397 398 399 400 401
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);
402
	cl_assert_equal_s(expected, git_buf_cstr(&buf));
403 404 405 406 407
	git_buf_free(&buf);
}

static void
check_joinbuf_n_2(
408 409 410 411 412 413 414 415 416 417
	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);

418
	git_buf_join_n(&buf, sep, 1, b);
419
	cl_assert(git_buf_oom(&buf) == 0);
420
	cl_assert_equal_s(expected, git_buf_cstr(&buf));
421 422 423 424 425

	git_buf_free(&buf);
}

static void
426
check_joinbuf_n_4(
427 428 429 430 431 432 433 434
	const char *a,
	const char *b,
	const char *c,
	const char *d,
	const char *expected)
{
	char sep = ';';
	git_buf buf = GIT_BUF_INIT;
435
	git_buf_join_n(&buf, sep, 4, a, b, c, d);
436
	cl_assert(git_buf_oom(&buf) == 0);
437
	cl_assert_equal_s(expected, git_buf_cstr(&buf));
438 439 440 441 442 443 444 445
	git_buf_free(&buf);
}

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

446
	git_buf_join_n(&a, '/', 1, "foo");
447
	cl_assert(git_buf_oom(&a) == 0);
448
	cl_assert_equal_s("foo", git_buf_cstr(&a));
449

450
	git_buf_join_n(&a, '/', 1, "bar");
451
	cl_assert(git_buf_oom(&a) == 0);
452
	cl_assert_equal_s("foo/bar", git_buf_cstr(&a));
453

454
	git_buf_join_n(&a, '/', 1, "baz");
455
	cl_assert(git_buf_oom(&a) == 0);
456
	cl_assert_equal_s("foo/bar/baz", git_buf_cstr(&a));
457 458 459

	git_buf_free(&a);

460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
	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/");

	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;");
504 505
}

506 507 508 509 510 511 512 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 539
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]);
540
				cl_assert_equal_s(*expect, buf.ptr);
541 542 543 544 545 546 547
				expect++;
			}
		}
	}

	git_buf_free(&buf);
}
548 549 550 551 552 553

void test_core_buffer__10(void)
{
	git_buf a = GIT_BUF_INIT;

	cl_git_pass(git_buf_join_n(&a, '/', 1, "test"));
554
	cl_assert_equal_s(a.ptr, "test");
555
	cl_git_pass(git_buf_join_n(&a, '/', 1, "string"));
556
	cl_assert_equal_s(a.ptr, "test/string");
557 558
	git_buf_clear(&a);
	cl_git_pass(git_buf_join_n(&a, '/', 3, "test", "string", "join"));
559
	cl_assert_equal_s(a.ptr, "test/string/join");
560
	cl_git_pass(git_buf_join_n(&a, '/', 2, a.ptr, "more"));
561
	cl_assert_equal_s(a.ptr, "test/string/join/test/string/join/more");
562 563 564

	git_buf_free(&a);
}
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579

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;
580
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
581 582 583 584
	cl_assert_equal_s(a.ptr, "");

	t.strings = t2;
	t.count = 3;
585
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
586 587 588 589
	cl_assert_equal_s(a.ptr, "some");

	t.strings = t3;
	t.count = 3;
590
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
591 592 593 594
	cl_assert_equal_s(a.ptr, "");

	t.strings = t4;
	t.count = 3;
595
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
596 597 598 599
	cl_assert_equal_s(a.ptr, "happ");

	t.strings = t5;
	t.count = 3;
600
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
601 602 603 604
	cl_assert_equal_s(a.ptr, "happ");

	t.strings = t6;
	t.count = 3;
605
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
606 607 608 609
	cl_assert_equal_s(a.ptr, "");

	t.strings = t7;
	t.count = 3;
610
	cl_git_pass(git_buf_text_common_prefix(&a, &t));
611 612 613 614
	cl_assert_equal_s(a.ptr, "");

	git_buf_free(&a);
}
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636

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
637
	git_buf_free(&a);
638 639 640 641 642 643 644
}

void test_core_buffer__puts_escaped(void)
{
	git_buf a = GIT_BUF_INIT;

	git_buf_clear(&a);
645
	cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "", ""));
646 647 648
	cl_assert_equal_s("this is a test", a.ptr);

	git_buf_clear(&a);
649
	cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "t", "\\"));
650 651 652
	cl_assert_equal_s("\\this is a \\tes\\t", a.ptr);

	git_buf_clear(&a);
653
	cl_git_pass(git_buf_text_puts_escaped(&a, "this is a test", "i ", "__"));
654 655 656
	cl_assert_equal_s("th__is__ __is__ a__ test", a.ptr);

	git_buf_clear(&a);
657
	cl_git_pass(git_buf_text_puts_escape_regex(&a, "^match\\s*[A-Z]+.*"));
658 659 660 661
	cl_assert_equal_s("\\^match\\\\s\\*\\[A-Z\\]\\+\\.\\*", a.ptr);

	git_buf_free(&a);
}
662 663 664 665 666

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

	cl_git_pass(git_buf_sets(&buf, to_unescape));
667
	git_buf_text_unescape(&buf);
668
	cl_assert_equal_s(expected, buf.ptr);
669
	cl_assert_equal_sz(strlen(expected), buf.size);
670 671 672 673 674 675 676 677 678 679 680 681

	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("", "");
}
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706

void test_core_buffer__base64(void)
{
	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
	 */
	cl_git_pass(git_buf_put_base64(&buf, "this", 4));
	cl_assert_equal_s("dGhpcw==", buf.ptr);

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

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

	git_buf_free(&buf);
}
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729

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;
	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;
	cl_assert(git_buf_text_is_binary(&b));
	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));
}