#!/bin/bash
# O(n^3) time for -insert with lower constant

trap "/usr/bin/killall -q -u $USER ./TSP 2>/dev/null" 0 1 2 3 9 15
trap "/bin/rm -f $STDERR" 0 1 2 3 9 15
if [ ! -x ./TSP ]; then
  echo './TSP is missing or not executable'
  echo './TSP is missing or not executable' 1>&2
  exit 1
fi

if [ -e i_count.txt ]; then
  rm i_count.txt
fi

if compgen -G "cachegrind.out.\*" > /dev/null; then
  rm cachegrind.out.*
fi

for N in 40 80 160; do
  for ON in 0 1; do
    ARGS=`$WHERE/make_arguments.sh $WHERE/4M_airports.dat -insert $N $ON`
    $BIN/run -stdout=stdout.out -stderr=/dev/null /usr/bin/valgrind --tool=cachegrind --trace-children=yes --log-file=valgrind.out ./TSP  $ARGS < /dev/null
    COMPLETE=`grep "I   refs" valgrind.out`
    if [ "$COMPLETE" == "" ]; then
      echo "FAIL: test did not complete"
      exit
    fi

tail -q -n 1 cachegrind.out.* | cut -d' ' -f 2 | sed "s/,//g" | paste -sd+ | bc >> i_count.txt
rm cachegrind.out.*
  done
done
$BIN/big_oh.py -message t093 -polynomial=3 400 40 80 160 < i_count.txt
rm i_count.txt
