Testing and Debugging Hakam Alomari

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

C++ Language Fundamentals. 2 Contents 1. Introduction to C++ 2. Basic syntax rules 3. Declaring and using variables.
Annoucements  Next labs 9 and 10 are paired for everyone. So don’t miss the lab.  There is a review session for the quiz on Monday, November 4, at 8:00.
 C++ programming facilitates a disciplined approach to program design. ◦ If you learn the correct way, you will be spared a lot of work and frustration.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Chapter 11 Separate Compilation and Namespaces Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation Namespaces Simple Make Files (Ignore all class references.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Chapter 5: Loops and Files.
A simple C++ program /* * This program prints the phrase "Hello world!" * on the screen */ #include using namespace std; int main () { cout
Computer Science 1620 C++ - Basics. #include using namespace std; int main() { return 0; } A very basic C++ Program. When writing your first programs,
G++ and make Dan Wilson CS193 02/01/06. The g++ Compiler What happens when you call g++ to build your program? Phase 1, Compilation:.cpp files are compiled.
1 Functional Testing Motivation Example Basic Methods Timing: 30 minutes.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Dr. Pedro Mejia Alvarez Software Testing Slide 1 Software Testing: Building Test Cases.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering.
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
Intro. to Game Programming Want to program a game?
Modular Programming Chapter Value and Reference Parameters t Function declaration: void computesumave(float num1, float num2, float& sum, float&
CMSC 345 Fall 2000 Unit Testing. The testing process.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
Modular Programming Chapter Value and Reference Parameters computeSumAve (x, y, sum, mean) ACTUALFORMAL xnum1(input) ynum2(input) sumsum(output)
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Chapter 02 (Part III) Introduction to C++ Programming.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 A simple C++ program // ======================================================= // File:helloworld.cpp // Author:Vana Doufexi // Date:1/4/2006 // Description:Displays.
Chapter 2 part #1 C++ Program Structure
Module 4: I/O and Strings #1 2000/01Scientific Computing in OOCourse code 3C59 Module 4: I/O In this module we will cover Keyboard/screen input and output.
Loops and Files. 5.1 The Increment and Decrement Operators.
CHAPTER 2 PART #3 C++ INPUT / OUTPUT 1 st Semester King Saud University College of Applied studies and Community Service CSC1101 By: Fatimah.
Separating Class Specification tMyn1 Separating Class Specification from Implementation Usually class declarations are stored in their own header files.
Multiple File Compilation and linking By Bhumik Sapara.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CSCI 161 Lecture 3 Martin van Bommel. Operating System Program that acts as interface to other software and the underlying hardware Operating System Utilities.
Software Engineering Algorithms, Compilers, & Lifecycle.
STRUCTURED PROGRAMMING Complete C++ Program. Content 2  Main Function  Preprocessor directives  User comments  Escape characters  cout statement.
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Software Testing CS II: Data Structures & Abstraction
Bill Tucker Austin Community College COSC 1315
Last week: We talked about: History of C Compiler for C programming
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter Topics The Basics of a C++ Program Data Types
Testing Tutorial 7.
Brief Intro to Make CST494/ Gannod.
Introduction to C++ Programming
ANNOUNCEMENT The missed lecture will be made up this Monday evening in the Tech PC classroom (MG51). A tentative time interval is 6:30-8:00. The exact.
Andy Wang Object Oriented Programming in C++ COP 3330
Compilation and Debugging
Compilation and Debugging
Basic Elements of C++.
Chapter 2 part #3 C++ Input / Output
Testing UW CSE 160 Spring 2018.
Basic Elements of C++ Chapter 2.
UNIT-4 BLACKBOX AND WHITEBOX TESTING
C++ Compilation Model C++ is a compiled language
CSCE-221 Makefile Introduction
Chapter 2 part #3 C++ Input / Output
CSE 1020:Software Development
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Introduction to Programming - 1
Chapter 1 c++ structure C++ Input / Output
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Chapter 2 part #1 C++ Program Structure
CHAPTER 6 Testing and Debugging.
UNIT-4 BLACKBOX AND WHITEBOX TESTING
Presentation transcript:

Testing and Debugging Hakam Alomari

Outline Include Guards Namespace name access Testing Concepts – What is software testing? – How to find faults? – Black Box vs. Glass Box testing – How many tests cases are there? – Regression testing Using assert and cerr Building test cases, Makefile

Include Guards Are used to prevent the contents of a file from being included more than once – Example: (string.h) #ifndef CS2_STRING_H #define CS2_STRING_H Class string { //… }; #endif

Example [wiki] File "grandfather.h“ struct foo { int member; }; File "father.h“ #include "grandfather.h" File "child.c“ #include "grandfather.h" #include "father.h"

Use of #include guards File "grandfather.h“ #ifndef GRANDFATHER_H #define GRANDFATHER_H struct foo { int member; }; #endif File "father.h“ #include "grandfather.h" File "child.c“ #include "grandfather.h" #include "father.h"

Namespaces Used to group entities (e.g., objects) under a name, such as each group has its own name Example: namespace foo1 { int var = 1; } namespace foo2 { int var = 2; } int main () { std::cout<< foo1::var <<“\n”; // output 1 std::cout<< foo2::var <<“\n”; // output 2 return 0; }

Standard Includes Names defined by standard includes (e.g., cout) can be used as: – Prefix the name with std:: – Use using std::name before the name is used – Put using namespace::std before the name is used – Example: int main() { std::cout << "Hello World!\n"; //... int main() { using std::cout; cout << "Hello World!\n"; //... using namespace std; int main() { cout << "Hello World!\n"; //...

Software Testing Software testing is used to find errors error: – Incorrect output for a given input – Caused by faults inside the program – A fault is represent the incorrect part of the code E.g., incorrect stopping condition So, test cases should be developed to exercise the program and uncovering errors

Testing levels Start by testing each method (unit tests) Then each class in full (module tests) Then the whole program (system tests) The test case that has a high probability to uncover an error is known to be the best one

Testing Types Black box vs Glass box (white box) – Black box Uses only the I/O spec. to develop test cases – Glass box Uses only the implementation details to develop test cases Both types are necessary to develop a good set of test cases

Number of Test Cases Functions usually have a very large number of pre. and post. conditions So, there is no need to test all of these to make sure our function behaves correctly How? – Pairing down test cases, by: – Develop equivalence classes of test cases – Examine the boundaries of these classes carefully

Equivalence Classes The input and output often fall into equivalence partitions where the system behaves in an equivalent way for each class Then, the test cases should be developed to test each partition

Classes Boundaries Example: – Partition system inputs and outputs into equivalence classes – If input is a 2 digit integer between 10 and 99 then the equivalence partitions are 99 (invalid) – Choose test cases at the boundary of these partitions – 09, 10, 99, 100

Building Test Cases 1.Determine the I/O spec. for the method 2.Develop test cases 3.Method implementation 4.Run the method against the test cases from 2 5.Debugging (fix) 6.Go to 4

Test Steps There are three steps in a test: – Set-up the "fixture", the system to be tested – Perform the test – Verify the results, make sure there are no unwanted side effects Example: Test for existence of const char& operator[](int) const – // Setup fixture const string str("abc"); – // Test assert(str[0] == 'a'); assert(str[1] == 'b'); assert(str[2] == 'c'); – // Verify assert(str.length() == 3);

Regression Testing The intent of regression testing is to ensure that a change, such as a bugfix, did not introduce new faults Each time you add a new method to your class or fix a fault run your test cases (all of them) – Adding something new or fixing a problem may have side effects Re-running your test cases will uncover these problems

Asserts Used to output an error message and terminate a program if a condition is false Asserts are useful for testing #include is required to use asserts Example: – assert(result == correct_result); Asserts can be turned off using – #define NDEBUG

Debugging Used when the program is not working properly One easy thing to do is output the value of relevant variables Use cerr instead of cout. cout is buffered Buffering means output is accumulated in a "buffer" and the accumulated output sent out to the OS together Output to cerr is not buffered, it is output immediately If a program crashes there may be output in a buffer that gets lost It is a good idea to include a message with values that are output – std::cerr << "var1 = " < < var1 << "\n";

The make Command The make command may be used to automate compiling The make command when executed will look for a file named makefile and then, if not found, for a file named Makefile to specify dependencies Otherwise a makefile must be specified with the - f option The make command has some rules built-in so it can do some basic compiling but usually the developer provides a makefile

Makefile Rules Rules have the form: – target: dependency_list – TAB command TargetA file or name dependency_listFiles that the target "depends on" TABThe TAB character. REQUIRED! CommandTypically a compiling/linking command but can be any command. There may be more than one TAB/command line If the modification time of any dependency is more recent than the target the command lines are executed

Common Rule Types Creating machine language,.o, files and linking are two things that must be done # Link executable: file_1.o file_2.o g++ -Wall file_1.o file_2.o -o executable # Create.o file file.o: file.h file.cpp g++ -Wall -c file.cpp Note.cpp files are typically not targets,.cpp files do not depend on other files

Makefile Example # Create the executable is the first rule here # Link main: bigint.o main.o g++ -Wall bigint.o main.o -o main # Create.o file main.o: bigint.h main.cpp g++ -Wall -c main.cpp # Create.o file bigint.o: bigint.h bigint.cpp g++ -Wall -c bigint.cpp # Remove.o and executable clean: rm -f *.o rm -f main

References [wiki]: Computer Science II Lab: Software Testing for CS II slides: