]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Birdtest: Refactore tests and build system
authorPavel Tvrdík <pawel.tvrdik@gmail.cz>
Mon, 23 Mar 2015 16:40:13 +0000 (17:40 +0100)
committerPavel Tvrdík <pawel.tvrdik@gmail.cz>
Mon, 23 Mar 2015 16:40:13 +0000 (17:40 +0100)
Rename directory:
  birdtest/* -> test/*

Rename Makefile rule:
  build-tests -> tests

Move run-all-test shell script from Makefile to stand-alone shell script

Simplify Makefile test build system

lib/heap_test.c
lib/lists_test.c
test/birdtest.c [moved from birdtest/birdtest.c with 98% similarity]
test/birdtest.h [moved from birdtest/birdtest.h with 96% similarity]
tools/Makefile-top.in
tools/Makefile.in
tools/Rules.in
tools/run_tests.sh [new file with mode: 0755]

index dc188a626516452c65ad702ea8e876faf1dcb332..d86a5985e252df45f6de51494e36ad775a936611 100644 (file)
@@ -6,7 +6,7 @@
  *     Can be freely distributed and used under the terms of the GNU GPL.
  */
 
-#include "birdtest.h"
+#include "test/birdtest.h"
 #include "lib/heap.h"
 
 #define MAX_NUM 1000
 
 #define MY_CMP(x, y) ((x) < (y))
 
+#define MY_HEAP_SWAP(heap,a,b,t)               \
+    do {                                       \
+      bt_debug("swap(%d %d) ", a, b);          \
+      HEAP_SWAP(heap,a,b,t);                   \
+    } while(0)
+
 static int num;
 static int heap[MAX_NUM+1];
 
@@ -29,16 +35,6 @@ static int heap[MAX_NUM+1];
        bt_debug("NON-VALID HEAP! \n");         \
     } while(0)
 
-
-#undef HEAP_SWAP
-#define HEAP_SWAP(heap,a,b,t)                  \
-    do {                                       \
-      t=heap[a];                               \
-      heap[a]=heap[b];                         \
-      heap[b]=t;                               \
-      bt_debug("swap(%d %d) ", a, b);          \
-    } while(0)
-
 static int
 is_heap_valid(int heap[], uint num)
 {
@@ -79,7 +75,7 @@ t_heap_insert(void)
   {
     bt_debug("ins %d at pos %d ", MAX_NUM - i, i);
     heap[i] = MAX_NUM - i;
-    HEAP_INSERT(heap, ++num, int, MY_CMP, HEAP_SWAP);
+    HEAP_INSERT(heap, ++num, int, MY_CMP, MY_HEAP_SWAP);
     SHOW_HEAP(heap);
     bt_assert(is_heap_valid(heap, num));
   }
@@ -100,13 +96,13 @@ t_heap_increase_decrease(void)
     {
       bt_debug("inc %d ", i);
       heap[i] = i;
-      HEAP_INCREASE(heap, num, int, MY_CMP, HEAP_SWAP, i);
+      HEAP_INCREASE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
     }
     else if (i < heap[i])
     {
       bt_debug("dec %d ", i);
       heap[i] = i;
-      HEAP_INCREASE(heap, num, int, MY_CMP, HEAP_SWAP, i);
+      HEAP_INCREASE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
     }
     SHOW_HEAP(heap);
     bt_assert(is_heap_valid(heap, num));
@@ -126,7 +122,7 @@ t_heap_delete(void)
   for(i = 1; i <= num; i++)
   {
     bt_debug("del at pos %d ", i);
-    HEAP_DELETE(heap, num, int, MY_CMP, HEAP_SWAP, i);
+    HEAP_DELETE(heap, num, int, MY_CMP, MY_HEAP_SWAP, i);
     SHOW_HEAP(heap);
     bt_assert(is_heap_valid(heap, num));
   }
index 4e6d9b1e5873153bebc9d3bde59c94902252e26e..670f892a15b4fdca79453fa76ecf28717cf0e2e6 100644 (file)
@@ -6,7 +6,7 @@
  *     Can be freely distributed and used under the terms of the GNU GPL.
  */
 
-#include "birdtest.h"
+#include "test/birdtest.h"
 #include "lib/lists.h"
 
 #define MAX_NUM 1000
@@ -70,7 +70,7 @@ is_empty_list_well_unlinked(void)
 }
 
 static void
-_init_list2(list *l, struct node nodes[])
+init_list_2(list *l, struct node nodes[])
 {
   init_list(l);
 
@@ -83,9 +83,9 @@ _init_list2(list *l, struct node nodes[])
 }
 
 static void
-_init_list(void)
+init_list_(void)
 {
-  _init_list2(&l, (node *) nodes);
+  init_list_2(&l, (node *) nodes);
 }
 
 static int
@@ -93,7 +93,7 @@ t_add_tail(void)
 {
   int i;
 
-  _init_list();
+  init_list_();
   for (i = 0; i < MAX_NUM; i++)
   {
     add_tail(&l, &nodes[i]);
@@ -118,7 +118,7 @@ t_add_head(void)
 {
   int i;
 
-  _init_list();
+  init_list_();
   for (i = MAX_NUM-1; i >= 0; i--)
   {
     add_head(&l, &nodes[i]);
@@ -138,7 +138,7 @@ t_add_head(void)
 }
 
 static void
-_insert_node(node *n, node *after)
+insert_node_(node *n, node *after)
 {
   insert_node(n, after);
   bt_debug(".");
@@ -149,18 +149,18 @@ t_insert_node(void)
 {
   int i;
 
-  _init_list();
+  init_list_();
 
   // add first node
-  _insert_node(&nodes[0], NODE &l.head);
+  insert_node_(&nodes[0], NODE &l.head);
 
   // add odd nodes
   for (i = 2; i < MAX_NUM; i+=2)
-    _insert_node(&nodes[i], &nodes[i-2]);
+    insert_node_(&nodes[i], &nodes[i-2]);
 
   // add even nodes
   for (i = 1; i < MAX_NUM; i+=2)
-    _insert_node(&nodes[i], &nodes[i-1]);
+    insert_node_(&nodes[i], &nodes[i-1]);
 
   bt_debug("\n");
   bt_assert(is_filled_list_well_linked());
@@ -187,7 +187,7 @@ t_remove_node(void)
 {
   int i;
 
-  _init_list();
+  init_list_();
 
   /* Fill & Remove & Check */
   fill_list();
@@ -223,7 +223,7 @@ t_remove_node(void)
 static int
 t_replace_node(void)
 {
-  _init_list();
+  init_list_();
   show_list();
   fill_list();
 
@@ -256,10 +256,10 @@ t_add_tail_list(void)
   node nodes2[MAX_NUM];
   list l2;
 
-  _init_list2(&l, (node *) nodes);
+  init_list_2(&l, (node *) nodes);
   fill_list2(&l, (node *) nodes);
 
-  _init_list2(&l2, (node *) nodes2);
+  init_list_2(&l2, (node *) nodes2);
   fill_list2(&l2, (node *) nodes2);
 
   add_tail_list(&l, &l2);
similarity index 98%
rename from birdtest/birdtest.c
rename to test/birdtest.c
index a5e039f1e02bcf096315d9967d5a98209f0e2688..dc515844d3281ba68029ffa836bed137869de2a0 100644 (file)
@@ -9,11 +9,12 @@
 #include <stdint.h>
 #include <string.h>
 #include <unistd.h>
+#include <signal.h>
 
 #include <sys/resource.h>
 #include <sys/wait.h>
 
-#include "birdtest.h"
+#include "test/birdtest.h"
 
 
 static const char *request;
similarity index 96%
rename from birdtest/birdtest.h
rename to test/birdtest.h
index 6a0bed2488b2ccfc5043a766567f2efc530f7c88..fbfbfec26fc7358053c18182988e9c57b39dbf93 100644 (file)
@@ -13,6 +13,7 @@
 #include <string.h>
 #include <errno.h>
 
+// TODO: add a pseudo random number generator with fixed seed
 
 extern int bt_verbose;
 extern const char *bt_filename;
index c5ebff6bf06d463088f4b28923960bb43d953e48..ff98037f4577c00a161507bd4f825258002bd074 100644 (file)
@@ -3,32 +3,14 @@
 
 objdir=@objdir@
 
-all depend tags install install-docs build-tests:
+all depend tags install install-docs tests:
        $(MAKE) -C $(objdir) $@
 
 docs userdocs progdocs:
        $(MAKE) -C doc $@
 
-check: build-tests
-       @all_tests=( `find . -name '*_test' -executable` )                              ; \
-       all_tests_source=( `find . -name '*_test.c'` )                                  ; \
-       num_build_fail_tests=$$(($${#all_tests_source[@]} - $${#all_tests[@]}))         ; \
-       test_num=1                                                                      ; \
-       num_succ_tests=0                                                                ; \
-       num_fail_tests=0                                                                ; \
-       echo -e "\n== Start all $${#all_tests[@]} unit tests ==\n"                      ; \
-       for test in "$${all_tests[@]}" ; do                                               \
-               echo -e "[$$((test_num++))/$${#all_tests[@]}] $$test"                   ; \
-               ./$$test                                                                  \
-                       && num_succ_tests=$$((num_succ_tests+1))                          \
-                       || num_fail_tests=$$((num_fail_tests+1))                        ; \
-       done                                                                            ; \
-       echo ""                                                                         ; \
-       echo "------------------------------"                                           ; \
-       echo "  Success: $$num_succ_tests"                                              ; \
-       echo "  Failure: $$num_fail_tests"                                              ; \
-       echo "  Build-Failure: $$num_build_fail_tests"                                  ; \
-       echo "------------------------------"
+check: tests
+       tools/run_tests.sh $(objdir) $(srcdir)
 
 clean:
        $(MAKE) -C $(objdir) clean
index dfd61ce2b95968078a07e88415825dba5aebbe23..e3d3fc09a7a83fd88a930eff0c1a6cb1d372cbbb 100644 (file)
@@ -1,22 +1,21 @@
 # Makefile for the BIRD Internet Routing Daemon
 # (c) 1999--2000 Martin Mares <mj@ucw.cz>
 
+root-rel=./
+
 include Rules
 
 .PHONY: all daemon birdc birdcl subdir depend clean distclean tags docs userdocs progdocs
 
 all: sysdep/paths.h .dep-stamp subdir daemon birdcl @CLIENT@
 
-build-tests: birdtest/birdtest.a
+tests: test/birdtest.o
        set -e ; for a in $(dynamic-dirs) ; do $(MAKE) -C $$a $@ ; done
        set -e ; for a in $(static-dirs) $(client-dirs) ; do $(MAKE) -C $$a -f $(srcdir_abs)/$$a/Makefile $@ ; done
 
-birdtest/birdtest.a: birdtest/birdtest.o
-       $(AR) rcs $@ $^
-
-birdtest/birdtest.o: $(srcdir)/birdtest/birdtest.c
-       mkdir -p birdtest
-       $(CC) $(CFLAGS) -I$(srcdir)/birdtest $(TARGET_ARCH) -c $^ $(LDLIBS) -o $@
+test/birdtest.o: $(srcdir)/test/birdtest.c
+       mkdir -p test
+       $(CC) $(CFLAGS) $(TARGET_ARCH) -c $^ $(LDLIBS) -o $@
 
 daemon: $(exedir)/bird
 
index 53371b30451576c5f35b28678fdbab3d8561cec4..1a50dd17066ccfe428bfe34edf30ef74edaf9270 100644 (file)
@@ -41,19 +41,14 @@ sysconfdir=@sysconfdir@
 localstatedir=@localstatedir@
 docdir=@prefix@/doc
 
-test-src-path := $(srcdir)/$(dir-name)/
-
-build-tests:
-       @for i in `find $(test-src-path) -name '*_test.c'` ; do                                            \
-               testname=`basename $$i .c`                                                              ;  \
-               testobj=$${testname}.o                                                                  ;  \
-               printf "$(CC) $(CFLAGS) -I $(srcdir)/birdtest/ -o $$testobj -c $$i && "                 ;  \
-               printf "$(CC) $(LDFLAGS) $$testobj -o $$testname $(root-rel)birdtest/birdtest.a || "    ;  \
-               printf "rm -rf $$testname \n"                                                           ;  \
-               $(CC) $(CFLAGS) -I $(srcdir)/birdtest/ -o $$testobj -c $$i                              && \
-               $(CC) $(LDFLAGS) $$testobj -o $$testname $(root-rel)birdtest/birdtest.a                 || \
-               rm -rf $$testname                                                                       ;  \
-       done
+
+tests_sources := $(wildcard $(srcdir)/$(dir-name)/*_test.c)
+tests_executables := $(notdir $(basename $(tests_sources)))
+
+tests: $(tests_executables)
+
+%_test: $(srcdir)/$(dir-name)/%_test.c
+       $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ $(root-rel)test/birdtest.o
 
 ifdef source
 
diff --git a/tools/run_tests.sh b/tools/run_tests.sh
new file mode 100755 (executable)
index 0000000..d06d341
--- /dev/null
@@ -0,0 +1,31 @@
+#!/bin/sh
+
+objdir=$1
+srcdir=$2
+
+all_tests=$(find $objdir -name '*_test')
+
+num_all_tests=0
+for i in $all_tests; do num_all_tests=$((num_all_tests + 1)); done
+
+num_test=1
+num_succ_tests=0
+num_fail_tests=0
+echo -e "  == Start all $num_all_tests unit tests ==\n"
+for test in $all_tests ; do
+       echo -e "  [$((num_test++))/$num_all_tests] $test"
+       ./$test                                                 \
+               && num_succ_tests=$((num_succ_tests+1))         \
+               || num_fail_tests=$((num_fail_tests+1))
+done
+
+num_all_tests_src=0
+for i in $(find $srcdir -name '*_test.c'); do num_all_tests_src=$((num_all_tests_src + 1)); done
+num_build_fail_tests=$((num_all_tests_src - num_all_tests))
+
+echo ""
+echo "  ------------------------------"
+echo "    Success: $num_succ_tests"
+echo "    Failure: $num_fail_tests"
+echo "    Build-Failure: $num_build_fail_tests"
+echo "  ------------------------------"