#!/bin/sh
# PCP QA Test No. 1066
#
# The Battle of Hastings easter egg!
#
# check memleaks with derived metrics on the pmFetch path with
# context churning ... so build_expr() gets hammered.
#
# Aim to exercise _every_ derived metric function and operator with
# simple and complex expressions drawn from all the other derived
# metric QA tests.

# Copyright (c) 2017 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

_check_valgrind

_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
}

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

# real QA test starts here
cat <<End-of-File >$tmp.config
qa$seq.m03 = 42
qa$seq.m04 = 42.001
qa$seq.m05 = sample.ulong.hundred
qa$seq.m06 = sample.long.hundred
qa$seq.m07 = sample.ulonglong.hundred
qa$seq.m08 = sample.longlong.hundred
qa$seq.m09 = sample.float.hundred
qa$seq.m10 = sample.double.hundred
qa$seq.m11 = sample.string.hullo
qa$seq.m12 = 10 * sample.ulonglong.ten + sample.ulonglong.million / sample.ulonglong.hundred
qa$seq.m13 = sample.long.one + sample.long.ten + sample.long.hundred + sample.long.million - sample.longlong.one - sample.longlong.ten - sample.longlong.hundred - sample.longlong.million
qa$seq.m14 = sample.bin + 100 - sample.bin - 100
qa$seq.m15 = sample.bin + sample.bucket*2 - (sample.bin + 100) - sample.bucket
qa$seq.m16 = sample.load - sample.load + sample.load / sample.load * sample.load - sample.load
qa$seq.m17 = sample.daemon_pid - sample.daemon_pid + sample.daemon_pid / sample.daemon_pid * sample.daemon_pid - sample.daemon_pid
qa$seq.m18 = sample.longlong.hundred - sample.longlong.hundred + sample.longlong.hundred / sample.longlong.hundred * sample.longlong.hundred - sample.longlong.hundred
qa$seq.m19 = sample.ulonglong.hundred - sample.ulonglong.hundred + sample.ulonglong.hundred / sample.ulonglong.hundred * sample.ulonglong.hundred - sample.ulonglong.hundred
qa$seq.m20 = sample.float.hundred - sample.float.hundred + sample.float.hundred / sample.float.hundred * sample.float.hundred - sample.float.hundred
qa$seq.m21 = sample.double.hundred - sample.double.hundred + sample.double.hundred / sample.double.hundred * sample.double.hundred - sample.double.hundred
qa$seq.m22 = delta(sample.longlong.million)
qa$seq.m23 = delta(sampledso.recv_pdu)
qa$seq.m24 = 3 * delta(sample.const_rate.value) / delta(sample.const_rate.value) - delta(sampledso.const_rate.value) / delta(sample.const_rate.value)
qa$seq.m25 = count(sample.bin)*(min(sample.bin) + max(sample.bin))/2 / sum(sample.bin)
qa$seq.m26 = 4499 < mkconst(2, units="count^-1")*avg(sample.bin)*count(sample.bin) - sum(sample.bin) && mkconst(2, units="count^-1")*avg(sample.bin)*count(sample.bin) - sum(sample.bin) < 4501
qa$seq.m27 = rate(sampledso.pdu) > 0
qa$seq.m28 = instant(sample.long.bin_ctr)
qa$seq.m29 = (sample.long.hundred <= 100) + (sample.long.hundred >= 100) - (sample.long.hundred == 100)
qa$seq.m30 = 1 && sample.long.one
qa$seq.m31 = sample.double.one && 0
qa$seq.m32 = sample.long.hundred < 100 || sample.long.hundred > 100 || sample.long.hundred != 100 || sample.long.hundred == 100
qa$seq.m33 = sample.long.ten + (sample.long.ten >= sample.long.ten) + sample.long.ten
qa$seq.m34 = -12345
qa$seq.m35 = -sample.dupnames.three.bin + sample.bin
qa$seq.m36 = sample.long.one == 1 ? sample.ulonglong.ten : sample.ulonglong.hundred
qa$seq.m37 = sample.bin > 400 ? sample.bin-sample.bin : sample.bin
qa$seq.m38 = sample.bin!=100 && sample.bin!=900 && sample.bin!=500 ? sample.long.ten : sample.bin
qa$seq.m39 = mkconst(20, type=u64, semantics=counter, units=ms)
qa$seq.m40 = ! sample.bin >= 700 || ! sample.bin <= 300
qa$seq.m41 = rescale(sample.byte_rate, "Kbyte/min") <= 60
qa$seq.m42 = rescale(mkconst(36000, units="count/hour")+rescale(mkconst(600, units="count/min"),"count/hour"), "count/sec")
qa$seq.m43 = defined(sample.long.million)
qa$seq.m44 = defined(sample.long.not_million)
qa$seq.m45 = ! defined(sample.long.ten) ? 0 : defined(sample.ulong.ten) + 2*defined(sample.ulong.hundred) + 3*defined(sample.ulong.million)
qa$seq.m46 = ! defined(sample.long.bozo) ? defined(sample.ulong.ten) + 2*defined(sample.ulong.hundred) + 3*defined(sample.ulong.million) : 0
#qa$seq.m47
#qa$seq.m48
#qa$seq.m49

#qa$seq.m99 
End-of-File

export PCP_DERIVED_CONFIG=$tmp.config

# DEBUG=-Dderive,appl1
DEBUG=
_run_valgrind src/churnctx -v -t 0 -s 5 -f 2 $DEBUG qa$seq

# success, all done
status=0
exit
