test_lookup.c 2.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*  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 <http://www.gnu.org/licenses/>.
 */

#include <tap/basic.h>

19
#include "libknot/lookup.h"
20

21
const knot_lookup_t test_table[] = {
22 23 24 25 26
	{  0, "test item 0" },
	{ 10, "" },
	{  2, "test item 2" },
	{ -1, "test item -1" },
	{  0, NULL }
27 28 29 30
};

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

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

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

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

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

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

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

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

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

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

	return 0;
}