Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown, 2004-7.

Similar presentations


Presentation on theme: "1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown, 2004-7."— Presentation transcript:

1 1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown, 2004-7

2 2 Program Correctness Correctness is an important aspect of software quality. There are a number of different types of errors: ●Lexical error: using words or symbols not in the language ●Syntax error: using words or symbols in the wrong order ●Semantic error: using constructs in a meaningless way ●Run-time error: your code cannot be executed ●Logical error: your program compiles and runs, but doesn't do what it was meant to do

3 3 What could go wrong? (I) Lexical error: keywords or symbols not in the language ●Misspellings and typos ● total = mutlipy(x, y); ● total <- multiply(x,y); Syntax error: words or symbols in the wrong order ●Example: wrong type of bracket ● total = multiply(x, y): ● total multiply(x,y); picked up at compile time

4 4 What could go wrong? (II) Semantic error: constructs used in the wrong way ● int sum = true; ●calling a method where the class doesn't define that method Run-time error: your code cannot be executed, i.e. crashes ● int values[] = new int[10]; last = values[20]; Logical error: Your program compiles and runs, but doesn't do what it was meant to do ● int values[] = {3,4,5,6,7} //compute the average of the array double ave = (value[0] + values[4]) / 5.0; may be picked up at compile time if lucky may be picked up at compile time if lucky the hardest errors to spot – the language tools give you no help – you must implement test routines to check for logical errors the hardest errors to spot – the language tools give you no help – you must implement test routines to check for logical errors

5 5 A possible Software Development process 1 ? Type the whole program in in one go, off the top of your head Repeat compile and hack away at the compile-time errors until no errors For each example given in assignment sheet run the program while program is not giving correct results hack away at parts that might be causing the problem repeat compile and hack away at the compile-time errors until no errors Add comments as time allows Submit the program 1. suggested by Dr Derek Bridge

6 6 A better process (expanding on the development process from last week) determine the class interface for each item in the interface ●write test-cases in a test method ●write pseudocode method body first ●write Java code ●add test cases ●compile and run test cases

7 7 Testing: the proper attitude ●Goal is destructive - aim is to break the program ●You should assume you will never eliminate all the bugs on a program of any reasonable complexity ●Question: what is testing complete? ●Testing is tedious – nobody likes doing it – so we try to automate it ●Write a test driver for each class ●the test driver should create an object and test the methods on a variety of inputs, including random ones ●a test-driver is reused, so when we change the code, we run the test driver again to see if we have introduced more bugs ●You can use the main method as a test driver, or write another static method, or another class

8 8 Automated Testing ●try to automate it as much as possible ●comparing actual values returned by methods with the required values should be automated ●calculate the required values by hand and hard-code them into the driver ●check some known properties of the actual values ●include code that computes the required value by some other approach ●you will not be able to test every possible permutation ●aim for good coverage – a smaller number of well-targeted tests is better than many redundant ones ●choose test cases that are representative of the data you expect the final program to use ●test using boundary cases – values at the limits of what is acceptable

9 9 Example Class: Fraction Representing rational numbers (i.e. fractions). We need to be able to create them from the numerator (top) and denominator (bottom), compare them, add them, subtract them, multiply them, divide them, and display them. Constructor: ●create with a (integer) numerator and denominator public methods: ●add another fraction and return a fraction ● (and subtract, multiply and divide) ●check if is equal to another fraction and return boolean ●check if is less than another fraction and return boolean ●represent as a string

10 10 The Fraction public interface public class Fraction { public Fraction(int top, int bottom); public String toString(); public boolean isEqualTo(Fraction other); public boolean isLessThan(Fraction other); public Fraction plus(Fraction other); public Fraction minus(Fraction other); public Fraction multiply(Fraction other); public Fraction divideBy(Fraction other); public static void main(String[] args); } Test driver … and now go over each one in turn, writing statements to test it in the test driver.

11 11 Constructor and toString() /** * Creates a new Fraction object with numerator and * denominator. Either input may be negative * @param top the numerator * @param bottom the denominator; cannot be 0 */ public Fraction(int top, int bottom) Testing the Constructor (and other methods) will be made easier if we specify the toString() method: /** * Returns a String representation of the Fraction * in normal form: i.e. returns "2/3" and never "4/6". * @return a String representation */ public String toString()

12 12 Starting the testDriver /** * Test driver. Displays results to standard output. */ public static void main(String[] args) { Fraction f; // Testing the constructor and toString f = new Fraction(2,3); if (! (f.toString().equals("2/3"))) System.out.println("Error: " + f + " should be 2/3"); f = new Fraction(4,6); if (! (f.toString().equals("2/3"))) System.out.println("Error: " + f + " (input = (4,6)" + " should be 2/3"); } Other test cases might include e.g. 3/2, -2/3, -4/6, -3/2, 6/4, -6/4, 3/1, -3/1, 0/1, 3/3, -3/3, 1/1, -1/1, 0/3, 2/-3, -2/-3, 1/0, 3/0, -3/0, 0/0

13 13 Continuing the interface: plus public Fraction plus(Fraction other) //minus, multiply, divide are similar /** * Returns a new Fraction that represents the sum of this * Fraction and the input Fraction. * @param other the Fraction we are adding; must not be null * @return a Fraction representing the sum */

14 14 Continuing the test driver f1 = new Fraction(1,2); f2 = new Fraction(1,3); f3 = new Fraction(5,6); System.out.println("Test: " + f1 + " + " + f2 + " == " + f3); if (! (f1.plus(f2).toString().equals(f3.toString()))) System.out.println("Error!"); //exploiting mathematical properties: // f1 + f2 – f1 == f2 System.out.println("Test: f1 + f2 – f1 == f2"); if (! (f1.plus(f2).minus(f1).toString().equals(f2.toString()))) System.out.println("Error!);

15 15 Interface (cont): isEqualTo() public boolean isEqualTo(Fraction other) //lessThan is similar /** * Determines whether the input Fraction is equal to this one. * True if and only if input Fraction is not null, and its * reduced form is identical to the reduced form of this one. * @param other the input Fraction to be compared * @return true if mathematically equal; false otherwise */

16 16 Improving the test driver f1 = new Fraction(1,2); f2 = new Fraction(1,3); f3 = new Fraction(5,6); f4 = new Fraction(2,6); System.out.println("Test: 1/3 == 2/6"); if (! (f2.isEqualTo(f4))) System.out.println("Error!"); System.out.println("Test: " + f1 + " + " + f2 + " == " + f3); if (! (f1.plus(f2).isEqualTo(f3))) System.out.println("Error!"); //exploiting mathematical properties: // f1 + f2 – f1 == f2 System.out.println("Test: f1 + f2 – f1 == f2"); if (! (f1.plus(f2).minus(f1).isEqualTo(f2))) System.out.println("Error!);

17 17 Using Random Tests System.out.println("Test: random testing"); for (int i = 0; i<1000; i++) { int r1 = (int)(Math.random() * 100 – 50); int r2 = (int)(Math.random() * 100 – 50); int r3 = (int)(Math.random() * 100 – 50); int r4 = (int)(Math.random() * 100 – 50); if (r2 != 0 && r4 != 0) { f1 = new Fraction(r1,r2); f2 = new Fraction(r3,r4); if (! (f1.plus(f2).minus(f1).isEqualTo(f2))) System.out.println("Error! " + f1 + "::" + f2); }


Download ppt "1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown, 2004-7."

Similar presentations


Ads by Google