summaryrefslogtreecommitdiff
path: root/tests/vec.c
blob: 1ed5f729ce9f43c0610129f92faa673766f1d058 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#include <assert.h>
#include "test.h"

/* required defs */
#define VEC_TYPE int
#define VEC_NAME ints

/* optional defs */
#define VEC_MALLOC mallocc
#define VEC_CALLOC callocc
#define VEC_REALLOC reallocc
#define VEC_FREE free

#include <conts/vec.h>

/* used for sorting testing */
static int int_comp(int *a, int *b)
{
	return *a - *b;
}

int main()
{
#if defined(COVERAGE)
	assert(!covsrv_init());
	atexit(covsrv_destroy);
#endif

	struct ints ints = ints_create(0);
	for (int i = 0; i < 1000000; ++i) {
		if (!ints_append(&ints, i)) {
			fprintf(stderr, "failed appending %d to vec\n", i);
			ints_destroy(&ints);
			return -1;
		}
	}
	assert(ints_len(&ints) == 1000000);

	for (int i = 0; i < 1000000; ++i) {
		int *v = ints_at(&ints, i);
		assert(v && *v == i);
	}

	int i = 0;
	foreach(ints, iter, &ints) {
		assert(iter && *iter == i);
		i++;
	}

	/* TEN million !!1! */
	if (!ints_reserve(&ints, 10000000)) {
		fprintf(stderr, "failed reserving vec\n");
		ints_destroy(&ints);
		return -1;
	}

	/* set size back to keep test runtime reasonable
	 * (is shrink necessary when we already have reserve? I
	 * guess it shows intention a bit better and just asserts instead of
	 * maybe fails?) */
	ints_shrink(&ints, 1000000);

	/* so the above is equivalent to */
	assert(ints_reserve(&ints, 1000000));
	assert(ints_len(&ints) == 1000000);

	for (int i = 1000000 - 1; i >= 0; --i) {
		ints_remove(&ints, i);
	}
	assert(ints_len(&ints) == 0);

	/* test out resetting as well */
	assert(ints_reserve(&ints, 10));
	assert(ints_len(&ints) == 10);

	ints_reset(&ints);
	assert(ints_len(&ints) == 0);

	/* try out sorting and special accesses */
	ints_append(&ints, 3);
	ints_append(&ints, 2);
	ints_append(&ints, 1);

	ints_sort(&ints, int_comp);

	assert(*ints_back(&ints) == 3);
	assert(*ints_pop(&ints) == 3);
	assert(*ints_back(&ints) == 2);

	ints_destroy(&ints);
}