Testing and Debugging

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

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

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

Program Correctness and Efficiency. Chapter 2

Absolute C++ Walter Savitch

Test-Driven Development (a.k.a. Design to Test) CSE260, Computer Science B: Honors Stony Brook University

Iteration Abstraction

CSC Advanced Object Oriented Programming, Spring Specification

Testing and Debugging

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

Unit Testing as Hypothesis Testing

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Assertions, pre/postconditions

MSO Lecture Design by Contract"

Problem Solving with C++

Lecture 1 Contracts : Principles of Imperative Computation (Fall 2018) Frank Pfenning

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

Learning outcomes. Systems Engineering. Debugging Process. Debugging Process. Review

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

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

Software Testing Prof. Meenakshi D Souza Department of Computer Science and Engineering International Institute of Information Technology, Bangalore

Announcements. Testing. Announcements. Announcements

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Verification and Validation. Verification and validation

Lecture 1 Contracts. 1 A Mysterious Program : Principles of Imperative Computation (Spring 2018) Frank Pfenning

CSE 331 Software Design & Implementation

Topics in Software Testing

CS 3 Introduction to Software Engineering. 3: Exceptions

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

Assertions. Assertions - Example

CSE 374 Programming Concepts & Tools. Brandon Myers Winter 2015 Lecture 11 gdb and Debugging (Thanks to Hal Perkins)

Top Down Design vs. Modularization

CSE 374 Programming Concepts & Tools

Unit Testing as Hypothesis Testing

Testing. UW CSE 160 Winter 2016

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Procedural Abstraction

LECTURE 0: Introduction and Background

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

Test suites Obviously you have to test your code to get it working in the first place You can do ad hoc testing (testing whatever occurs to you at

Course Content. Objectives of Lecture 18 Black box testing and planned debugging. Outline of Lecture 18

Assertions and Exceptions Lecture 11 Fall 2005

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

6.170 Lecture 6 Procedure specifications MIT EECS

Exceptions and Testing Michael Ernst Saman Amarasinghe

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

Review sheet for Final Exam (List of objectives for this course)

Analysis of the Test Driven Development by Example

Exceptions and assertions

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

G Programming Languages - Fall 2012

In this Lecture you will Learn: Testing in Software Development Process. What is Software Testing. Static Testing vs.

CSE 331 Summer 2016 Final Exam. Please wait to turn the page until everyone is told to begin.

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

Object Oriented Programming

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

CMPSCI 187 / Spring 2015 Postfix Expression Evaluator

Testing, Debugging, Program Verification

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

CS61B Lecture #2. Public Service Announcements:

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Exploring Code with Microsoft Pex

Software Engineering

Comparing procedure specifications

Verification and Validation

Location: Planet Laser Interview Skills Workshop

Second Midterm Review

Section 05: Solutions

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

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

What is Testing? Table of Contents

Java Bytecode (binary file)

CSE 331 Midterm Exam Sample Solution 2/13/12

Reading 8 : Recursion

Chapter 9 Quality and Change Management

MITOCW watch?v=9h6muyzjms0

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

1 Truth. 2 Conditional Statements. Expressions That Can Evaluate to Boolean Values. Williams College Lecture 4 Brent Heeringa, Bill Jannen

Pearson Education 2007 Chapter 9 (RASD 3/e)

CSE wi Midterm Exam 2/8/18 Sample Solution

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

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

CSE 331 Midterm Exam 2/13/12

Object Oriented Software Design - I

Lecture 19: Recursion

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Recursion. Readings: RS Chapter 12. Recursion. Recursion: The definition of an operation in terms of itself.

CSE 331 Software Design and Implementation. Lecture 16 Debugging

The compiler is spewing error messages.

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Thread Programming. Comp-303 : Programming Techniques Lecture 11. Alexandre Denault Computer Science McGill University Winter 2004

Lecture 20: SW Testing Presented by: Mohammad El-Ramly, PhD

Hardware versus software

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

CSCI-1200 Data Structures Spring 2018 Lecture 14 Associative Containers (Maps), Part 1 (and Problem Solving Too)

Introduction to Programming Using Java (98-388)

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

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Programming II (CS300)

Transcription:

Testing and Debugging Comp-303 : Programming Techniques Lecture 14 Alexandre Denault Computer Science McGill University Winter 2004 March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 1

Announcements... I hope everybody enjoyed their week of rest. Assignment 2 is due today. Don t forget to drop a paper copy in the hand in box. Most of the midterm correction is done and I will be giving them back on Thursday. Last day for project interview is tomorrow. CSGames still needs volunteers. If you want to help out this week-end, send an email to helpus@csgames.org. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 2

Testing terminology Validation : a process designed to increase our confidence that a program works as advertised Verification : a formal or informal argument that a program works on all possible inputs Testing : a process of running a program on a limited set of inputs and comparing the actual results with expected results Debugging : a process designed to determine why a program is not working correctly Defensive programming : the practice of writing a program in a way designed specifically to ease validation and debugging March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 3

Designing test cases Exhaustive testing is usually impossible A program with three inputs ranging from 1 to 1000 would take 1 000 000 000 test cases. With a speed of 1 test per second, it would take 31 years. How to define a limited set of good test cases? Black-box testing : testing from specification without regarding implementation or internal structure. Glass-box testing : augments black-box testing by looking at implementation. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 4

Black-box testing Advantages: not influenced by assumptions about implementation details robust with respect to changes in implementation allows observers with no internal knowledge of the program to interpret the results of the test Disadvantages: unlikely to test all parts of a program March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 5

Testing by looking at the Specs (1) static boolean isprime (int x) // EFFECTS: if x is prime returns true else returns false The effects clause has two cases. Both need to be tested. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 6

Testing by looking at the Specs (2) static int search (int [ ] a, int x) throws NullPointerException, NotFoundException // EFFECTS: if a is null throws NullPointerException // else if x is in a returns i such that a[ i ] = x // else throws NotFoundException We should test all 3 cases mentioned in effects clause. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 7

Testing by looking at the Specs (3) static float sqrt (float x, float epsilon) // REQUIRES: x >= 0 && 0.00001 < epsilon < 0.001 // EFFECTS: returns sq such that x - epsilon <= sq*sq <= x + epsilon The requires clause consists of two cases: x = 0 && 0.00001 < epsilon < 0.001 x > 0 && 0.00001 < epsilon < 0.001 Both need to be tested. The effects clause can be satisfied in many ways: We get an exact result We get a larger result We get a smaller result March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 8

Testing beyond the Specs static void appendvector (Vector v1, Vector v2) throws NullPointerException // MODIFIES: v1 and v3 // EFFECTS: If v1 or v2 is null throws NullPointerException // else removes all elements from v2 and appends them in // reverse order to v1 In certain situations, you should test beyond the specification. For example, if I were to call the appendvector function with v1 == v2, I could get a serious looping error. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 9

Testing boundary conditions A program should test typical input values: Arrays or sets are not empty. Integers are between smallest and largest values. Boundary conditions usually reveal: Logical errors where the path to a special case is absent. Conditions which cause the underlying hardware or system to raise an exception (e.g. arithmetic overflow). Test data should cover all combinations of largest and smallest values: Epsilon close to 0.001 and 0.00001 Arrays of 0 and 1 element Empty strings and strings of one character March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 10

Black-box test summary Black-box tests are based on a program s specification, not on its implementation. Black-box tests remain valid if program is reimplemented. Black-box tests should Test all paths through a specification Test boundary conditions and combinations of boundary conditions Sometimes, even test a little beyond the specification March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 11

Glass-box testing Glass-box tests complement Black-box testing by adding a test for each possible path through the program s implementation. A glass-box test set should be path-complete. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 12

Example of path-completeness static int maxofthree (int x, int y, int z) { if (x > y) if (x > z) return x; else return z; if (y > z) return y; else return z; } There are four possible paths through this function. This means we need four test cases: 3,2,1 3,2,4 1,2,1 1,2,3 March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 13

Beyond Path-Completeness static int maxofthree (int x, int y, int z) { return x; } However, path-completeness is not sufficient. Here, I only have one path. This means I would only need one test (ex: 1,2,3). This shows that specification should be tested, not just the implementation. Glass-Box testing does not reveal missing paths. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 14

Feasibility of Path-Completeness Sometimes, it s not feasible to test every path. for (int i = 1; i <= 100; i ++) for (int j = 1; j <= 100; j ++) if (Test.predicate(i*j))... In this example, we have 1 267 650 600 228 229 401 496 703 205 376 paths. Instead, we should test a subset. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 15

Approximating path-completeness Always test each branch of a conditional. Loops with fixed amount of iteration. test 2 Loops with variable amount of iteration. test 0,1,2 For recursive procedures, test the immediate return. test one recursive call. Don t forget to raise all possible exceptions. Use the Engineer s induction: One, two, three, that s good enough for me. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 16

Testing procedures: palindrome static boolean palindrome (String s) throws NullPointerException { // EFFECTS: If s is null throws NullPointerException else // returns true if s reads the same forward and backward // e.g. "deed" and " " are both palindromes int low = 0; int high = s.length -1; } while (high > low) { if (s.charat(low)!= s.charat(high)) return false; low ++; high --; } return true; March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 17

Testing palindrome Black-box testing of specification: s = null s = s = a s = deed s = seed Glass-box testing of implementation NullPointerException not executing loop return false in first iteration return true after first iteration return false after second iteration add case s = asia return true after the second iteration March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 18

Testing palindrome Missed any cases? What if s has odd size greater than one 1? March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 19

Testing polymorphic abstractions This is similar to testing non-polymorphic data abstractions, but one type per parameter is not enough. If an interface is used, extra tests for incompatibility should be added. e.g. To test OrderedList, add a String and then add an incomparable type (Integer?) In the related subtype approach, testing one subtype of the interface is not enough. e.g. Insert a String in a SumSet that uses a PolyAdder. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 20

Testing type hierarchies Blackbox testing for a subtype must include the blackbox tests of the supertype. However, no Glassbox testing of the supertype is required. When testing a subtype, you should... Test weakened preconditions. Cases supported by subtype but not supertype. Test strengthened postconditions. For example, test whether elements() of SortedIntSet are sorted. Test additional methods defined for subtypes. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 21

Unit testing and Integration testing Unit testing: to test whether a program unit implements its specification (i.e. specification is considered correct) Integration testing: to test the combination of two or more units (i.e. specification may be incompatible) Unit testing should always precede integration testing (divide and rule). March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 22

Tools for testing We might need to piece of code for unit testing: Test drivers: used to test a module when using code is still unimplemented (executes tests + compares results with expected results) Stubs: used to test a module when the code used by the module is still unimplemented (checks arguments and environment + produces expected results) Regression testing: repeat all previous tests after a change is made to fix a failed test March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 23

Debugging Testing is used to detect errors. Debugging is used to understand and fix errors. Some common sense issues: debugging takes more time than programming small modules reduce debugging effort well-written specifications reduce debugging effort March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 24

Scientific Method When debugging, apply the scientific method: 1. Study the available data. 2. Formulate a hypothesis that is consistent with the data. 3. Design and run a repeatable experiment that can refute the hypothesis. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 25

Debugging strategies Find the simplest input that causes the error to occur. e.g. for palindrome(): "able was I ere I saw Elba" returns false => hypothesis 1: the procedure doesnt work for odd-size palindromes "ere" returns true => hypothesis 2: the procedure doesnt work with blanks " " returns true => hypothesis 3: the procedure doesnt work with mixed upper and lower case characters "Abba" returns false => bingo! March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 26

Debugging strategies Trace the code by checking intermediate results. (System.out.println(o.toString()) An even better idea is to use an Interactive Development Environment (IDE) that allows you to inspect variables easily. This allows you to find the procedure where the bug occurs (which is often most of the work). The bug is probably not where you think it is. Ask yourself where the bug is not. Sherlock Holmes: If you eliminate the impossible, what remains, however improbable, is the truth March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 27

Debugging strategies Try the simple things first: reversing the order of input arguments looping through an array (or String or Vector) one index too far failing to re-initialize a variable a second time copying only the top level of a data structure (shallow copy - aliasing errors) failing to parenthesize an expression correctly failing to use = instead of == March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 28

Debugging strategies Get someone else to help you In debugging you often follow the same reasoning as when you wrote the code. Explain the problem to someone else Articulating your reasoning often reveals the source. If all else fails, go away Debugging when overly tired makes you repeat the same mistakes: take a break. When you find a bug, think why you put it there This often leads you to discover new bugs. Don t be in a rush to fix the bug Think through all the ramifications: it is better to fix a bug you understand completely than to repeatedly apply small fixes until it works. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 29

Defensive programming In development, check often: requirements (e.g. check if sorted before binary search) conditionals (e.g. tests all cases, even those that can not occur) In production code, disable the checks that are too inefficient by putting them in comments (so they can be reactivated easily). March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 30

Summary Testing is a way of validating correctness of your code. Black-box testing is generated from the specification. It always remains, even when implementation changes. check boundary conditions check each path through the specifications Glass-box testing complements BB-testing by testing each path in your code. all branches in a conditional 0,1,2 iterations 0 and 1 recursive call Debugging allows you to find and correct errors using the scientific method. (analyze data, formulate hypothesis, try to disprove) March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 31

Tool of the day: JUnit JUnit is a regression testing framework written by Erich Gamma and Kent Beck. It is used by the developer who implements unit tests in Java. You can create unit tests by subclassing TestCase. JUnit allows you to automate the testing of all your test cases. More info on JUnit is available at http://www.junit.org/ March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 32

Tool of the day: Jdb Jdb is one of the best kept secret of Java. It is a demonstration of the Java Platform Debugger Architecture that provides inspection and debugging of a local or remote Java Virtual Machine. It works like gdb, but a little more complicated. Unlike gdb, it has extensive support for tracking threads. To use jdb, you need to compile your classes with Debug information (-g). You can/should find a tutorial on the web. March 1, 2004 Lecture 14 Comp 303 : Testing and Debugging Page 33