From f26cf701529e35803699ec287aea78d9bdde817a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Tvrd=C3=ADk?= Date: Mon, 23 Mar 2015 17:40:13 +0100 Subject: [PATCH] Birdtest: Refactore tests and build system 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 | 26 +++++++++++--------------- lib/lists_test.c | 30 +++++++++++++++--------------- {birdtest => test}/birdtest.c | 3 ++- {birdtest => test}/birdtest.h | 1 + tools/Makefile-top.in | 24 +++--------------------- tools/Makefile.in | 13 ++++++------- tools/Rules.in | 19 +++++++------------ tools/run_tests.sh | 31 +++++++++++++++++++++++++++++++ 8 files changed, 76 insertions(+), 71 deletions(-) rename {birdtest => test}/birdtest.c (98%) rename {birdtest => test}/birdtest.h (96%) create mode 100755 tools/run_tests.sh diff --git a/lib/heap_test.c b/lib/heap_test.c index dc188a62..d86a5985 100644 --- a/lib/heap_test.c +++ b/lib/heap_test.c @@ -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 @@ -14,6 +14,12 @@ #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)); } diff --git a/lib/lists_test.c b/lib/lists_test.c index 4e6d9b1e..670f892a 100644 --- a/lib/lists_test.c +++ b/lib/lists_test.c @@ -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); diff --git a/birdtest/birdtest.c b/test/birdtest.c similarity index 98% rename from birdtest/birdtest.c rename to test/birdtest.c index a5e039f1..dc515844 100644 --- a/birdtest/birdtest.c +++ b/test/birdtest.c @@ -9,11 +9,12 @@ #include #include #include +#include #include #include -#include "birdtest.h" +#include "test/birdtest.h" static const char *request; diff --git a/birdtest/birdtest.h b/test/birdtest.h similarity index 96% rename from birdtest/birdtest.h rename to test/birdtest.h index 6a0bed24..fbfbfec2 100644 --- a/birdtest/birdtest.h +++ b/test/birdtest.h @@ -13,6 +13,7 @@ #include #include +// TODO: add a pseudo random number generator with fixed seed extern int bt_verbose; extern const char *bt_filename; diff --git a/tools/Makefile-top.in b/tools/Makefile-top.in index c5ebff6b..ff98037f 100644 --- a/tools/Makefile-top.in +++ b/tools/Makefile-top.in @@ -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 diff --git a/tools/Makefile.in b/tools/Makefile.in index dfd61ce2..e3d3fc09 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -1,22 +1,21 @@ # Makefile for the BIRD Internet Routing Daemon # (c) 1999--2000 Martin Mares +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 diff --git a/tools/Rules.in b/tools/Rules.in index 53371b30..1a50dd17 100644 --- a/tools/Rules.in +++ b/tools/Rules.in @@ -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 index 00000000..d06d341b --- /dev/null +++ b/tools/run_tests.sh @@ -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 " ------------------------------"