Tutorial: Moodle VPL -- Feed Different Tests on Command Line

From DftWiki

Jump to: navigation, search

--D. Thiebaut (talk) 19:22, 24 October 2014 (EDT)


Contents

MoodleVPLLogo.png



Moodle VPL Tutorials



This VPL activity tests a Java program that outputs 0, 1, or several numbers (on the same) line depending on an input that is provided on the command line.


vpl_run.sh


#! /bin/bash
# D. Thiebaut

cat > vpl_execution <<EEOOFF
#! /bin/bash
 
prog1=Hw5_1
 
javac \${prog1}.java > /dev/null
 
if ((\$? > 0)); then
     echo "Error compiling your program"
     exit
fi
KEYS=(0 3 7 34 45 91 100)
CORRECTOUTPUTS=("" "0" "1" "10 11 12 13" 14 29 "" )

for i in \`seq 0 5\`; do
  echo ""
  key=\${KEYS[\$i]}
  echo "Test # \$i"
  echo "key = \$key"
  echo "The correct output is: \${CORRECTOUTPUTS[\$i]}"
  echo "Your program output:"
  echo "java Hw5_1 30 \$key"
  java Hw5_1 30 \$key
done

EEOOFF

 
chmod +x vpl_execution


vpl_evaluate.sh


#! /bin/bash
# D. Thiebaut

cat > vpl_execution <<EEOOFF
#! /bin/bash
#set -x
 
# --- program tested (no extension) ---
prog1=Hw5_1

# --- grades
gradeNotCompile=20
gradeCompileNoGoodOutput=30
gradeIncrement=10    # (100-30)/7 / number of tests

# --- compile student program ---
javac \${prog1}.java
 
if ((\$? > 0)); then
     echo "Comment :=>>  Error compiling your program"
     echo "Grade :=>> \$gradeNotCompile"
     exit
fi

# --- minimum grade if not passing any test ---
grade=\$gradeCompileNoGoodOutput

# =================================================
KEYS=(0 3 7 34 45 91 100)
CORRECTOUTPUTS=("" "0" "1" "10 11 12 13" 14 29 "" )
N=30
NOTESTS=7


# =================================================
# function that prints the difference between user and expected output
incorrectOutput() {
    if [ "\$1" ]; then   # if there's a parameter
        i=\$1
        echo "Comment :=>>- Your output is incorrect."

        #--- display test file ---
        echo "Comment :=>>- Your program tested with:"
        echo "<|--"
        cat data\${i}.txt
        echo "--|>"

        echo "Comment :=>> ---------------"
        echo "Comment :=>>- Your output:"
        echo "Comment :=>> ---------------"
        echo "<|--"
        cat user.out.org
        echo "--|>"
        echo ""
        echo "Comment :=>> ---------------"
        echo "Comment :=>>- Expected output: "
        echo "Comment :=>> ---------------"
        echo "<|--"
        cat data\${i}.out
        echo "--|>"
    fi
}

# function that tells user of program crash or infinite loop, and what the test was.
timeoutOutput() {
    if [ "\$1" ]; then   # if there's a parameter
        i=\$1
        echo "Comment :=>>- Your program has timed out or crashed."

        #--- display test file ---
        echo "Comment :=>>- Your program tested with:"
        echo "<|--"
        cat data\${i}.txt
        echo "--|>"
    fi
}

# function that removes non-digits, extra spaces, and extra blank lines from text file.
cleanup () {
    if [ "\$1" ]; then   # if there's a parameter
       
        #--- remove non numbers and non minus---
        cat \$1 | sed 's/[^0-9*.0-9\ -]*//g' > dummy.out
        mv dummy.out \$1
 
        #--- remove multiple spaces ---
        cat \$1 | sed 's/  */ /g' > dummy.out
        mv dummy.out \$1

        #--- remove blank lines ---
        cat \$1 | sed '/^\s*\$/d' > dummy.out
        mv dummy.out \$1
   fi
}
NOTESTS_1=\`expr \$NOTESTS - 1 \`
for i in \`seq 0  \$NOTESTS_1 \`; do
   # ==============================================
   # TEST i
   # timeout code from
   # http://stackoverflow.com/questions/5161193/bash-script-that-kills-a-child-process-after-a-given-timeout
   # ==============================================
   #--- run program, capture output, display to student ---

   key=\${KEYS[\$i]}
   echo "Comment :=>>-TEST \$i"
   echo "Comment :=>> key = \$key"
   echo "Comment :=>> N = \$N"

   echo "\$N \$key" > data\${i}.txt
   
   timeout 30 java \${prog1} \$N \$key  &> user.out
   retcode=\$?

   #--- if we killed the waiter, everything is good ---
   if [ \$retcode -eq  124 ]; then
       echo "Comment :=>>-Your program has stopped working."
       timeoutOutput \$i
       continue
   else
       echo "Comment :=>>-Your program ran to completion."
   fi

   cp user.out user.out.org

   #--- copy expected output in output file
   echo \${CORRECTOUTPUTS[\$i]} > data\${i}.out

   #--- remove non numbers, space and extra lines from user.out ---
   #--- and from datai.out
   cleanup user.out
   cleanup data\${i}.out

   #--- compute difference ---
   diff -y -w --ignore-all-space user.out data\${i}.out > diff.out
   #echo "----- diff.out ------"
   #cat diff.out
   #echo "---------------------"
   #diff -y -w --ignore-all-space user.out data\${i}.out > diff.out

   #--- reject if different ---
   if ((\$? > 0)); then
      incorrectOutput \$i
   
      # --------------------- REWARD IF CORRECT OUTPUT -----------------
   else
      #--- good output ---
      echo "Comment :=>>- Congrats, your output is correct."
      echo "Comment :=>> --------------------------------."
      echo "<|--"
      cat user.out.org
      echo "--|>"
      grade=\$((grade+gradeIncrement))
   fi
done

# =================================================
# Report grade
if (( grade > 100 )); then
   grade=100
fi
echo "Grade :=>> \$grade"



EEOOFF

 
chmod +x vpl_execution



...