Testing Recap Testing can find problems, but cannot prove your program works Since exhaustive testing is impossible, select test cases with maximum likelihood.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Abstraction and Modular Reasoning for the Verification of Software Corina Pasareanu NASA Ames Research Center.
David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 2: Java Semantics, Validation.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
Cs205: engineering software university of virginia fall 2006 Validation David Evans
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
1 Assertions. 2 assertions communicate assumptions about the state of the program, and stop processing if they turn out to be false very often comments.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
David Evans These slides: Introduction to Static Analysis.
Mixing integer and floating point numbers in an arithmetic operation.
Chapter 3 Part II Describing Syntax and Semantics.
Programming with Assertions © Allan C. Milne v
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
1 CS November Testing/Debugging. And Applications Read chapter 14, pp. 385–401 Prelim 2 Max 100 Median 82 Mean 78.3 Min * ***************************
Programs That Calculate. Arithmetic Expressions +addition -subtruction *multiplication /division  Order of Operations BEDMAS (brackets, exponentiation,
The Java Assertion. 2 Assertion A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates the a particle’s.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Cs205: engineering software university of virginia fall 2006 Programming Exceptionally David Evans
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Object Oriented Programming Lecture 2: BallWorld.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
CSE 143 Lecture 1 Arrays (review) slides created by Marty Stepp
Copyright © 2016 Curt Hill Static Code Analysis What it is and does.
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
CS0007: Introduction to Computer Programming
Numbers How does computer understand numbers? Knows only two symbols
Static Code Analysis What it is and does. Copyright © 2016 Curt Hill.
Chapter 6 CS 3370 – C++ Functions.
Logger, Assert and Invariants
Introduction to Computer Science / Procedural – 67130
CSE 374 Programming Concepts & Tools
Important terms Black-box testing White-box testing Regression testing
Building Java Programs
Important terms Black-box testing White-box testing Regression testing
An Introduction to Java – Part I, language basics
Programming in Java Assertion.
Building Java Programs
Building Java Programs
CMSC 202 Exceptions 2nd Lecture.
Lecture 9: Arrays Building Java Programs: A Back to Basics Approach
class PrintOnetoTen { public static void main(String args[]) {
LCC 6310 Computation as an Expressive Medium
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Additional control structures
Foundations and Definitions
Outline Software Development Activities
Building Java Programs
Computer Science 340 Software Design & Testing
CSS161: Fundamentals of Computing
First Semester Review.
Lecture 2: Java Semantics, Validation CS201j: Engineering Software?
Defensive Programming
Presentation transcript:

Testing Recap Testing can find problems, but cannot prove your program works Since exhaustive testing is impossible, select test cases with maximum likelihood of finding bugs A successful test case is one that reveals a bug in your program! Typically at least 40% of cost of software project is testing, often >80% of cost for safety-critical software

Is it really hopeless? Since we can’t test all possible paths through a program, how can we increase our confidence that it works?

Static Analysis Goal: Make claims about all possible paths by examining the program code directly Testing (dynamic analysis): checks exactly one program path. Not good. Static analysis: reasons about all possible program paths Use formal semantics of programming language to know what things mean Use formal specifications of procedures to know that they do

Hopelessness of Analysis It is impossible to correctly determine if any interesting property is true for an arbitrary program! The Halting Problem: it is impossible to write a program that determines if an arbitrary program halts.

Compromises Use imperfect automated tools: Accept unsoundness and incompleteness False positives: sometimes an analysis tool will report warnings for a program, when the program is actually okay (unsoundness) False negatives: sometimes an analysis tool will report no warnings for a program, even when the program violates properties it checks (incompleteness) Java compiler warnings attempt to do this

Dealing with Hopelessness Since both testing and analysis are hopeless in general what can we do? Design for Testability Design for Analyzability

Programming Defensively

Assertions Statement ::= assert booleanExpression optStringExpression; [any Java expression that evaluates to a boolean value] optStringExpression ::=  | : stringExpression stringExpression ::= [any Java expression that can be converted to a String value] Semantics: To evaluate an assert statement, evaluate the booleanExpression. If the booleanExpression evaluates to true, do nothing. If it is false, the assertion fails and an AssertionException thrown. If there is an optional stringExpression, it is evaluated (and converted to a String) and included in the AssertionException.

Enabling Assertions Without this, assert does nothing!

Examples 0.75 Exception in thread "main" java.lang.AssertionError public class TestClass { public static double divide(int a, int b) { assert b != 0; return (double) a / b; } public static void main(String[] args) { System.out.println (divide (3, 4)); System.out.println (divide (3, 0)); 0.75 Exception in thread "main" java.lang.AssertionError at ps3.TestClass.divide(TestClass.java:6) at ps3.TestClass.main(TestClass.java:16)

Examples public class TestClass { public static double divide(int a, int b) { assert b != 0 : "Division by zero"; return (double) a / b; } public static void main(String[] args) { System.out.println (divide (3, 4)); System.out.println (divide (3, 0)); 0.75 Exception in thread "main" java.lang.AssertionError: Division by zero at ps3.TestClass.divide(TestClass.java:6) at ps3.TestClass.main(TestClass.java:16)

Tricky Example public static double divide(int a, int b) { assert b != 0 : divide(a, b); return (double) a / b; } public static void main(String[] args) { System.out.println (divide (3, 4)); System.out.println (divide (3, 0)); 0.75 Exception in thread "main" java.lang.StackOverflowError at ps3.TestClass.divide(TestClass.java:6) …

Where should we use assert? public static int [] histogram (int [] a) { int maxval = 0; for (int i = 0; i < a.length; i++) { if (a[i] > maxval) { maxval = a[i]; } int histo [] = new int [maxval + 1]; histo[a[i]]++; return histo; To give useful debugging information when a REQUIRES precondition is violated. To check assumptions on which our code relies. Judicious use of asserts: saves debugging time provides useful documentation increases confidence in results

How many assertions? Gunnar Kudrjavets, Nachiappan Nagappan, Thomas Ball. Assessing the Relationship between Software Assertions and Code Quality: An Empirical Investigation http://research.microsoft.com/pubs/70290/tr-2006-54.pdf >150 assertions per 1000 lines of code About 15% of the statements in a good Java program should be asserts!

Rewind: Analysis Goal: Make claims about all possible paths by examining the program code directly Testing (dynamic analysis): checks exactly one program path. Not good. Static analysis: reasons about all possible program paths Use formal semantics of programming language to know what things mean Use formal specifications of procedures to know that they do