Presentation is loading. Please wait.

Presentation is loading. Please wait.

In this session you will: See how various techniques are used to test methods for correct operation See how using testing templates can speed up code magnet.

Similar presentations


Presentation on theme: "In this session you will: See how various techniques are used to test methods for correct operation See how using testing templates can speed up code magnet."— Presentation transcript:

1 In this session you will: See how various techniques are used to test methods for correct operation See how using testing templates can speed up code magnet lab development Write the function you want students to construct and adapt one of the provided templates to suit your needs Implement and test your method outside of WAGS Designing a Method and Test Program

2 Three common approaches 1.Compare student result with a working method that generates a correct answer 2.Supply a known answer as a constant value for comparison 3.Test independently if a criteria is satisfied We illustrate each approach with these programs 1.Josephus selection problem using queues 2.Discrete log 2 using a while loop 3.The NegPosPartition problem just examined Three Approaches for Test Programs

3 public class Josephus { /** * This method applies the Josephus selection algorithm. * @param skipValue the number of items to be advanced to next item selected * @param initialSize the inital number of items * @return the ID of the one item selected */ public int lastSelected(int skipValue, int initialSize) { LinkedQueue q = new LinkedQueue (); int currentSelection = 0; for (int i = 0; i < initialSize; i++) q.add(i); while (!q.isEmpty()) { for (int i = 0; i < skipValue-1; i++) q.add(q.remove()); currentSelection = q.remove(); } return currentSelection; } 1. Test Against a Correct Method - 1 This is a correct method that is encapsulated in its own class. The test program will create an instance of this class and call the method to find the expected result. This is the most common technique for testing.

4 public class TestJosephus { private boolean testLastSelected(Josephus myJosephus, Student student, int skipValue, int initialSize){ System.out.println("initialSize = "+initialSize+" and skipValue = "+skipValue); System.out.println("The correct last item is " + myJosephus.lastSelected(skipValue, initialSize)); System.out.println("The last item you selected is " + student.lastSelected(skipValue, initialSize)); if(student.lastSelected(skipValue, initialSize) == myJosephus.lastSelected(skipValue, initialSize)) { System.out.println("Correct!\n"); return true; } else { System.out.println("Incorrect.\n"); return false; } private boolean test() { Student student = new Student(); // used to call method submitted by student Josephus myJosephus = new Josephus(); // used to call a correct method if(!testLastSelected(myJosephus,student,2,7)) return false; else if(!testLastSelected(myJosephus,student,3,10)) return false; else if(!testLastSelected(myJosephus,student, 13, 100)) return false; return true; } public static void main(String[] args) { TestJosephus myTest = new TestJosephus(); if(myTest.test()) System.out.println(args[0]); } 1. Test Against a Correct Method - 2 This printing of a nonce value is required for security reasons.

5 public class TestWhileLog { private boolean testLog2Num(Student student, int num, int result){ int studentResult = student.log2Num(num); System.out.println("Given num = " + num); System.out.println("Your log2Num method returned " + studentResult); System.out.println("The correct answer is " + result); if(studentResult == result) return true; else return false; } private TestWhileLog(String[] args){ boolean correct = true; Student student = new Student(); if(correct && !testLog2Num(student, 1024, 10)) correct = false; if(correct && !testLog2Num(student, 63, 5)) correct = false; if(correct && !testLog2Num(student, 64,6)) correct = false; if(correct && !testLog2Num(student, 127,6)) correct = false; if(correct) System.out.println(args[0]); else System.out.println("Incorrect"); } public static void main(String[] args) { TestWhileLog myTest = new TestWhileLog(args); } 2. Test Against a Known Solution This printing of a nonce value is required for security reasons. The correct answer is supplied as a constant value.

6 public class TestNegativePositivePartition { private boolean correctCheckPartition(int[] arr){ int index = 0; while(index < arr.length && arr[index]<0) index++; while(index < arr.length){ if(arr[index] < 0) return false; index++; } return true; } private void testArray(int[] data, Student student){ student.partition(data); boolean correctCheck = correctCheckPartition(data); if(correctCheck){System.out.print("correct, your data is ");} else {System.out.print("incorrect, your data is ");} printData(data); System.out.println(); } private void printData(int[] arr){ for(int index = 0; index < arr.length; index++) { System.out.print(arr[index]+" ");} } The PosNegPartition Test Program - 1 This method independently checks that the student solution has all negative values before all positive values. These are helper methods.

7 private void testNegPosPartition(){ Student student = new Student(); int[] data = {-1, 3, 4, -7, 5, 0, -1, 9}; testArray(data, student); int[] data1 = {-1, -3, -4, -7, -5, -3, -1, -9}; testArray(data1, student); int[] data2 = {1, 3, 4, 0, 5, 3, 1, 9}; testArray(data2, student); int[] data3 = {4, -7}; testArray(data3, student); int[] data4 = {-1}; testArray(data4, student); int[] data5 = {9}; testArray(data5, student); int[] data6 = {}; testArray(data6, student); } public static void main(String[] args) { TestNegativePositivePartition tester = new TestNegativePositivePartition(); tester.testNegPosPartition(); System.out.println(args[0]); } The PosNegPartition Test Program - 2 This printing of a nonce value is required for security reasons. These are the unit tests. Make sure that tests with limiting cases are included.

8 You will use one of the testing templates provided Eight templates are available: Test Program Templates Parameter Type(s)Return TypeSample Method integer iterative Fibonacci integer, integerintegercombinations C(n,r) integer arrayintegerlongest consecutive increasing streak stringinteger# of digit characters in string character, stringinteger# of characters greater than the character parameter integer, integer arrayinteger array return new array with all instances of the integer parameter removed double arraydoublevariance double array, double arraydoublePearson correlation coefficient

9 We illustrate using the “int return int” signature The sample method is a Fibonacci function There are four Java files involved 1.IntReturnIntTest.java – this is heart of the test program; does not need modification 2.ParamDataAndInvocations.java –This file will be modified as described in the steps outlined below 3. CorrectMethod.java – this file contains a correct version of the method being implemented 4.Student.java – this file contains the method that the student develops as a code magnet microlab How to Use the Test Templates

10 1.Design your own method with the same signature as the testing template program and put a copy inside of CorrectMethod.java and Student.java 2.Change the test values in ParamDataAndInvocations.java as appropriate 3.Change invokeCorrectMethod and invokeStudentMethod to call the appropriate methods in the CorrectMethod and Student classes, respectively 4.Upload the four files into your IDE 5.Set the first main method parameter value to any integer 6.Run the test program and insure you get the expected results Steps for Implementing a New Method

11 We will change the Fibonacci method into a recursive factorial method to illustrate this step- by-step process. Then you will select a method signature matching one of the available testing templates. Design a method with the same signature that would be appropriate for one of the courses you teach. Use the link www.cs.appstate.edu/wags/beta Develop your subprogram and test program now. Demonstration Then It’s Your Turn


Download ppt "In this session you will: See how various techniques are used to test methods for correct operation See how using testing templates can speed up code magnet."

Similar presentations


Ads by Google