added flame, modified indentation
[xfstests-dev.git] / common.filter
1 ##/bin/sh
2 #
3 # Copyright (c) 2000-2001 Silicon Graphics, Inc.  All Rights Reserved.
4 #
5 # standard filters
6 #
7
8 # Checks that given_value is in range of correct_value +/- tolerance.
9 # Tolerance can be an absolute value or a percentage of the correct value
10 # (see examples with tolerances below).
11 # Outputs suitable message to stdout if it's not in range.
12 #
13 # A verbose option, -v, may be used as the LAST argument
14
15 # e.g. 
16 # foo: 0.0298 = 0.03 +/- 5%
17 # _within_tolerance "foo" 0.0298 0.03 5%  
18
19 # foo: 0.0298 = 0.03 +/- 0.01
20 # _within_tolerance "foo" 0.0298 0.03 0.01
21 #
22 # foo: 0.0298 = 0.03 -0.01 +0.002
23 # _within_tolerance "foo" 0.0298 0.03 0.01 0.002
24 #
25 # foo: verbose output of 0.0298 = 0.03 +/- 5% 
26 # _within_tolerance "foo" 0.0298 0.03 5% -v 
27 _within_tolerance()
28 {
29   _name=$1
30   _given_val=$2
31   _correct_val=$3
32   _mintol=$4
33   _maxtol=$_mintol
34   _verbose=0
35   _debug=false
36
37   # maxtol arg is optional
38   # verbose arg is optional
39   if [ $# -ge 5 ]
40   then 
41      if [ "$5" = "-v" ]
42      then
43         _verbose=1
44      else
45         _maxtol=$5
46      fi
47   fi
48   if [ $# -ge 6 ]
49   then
50      [ "$6" = "-v" ] && _verbose=1
51   fi
52
53   # find min with or without %
54   _mintolerance=`echo $_mintol | sed -e 's/%//'` 
55   if [ $_mintol = $_mintolerance ]
56   then 
57       _min=`echo "scale=5; $_correct_val-$_mintolerance" | bc`
58   else
59       _min=`echo "scale=5; $_correct_val-$_mintolerance*0.01*$_correct_val" | bc`
60   fi
61
62   # find max with or without %
63   _maxtolerance=`echo $_maxtol | sed -e 's/%//'` 
64   if [ $_maxtol = $_maxtolerance ]
65   then 
66       _max=`echo "scale=5; $_correct_val+$_maxtolerance" | bc`
67   else
68       _max=`echo "scale=5; $_correct_val+$_maxtolerance*0.01*$_correct_val" | bc`
69   fi
70
71   $_debug && echo "min = $_min"
72   $_debug && echo "max = $_max"
73
74   cat <<EOF >$tmp.bc.1
75 scale=5;
76 if ($_min <= $_given_val) 1;
77 if ($_min > $_given_val) 0; 
78 EOF
79
80   cat <<EOF >$tmp.bc.2
81 scale=5;
82 if ($_given_val <= $_max) 1;
83 if ($_given_val > $_max) 0;
84 EOF
85
86   _above_min=`bc <$tmp.bc.1`
87   _below_max=`bc <$tmp.bc.2`
88
89   rm -f $tmp.bc.[12]
90
91   _in_range=`expr $_above_min \& $_below_max` 
92
93   # fix up min, max precision for output
94   # can vary for 5.3, 6.2
95   _min=`echo $_min | sed -e 's/0*$//'` # get rid of trailling zeroes
96   _max=`echo $_max | sed -e 's/0*$//'` # get rid of trailling zeroes
97
98   if [ $_in_range -eq 1 ] 
99   then
100         [ $_verbose -eq 1 ] && echo $_name is in range
101         return 0
102   else
103         [ $_verbose -eq 1 ] && echo $_name has value of $_given_val
104         [ $_verbose -eq 1 ] && echo $_name is NOT in range $_min .. $_max       
105         return 1
106   fi
107 }
108
109 # ctime(3) dates
110 #
111 _filter_date()
112 {
113     sed \
114         -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/'
115 }
116
117 # prints filtered output on stdout, values (use eval) on stderr
118
119 _filter_mkfs()
120 {
121     set -
122     perl -ne '
123     if (/^meta-data=([\w,|\/.-]+)\s+isize=(\d+)\s+agcount=(\d+), agsize=(\d+) blks/) {
124         print STDERR "ddev=$1\nisize=$2\nagcount=$3\nagsize=$4\n";
125         print STDOUT "meta-data=DDEV isize=XXX agcount=N, agsize=XXX blks\n";
126     }
127     if (/^data\s+=\s+bsize=(\d+)\s+blocks=(\d+), imaxpct=(\d+)/) {
128         print STDERR "dbsize=$1\ndblocks=$2\nimaxpct=$3\n";
129         print STDOUT "data     = bsize=XXX blocks=XXX, imaxpct=PCT\n";
130     }
131     if (/^\s+=\s+sunit=(\d+)\s+swidth=(\d+) blks, unwritten=(\d)/) {
132         print STDERR "sunit=$1\nswidth=$2\nunwritten=$3\n";
133         print STDOUT "         = sunit=XXX swidth=XXX, unwritten=X\n";
134     }
135     if (/^naming\s+=version\s+(\d+)\s+bsize=(\d+)/) {
136         print STDERR "dirversion=$1\ndirbsize=$2\n";
137         print STDOUT "naming   =VERN bsize=XXX\n";
138     }
139     if (/^log\s+=(internal log|[\w|\/.-]+)\s+bsize=(\d+)\s+blocks=(\d+),\s+version=(\d+)/ ||
140         /^log\s+=(internal log|[\w|\/.-]+)\s+bsize=(\d+)\s+blocks=(\d+)/) {
141         print STDERR "ldev=\"$1\"\nlbsize=$2\nlblocks=$3\nlversion=$4\n";
142         print STDOUT "log      =LDEV bsize=XXX blocks=XXX\n";
143     }
144     if (/^\s+=\s+sectsz=(\d+)\s+sunit=(\d+) blks/) {
145         print STDERR "logsectsz=$1\nlogsunit=$2\n\n";
146     }
147     if (/^realtime\s+=([\w|\/.-]+)\s+extsz=(\d+)\s+blocks=(\d+), rtextents=(\d+)/) {
148         print STDERR "rtdev=$1\nrtextsz=$2\nrtblocks=$3\nrtextents=$4\n";
149         print STDOUT "realtime =RDEV extsz=XXX blocks=XXX, rtextents=XXX\n";
150     }'
151 }
152
153 # make sure this script returns success
154 /bin/true