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

unittests: Finished utils test +lookup table fixed

lookup table: checking parameters
serial compare: legal corner cases proper testing
                (Maybe they should be ommited, because it would
                also be legal if the comparisons had opposite
                results. But at least we will know if we change
                something.)
parent 9b7d93cb
No related branches found
No related tags found
No related merge requests found
......@@ -25,9 +25,12 @@
/*----------------------------------------------------------------------------*/
_public_
lookup_table_t *lookup_by_name(lookup_table_t *table,
const char *name)
lookup_table_t *lookup_by_name(lookup_table_t *table, const char *name)
{
if (table == NULL || name == NULL) {
return NULL;
}
while (table->name != NULL) {
if (strcasecmp(name, table->name) == 0) {
return table;
......@@ -40,9 +43,12 @@ lookup_table_t *lookup_by_name(lookup_table_t *table,
/*----------------------------------------------------------------------------*/
_public_
lookup_table_t *lookup_by_id(lookup_table_t *table,
int id)
lookup_table_t *lookup_by_id(lookup_table_t *table, int id)
{
if (table == NULL) {
return NULL;
}
while (table->name != NULL) {
if (table->id == id) {
return table;
......
......@@ -46,9 +46,6 @@ 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.
*/
......@@ -60,8 +57,6 @@ 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.
*/
......
......@@ -19,6 +19,7 @@
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "libknot/internal/utils.h"
......@@ -39,33 +40,51 @@ enum serials {
S_HIGHEST = 0xffffffff // highest value
};
static uint32_t random_serial()
{
uint32_t s = rand() & 0xff;
s |= (rand() & 0xff) << 8;
s |= (rand() & 0xff) << 16;
s |= (rand() & 0xff) << 24;
return s;
}
int main(int argc, char *argv[])
{
plan(5 + 18);
plan(9 + 20);
/* Lookup by ID. */
lookup_table_t *found = lookup_by_id(test_table, 3);
ok(found == NULL, "lookup table: find non-existent ID");
ok(found == NULL, "lookup table: find by id - 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 table: find by id - ID 2 (unordered IDs)");
found = lookup_by_id(NULL, 2);
ok(found == NULL, "lookup table: find by id - table == NULL");
/* 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");
"lookup table: find by name - existent");
found = lookup_by_name(test_table, "");
ok(found && found->id == 10 && strcmp(found->name, "") == 0,
"lookup table: find item with empty string");
"lookup table: find by name - empty string");
found = lookup_by_name(test_table, NULL);
ok(found == NULL, "lookup table: find by name - NULL name");
// The function does not take NULL argument
// found = lookup_by_name(test_table, NULL);
// ok(found == NULL, "Find NULL");
found = lookup_by_name(NULL, "test item 2");
ok(found == NULL, "lookup table: find by name - NULL table");
found = lookup_by_name(NULL, NULL);
ok(found == NULL, "lookup table: find by name - NULL table & NULL name");
found = lookup_by_name(test_table, "non existent name");
ok(found == NULL, "lookup table: find non-existent name");
ok(found == NULL, "lookup table: find by name - non-existent name");
/* Serial compare test. */
......@@ -74,9 +93,9 @@ int main(int argc, char *argv[])
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");
/* Corner-case: these serials' distance is exactly 2^31. */
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");
......@@ -90,9 +109,9 @@ int main(int argc, char *argv[])
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");
/* Corner-case: these serials' distance is exactly 2^31. */
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");
......@@ -116,5 +135,13 @@ int main(int argc, char *argv[])
ok(serial_compare(S_LOWEST, S_HIGHEST + 1) == 0,
"serial compare: lowest== highest + 1");
/* Corner-case: these serials' distance is exactly 2^31. */
uint32_t s1 = random_serial();
uint32_t s2 = s1 + S_ABOVE_MIDDLE; // exactly the 'oposite' number
ok(serial_compare(s1, s2) < 0,
"serial compare: random oposites (s1 < s2)");
ok(serial_compare(s2, s1) < 0,
"serial compare: random oposites (s2 < s1)");
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