pre_testing1/collatz_test.sh
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 27 Nov 2020 00:55:24 +0000
changeset 373 d29cd5883c7b
parent 360 e45d2890749d
child 376 6cc36d0ef79e
permissions -rwxr-xr-x
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
#!/bin/bash
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
# to make the script fail safely
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
set -euo pipefail
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
out=${1:-output}
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
     9
echo -e "" > $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    11
echo -e "Below is the feedback for your submission collatz.scala" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    12
echo -e "" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
# compilation tests
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
function scala_compile {
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
    18
  (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> c$out 1> c$out)
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    19
}
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
# functional tests
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    22
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
function scala_assert {
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
    24
  (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala -nc -i "$1" -- "$2" -e "" 2> /dev/null 1> /dev/null)
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
}
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
# purity test
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    28
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
function scala_vars {
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    30
   (egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|mutable|util.control|new Array' c$out 2> /dev/null 1> /dev/null)
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
}
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    34
### compilation test
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    35
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    36
echo -e "collatz.scala runs?" >> $out
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    37
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    38
if (scala_compile collatz.scala)
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    39
then
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
    40
    echo -e "  --> success" >> $out
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    41
    tsts=$(( 0 ))
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    42
else
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    43
    echo -e "  --> SCALA DID NOT RUN collatz.scala\n" >> $out
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    44
    tsts=$(( 1 )) 
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    45
fi
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    46
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    47
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    48
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
# var, .par return, ListBuffer test
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
#
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    52
if  [ $tsts -eq 0 ]
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    53
then
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    54
   echo -e "collatz.scala does not contain VARS, RETURNS etc?" >> $out
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    55
    
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    56
   if (scala_vars collatz.scala)
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    57
   then
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    58
      echo -e "  --> FAIL (make triple-sure your program conforms to the required format)\n" >> $out
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    59
      tsts=$(( 1 ))
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    60
   else
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
    61
      echo -e "  --> success" >> $out
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    62
      tsts=$(( 0 )) 
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    63
   fi
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    64
fi    
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
### collatz tests
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
if [ $tsts -eq 0 ]
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    71
  echo -e "collatz.scala tests:" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    72
  echo -e "  collatz(1) == 0" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    73
  echo -e "  collatz(6) == 8" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    74
  echo -e "  collatz(9) == 19" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
  if (scala_assert "collatz.scala" "collatz_test1.scala")
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    78
    echo -e "  --> success" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  else
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    80
    echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  fi
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
fi
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    83
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
### collatz-max tests
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
if [ $tsts -eq 0 ]
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    88
  echo -e "  collatz_max(10) == (19, 9)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    89
  echo -e "  collatz_max(100) == (118, 97)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    90
  echo -e "  collatz_max(1000) == (178, 871)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    91
  echo -e "  collatz_max(10000) == (261, 6171)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    92
  echo -e "  collatz_max(100000) == (350, 77031)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    93
  echo -e "  collatz_max(1000000) == (524, 837799)" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
  if (scala_assert "collatz.scala" "collatz_test2.scala") 
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
  then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    97
    echo -e "  --> success" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  else
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    99
    echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  fi
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   101
fi
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   104
### last-odd tests
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   105
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   106
if [ $tsts -eq 0 ]
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   107
then
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   108
  echo -e "  last_odd(113) == 85" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   109
  echo -e "  last_odd(84) == 21" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   110
  echo -e "  last_odd(605) == 341" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   111
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   112
  if (scala_assert "collatz.scala" "collatz_test3.scala") 
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   113
  then
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   114
    echo -e "  --> success" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   115
  else
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   116
    echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   117
  fi
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   118
fi