Tutorial: Moodle VPL -- Testing a C Program

From DftWiki

Jump to: navigation, search

--D. Thiebaut (talk) 11:44, 19 May 2017 (EDT)


Contents

MoodleVPLLogo.png



Moodle VPL Tutorials



This VPL activity tests a C program that is supposed to get parameters from the command line, and replicate the behavior of the Linux grep command, in particular the ability to control whether searching is case-sensitive or not with the "-i" switch.


vpl_run.sh


#! /bin/bash
# D. Thiebaut
# Smith College
# vpl_evaluate.sh script looping through several tests, each test with
# its own input file and its own expected output file.  The output of the
# student program is tested against the expected output file.
#


cat > vpl_execution <<EEOOFF
#! /bin/bash
 
# --- program tested (no extension) ---
prog1=231grep
gcc -o \${prog1} \${prog1}.c

# --- compile student program ---
#chmod a+x \${prog1}

# --- create test input files ---
cat > data1.txt <<EOF
one file1.txt
EOF

cat > data1.out <<EOF
one two
one two three
EOF

# ---
cat > data2.txt <<EOF
-i one file1.txt
EOF

cat > data2.out <<EOF
ONE
one two
one two three
ONE TWO
ONE TWO THREE
EOF

# ---
cat > data3.txt <<EOF
-i one file2.txt
EOF

cat > data3.out <<EOF
grep: file2.txt: No such file or directory
EOF
   
# ---

cat > data4.txt <<EOF
-i eleven file1.txt
EOF

cat > data4.out <<EOF
EOF

# ---
cat > data5.txt <<EOF
eleven file1.txt
EOF

cat > data5.out <<EOF
EOF




grade=100

for i in 1  ; do
   echo "Comment :=>>-TEST \$i"

   # ==============================================
   # TEST i
   # ==============================================
   #--- run program, capture output, display to student ---
   command="head -n 1 data\${i}.txt"
   input=\$( \$command )
   ./\${prog1} \${input}  &> user.out
   cp user.out user.out.org

   #--- remove non numbers and non minus---
   #cat user.out | sed 's/[^0-9\ -]*//g' > dummy.out
   #mv dummy.out user.out
 
   #--- remove multiple spaces ---
   cat user.out | sed 's/  */ /g' > dummy.out
   mv dummy.out user.out

   #--- remove blank lines ---
   cat user.out | sed '/^\s*$/d' > dummy.out
   mv dummy.out user.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
      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 (only the numbers): "
      echo "Comment :=>> ---------------"
      echo "<|--"
      cat data\${i}.out
      echo "--|>"
   
      #--- consolation grade ---  
      #grade=\$((grade-5))

      # --------------------- REWARD IF CORRECT OUTPUT -----------------
   else
      #--- good output ---
      echo "Comment :=>>- Congrats, your output is correct."
      echo "Comment :=>> --------------------------------."
      echo "<|--"
      cat user.out.org
      echo "--|>"
      #grade=\$( expr \$grade + 13 )
   fi
done

#echo "Grade :=>> \$grade"

EEOOFF


 
chmod +x vpl_execution


vpl_evaluate.sh


#! /bin/bash
# D. Thiebaut
# Smith College
# vpl_evaluate.sh script looping through several tests, each test with
# its own input file and its own expected output file.  The output of the
# student program is tested against the expected output file.
#


cat > vpl_execution <<EEOOFF
#! /bin/bash
 
# --- program tested (no extension) ---
prog1=231grep
gcc -o \${prog1} \${prog1}.c

# for debugging...
#cp 231grep.c grep.py
#prog1=grep.py
#chmod +x grep.py


# --- compile student program ---
#chmod a+x \${prog1}

# --- create test input files ---
cat > data1.txt <<EOF
one file1.txt
EOF

cat > data1.out <<EOF
one two
one two three
EOF

# ---
cat > data2.txt <<EOF
-i one file1.txt
EOF

cat > data2.out <<EOF
ONE
one two
one two three
ONE TWO
ONE TWO THREE
EOF

# ---
cat > data3.txt <<EOF
-i one file2.txt
EOF

cat > data3.out <<EOF
grep: file2.txt: No such file or directory
EOF
   
# ---

cat > data4.txt <<EOF
-i eleven file1.txt
EOF

cat > data4.out <<EOF
EOF

# ---
cat > data5.txt <<EOF
eleven file1.txt
EOF

cat > data5.out <<EOF
EOF




grade=100

for i in 1 2 3 4 5 ; do
   echo "Comment :=>>-TEST \$i"

   # ==============================================
   # TEST i
   # ==============================================
   #--- run program, capture output, display to student ---
   command="head -n 1 data\${i}.txt"
   input=\$( \$command )
   ./\${prog1} \${input}  &> user.out
   cp user.out user.out.org

   #--- remove non numbers and non minus---
   #cat user.out | sed 's/[^0-9\ -]*//g' > dummy.out
   #mv dummy.out user.out
 
   #--- remove multiple spaces ---
   cat user.out | sed 's/  */ /g' > dummy.out
   mv dummy.out user.out

   #--- remove blank lines ---
   cat user.out | sed '/^\s*$/d' > dummy.out
   mv dummy.out user.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
      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 (only the numbers): "
      echo "Comment :=>> ---------------"
      echo "<|--"
      cat data\${i}.out
      echo "--|>"
   
      #--- consolation grade ---  
      #grade=\$((grade-5))

      # --------------------- REWARD IF CORRECT OUTPUT -----------------
   else
      #--- good output ---
      echo "Comment :=>>- Congrats, your output is correct."
      echo "Comment :=>> --------------------------------."
      echo "<|--"
      cat user.out.org
      echo "--|>"
      #grade=\$( expr \$grade + 13 )
   fi
done

echo "Grade :=>> \$grade"

EEOOFF


 
chmod +x vpl_execution


file1.txt


ONE
one two
one two three
ONE TWO
three
ONE TWO THREE
four five
three
FOUR FIVE