Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
#!/bin/bash

# check parameters
if [[ "$#" != "2" ]] ; then
  echo "Usage: ./cmpr.sh test.dat [html|text]" >&2
  exit 1
fi

TEST_DAT="$1"
FORMAT="$2"
REFERENCE_DAT="reference.dat"

if ! [ -f ${TEST_DAT} ] || ! [ -s ${TEST_DAT} ] ; then
  echo "ERROR: $0: ${TEST_DAT} doesn't exist or empty." >&2
  exit 1
fi

#----------------------------

top_text () {
  printf "%-15s %5s | %-12s %-12s   %-12s %-12s | %10s %-10s\n" "Routine" "Test"  "Y" "dY"  "Yref" "dYref"  "Pull" "Status"
  echo "-------------------------------------------------------------------------------------------------"
}

top_html () {
cat << EOT
<html>
<head>
  <title>Test results</title>
  <style>
    tr#good {}
    .switchlink {color: #0055CC; cursor: pointer; margin: 4px; border-bottom: 1px dashed #0055CC; }
  </style>
  
  <script language="javascript">
<!--
function turn_good()
{
  var rows = document.getElementsByTagName('tr');
  for (var i = 0; i < rows.length; i++)
  {
    var row = rows[i];

    if (row.id == "good") {
      if (row.style.display != "none")
        row.style.display = "none";
      else
        row.style.display = "table-row";
    }
  }
}
-->
  </script>
</head>
<body>
<h1>Test results</h1>
<p>
Platform: ${PLATFORM}<br>
Test date: $(date)<br>
<hr>
<h4>Notation:</h4>
<table cellpadding=3>
<tr>
  <td align=left> Y , &nbsp; &nbsp; &nbsp; dY </td>
  <td> -- value of an observable and its stat. error  </td>
</tr>
<tr>
  <td align=left> Y<sub>ref</sub> ,  dY<sub>ref</ref> </td> 
  <td> -- reference value of an observable and its stat. error  </td>
</tr>
<tr>
  <td align=left> Pull  </td> 
  <td> -- ( Y - Y<sub>ref</sub> ) / ( dY<sup> 2</sup> + dY<sup> 2</sup><sub>ref</sub> )<sup> 1/2</sup> </td>
</tr>
<tr>
  <td bgcolor="#00ff00" align=center>ok</td> 
  <td> -- test is succesfully compiled and executed with <i>pull</i> &lt; 3 </td>
</tr>
<tr>
  <td bgcolor="#4aa1ff" align=center>badstat</td>
  <td> -- as above, but statistics is insufficient: Y<sub>ref</sub> &lt; 5dY<sub>ref</sub> or Y &lt; 4dY </td>
</tr>
<tr>
  <td bgcolor="#ffaa00" align=center> deviation</td> 
  <td> -- <i>pull</i> &gt; 3 </td>
</tr>
<tr>
  <td bgcolor="#ff0000" align=center> failed </td>
  <td> -- test crashed </td>
</tr>
<tr>
  <td bgcolor="#ff0000" align=center> errors </td>
  <td> -- test failed to compile </td>
</tr>
</table>
<hr>

<p>
  <span class="switchlink" onClick="turn_good()">Show/Hide rows with Status = [OK]</span>
</p>

<table border=1>
<tr>
  <td>Generator</td>
  <td>Test</td>
  <td>Y</td>
  <td>dY</td>
  <td>Y<sub>ref</sub></td>
  <td>dY<sub>ref</sub></td>
  <td>Pull</td>
  <td>Status</td>
</tr>

EOT
}

bottom_html () {
  echo "</table>";
  return;
}

print_text () {
  printf "%-15s %5s | %-12s %-12s   %-12s %-12s | %10s %-10s\n" $*
}

print_html () {
  local g=$(echo $* | cut -d ' ' -f 1)
  local n=$(echo $* | cut -d ' ' -f 2)
  local end=$(echo $* | cut -d ' ' -f 3-)
  
  local rowid="bad"
  if [ "x$end" != "x${end/OK/}" ] ; then
    rowid="good"
  fi
  
  local color
  if   [[ "$end" != "${end/OK/}" ]] ; then
    color="#00ff00"
  elif [[ "$end" != "${end/NO_REFERENCE/}" ]] ; then
    color="#FFD91C"
  elif [[ "$end" != "${end/BADSTAT/}" ]] ; then
    color="#4aa1ff"
  elif [[ "$end" != "${end/DEVIATION/}" ]] ; then
    color="#ffaa00"
  else
    color="#ff0000"
  fi
  
  echo "<tr id=\"$rowid\">"
  echo "  <td> <a href=\"#${g}\"> ${g} </a> </td>"
  echo "  <td align=right> $n </td>"
  
  printf "<td>%s</td> <td>%s</td>   <td>%s</td> <td>%s</td>  <td align=right>%s</td> <td align=center bgcolor=\"$color\">%s</td> </tr>\n" $end
}

process_tests () {
  cc -o chi.exe chi.c -lm >&2
  
  # TODO: check the case of empty $TEST_DAT file = [GENERATOR FAILED!]
  
  {
    # print the list of tested generators
    # [output]:
    #   "generator name"
    
    cat $TEST_DAT | cut -d _ -f 1 | sort | uniq
  } | \
  {
    # print the list of all available tests for
    # generators from the input stream
    # [output]:
    #   "test name" "test #"
    
    while read gen ; do
      cat $TEST_DAT $REFERENCE_DAT | sed 's,!.*$,,' | grep "^${gen}_" | sed 's,  *, ,g' | cut -d ' ' -f 1-2 | sort -u
    done
  } | \
  {
    # print the list of test results for
    # the tests from the input stream
    # [output]:
    #   "test name" "test #" "y" "dy" "y0" "dy0" "chi2" "result"
    
    while read gentest no ; do
      
      output="$gentest $no "

      # get test and reference values
#      echo "DBG:^$gentest $no:" >&2
      ydy=$(cat $TEST_DAT | sed 's,  *, ,g; s,!.*$,,;' | grep -E "^${gentest} ${no} " | cut -d ' ' -f 3-4)
#      echo "DBG ydy:$ydy:">&2
      y0dy0=$(cat $REFERENCE_DAT | sed 's,  *, ,g; s,!.*$,,;' | grep -E "^${gentest} ${no} " | cut -d ' ' -f 3-4)
#      echo "DBG y0dy0:$y0dy0:">&2
      
      if [[ "$ydy" != "" && "$y0dy0" != "" ]] ; then
#	      echo "DBG: --- echo $ydy $y0dy0 | chi --- " >&2
        output1=`echo "$ydy $y0dy0" | ./chi.exe`
	echo "$output $output1"
      else
        if [[ "$y0dy0" == "" ]] ; then
          # reference is missing
          y0dy0="- -"
          result="[NO_REFERENCE]"
        fi
        
        if [[ "$ydy" == "" ]] ; then
          # test is missing
          ydy="- -"
          result="[TEST_FAILED]"
        fi
        
        # NOTE: if both - reference and test is missing
        #       then the test will be marked as FAILED
        
        echo "$output $ydy $y0dy0 - $result"
      fi
    done
  }
  
  rm -f chi.exe >&2
}

# print test dependencies
print_tests_info() {
  # find library dependencies tool
  if which ldd >& /dev/null ; then
    # linux machine:
    LDD="ldd "
  elif which otool >& /dev/null ; then
    # mac machine:
    LDD="otool -L "
  else
    echo "WARNING: can't find ldd routine. Can't get dynamically linked libraries the tests are compiled against." >&2
    LDD="true "
  fi
  
}

#---------------------

if [[ "${FORMAT}" == "text" ]] ; then
  top_text
  process_tests | while read line ; do print_text $line ; done
else
  top_html
  process_tests | while read line ; do print_html $line ; done
  bottom_html
  print_tests_info
fi