You need to sign in or sign up before continuing.
common.filter 4.3 KB
Newer Older
1
#!/bin/bash
2 3 4 5 6 7 8 9 10 11 12 13 14 15
#
# Copyright (C) 2009 Red Hat, Inc.
# Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
16
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22 23 24 25 26 27
#
#
# standard filters
#

# Checks that given_value is in range of correct_value +/- tolerance.
# Tolerance can be an absolute value or a percentage of the correct value
# (see examples with tolerances below).
# Outputs suitable message to stdout if it's not in range.
#
# A verbose option, -v, may be used as the LAST argument
K
Kevin Wolf 已提交
28 29
#
# e.g.
30
# foo: 0.0298 = 0.03 +/- 5%
K
Kevin Wolf 已提交
31 32
# _within_tolerance "foo" 0.0298 0.03 5%
#
33 34 35 36 37 38
# foo: 0.0298 = 0.03 +/- 0.01
# _within_tolerance "foo" 0.0298 0.03 0.01
#
# foo: 0.0298 = 0.03 -0.01 +0.002
# _within_tolerance "foo" 0.0298 0.03 0.01 0.002
#
K
Kevin Wolf 已提交
39 40
# foo: verbose output of 0.0298 = 0.03 +/- 5%
# _within_tolerance "foo" 0.0298 0.03 5% -v
41 42 43 44 45 46 47 48 49 50 51 52 53
_within_tolerance()
{
  _name=$1
  _given_val=$2
  _correct_val=$3
  _mintol=$4
  _maxtol=$_mintol
  _verbose=0
  _debug=false

  # maxtol arg is optional
  # verbose arg is optional
  if [ $# -ge 5 ]
K
Kevin Wolf 已提交
54
  then
55 56
     if [ "$5" = "-v" ]
     then
K
Kevin Wolf 已提交
57
        _verbose=1
58 59 60 61 62 63 64 65 66 67
     else
        _maxtol=$5
     fi
  fi
  if [ $# -ge 6 ]
  then
     [ "$6" = "-v" ] && _verbose=1
  fi

  # find min with or without %
K
Kevin Wolf 已提交
68
  _mintolerance=`echo $_mintol | sed -e 's/%//'`
69
  if [ $_mintol = $_mintolerance ]
K
Kevin Wolf 已提交
70
  then
71 72 73 74 75 76
      _min=`echo "scale=5; $_correct_val-$_mintolerance" | bc`
  else
      _min=`echo "scale=5; $_correct_val-$_mintolerance*0.01*$_correct_val" | bc`
  fi

  # find max with or without %
K
Kevin Wolf 已提交
77
  _maxtolerance=`echo $_maxtol | sed -e 's/%//'`
78
  if [ $_maxtol = $_maxtolerance ]
K
Kevin Wolf 已提交
79
  then
80 81 82 83 84 85 86 87 88 89 90
      _max=`echo "scale=5; $_correct_val+$_maxtolerance" | bc`
  else
      _max=`echo "scale=5; $_correct_val+$_maxtolerance*0.01*$_correct_val" | bc`
  fi

  $_debug && echo "min = $_min"
  $_debug && echo "max = $_max"

  cat <<EOF >$tmp.bc.1
scale=5;
if ($_min <= $_given_val) 1;
K
Kevin Wolf 已提交
91
if ($_min > $_given_val) 0;
92 93 94 95 96 97 98 99 100 101 102 103 104
EOF

  cat <<EOF >$tmp.bc.2
scale=5;
if ($_given_val <= $_max) 1;
if ($_given_val > $_max) 0;
EOF

  _above_min=`bc <$tmp.bc.1`
  _below_max=`bc <$tmp.bc.2`

  rm -f $tmp.bc.[12]

K
Kevin Wolf 已提交
105
  _in_range=`expr $_above_min \& $_below_max`
106 107 108 109 110 111

  # fix up min, max precision for output
  # can vary for 5.3, 6.2
  _min=`echo $_min | sed -e 's/0*$//'` # get rid of trailling zeroes
  _max=`echo $_max | sed -e 's/0*$//'` # get rid of trailling zeroes

K
Kevin Wolf 已提交
112
  if [ $_in_range -eq 1 ]
113
  then
K
Kevin Wolf 已提交
114 115
        [ $_verbose -eq 1 ] && echo $_name is in range
        return 0
116
  else
K
Kevin Wolf 已提交
117 118 119
        [ $_verbose -eq 1 ] && echo $_name has value of $_given_val
        [ $_verbose -eq 1 ] && echo $_name is NOT in range $_min .. $_max
        return 1
120 121 122 123 124 125 126 127
  fi
}

# ctime(3) dates
#
_filter_date()
{
    sed \
K
Kevin Wolf 已提交
128
        -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] [0-9][0-9][0-9][0-9]$/DATE/'
129 130
}

131
# replace occurrences of the actual TEST_DIR value with TEST_DIR
132 133 134 135 136
_filter_testdir()
{
    sed -e "s#$TEST_DIR#TEST_DIR#g"
}

137 138 139 140 141 142
# replace occurrences of the actual IMGFMT value with IMGFMT
_filter_imgfmt()
{
    sed -e "s#$IMGFMT#IMGFMT#g"
}

143 144 145 146 147 148
# Removes \r from messages
_filter_win32()
{
    sed -e 's/\r//g'
}

149 150 151
# sanitize qemu-io output
_filter_qemu_io()
{
152 153
    _filter_win32 | sed -e "s/[0-9]* ops\; [0-9/:. sec]* ([0-9/.inf]* [EPTGMKiBbytes]*\/sec and [0-9/.inf]* ops\/sec)/X ops\; XX:XX:XX.X (XXX YYY\/sec and XXX ops\/sec)/" \
        -e "s/qemu-io> //g"
154 155
}

156 157 158
# replace occurrences of QEMU_PROG with "qemu"
_filter_qemu()
{
159
    sed -e "s#\\(^\\|(qemu) \\)$(basename $QEMU_PROG):#\1QEMU_PROG:#" \
160 161
        -e 's#^QEMU [0-9]\+\.[0-9]\+\.[0-9]\+ monitor#QEMU X.Y.Z monitor#' \
        -e $'s#\r##' # QEMU monitor uses \r\n line endings
162 163
}

164 165 166 167 168 169 170 171
# replace problematic QMP output like timestamps
_filter_qmp()
{
    _filter_win32 | \
    sed -e 's#\("\(micro\)\?seconds": \)[0-9]\+#\1 TIMESTAMP#g' \
        -e 's#^{"QMP":.*}$#QMP_VERSION#'
}

172 173
# make sure this script returns success
/bin/true