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

Similar documents
Program Verification! Goals of this Lecture! Words from the Wise! Testing!

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

For Your Amusement. Beware of bugs in the above code; I have only proved it correct, not tried it. Donald Knuth. Program Verification

Princeton University. Testing. Computer Science 217: Introduction to Programming Systems

EXTERNAL TESTING. Princeton University Computer Science 217: Introduction to Programming Systems. Why Test? Testing. Why Test? Why Test?

Princeton University Computer Science 217: Introduction to Programming Systems. Testing

Το Υλικό από το βιβλίο. The Practice of Programming. (Kernighan & Pike) Chapter 6

Το Υλικό από το βιβλίο. The Practice of Programming. (Kernighan & Pike) Chapter 6

do { write_program; OK = test_program ( ); } while (!OK); upload program to customer;

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

Debugging (Part 1) The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 5

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

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

Program Verification. Aarti Gupta

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

Unit Testing as Hypothesis Testing

Welcome! COMP s1. Programming Fundamentals

Welcome! COMP s1 Lecture 7. COMP s1. Before we begin. Strings. Programming Fundamentals. Overview. Andrew Bennett

Why testing and analysis. Software Testing. A framework for software testing. Outline. Software Qualities. Dependability Properties

Software Testing part II (white box) Lecturer: Giuseppe Santucci

Lecture 15 Software Testing

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

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

Topics in Software Testing

An Introduction to Systematic Software Testing. Robert France CSU

Introduction to Dynamic Analysis

The Design of C: A Rational Reconstruction: Part 2

Software Engineering

Continued from previous lecture

Goals of C "" The Goals of C (cont.) "" Goals of this Lecture"" The Design of C: A Rational Reconstruction"

Software Testing. Software Testing

Unit Testing as Hypothesis Testing

Verification and Validation. Verification and validation

Facts About Testing. Cost/benefit. Reveal faults. Bottom-up. Testing takes more than 50% of the total cost of software development

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

CSE 331 Midterm Exam Sample Solution 2/13/12

Testing & Debugging TB-1

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

Program Analysis. Program Analysis

Princeton University Computer Science 217: Introduction to Programming Systems The Design of C

Best Practices in Programming

Principles of Software Construction: Objects, Design, and Concurrency (Part 2: Designing (Sub )Systems)

Intermediate Programming, Spring 2017*

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

Problem Solving with C++

Testing. UW CSE 160 Winter 2016

Goals of this Lecture

Software Testing Lecture 1. Justin Pearson

Absolute C++ Walter Savitch

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

Program Correctness and Efficiency. Chapter 2

Terminology. There are many different types of errors and different ways how we can deal with them.

Software Engineering 2 A practical course in software engineering. Ekkart Kindler

Testing, Timing, Profiling, & Instrumentation. Testing, Profiling, & Instrumentation

Princeton University Computer Science 217: Introduction to Programming Systems. A Taste of C

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Lecture 02 C FUNDAMENTALS

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

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

Testing and Debugging

Software Engineering Testing and Debugging Testing

Introduction to C. CS2023 Winter 2004

Programming Embedded Systems

Software Quality Assurance. David Janzen

About this exam review

Software Testing. Testing: Our Experiences

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

The four laws of programs

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. One More Type of Control Flow. What s Wrong with HW1?

The Design of C: A Rational Reconstruction (cont.)

Testing Objectives. Successful testing: discovers previously unknown errors

Objectives. Chapter 19. Verification vs. validation. Topics covered. Static and dynamic verification. The V&V process

Computer Science and Software Engineering University of Wisconsin - Platteville 9-Software Testing, Verification and Validation

Chapter 7 Control Statements Continued

Getting those Bugs Out --Software Testing

Chapter 8 Software Testing. Chapter 8 Software testing

Software Testing. Software Testing. in the textbook. Chapter 8. Verification and Validation. Verification and Validation: Goals

CS102: Standard I/O. %<flag(s)><width><precision><size>conversion-code

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

CSCI 2132 Software Development. Lecture 17: Functions and Recursion

MONIKA HEINER.

MPLAB X Debugging Techniques

INTRODUCTION TO SOFTWARE ENGINEERING

Decision Making and Loops

CMSC 132: OBJECT-ORIENTED PROGRAMMING II

The Design of C: A Rational Reconstruction (cont.)" Jennifer Rexford!

Structural Testing. (c) 2007 Mauro Pezzè & Michal Young Ch 12, slide 1

Software Testing TEST CASE SELECTION AND ADEQUECY TEST EXECUTION

CSCI S-Q Lecture #12 7/29/98 Data Structures and I/O

Performance Improvement. The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 7

10. Software Testing Fundamental Concepts

Agile Software Development. Lecture 7: Software Testing

Variables and Data Representation

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Fundamental of Programming (C)

Topic 8: I/O. Reading: Chapter 7 in Kernighan & Ritchie more details in Appendix B (optional) even more details in GNU C Library manual (optional)

o Echo the input directly to the output o Put all lower-case letters in upper case o Put the first letter of each word in upper case

Announcements. Testing. Announcements. Announcements

Testing & Continuous Integration. Kenneth M. Anderson University of Colorado, Boulder CSCI 5828 Lecture 20 03/19/2010

Lecture 26: Testing. Software Engineering ITCS 3155 Fall Dr. Jamie Payton

Transcription:

Testing The material for this lecture is drawn, in part, from The Practice of Programming (Kernighan & Pike) Chapter 6 1 Goals of this Lecture Help you learn about: Internal testing External testing General testing strategies Why? It s hard to know if a large program works properly Well-written test code finds errors early, saving time Good testing strategies give you confidence that the code might actually work 2 1

Program Verification Ideally: Prove that your program is correct Can you prove properties of the program? Can you prove that it even terminates? See Turing s Halting Problem Specification program.c Program Checker? Right or Wrong 3 Program Testing Pragmatically: Convince yourself that your program probably works Specification program.c Testing Strategy Probably Right or Certainly Wrong 4 2

External vs. Internal Testing Types of testing External testing Designing data to test your program Internal testing Designing your program to test itself 5 External Testing External Testing Designing data to test your program 4 techniques 6 3

Coverage Testing (1) Statement testing Testing to satisfy the criterion that each statement in a program be executed at least once during program testing. Glossary of Computerized System and Software Development Terminology (2) Path testing Testing to satisfy coverage criteria that each logical path through the program be tested. Often paths through the program are grouped into a finite set of classes. One path from each class is then tested. Glossary of Computerized System and Software Development Terminology More difficult than statement testing For simple programs, can enumerate all paths through the code Otherwise, sample paths through code with random input 7 Coverage Testing Example Example pseudocode: if (condition1) statement1; else statement2; if (condition2) statement3; else statement4; if (condition3) statement5; else statement6; Statement testing: Should make sure all 3 if statements and all 6 nested statements are executed Path testing: Should make sure all logical paths are executed Note: combinatorial 8 4

Brute Force: Stress Testing (3) Stress testing Testing conducted to evaluate a system or component at or beyond the limits of its specified requirements Glossary of Computerized System and Software Development Terminology What to generate Very large input sets Random input sets (binary vs. ASCII) Use computer to generate input sets 9 Stress Testing Example 1 Specification: Copy all characters of stdin to stdout Attempt: #include <stdio.h> int main(void) { char c; while ((c = getchar()) = EOF) putchar(c); return 0; Does it work? Hint: Consider random input sets 10 5

Stress Testing Example 2 Specification: Print number of characters in stdin Attempt: #include <stdio.h> int main(void) { char charcount = 0; while (getchar() = EOF) charcount++; printf("%d\n", charcount); return 0; Does it work? Hint: Consider large input sets 11 Apply Smarts: Boundary Testing (4) Boundary testing A testing technique using input values at, just below, and just above, the defined limits of an input domain; and with input values causing outputs to be at, just below, and just above, the defined limits of an output domain. Glossary of Computerized System and Software Development Terminology Alias corner case testing 12 6

Boundary Testing Example Specification: Read line from stdin, store as string in array (without \n ) First attempt: int i; char s[arraysize]; for (i=0; ((i < ARRAYSIZE-1) && (s[i]=getchar()) = '\n'); i++) ; s[i] = '\0'; Consider boundary conditions: 1. stdin contains no characters (empty file) 2. stdin starts with '\n' (empty line) 3. stdin contains characters but no '\n 4. stdin line contains exactly ARRAYSIZE-1 characters 5. stdin line contains exactly ARRAYSIZE characters 6. stdin line contains more than ARRAYSIZE characters 13 Testing the First Attempt Embed code in complete program: #include <stdio.h> enum {ARRAYSIZE = 5; /* Artificially small */ int main(void) { int i; char s[arraysize]; for (i=0; ((i < ARRAYSIZE-1) && (s[i]=getchar()) = '\n'); i++) ; s[i] = '\0'; for (i = 0; i < ARRAYSIZE; i++) { if (s[i] == '\0') break; putchar(s[i]); return 0; 14 7

Test Results for First Attempt int i; char s[arraysize]; for (i=0; ((i < ARRAYSIZE) && (s[i]=getchar()) = '\n')); i++) ; s[i] = '\0'; 1. stdin contains no characters (empty file) ÿÿÿÿÿ Fail 2. stdin starts with '\n' (empty line) n Again: Pass Does it work? 3. stdin contains characters but no '\n ab abÿÿÿ Fail 4. stdin line contains exactly ARRAYSIZE-1 characters abc n abc Pass 5. stdin line contains exactly ARRAYSIZE characters abcd n abcd Pass 6. stdin line contains more than ARRAYSIZE characters abcde n abcd Pass or Fail??? 15 Ambiguity in Specification If stdin line is too long, what should happen? Keep first ARRAYSIZE characters, discard the rest? Keep first ARRAYSIZE -1 characters + '\0' char, discard the rest? Keep first ARRAYSIZE -1 characters + '\0' char, save the rest for the next call to the input function? Probably, the specification didn t even say what to do if MAXLINE is exceeded Probably the person specifying it would prefer that unlimited-length lines be handled without any special cases at all Moral: testing has uncovered a design problem, maybe even a specification problem Define what to do Keep first ARRAYSIZE -1 characters + '\0' char, save the rest for the next call to the input function 16 8

Testing A Second Attempt Embed code in complete program: #include <stdio.h> enum {ARRAYSIZE = 5; /* Artificially small */ int main(void) { int i; char s[arraysize]; for (i = 0; i < ARRAYSIZE; i++) { s[i] = getchar(); if ((s[i] == EOF) (s[i] == '\n')) break; s[i] = '\0'; for (i = 0; i < ARRAYSIZE; i++) { if (s[i] == '\0') break; putchar(s[i]); return 0; 17 Test Results for Second Attempt int i; char s[arraysize]; for (i = 0; i < ARRAYSIZE; i++) { s[i] = getchar(); if ((s[i] == EOF) (s[i] == '\n')) break; s[i] = '\0'; 1. stdin contains no characters (empty file) Pass 2. stdin starts with '\n' (empty line) n Again: Pass Does it work? 3. stdin contains characters but no '\n ab ab Pass 4. stdin line contains exactly ARRAYSIZE-1 characters abc n abc Pass 5. stdin line contains exactly ARRAYSIZE characters abcd n abcd Pass 6. stdin line contains more than ARRAYSIZE characters abcde n abcd Pass 18 9

Morals of this Little Story Testing can reveal the presence of bugs, but not their absence Complicated boundary cases often are symptomatic of bad design or bad specification Clean up the specification if you can Otherwise, fix the code 19 External Testing Summary External testing: Designing data to test your program External testing taxonomy (1) Statement testing (2) Path testing (3) Stress testing (4) Boundary testing 20 10

Relevant Quotations On two occasions I have been asked [by members of Parliament], Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out? I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. Charles Babbage Program testing can be quite effective for showing the presence of bugs, but is hopelessly inadequate for showing their absence. Edsger Dijkstra Beware of bugs in the above code; I have only proved it correct, not tried it. Donald Knuth 21 Internal Testing Internal testing Designing your program to test itself 4 techniques 22 11

Checking Invariants (1) Checking invariants Function should check aspects of data structures that shouldn t vary Remember this for Assignment 6 Example: doubly-linked list insertion function At leading and trailing edges Traverse doubly-linked list; when node x points forward to node y, does node y point backward to node x? What other invariants could be checked? Example: balanced binary search tree insertion function At leading and trailing edges What other Traverse tree; are nodes still sorted? invariants could be checked? 23 Checking Invariants (cont.) Convenient to use assert to check invariants int isvalid(mytype object) { Check invariants here. Return 1 (TRUE) if object passes all tests, and 0 (FALSE) otherwise. void myfunction(mytype object) { assert(isvalid(object)); Manipulate object here. assert(isvalid(object)); 24 12

Aside: The assert Macro The assert macro One actual parameter Should evaluate to 0 (FALSE) or non-0 (TRUE) If TRUE: Do nothing If FALSE: Print message to stderr like assert at line x failed Exit the process Note: this is for developers, not users do not expect to use for actual error reporting 25 Uses of assert Typical uses of assert Validate formal parameters int gcd(int i, int j) { assert(i > 0); assert(j > 0); Check for impossible logical flow switch (state) { case START: break; case COMMENT: break; default: assert(0); /* Never should get here */ Check invariants 26 13

Checking Return Values (2) Checking function return values In Java and C++: Method that detects error can throw a checked exception Calling method must handle the exception (or rethrow it) In C: No exception-handling mechanism Function that detects error typically indicates so via return value Programmer easily can forget to check return value Programmer (generally) should check return value 27 Checking Return Values (cont.) (2) Checking function return values (cont.) Example: scanf() returns number of values read Bad code int i; scanf("%d", &i); Good code int i; if (scanf("%d", &i) = 1) /* Error */ Example: printf() can fail if writing to file and disk is full; returns number of characters (not values) written Bad code??? int i = 100; printf("%d", i); Good code??? int i = 100; if (printf("%d", i) = 3) /* Error */ overkill? 28 14

Changing Code Temporarily (3) Changing code temporarily Temporarily change code to generate artificial boundary or stress tests Example: Array-based sorting program Temporarily make array very small Does the program handle overflow? Remember this for Assignment 3 Example: Program that uses a hash table Temporarily make hash function return a constant All bindings map to one bucket, which becomes very large Does the program handle large buckets? 29 Leaving Testing Code Intact (4) Leaving testing code intact Do not remove testing code when your code is finished In industry, no code ever is finished Leave tests in the code Maybe embed in calls of assert Calls of assert can be disabled; described in precept 30 15

Internal Testing Summary Internal testing: Designing your program to test itself Internal testing techniques (1) Checking invariants (2) Checking function return values (3) Changing code temporarily (4) Leaving testing code intact Beware: Do you see a conflict between internal testing and code clarity? 31 General Testing Strategies General testing strategies 5 strategies 32 16

Automation (1) Automation Create scripts and data files to test your programs Create software clients to test your modules Know what to expect Generate output that is easy to recognize as right or wrong Automated testing can provide: Much better coverage than manual testing Bonus: Examples of typical use of your code Have you used these techniques in COS 217 programming assignments? 33 Testing Incrementally (2) Testing incrementally Test as you write code Add test cases as you create new code Test individual modules, and then their interaction Do regression testing After a bug fix, make sure program has not regressed That is, make sure previously working code is not broken Rerun all test cases Note the value of automation 34 17

Testing Incrementally (cont.) (2) Testing incrementally (cont.) Create scaffolds and stubs to test the code that you care about Function 1 Scaffold: Temporary code that calls code that you care about Function that you care about Function 2 Function 3 Stub: Temporary code that is called by code that you care about 35 Comparing Implementations (3) Comparing implementations Make sure independent implementations behave the same Could you have you used this technique in COS 217 programming assignments? 36 18

Bug-Driven Testing (4) Bug-driven testing Find a bug à create a test case that catches it Facilitates regression testing 37 Fault Injection (5) Fault injection Intentionally (temporarily) inject bugs Determine if testing finds them Test the testing 38 19

General Strategies Summary General testing strategies (1) Automation (2) Testing incrementally (3) Comparing implementations (4) Bug-driven testing (5) Fault injection 39 Who Tests What Programmers White-box testing Pro: Programmer knows all data paths Con: Influenced by how code is designed/written Quality Assurance (QA) engineers Black-box testing Pro: No knowledge about the implementation Con: Unlikely to test all logical paths Customers Field testing Pros: Unexpected ways of using the software; debug specs Cons: Not enough cases; customers don t like participating in this process; malicious users exploit the bugs 40 20

Summary External testing taxonomy Statement testing Path testing Stress testing Boundary testing Internal testing techniques Checking invariants Checking function return values Changing code temporarily Leaving testing code intact 41 Summary (cont.) General testing strategies Automation Testing incrementally Regression testing Scaffolds and stubs Comparing independent implementations Bug-driven testing Fault injection Test the code, the tests and the specification 42 21