what/how do we care about a program? robustness correctness efficiency (speed, space) 11/2/20151it...

Download What/how do we care about a program? Robustness Correctness Efficiency (speed, space) 11/2/20151IT 179  Software Testing  Error Handling  Efficiency

Post on 04-Jan-2016

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • What/how do we care about a program? Robustness

    CorrectnessEfficiency (speed, space)**IT 179 Software Testing Error Handling Efficiency Analysis

    IT 179

  • Things that can go wrongLogic error The program is incorrect

    Environment error - e.g. out of memory

    I/O error e.g., lost network connection.

    **IT 179Lets handle it!! Beyond programmers control

    IT 179

  • ExceptionsThrowing Exceptions is Javas way of telling you something has gone wrong

    When an exceptional condition occurs, an exception object is created storing information about the nature of the exception (kind, where it occurred, etc.). When this happens, we say that an exception is thrown.

    The JVM looks for a block of code to catch and handle the exception (do something with it)In Java, they are classes**IT 179

    IT 179

  • Exception throwingThe sequence of callingThe sequence of throwingexceptionsXbad thing happensJVM starts application at main()main() calls method a()method a method b()method b method c()method c exception occursIf the exception cannot be handled here, it will continue to throw back**IT 179

    IT 179

  • Categories of exceptionsChecked exceptions descended from class Exception, but outside the hierarchy rooted at RuntimeException. The compiler will check that you either catch or re-throw checked exceptions.

    Unchecked exceptions (aka Runtime Exception ) represent the kinds of errors your program can avoid through careful programming and testing. The compile does not check to see that you handle these exceptions.These categorization affect compile-time behavior only These represent some error, not programmers fault, but the programmer can (should) do something about itThey are programmers fault, the compiler cantdo a thing. **IT 179

    IT 179

  • Unchecked Exceptions handle:Logic error The program is incorrect

    Environment error - e.g. out of memory

    I/O error e.g., lost network connection.

    **IT 179Checked Exceptions handle:

    IT 179

  • Javas Exception HierarchyUncheckedChecked

    IT 179

  • Try-Catch-Finally Blockstry { program statements; some of which may throw an exception} catch ( ExceptionType1 exception ) { program statements to handle exceptions of type ExceptionType1 or any of its subclasses}catch ( ExceptionType2 exception ) { program statements to handle exceptions of type ExceptionType2 or any of its subclasses}. . . other catch clausescatch ( ExceptionTypeN exception ) { program statements to handle exceptions of type ExceptionTypeN or any of its subclasses}finally { this block is optional; this block will execute whether or not an exception is thrown}

    IT 179

  • public class HandleExceptions { public static void main(String[] args) { System.out.println("Testing Exception Handling\n"); try { int i=1,j=2;

    Exception myEx=null;

    String s="0.2"; i=Integer.parseInt(s); if (i > j) throw new RuntimeException(); int[] A = new int[5]; i=1; if (i4) throw new IndexOutOfBoundsException(); i = 5; i = A[i]; } catch (ArithmeticException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace();catch (NullPointerException ex) { System.out.println("I'm here 12345"); System.out.println("\n"+ex.toString()); ex.printStackTrace(); }catch (NumberFormatException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (IllegalArgumentException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (ArrayIndexOutOfBoundsException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (IndexOutOfBoundsException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (RuntimeException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } finally { System.out.println("\nWe are done. Finally!!"); } }}**IT 179

    IT 179

  • Correctness Software TestingYou should test throughout the software development cycle, namely, every stages:

    After the analysis and design are completedDuring the implementation (test driven development) (Write a little, test a little)After the implementation is completed**IT 179

    IT 179

  • An algorithms performance can be measured by:

    Time complexity

    Space complexity**IT 179How long it takes to execute. How much additional space the algorithm needs. (memory, storage, tape). Efficiency (speed, space) Efficiency Analysis

    IT 179

  • Time Complexity: Count InstructionsHow many timeswill each instructionexecute?**IT 179

    StatementsCost1double average( int []a, int n ) {2 double sum = 0;13 int count = 0;14 while ( count < n ) {n + 15 sum += a[count];n6 count++;n7 }8 if ( n > 0 )19 return sum / n;110 else11 return 0.0f;12}TOTAL 3n + 5

    IT 179

  • asymptotic approach

    for (int i=0; i< n; i++) { .......... for (int j=i; j < n; j++) { ............ } .........}n+(n-1)+(n-2)+......+3+2+1

    = n(n+1)/2

    = ()(n2 +n) n2**IT 179

    IT 179

  • Consider the worst part of the program

    if (....) { ......... .........} else { ......... .........}n2n3 n3**IT 179

    IT 179

  • Big-O notation in Algorithm Analysis

    for (int i=0; i< n; i++) { ................ ................}

    for (int i=0; i< n; i++) { .......... for (int j=0; j< n; j++) { ............ for (int k=0; k

  • Linear Search**IT 179

    StatementsCost1int linearSearch( int [] a, int n, int target ) {2 int i = 0;13 while ( i < n ) {n + 14 if ( target == a[i] )n 5 return i;16 i++;n7 }8 return 1;19}TOTAL3n + 3

    IT 179

  • *IT 179*// select the minimum between a[s] to the end// and return the index of the minimum//private int min(int a[], int s){ int m = s;

    for (int i=s; i < a.length;i++) { if (a[i] < a[m]) m=i; } return m;}

    // exchange the contents of a[i] and a[j]// private void exchange(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp;}O(n-s)=O(n)where n is the size of the arrayO(3) = O(1)

    IT 179

  • *IT 179*Selection Sortpublic void sort(int a[]) { // select right element for a[i] for (int i = 0; i
  • **IT 179Logarithm (binary search)

    Linear (sequential search)

    n log n (quick sort)

    Quadratic (n2, selection sort, insertion sort)

    Polynomial (Primality test)

    Nondeterministic polynomial (Traveling Salesman problem)

    Exponential (en Backtracking)

    IT 179

  • Big-Oh (): Linear SearchTlinearSearch(n) = 3n + 3 = (n) for c = 4 and n0 = 0, in the worst case.4nn23n + 3TlinearSearch(n) = (n2)But this is an abuse ofO-notation**IT 179

    IT 179

  • Some Common Computing Times**IT 179

    log2nnn log2nn22n1224 424816 16382464 25641664256 65,5365321601,0244,294,967,2966643844,0961.84 1019 712889616,3843.40 103882562,04865,5361.16 107795124,608262,1441.34 10154101,02410,2401,048,5761.80 10308

    IT 179

  • Most commonly used Asymptotic notations Big-O notation: O(f) Big- notation: (f) Big- notation: (f) Little-o notation, o(f)There are collections of functions with certain asymptotic properties.

    **IT 179

    IT 179

  • Asymptotic notation(for all but finitely many) Upper bound: Big-O notation, O(f)g O(f) iff c n0 n [n n0 g(n) cf(n)] Lower bound: Big- notation, (f)g (f) iff c n0 n [n n0 g(n) cf(n)] Approximation: Big- notation, (f)g (f) iff c1 c2 n0 n [n n0 c2f(n)] g(n) c2f(n)]equivalently g O(f) and g (f) Tied Upper bound: Little-o notation, o(f)g o(f) iff c n0 n [n n0 g(n) cf(n)]**IT 179

    IT 179

    *