Docsity

Programing Language Technologies and Paradigms - Lecture Slides | CMSC 433, Exams of Programming Languages

Toggle sidebar
bg1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2005
Testing and Specifications
February 3, 2005
Some slides adapted from FSE’98 Tutorial by Michal Young and
Mauro Pezze’
2
Testing
• Execute program on sample input data
– Check if output correct (acceptable)
• Goals
– Increase confidence program works correctly
• Acceptance Testing
– Find bugs in program
• Debug Testing
3
Example (Black Box)
% java TestServlet HelloWorld /F ooBar/Test > out
HTTP/1.0 200
Content-Type: text/plain
Hello /FooBar/Test
% diff out expectedOutp ut
4
Limitations of Testing
• Program runs on (very small) subset of input data
– Exhaustive testing usually impossible
• Too large input space (possibly infinite)
• Many situations hard to test
– Parallel code (due to non-determinism)
– Hard-to-reach states (e.g., error states)
– Inadequate test environment (e.g., lack of hardware)
• Testing cannot prove absence of bugs
– Especially a problem in security
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Programing Language Technologies and Paradigms - Lecture Slides | CMSC 433 and more Exams Programming Languages in PDF only on Docsity!

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2005

Testing and Specifications

February 3, 2005

Some slides adapted from FSE’98 Tutorial by Michal Young and Mauro Pezze’ 2

Testing

• Execute program on sample input data

– Check if output correct (acceptable)

• Goals

– Increase confidence program works correctly

• Acceptance Testing

– Find bugs in program

• Debug Testing

Example (Black Box)

% java TestServlet HelloWorld /FooBar/Test > out HTTP/1.0 200 Content-Type: text/plain Hello /FooBar/Test % diff out expectedOutput

Limitations of Testing

• Program runs on (very small) subset of input data

– Exhaustive testing usually impossible

• Too large input space (possibly infinite)

• Many situations hard to test

– Parallel code (due to non-determinism)

– Hard-to-reach states (e.g., error states)

– Inadequate test environment (e.g., lack of hardware)

• Testing cannot prove absence of bugs

– Especially a problem in security

5

Black Box Testing

• Pick subcomponent of program

– Internals of component not considered

• Give it inputs

• Compare against expected outputs

inputs outputs subcomponent being tested Is it correct? oracle 6

Black Box Testing

• Pick subcomponent of program

– Internals of component not considered

• Give it inputs

• Compare against expected outputs

– But how do I know what the expected outputs are?

– Depends on the software specification …

Software Specifications

• A specification defines the behavior of an abstraction

• This is the contract between user and provider

– Provider’s code must implement the specification

– Providers are free to change the implementation

• So long as the new code still meets the specification

– Users that depend implementation could be in trouble

• Only rely on specification

• Black box testing essentially checks compliance of an

implementation with its specification

Good Specifications are Hard and Rare

• Very difficult to get people to write specifications

– Even harder to keep them up to date

• Having specifications in a separate document

from code almost guarantees failure

– Rationale for Javadoc : extract a standalone

specification from the code and embedded comments

• Hard to accurately and formally capture all

properties of interest

– Always finding important details not specified

13

Types of External Specifications

• Specifications on methods

– Pre-conditions/requires: What must be true before call

– Post-conditions/effects: What is must be true after call

• Often relates final values to initial values

// precondition: the array d is sorted

// postcondition:

// returnValue >= 0 && d[returnValue] == x

// or (returnValue == -1 && x does not occur in d)

static int find(int d[], int x);

14

Types of Internal Specifications

• Specifications appearing within code itself

– i.e., comments

• Loop invariants: condition that must hold at the

beginning of each iteration of a loop

– d[0..i] is sorted

• Data structure or field invariants

– elementCount <= elementData.length

Behavior vs. Function

• Side effects

– Writes output to a file

– Could block on a condition or mutex

• Performance

– Should you specify performance of operations?

– As hard as 451: what kind of bound (upper bound,

amortized bound, expected bound, …), order of bound,

– But need at least informal specs

• Random access is fast, insertion/deletion may be

slow

Specifications and Subtyping

• Liskov substitution principle (original? formal stmt)

  • If for each object o1 of type S there is an object o2 of type T such that for all programs P defined in terms of T , the behavior of P is unchanged when o1 is substituted for o2 then S is a subtype of T.

– I.e, if anyone expecting a T can be given an S , then S is a

subtype of T.

• If we override a method, how do the specifications

of the original and new method relate?

17

Specifications and Subtyping (cont’d)

// precondition: the array d is sorted

// postcondition:

// returnValue >= 0 && d[returnValue] == x

// or (returnValue == -1 && x does not occur in d)

static int find(int d[], int x);

• If we override this method, can the new method

  • Have true as a precondition?
  • Have precond “ d is sorted and exists i s.t. d[i] == x ”?
  • Have postcond “( returnValue==- 1 and x does not occur in d)

or returnValue is first index such that d[returnValue] == x ”?

  • Throw NoSuchElementException rather than returning -

when x does not occur in d?

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2005

Testing and Specifications

February 8, 2005

Some slides adapted from FSE’98 Tutorial by Michal Young and Mauro Pezze’

What Makes a Good Specification?

• Sufficiently restrictive

– Forbids unacceptable implementations

• Sufficiently general

– Allows all acceptable implementations

• Clear

– Easy to understand

– A little redundancy may help (some people disagree)

Javadoc

• Integrates documentation into source code as comments

• Will generate an external specification

/ Javadoc Comment for this class / public class MyClass { /* Javadoc Comment for field** text / String text; / Javadoc Comment for method* setText *@param t Javadoc comment for parameter t / public void setText(String t) {...} }

25

Test Cases via Specifications

// Return true if x in a, else returns false boolean contains(int[] a, int x);

• Two “paths” in specification

– Test case where x is in a

– Test case where x is not in a

26

Test Cases via Inferred Implementation

• Think about how the implementation might look

– Test by boundary condition

• What test cases are likely to exercise the same

logic?

• Want to avoid redundant tests, to save time

– Test by common mistake

• What cases my be tricky to implement?

• At the same time, tests should still be

implementation-independent

Test Cases via Boundary Conditions

interface List { ... Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). public void add(int index, Object element) }

• Test with empty list

• Test with index at first/last element

• Others?

Test Cases via Common Mistakes

// Appends l2 to the end of l void append(List l1, List l2);

• Does append work if l1==l2?

class A { ...boolean equals(...); }

• Does equals work if operand is an Object?

29

White/Glass Box Testing

• Pick subcomponent of program

• Give it inputs

– Based on component code

• If you don’t execute the code, you don’t know

whether or not it works

• Compare against correct outputs (properties)

inputs outputs subcomponent being tested Is it correct? oracle 30

Statement Coverage

One test case (n=1, a[0]=-7, x=9) covers all statements Faults handling positive values of a[i] not revealed int select(int[] a, int n, int x) { int i=0; while (i<n && a[i] <x) { if (a[i]<0) a[i] = - a[i]; i++; } return 1; } (^) i++ i<n and a[i] <x a[i]< a[i] = - a[i]; return 1 true false true false i=

Branch Coverage

i= i<n and a[i] <x a[i]< a[i] = - a[i]; return 1 true false true false Must add test case (n=1, a[0]=7, x=9) to cover false branch of if Faults handling positive values of a[i] revealed. Faults exiting the loop with a[i] <x not revealed int select(int[] a, int n, int x) { int i=0; while (i<n && a[i] <x) { if (a[i]<0) a[i] = - a[i]; i++; } return 1; } (^) i++

Condition Coverage

i= i<n and a[i] <x a[i]< a[i] = - a[i]; return 1 true false true false Both i<n and a[i]<x must be false and true for different tests. Must add tests that cause loop to exit for a value greater than X. Faults that arise after several loop iterations not revealed. int select(int[] a, int n, int x) { int i=0; while (i<n && a[i] <x) { if (a[i]<0) a[i] = - a[i]; i++; } return 1; } (^) i++

37

The Testing Environment

• Want to create a scaffold for executing tests

– Code infrastructure to run tests and check output

• Many benefits

– Can automate testing process

– Useful for regression testing

• But, can take some time to implement

38

Testing Environment Components

• A user to generate input for tested component

• An oracle for verifying the results are correct

• These two may be combined into a single system

Unit Testing with Junit

• Testing environment for writing black-box tests

– Write special TestCase classes to test other classes

– Several ways to use/set up test cases

• Can be downloaded from

– http://www.junit.org

Junit Philosophy

• Iterative, incremental process

– Write small black-box test cases (as needed)

– Test-as-you-go

• I.e., after changes, when new method added, when

bug identified

• Junit test cases must be completely automated

– No human judgment

– Easy to run many of them at the same time

• Goal: lots of bang for the buck

– Even simple tests can find many bugs quickly

41

TestCase Example with Lists

import junit.framework.; import java.util.; public class ListTest extends TestCase { public void testAdd() { LinkedList l = new LinkedList(); Object o = new Object(); l.add(o); assertTrue(l.contains(o)); } public void testIsEmpty() { LinkedList l = new LinkedList(); assertTrue(l.isEmpty()); } }** Create objects Perform test/ check result Another test 42

To Execute Tests within a Class

• Pick a Test Runner:

– junit.awtui.TestRunner – Graphical

– junit.swingui.TestRunner – Graphical

– junit.textui.TestRunner – Textual

• Invoke on the test case class

> java junit.textui.TestRunner ListTest .. Time: 0. OK (2 tests)

Junit Components

• Test cases (class TestCase )

– Individual tests

– Can reuse test case setup (optional)

• Test suites (class TestSuite )

– Test case container

• Test runner (various classes)

– Executes test suites and presents results

Each Test Has Three Parts

• Code that creates test objects

– Create a subclass of junit.framework.TestCase

• Code that executes the test

– Override the method runTest() (which executes the test)

• Code that verifies the result

– E.g., use junit.framework.assertTrue() to check results

(throws exception is test fails)

Manually Constructing a Suite (cont’d)

• You can also create test suites more easily:

public static Test suite() { TestSuite suite = new TestSuite(); suite.addTest(new ListTest(“testAdd”)); suite.addTest(new ListTest(“testPushPop”)); return suite; }

• Or simply:

public static Test suite() { return new TestSuite(ListTest.class); }

Using a Test Suite

• Test runners will use static suite() method

• If no suite() method, suite selected automatically

– Every method that is public , returns void , takes no

arguments, and begins with “test”

– This is the way to go – for project 2, use this style

• Then use junit.*.TestRunner TestClass

– Or use DrJava