Top Banner
JUnit test and Project 3 simulation
59

JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Dec 23, 2015

Download

Documents

Hugh Singleton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

JUnit test and Project 3 simulation

Page 2: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

2

Midterm Exam

Wednesday, March 18, 2009Content: Week 1 to Week 9Guideline: posted on D2L.Format:

Multiple choicesSimple problem solving questionsWriting code

Page 3: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

3

JUnit

The testing problemsThe framework of JUnitA case study

Acknowledgement: using some materials from JUNIT tutorial by Hong Qing Yu (www.cs.le.ac.uk/people/hqy1)

Page 5: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

5

The Testing Problems

Programmers need such kind of tool:

“Writing a few lines of code, then a test that should run, or even better, to write a test that won't run, then write the code that will make it run.”

JUnit is that kind of tool!

Page 6: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

6

JUnit

The testing problems The framework of JUnitA case study

Page 8: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

8

JUnit

The testing problemsThe framework of JUnitA case study

Page 9: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

9

A Case Study

Lab3Queue:enQueue methoddeQueue method

Page 10: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

10

Include junit library in eclipse

Page 11: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

11

How to Write A TestCase using Junit (available in Eclipse 3.1 or later)

Step 1:Create a JUNIT test case (File -> New -> Junit Test Case

Page 12: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

12

Create a test case

Page 13: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

13

Create a test case

import junit.framework.*;

public class Lab3QueueTest {

public void setUp() throws Exception {

}

public void tearDown() throws Exception {

}

}

Page 14: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

14

Create a test case

import junit.framework.*;

public class Lab3QueueTest extends TestCase {

Lab3Queue testQueue;

int queueSize;

public void setUp() throws Exception {

testQueue = new Lab3Queue();

queueSize = testQueue.getSize();

}

public void tearDown() throws Exception {

}

}

Page 15: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

15

Create a test case

For each method that you are going to test:Write a corresponding test method named:

test<method name> in the test case

Page 16: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

16

Create a test case

public void testenQueue() {

int newItem = 1;

queueSize = testQueue.getSize();

testQueue.enQueue(newItem);

Assert.assertEquals(queueSize+1, testQueue.getSize());

int actualItem = ((Integer) testQueue.getLastNode()).intValue();

Assert.assertEquals(newItem, actualItem);

}

Page 17: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

17

Assert assertEquals(expected, actual) assertEquals(message, expected, actual) assertEquals(expected, actual, delta) assertEquals(message, expected, actual, delta) assertFalse(condition) assertFalse(message, condition) Assert(Not)Null(object) Assert(Not)Null(message, object) Assert(Not)Same(expected, actual) Assert(Not)Same(message, expected, actual) assertTrue(condition) assertTrue(message, condition)

Page 18: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

18

Structure

setUp() Storing the fixture's objects in instance variables of your

TestCase subclass and initialize them by overriding the setUp method

tearDown() Releasing the fixture’s

Page 19: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

19

Writing a test suite

Step 2: Create a test suite by choosing

Page 20: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

20

Writing a test suite

Page 21: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

21

Writing a test suite

import junit.framework.Test;

import junit.framework.TestSuite;

public class AllTests {

public static Test suite() {

TestSuite suite = new TestSuite("Test for AiportSimulation");

//$JUnit-BEGIN$

suite.addTestSuite(Lab3QueueTest.class);

//$JUnit-END$

return suite;

}

}

Page 22: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

22

Running a test

AllTests -> choose Run -> Run As -> Junit Test

Page 23: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

23

Running a test

Page 24: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

24

Design Test Cases

The real world scenarios The number boundaries

Page 25: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

25

Tips

Testcases must extend TestCaseAll ‘test’ methods must include at least one call toan assert method or to fail:

assertEquals (String message, ...)assertNotNull (String message, Object obj)assertNull (String message, Object obj)assertSame (String message, Obj exp, Obj

actual)assertTrue (String message, boolean condition)fail (String message)

Remove System.out.println after test cases are working and rely on Junit assert methods to determine success/failure.

Page 26: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

26

Dynamic Run

Since JUnit 2.0 there is an even simpler dynamic way. You only pass the class with the tests to a TestSuite and it extracts the test methods automatically.

suite.addTestSuite(Lab3QueueTest.class);

Page 27: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

27

Project 3 - Algorithm

Page 28: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Recursion

Page 29: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

29

Recursive Thinking

Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways

Recursion splits a problem into one or more simpler versions of itself

Page 30: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

30

Recursive Thinking

Recursion is a problem-solving approach that can be used to generate simple solutions to certain kinds of problems that would be difficult to solve in other ways

Recursion splits a problem into one or more simpler versions of itself

Page 31: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 31

Steps to Design a Recursive Algorithm

Step 1: There must be at least one case (the base case), for a small value of

n, that can be solved directly Step 2:

A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case)

Step 3: Recognize the base case and provide a solution to it

Step 4: Devise a strategy to split the problem into smaller versions of itself

while making progress toward the base case Step 5:

Combine the solutions of the smaller problems in such a way as to solve the larger problem

Page 32: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 32

Steps to Design a Recursive Algorithm

Step 1: There must be at least one case (the base case), for a small value of

n, that can be solved directly Step 2:

A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case)

Step 3: Recognize the base case and provide a solution to it

Step 4: Devise a strategy to split the problem into smaller versions of itself

while making progress toward the base case Step 5:

Combine the solutions of the smaller problems in such a way as to solve the larger problem

Page 33: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 33

Steps to Design a Recursive Algorithm

Step 1: There must be at least one case (the base case), for a small value of

n, that can be solved directly Step 2:

A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case)

Step 3: Recognize the base case and provide a solution to it

Step 4: Devise a strategy to split the problem into smaller versions of itself

while making progress toward the base case Step 5:

Combine the solutions of the smaller problems in such a way as to solve the larger problem

Page 34: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 34

Steps to Design a Recursive Algorithm

Step 1: There must be at least one case (the base case), for a small value of

n, that can be solved directly Step 2:

A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case)

Step 3: Recognize the base case and provide a solution to it

Step 4: Devise a strategy to split the problem into smaller versions of itself

while making progress toward the base case Step 5:

Combine the solutions of the smaller problems in such a way as to solve the larger problem

Page 35: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 35

Steps to Design a Recursive Algorithm

Step 1: There must be at least one case (the base case), for a small value of

n, that can be solved directly Step 2:

A problem of a given size n can be split into one or more smaller versions of the same problem (recursive case)

Step 3: Recognize the base case and provide a solution to it

Step 4: Devise a strategy to split the problem into smaller versions of itself

while making progress toward the base case Step 5:

Combine the solutions of the smaller problems in such a way as to solve the larger problem

Page 36: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 36

Proving that a Recursive Method is Correct

Proof by induction Prove the theorem is true for the base case Show that if the theorem is assumed true for n, then it

must be true for n+1 Recursive proof is similar to induction

Verify the base case is recognized and solved correctly

Verify that each recursive case makes progress towards the base case

Verify that if all smaller problems are solved correctly, then the original problem is also solved correctly

Page 37: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 37

Recursive Definitions of Mathematical Formulas

Mathematicians often use recursive definitions of formulas that lead very naturally to recursive algorithms

Examples include: Factorial Powers Greatest common divisor

If a recursive function never reaches its base case, a stack overflow error occurs

Page 38: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 38

Recursion Versus Iteration

There are similarities between recursion and iteration

In iteration, a loop repetition condition determines whether to repeat the loop body or exit from the loop

In recursion, the condition usually tests for a base case

You can always write an iterative solution to a problem that is solvable by recursion

Recursive code may be simpler than an iterative algorithm and thus easier to write, read, and debug

Page 39: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 39

Efficiency of Recursion

Recursive methods often have slower execution times when compared to their iterative counterparts

The overhead for loop repetition is smaller than the overhead for a method call and return

If it is easier to conceptualize an algorithm using recursion, then you should code it as a recursive method The reduction in efficiency does not outweigh the

advantage of readable code that is easy to debug

Page 40: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

40

Efficiency of Recursion (continued)

Efficient

Inefficient

Page 41: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 41

Recursive Array Search

Searching an array can be accomplished using recursion

Simplest way to search is a linear search Examine one element at a time starting with the first

element and ending with the last Base case for recursive search is an empty array

Result is negative one Another base case would be when the array element

being examined matches the target Recursive step is to search the rest of the array,

excluding the element just examined

Page 42: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 42

Algorithm for Recursive Linear Array Search

Page 43: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 43

Design of a Binary Search Algorithm

Binary search can be performed only on an array that has been sorted

Stop casesThe array is emptyElement being examined matches the target

Checks the middle element for a match with the target

Throw away the half of the array that the target cannot lie within

Page 44: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 44

Design of a Binary Search Algorithm (continued)

Page 45: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 45

Efficiency of Binary Search and the Comparable Interface

At each recursive call we eliminate half the array elements from considerationO(log2 n)

Classes that implement the Comparable interface must define a compareTo method that enables its objects to be compared in a standard wayCompareTo allows one to define the

ordering of elements for their own classes

Page 46: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 46

Method Arrays.binarySearch

Java API class Arrays contains a binarySearch methodCan be called with sorted arrays of primitive

types or with sorted arrays of objects If the objects in the array are not mutually

comparable or if the array is not sorted, the results are undefined

If there are multiple copies of the target value in the array, there is no guarantee which one will be found

Throws ClassCastException if the target is not comparable to the array elements

Page 47: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 47

Method Arrays.binarySearch (continued)

Page 48: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 48

Recursive Data Structures

Computer scientists often encounter data structures that are defined recursively Trees (Chapter 8) are defined recursively

Linked list can be described as a recursive data structure

Recursive methods provide a very natural mechanism for processing recursive data structures

The first language developed for artificial intelligence research was a recursive language called LISP

Page 49: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 49

Recursive Definition of a Linked List

A non-empty linked list is a collection of nodes such that each node references another linked list consisting of the nodes that follow it in the list

The last node references an empty listA linked list is empty, or it contains a

node, called the list head, that stores data and a reference to a linked list

Page 50: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 50

Problem Solving with Recursion

Will look at two problemsTowers of HanoiCounting cells in a blob

Page 51: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 51

Towers of Hanoi

Page 52: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 52

Towers of Hanoi (continued)

Page 53: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 53

Counting Cells in a Blob

Consider how we might process an image that is presented as a two-dimensional array of color values

Information in the image may come from X-Ray MRI Satellite imagery Etc.

Goal is to determine the size of any area in the image that is considered abnormal because of its color values

Page 54: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 54

Counting Cells in a Blob (continued)

Page 55: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 55

Counting Cells in a Blob (continued)

Page 56: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 56

Counting Cells in a Blob (continued)

Page 57: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 57

Backtracking

Backtracking is an approach to implementing systematic trial and error in a search for a solution An example is finding a path through a maze

If you are attempting to walk through a maze, you will probably walk down a path as far as you can go

Eventually, you will reach your destination or you won’t be able to go any farther

If you can’t go any farther, you will need to retrace your steps

Backtracking is a systematic approach to trying alternative paths and eliminating them if they don’t work

Page 58: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 58

Backtracking (continued)

Never try the exact same path more than once, and you will eventually find a solution path if one exists

Problems that are solved by backtracking can be described as a set of choices made by some method

Recursion allows us to implement backtracking in a relatively straightforward manner Each activation frame is used to remember the choice

that was made at that particular decision point A program that plays chess may involve some kind

of backtracking algorithm

Page 59: JUnit test and Project 3 simulation. Midterm Exam Wednesday, March 18, 2009 Content: Week 1 to Week 9 Guideline: posted on D2L. Format: Multiple choices.

Chapter 7: Recursion 59

Backtracking (continued)