Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Testing with JUnit, Introduction to Analysing Costs 2013-T2 Lecture 10 School of Engineering and Computer Science, Victoria University of Wellington "— Presentation transcript:

1 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

2 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 220 2 RECAP-TODAY turn phones off try to sit between empty seats (but not possible for all  ) remember to hand it in!

3 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 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 5 JUnit documentation

6 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 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” before a “setup” method, e.g. make an empty Set. “@Test” before each test method. We don’t need to use @After, but it “cleans up”. See ArraySetTestCore.java (and...Completion.java) for examples.

8 8 Using JUnit public class ArrayListTest { private List list; @Before public void initialiseEmptyList() { list = new ArrayList (); } @Test public void testIsEmptyOnEmptyList() { assertTrue("A new list should be empty", list.isEmpty()); } @Test 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 9 BlueJ knows about JUnit tests

10 10 BlueJ displays the test results:

11 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!)

12 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

13 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

14 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 2 - 67n + 53 steps  3n log(n) - 5n + 6 steps 14

15 Analysis: Notation  We only care about the cost when it is large, and we don’t care about the constant multipliers: 3.47 n 2 + 102n + 10064 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

16 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!!)

17 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 1510.00 111 5552.3211.6125125120 105 3.3233.2210010003628800 155 3.9158.6022533751.31E+12 205 4.3286.4440080002.43E+18 O(N^2)

18  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

19 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

20 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


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

Similar presentations


Ads by Google