program correctness & efficiency
TRANSCRIPT
-
8/7/2019 Program Correctness & Efficiency
1/45
Program Correctness & Efficiency
Briana Morrison
-
8/7/2019 Program Correctness & Efficiency
2/45
2
OutlineCategories of program errorsThrowing an exception: What it means How to do itWhy you should catch exceptionsA variety of testing strategies
Debugging techniques Big-O notation What it is How to use it to analyze an algorithms
efficiency
-
8/7/2019 Program Correctness & Efficiency
3/45
3
Program Defects and Bugs
An efficient program is worthless if it breaksor produces a wrong answer
Defects often appear in software after it is
deliveredTesting cannot prove the absence of defects
It can be difficult to test a software productcompletely in the environment in which it is
usedDebugging: removing defects
-
8/7/2019 Program Correctness & Efficiency
4/45
4
Major Categories of Defects
Syntax and other in-advance errors
Run-time errors and exceptions
Logic Errors
-
8/7/2019 Program Correctness & Efficiency
5/45
5
Syntax Errors
Syntax errors: grammatical mistakes in aprogram
The compiler detects syntax errors You must correct them to compile successfully
Some common syntax errors include:
Omitting or misplacing braces, parentheses,etc.
Misplacedend-of-comment
Typographical errors (in names, etc.)
Misplacedkeywords
-
8/7/2019 Program Correctness & Efficiency
6/45
6
Semantic ErrorsSemantic errors: may obey grammar, but violateother rules of the language
The compiler detects semantic errors You must correct them to compile successfully
Some common semantic errors include: Performing an incorrect operation on a primitive type
value
Invoking a member function notdefined Notdeclaring a variable before using it
Providing multiple declarations of a variable
Failure to include a library header
-
8/7/2019 Program Correctness & Efficiency
7/45
7
Run-time Errors or Exceptions
Run-time errors Occur during program execution (run-
time!)
Occur when the C++ run-time librarydetects an operation that it knows to beincorrect
Cause program to halt execution
Examples of run-time errors include Division by zero
Array index out of bounds
Null pointer reference
-
8/7/2019 Program Correctness & Efficiency
8/45
8
Run-time Errors or Exceptions(continued)
Run-Time Error Cause
Division by zero Integer division by zero.
Array index out of bounds Attempt to access an array with an index value that is greaterthan the array length (not detected by C++).
Null pointer reference. Attempt to dereference a pointer whose value is NULL.
-
8/7/2019 Program Correctness & Efficiency
9/45
9
Logic Errors
A logic error is programmer mistake in the design of a class or method, or
the implementation of an algorithm
Mostlogic errors Are not syntax or semantic errors: get by
the compiler
Do not cause run-time errors Thus they are difficult to find
Sometimes found through testing
Sometimes foundby users
-
8/7/2019 Program Correctness & Efficiency
10/45
10
Ways to Indicate an Error
Return a special value
Use a bool return value to indicatesuccess or failure.
Set a global variable.
Print an error message.
Print an error message and exit the
program.Put an input or output stream in a failstate.
Throw an exception
-
8/7/2019 Program Correctness & Efficiency
11/45
11
Example using a specialreturn value
The function get_element_of_x will returnnumeric_limits::min() if the index is notvalid.
Its use is illustrated as follows:int value = get_element_of_x(i);
if (value > numeric_limits::min()) {
// Do something with value
...} else {
// Recover from the error
...
}
// Continue normal processing
-
8/7/2019 Program Correctness & Efficiency
12/45
12
Throwing an Exception
Using special return values to indicateerrors can lead to complicated logic.
The error detection/recovery logic isintermixed with the normal processing.
Using exceptions allows separation of
the error detection/recovery logic fromthe normal processing logic.
-
8/7/2019 Program Correctness & Efficiency
13/45
13
Example using a throw/** Gets the value in the element of array x with subscript
index.
@param index The subscript of the element to be retrieved
@return The value stored at x[index]
@throws A std::out_of_range exception if index is not inrange.
*/
int get_element_of_x(int index) {
if (index < 0 || index >= X_SIZE) {
throw std::out_of_range(
"In get_element_of_x, "
"the value of index is out of range");
}
return x[index];
}
-
8/7/2019 Program Correctness & Efficiency
14/45
14
Uncaught exceptions
If there is no exception handler, the programexecution is aborted.
The message depends on the compiler and
operating system. The g++ compiler issues the message
Aborted
The Microsoft .NET C++ compiler issues the
messageThis application has requested the Runtime toterminate it in an unusual way.
Please contact the application's support teamfor more information.
-
8/7/2019 Program Correctness & Efficiency
15/45
15
Catching and Handling Exceptions
A try blockencloses functions that maythrow an exception.
One or more catch blocks (exception
handlers) follow the try block.try {
val = get_element_of_x(10);
}
catch (std::out_of_range& ex) {std::cerr
-
8/7/2019 Program Correctness & Efficiency
16/45
16
Standard Exceptions
Sta dard xc pti as hr w hr w by
bad_alloc Memor ot a aila le. T enewoperator.bad_cast
ttemptto ast a referencethat is notofthetargettype.
Thedynamic_castoperator.
bad_typeid
ttempttoobtainthetypeidof a null
pointer or the
efaulttypeidobject.
Thetypeidoperator.
domain_error Functionnot efined for the input alue.
otthrownbythe
standardlibrary.
invalid_argument nvalid function argument. Thebitsetconstructor.
length_error
ttempttocreate a stringor vector thatis larger thanthemaximum allowed si e.
Thestring andvectorclasses.
out_of_range ndex that is larger thansize() Theatoperator inthestring,
vector, anddequeclass.ios_base::failure
nerror flag is being setthatmatchesone setbyanearlier call totheexceptions function.
nput/output streamobjects after anerroneous input/outputoperaton.
-
8/7/2019 Program Correctness & Efficiency
17/45
17
Testing ProgramsA program with
No syntax/semantic errors, and
No run-time errors,
May still contain logic errorsBest case is logic error thatalways executes
Otherwise, hard to find!
Worst case is logic error in code rarely run
Goal of testing: Test every part of the code, ongood and bad/hard cases
-
8/7/2019 Program Correctness & Efficiency
18/45
18
Testing Defined
Testing:
Exercising a program under controlledconditions
Verifying the results
Purpose: detect program defects after
All syntax/semantic errors removed
Program compilesNo amount of testing can guarantee the absenceof defects in sufficiently complex programs
-
8/7/2019 Program Correctness & Efficiency
19/45
19
Levels of Testing
Unittesting: checking the smallest testablepiece
A method or class
Integrationtesting:
The interactions among units
System testing: testing the program in context
Acceptance testing: system testing intendedto show that the program meets its functionalrequirements
-
8/7/2019 Program Correctness & Efficiency
20/45
20
Some Types of Testing
Black-boxtesting:
Tests item based only on its
interfaces and functionalrequirements
Assumes noknowledge of internals
White-boxtesting:
Tests with knowledge of internalstructure
-
8/7/2019 Program Correctness & Efficiency
21/45
21
Preparing to TestDevelop test plan early, in the design phase How to test the software
When to do the tests
Who will do the testing What test data to use
Early test plan allows testing during design &coding
Good programmer practices defensiveprogramming
Includes code to detectunexpected or invaliddata
-
8/7/2019 Program Correctness & Efficiency
22/45
22
Testing Tips for Program Systems
Program systems contain collections ofclasses, each with several methods
A method specification should document
Input parameters
Expected results
Carefully document:
Each method parameter
Each class attribute (instance and static variable)
As you write the code!
-
8/7/2019 Program Correctness & Efficiency
23/45
23
Testing Tips for Program Systems
Trace execution by displaying methodname as you enter a method:
#define TRACING
...intcompute_weight (...) {#ifdef TRACINGcerr
-
8/7/2019 Program Correctness & Efficiency
24/45
24
Testing Tips for Program Systems
Display values of all input parameterson entry:
intcompute_weight (float volume,
float density) {#ifdef TRACING
cerr
-
8/7/2019 Program Correctness & Efficiency
25/45
25
Testing Tips for Program Systems
Display valuesof any class datamembers (instance and static) accessed
by the methodDisplay valuesof all method outputsat point of return from a method
Plan fortesting as you write eachmodule,
Not after the fact!
-
8/7/2019 Program Correctness & Efficiency
26/45
26
Developing Test Data
Specifytest dataduring analysis and design
For each level of testing: unit, integration, and system
Black-boxtesting: unit inputs outputs
Check all expected inputs
Check unanticipated data
White-boxtesting: exercise all code paths
Different tests to make each if test (etc.) true and false Called coverage
-
8/7/2019 Program Correctness & Efficiency
27/45
27
Developing Test Data (2)
Helpful to do both black- and white-boxtesting
Black-boxtests can be developed earlysince theyhave to do with the unit
specification
White-boxtests are developed withdetailed design or implementation: needcode structure
-
8/7/2019 Program Correctness & Efficiency
28/45
28
Testing Boundary Conditions
Exercise allpaths for
Hand-tracing in a structured walkthrough
Performing white-box testing
Must check specialcases:
boundary conditions
Examples:
Loop executes 0 times, 1 time, all the way to theend
Item not found
-
8/7/2019 Program Correctness & Efficiency
29/45
29
The Big Idea
How will we compare one datastructure with another?
How do I know when to use a Red-Black Tree versus a Hash Table?
We must know something about theoperations that each support, and theefficiency of those operations given aspecific implementation.
-
8/7/2019 Program Correctness & Efficiency
30/45
30
Efficiency of Algorithms
By comparing the efficiency of algorithms,we have one basis of comparison that can
help us determine the best technique touse in a particular situation.
Travel
- by foot- by car
- by train
- by airplane
-
8/7/2019 Program Correctness & Efficiency
31/45
31
Efficiency of Algorithms
Question: How can we characterize theperformance of an algorithm ...
Without regard to a specific computer?Without regard to a specific language?
Over a wide range of inputs?
Desire: Function that describes executiontime in terms of input size
Other measures might be memory
needed, etc.
-
8/7/2019 Program Correctness & Efficiency
32/45
32
Analysis of Algorithms
The measure of the amount of work analgorithm performs (time)
orthe space requirements of animplementation (space)
ComplexityOrder of magnitude
is a function of the number of data items.
-
8/7/2019 Program Correctness & Efficiency
33/45
33
Running Time
Most algorithms transforminput objects into outputobjects.
The running time of analgorithm typically growswith the input size.
Average case time is oftendifficult to determine.
We focus on the worst caserunning time. Easier to analyze
Crucial to applications such asgames, finance and robotics
0
20
40
60
80
100
120
Running
Time
1000 2000 3000 4000
Input Size
best case
average case
worst case
-
8/7/2019 Program Correctness & Efficiency
34/45
34
The Order of Performance:(Big) O
Basic idea:
1. Ignore constant factor: computer and languageimplementation details affect that: go for
fundamental rate of increase with problem size.2. Consider fastest growing term: Eventually, for
large problems, it will dominate.
Value: Compares fundamental performance
difference of algorithmsCaveat: For smaller problems, big-O worseperformer may actually do better
-
8/7/2019 Program Correctness & Efficiency
35/45
35
Big-O notation
For the selection sort, the number of comparisons isT(n) = n2/2 - n/2.
2
n
2
2nT(n) !
Entire expression is called the "Big-O" measure for thealgorithm.
Big-O notation provides a machine independent
means for determining the efficiency of anAlgorithm.
n = 100: T(100) = 1002/2 -100/2 = 10000/2 - 100/2 = 5,000 - 50 =4,950
-
8/7/2019 Program Correctness & Efficiency
36/45
36
Big-O Notation
Algorithm A requires time proportional to afunction F(N) given a reasonableimplementation and computer.
big-o notation is then written as O(n) where nis proportional to the number of data items
You can ignore constants
O(5n2
) is O(n2
)You can ignore low-order terms
O(n3 +n2 + n) is O(n3)
-
8/7/2019 Program Correctness & Efficiency
37/45
37
T(n) = O(f(n))
T(n) = time for algorithm on input size n
f(n) = a simpler function that grows at
about the same rate
Example: T(n) = 3n2+5n-17 = O(n2)
f(n) has faster growing term
no extra leading constant in f(n)
-
8/7/2019 Program Correctness & Efficiency
38/45
38
Growth Rates
Growth rates offunctions: Linear } n
Quadratic } n2
Cubic } n3
In a log-log chart,the slope of the linecorresponds to thegrowth rate of thefunction
1E+0
1E+2
1E+4
1E+6
1E+8
1E+10
1E+12
1E+14
1E+16
1E+181E+20
1E+22
1E+24
1E+26
1E+28
1E+ 0
1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n
T(n)
Cubic
uadratic
Linear
-
8/7/2019 Program Correctness & Efficiency
39/45
39
Constant Factors
The growth rate isnot affected by constant factors or
lower-order terms
Examples 102n 105 is a linear
function
105n2 108nis aquadratic function
1E+0
1E+2
1E+4
1E+6
1E+8
1E+10
1E+12
1E+14
1E+16
1E+18
1E+20
1E+22
1E+24
1E+26
1E+0 1E+2 1E+4 1E+6 1E+8 1E+10
n
T(n)
Quadrati
Quadrati
i ar
i ar
-
8/7/2019 Program Correctness & Efficiency
40/45
40
Growth Rates
Common Growth Rates
Big-O Name
O
(1) ConstantO(logn) Logarithmic
O(n) Linear
O(n logn) Log-Linear
O(n2) Quadric
O(n3) Cubic
O(2n) ExponentialO(n!) Factorial
-
8/7/2019 Program Correctness & Efficiency
41/45
41
Growth Rates
Constant O(1) print first item
Linear O(n) print list of items
Polynomial O(n2) print table of items
Logarithmic O(log n) binary search
Exponential O(2n) listing all thesubsets of a set
Factorial O(n!) traveling salespersonproblem
-
8/7/2019 Program Correctness & Efficiency
42/45
42
Efficiency of Algorithms(continued)
-
8/7/2019 Program Correctness & Efficiency
43/45
43
Chessboard Puzzle
Paymentscheme #1: $1 on first square, $2 onsecond, $3 on third, ..., $64 on 64th.
Paymentscheme #2: 1 on first square, 2 onsecond, 4 on third, 8 on fourth, etc.
Which isbest?
-
8/7/2019 Program Correctness & Efficiency
44/45
44
Chessboard Puzzle Analyzed
Paymentscheme #1: Total =$1+$2+$3+...+$64 = $64v65/2 = $1755
Paymentscheme #2: 1+2+4+...+263 =264-1 =$184.467440737trillion
Many cryptographic schemes require O(2n) worktobreak a key oflength nbits. A key oflengthn=40is perhaps breakable, butone withn=100is not.
-
8/7/2019 Program Correctness & Efficiency
45/45
45
ExponentialExponential AlgorithmsAlgorithms
n log2n n log2n n2 n3 2n2 1 2 4 8 4
4 2 8 16 64 16
8 3 24 64 512 256
16 4 64 256 4096 6553632 5 160 1024 32768 4294967296
128 7 896 16384 2097152 3.4 x 1038
1024 10 10240 1048576 1073741824 1.8 x 10308
65536 16 1048576 4294967296 2.8 x 1014 Forget it!