[lttng-dev] [PATCH babeltrace] Convert the unit tests to the TAP format

Mathieu Desnoyers mathieu.desnoyers at efficios.com
Wed Feb 13 14:47:26 EST 2013


* Jérémie Galarneau (jeremie.galarneau at efficios.com) wrote:
> 
> Signed-off-by: Jérémie Galarneau <jeremie.galarneau at efficios.com>

merged with minor style edits.

Some suggestions for improvements:

# TAP mode:
./test-bitfield -t
1..26
......

# TAP mode for N random attempts:
./test-bitfield -t 100
1..1xx

# Print "value", shifted from "shift" bits, in "len" bits type
./test-bitfield -p value shift len

# Print help:
./test-bitfield
./test-bitfield -h (or --help)

I look forward to see the patch!

Thanks,

Mathieu


> ---
>  tests/lib/Makefile.am     |   2 +
>  tests/lib/test-bitfield.c | 266 ++++++++++++++++++++++------------------------
>  tests/runall.sh           |  88 +++++++--------
>  3 files changed, 165 insertions(+), 191 deletions(-)
> 
> diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am
> index 0613beb..01831de 100644
> --- a/tests/lib/Makefile.am
> +++ b/tests/lib/Makefile.am
> @@ -8,6 +8,8 @@ test_seeks_LDADD = libtestcommon.a \
>  	$(top_builddir)/lib/libbabeltrace.la \
>  	$(top_builddir)/formats/ctf/libbabeltrace-ctf.la
>  
> +test_bitfield_LDADD = libtestcommon.a
> +
>  noinst_PROGRAMS = test-seeks test-bitfield
>  
>  test_seeks_SOURCES = test-seeks.c
> diff --git a/tests/lib/test-bitfield.c b/tests/lib/test-bitfield.c
> index 3bf7568..5d4ecf6 100644
> --- a/tests/lib/test-bitfield.c
> +++ b/tests/lib/test-bitfield.c
> @@ -25,6 +25,8 @@
>  #include <stdlib.h>
>  #include <stdio.h>
>  
> +#include "tap.h"
> +
>  unsigned int glob;
>  
>  /*
> @@ -39,6 +41,10 @@ void fct(void)
>  /* Test array size, in bytes */
>  #define TEST_LEN 128
>  #define NR_TESTS 10
> +#define SIGNED_TEST_DESC_FMT_STR "Writing and reading back 0x%X, signed"
> +#define UNSIGNED_TEST_DESC_FMT_STR "Writing and reading back 0x%X, unsigned"
> +#define DIAG_FMT_STR "Failed reading value written \"%s\"-wise, with start=%i" \
> +	" and length=%i. Read %llX"
>  
>  unsigned int srcrand;
>  
> @@ -116,7 +122,19 @@ do {					\
>  		(c)[i] = (val);		\
>  } while (0)
>  
> -int run_test_unsigned(void)
> +#define check_result(ref, val, buffer, typename, start, len,		\
> +		     desc_fmt_str)					\
> +	({								\
> +	if (val != ref) {						\
> +		fail(desc_fmt_str, ref);				\
> +		diag(DIAG_FMT_STR, #typename, start, len, val);	\
> +		printf("# ");						\
> +		print_byte_array(buffer, TEST_LEN);			\
> +	}								\
> +	val != ref;							\
> +})
> +
> +void run_test_unsigned(void)
>  {
>  	unsigned int src, nrbits;
>  	union {
> @@ -128,9 +146,6 @@ int run_test_unsigned(void)
>  	} target;
>  	unsigned long long readval;
>  	unsigned int s, l;
> -	int err = 0;
> -
> -	printf("Running unsigned test with 0x%X\n", srcrand);
>  
>  	src = srcrand;
>  	nrbits = fls(src);
> @@ -140,62 +155,49 @@ int run_test_unsigned(void)
>  			init_byte_array(target.c, TEST_LEN, 0xFF);
>  			bt_bitfield_write(target.c, unsigned char, s, l, src);
>  			bt_bitfield_read(target.c, unsigned char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (bytewise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, unsigned char,
> +					  s, l, UNSIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  
>  			init_byte_array(target.c, TEST_LEN, 0xFF);
>  			bt_bitfield_write(target.s, unsigned short, s, l, src);
>  			bt_bitfield_read(target.c, unsigned char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (shortwise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, unsigned short,
> +					  s, l, UNSIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  
>  			init_byte_array(target.c, TEST_LEN, 0xFF);
>  			bt_bitfield_write(target.i, unsigned int, s, l, src);
>  			bt_bitfield_read(target.c, unsigned char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (intwise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, unsigned int,
> +					   s, l, UNSIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  
>  			init_byte_array(target.c, TEST_LEN, 0xFF);
>  			bt_bitfield_write(target.l, unsigned long, s, l, src);
>  			bt_bitfield_read(target.c, unsigned char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (longwise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, unsigned long,
> +					  s, l, UNSIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  
>  			init_byte_array(target.c, TEST_LEN, 0xFF);
>  			bt_bitfield_write(target.ll, unsigned long long, s, l, src);
>  			bt_bitfield_read(target.c, unsigned char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (longlongwise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, unsigned long long,
> +				     s, l, UNSIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  		}
>  	}
> -	if (!err)
> -		printf("Success!\n");
> -	else
> -		printf("Failed!\n");
> -	return err;
> +
> +	pass(UNSIGNED_TEST_DESC_FMT_STR, src);
>  }
>  
> -int run_test_signed(void)
> +void run_test_signed(void)
>  {
>  	int src, nrbits;
>  	union {
> @@ -207,9 +209,6 @@ int run_test_signed(void)
>  	} target;
>  	long long readval;
>  	unsigned int s, l;
> -	int err = 0;
> -
> -	printf("Running signed test with 0x%X\n", srcrand);
>  
>  	src = srcrand;
>  	if (src & 0x80000000U)
> @@ -217,155 +216,142 @@ int run_test_signed(void)
>  	else
>  		nrbits = fls(src) + 1;	/* Keep sign at 0 */
>  
> -	for (s = 0; s < 8 * TEST_LEN; s++) {
> -		for (l = nrbits; l < (8 * TEST_LEN) - s; l++) {
> +	for (s = 0; s < CHAR_BIT * TEST_LEN; s++) {
> +		for (l = nrbits; l < (CHAR_BIT * TEST_LEN) - s; l++) {
>  			init_byte_array(target.c, TEST_LEN, 0x0);
>  			bt_bitfield_write(target.c, signed char, s, l, src);
>  			bt_bitfield_read(target.c, signed char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (bytewise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, signed char,
> +					  s, l, SIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  
>  			init_byte_array(target.c, TEST_LEN, 0x0);
>  			bt_bitfield_write(target.s, short, s, l, src);
>  			bt_bitfield_read(target.c, signed char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (shortwise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, short,
> +					  s, l, SIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  
>  			init_byte_array(target.c, TEST_LEN, 0x0);
>  			bt_bitfield_write(target.i, int, s, l, src);
>  			bt_bitfield_read(target.c, signed char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (intwise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, int,
> +					  s, l, SIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  
>  			init_byte_array(target.c, TEST_LEN, 0x0);
>  			bt_bitfield_write(target.l, long, s, l, src);
>  			bt_bitfield_read(target.c, signed char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (longwise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, long,
> +					  s, l, SIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  
>  			init_byte_array(target.c, TEST_LEN, 0x0);
>  			bt_bitfield_write(target.ll, long long, s, l, src);
>  			bt_bitfield_read(target.c, signed char, s, l, &readval);
> -			if (readval != src) {
> -				printf("Error (longlongwise) src %X read %llX shift %d len %d\n",
> -				       src, readval, s, l);
> -				print_byte_array(target.c, TEST_LEN);
> -				err = 1;
> +			if (check_result(src, readval, target.c, long long,
> +					  s, l, SIGNED_TEST_DESC_FMT_STR)) {
> +				return;
>  			}
>  		}
>  	}
> -	if (!err)
> -		printf("Success!\n");
> -	else
> -		printf("Failed!\n");
> -	return err;
> +
> +	pass(SIGNED_TEST_DESC_FMT_STR, src);
>  }
>  
> -int run_test(void)
> +void run_test(void)
>  {
> -	int err = 0;
>  	int i;
> +	plan_tests(NR_TESTS * 2 + 6);
>  
>  	srand(time(NULL));
>  
>  	srcrand = 0;
> -	err |= run_test_unsigned();
> +	run_test_unsigned();
>  	srcrand = 0;
> -	err |= run_test_signed();
> +	run_test_signed();
> +
>  	srcrand = 1;
> -	err |= run_test_unsigned();
> +	run_test_unsigned();
> +
>  	srcrand = ~0U;
> -	err |= run_test_unsigned();
> +	run_test_unsigned();
> +
>  	srcrand = -1;
> -	err |= run_test_signed();
> +	run_test_signed();
> +
>  	srcrand = (int)0x80000000U;
> -	err |= run_test_signed();
> +	run_test_signed();
>  
>  	for (i = 0; i < NR_TESTS; i++) {
>  		srcrand = rand();
> -		err |= run_test_unsigned();
> -		err |= run_test_signed();
> +		run_test_unsigned();
> +		run_test_signed();
>  	}
> -	return err;
>  }
>  
>  int main(int argc, char **argv)
>  {
> -	unsigned long src;
> -	unsigned int shift, len;
> -	int ret;
> -	union {
> -		unsigned char c[8];
> -		unsigned short s[4];
> -		unsigned int i[2];
> -		unsigned long l[2];
> -		unsigned long long ll[1];
> -	} target;
> -	unsigned long long readval;
> +	if (argc > 1) {
> +		/* Run interactive tests */
> +		unsigned long src;
> +		unsigned int shift, len;
> +		union {
> +			unsigned char c[8];
> +			unsigned short s[4];
> +			unsigned int i[2];
> +			unsigned long l[2];
> +			unsigned long long ll[1];
> +		} target;
> +		unsigned long long readval;
>  
> -	if (argc > 1)
>  		src = atoi(argv[1]);
> -	else
> -		src = 0x12345678;
> -	if (argc > 2)
> -		shift = atoi(argv[2]);
> -	else
> -		shift = 12;
> -	if (argc > 3)
> -		len = atoi(argv[3]);
> -	else
> -		len = 40;
> -
> -	target.i[0] = 0xFFFFFFFF;
> -	target.i[1] = 0xFFFFFFFF;
> -	bt_bitfield_write(target.c, unsigned char, shift, len, src);
> -	printf("bytewise\n");
> -	print_byte_array(target.c, 8);
> -
> -	target.i[0] = 0xFFFFFFFF;
> -	target.i[1] = 0xFFFFFFFF;
> -	bt_bitfield_write(target.s, unsigned short, shift, len, src);
> -	printf("shortwise\n");
> -	print_byte_array(target.c, 8);
> -
> -	target.i[0] = 0xFFFFFFFF;
> -	target.i[1] = 0xFFFFFFFF;
> -	bt_bitfield_write(target.i, unsigned int, shift, len, src);
> -	printf("intwise\n");
> -	print_byte_array(target.c, 8);
> -
> -	target.i[0] = 0xFFFFFFFF;
> -	target.i[1] = 0xFFFFFFFF;
> -	bt_bitfield_write(target.l, unsigned long, shift, len, src);
> -	printf("longwise\n");
> -	print_byte_array(target.c, 8);
> -
> -	target.i[0] = 0xFFFFFFFF;
> -	target.i[1] = 0xFFFFFFFF;
> -	bt_bitfield_write(target.ll, unsigned long long, shift, len, src);
> -	printf("lluwise\n");
> -	print_byte_array(target.c, 8);
> -
> -	bt_bitfield_read(target.c, unsigned char, shift, len, &readval);
> -	printf("read: %llX\n", readval);
> -
> -	ret = run_test();
> -
> -	return ret;
> +		if (argc > 2)
> +			shift = atoi(argv[2]);
> +		else
> +			shift = 12;
> +		if (argc > 3)
> +			len = atoi(argv[3]);
> +		else
> +			len = 40;
> +
> +		init_byte_array(target.c, 8, 0xFF);
> +		bt_bitfield_write(target.c, unsigned char, shift, len, src);
> +		printf("bytewise\n");
> +		print_byte_array(target.c, 8);
> +
> +		init_byte_array(target.c, 8, 0xFF);
> +		bt_bitfield_write(target.s, unsigned short, shift, len, src);
> +		printf("shortwise\n");
> +		print_byte_array(target.c, 8);
> +
> +		init_byte_array(target.c, 8, 0xFF);
> +		bt_bitfield_write(target.i, unsigned int, shift, len, src);
> +		printf("intwise\n");
> +		print_byte_array(target.c, 8);
> +
> +		init_byte_array(target.c, 8, 0xFF);
> +		bt_bitfield_write(target.l, unsigned long, shift, len, src);
> +		printf("longwise\n");
> +		print_byte_array(target.c, 8);
> +
> +		init_byte_array(target.c, 8, 0xFF);
> +		bt_bitfield_write(target.ll, unsigned long long, shift, len, src);
> +		printf("lluwise\n");
> +		print_byte_array(target.c, 8);
> +
> +		bt_bitfield_read(target.c, unsigned char, shift, len, &readval);
> +		printf("read: %llX\n", readval);
> +		print_byte_array(target.c, 8);
> +
> +		return 0;
> +	}
> +
> +	/* Run tap-formated tests */
> +	run_test();
> +	return exit_status();
>  }
> diff --git a/tests/runall.sh b/tests/runall.sh
> index 9e0bdca..c10e88a 100755
> --- a/tests/runall.sh
> +++ b/tests/runall.sh
> @@ -5,44 +5,20 @@ DIR=$(readlink -f ${TESTDIR})
>  BABELTRACE_BIN=${DIR}/../converter/babeltrace
>  CTF_TRACES=${DIR}/ctf-traces
>  
> -function print_ok ()
> -{
> -	# Check if we are a terminal
> -	if [ -t 1 ]; then
> -		echo -e "\e[1;32mOK\e[0m"
> -	else
> -		echo -e "OK"
> -	fi
> -}
> -
> -function print_fail ()
> -{
> -	# Check if we are a terminal
> -	if [ -t 1 ]; then
> -		echo -e "\e[1;31mFAIL\e[0m"
> -	else
> -		echo -e "FAIL"
> -	fi
> -}
> -
> -function test_check ()
> +function test_check_success ()
>  {
>  	if [ $? -ne 0 ] ; then
> -		print_fail
>  		return 1
>  	else
> -		print_ok
>  		return 0
>  	fi
>  }
>  
>  function test_check_fail ()
>  {
> -	if [ $? -ne 1 ] ; then
> -		print_fail
> +	if [ $? -eq 0 ] ; then
>  		return 1
>  	else
> -		print_ok
>  		return 0
>  	fi
>  }
> @@ -53,38 +29,48 @@ function run_babeltrace ()
>  	return $?
>  }
>  
> -#run babeltrace expects success
> -echo -e "Running babeltrace without argument..."
> -run_babeltrace
> -test_check
> -if [ $? -ne 0 ]; then
> -	exit 1
> -fi
> -
> -for a in ${CTF_TRACES}/succeed/*; do
> -	echo -e "Running babeltrace for trace ${a}..."
> -	run_babeltrace ${a}
> -	test_check
> -	if [ $? -ne 0 ]; then
> +function print_test_result ()
> +{
> +	if [ $# -ne 3 ] ; then
> +		echo "Invalid arguments provided"
>  		exit 1
>  	fi
> -done
>  
> -#run babeltrace expects failure
> -echo -e "Running babeltrace with bogus argument..."
> +	if [ ${2} -eq 0 ] ; then
> +		echo -n "ok"
> +	else
> +		echo -n "not ok"
> +	fi
> +	echo -e " "${1}" - "${3}
> +}
> +
> +successTraces=(${CTF_TRACES}/succeed/*)
> +failTraces=(${CTF_TRACES}/fail/*)
> +testCount=$((2 + ${#successTraces[@]} + ${#failTraces[@]}))
> +
> +currentTestIndex=1
> +echo -e 1..${testCount}
> +
> +#run babeltrace, expects success
> +run_babeltrace
> +test_check_success
> +print_test_result $((currentTestIndex++)) $? "Running babeltrace without arguments"
> +
> +#run babeltrace with a bogus argument, expects failure
>  run_babeltrace --bogusarg
>  test_check_fail
> -if [ $? -ne 0 ]; then
> -	exit 1
> -fi
> +print_test_result $((currentTestIndex++)) $? "Running babeltrace with a bogus argument"
> +
> +for tracePath in ${successTraces[@]}; do
> +	run_babeltrace ${tracePath}
> +	test_check_success
> +	print_test_result $((currentTestIndex++)) $? "Running babeltrace with trace ${tracePath}"
> +done
>  
> -for a in ${CTF_TRACES}/fail/*; do
> -	echo -e "Running babeltrace for trace ${a}..."
> -	run_babeltrace ${a}
> +for tracePath in ${failTraces[@]}; do
> +	run_babeltrace ${tracePath}
>  	test_check_fail
> -	if [ $? -ne 0 ]; then
> -		exit 1
> -	fi
> +	print_test_result $((currentTestIndex++)) $? "Running babeltrace with trace ${tracePath}"
>  done
>  
>  exit 0
> -- 
> 1.8.1.1
> 
> 
> _______________________________________________
> lttng-dev mailing list
> lttng-dev at lists.lttng.org
> http://lists.lttng.org/cgi-bin/mailman/listinfo/lttng-dev

-- 
Mathieu Desnoyers
EfficiOS Inc.
http://www.efficios.com



More information about the lttng-dev mailing list