summaryrefslogtreecommitdiffstats
path: root/tests/test_pack.c
blob: a648832e5f76cab7b68a1cbc99ada30e9ab57b58 (plain)
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
/*  Copyright (C) 2015-2017 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "tests/test.h"
#include "lib/generic/pack.h"

#define U8(x) (const uint8_t *)(x)
knot_mm_t global_mm;

static void test_pack_std(void **state)
{
	int ret = 0;
	pack_t pack;
	pack_init(pack);
	assert_int_equal(pack.len, 0);

	/* Test that iterator on empty pack works */
	assert_null(pack_head(pack));
	assert_null(pack_tail(pack));
	assert_null(pack_obj_find(&pack, U8(""), 1));
	assert_int_equal(pack_obj_len(pack_head(pack)), 0);
	assert_int_equal(pack_obj_del(&pack, U8(""), 1), -1);

	/* Push/delete without reservation. */
	assert_int_not_equal(pack_obj_push(&pack, U8(""), 1), 0);
	assert_int_not_equal(pack_obj_del(&pack, U8(""), 1), 0);

	/* Reserve capacity and fill. */
	assert_true(pack_reserve(pack, 10, 10 * 2) >= 0);
	for (unsigned i = 0; i < 10; ++i) {
		ret = pack_obj_push(&pack, U8("de"), 2);
		assert_true(ret >= 0);
	}

	/* Iterate */
	uint8_t *it = pack_head(pack);
	assert_non_null(it);
	unsigned count = 0;
	while (it != pack_tail(pack)) {
		assert_int_equal(pack_obj_len(it), 2);
		assert_true(memcmp(pack_obj_val(it), "de", 2) == 0);
		it = pack_obj_next(it);
		count += 1;
	}

	/* Find */
	it = pack_obj_find(&pack, U8("de"), 2);
	assert_non_null(it);
	it = pack_obj_find(&pack, U8("ed"), 2);
	assert_null(it);

	/* Delete */
	assert_int_not_equal(pack_obj_del(&pack, U8("be"), 2), 0);
	assert_int_equal(pack_obj_del(&pack, U8("de"), 2), 0);
	assert_int_equal(pack.len, 9*(2+2)); /* 9 objects, length=2 */

	pack_clear(pack);
}

int main(void)
{
	test_mm_ctx_init(&global_mm);

	const UnitTest tests[] = {
		unit_test(test_pack_std),
	};

	return run_tests(tests);
}