Testing (final thoughts). equals() and hashCode() Important when using Hash-based containers class Duration { public final int min; public final int sec;

Slides:



Advertisements
Similar presentations
P3 / 2004 Register Allocation. Kostis Sagonas 2 Spring 2004 Outline What is register allocation Webs Interference Graphs Graph coloring Spilling Live-Range.
Advertisements

Identity and Equality Based on material by Michael Ernst, University of Washington.
Reasoning About Code; Hoare Logic, continued
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.
CMSC 345, Version 11/07 SD Vick from S. Mitchell Software Testing.
Unit Testing CSSE 376, Software Quality Assurance Rose-Hulman Institute of Technology March 27, 2007.
== equals ? CSE 331 SOFTWARE DESIGN & IMPLEMENTATION EQUALITY Autumn 2011.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION TESTING II Autumn 2011.
Testing, cont. Based on material by Michael Ernst, University of Washington.
Software Testing Sudipto Ghosh CS 406 Fall 99 November 16, 1999.
Software Testing. “Software and Cathedrals are much the same: First we build them, then we pray!!!” -Sam Redwine, Jr.
Exam 2 Review. Exam 2 Exam 2 on Tuesday, April 7 Closed book, closed computer, closed phone You are allowed two “cheat” pages Either a standard 8.5x11-sized.
Software Testing.
CompSci 230 Software Design and Construction
Precision Going back to constant prop, in what cases would we lose precision?
Software Testing Sudipto Ghosh CS 406 Fall 99 November 9, 1999.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Software Systems Verification and Validation Laboratory Assignment 3
Equality CSE 331 University of Washington. Object equality A simple idea - we have intuitions about equality: - Two objects are equal if they have the.
Course Outline Traditional Static Program Analysis –Theory –Classic analysis and applications Points-to analysis, CHA, RTA –The Soot analysis framework.
Software Testing The process of operating a system or component under specified conditions, observing and recording the results, and making an evaluation.
Agenda Introduction Overview of White-box testing Basis path testing
Testing Testing Techniques to Design Tests. Testing:Example Problem: Find a mode and its frequency given an ordered list (array) of with one or more integer.
Test Coverage CS-300 Fall 2005 Supreeth Venkataraman.
White-box Testing.
CS 217 Software Verification and Validation Week 7, Summer 2014 Instructor: Dong Si
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 ==, equals(), and all that (Slides by David Notkin and Mike Ernst) 1.
1 Program Testing (Lecture 14) Prof. R. Mall Dept. of CSE, IIT, Kharagpur.
Reasoning about programs March CSE 403, Winter 2011, Brun.
Design - programming Cmpe 450 Fall Dynamic Analysis Software quality Design carefully from the start Simple and clean Fewer errors Finding errors.
Parametric Polymorphism and Java Generics. Announcements One day extension on HW5 Because of an error in my HW5 config HW6 out, due November 10 Grades.
Software Construction Lecture 19 Software Testing-2.
Testing CSE 160 University of Washington 1. Testing Programming to analyze data is powerful It’s useless (or worse!) if the results are not correct Correctness.
Theory and Practice of Software Testing
SOFTWARE TESTING. Introduction Software Testing is the process of executing a program or system with the intent of finding errors. It involves any activity.
1 Test Coverage Coverage can be based on: –source code –object code –model –control flow graph –(extended) finite state machines –data flow graph –requirements.
Week 6 MondayTuesdayWednesdayThursdayFriday Testing III Reading due Group meetings Testing IVSection ZFR due ZFR demos Progress report due Readings out.
Reasoning and Design (and Assertions). How to Design Your Code The hard way: Just start coding. When something doesn’t work, code some more! The easier.
White-Box Testing Statement coverage Branch coverage Path coverage
SOFTWARE TESTING LECTURE 9. OBSERVATIONS ABOUT TESTING “ Testing is the process of executing a program with the intention of finding errors. ” – Myers.
1 Software Testing. 2 What is Software Testing ? Testing is a verification and validation activity that is performed by executing program code.
Week 5-6 MondayTuesdayWednesdayThursdayFriday Testing I No reading Group meetings MidtermNo Section Testing II Progress report due Readings out Testing.
Software Testing. Software Quality Assurance Overarching term Time consuming (40% to 90% of dev effort) Includes –Verification: Building the product right,
White-Box Testing Pfleeger, S. Software Engineering Theory and Practice 2nd Edition. Prentice Hall, Ghezzi, C. et al., Fundamentals of Software Engineering.
Software Testing.
Black Box Testing PPT Sources: Code Complete, 2nd Ed., Steve McConnell
CompSci 230 Software Construction
Reasoning About Code.
Structural testing, Path Testing
Types of Testing Visit to more Learning Resources.
White-Box Testing.
Testing Approaches.
White-Box Testing.
Exam 2 Review.
UNIT-4 BLACKBOX AND WHITEBOX TESTING
CSE 331 Software Design and Implementation
Testing, conclusion Based on material by Michael Ernst, University of Washington.
Testing, cont., Equality and Identity
Software Testing (Lecture 11-a)
Reasoning About Code; Hoare Logic
Testing.
White-Box Testing.
Equality, conclusion Based on material by Michael Ernst, University of Washington.
CSE 303 Concepts and Tools for Software Development
Whitebox Testing.
White-Box Testing.
Whitebox Testing.
CSE 1020:Software Development
UNIT-4 BLACKBOX AND WHITEBOX TESTING
Software Testing.
Presentation transcript:

Testing (final thoughts)

equals() and hashCode() Important when using Hash-based containers class Duration { public final int min; public final int sec; public Duration(int min, int sec) { this.min = min; this.sec = sec; } Set ds = new HashSet<>(); ds.add(new Duration(5,10)); // true or false? ds.contains(new Duration(5,10)); // T or F? 2

equals() and hashCode() Now, let’s add equals and hashCode() : public boolean equals(Object o) { if (!(o instanceof Duration)) return false; Duration d = (Duration o); return min == d.min && sec == d.sec; } public int hashCode() { return min+sec; } Set ds = new HashSet<>(); ds.add(new Duration(5,10)); // T or F? ds.contains(new Duration(5,10)); // T or F? 3

Date Example Date is mutable, and takes the “compare values now” approach Date d1 = new Date(0); // Jan 1, :00:00 GMT Date d2 = new Date(0); System.out.println(d1.equals(d2)); // true d2.setTime(1); // a millisecond later System.out.println(d1.equals(d2)); // false Fall 15 CSCI 2600, A Milanova (based on slide by Michael Ernst) 4

Equality and Mutation We can violate rep invariant of a Set container (rep invariant: there are no duplicates in set) by mutating after insertion Set s = new HashSet (); Date d1 = new Date(0); Date d2 = new Date(1); s.add(d1); s.add(d2); d2.setTime(0); // mutation after d2 already in the Set! for (Date d : s) { System.out.println(d); } 5

Equality and Mutation Sets assume hash codes don’t change Set s = new HashSet (); Date d1 = new Date(0); Date d2 = new Date(1000); // 1 sec later s.add(d1); s.add(d2); d2.setTime(10000); s.contains(d2); s.contains(new Date(10000)); s.contains(new Date(1000)); Fall 15 CSCI 2600, A Milanova 6 // false // false again

Equality and Mutation Be very careful with elements of Sets Ideally, elements will be immutable objects Java spec for Sets warns about using mutable objects as set elements Same problem applies to keys in maps Fall 15 CSCI 2600, A Milanova 7

Implementing equals Efficiently equals can be expensive! How can we speed-up equals ? public boolean equals(Object o) { if (this == o) return true; // class-specific prefiltering (e.g., // compare file size if working with files) // Lastly, compare fields (can be expensive) } 8

White Box Testing Ensure test suite covers (covers means executes) “all of the program” We approximate ”all of the program” using coverage targets CFG nodes CFG edges CFG paths Assumption: higher coverage implies fewer remaining errors in the program Spring 15 CSCI 2600, A Milanova 9

White Box Testing: Dataflow-based Testing A definition (def) of x is x at the left-hand-side E.g., x = y+z, x = x+1, x = foo(y) A use of x is when x is at the right-hand side E.g., z = x+y, x = x+y, x>y, z = foo(x) A def-use pair of x is a pair of nodes, k and n in the CFG, s.t. k is a def of x, n is a use of x, and there is a path from k to n free of definition of x 10 k: x=… n: …= x… x = …

White Box Testing: Dataflow-based Testing Dataflow-based testing targets: write tests that cover paths between def-use pairs Intuition: If code computed a wrong value at a def of x, the more uses of this def of x we “cover”, the higher the probability we’ll expose the error If code had erroneous use of x, the more def-use pairs we “cover”, the higher the probability we’ll expose the use 11

A Buggy gcd // requires a,b > 0 static int gcd(int a, int b) { int x=a; int y=b; while (x != y) { if (x > y) { x = x – 2y; } else { y = y – x; } } return x; } Let’s test with gcd(15,6) and gcd(4,8). What’s the statement coverage? Branch? 12

CFG for Buggy GCD 13 1.x=a; y=b; 2. x!=y 3. x>y 4.x=x-2y;5.y=y-x; 7.res=x; True False True False “Merge” node Back edge 6. Def-use pairs for x: (node 1, node 2) (4,2) (1,3) (4,3) (1,4) (4,4) (1,5) (4,5) Def-use coverage targets: cover paths “connecting” def-use pairs (1,7) (4,7)

Def-use Coverage Targets The All-defs coverage target: for every def x, cover at least one path (free of definition of x), to at least one use x The All-uses coverage target: for every def- use pair of x, cover at least one path (free of definition of x) from the def x to the use x The All-du-paths coverage target: for every def-use pair of x, cover every path (free of definition of x) from the def x to the use x Spring 15 CSCI 2600, A Milanova 14

Def-use Coverage Targets Order def-use targets by strength (implication) Coverage target A is stronger than (i.e., implies) coverage target B if a test suite that achieves 100% coverage under A, achieves 100% coverage under B All-du-paths => All-uses => All-defs Spring 15 CSCI 2600, A Milanova 15

White Box Testing: Dataflow-based Testing Def-use coverage forces more targeted tests Higher probability to find errors Research has shown it leads to higher quality test suites Nevertheless, def-use coverage is rarely used in practice. Why? Difficult to find ground truth, i.e., the 100% Aliasing: x.f = A and B = y.f can be a def-use pair Spring 15 CSCI 2600, A Milanova 16

White Box Testing Covering “all of the program” Statement coverage Branch coverage Def-use coverage (a kind of path coverage) Path coverage Just a heuristic Even 100% all-uses coverage (one of the strongest targets) can still miss bugs High coverage increases confidence in test suite 17

In Practice 1. Write test suite based on spec (using paths-in- spec, equivalence partitioning, boundary values). Write test suite before code! 2. Write code 3. Run test suite, fix bugs, measure coverage (typically branch) 4. If coverage is inadequate, write more tests. Go to Step 3 Good “coverage” of paths-in-spec and boundary values typically (but not always!) yields good program coverage 18

Specification Tests vs. Implementation Tests Specification tests are black-box tests Based entirely on the specification If some behavior is undefined in spec, then we cannot write a test case to test this behavior Implementation tests are white-box tests Based on code Covers control-flow, different algorithmic paths Code may define behavior undefined in spec. Implementation tests must test this behavior Spring 15 CSCI 2600, A Milanova 19

Regression Testing Regression testing is the process of re- running test suite (e.g., after a bug fix) Whenever you find a bug Add test case with input that elicited the bug Verify that test suite fails Fix the bug Verify that test suite succeeds Ensures that we populate test suite with good tests Spring 15 CSCI 2600, A Milanova 20

Rules of Testing First rule of testing: do it early and do it often Best to catch bugs soon, before they hide Automate the process Regression testing will save time Second rule of testing: be systematic Writing tests is a good way to understand the spec Specs can be buggy too! When you find a bug, write a test first, then fix Spring 15 CSCI 2600, A Milanova (based on slide by Michael Ernst) 21

Testing, Summary Testing is extremely important. Two rules Test early and test often Be systematic Large test suite does not mean quality test suite Can miss relevant test cases Use black box and white box heuristics Write tests based on Specification (black-box heuristics) Implementation (white-box heuristics) Testing cannot prove absence of bugs But can increase confidence in program 22