Testing with JUnit, Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington 

Slides:



Advertisements
Similar presentations
Introduction to Analysis of Algorithms
Advertisements

1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Complexity Analysis (Part I)
1 Algorithm Efficiency, Big O Notation, and Role of Data Structures/ADTs Algorithm Efficiency Big O Notation Role of Data Structures Abstract Data Types.
Elementary Data Structures and Algorithms
Abstract Data Types (ADTs) Data Structures The Java Collections API
Algorithm Analysis (Big O)
COMP s1 Computing 2 Complexity
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Java Programs COMP 102 #3.
Chapter 1 Algorithm Analysis
1 Complexity Lecture Ref. Handout p
Introduction to Analysing Costs 2015-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Week 2 CS 361: Advanced Data Structures and Algorithms
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
Lecture 10: Class Review Dr John Levine Algorithms and Complexity March 13th 2006.
A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
ArrayList, Multidimensional Arrays
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
Analysing Costs: ArraySet Binary Search COMP 103.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
FASTER SORTING using RECURSION : QUICKSORT 2014-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus.
2011-T1 Lecture 13 School of Engineering and Computer Science, Victoria University of Wellington  Rashina Hoda and Peter Andreae COMP 102 Rashina Hoda.
An introduction to costs (continued), and Binary Search 2013-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
More about costs: cost of “ensureCapacity”, cost of ArraySet, Binary Search 2014-T2 Lecture 12 School of Engineering and Computer Science, Victoria University.
Lecture 10 – Algorithm Analysis.  Next number is sum of previous two numbers  1, 1, 2, 3, 5, 8, 13, 21 …  Mathematical definition 2COMPSCI Computer.
M180: Data Structures & Algorithms in Java Algorithm Analysis Arab Open University 1.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
Testing with JUnit, and ArraySet costs 2014-T2 Lecture 11 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean,
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
QUICKSORT 2015-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
Algorithm Analysis (Big O)
ANALYSING COSTS COMP 103. RECAP  ArrayList: add(), ensuring capacity, iterator for ArrayList TODAY  Analysing Costs 2 RECAP-TODAY.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Searching Topics Sequential Search Binary Search.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Analysing Costs COMP 103.
© Peter Andreae Java Programs COMP 102 # T1 Peter Andreae Computer Science Victoria University of Wellington.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
1 Algorithms Searching and Sorting Algorithm Efficiency.
CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis Linda Shapiro Winter 2015.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina.
Complexity Analysis (Part I)
Introduction to Analysis of Algorithms
October 2nd – Dictionary ADT
Introduction to complexity
Introduction to Analysing Costs
COMP 103 Testing with JUnit 2016-T2 extras
Introduction to Algorithms
Implementing ArrayList Part 1
Data Structures and Algorithms
Big-Oh and Execution Time: A Review
Building Java Programs
Building Java Programs
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
Algorithmic complexity
Lecture 4: Introduction to Code Analysis
Complexity Analysis (Part I)
Complexity Analysis (Part I)
Presentation transcript:

Testing with JUnit, Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Rashina Hoda, and Peter Andreae COMP 103 Marcus Frean

RECAP  using collections, implementing ArrayList, comparators, iterators TODAY  So.. what’s in the test?  A bit of an aside: Testing, using JUnit – used in Assignment #4  A gentle introduction to analysing costs Announcements:  Assignment#4 out here/now (with all of its pages, as a special treat)  The test is on tomorrow, 10am. Ackroyd – Lee:Kirk 301 Libunao – Riddiford:Hunter 220 Roberts – Zhang:Murphy RECAP-TODAY turn phones off try to sit between empty seats (but not possible for all  ) remember to hand it in!

3 how can we test a program?  Testing a program:  Should test the program as a whole.  Should test all the classes separately  We should probably do both...  Testing a class separately:  Need to create instances of the class and call methods on it  May need set up code to fill the fields of the instance objects.  Should test all the different methods...  Should test methods under different situations...  Should report on the errors it finds.  typically requires writing a test program.

4 eg: testing a Collection  Can have a test program with a main method: create an instance of the other class, and call methods on it. public class ArrayListTest { public static void main(String args) { UI.println("Testing ArrayList…"); List list = new ArrayList (); if (! list.isEmpty() ) UI.println(" *** New list is not empty"); if (list.size()!=0 ) UI.println(" *** New list has a size != 0 "); for (int i=0; i<20; i++) { list.add(i, "v"+i); } for (int i=0; i<20; i++) { if ( ! list.get(i).equals("v"+i) ) UI.printf(" *** %d'th value of list should be v%d", i, i); if ( list.size() != 20 ) UI.println(" *** List size should be 20 after adding 20 values"); :

5 JUnit documentation

6 JUnit makes testing really easy  A special class Assert, with special methods made for testing things: assertTrue("The new list should be empty.", myList.isEmpty()); assertFalse("After adding, the list should not be empty.", myList.isEmpty()); assertEquals("Size should be 1.", myList.size(), 1); assertNull("Item 10 should be null.", myList.get(10));  The 1 st argument is always the message to display if test fails.  Then we can automatically run all the test methods, and report whether they passed or failed.  BlueJ has excellent support for this.

7 The magic incantations At the beginning of the tester program we need: import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; import org.junit.After; import org.junit.runner.JUnitCore; and then before a “setup” method, e.g. make an empty Set. before each test method. We don’t need to but it “cleans up”. See ArraySetTestCore.java (and...Completion.java) for examples.

8 Using JUnit public class ArrayListTest { private List public void initialiseEmptyList() { list = new ArrayList (); public void testIsEmptyOnEmptyList() { assertTrue("A new list should be empty", list.isEmpty()); public void testAdd() { for (int i = 0; i < 20; i++) { String value = "v" + i; list.add(value); assertEquals("Size should be larger after add", (i+1), list.size()); assertEquals("Item should be in list", value, list.get(list.size()-1)); assertFalse("List should not be empty after add", list.isEmpty()); } }

9 BlueJ knows about JUnit tests

10 BlueJ displays the test results:

11 Assignment 4  You have to implement ArraySet  There is a JUnit test (ArraySetTest) to help you test your implementation.  You have to measure how efficient your ArraySet is compared to HashSet, on a Spelling Checker program.  We have given you an implementation of ArrayQueue  It has some errors   You have to write a JUnit test (ArrayQueueTest) to test ArrayQueue (Hint: maybe adapt the one from the ArraySet?) (The tests ought to fail!)

Analysing Costs (in general) How can we determine the costs of a program?  Time:  Run the program and count the milliseconds/minutes/days.  Count number of steps/operations the algorithm will take.  Space:  Measure the amount of memory the program occupies.  Count the number of elementary data items the algorithm stores.  Programs or Algorithms? Both. programs: called “benchmarking” algorithms: called “analysis” 12

Benchmarking: program cost  Measure:  actual programs  on real machines  with specific input  measure elapsed time System.currentTimeMillis () → time from the system clock in milliseconds (long)  measure real memory  Problems:  what input ? ⇒ use large data sets don’t include user input  other users/processes ? ⇒ minimise average over many runs  which computer ? ⇒ specify details 13

Analysis: Algorithm “complexity”  Abstract away from the details of  the hardware  the operating system  the programming language  the compiler  the program  the specific input  Measure number of “steps” as a function of the data size.  best case (easy, but useless)  worst case (easier)  average case (harder)  We could try to construct an expression for the number of steps, for example:  3.47 n n + 53 steps  3n log(n) - 5n + 6 steps 14

Analysis: Notation  We only care about the cost when it is large, and we don’t care about the constant multipliers: 3.47 n n steps  O(n 2 ) 3n log(n) + 12 n steps  O(n log n) Lower-order terms become insignificant for large n Multiplication factors don’t tell us how things “scale up” We care about how cost grows with input size 15

Big-O costs 16  “Asymptotic cost”, or “big-O” cost. describes how cost grows with input size. Examples: O(1) constant cost doesn’t grow with n at all: it’s a fixed cost O(n) linear cost grows linearly with n O(log n) logarithmic cost grows up by 1everytime n doubles itself O(n^2) quadratic costs grows up by n*n with n (bad!!)

Big-O costs 17 NO(1) Constant O(N) Linear O(log N) Logarithmic O(N log N)O(N^2) Quadratic O(N^3) Cubic O(N!) Factorial E E+18 O(N^2)

 We should probably count these:  actions in the innermost loop (happen the most times)  actions that happen every time round (not inside an “if”)  actions involving “data values”(rather than indexes) public E remove (int index){ if (index = count) throw new ….Exception(); E ans = data[index]; for (int i=index+1; i< count; i++) data[i-1]=data[i]; count--; data[count] = null; return ans; } Problem: What is a “step” ? ←Key Step 18

ArrayList: get, set, remove  Assume some List contains n items.  Cost of get and set:  best, worst, average:  ⇒ constant number of steps, regardless of n  Cost of Remove:  worst case: what is the worst case ? how many steps ?  average case: half way what is the average case ? how many steps ? n 19

ArrayList: add (add at some position) public void add(int index, E item){ if (index count) throw new IndexOutOfBoundsException(); ensureCapacity(); for (int i=count; i > index; i--) data[i]=data[i-1]; data[index]=item; count++; }  Cost of add(index, value):  key step?  worst case:  average case: n 20