Skip to content
Snippets Groups Projects
Commit 9b7d93cb authored by Lubos Slovak's avatar Lubos Slovak
Browse files

unittests: Test for lookup table and serial_compare

TODO: Handle the undefined corner cases of serial difference.
TODO: NULL arguments to lookup table?
parent a437b0df
No related branches found
No related tags found
No related merge requests found
......@@ -349,6 +349,7 @@ tests/rrset.c
tests/rrset_wire.c
tests/sample_conf.h
tests/server.c
tests/utils.c
tests/wire.c
tests/worker_pool.c
tests/worker_queue.c
......
......@@ -46,6 +46,9 @@ typedef struct lookup_table lookup_table_t;
* \param table Lookup table.
* \param name Name to look up.
*
* \warning Both \a table and \a name must be non-NULL. The function does not
* check parameters.
*
* \return Item in the lookup table with the given name or NULL if no such is
* present.
*/
......@@ -57,6 +60,8 @@ lookup_table_t *lookup_by_name(lookup_table_t *table, const char *name);
* \param table Lookup table.
* \param id ID to look up.
*
* \warning \a table must be non-NULL. The function does not check parameters.
*
* \return Item in the lookup table with the given id or NULL if no such is
* present.
*/
......
......@@ -40,6 +40,7 @@ check_PROGRAMS = \
rrset \
rrset_wire \
server \
utils \
wire \
worker_pool \
worker_queue \
......
/* 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>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "libknot/internal/utils.h"
lookup_table_t test_table[] = {
{0, "test item 0"},
{10, ""},
{2, "test item 2"},
{-1, "test item -1"},
{ 0, NULL }
};
enum serials {
S_LOWEST = 0, // lowest value
S_2LOWEST = 1, // second lowest value
S_BELOW_MIDDLE = 0x7fffffff, // one below middle
S_ABOVE_MIDDLE = 0x80000000, // one above middle
S_2HIGHEST = 0xffffffff - 1, // second highest value
S_HIGHEST = 0xffffffff // highest value
};
int main(int argc, char *argv[])
{
plan(5 + 18);
/* Lookup by ID. */
lookup_table_t *found = lookup_by_id(test_table, 3);
ok(found == NULL, "lookup table: find non-existent ID");
found = lookup_by_id(test_table, 2);
ok(found && found->id == 2 && strcmp(found->name, "test item 2") == 0,
"lookup table: find ID 2 (unordered IDs)");
/* Lookup by name. */
found = lookup_by_name(test_table, "test item 2");
ok(found && found->id == 2 && strcmp(found->name, "test item 2") == 0,
"lookup table: find existent name");
found = lookup_by_name(test_table, "");
ok(found && found->id == 10 && strcmp(found->name, "") == 0,
"lookup table: find item with empty string");
// The function does not take NULL argument
// found = lookup_by_name(test_table, NULL);
// ok(found == NULL, "Find NULL");
found = lookup_by_name(test_table, "non existent name");
ok(found == NULL, "lookup table: find non-existent name");
/* Serial compare test. */
ok(serial_compare(S_LOWEST, S_BELOW_MIDDLE) < 0,
"serial compare: lowest < below middle");
ok(serial_compare(S_BELOW_MIDDLE, S_LOWEST) > 0,
"serial compare: below middle > lowest");
// TODO: Both are -1, but that's not an error!
ok(serial_compare(S_LOWEST, S_ABOVE_MIDDLE) > 0,
"serial compare: lowest > above_middle");
ok(serial_compare(S_ABOVE_MIDDLE, S_LOWEST) < 0,
"serial compare: above_middle < lowest");
ok(serial_compare(S_LOWEST, S_HIGHEST) > 0,
"serial compare: lowest > highest");
ok(serial_compare(S_HIGHEST, S_LOWEST) < 0,
"serial compare: highest < lowest");
ok(serial_compare(S_2LOWEST, S_ABOVE_MIDDLE) < 0,
"serial compare: 2nd lowest < above middle");
ok(serial_compare(S_ABOVE_MIDDLE, S_2LOWEST) > 0,
"serial compare: above middle > 2nd lowest");
// TODO: Both are -1, but that's not an error!
ok(serial_compare(S_BELOW_MIDDLE, S_HIGHEST) > 0,
"serial compare: below middle > highest");
ok(serial_compare(S_HIGHEST, S_BELOW_MIDDLE) < 0,
"serial compare: highest < below middle");
ok(serial_compare(S_BELOW_MIDDLE, S_2HIGHEST) < 0,
"serial compare: below middle < 2nd highest");
ok(serial_compare(S_2HIGHEST, S_BELOW_MIDDLE) > 0,
"serial compare: 2nd highest > below middle");
ok(serial_compare(S_ABOVE_MIDDLE, S_HIGHEST) < 0,
"serial compare: above middle < highest");
ok(serial_compare(S_HIGHEST, S_ABOVE_MIDDLE) > 0,
"serial compare: highest > above middle");
ok(serial_compare(S_LOWEST, S_LOWEST) == 0,
"serial compare: lowest == lowest");
ok(serial_compare(S_HIGHEST, S_HIGHEST) == 0,
"serial compare: highest == highest");
ok(serial_compare(S_LOWEST - 1, S_HIGHEST) == 0,
"serial compare: lowest - 1 == highest");
ok(serial_compare(S_LOWEST, S_HIGHEST + 1) == 0,
"serial compare: lowest== highest + 1");
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment