summaryrefslogtreecommitdiffstats
path: root/tests/libknot/test_lookup.c
blob: 3f7b514b0a424c1fb9ee64f64ebadfe4c5c9f9c1 (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
/*  Copyright (C) 2014 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 <tap/basic.h>

#include "libknot/lookup.h"

const knot_lookup_t test_table[] = {
	{  0, "test item 0" },
	{ 10, "" },
	{  2, "test item 2" },
	{ -1, "test item -1" },
	{  0, NULL }
};

int main(int argc, char *argv[])
{
	plan(9);

	/* Lookup by ID. */
	const knot_lookup_t *found = knot_lookup_by_id(test_table, 3);
	ok(found == NULL, "lookup table: find by id - non-existent ID");

	found = knot_lookup_by_id(test_table, 2);
	ok(found && found->id == 2 && strcmp(found->name, "test item 2") == 0,
	   "lookup table: find by id - ID 2 (unordered IDs)");

	found = knot_lookup_by_id(NULL, 2);
	ok(found == NULL, "lookup table: find by id - table == NULL");

	/* Lookup by name. */
	found = knot_lookup_by_name(test_table, "test item 2");
	ok(found && found->id == 2 && strcmp(found->name, "test item 2") == 0,
	   "lookup table: find by name - existent");

	found = knot_lookup_by_name(test_table, "");
	ok(found && found->id == 10 && strcmp(found->name, "") == 0,
	   "lookup table: find by name - empty string");

	found = knot_lookup_by_name(test_table, NULL);
	ok(found == NULL, "lookup table: find by name - NULL name");

	found = knot_lookup_by_name(NULL, "test item 2");
	ok(found == NULL, "lookup table: find by name - NULL table");

	found = knot_lookup_by_name(NULL, NULL);
	ok(found == NULL, "lookup table: find by name - NULL table & NULL name");

	found = knot_lookup_by_name(test_table, "non existent name");
	ok(found == NULL, "lookup table: find by name - non-existent name");

	return 0;
}