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

Mathieu Desnoyers mathieu.desnoyers at efficios.com
Thu Feb 7 17:34:09 EST 2013


* Jérémie Galarneau (jeremie.galarneau at efficios.com) wrote:
> 
> Signed-off-by: Jérémie Galarneau <jeremie.galarneau at efficios.com>
> ---
>  tests/lib/Makefile.am     |   2 +
>  tests/lib/test-bitfield.c | 242 +++++++++++++++++++---------------------------
>  tests/runall.sh           |  88 +++++++----------
>  3 files changed, 139 insertions(+), 193 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..9b8e737 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;
>  
>  /*
> @@ -130,8 +132,6 @@ int run_test_unsigned(void)
>  	unsigned int s, l;
>  	int err = 0;
>  
> -	printf("Running unsigned test with 0x%X\n", srcrand);
> -
>  	src = srcrand;
>  	nrbits = fls(src);
>  
> @@ -140,58 +140,38 @@ 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);

we seem to be losing a lot of information with this change. Is it
possible to keep this info with the transition to TAP when an error is
detected ?

Thanks,

Mathieu

> -				err = 1;
> -			}
> +			err = readval != src ? 1 : err;
>  
>  			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;
> -			}
> +			err = readval != src ? 1 : err;
>  
>  			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;
> -			}
> +			err = readval != src ? 1 : err;
>  
>  			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;
> -			}
> +			err = readval != src ? 1 : err;
>  
>  			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;
> +			err = readval != src ? 1 : err;
> +
> +			if (err) {
> +				goto end;
>  			}
>  		}
>  	}
> -	if (!err)
> -		printf("Success!\n");
> -	else
> -		printf("Failed!\n");
> +end:
> +	ok(err == 0, "Writing and reading back 0x%X, unsigned", src);
> +	if (err) {
> +		printf("# Failed with start=%i and length=%i\n", s, l);
> +	}
>  	return err;
>  }
>  
> @@ -209,163 +189,141 @@ int run_test_signed(void)
>  	unsigned int s, l;
>  	int err = 0;
>  
> -	printf("Running signed test with 0x%X\n", srcrand);
> -
>  	src = srcrand;
>  	if (src & 0x80000000U)
>  		nrbits = fls(~src) + 1;	/* Find least significant bit conveying sign */
>  	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;
> -			}
> +			err = readval != src ? 1 : err;
>  
>  			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;
> -			}
> +			err = readval != src ? 1 : err;
>  
>  			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;
> -			}
> +			err = readval != src ? 1 : err;
>  
>  			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;
> -			}
> +			err = readval != src ? 1 : err;
>  
>  			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;
> +			err = readval != src ? 1 : err;
> +
> +			if (err) {
> +				goto end;
>  			}
>  		}
>  	}
> -	if (!err)
> -		printf("Success!\n");
> -	else
> -		printf("Failed!\n");
> +end:
> +	ok(err == 0, "Writing and reading back 0x%X, signed", src);
> +	if (err) {
> +		printf("#Failed with start=%i and length=%i\n", s, l);
> +	}
>  	return err;
>  }
>  
> -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