core_testing1/collatz_test.sh
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 02 Nov 2023 11:32:10 +0000
changeset 472 6a77c260c8a5
parent 399 b17a98b0c52f
child 474 b528d1d3d3c3
permissions -rw-r--r--
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 {
472
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 399
diff changeset
    18
  (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala-cli compile "$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 {
472
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 399
diff changeset
    24
  (ulimit -t 30; JAVA_OPTS="-Xmx1g" scala-cli -i "$1" "$2" -e "urbanmain()" ) #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
472
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 399
diff changeset
    28
function scala_vars {
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 399
diff changeset
    29
   (sed 's/immutable/ok/g' c$out > cb$out;
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 399
diff changeset
    30
    egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|AtomicInteger|mutable|util.control|new Array' cb$out 2> /dev/null 1> /dev/null)
6a77c260c8a5 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 399
diff changeset
    31
}
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    35
### compilation test
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    36
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    37
echo -e "collatz.scala runs?" >> $out
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    38
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    39
if (scala_compile collatz.scala)
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    40
then
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
    41
    echo -e "  --> success" >> $out
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    42
    tsts=$(( 0 ))
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    43
else
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    44
    echo -e "  --> SCALA DID NOT RUN collatz.scala\n" >> $out
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    45
    tsts=$(( 1 )) 
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    46
fi
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
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    49
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    50
# var, .par return, ListBuffer test
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
#
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    53
if  [ $tsts -eq 0 ]
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    54
then
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    55
   echo -e "collatz.scala does not contain VARS, RETURNS etc?" >> $out
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    56
    
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    57
   if (scala_vars collatz.scala)
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    58
   then
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    59
      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
    60
      tsts=$(( 1 ))
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    61
   else
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
    62
      echo -e "  --> success" >> $out
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    63
      tsts=$(( 0 )) 
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    64
   fi
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    65
fi    
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
### collatz tests
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
if [ $tsts -eq 0 ]
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    72
  echo -e "collatz.scala tests:" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    73
  echo -e "  collatz(1) == 0" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    74
  echo -e "  collatz(6) == 8" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    75
  echo -e "  collatz(9) == 19" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
  if (scala_assert "collatz.scala" "collatz_test1.scala")
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
  then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    79
    echo -e "  --> success" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
  else
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    81
    echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
125
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
fi
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    84
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
### collatz-max tests
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
if [ $tsts -eq 0 ]
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    89
  echo -e "  collatz_max(10) == (19, 9)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    90
  echo -e "  collatz_max(100) == (118, 97)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    91
  echo -e "  collatz_max(1000) == (178, 871)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    92
  echo -e "  collatz_max(10000) == (261, 6171)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    93
  echo -e "  collatz_max(100000) == (350, 77031)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    94
  echo -e "  collatz_max(1000000) == (524, 837799)" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
  if (scala_assert "collatz.scala" "collatz_test2.scala") 
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    98
    echo -e "  --> success" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
  else
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
   100
    echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
125
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
fi
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   103
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   105
### last-odd tests
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   106
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   107
if [ $tsts -eq 0 ]
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   108
then
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   109
  echo -e "  last_odd(113) == 85" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   110
  echo -e "  last_odd(84) == 21" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   111
  echo -e "  last_odd(605) == 341" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   112
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   113
  if (scala_assert "collatz.scala" "collatz_test3.scala") 
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   114
  then
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   115
    echo -e "  --> success" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   116
  else
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   117
    echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   118
  fi
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   119
fi