core_marking1/collatz_test.sh
author Christian Urban <christian.urban@kcl.ac.uk>
Sun, 15 Sep 2024 12:57:59 +0100
changeset 493 244df77507c2
parent 463 0315d9983cd0
permissions -rwxr-xr-x
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
#!/bin/bash
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
# to make the script fail safely
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
set -euo pipefail
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
out=${1:-output}
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
     9
echo -e "" > $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    11
echo `date` | tee -a $out
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    12
echo "" >> $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
echo "Below is the feedback and provisional marks for your submission" >> $out
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    14
echo "for the Core Part 1 (Scala).  Please note all marks are provisional until" >> $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    15
echo "ratified by the assessment board -- this is not an official" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    16
echo "results transcript." >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    17
echo "" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    18
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    19
echo "The feedback for your submission for collatz.scala" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    20
echo "" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    21
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    22
# marks for core part 1
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    23
marks=$(( 0 ))
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    24
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    25
# compilation tests
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    26
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    27
function scala_compile {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    28
  (JAVA_OPTS="-Xmx1g" scala -Xprint:parser "$1" 2> c$out 1> c$out)
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    29
}
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    30
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    31
# functional tests
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    32
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    33
function scala_assert {
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    34
  (JAVA_OPTS="-Xmx1g" scala -nc -i "$1" -- "$2" -e "" 2> /dev/null 1> /dev/null)
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    35
}
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    36
 
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    37
# purity test
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    38
function scala_vars {
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    39
   (sed 's/immutable/ok/g' c$out > cb$out;
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    40
    egrep '\bvar\b|\breturn\b|\.par\.|\.par |ListBuffer|AtomicInteger|mutable|util.control|new Array' cb$out 2> /dev/null 1> /dev/null)
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    41
}
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    42
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    43
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    44
### compilation test
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    45
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    46
echo -e "collatz.scala runs?" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    47
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    48
if (scala_compile collatz.scala)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    49
then
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    50
    echo -e "  --> success" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    51
    tsts0=$(( 0 ))
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    52
else
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    53
    echo -e "  --> SCALA DID NOT RUN collatz.scala\n" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    54
    tsts0=$(( 1 )) 
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    55
fi
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    56
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    57
# var, .par return, ListBuffer test
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    58
#
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    59
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    60
if  [ $tsts0 -eq 0 ]
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    61
then
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    62
   echo -e "collatz.scala does not contain VARS, RETURNS etc?" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    63
    
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    64
   if (scala_vars collatz.scala)
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    65
   then
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    66
      echo -e "  --> test failed\n" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    67
      tsts=$(( 1 ))
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    68
   else
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    69
      echo -e "  --> success" >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    70
      tsts=$(( 0 )) 
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    71
   fi
375
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 373
diff changeset
    72
else
ddda2e76a40f updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 373
diff changeset
    73
   tsts=$(( 1 ))  
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    74
fi    
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    75
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    76
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    77
echo >> $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    78
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    79
### collatz tests
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    80
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    81
if [ $tsts -eq 0 ]
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    82
then
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    83
  echo "collatz.scala tests:"  | tee -a $out
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    84
  echo "  collatz(1) == 0"     | tee -a $out
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    85
  echo "  collatz(6) == 8"     | tee -a $out
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
    86
  echo "  collatz(9) == 19"    | tee -a $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    87
  echo "  collatz(9000) == 47" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    88
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    89
  if (scala_assert "collatz.scala" "collatz_test1.scala")
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    90
  then
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    91
      echo -e "  --> success (+ 1 Mark)\n" | tee -a $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    92
      marks=$(( marks + 1 ))
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    93
  else
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
    94
      echo -e "  --> one of the tests failed\n" | tee -a $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    95
  fi
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    96
fi
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    97
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    98
### collatz-max tests
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    99
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   100
if [ $tsts -eq 0 ]
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   101
then
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   102
  echo "  collatz_max(10) == (19, 9)" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   103
  echo "  collatz_max(100) == (118, 97)" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   104
  echo "  collatz_max(1000) == (178, 871)" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   105
  echo "  collatz_max(10000) == (261, 6171)" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   106
  echo "  collatz_max(100000) == (350, 77031)" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   107
  echo "  collatz_max(1000000) == (524, 837799)" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   108
  #  echo "  collatz_max(2) == (1, 2) || collatz_max(2) == (0, 1)" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   109
  echo "  collatz_max(2) == (1, 2)" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   110
  echo "  collatz_max(77000) == (339, 52527)" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   111
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   112
  if (scala_assert "collatz.scala" "collatz_test2.scala") 
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   113
  then
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   114
      echo -e "  --> success (+ 1 Mark)\n" | tee -a $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   115
      marks=$(( marks + 1 ))
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   116
  else
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   117
      echo -e "  --> one of the tests failed\n" | tee -a $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   118
  fi
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   119
fi
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   120
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   121
### last-odd tests
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   122
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   123
if [ $tsts -eq 0 ]
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   124
then
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   125
  echo "  last_odd(113) == 85" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   126
  echo "  last_odd(84) == 21" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   127
  echo "  last_odd(605) == 341" | tee -a $out
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   128
  START=$(date +%s)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   129
  
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   130
  if (scala_assert "collatz.scala" "collatz_test3.scala") 
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   131
  then
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   132
      END=$(date +%s)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   133
      DIFF=$(( $END - $START ))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   134
      echo -e "  --> success (+ 1 Mark)\n" | tee -a $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   135
      marks=$(( marks + 1 ))
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   136
  else
463
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   137
      END=$(date +%s)
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   138
      DIFF=$(( $END - $START ))
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   139
      echo -e "  This test ran for $DIFF seconds" | tee -a $out  
0315d9983cd0 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 424
diff changeset
   140
      echo -e "  --> one of the tests failed\n" | tee -a $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   141
  fi
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   142
fi
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   143
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   144
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   145
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   146
## final marks
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   147
echo >> $out
424
Christian Urban <christian.urban@kcl.ac.uk>
parents: 382
diff changeset
   148
echo "Overall mark for the Core Part 1 (Scala)" | tee -a $out
373
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   149
echo " $marks" | tee -a $out
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   150
d29cd5883c7b updated
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
   151