VPL Module for C

From dftwiki3
Revision as of 08:50, 12 April 2018 by Thiebaut (talk | contribs) (Create a simple solution program: hw20sol.cs)
Jump to: navigation, search

D. Thiebaut (talk) 09:47, 12 April 2018 (EDT)


This is a rough tutorial, put together quickly to help out Ariana Maksimović who was trying to setup a VPL module for C#. I'm assuming that you are familiar with the VPL interface and understand how vpl_run.sh and vpl_evaluate.sh work.


Setup


Install the mono C# compiler on your jail server. These commands should work:

sudo apt-get install mono-complete
sudo apt-get install monodevelop



Requested File


Go to the Requested Files menu in the VPL Administration and create the file hw20.cs as the name of the file students will submit.

Create a simple solution program: hw20sol.cs


We assume that the students will submit a file called hw20.cs, and we add hw20sol.cs as a solution program in the VPL Execution Files section.

// hw20sol.cs
// A Hello World! program in C#. 
using System;
namespace HelloWorld
{
    class Hello 
    {
        static void Main() 
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine("Hello World!");

            // Keep the console window open in debug mode.
            //Console.WriteLine("Press any key to exit.");
            //Console.ReadKey();
        }
    }
}


Create a vpl_run.sh Script


#! /bin/bash
# D. Thiebaut
# Smith College
# vpl_run.sh script
# runs the student program and compare the output to the solution
# program and displays some information about the lines that do not
# match
# set -x

#--- figure out how to filter the outputs --- 
keepOnlyNumbers=false
removeMultipleSpaces=true
removeBlankLines=true
keepLastLines=0    # set to 0 if not needed 

# --- program tested (no extension) --- 
prog1=hw20

# create vpl_execution which will be run on the jail server
# 
cat > vpl_execution <<EEOOFF
#! /bin/bash

# --- program tested (no extension) ---
prog1=$prog1


# ---------------------------------------------------------------
# scale( percentGood )
# transforms inGrade into outGrade
# ---------------------------------------------------------------
function scale {
    percent=\$1
    inGrade=(   95 90 85 80 75 70 65 60 55 50 0 )
    #            A A- B+ B  B- C+ C  C- D+ D  F
    outGrade=( 100 92 90 88 86 84 80 76 72 68 50 )
    #outGrade=( 100 93 87 83 80 77 73 80 67 60 50 )
 
    # get length of an array
    arraylength=\${#inGrade[@]}

    # loop through all the grades
    for (( i=0; i<\${arraylength}; i++ ));
    do
        if (( \$percent > \${inGrade[\$i]} )) ; then 
    return \${outGrade[\$i]}
        fi
    done
    return 50
}

# ---------------------------------------------------------------
# computeMatching( solution, student )
# returns the percentage of matching lines
# ---------------------------------------------------------------
function computeMatching {
    noLinesToMatch=\`cat \$1 | wc -l\`
    noGoodLines=\`grep -Fxf \$2 \$1 | wc -l\`
    percentGood=\$(( \$noGoodLines * 100 / \$noLinesToMatch ))
    return \$percentGood
}

# ---------------------------------------------------------------
# runProgram.  Stop if program crashes
# ---------------------------------------------------------------
function run {
    mcs \${1}.cs &&  mono \${1}.exe  &> user.out  
    
    errCode=\$?
     if [ \$errCode -ne 0 ]; then 
         echo "Error! Unfortunately, your program crashed."
         cat user.out
         exit 0
     fi
}
 

#-----------------------------------------------------------------
#--- run studentprogram, capture output, keep  original output ---
#-----------------------------------------------------------------
run \${prog1}      # output will &> user.out

#-----------------------------------------------------------------
#--- generate output for solution program and treat it same as ---
#--- output of user.                                           ---
#-----------------------------------------------------------------

mcs \${prog1}sol.cs  
mono \${prog1}sol.exe > expectedOutput 

#--- compute difference.  Count number of lines that are  --- 
#--- different                                            ---
diff -y -w --ignore-all-space user.out expectedOutput > diff.out
   
#--- reject if different ---
if ((\$? > 0)); then
      echo "Your output is incorrect."

      echo "Output from your program:"
      cat user.out
      echo ""
      echo "Expected output: "
      cat expectedOutput
      echo ""

      #--- accumulate percentages obtained at different tests ---   
      computeMatching expectedOutput user.out
      percent=\$?
else
      # --------------------- REWARD IF CORRECT OUTPUT -----------------

      #--- good output ---
      echo "Congrats, the output of your program is correct!"
      cat user.out
      percent=100
fi


scale \$percent
grade=\$?
echo ""
echo  \${grade}


EEOOFF

chmod +x vpl_execution


vpl_evaluate.sh


#! /bin/bash
cp vpl_run.sh vpl_evaluate2.sh
./vpl_evaluate2.sh