marking1/collatz_test.sh
author Christian Urban <christian.urban@kcl.ac.uk>
Sat, 11 Mar 2023 23:09:51 +0000
changeset 465 8ce150207792
parent 343 c8fcc0e0a57f
permissions -rwxr-xr-x
updated to scala 3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
#!/bin/bash
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
# to make the script fail safely
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
set -euo pipefail
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
out=${1:-output}
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     9
echo "" > $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    10
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    11
echo `date` >> $out
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    12
echo >> $out
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
echo "Below is the feedback and provisional marks for your submission" >> $out
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 267
diff changeset
    14
echo "for the Preliminary Part of Assignment 6.  Please note all marks are provisional until" >> $out
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    15
echo "ratified by the assessment board -- this is not an official" >> $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    16
echo "results transcript." >> $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
echo "" >> $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    18
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    19
echo "The feedback for your submission for collatz.scala" >> $out
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    20
echo "" >> $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    21
267
9e0216756771 updated myassert workaround
Christian Urban <urbanc@in.tum.de>
parents: 266
diff changeset
    22
# marks for CW6 basic part 
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    23
marks=$(( 0 ))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    24
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    25
# compilation tests
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    27
function scala_compile {
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    28
  (ulimit -t 60; JAVA_OPTS="-Xmx1g" scala "$1" 2> /dev/null 1> /dev/null)
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
}
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    31
# functional tests
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    32
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    33
function scala_assert {
281
87b9e3e2c1a7 updated
Christian Urban <urbanc@in.tum.de>
parents: 267
diff changeset
    34
  (ulimit -t 60; JAVA_OPTS="-Xmx1g" scala -i "$1" -- "$2" 2> /dev/null 1> /dev/null)
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    35
}
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    36
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    37
# purity test
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    38
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    39
function scala_vars {
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    40
   (egrep '\bvar\b|\breturn\b|\.par|ListBuffer|mutable|new Array' "$1" 2> /dev/null 1> /dev/null)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
}
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
# var, .par return, ListBuffer test
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
#
210
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
    46
echo "collatz.scala does not contain vars, return etc?" | tee -a $out
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    47
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    48
if (scala_vars collatz.scala)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    49
then
210
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
    50
  echo "  --> test failed" | tee -a $out
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    51
  tsts0=$(( 1 ))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    52
else
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    53
  echo "  --> success" | tee -a $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    54
  tsts0=$(( 0 )) 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
fi
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    56
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    57
### compilation test
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    58
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
if  [ $tsts0 -eq 0 ]
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
then 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  echo "collatz.scala runs?" | tee -a $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  if (scala_compile collatz.scala)
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  then
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
    echo "  --> success" | tee -a $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
    tsts=$(( 0 ))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
  else
210
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
    69
    echo "  --> scala collatz.scala did not run successfully" | tee -a $out
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
    tsts=$(( 1 )) 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    71
  fi
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
else
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    73
  tsts=$(( 1 ))     
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    74
fi
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    75
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    76
echo >> $out
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
    77
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    78
### collatz tests
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    79
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    80
if [ $tsts -eq 0 ]
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    81
then
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    82
  echo "collatz.scala tests:" | tee -a $out
210
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
    83
  echo "  collatz(1) == 0" | tee -a $out
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
    84
  echo "  collatz(6) == 8" | tee -a $out
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
    85
  echo "  collatz(9) == 19" | tee -a $out
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
    86
  echo "  collatz(9000) == 47" | tee -a $out
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    87
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    88
  if (scala_assert "collatz.scala" "collatz_test1.scala")
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    89
  then
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    90
      echo "  --> success" | tee -a $out
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
    91
      marks=$(( marks + 1 ))
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    92
  else
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    93
    echo "  --> one of the tests failed" | tee -a $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    94
  fi
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    95
fi
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    96
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    97
### collatz-max tests
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    98
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    99
if [ $tsts -eq 0 ]
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   100
then
210
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   101
  echo "  collatz_max(10) == (19, 9)" | tee -a $out
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   102
  echo "  collatz_max(100) == (118, 97)" | tee -a $out
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   103
  echo "  collatz_max(1000) == (178, 871)" | tee -a $out
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   104
  echo "  collatz_max(10000) == (261, 6171)" | tee -a $out
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   105
  echo "  collatz_max(100000) == (350, 77031)" | tee -a $out
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   106
  echo "  collatz_max(1000000) == (524, 837799)" | tee -a $out
212
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   107
  #  echo "  collatz_max(2) == (1, 2) || collatz_max(2) == (0, 1)" | tee -a $out
4bda49ec24da updated
Christian Urban <urbanc@in.tum.de>
parents: 210
diff changeset
   108
  echo "  collatz_max(2) == (1, 2)" | tee -a $out
210
63a1376cbebd updated
Christian Urban <urbanc@in.tum.de>
parents: 158
diff changeset
   109
  echo "  collatz_max(77000) == (339, 52527)" | tee -a $out
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   110
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
  if (scala_assert "collatz.scala" "collatz_test2.scala") 
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  then
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
      echo "  --> success" | tee -a $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
      marks=$(( marks + 1 ))
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  else
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
    echo "  --> one of the tests failed" | tee -a $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   117
  fi
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   118
fi
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   119
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   120
### last-odd tests
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   121
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   122
if [ $tsts -eq 0 ]
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   123
then
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   124
  echo "  last_odd(113) == 85" | tee -a $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   125
  echo "  last_odd(84) == 21" | tee -a $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   126
  echo "  last_odd(605) == 341" | tee -a $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   127
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   128
  if (scala_assert "collatz.scala" "collatz_test3.scala") 
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   129
  then
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   130
      echo "  --> success" | tee -a $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   131
      marks=$(( marks + 1 ))
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   132
  else
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   133
    echo "  --> one of the tests failed" | tee -a $out
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   134
  fi
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   135
fi
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   136
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
## final marks
266
ca48ac1d3c3e updated to 2.13
Christian Urban <urbanc@in.tum.de>
parents: 261
diff changeset
   140
echo >> $out
343
c8fcc0e0a57f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 281
diff changeset
   141
echo "Overall mark for the Preliminary Part" | tee -a $out
158
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
echo " $marks" | tee -a $out
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
94b11ac19b41 updated
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144