Test and debug the program

Programming
Types of Testing
Lesson Objectives
• Review and understand the concept
of testing and debugging a program.
• Review the concept of a dry run
test.
• Understand the concept of a trace
table.
• Perform try run tests using trace
tables.
Test and debug the program
• Testing is performed by a tester or
end user
– to find bugs, faults or error in the
program.
• The tester reports to the
programmer or program development
team.
Types of Testing
• There are two types of testing:
– A dry –run test
involves applying test data to the program
manually and tracing the logic or reasoning of
the program using test data that will test each
section of the program.
– This can even be done before the algorithm
is translated into the programming language.
Types of testing
• Computer testing
– Compile the program.
• A computer can only execute a program
written in machine language.
• Before the computer can run the Pascal
program, the latter must be converted to
machine language.
• The source code must be converted into
machine language. The program which does
this is a compiler.
• The compiler will check for errors in the
program. e.g syntax errors
Types of testing
• What are some types of syntax
errors in Lazarus?
– Omitting
•
•
•
•
;
,
‘
()
– i.e. any violation of syntax rules
Types of testing
• Run the program.
– The user requests the computer to execute the
program and the user supplies data to the program for
which he or she knows the answer.
– Such data is called test data.
– If the program does not give the correct answers the
user knows that the program has at least one logic
error.
– If the program contains logic errors we must debug the
program; find and correct those errors.
Types of testing
• Debugging is the process of fixing the
errors found by the testers.
NB
• Debugging is done by the programmer to
fix the errors during the debugging phase.
Question
• Distinguish between testing and
debugging. (2mks)
Answer
• Testing is performed by a tester or end user to find bugs,
faults or error in the program. The tester reports to the
programmer or program development team. However,
Debugging is the process of fixing the errors found by the
testers. Debugging is done by the programmer to fix the
errors during the debugging phase.
Types of testing
• What type(s) of testing have we
been doing so far?
Dry Run Testing
• Dry-running or desk checking or tracing involves
executing the program manually by using input
values for variables and recording what takes
place after each instruction is executed.
• Tracing is a technique that allows the user to
detect any logic errors in the program or
algorithm.
Trace Tables
• A trace table is a table that is completed by
tracing the instruction of the algorithm with
given data to arrive at solutions.
•
In designing the table, draw a column for each
variable used in the algorithm and draw enough
rows to form the cells to store the values.
• There should also be a heading ‘Output’ to record
what is printed.
Trace Tables
1. Choose simple input test data which is valid. Two or three sets of
data is valid.
2. Establish what the expected results should be for each teat case.
This is do a manual solution beforehand.
3. Create a trace table of all the variables in the algorithm.
4. Walk the first test case through the algorithm, keeping a step by
step record of the contents of each variable in the table as the
data passes through the logic until the algorithm reaches its logical
end.
5. Check that the expected result established in step two matches the
actual result developed in step 4
6. Repeat the process using a different set of data.
Example
Super six is the name of the junior cricket team.
The coach of super six has hired you to design a
program to provide him with statistics after each
match. Design an algorithm that reads a list
consisting of the number of runs scored by each
batsman in the team. There are six batsmen in
the team. Compute and print the average number
of runs scored by the team.
Solution
Computer Testing
• Computer testing
– involves running the program using test
data to discover any errors in the
program.
Computer Testing
• Computer testing involves the following steps:
– Compile the program.
– A computer can only execute a program written in
machine language. Before the computer can run the
Pascal program, the latter must be converted to
machine language.
– The source code must be converted into machine
language. The program which does this is a compiler.
The compiler will check for errors in the program .e.g.
syntax errors
Computer Testing
•
Run the program.
– The user requests the computer to execute the
program and the user supplies data to the program for
which he or she knows the answer. Such data is called
test data.
– f the program does not give the correct answers the
user knows that the program has at least one logic
error. If the program contains logic errors we must
debug the program; find and correct those errors.
Computer Testing
• The first step in computer testing is translating the high
level program into machine code.
• The program written in the specific programming language
is called the source code. The source code is translated
into object code by the compiler. Programmers write
programs in a form called source code.
• The source code consists of instructions in a particular
language, like C or FORTRAN. Computers, however, can only
execute instructions written in a low-level language called
machine language.
Computer Testing
• To get from source code to machine language, the programs
must be transformed by a compiler.
• The compiler produces an intermediary form called object
code. Object code is often the same as or similar to a
computer's machine language.
• The final step in producing an executable program is to
transform the object code into machine language, if it is
not already in this form.
• This can be done by a number of different types of
programs, called assemblers, linkers, and loaders.
Computer Testing
• Converting a program to machine language is
performed by a translator program.
•
These are programs that translate a specific
program from a high level language to machine
language that the computer can understand.
Interpreters, compilers and assemblers are all
translators.
Interpreter
• An interpreter translates the source program line by line
and if an error is detected then translation is aborted.
• If no errors are detected the interpreter instructs the
control unit to execute the translated instruction.
• This cycle is repeated for each instruction. Therefore the
interpreter translates one instruction at a time while the
program is being executed.
• Translated codes are not saved; therefore each time the
program is executed, translation is necessary.
Interpreter
Advantages
• It translates one instruction at a time; therefore it uses a
minimum amount of memory.
• It is also helpful in the debugging process because the
interpreter can relate error messages to the instruction
being executed
Disadvantage
• Each time the program is executed time is spent
interpreting, hence a slower execution time.
Compiler
• A compiler translates all program instructions at one time
and produces machine language which can be executed.
Error checks are performed and an error summary is used.
This is not done at run time.
• The translated codes are known as object codes and are
saved as a file on a disk. The object code has an EXE file
extension. It is then loaded or linked and executed.
• The object code is executed each time the program is run
but the source code needs to be compiled only once.
Compiler
Advantages
•
EXE files execute faster than source codes. The object codes are
saved and can be run at any time.
•
Object codes are harder for a user to change than source codes
•
The compiler checks the source code for syntax errors during
compilation
Disadvantages
•
As the entire program is translated it uses much more of the
computer’s memory.
Assembler
• The assembler translates mnemonic type instruction to
machine code.
• It translates low-level languages to machine language.
• The assembler program takes each program statement in
the source program and generates a corresponding bit
stream or pattern (a series of 0's and 1's).
Run the Program
• The second step in computer testing is to run
the program.
• The user requests the computer to execute the
program.
• The two terms commonly used with program
execution are linking and loading.
Linking
• A compiled object program is not executable by itself. It
needs to be combined with other system modules to form
an executable program.
• A linker also called a link editor and binder is a program
that combines object modules to form an executable
program.
• Linking combines various pieces of code and data together
to form a single executable object than can be loaded in
memory. Linking can be done at compile time at load time or
at run time.
Loading
• Loading involves copying a program from the hard
disk to main memory in order to put the program
in a ready to run state.
Executing The Program
• Program execution is the process whereby the program is
dispatched to the CPU.
• In many of the language compilers today, the process of
compiling, linking and executing are transparent to the
user.
• On some menu- driven systems the user selects compile
from the menu and the program is compiled and linked from
this single option.
• On other systems selecting ‘run’ results in the program
being linked and executed.
Program Execution
When the program is being executed or run the
user supplies test data to discover any errors in the
program. There are three types of errors:
• Syntax errors
• Logic errors
• Run-time errors
Syntax Errors
• Syntax errors are mistakes made by a non conformance to
the grammatical rules of the programming language.
•
For example if we forget to terminate an assignment
statement with a semi- colon or if a reserved word was
used incorrectly.
• Syntax errors stop the source code being converted to
machine code.
• Syntax errors can be easily located and corrected as the
compiler usually issues messages which identify the location
of the errors.
Logic Error
• Logic errors occur due to out of sequence instructions,
faulty comparisons and selections.
• For example in order to find the average of three numbers,
the numbers must be entered and added before dividing by
three. Dividing by three before entering the numbers
would be a logic error.
• The program will usually compile.
•
Compilation with logic errors does not generate any syntax
errors or warning messages but when the program is run it
produces the wrong results.
Runtime Error
• Run-time errors are errors that occur after the
program has been tested and debugged.
• They occur during the execution and running of
the program.
• They usually occur due to events such as division
by zero or lack of memory for the computer to
manipulate the data.