CS1101X Programming Methodology

Slides:



Advertisements
Similar presentations
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Advertisements

Why care about debugging? How many of you have written a program that worked perfectly the first time? No one (including me!) writes a program that works.
Programming Types of Testing.
FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Debugging Techniques1. 2 Introduction Bugs How to debug Using of debugger provided by the IDE Exception Handling Techniques.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Well-behaved objects Debugging. 2 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Prevention vs Detection.
The IDE (Integrated Development Environment) provides a DEBUGGER for locating and correcting errors in program logic (logic errors not syntax errors) The.
Finding and Debugging Errors
16/27/2015 3:38 AM6/27/2015 3:38 AM6/27/2015 3:38 AMTesting and Debugging Testing The process of verifying the software performs to the specifications.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
CS1101: Programming Methodology Aaron Tan.
CS1101: Programming Methodology
Testing. What is Testing? Definition: exercising a program under controlled conditions and verifying the results Purpose is to detect program defects.
CMSC 345 Fall 2000 Unit Testing. The testing process.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Chapter 6: Functions Starting Out with C++ Early Objects
Compiling & Debugging Quick tutorial. What is gcc? Gcc is the GNU Project C compiler A command-line program Gcc takes C source files as input Outputs.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
ME 142 Engineering Computation I Debugging Techniques.
Unit Testing 101 Black Box v. White Box. Definition of V&V Verification - is the product correct Validation - is it the correct product.
Testing and Debugging Session 9 LBSC 790 / INFM 718B Building the Human-Computer Interface.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
Starting Out with C++ Early Objects ~~ 7 th Edition by Tony Gaddis, Judy Walters, Godfrey Muganda Modified for CMPS 1044 Midwestern State University 6-1.
School of Computer Science & Information Technology G6DICP - Lecture 6 Errors, bugs and debugging.
Testing Chapter 23 IB103 Week 12 (part 3). Verify that a complex (any) program works correctly, that the program meets specifications The chapter reviews.
Debugging 1/6/2016. Debugging 1/6/2016 Debugging  Debugging is a methodical process of finding and reducing the number of bugs, or defects, in a program.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
CS 177 Week 10 Recitation Slides 1 1 Debugging. Announcements 2 2.
Testing and Debugging. Testing Fundamentals  Test as you develop Easier to find bugs early rather than later Prototyping helps identify problems early.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
Software Development Cycle From Wikipedia: fe-cycle
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Harvard Mark I Howard Aiken was a pioneer in computing and a creator of conceptual design for IBM in the 1940s. He envisioned an electro-mechanical computing.
Debuggers. Errors in Computer Code Errors in computer programs are commonly known as bugs. Three types of errors in computer programs –Syntax errors –Runtime.
Dale Roberts Debugger Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Debugging with Eclipse
ME 142 Engineering Computation I
Testing and Debugging PPT By :Dr. R. Mall.
Testing and Debugging.
Computer Programming I
Loop Structures.
Debugging CMSC 202.
Scripts & Functions Scripts and functions are contained in .m-files
Important terms Black-box testing White-box testing Regression testing
Important terms Black-box testing White-box testing Regression testing
UNIT-4 BLACKBOX AND WHITEBOX TESTING
Software Testing Program testing can be used to show the presence of bugs, but never to show their absence. – Edgar Dijkstra.
Common C Programming Errors, GDB Debugging
DEBUGGING JAVA PROGRAMS USING ECLIPSE DEBUGGER
Tonga Institute of Higher Education
Algorithm Correctness
Testing, debugging, and using support libraries
Test Case Test case Describes an input Description and an expected output Description. Test case ID Section 1: Before execution Section 2: After execution.
Debuggers and Debugging
Debugging “Why you were up till 2AM”
PROGRAMMING FUNDAMENTALS Lecture # 03. Programming Language A Programming language used to write computer programs. Its mean of communication between.
CSE 1020:Software Development
Debugging.
How to allow the program to know when to stop a loop.
CHAPTER 6 Testing and Debugging.
UNIT-4 BLACKBOX AND WHITEBOX TESTING
Chapter 15 Debugging.
Presentation transcript:

CS1101X Programming Methodology Testing and debugging Testing and debugging are important activities in software development.

CS1101X Programming Methodology Programming Errors Compilation errors Syntax error (example: missing a semi-colon). Semantic error. (For example, applying modulus % on floating-point value for certain programming languages. In Java ,is it fine? Yes!) Easiest type of errors to fix. Runtime errors Occur at runtime. Java’s exception mechanism can catch such errors. Logic errors Program runs but produces incorrect result. Hard to characterize, hence hardest to fix. Programming errors are also known as bugs Origin: a moth in the Mark I computer.

CS1101X Programming Methodology Testing and Debugging Testing To determine if a code contains errors. Debugging To locate the error and fix it. Testing Yes Error? Debug

Testing vs. verification CS1101X Programming Methodology Testing vs. verification Testing A process of running a program on a set of test cases and comparing the actual results with the expected results. Verification A formal or informal reasoning that a program works for all possible inputs.

Black-box and White-box Testing CS1101X Programming Methodology Black-box and White-box Testing Black-box testing indicates that we cannot examine the code as we devise test cases Seeing the code can bias the test cases we create Forces testers to use design specification rather than the code Black-box testing is also known as functional testing White-box (or glass-box) testing indicates that we can “see” or examine the code as we develop test cases. white-box testing is also called structural testing. Complementary techniques

Testing paths through the specification CS1101X Programming Methodology Testing paths through the specification Black-box tests should test all paths through the specification. double sqrt( double x, double epsilon) { // requirement: x>=0 && .00001 < epsilon < .001 // result: x-epsilon <= sqrt*sqrt <= x+epsilon ... } To test all paths X = 0 and .00001 < epsilon < .001 X > 0 and .00001 < epsilon < .001 6

CS1101X Programming Methodology Path Testing White-box tests should be path complete. Design test data to check all paths Example if (x != 3) { y = 5; } else { z = z - x; }\ if (z > 1) { z = z / x; z = 0; } if (x != 3) y = 5 z = z - x if (z > 1) z = z / x z = 0 A C B D <x=0, z=1> Paths A, D <x=0, z=3> Paths A, C. <x=3, z=3> Paths B, C. <x=3, z=1> Paths B, D.

Path testing for loops while (x>0) { do something; x--; } The number of iterations is not known. It’s impossible to test all possible number of iterations. Test cases should include 0, 1, 2 iterations

Path-completeness doesn’t guarantee no errors int max (int x, int y, int z) { return x; } In the above program, there is only one path. Test case (3, 1, 2) works for the only path. Obviously, the program doesn’t work for a lot of inputs.

CS1101X Programming Methodology Testing Boundaries It is important to test the boundary conditions. Example final int CALENDAR_START = 1583; // validate input  if ((year < CALENDAR_START) || (month < 1) || (month > 12)) { System.output.println("Bad request: " + year + " " + month); } Suggests the following boundary tests Input Year Input Month 1582 2 1583 13 1 12

The testing phases Component/unit testing Testing of individual program components Usually the responsibility of the component developer Tests are derived from the developer’s experience Integration testing Testing of groups of components integrated to create a system or sub-system The responsibility of an independent testing team Tests are based on a system specification

Component being tested Tools for testing Test driver A driver simulates the part of a program that calls the component being tested. A program that automatically tests components. Stubs A stub is a program simulates part of a program called by the component being tested. stub Component being tested driver stub

Debugging Once errors are identified: it is necessary identify the precise location of the errors and to fix them.

CS1101X Programming Methodology Brute-force method Print statements Easy to add Provide information: Which methods have been called The value of parameters The order in which methods have been called The values of local variables and fields at strategic points Disadvantages Not practical to add print statements in every method Too many print statements lead to information overload Removal of print statements tedious

Turning Debugging Info On/Off CS1101X Programming Methodology Turning Debugging Info On/Off public static int sum(int a, int b) { int left = 0; int right = data.length - 1; if (debugging) { System.out.println("sum called with a = " + a + " b = " + b); } int total = a + b; System.out.println("total = " + total); return total; Use an extra boolean variable to turn on/off debugging info.

CS1101X Programming Methodology Debugger Using the debugger Stepping Breakpoint Inspecting variables

JDB Type “run” to execute the program Compile your source file with -g option Produce debugging information For example javac –g rsdimu.java To run jdb Type “jdb” to load an executable jdb <class name> Type “run” to execute the program Type “quit” to exit jdb

To attach JDB to a running JVM Start the JVM with the following options: java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=<port> <class> The JVM starts up, but suspends execution before it starts the Java application. In a separate session, you can attach the debugger to the JVM: jdb -attach <port> The debugger will attach to the JVM, and you can now issue a range of commands to examine and control the Java application; for example, type run to allow the Java application to start.

Breakpoint Make your program stops whenever a certain point in the program is reached For example Make a breakpoint in line 3 stop at DebugDemo:3 Program stops before execute line 3 Allow you to examine code, variables, etc. public class DebugDemo { public static void main() { System.out.println("A"); int x = 1; System.out.println("x="+x); } 1 2 3 4 5 6 7

Breakpoint Add breakpoint stop at MyClass:<line num> stop in java.lang.String.length stop in MyClass.<method name> Delete breakpoint clear (clear all breakpoints) clear <breakpoint> e.g. clear MyClasss:22 Line break point Method break point

Step Stepping into the current line. Execute the current line, then stop and return to JDB If the current line contains a call to a method, the execution traverses to the first line in the called method.

Next Stepping over the current line. Similar to step, but treat function call as one source line.

cont Resume continuous execution of the program until either one of the followings Next breakpoint End of program

print Print Display the value of an expression print expression print MyClass.myStaticField print i + j + k print myObj.myMethod() (if myMethod returns a non-null) print new java.lang.String("Hello").length() Dump Display all the content of an object dump <object>

Homework Design path-complete test cases for each of the programs below. (a) int max(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; } (b) while(x>0) { if (y<0) {do something and then break;} do something; x--; y--;