#!/bin/sh
#
# Check -S option.
#
# Copyright (c) 2016-2019 The strace developers.
# All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-or-later

. "./init.sh"

run_prog ../readv > /dev/null

test_c()
{
	local sortby sortopts sedexpr
	sortby="$1"; shift
	sortopts="$1"; shift
	sedexpr="$1"; shift

	run_strace -c -w ${sortby:+-S} $sortby ../readv > /dev/null
	sed -r -n -e "$sedexpr" < "$LOG" > "$OUT"

	[ -s "$OUT" ] ||
		fail_ "$STRACE $args output mismatch"
	LC_ALL=C sort -c $sortopts "$OUT" || {
		echo 'Actual output:'
		cat < "$LOG"
		fail_ "$STRACE $args output not sorted properly"
	}
}

c='[[:space:]]+([^[:space:]]+)'
for s in '' time time_total total_time; do
	test_c "$s" '-n -r' \
		's/^[[:space:]]+([0-9]+)[.,]([0-9]+)[[:space:]].*/\1\t\2/p'
done
for s in calls count; do
	test_c "$s" '-n -r' \
		'/^[[:space:]]+[0-9]/ s/^'"$c$c$c$c"'[[:space:]].*/\4/p'
done
for s in name syscall syscall_name; do
	test_c "$s" '' \
		'/^[[:space:]]+[0-9]/ s/^'"$c$c$c$c"'([[:space:]]+[0-9]+)?'"$c"'$/\6/p'
done
for s in error errors; do
	test_c "$s" '-n -r' \
		'/^[[:space:]]+[0-9]/ s/^'"$c$c$c$c"'([[:space:]]+([0-9]+))?'"$c"'$/\6/p'
done
