core_testing1/collatz_test.sh
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 02 Nov 2023 13:53:37 +0000
changeset 474 b528d1d3d3c3
parent 472 6a77c260c8a5
child 477 a4e1f63157d8
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 {
474
b528d1d3d3c3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 472
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 {
474
b528d1d3d3c3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 472
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
474
b528d1d3d3c3 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 472
diff changeset
    45
    echo -e "  --> try running scala-cli compile collatz.scala on your own computer\n" >> $out
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    46
    tsts=$(( 1 )) 
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    47
fi
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
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    50
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
# var, .par return, ListBuffer test
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
#
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    54
if  [ $tsts -eq 0 ]
356
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    55
then
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    56
   echo -e "collatz.scala does not contain VARS, RETURNS etc?" >> $out
d1046d9d3213 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 352
diff changeset
    57
    
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    58
   if (scala_vars collatz.scala)
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    59
   then
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    60
      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
    61
      tsts=$(( 1 ))
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    62
   else
360
e45d2890749d updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 356
diff changeset
    63
      echo -e "  --> success" >> $out
346
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    64
      tsts=$(( 0 )) 
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    65
   fi
663c2a9108d1 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 345
diff changeset
    66
fi    
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    69
### collatz tests
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
if [ $tsts -eq 0 ]
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    73
  echo -e "collatz.scala tests:" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    74
  echo -e "  collatz(1) == 0" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    75
  echo -e "  collatz(6) == 8" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    76
  echo -e "  collatz(9) == 19" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    77
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
  if (scala_assert "collatz.scala" "collatz_test1.scala")
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
  then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    80
    echo -e "  --> success" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
  else
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    82
    echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
125
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
fi
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    85
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    86
### collatz-max tests
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
if [ $tsts -eq 0 ]
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    90
  echo -e "  collatz_max(10) == (19, 9)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    91
  echo -e "  collatz_max(100) == (118, 97)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    92
  echo -e "  collatz_max(1000) == (178, 871)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    93
  echo -e "  collatz_max(10000) == (261, 6171)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    94
  echo -e "  collatz_max(100000) == (350, 77031)" >> $out
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    95
  echo -e "  collatz_max(1000000) == (524, 837799)" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
  if (scala_assert "collatz.scala" "collatz_test2.scala") 
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
  then
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
    99
    echo -e "  --> success" >> $out
125
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
  else
302
067afe8af35b updated
Christian Urban <urbanc@in.tum.de>
parents: 281
diff changeset
   101
    echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
125
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
fi
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   104
dcaab8068baa updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   105
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   106
### last-odd tests
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   107
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   108
if [ $tsts -eq 0 ]
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   109
then
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   110
  echo -e "  last_odd(113) == 85" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   111
  echo -e "  last_odd(84) == 21" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   112
  echo -e "  last_odd(605) == 341" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   113
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   114
  if (scala_assert "collatz.scala" "collatz_test3.scala") 
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   115
  then
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   116
    echo -e "  --> success" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   117
  else
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   118
    echo -e "  --> ONE OF THE TESTS FAILED\n" >> $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   119
  fi
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 329
diff changeset
   120
fi