Exceptions and Testing Michael Ernst Saman Amarasinghe

Similar documents
Who is our rival? Upcoming. Testing. Ariane 5 rocket (1996) Ariane 5 rocket 3/8/18. Real programmers need no testing!

5) I want to get this done fast, testing is going to slow me down.

Therac-25 radiation therapy machine

Testing. CSE 331 University of Washington. Michael Ernst

CSE 331 Software Design & Implementation

CSE 403: Software Engineering, Fall courses.cs.washington.edu/courses/cse403/16au/ Unit Testing. Emina Torlak

Beta. Team Assessment. Exam. Exam stats. Things to remember. TesAng 3/23/15

CSE 331 Software Design & Implementation

Announcements. Testing. Announcements. Announcements

Course&updates& Tes=ng& Ariane&5&rocket& Mars&Polar&Lander& Therac[25&radia=on&therapy&machine& 4/3/17& Real%programmers%need%no%tes/ng!

Regression testing. Whenever you find a bug. Why is this a good idea?

Exceptions and assertions

CSE 331 Software Design & Implementation

Testing and Debugging

Specifications. CSE 331 Spring 2010

Testing. Prof. Clarkson Fall Today s music: Wrecking Ball by Miley Cyrus

Lecture 4 Specifications

Violations of the contract are exceptions, and are usually handled by special language constructs. Design by contract

CSE 331 Software Design and Implementation. Lecture 4 Specifications

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

CS 3 Introduction to Software Engineering. 3: Exceptions

Software Engineering

CSE 331 Midterm Exam Sample Solution 2/13/12

Steps for project success. git status. Milestones. Deliverables. Homework 1 submitted Homework 2 will be posted October 26.

Lecture 10 Assertions & Exceptions

CS 3 Introduction to Software Engineering. 5: Iterators

Topics in Software Testing

Lecture 12 Assertions & Exceptions

CSE 331 Software Design & Implementation

CSE wi Midterm Exam 2/8/18. Name UW ID #

Program Correctness and Efficiency. Chapter 2

Comparing procedure specifications

Unit Testing as Hypothesis Testing

Software Engineering Testing and Debugging Testing

CS 520 Theory and Practice of Software Engineering Fall 2018

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

ITI Introduction to Computing II

CSE331 Winter 2014, Midterm Examination February 12, 2014

Object Oriented Programming Exception Handling

ITI Introduction to Computing II

Classes, interfaces, & documentation. Review of basic building blocks

No Source Code. EEC 521: Software Engineering. Specification-Based Testing. Advantages

CSE wi Midterm Exam 2/8/18 Sample Solution

Programming II (CS300)

References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 10/14/2004 1

Lecture 10: Introduction to Correctness

CSE 331 Midterm Exam 2/13/12

Equivalence Class Partitioning. Equivalence Partitioning. Definition and Example. Example set of classes

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Programming II (CS300)

Testing. CMSC 433 Programming Language Technologies and Paradigms Spring A Real Testing Example. Example (Black Box)?

Testing! Prof. Leon Osterweil! CS 520/620! Spring 2013!

Black Box Testing. EEC 521: Software Engineering. Specification-Based Testing. No Source Code. Software Testing

Black-box Testing Techniques

CSE 143. Programming is... Principles of Programming and Software Engineering. The Software Lifecycle. Software Lifecycle in HW

CSE 331 Software Design and Implementation. Lecture 12 Assertions & Exceptions

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

Assertions and Exceptions Lecture 11 Fall 2005

Representation Invariants and Abstraction Functions

Pages and 68 in [JN] conventions for using exceptions in Java. Chapter 8 in [EJ] guidelines for more effective use of exceptions.

Coding and Unit Testing! The Coding Phase! Coding vs. Code! Coding! Overall Coding Language Trends!

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 15 Testing

"Secure" Coding Practices Nicholas Weaver

Chapter 11, Testing. Using UML, Patterns, and Java. Object-Oriented Software Engineering

(See related materials in textbook.) CSE 435: Software Engineering (slides adapted from Ghezzi et al & Stirewalt

CSE 331 Software Design and Implementation. Lecture 14 Generics 2

Exception-Handling Overview

Testing! The material for this lecture is drawn, in part, from! The Practice of Programming (Kernighan & Pike) Chapter 6!

MITOCW watch?v=9h6muyzjms0

An Introduction to Systematic Software Testing. Robert France CSU

Lecture 15 Software Testing

So far. Specifications, conclusion. Abstract Data Types (ADTs) Comparison by Logical Formulas. Outline

Checked and Unchecked Exceptions in Java

Assertions, pre/postconditions

Programming Embedded Systems

Announcements. Lecture 15 Generics 2. Announcements. Big picture. CSE 331 Software Design and Implementation

CSE 331 Software Design and Implementation. Lecture 15 Generics 2

Testing. Christopher Simpkins Chris Simpkins (Georgia Tech) CS 2340 Objects and Design CS / 13

Assertions. Assertions - Example

Unit Testing as Hypothesis Testing

Verification Overview Testing Theory and Principles Testing in Practice. Verification. Miaoqing Huang University of Arkansas 1 / 80

Overview. State-of-the-Art. Relative cost of error correction. CS 619 Introduction to OO Design and Development. Testing.

Chapter 15. Exception Handling. Chapter Goals. Error Handling. Error Handling. Throwing Exceptions. Throwing Exceptions

CS 4387/5387 SOFTWARE V&V LECTURE 4 BLACK-BOX TESTING

Type Hierarchy. Comp-303 : Programming Techniques Lecture 9. Alexandre Denault Computer Science McGill University Winter 2004

CONTENTS: While loops Class (static) variables and constants Top Down Programming For loops Nested Loops

Chapter 9. Software Testing

Abstraction and Specification

17. Assertions. Outline. Built-in tests. Built-in tests 3/29/11. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

17. Assertions. Jelle Slowack, Bart Smets, Glenn Van Loon, Tom Verheyen

Testing. UW CSE 160 Winter 2016

CSE 143. More ArrayIntList; pre/post; exceptions; debugging. Computer Programming II. CSE 143: Computer Programming II

Adam Blank Lecture 3 Autumn 2016 CSE 143. Computer Programming II

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

COMP-202 Unit 4: Programming with Iterations

CSCI 261 Computer Science II

CS159. Nathan Sprague. September 30, 2015

Transcription:

Exceptions and Testing 6.170 Michael Ernst Saman Amarasinghe

Generic Types Bugfix to the last lecture public class List<T> { private T lst[]; public void add(int, T) { } public T get(int) { } } List<Integer> lst = new List<Integer>(); Integer fst = lst.get(0);

ow to handle abnormal situations? public static int Search(int[] lst, int key) requires: none modifies: none effects: none returns: some i such that lst[i] = key if such an i exists, otherwise -1 Returning {(insertion point), - 1} is very ugly, and an invitation to bugs and confusion.

ow to handle abnormal situations? public static int Search(int[] lst, int key) requires: key is in lst modifies: none effects: none returns: i where lst[i] = key Partial procedures make the world more complicated. Who is going to check if key is in a? Too much burden on the client.

se an exception public static int Search(int[] lst, int key) requires: none modifies: none effects: throws: NoSuchElementException if key is not in lst returns: some i such that lst[i] = key if such an i exists

How to use an Exception Declaration: public static int Search(int[] lst, int key) throws NoSuchElementException { for(int i = 0; i < lst.length; i++) if(lst[i] == k) return i; throw new NoSuchElementException(); } Use: try { j = s.search(mylist, k); } catch (NoSuchElementException e) { Handle the exception }... continue for both normal and exceptional cases

Catching Exceptions Caught by catch associated with nearest enclosing try If no such catch Exception propagated up call stack If not caught at all Program terminates

Two Kinds of Exceptions Checked exceptions E.g., class MissingException extends Exception Compiler error unless There exists a catch clause, or Caller declared to throw that exception There is guaranteed to be a dynamically enclosing catch Unchecked exceptions E.g., class ArithmeticException extends RuntimeException Compiler doesn t complain Rule of thumb Stick to checked exceptions most of the time Use unchecked exceptions to mean failure Expect program to terminate

Why Catch Exceptions Locally? Failure to catch exceptions violates modularity Call chain: A IntegerSet.insert IntegerList.insert IntegerList.insert throws an exception Implementer of IntegerSet.insert knows how list is being used Implementor of A may not even know that IntegerList exists Procedure up the line may think that it is handling an exception raised by a different call Even if exception is better handled up a level May be better to catch it and throw it again ( chaining ) Makes it clear to reader of code that it was not an omission

Exceptions in Review Use an exception when Used in a broad or unpredictable context Checking the condition is feasible Use a precondition when Checking would be prohibitive E.g., requiring that a list be sorted Used in a narrow context in which calls can be checked Preconditions should be avoided because Caller may violate precondition Program can fail in uninformative or dangerous way Want program to fail as early as possible

Exceptions in Review, cont. Use checked exceptions most of the time Handle exceptions sooner rather than later Don t think of them as errors A program structuring mechanism Used for exceptional (unpredictable) circumstances Documentation standards and conventions Will be covered in recitation Also see Bloch s Effective Java

Building Quality Software What Impacts the Software Quality? External Correctness Does it do what it suppose to do? Reliability Does it do it accurately all the time? Efficiency Does it do with minimum use of resources? Integrity Is it secure? Internal Portability Can I use it under different conditions? Maintainability Can I fix it? Flexibility Can I change it or extend it or reuse it? Quality Assurance The process of uncovering problems and improving the quality of software. Testing is a major part of QA.

The Phases of Testing Unit Testing Is each module does what it suppose to do? Integration Testing Do you get the expected results when the parts are put together work? Validation Testing Does the program satisfy the requirements System Testing Does it work within the overall system

Unit Testing A test is at the level of a method/class/interface Check if the implementation matches the specification. Black box testing Choose test data without looking at implementation Glass box (white box) testing Choose test data with knowledge of implementation

ow is testing done? Basic steps of a test 1) Choose input data/configuration 2) Define the expected outcome 3) Run program/method against the input and record the results 4) Examine results against the expected outcome

What s So Hard About Testing? "just try it and see if it works..." int proc1(int x, int y, int z) // requires: 1 <= x,y,z <= 1000 // effects: computes some f(x,y,z) Exhaustive testing would require 1 billion runs! Sounds totally impractical Could see how input set size would get MUCH bigger Key problem: choosing test suite (set of partitions of inputs) Small enough to finish quickly Large enough to validate the program

Approach: Partition the Input Space Input space very large, program small ==> behavior is the same for sets of inputs Ideal test suite: Identify sets with same behavior Try one input from each set Two problems 1. Notion of the same behavior is subtle Naive approach: execution equivalence Better approach: revealing subdomains 2. Discovering the sets requires perfect knowledge Use heuristics to approximate cheaply

Naive Approach: Execution Equivalence int abs(int x) { // returns: x < 0 => returns -x // otherwise => returns x } if (x < 0) return -x; else return x; All x < 0 are execution equivalent: program takes same sequence of steps for any x < 0 All x >= 0 are execution equivalent Suggests that {-3, 3}, for example, is a good test suite

Why Execution Equivalence Doesn't Work Consider the following buggy code: int abs(int x) { // returns: x < 0 => returns -x // otherwise => returns x } if (x < -2) return -x; else return x; Two executions: x < 2 x >= 2 Three behaviors: x < 2 (OK) x = 2 or 1 (bad) x >= 0 (OK) {-3, 3} does not reveal the error!

Revealing Subdomain Approach Same behavior depends on specification Say that program has same behavior on two inputs if 1) gives correct result on both, or 2) gives incorrect result on both Subdomain is a subset of possible inputs Subdomain is revealing for an error, E, if 1) Each element has same behavior 2) If program has error E, it is revealed by test Trick is to divide possible inputs into sets of revealing subdomains for various errors

Example For buggy abs,what are revealing subdomains? int abs(int x) { if (x < -2) return -x; else return x; } { 1} { 2} { 2, 1} { 3, 2, 1} { 2, 1} Which is best?

Heuristics for Designing Test Suites A good heuristic gives: few subdomains errors e in some class of errors E, high probability that some subdomain is revealing for e Different heuristics target different classes of errors In practice, combine multiple heuristics

Black Box Testing Heuristic: Explore alternate paths through specification Procedure an interface is a black box, internals hidden Example int max(int a, int b) // effects: a > b => returns a // a < b => returns b // a = b => returns a 3 paths, so 3 test cases: (4, 3) => 4 (i.e. any input in the subdomain a > b) (3, 4) => 4 (i.e. any input in the subdomain a < b) (3, 3) => 3 (i.e. any input in the subdomain a = b)

Black Box Testing: Advantages Process not influenced by component being tested Assumptions embodied in code not propagated to test data. Robust with respect to changes in implementation Test data need not be changed when code is changed Allows for independent testers Testers need not be familiar with code

More Complex Example Write test cases based on paths through the specification int find(int[] a, int value) throws Missing // returns: the smallest i such // that a[i] == value // throws: Missing if value not in a Two obvious tests: ( [4, 5, 6], 5 ) => 1 ( [4, 5, 6], 7 ) => throw Missing Have I captured all the paths? ( [4, 5, 5], 5 ) => 1 Must hunt for multiple cases in effects or requires

Heuristic: Boundary Testing Create tests at the edges of subdomains Why do this? off-by-one bugs forget to handle empty container overflow errors in arithmetic program does not handle aliasing of objects Small subdomains at the edges of the main subdomains have a high probability of revealing these common errors

Boundary Testing To define boundary, must define adjacent points One approach: Identify basic operations on input points Two points are adjacent if one basic operation away A point is isolated if can t apply a basic operation Example: list of integers Basic operations: create, append, remove Adjacent points: <[2,3],[2,3,3]>, <[2,3],[2]> Isolated point: [] (can t apply remove integer) Point is on a boundary if either There exists an adjacent point in different subdomain Point is isolated

Other Boundary Cases Arithmetic Smallest/largest values Zero Objects Null Circular Same object passed to multiple arguments (aliasing)

oundary cases: Arithmetic Overflow public int abs(int x) // returns: x Tests for abs what are some values or ranges of x that might be worth probing? x < 0 (flips sign) or x 0 (returns unchanged) around x = 0 (boundary condition) Specific tests: say x = -1, 0, 1 How about int x = -2147483648; // this is Integer.MIN_VALUE System.out.println(x<0); // true System.out.println(Math.abs(x)<0); // also true! From Javadoc for Math.abs: Note that if the argument is equal to the value of Integer.MIN_VALUE, the most negative representable int value, the result is that same value, which is negative

oundary cases: duplicates and aliases <E> void appendlist(list<e> src, List<E> dest) { // modifies: src, dest // effects: removes all elements of src and // appends them in reverse order to // the end of dest } while (src.size()>0) { E elt = src.remove(src.size()-1); dest.add(elt) } What happens if src and dest refer to the same thing? This is aliasing often forgotten Watch out for shared references in inputs

Glass-box Testing Goal: Ensure test suite covers (executes) all of the program Measure quality of test suite with % coverage Assumption: high coverage (no errors in test suite output few mistakes in the program) Focus: features not described by specification Control-flow details Performance optimizations Alternate algorithms for different cases

lass-box motivation There are some subdomains that black-box testing won't give: boolean[] primetable = new boolean[cache_size]; boolean isprime(int x) { if (x>cache_size) { for (int i=2; i<x/2; i++) { if (x%i==0) return false; } return true; } else { return primetable[x]; } } Important transition around x = CACHE_SIZE

Glass Box Testing: Advantages Insight into test cases Which are likely to yield new information Finds an important class of boundaries Consider CACHE_SIZE in isprime example Need to check numbers on each side of CACHE_SIZE CACHE_SIZE-1, CACHE_SIZE, CACHE_SIZE+1 If CACHE_SIZE is mutable, we may need to test with different CACHE_SIZE s

Glass-box Challenges Definition of all of the program What needs to be covered? Options: Statement coverage Decision coverage Loop coverage Condition/Decision coverage Path-complete coverage increasing number of Cases (more or less) 100% coverage not always reasonable target 100% may be unattainable (dead code) High cost to approach the limit

ragmatics: regression testing Whenever you find and fix a bug Store input that elicited that bug Store correct output Put into test suite Why is this a good idea Helps to populate test suite with good tests Protects against reversions that reintroduce bug Arguably is an easy error to make (happened at least once, why not again?)

Rules of Testing First rule of testing: Do it early and do it often Best to catch bugs soon, before they have a chance to hide. Automate the process if you can Regression testing will save time. Second rule of testing: Be systematic If you randomly thrash, bugs will hide in the corner until you're gone Writing tests is a good way to understand the spec Think about revealing domains and boundary cases If the spec is confusing write more tests Spec can be buggy too Incorrect, incomplete, ambiguous, and missing corner cases When you find a bug fix it first and then write a test for it

ummary Testing matters You need to convince others that module works Catch problems earlier Bugs become obscure beyond the unit they occur in Don't confuse volume with quality of test data Can lose relevant cases in mass of irrelevant ones Look for revealing subdomains Choose test data to cover Specification (black box testing) Code (glass box testing) Testing can't generally prove absence of bugs But can increase quality by reducing the bugs