vector.c 10.1 KB
Newer Older
1 2
#include <stdint.h>

3
#include "clar_libgit2.h"
Vicent Marti committed
4 5 6
#include "vector.h"

/* initial size of 1 would cause writing past array bounds */
7
void test_vector__0(void)
Vicent Marti committed
8 9 10
{
	git_vector x;
	int i;
11
	cl_git_pass(git_vector_init(&x, 1, NULL));
Vicent Marti committed
12 13 14 15 16 17 18 19
	for (i = 0; i < 10; ++i) {
		git_vector_insert(&x, (void*) 0xabc);
	}
	git_vector_free(&x);
}


/* don't read past array bounds on remove() */
20
void test_vector__1(void)
Vicent Marti committed
21 22
{
	git_vector x;
23
	/* make initial capacity exact for our insertions. */
24
	cl_git_pass(git_vector_init(&x, 3, NULL));
Vicent Marti committed
25 26 27 28
	git_vector_insert(&x, (void*) 0xabc);
	git_vector_insert(&x, (void*) 0xdef);
	git_vector_insert(&x, (void*) 0x123);

29
	git_vector_remove(&x, 0); /* used to read past array bounds. */
Vicent Marti committed
30 31 32 33 34 35 36 37 38 39
	git_vector_free(&x);
}


static int test_cmp(const void *a, const void *b)
{
	return *(const int *)a - *(const int *)b;
}

/* remove duplicates */
40
void test_vector__2(void)
Vicent Marti committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
{
	git_vector x;
	int *ptrs[2];

	ptrs[0] = git__malloc(sizeof(int));
	ptrs[1] = git__malloc(sizeof(int));

	*ptrs[0] = 2;
	*ptrs[1] = 1;

	cl_git_pass(git_vector_init(&x, 5, test_cmp));
	cl_git_pass(git_vector_insert(&x, ptrs[0]));
	cl_git_pass(git_vector_insert(&x, ptrs[1]));
	cl_git_pass(git_vector_insert(&x, ptrs[1]));
	cl_git_pass(git_vector_insert(&x, ptrs[0]));
	cl_git_pass(git_vector_insert(&x, ptrs[1]));
	cl_assert(x.length == 5);

59
	git_vector_uniq(&x, NULL);
Vicent Marti committed
60 61 62 63
	cl_assert(x.length == 2);

	git_vector_free(&x);

64 65
	git__free(ptrs[0]);
	git__free(ptrs[1]);
Vicent Marti committed
66 67 68
}


69 70
static int compare_them(const void *a, const void *b)
{
71
	return (int)((intptr_t)a - (intptr_t)b);
72 73 74
}

/* insert_sorted */
75
void test_vector__3(void)
76 77
{
	git_vector x;
78
	intptr_t i;
79
	cl_git_pass(git_vector_init(&x, 1, &compare_them));
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

	for (i = 0; i < 10; i += 2) {
		git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
	}

	for (i = 9; i > 0; i -= 2) {
		git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
	}

	cl_assert(x.length == 10);
	for (i = 0; i < 10; ++i) {
		cl_assert(git_vector_get(&x, i) == (void*)(i + 1));
	}

	git_vector_free(&x);
}

/* insert_sorted with duplicates */
98
void test_vector__4(void)
99 100
{
	git_vector x;
101
	intptr_t i;
102
	cl_git_pass(git_vector_init(&x, 1, &compare_them));
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

	for (i = 0; i < 10; i += 2) {
		git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
	}

	for (i = 9; i > 0; i -= 2) {
		git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
	}

	for (i = 0; i < 10; i += 2) {
		git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
	}

	for (i = 9; i > 0; i -= 2) {
		git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
	}

	cl_assert(x.length == 20);
	for (i = 0; i < 20; ++i) {
		cl_assert(git_vector_get(&x, i) == (void*)(i / 2 + 1));
	}

	git_vector_free(&x);
}

typedef struct {
	int content;
	int count;
} my_struct;

static int _struct_count = 0;

static int compare_structs(const void *a, const void *b)
{
	return ((const my_struct *)a)->content -
		((const my_struct *)b)->content;
}

static int merge_structs(void **old_raw, void *new)
{
	my_struct *old = *(my_struct **)old_raw;
	cl_assert(((my_struct *)old)->content == ((my_struct *)new)->content);
	((my_struct *)old)->count += 1;
	git__free(new);
	_struct_count--;
	return GIT_EEXISTS;
}

static my_struct *alloc_struct(int value)
{
	my_struct *st = git__malloc(sizeof(my_struct));
	st->content = value;
	st->count = 0;
	_struct_count++;
	return st;
}

/* insert_sorted with duplicates and special handling */
161
void test_vector__5(void)
162 163 164 165
{
	git_vector x;
	int i;

166
	cl_git_pass(git_vector_init(&x, 1, &compare_structs));
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

	for (i = 0; i < 10; i += 2)
		git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs);

	for (i = 9; i > 0; i -= 2)
		git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs);

	cl_assert(x.length == 10);
	cl_assert(_struct_count == 10);

	for (i = 0; i < 10; i += 2)
		git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs);

	for (i = 9; i > 0; i -= 2)
		git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs);

	cl_assert(x.length == 10);
	cl_assert(_struct_count == 10);

	for (i = 0; i < 10; ++i) {
		cl_assert(((my_struct *)git_vector_get(&x, i))->content == i);
		git__free(git_vector_get(&x, i));
		_struct_count--;
	}

	git_vector_free(&x);
}
Edward Thomson committed
194

195
static int remove_ones(const git_vector *v, size_t idx, void *p)
Edward Thomson committed
196
{
197
	GIT_UNUSED(p);
Edward Thomson committed
198 199 200 201
	return (git_vector_get(v, idx) == (void *)0x001);
}

/* Test removal based on callback */
202
void test_vector__remove_matching(void)
Edward Thomson committed
203 204 205 206 207
{
	git_vector x;
	size_t i;
	void *compare;

208
	cl_git_pass(git_vector_init(&x, 1, NULL));
Edward Thomson committed
209 210 211
	git_vector_insert(&x, (void*) 0x001);

	cl_assert(x.length == 1);
212
	git_vector_remove_matching(&x, remove_ones, NULL);
Edward Thomson committed
213 214 215 216 217 218 219
	cl_assert(x.length == 0);

	git_vector_insert(&x, (void*) 0x001);
	git_vector_insert(&x, (void*) 0x001);
	git_vector_insert(&x, (void*) 0x001);

	cl_assert(x.length == 3);
220
	git_vector_remove_matching(&x, remove_ones, NULL);
Edward Thomson committed
221 222 223 224 225 226 227 228
	cl_assert(x.length == 0);

	git_vector_insert(&x, (void*) 0x002);
	git_vector_insert(&x, (void*) 0x001);
	git_vector_insert(&x, (void*) 0x002);
	git_vector_insert(&x, (void*) 0x001);

	cl_assert(x.length == 4);
229
	git_vector_remove_matching(&x, remove_ones, NULL);
Edward Thomson committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243
	cl_assert(x.length == 2);

	git_vector_foreach(&x, i, compare) {
		cl_assert(compare != (void *)0x001);
	}

	git_vector_clear(&x);

	git_vector_insert(&x, (void*) 0x001);
	git_vector_insert(&x, (void*) 0x002);
	git_vector_insert(&x, (void*) 0x002);
	git_vector_insert(&x, (void*) 0x001);

	cl_assert(x.length == 4);
244
	git_vector_remove_matching(&x, remove_ones, NULL);
Edward Thomson committed
245 246 247 248 249 250 251 252 253 254 255 256 257 258
	cl_assert(x.length == 2);

	git_vector_foreach(&x, i, compare) {
		cl_assert(compare != (void *)0x001);
	}

	git_vector_clear(&x);

	git_vector_insert(&x, (void*) 0x002);
	git_vector_insert(&x, (void*) 0x001);
	git_vector_insert(&x, (void*) 0x002);
	git_vector_insert(&x, (void*) 0x001);

	cl_assert(x.length == 4);
259
	git_vector_remove_matching(&x, remove_ones, NULL);
Edward Thomson committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273
	cl_assert(x.length == 2);

	git_vector_foreach(&x, i, compare) {
		cl_assert(compare != (void *)0x001);
	}

	git_vector_clear(&x);

	git_vector_insert(&x, (void*) 0x002);
	git_vector_insert(&x, (void*) 0x003);
	git_vector_insert(&x, (void*) 0x002);
	git_vector_insert(&x, (void*) 0x003);

	cl_assert(x.length == 4);
274
	git_vector_remove_matching(&x, remove_ones, NULL);
Edward Thomson committed
275 276 277 278
	cl_assert(x.length == 4);

	git_vector_free(&x);
}
279 280 281 282 283 284 285 286 287 288 289

static void assert_vector(git_vector *x, void *expected[], size_t len)
{
	size_t i;

	cl_assert_equal_i(len, x->length);

	for (i = 0; i < len; i++)
		cl_assert(expected[i] == x->contents[i]);
}

290
void test_vector__grow_and_shrink(void)
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
{
	git_vector x = GIT_VECTOR_INIT;
	void *expected1[] = {
		(void *)0x02, (void *)0x03, (void *)0x04, (void *)0x05,
		(void *)0x06, (void *)0x07, (void *)0x08, (void *)0x09,
		(void *)0x0a
	};
	void *expected2[] = {
		(void *)0x02, (void *)0x04, (void *)0x05, (void *)0x06,
		(void *)0x07, (void *)0x08, (void *)0x09, (void *)0x0a
	};
	void *expected3[] = {
		(void *)0x02, (void *)0x04, (void *)0x05, (void *)0x06,
		(void *)0x0a
	};
	void *expected4[] = {
		(void *)0x02, (void *)0x04, (void *)0x05
	};
	void *expected5[] = {
		(void *)0x00, (void *)0x00, (void *)0x02, (void *)0x04,
		(void *)0x05
	};
	void *expected6[] = {
		(void *)0x00, (void *)0x00, (void *)0x02, (void *)0x04,
		(void *)0x05, (void *)0x00
	};
	void *expected7[] = {
		(void *)0x00, (void *)0x00, (void *)0x02, (void *)0x04,
		(void *)0x00, (void *)0x00, (void *)0x00, (void *)0x05,
		(void *)0x00
	};
	void *expected8[] = {
		(void *)0x04, (void *)0x00, (void *)0x00, (void *)0x00,
		(void *)0x05, (void *)0x00
	};
	void *expected9[] = {
		(void *)0x04, (void *)0x00, (void *)0x05, (void *)0x00
	};
	void *expectedA[] = { (void *)0x04, (void *)0x00 };
	void *expectedB[] = { (void *)0x04 };

	git_vector_insert(&x, (void *)0x01);
	git_vector_insert(&x, (void *)0x02);
	git_vector_insert(&x, (void *)0x03);
	git_vector_insert(&x, (void *)0x04);
	git_vector_insert(&x, (void *)0x05);
	git_vector_insert(&x, (void *)0x06);
	git_vector_insert(&x, (void *)0x07);
	git_vector_insert(&x, (void *)0x08);
	git_vector_insert(&x, (void *)0x09);
	git_vector_insert(&x, (void *)0x0a);

343
	git_vector_remove_range(&x, 0, 1);
344 345
	assert_vector(&x, expected1, ARRAY_SIZE(expected1));

346
	git_vector_remove_range(&x, 1, 1);
347 348
	assert_vector(&x, expected2, ARRAY_SIZE(expected2));

349
	git_vector_remove_range(&x, 4, 3);
350 351
	assert_vector(&x, expected3, ARRAY_SIZE(expected3));

352
	git_vector_remove_range(&x, 3, 2);
353 354
	assert_vector(&x, expected4, ARRAY_SIZE(expected4));

355
	git_vector_insert_null(&x, 0, 2);
356 357
	assert_vector(&x, expected5, ARRAY_SIZE(expected5));

358
	git_vector_insert_null(&x, 5, 1);
359 360
	assert_vector(&x, expected6, ARRAY_SIZE(expected6));

361
	git_vector_insert_null(&x, 4, 3);
362 363
	assert_vector(&x, expected7, ARRAY_SIZE(expected7));

364
	git_vector_remove_range(&x, 0, 3);
365 366
	assert_vector(&x, expected8, ARRAY_SIZE(expected8));

367
	git_vector_remove_range(&x, 1, 2);
368 369
	assert_vector(&x, expected9, ARRAY_SIZE(expected9));

370
	git_vector_remove_range(&x, 2, 2);
371 372
	assert_vector(&x, expectedA, ARRAY_SIZE(expectedA));

373
	git_vector_remove_range(&x, 1, 1);
374 375
	assert_vector(&x, expectedB, ARRAY_SIZE(expectedB));

376
	git_vector_remove_range(&x, 0, 1);
377 378 379 380
	assert_vector(&x, NULL, 0);

	git_vector_free(&x);
}
381

382
void test_vector__reverse(void)
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
{
	git_vector v = GIT_VECTOR_INIT;
	size_t i;

	void *in1[] = {(void *) 0x0, (void *) 0x1, (void *) 0x2, (void *) 0x3};
	void *out1[] = {(void *) 0x3, (void *) 0x2, (void *) 0x1, (void *) 0x0};

	void *in2[] = {(void *) 0x0, (void *) 0x1, (void *) 0x2, (void *) 0x3, (void *) 0x4};
	void *out2[] = {(void *) 0x4, (void *) 0x3, (void *) 0x2, (void *) 0x1, (void *) 0x0};

	for (i = 0; i < 4; i++)
		cl_git_pass(git_vector_insert(&v, in1[i]));

	git_vector_reverse(&v);

	for (i = 0; i < 4; i++)
		cl_assert_equal_p(out1[i], git_vector_get(&v, i));

	git_vector_clear(&v);
	for (i = 0; i < 5; i++)
		cl_git_pass(git_vector_insert(&v, in2[i]));

	git_vector_reverse(&v);

	for (i = 0; i < 5; i++)
		cl_assert_equal_p(out2[i], git_vector_get(&v, i));
409 410

	git_vector_free(&v);
411
}
412

413
void test_vector__dup_empty_vector(void)
414 415 416
{
	git_vector v = GIT_VECTOR_INIT;
	git_vector dup = GIT_VECTOR_INIT;
417
	int dummy;
418 419 420 421 422 423 424

	cl_assert_equal_i(0, v.length);

	cl_git_pass(git_vector_dup(&dup, &v, v._cmp));
	cl_assert_equal_i(0, dup._alloc_size);
	cl_assert_equal_i(0, dup.length);

425
	cl_git_pass(git_vector_insert(&dup, &dummy));
426 427 428 429 430
	cl_assert_equal_i(8, dup._alloc_size);
	cl_assert_equal_i(1, dup.length);

	git_vector_free(&dup);
}