Browse Source

[radix]: Fix wrong var in macro, add test cases

personal/stbuehler/wip
Stefan Bühler 12 years ago
parent
commit
1790afc37f
  1. 1
      src/CMakeLists.txt
  2. 6
      src/common/radix.c
  3. 2
      src/unittests/Makefile.am
  4. 72
      src/unittests/test-radix.c

1
src/CMakeLists.txt

@ -439,5 +439,6 @@ IF(BUILD_UNIT_TESTS)
ADD_TEST_BINARY(Utils-UnitTest test-utils unittests/test-utils.c)
ADD_TEST_BINARY(Chunk-UnitTest test-chunk unittests/test-chunk.c)
ADD_TEST_BINARY(RangeParser-UnitTest test-range-parser unittests/test-range-parser.c)
ADD_TEST_BINARY(Radix-UnitTest test-radix unittests/test-radix.c)
ENDIF(BUILD_UNIT_TESTS)

6
src/common/radix.c

@ -8,7 +8,7 @@ typedef guint32 rdxBase;
#define RDXBITS (sizeof(rdxBase)*8)
/* 1^(width) 0^(RDXBITS-width): "1..10..0" */
#define RDX_MASK(width) ( bits ? ~( (((rdxBase)1) << (RDXBITS - width)) - 1 ) : 0 )
#define RDX_MASK(width) ( width ? ~( (((rdxBase)1) << (RDXBITS - width)) - 1 ) : 0 )
#define INPUT_SIZE(bits) ( bits ? (bits+RDXBITS-1) / RDXBITS : 1 )
#define INPUT_CHARS(bits) ( (bits+7) / 8 )
@ -18,8 +18,8 @@ struct liRadixNode{
rdxBase key;
guint32 width;
gpointer data;
liRadixNode *right;
liRadixNode *left;
liRadixNode *right; /* "1" bit */
liRadixNode *left; /* "0" bit */
};
struct liRadixTree {

2
src/unittests/Makefile.am

@ -5,7 +5,7 @@ AM_CFLAGS += $(GTHREAD_CFLAGS) $(GMODULE_CFLAGS) $(LIBEV_CFLAGS) $(LUA_CFLAGS)
AM_LDFLAGS = -export-dynamic -avoid-version -no-undefined $(GTHREAD_LIBS) $(GMODULE_LIBS) $(LIBEV_LIBS) $(LUA_LIBS)
LDADD = ../common/liblighttpd2-common.la ../main/liblighttpd2-shared.la
test_binaries=test-chunk test-range-parser test-utils
test_binaries=test-chunk test-range-parser test-utils test-radix
check_PROGRAMS=$(test_binaries)

72
src/unittests/test-radix.c

@ -0,0 +1,72 @@
#include <lighttpd/radix.h>
static void test_radix_insert_lookup(void) {
static const guint magic1 = 4235;
guint value;
/* 127.0.0.1 */
in_addr_t ip1 = htonl(INADDR_LOOPBACK);
liRadixTree *rd = li_radixtree_new();
li_radixtree_insert(rd, &ip1, 32, GUINT_TO_POINTER(magic1));
value = GPOINTER_TO_UINT(li_radixtree_lookup_exact(rd, &ip1, 32));
g_assert_cmpuint(magic1, ==, value);
li_radixtree_free(rd, NULL, NULL);
}
static void test_radix_insert_insert_lookup(void) {
static const guint magic1 = 4235 /* 0x108b */, magic2 = 59234 /* 0xe762 */;
guint value;
/* 127.0.0.1, 192.168.0.125 */
/* as guint32: 2130706433, 3232235645 */
in_addr_t ip1 = htonl(INADDR_LOOPBACK), ip2 = htonl( (in_addr_t) 0xC0A8007D );
liRadixTree *rd = li_radixtree_new();
li_radixtree_insert(rd, &ip1, 32, GUINT_TO_POINTER(magic1));
li_radixtree_insert(rd, &ip2, 32, GUINT_TO_POINTER(magic2));
value = GPOINTER_TO_UINT(li_radixtree_lookup_exact(rd, &ip1, 32));
g_assert_cmpuint(magic1, ==, value);
value = GPOINTER_TO_UINT(li_radixtree_lookup_exact(rd, &ip2, 32));
g_assert_cmpuint(magic2, ==, value);
li_radixtree_free(rd, NULL, NULL);
}
static void test_radix_insert_insert_del_lookup(void) {
static const guint magic1 = 4235 /* 0x108b */, magic2 = 59234 /* 0xe762 */;
guint value;
/* 127.0.0.1, 192.168.0.125 */
/* as guint32: 2130706433, 3232235645 */
in_addr_t ip1 = htonl(INADDR_LOOPBACK), ip2 = htonl( (in_addr_t) 0xC0A8007D );
liRadixTree *rd = li_radixtree_new();
li_radixtree_insert(rd, &ip1, 32, GUINT_TO_POINTER(magic1));
li_radixtree_insert(rd, &ip2, 32, GUINT_TO_POINTER(magic2));
li_radixtree_remove(rd, &ip2, 32);
value = GPOINTER_TO_UINT(li_radixtree_lookup_exact(rd, &ip1, 32));
g_assert_cmpuint(magic1, ==, value);
li_radixtree_free(rd, NULL, NULL);
}
int main(int argc, char **argv) {
g_test_init(&argc, &argv, NULL);
g_test_add_func("/radix/insert-lookup", test_radix_insert_lookup);
g_test_add_func("/radix/insert-insert-lookup", test_radix_insert_insert_lookup);
g_test_add_func("/radix/insert-insert-del-lookup", test_radix_insert_insert_del_lookup);
return g_test_run();
}
Loading…
Cancel
Save