#!/bin/bash

############################################################################
#  misc/run-all-tests
#
#  Part of the STXXL. See http://stxxl.sourceforge.net
#
#  Copyright (C) 2008, 2009 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
#
#  Distributed under the Boost Software License, Version 1.0.
#  (See accompanying file LICENSE_1_0.txt or copy at
#  http://www.boost.org/LICENSE_1_0.txt)
############################################################################

# Usage: $0 [stxxl|mcstxxl|pmstxxl] [yes|no (valgrind)] [yes|no (leakreport=full)]
# Environment: STXXL_TMPDIR (default: .) for location of generated input/output files


ulimit -t 7200
set -o pipefail

stxxl=stxxl
test -n "$1" && stxxl="$1"
valgrind=yes
test -n "$2" && valgrind="$2"
leakreport=""
if [ "$3" = "yes" ]; then
	leakreport="--leak-check=full --show-reachable=yes"
fi

suppressions=""
for supp in `dirname $0`/*.supp
do
	suppressions="$suppressions --suppressions=$supp"
done


testresults=testresults
mkdir -p $testresults

AMI_SINGLE_DEVICE=/tmp/TPIE/
export AMI_SINGLE_DEVICE

if ! [ -n "$STXXL_TMPDIR" -a -d "$STXXL_TMPDIR" -a -w "$STXXL_TMPDIR" ]; then
	STXXL_TMPDIR="."
fi
echo "Using STXXL_TMPDIR='$STXXL_TMPDIR'"

i=0
run()
{
	echo "==================== $stxxl valgrind=$valgrind $i: $1 ===================="
	cmd="$1.$stxxl.bin"
	bn=`basename $1`
	shift
	i=$(($i + 1))
	pfx="`printf '%s/test.%s.%05d.%04d.%s' $testresults $stxxl $$ $i $bn`"
	rm -f stxxl.log stxxl.errlog
	test -x "$cmd" || return 255
	echo "+ $cmd $@" | tee "$pfx.log"
	echo "[`date`]" | tee -a "$pfx.log"
	if [ "$valgrind" = "yes" ]; then
		valgrind --track-fds=no --log-file="$pfx.valgrind" --freelist-vol=2147483648 --max-stackframe=8388608 $suppressions $leakreport \
		$cmd "$@" < /dev/null 2>&1 | tee -a "$pfx.log"
		result=$?
	else
		$cmd "$@" < /dev/null 2>&1 | tee -a "$pfx.log"
		result=$?
	fi
	echo "[`date`]" | tee -a "$pfx.log"
	if [ -n "$XFAIL" ]; then
		if [ "$result" = 0 ]; then
			mv "$pfx.log" "$pfx.FAIL.xfail.$result"
		else
			mv "$pfx.log" "$pfx.xfail.$result"
		fi
	else
		test "$result" = 0 || mv "$pfx.log" "$pfx.FAIL.$result"
	fi
	test -s stxxl.errlog && mv stxxl.errlog "$pfx.ERR"
}

if false; then
:
fi

run common/stxxl_info
XFAIL=1 run common/stxxl_info foo # returns -1 intentionally
XFAIL=1 run common/stxxl_info foo bar # returns !0 if assertions are enabled
run common/test_random
run common/test_manyunits
run common/test_globals

run io/gen_file "$STXXL_TMPDIR/in"
run utils/createdisks 1024 "$STXXL_TMPDIR/testdiskx" "$STXXL_TMPDIR/testdisky"
run utils/createdisks 1000 "$STXXL_TMPDIR/testdiskx" "$STXXL_TMPDIR/testdisky"
run utils/log2
run utils/malloc 1024
run utils/malloc 1024000
run utils/malloc 4000000000
run utils/pq_param
#run utils/mlock 42  # does not terminate intentionally

run io/benchmark_disks 0 2 256 "$STXXL_TMPDIR/testdiskx" "$STXXL_TMPDIR/testdisky"
run io/flushbuffers 2 "$STXXL_TMPDIR/testdiskx" "$STXXL_TMPDIR/testdisky"
run io/benchmark_disk_and_flash 0 2 "$STXXL_TMPDIR/testdiskx" "$STXXL_TMPDIR/testdisky"
run io/sd_test
run io/test_io "$STXXL_TMPDIR"
run io/test_cancel syscall "$STXXL_TMPDIR/testdiskx"
run io/test_cancel fileperblock_syscall "$STXXL_TMPDIR/testdiskx"
run io/test_cancel mmap "$STXXL_TMPDIR/testdiskx"
run io/test_cancel fileperblock_mmap "$STXXL_TMPDIR/testdiskx"
run io/test_cancel simdisk "/tmp/testramdisk"
rm -f "/tmp/testramdisk"
run io/test_cancel boostfd "$STXXL_TMPDIR/testdiskx"
run io/test_cancel fileperblock_boostfd "$STXXL_TMPDIR/testdiskx"
run io/test_cancel memory "$STXXL_TMPDIR/testdiskx"
run io/test_cancel wbtl "$STXXL_TMPDIR/testdiskx"
run io/test_io_sizes syscall "$STXXL_TMPDIR/out" 5368709120
run io/test_io_sizes mmap "$STXXL_TMPDIR/out" 5368709120
run io/test_io_sizes boostfd "$STXXL_TMPDIR/out" 5368709120
#run io/benchmark_configured_disks 2 128  # our disks are usually set to RAM
run io/benchmark_random_block_access 2048 1024 1000000 i
run io/benchmark_random_block_access 2048 128 1000000 r
run io/benchmark_random_block_access 2048 128 1000000 w
run io/verify_disks 123456 0 2 w "$STXXL_TMPDIR/out"
run io/verify_disks 123456 0 2 r "$STXXL_TMPDIR/out"
run io/iobench_scatter_in_place 100 10 4096 "$STXXL_TMPDIR/out"  # consumes out

run mng/test_block_alloc_strategy
run mng/test_buf_streams
run mng/test_mng
run mng/test_mng1
run mng/test_prefetch_pool
run mng/test_write_pool
run mng/test_pool_pair
run mng/test_read_write_pool
run mng/test_mng_recursive_alloc
run mng/test_aligned

run containers/bench_pqueue
run containers/copy_file "$STXXL_TMPDIR/in" "$STXXL_TMPDIR/out"
run containers/monotonic_pq 1000 1
run containers/pq_benchmark 1 1000000
run containers/pq_benchmark 2 1000000
run containers/stack_benchmark 1 1073741824
run containers/stack_benchmark 2 1073741824
run containers/stack_benchmark 3 1073741824
run containers/stack_benchmark 4 1073741824
run containers/test_deque 33333333
run containers/test_ext_merger
run containers/test_ext_merger2
run containers/test_iterators
run containers/test_many_stacks 42
run containers/test_migr_stack
run containers/test_pqueue
run containers/test_queue
run containers/test_stack 1024
run containers/test_vector
run containers/test_vector_export
run containers/write_vector "$STXXL_TMPDIR/in" "$STXXL_TMPDIR/out"
run containers/write_vector2 "$STXXL_TMPDIR/in" "$STXXL_TMPDIR/out"
run containers/test_vector_sizes "$STXXL_TMPDIR/out" syscall
run containers/test_vector_sizes "$STXXL_TMPDIR/out" mmap
run containers/test_vector_sizes "$STXXL_TMPDIR/out" boostfd
run containers/benchmark_naive_matrix
run containers/test_map 32
run containers/test_map_random 2000
run containers/btree/test_btree 10000
run containers/btree/test_btree 100000
run containers/btree/test_btree 1000000
run containers/btree/test_const_scan 10000
run containers/btree/test_const_scan 100000
run containers/btree/test_const_scan 1000000
run containers/btree/test_corr_insert_erase 14
run containers/btree/test_corr_insert_find 14
run containers/btree/test_corr_insert_scan 14

run algo/sort_file generate "$STXXL_TMPDIR/sort_file.dat"
run algo/sort_file sort "$STXXL_TMPDIR/sort_file.dat"
run algo/sort_file generate "$STXXL_TMPDIR/sort_file.dat"
run algo/sort_file ksort "$STXXL_TMPDIR/sort_file.dat"
run algo/sort_file generate "$STXXL_TMPDIR/sort_file.dat"
run algo/sort_file stable_sort "$STXXL_TMPDIR/sort_file.dat"
run algo/sort_file generate "$STXXL_TMPDIR/sort_file.dat"
run algo/copy_and_sort_file "$STXXL_TMPDIR/sort_file.dat" "$STXXL_TMPDIR/sorted_file.dat"
test -n "$WITH_STABLE_KSORT" && run algo/sort_file generate "$STXXL_TMPDIR/sort_file.dat"
test -n "$WITH_STABLE_KSORT" && run algo/sort_file stable_ksort "$STXXL_TMPDIR/sort_file.dat"
run algo/test_asch 3 100 1000 42
run algo/test_ksort
run algo/test_random_shuffle
run algo/test_scan
run algo/test_sort
run algo/test_bad_cmp
test -n "$WITH_STABLE_KSORT" && run algo/test_stable_ksort

run stream/test_materialize
run stream/test_push_sort
run stream/test_sorted_runs
run stream/test_stream
run stream/test_stream1
run stream/test_transpose
run stream/test_loop 100 -v
run stream/test_loop 1000000

run io/unittest
run mng/unittest

rm -rf $AMI_SINGLE_DEVICE
mkdir $AMI_SINGLE_DEVICE

# for the tpie and bdb tests valgrind reports lots of
# new/delete mismatches and off-by-one errors
# due to bad partial tpie new/delete replacements

run containers/tpie_stack_benchmark 1 2147483648
run containers/tpie_stack_benchmark 2 2147483648

run containers/berkeley_db_benchmark 1 100
run containers/berkeley_db_benchmark 2 10000000
run containers/berkeley_db_benchmark 3 100
run containers/berkeley_db_benchmark 4 100
run containers/berkeley_db_benchmark 5 10000000

rm -rf $AMI_SINGLE_DEVICE
rm -f /var/tmp/bdb_file

run containers/leda_sm_pq_benchmark 1 1000000
run containers/leda_sm_pq_benchmark 2 1000000
run containers/leda_sm_stack_benchmark 1 1073741824
run containers/leda_sm_stack_benchmark 2 1073741824

#algo/test_sort_all_parameters		<MiB to sort> <MiB to use> <alloc_strategy [0..3]> <blk_size [0..14]> <seed>
#algo/test_ksort_all_parameters		<MiB to sort> <MiB to use> <alloc_strategy [0..3]> <blk_size [0..14]> <seed>
#algo/test_stable_ksort_all_parameters	<MiB to sort> <MiB to use> <alloc_strategy [0..3]> <blk_size [0..14]> <seed>
for data in 1024 ; do
for ram in 384 ; do
for strategy in 0 1 2 3 ; do
for blk_size in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ; do
run algo/test_sort_all_parameters		$data $ram $strategy $blk_size 42
run algo/test_ksort_all_parameters		$data $ram $strategy $blk_size 42
test -n "$WITH_STABLE_KSORT" && \
    run algo/test_stable_ksort_all_parameters	$data $ram $strategy $blk_size 42
done
done
done
done

#algo/test_parallel_sort [n in MiB] [p threads] [M in MiB] [sorting algorithm: m | q | qb | s] [merging algorithm: p | s | n]
for data in 768 ; do
for ram in 128 ; do
for threads in 1 2 3 4 ; do
for sort in m q qb s ; do
for merge in p s n ; do
run algo/test_parallel_sort $data $threads $ram $sort $merge
done
done
done
done
done

exit 0
