#!/bin/sh
# PCP QA Test No. 849
# pmie extensions for string-valued expressions
#
# Copyright (c) 2016 Ken McDonell.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

status=1	# failure is the default!
$sudo rm -rf $tmp $tmp.* $seq.full
trap "cd $here; rm -rf $tmp $tmp.*; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/DATE/' \
	-e 's/DATE [12][0-9][0-9][0-9]/DATE/' \
	-e '/evaluator exiting/s/pmie([0-9][0-9]*)/pmie(PID)/' \
	-e '/Performance Co-Pilot Inference Engine/s/Version .*/Version .../'
}

# mapping to 0x0 -> (nil) needed for C compilers that don't do the
# (nil) translation automatically for a printf %p
#
_filter2()
{
    sed \
	-e 's/0x0\([ (]\)/(nil)\1/g' \
	-e 's/0x0$/(nil)/' \
	-e "s@$tmp@TMP@g" \
    | $PCP_AWK_PROG '
$1 == "pmie:"		{ print; next }
/failed: /		{ print; next }
$1 == "bundle:"	{ skip=1 }
skip == 1		{ if ($1 == "Rule:") skip = 0 }
skip == 1		{ next }
			{ print }' >$tmp.tmp
    tr ' =' '\012' <$tmp.tmp \
    | grep '0x' \
    | $PCP_AWK_PROG >$tmp.sed '
BEGIN	{ n = 0 }
    	{ if (seen[$1] == 1) next
	  printf "s/%s/ADDR%03d/\n",$1,n
	  n++
	  seen[$1] = 1
	}'
    sed -f $tmp.sed <$tmp.tmp
}

# real QA test starts here

cat <<'End-of-File' >$tmp.pmie.00
//args -Dappl0,appl1 -t 2sec -T 1sec
//expect parser syntax error
sample.long.ten == "foo" -> print "blah";
End-of-File

cat <<'End-of-File' >$tmp.pmie.01
//args -v -t 0.5sec -T 1.2sec
//expect 3 samples x 5 metric values to be reported
sample.long.million;
sample.string.hullo;
sample.bin;
sample.string.bin;
sample.string.bin #'bin-400'
End-of-File

cat <<'End-of-File' >$tmp.pmie.02
//args -v -t 0.5sec -T 0.2sec
//expect all true values to be reported
"hullo world!" == sample.string.hullo;
sample.string.hullo == "hullo world!";
sample.string.hullo == sample.string.hullo;
sample.string.hullo != sample.string.bin #'bin-100';
sample.string.bin == sample.string.bin;
End-of-File

cat <<'End-of-File' >$tmp.pmie.03
//args -t 0.5sec -T 0.2sec
//expect several OK #N msgs
some_inst sample.string.bin == "300" -> print "OK #1 @ inst:" " %i";
some_inst "900" == sample.string.bin -> print "OK #2 @ inst:" " %i";
some_inst sample.string.bin != "300" -> print "OK #3 @ inst:" " %i";
some_inst "100" != sample.string.bin -> print "OK #4 @ inst:" " %i";
ruleset
    all_inst sample.string.bin != "42" -> print "OK #5 @ inst:" " %i"
otherwise
    -> print "BAD #5";
ruleset
    all_inst "500" != sample.string.bin -> print "BAD #6 @ inst:" " %i"
otherwise
    -> print "OK #6"
End-of-File

for x in $tmp.pmie.*
do
    echo | tee -a $seq.full
    echo "Test: `echo $x | sed -e "s;$tmp.pmie.;;"` [expect: `sed -n -e '/^\/\/expect /s///p' <$x`]" | tee -a $seq.full
    cat $x | tee -a $seq.full >>$seq.full
    pmie -c $x `sed -n -e '/^\/\/args /s///p' <$x` >$tmp.out 2>$tmp.err
    echo "=== pmie output ===" >>$seq.full
    cat $tmp.out >>$seq.full
    echo >>$seq.full
    echo "=== pmie errors ===" >>$seq.full
    cat $tmp.err >>$seq.full

    _filter2 <$tmp.err | _filter
    _filter <$tmp.out
done

# success, all done
status=0

exit
