Tutorial: Moodle VPL Video Demo -- Java Heap

From dftwiki
Jump to: navigation, search

--D. Thiebaut (talk) 11:59, 7 November 2014 (EST)


MoodleVPLLogo.png



Moodle VPL Tutorials



The files in this tutorial are the files used in the YouTube video demonstrating the creation of a VPL module.





vpl_run.sh


#! /bin/bash
# D. Thiebaut

cat > vpl_execution <<EEOOFF
#! /bin/bash
 
prog1=Heap
 
javac \${prog1}.java 2>&1 >/dev/null | grep -v Note > grepLines.out
javac HeapTest.java  2>&1 >/dev/null 
javac HeapEmptyException.java  2>&1 >/dev/null 

if [ -s grepLines.out ] ; then 
     echo "Some compiler ERRORS reported"
     cat grepLines.out
     exit
fi

echo "--------------------"
echo "Your program output:"
java HeapTest | cat -v
echo "--------------------"


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.
#
#set -x

cat > vpl_execution <<EEOOFF
#! /bin/bash

# ----------------------------------------------------------------------
# User program and test program
prog=Heap
testProg=HeapTest
exception=HeapEmptyException

logKey=Zorro      # if found in user program, generates a log
log=false

if grep --quiet -i \$logKey \${prog}.java ; then
  set -x
  log=true
fi

# ----------------------------------------------------------------------
# Grade information
gradeCompileError=10 
gradeCompileGood=30
gradeIncrement=6    # (100-30)/ # test (70/12 = 6)

# ----------------------------------------------------------------------
# compile function
compile() {

   if [ "\$log" = "true" ]; then
       echo "--- compile() ---"
       javac \${prog}.java  | grep -v Note > grepLines.out
       javac \${testProg}.java 
       javac \${exception}.java
   fi


   javac \${prog}.java 2>&1 >/dev/null | grep -v Note > grepLines.out
   javac \${testProg}.java 2>&1 >/dev/null 
   javac \${exception}.java 2>&1 > /dev/null
   
   if [ -s grepLines.out ] ; then 
     echo "Comment :=>> Compilation ERRORS reported"
     echo "<|--"
     cat grepLines.out
     echo "--|>"
     echo "Grade :=>> \$gradeCompileError"
     exit
   fi
}

# ----------------------------------------------------------------------
# getUserOutput: get the output from the user program
getUserOutput() {

    if [ "\$log" = "true" ]; then
       echo "--- getUserOutput() ---"
    fi


    java  \${testProg} 1 > user1.out
    java  \${testProg} 1 | grep "OK" | wc -l  > user2.out

    if [ "\$log" = "true" ]; then
        echo "--- user1.out ---"
        cat -v user1.out
        echo "--- user2.out ---"
        cat -v user2.out
    fi
}

# ----------------------------------------------------------------------
# generateExpectedOutput: generate files containg what is expected to
# be output.
generateExpectedOutput() {

    if [ "\$log" = "true" ]; then
       echo "--- generateExpectedOutput() ---"
    fi

    cat > expected1.out <<EOF
Test #1: Empty-Heap Test -- OK
Test #2: Empty-Size Test -- OK
Test #3: Heap-Size Test -- OK
Test #4: Heap-GetTop Test -- OK
Test #5: Heap-GetTop Test -- OK
Test #6: Heap-Size Test -- OK
Test #7: Heap-GetTop Test -- OK
Test #8: Heap-GetTop Test -- OK
Test #9: Heap-Empty One-At-A-Time Test -- OK
Test #10: Heap-Pop Throw Execption Test -- OK
Test #12: Heap-Pop Throw Execption Test -- OK
Test #13: Heap-Pop Throw Execption Test -- OK
EOF

    cat > expected2.out <<EOF
12
EOF

    if [ "\$log" = "true" ]; then
        echo "--- expected1.out ---"
        cat expected1.out
        echo "--- expected2.out ---"
        cat expected2.out
    fi
}

# ----------------------------------------------------------------------
# EvaluateUserOutput: evaluates what the program outputs
EvaluateUserOutput() {
   user=\$1
   expected=\$2

   if [ "\$log" = "true" ]; then
       echo "--- EvaluateUserOutput() ---"
       echo "user = \$user"
       echo "expected = \$expected"
   fi

   diff -y -w --ignore-all-space \$user \$expected > diff.out

   if ((\$? > 0)); then
      if [ "\!$bool" = "true" ]; then
          echo "---diff.out---"
          cat diff.out
      fi 


      echo "Comment :=>>- Your output is incorrect."

      echo "Comment :=>> ---------------"
      echo "Comment :=>>- Your output:"
      echo "Comment :=>> ---------------"
      echo "<|--"
      cat \$user
      echo "--|>"
      echo ""
      #echo "Comment :=>> ---------------"
      #echo "Comment :=>>- Expected output "
      #echo "Comment :=>> ---------------"
      #echo "<|--"
      #cat \$expected
      #echo "--|>"
   else
      echo "Comment :=>> Correct output."
      grade=\$((grade+gradeIncrement))
   fi
}

cleanup() {
   rm *.out?
}

#============================================================
#                           M  A  I  N 
#============================================================

compile

#--- if we're here, there's no compilation errors ---

grade=\$gradeCompileGood

#---  generate the user output ---

getUserOutput

#--- generate output from solution program ---

generateExpectedOutput

#--- compare user output to computer output ---

echo "Comment :=>> Test Results"
EvaluateUserOutput user1.out expected1.out

noTestsPassed=\`cat user2.out\`
echo "Comment :=>> Your program passed \$noTestsPassed tests out of 12"
grade=\$((grade+gradeIncrement*noTestsPassed))
if (( grade > 100 )); then
    grade=100
fi

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

chmod +x vpl_execution


HeapTest.java


public class HeapTest {

	private static void test( boolean ok, int n, String msg ) {
		System.out.print( "Test #" + n + ": " + msg );
		if ( ok ) System.out.println( " -- OK" );
		else System.out.println( " -- FAIL" );
	}

	public static void main( String[] args ) {
		if ( args.length != 0 ) 
			test1();
		else
			test0();
	}
	public static void test0() {
		Heap heap = new Heap();
		int[] keys = new int[] { 3, 100, 10 };
		
		System.out.println( "Heap empty status: " + heap.isEmpty() );
		
		for ( int i=0; i<keys.length; i++ ) {
			heap.insert( keys[i] );
			System.out.println( "After inserting "+ keys[i] + ": " 
					+ heap + " -- size = " + heap.size()  );
		}

		while ( ! heap.isEmpty() ) {
			int root = heap.getTop();
			System.out.println( "heap after deleting " + root + ": " 
					+ heap +" -- size = " + heap.size() );
		}	
	}
	
	public static void test1( ) {
		Heap heap = new Heap();
				
		test(  heap.isEmpty(), 1, "Empty-Heap Test" ); 
		test(  heap.size()==0, 2, "Empty-Size Test" ); 
				
		for ( int i=0; i<100; i++ ) 
			heap.insert( 0 );
		
		test( heap.size()==100, 3, "Heap-Size Test" );
		int x = heap.getTop();
		test( x==0, 4, "Heap-GetTop Test" );

		heap.insert( 1 );;
		test( heap.getTop()==1, 5, "Heap-GetTop Test" );
		
		heap.clear();
		
		
		for ( int i=0; i<100; i++ ) 
			heap.insert( i );
		
		test( heap.size()==100, 6, "Heap-Size Test" );
		x = heap.getTop();
		test( x==99, 7, "Heap-GetTop Test" );

		heap.insert( 100 );
		x = heap.getTop();
		test( x==100, 8, "Heap-GetTop Test" );

		// empty the heap
		int count = 0;
		int size = heap.size();
		while( ! heap.isEmpty() ) {
			heap.getTop();
			count++;
		}
		
		test( count==size, 9, "Heap-Empty One-At-A-Time Test" );
		
		// test Exception
		heap.clear();
		heap.insert( 1 );
		try {
			int x1 = heap.pop();
			test( x1==1, 10, "Heap-Pop Throw Execption Test" );
			x1 = heap.pop();
			test( false, 11, "Heap-Pop Throw Execption Test" );
		}
		catch ( HeapEmptyException e ) {
			test( true, 12, "Heap-Pop Throw Execption Test" );
		}
		test( true, 13, "Heap-Pop Throw Execption Test" );
		
	}
}


HeapEmptyException.java


public class HeapEmptyException extends Exception{
    //Parameterless Constructor
    public HeapEmptyException() {}

    //Constructor that accepts a message
    public HeapEmptyException(String message){
       super(message);
    }
}


Heap.java



...