Fix couple bugs in regtests, makefile cleanup
authorMarko Kreen <markokr@gmail.com>
Sat, 20 Nov 2010 11:38:39 +0000 (13:38 +0200)
committerMarko Kreen <markokr@gmail.com>
Mon, 22 Nov 2010 22:37:59 +0000 (00:37 +0200)
Now the makefile does not depend on top-level makefile,
regtest is built as ordinary executable.

test/Makefile
test/test_cbtree.c
test/test_heap.c
test/test_netdb.c
test/test_string.c

index 0d5f692a0643a0767bf9ce6e8d491fa6819d21c7..4a9d946fab3e92d5e63284551ac08219d4014ea6 100644 (file)
@@ -1,40 +1,55 @@
 
 
-#vpath %.mak ..
-#vpath %.mk ..
-#vpath %.c ..
-#vpath %.h ..
-
-override CPPFLAGS = -I. -I..
-override USUAL_DIR = ..
-
-override DEFS = -DUSUAL_TEST_CONFIG
 SRCS = test_string.c test_crypto.c test_aatree.c test_heap.c \
        test_common.c test_list.c tinytest.c test_cbtree.c \
        test_utf8.c test_strpool.c test_pgutil.c test_regex.c \
        test_cxalloc.c test_bits.c test_base.c test_netdb.c \
        test_cfparser.c
-OBJS = $(SRCS:.c=.o)
-
-test-all: regtest.compat
-
-include ../Makefile
-
-hdrs += test_config.h
-
-%.o: %.c $(hdrs)
-       @mkdir -p $(USUAL_OBJDIR)
+OBJS = $(addprefix obj/, $(SRCS:.c=.o))
+HDRS = test_common.h test_config.h tinytest.h tinytest_macros.h
+LIBS =
+
+USUAL_DIR = ..
+USUAL_OBJDIR = obj
+USUAL_LOCAL_SRCS = $(SRCS) $(HDRS)
+include $(USUAL_DIR)/Setup.mk
+include ../config.mak
+
+DEFS += -DUSUAL_TEST_CONFIG
+CPPFLAGS += -I.. -I.
+
+hdrs = $(HDRS) $(USUAL_HDRS)
+objs = $(OBJS) $(USUAL_OBJS)
+
+.SUFFIXES:
+
+# Quiet by default, 'make V=1' shows commands
+V=0
+ifeq ($(V), 0)
+Q = @
+E = @echo
+else
+Q = 
+E = @true
+endif
+
+all: regtest
+
+obj/%.o: %.c $(hdrs)
+       @mkdir -p obj
        $(E) "  CC" $<
-       $(Q) $(CC) -c -o $@ $(DEFS) $(CPPFLAGS) $(CFLAGS) $<
+       $(Q) $(CC) -c -o $@ $< $(DEFS) $(CFLAGS) $(CPPFLAGS) $(WFLAGS)
 
+obj/%.o: $(USUAL_DIR)/usual/%.c $(hdrs)
+       @mkdir -p obj
+       $(E) "  CC" $<
+       $(Q) $(CC) -c -o $@ $< $(DEFS) $(CFLAGS) $(CPPFLAGS) $(WFLAGS)
 
-regtest: libusual.a $(OBJS)
+regtest: $(objs)
        $(E) "  LD" $@
-       $(Q) $(CC) -o $@ $(OBJS) -L. -lusual
-
-clean: clean-test
+       $(Q) $(CC) -o $@ $(objs) $(LIBS)
 
-clean-test:
+clean:
        rm -f libusual.a obj/* *.o regtest
        rm -f config.test usual/config.h test_config.h
        rm -f regtest.compat regtest.system
@@ -43,21 +58,18 @@ clean-test:
 config.mak: ../config.mak
        cp ../config.mak .
 
-regtest.compat: ../usual/config.h force_compat.sed $(SRCS)
-       sed -f force_compat.sed $< > test_config.h
-       make regtest
-       mv regtest regtest.compat
-       mv libusual.a libusual_compat.a
-       rm test_config.h
-
-regtest.system: ../usual/config.h $(SRCS)
-       cp $< test_config.h
-       make regtest
-       mv regtest regtest.system
-       mv libusual.a libusual_system.a
-       rm test_config.h
-
-test: regtest.compat regtest.system
-       ./regtest.compat
-       ./regtest.system
+test_config.h: ../usual/config.h force_compat.sed
+       $(E) "  GEN-COMPAT" $<
+       $(Q) sed -f force_compat.sed $< > test_config.h
+
+sys: ../usual/config.h
+       rm -f test_config.h
+       cp ../usual/config.h test_config.h
+       make
+
+test: regtest
+       ./regtest
+
+vcheck: regtest
+       valgrind --leak-check=full ./regtest
 
index 480170f3b72b2856643c6bac44b295981b9ae312..11e65564ef9e5320e62f9e9ec47ec4ce6bb38140 100644 (file)
@@ -1,7 +1,8 @@
 
-#include "test_common.h"
+#include <usual/cbtree.h>
 
-#include <usual/cbtree.c>
+#include <usual/string.h>
+#include "test_common.h"
 
 static char *OK = "OK";
 
@@ -26,9 +27,10 @@ static struct MyNode *make_node(int value)
        return node;
 }
 
-static void my_node_free(struct MyNode *node)
+static bool my_node_free(void *ctx, void *obj)
 {
-       free(node);
+       free(obj);
+       return true;
 }
 
 /*
@@ -68,7 +70,6 @@ static const char *my_remove(struct CBTree *tree, int value)
        cbtree_delete(tree, buf, strlen(buf));
        if (cbtree_lookup(tree, buf, strlen(buf)) != NULL)
                return "still found";
-       my_node_free(my);
        return OK;
 }
 
@@ -81,7 +82,7 @@ static void test_cbtree_basic(void *p)
        struct CBTree *tree;
        int i;
 
-       tree = cbtree_create(my_getkey, NULL, NULL, USUAL_ALLOC);
+       tree = cbtree_create(my_getkey, my_node_free, NULL, USUAL_ALLOC);
 
        str_check(my_search(tree, 1), "not found");
 
@@ -138,7 +139,7 @@ static void test_cbtree_random(void *p)
        srandom(123123);
        memset(is_added, 0, sizeof(is_added));
 
-       tree = cbtree_create(my_getkey, NULL, NULL, USUAL_ALLOC);
+       tree = cbtree_create(my_getkey, my_node_free, NULL, USUAL_ALLOC);
 
        while (total < 100000) {
                int r = random() & 15;
index cc451c36b64f49e5e75238f662c1a84fec9a7a5d..6467b91eb751d89cb19b1ee9b3951c1fa1a180b7 100644 (file)
@@ -1,3 +1,4 @@
+
 #include <usual/heap.h>
 
 #include <stdio.h>
@@ -7,8 +8,6 @@
 
 #include "test_common.h"
 
-#include <usual/heap.c>
-
 struct MyNode {
        int value;
        unsigned heap_idx;
@@ -37,6 +36,17 @@ static struct MyNode *make_node(int v)
        return n;
 }
 
+static unsigned _heap_get_child(unsigned i, unsigned child_nr)
+{
+       return 2*i + 1 + child_nr;
+}
+
+static bool _heap_is_better(struct Heap *h, unsigned i1, unsigned i2)
+{
+       return heap_is_better(heap_get_obj(h, i1), heap_get_obj(h, i2));
+}
+
+
 /*
  * Test tree sanity
  */
@@ -54,17 +64,18 @@ static const char *check_sub(struct Heap *heap, unsigned idx, int i)
        unsigned c1 = _heap_get_child(idx, 1);
        struct MyNode *n;
        const char *res;
+       unsigned used = heap_size(heap);
 
-       if (idx >= heap->used)
+       if (idx >= used)
                return OK;
 
-       n = heap->data[idx];
+       n = heap_get_obj(heap, idx);
        if (n->heap_idx != idx)
                return mkerr("wrong saved idx", idx, i);
 
-       if (c0 < heap->used && _heap_is_better(heap, c0, idx))
+       if (c0 < used && _heap_is_better(heap, c0, idx))
                return mkerr("c0 wrong order", idx, i);
-       if (c1 < heap->used && _heap_is_better(heap, c1, idx))
+       if (c1 < used && _heap_is_better(heap, c1, idx))
                return mkerr("c1 wrong order", idx, i);
 
        res = check_sub(heap, c0, i);
@@ -90,21 +101,21 @@ static const char *my_insert(struct Heap *heap, int value)
        return check(heap, value);
 }
 
-static const char *my_remove(struct Heap *heap, unsigned idx)
+static const char *my_remove(struct Heap *h, unsigned idx)
 {
        struct MyNode *n;
-       if (idx >= heap->used)
+       if (idx >= heap_size(h))
                return "NEXIST";
-       n = heap->data[idx];
-       heap_remove(heap, idx);
+       n = heap_get_obj(h, idx);
+       heap_remove(h, idx);
        free(n);
-       return check(heap, 0);
+       return check(h, 0);
 }
 
 static const char *my_clean(struct Heap *heap)
 {
        const char *res;
-       while (heap->used > 0) {
+       while (heap_size(heap) > 0) {
                res = my_remove(heap, 0);
                if (res != OK)
                        return res;
index d8a62267b707b6dbd5128230ece3bc45743c98b3..b816654ba1bb4fc2039a2cad7df1ceabf522a872 100644 (file)
@@ -2,22 +2,42 @@
 #include <usual/netdb.h>
 
 #include <usual/string.h>
+#include <usual/time.h>
 
 #include "test_common.h"
 
+static int gotres;
+
+static void cb_func(sigval_t v)
+{
+       gotres++;
+}
 
 static void test_gai(void *p)
 {
        int res;
        struct sigevent sev;
+       struct gaicb req;
+       struct gaicb *rlist[] = { &req };
 
-       memset(&sev, 0, sizeof(sev));
+       memset(&req, 0, sizeof(req));
+       req.ar_name = "localhost";
 
+       memset(&sev, 0, sizeof(sev));
        sev.sigev_notify = SIGEV_THREAD;
+       sev.sigev_notify_function = cb_func;
 
-       res = getaddrinfo_a(GAI_NOWAIT, NULL, 0, &sev);
-
+       res = getaddrinfo_a(GAI_NOWAIT, rlist, 1, &sev);
        int_check(res, 0);
+
+       while (gai_error(&req) == EAI_INPROGRESS || gotres == 0)
+               usleep(10000);
+
+       int_check(gai_error(&req), 0);
+
+       freeaddrinfo(req.ar_result);
+
+       int_check(gotres, 1);
 end:;
 }
 
index dfbf79f423b6ba483293b85cbf1c5f17b93d452e..1ec9dad20002fe76d7b84f2c30b11fedec8c3a77 100644 (file)
@@ -102,10 +102,10 @@ end:;
 
 static const char *run_basename(const char *path)
 {
-       char buf[128];
+       static char buf[128];
        const char *res;
        if (!path)
-               return basename(path);
+               return basename(NULL);
        strlcpy(buf, path, sizeof(buf));
        res = basename(buf);
        if (strcmp(buf, path) != 0)
@@ -132,10 +132,10 @@ end:;
 
 static const char *run_dirname(const char *path)
 {
-       char buf[128];
+       static char buf[128];
        const char *res;
        if (!path)
-               return dirname(path);
+               return dirname(NULL);
        strlcpy(buf, path, sizeof(buf));
        res = dirname(buf);
        if (strcmp(buf, path) != 0)