Professional Documents
Culture Documents
Coding
1
Programming Practice
• The goal of the coding or programming activity is to
implement the design in the best possible manner.
simpler. 2
• As testing and maintenance costs are high,
aim of coding activity should be to write
code that reduces them
• Hence, goal should not be to reduce coding
cost, but testing and maintained cost, i.e.
make the job of tester and maintainer easier
3
• Code is read a lot more
• Coders themselves read the code many times for
debugging, extending etc
• Maintainers spend a lot of effort reading and
understanding code
• Other developers read code when they add to existing
code
• Hence, code should be written so it is easy to
understand and read, not easy to write
4
Common Coding Errors/ defects/
bugs
• Memory Leaks
• A memory leak is a situation where the memory is
allocated to the program which is not freed
subsequently.
• languages which do not have automatic garbage
collection
• They have little impact in short programs but can have
catastrophic effect on long running systems.
• A software system with memory leaks keeps
consuming memory, till at some point of time the
program may come to an exceptional halt because of5
the lack of free memory.
void memLeak( ) {
int *data = new int; *data = 15;
}
the ―*data‖ pointer is never deleted – which
means that the data it references is never
deallocated, and memory is wasted.
6
• Freeing an Already Freed Resource
• In general, in programs, resources are first
allocated and then freed.
9
10
Lack of Unique
• For example Addresses
in the string concatenation
function, we expect source and destination
addresses to be different.
synchronized (A){ } 12
}
• Race condition occurs when two threads
access same resource and result depends on
the order of the execution
14
Arithmetic exceptions
• These include errors like divide by zero and
floating point exceptions.
15
Off by One
• This is one of the most common errors
which can be caused in many ways.
16
Enumerated data types
• Overflow and underflow errors can easily
occur when working with enumerated types,
and care should be taken when assuming the
values of enumerated data types.
17
• An example of such an error is:
18
Buffer overflow
• Though buffer overflow is also a frequent
cause of software failures, in to days world
its main impact is that it is a security flaw
that can be exploited by a malicious user for
executing arbitrary code.
19
20
Some Programming Practices
• Control Constructs: it is desirable that as
much as possible single-entry, single-exit
constructs be used.
backward jump. 34
• Information Hiding: information hiding
should be supported where possible.
25
• Module Interface: A module with a
complex interface should be carefully
examined.
31
• Read Return to Be Checked: Often the
return value from reads is not checked,
assuming that the read returns the desired
values.
33
• a value is returned both in exception and
non-exception scenarios.
• at the caller site, the user will not be able to
distinguish between the two.
34
• when we have a return from try block. In
this case, if there is a return in finally also,
then the value from finally is returned
instead of the value from try.
35
• Correlated Parameters: Often there is an
implicit correlation between the parameters.
36
• Trusted Data Sources: Counter checks
should be made before accessing the input
data, particularly if the input data is being
provided by the user or is being obtained
over the network.
• We should put some checks, like parity checks,
hashes, etc., to ensure the validity of the
incoming data.
37
• Give Importance to Exceptions: To make
a software system more reliable, a
programmer should consider all possibilities
and write suitable exception handlers to
prevent failures or loss when such situations
occur.
38
Chapter Seven
Testing
39
Testing Fundamentals
40
• Fault is a condition that causes a system to fail
in performing its required function.
• A fault is the basic reason for software malfunction
and is synonymous with the commonly used term
bug.
• Failure is the inability of a system or
component to perform a required function
according to its specifications.
• A software failure occurs if the behavior of the
software is different from the specified behavior.
• A failure is produced only when there is a fault in
the system.
41
• Presence of a fault does not guarantee a
failure.
• In other words, faults have the potential to
cause failures and their presence is a
necessary but not a sufficient condition for
failure to occur.
• Note that the definition does not imply that
a failure must be observed.
• It is possible that a failure may occur but
not be detected.
42
• What is called a "failure" is dependent on
the project, and its exact definition is often
left to the tester or project manager.
• For example,
• is a misplaced line in the output a failure or
not? Clearly, it depends on the project; some
will consider it a failure and others will not.
43
• Take another example.
• If the output is not produced within a given
time period, is it a failure or not?
• For a real-time system this may be viewed as a
failure, but for an operating system it may not
be viewed as a failure.
44
• Presence of an error (in the state) implies
that a failure must have occurred, and the
observance of a failure implies that a fault
must be present in the system.
• However, the presence of a fault does not
imply that a failure must occur.
• The presence of a fault in a system only
implies that the fault has a potential to
cause a failure to occur
45
• Whether a fault actually manifests itself in a
certain time duration depends on many factors.
• This means that if we observe the behavior of a
system for some time duration and we do not
observe any errors, we cannot say anything about
the presence or absence of faults in the system.
• If, on the other hand, we observe some failure in
this duration, we can say that there are some faults
in the system.
46
Test Cases and Test Criteria
47
• Ideally, we would like to determine a set of
test cases such that successful execution of
all of them implies that there are no errors
in the program.
• This ideal goal cannot usually be achieved
due to practical and theoretical constraints.
• Each test case costs money, as effort is
needed to generate the test case, machine
time is needed to execute the program for
that test case, and more effort is needed to
evaluate the results.
48
• Therefore, we would also like to minimize
the number of test cases needed to detect
errors.
• These are the two fundamental goals of a
practical testing activity—maximize the
number of errors detected and minimize the
number of test cases (i.e., minimize the
cost).
49
Black-Box Testing
50
• There are no formal rules for designing test
cases for functional testing.
51
Equivalence Class Partitioning
52
• Example-1, the specifications of a module
that determines the absolute value for
integers specify one behavior for positive
integers and another for negative integers.
• In this case, we will form two equivalence
classes—one consisting of positive integers and
the other consisting of negative integers.
53
• Example-2 : User Authentication
54
• Example: consider a program that takes two
inputs—a string s of length up to N and an
integer n. The program is to determine the
top n highest occurring characters in s
55
Input Valid Equivalence Classes Invalid Equivalence
Classes
56
Boundary Value Analysis
57
• Example
• if the range is 0.0 < x < 1.0, then the test cases
are 0.0, 1.0 (valid inputs), and -0.1, and 1.1 (for
invalid inputs).
58
• Other techniques
• Cause-Effect Graphing
• Pair-wise Testing and
• State-Based Testing are also mechanisms by
which we perform black box testing.
59
White-Box Testing
60
Control Flow-Based Criteria
61
62
• branch coverage
• requires that each decision in the program be
evaluated to true and false values at least once
during testing.
• Other techniques of white-box testing
• data flow-based testing, and mutation testing
63
Test Case Template
64
65
The End
66