- 132 Views
- Uploaded on
- Presentation posted in: Science / Technology

lecture 7 of cmsc420

lecture

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Software Testing Techniques

- Software must be tested to uncover as many errors as possible through a series of test cases.
- How to design test cases?

- Software testing techniques provide systematic guidance for designing tests that
- Exercise the internal logic and interfaces of every software component
- Exercise the input and output domains of the program to uncover errors in program function, behavior, and performance

"Bugs lurk in corners

and congregate at

boundaries ..."

OBJECTIVE

to uncover errors

CRITERIA

in a complete manner

CONSTRAINT

with a minimum of effort and time

black-box

methods

white-box

methods

Methods

Strategies

requirements

1. Incorrect or missing function

2. Interface error

3. External data base access

4. Behavior or performance error

5. Initialization and termination errors

output

input

- Can be applied at all levels of system development
- Procedures
- Analyze the requirements or specifications
- Determine inputs
- Valid input: software under test (SUT) processes the input correctly
- Invalid input: SUT detects them and handles them properly

- Expected outputs for those inputs are determined
- Construct the test, and execute the test
- Actual outputs are compared with the expected outputs

- Equivalence class testing
- Boundary value testing
- Decision table testing
- Use case testing

ages

Hire

Hire status

- A human resources system processes employment applications based on a person’s age. The rules are
- 0-16do not hire
- 16-18can hire on a part-time basis only
- 18-75can hire as a full-time employee
- 75-99do not hire

If (applicantage >=0 && applicationage <16)

HireStatus = “NO”

If (applicantage >=16 && applicationage <18)

HireStatus = “PART”

If (applicantage >=18 && applicationage <75)

HireStatus = “FULL”

If (applicantage >=75 && applicationage <=99)

HireStatus = “NO”

- Divides the input domain of a program into classes of data. Then identify the valid or invalid states for input conditions
- The input domain is usually too large for exhaustive testing.
- It is therefore partitioned into a finite number of sub-domains for the selection of test inputs.
- Each sub-domain is known as an equivalence class and serves as a source of at least one test input.

Input domain

2

1

3

4

Input domain

partitioned into four

classes.

15

17

37

82

Four test inputs, one

selected from each class.

Too many test inputs.

(100 inputs) 0-99

- Any data value in a class is equivalent
- if one test input in an equivalence class can detect a defect, all other test cases in the same equivalence class are likely to detect the same defect.
- If one test input in an equivalence class does not detect a defect, no other test cases in the same equivalence class is likely to detect the defect

- Test 100, 106?
- Based on the design
- Design-by-contract -> testing-by-contract
- Defensive design -> defensive testing

- Its approach is to create test cases only for the situations in which the pre-conditions are met.
- Openfile module
- File must exist
- Has the name of the file
- File must be openable
- Has the access rights to the file….

- An approach that tests under both normal and abnormal preconditions
- If the normal preconditions are met, test if the module achieve its normal postcondition
- If the pre-conditions are not met, test if the modules notify the caller by returning an error code or throwing an exception correctly

- Invalid inputs like -42, FRED, &*#?
- Testing-by-contract
- The answer is NO

- Defensive testing
- The answer is YES

ages

Function Hire

- If (applicantage >=0 && applicationage <16)
- HireStatus = “NO”

- If (applicantage >=16 && applicationage <18)
- HireStatus = “PART”

- If (applicantage >=18 && applicationage <75)
- HireStatus = “FULL”

- If (applicantage >=75 && applicationage <=99)
- HireStatus = “NO”

- if (age == 96 )
- HireStatus = “FULL” strange statement

Hire status

- Inputs to a program provide clues to partitioning.
- Example 1:
- Suppose that program P takes an input X, X being an integer.
- For X<0 the program is required to perform task T1 and for X>=0 task T2.

- The input domain is prohibitively large because X can assume a large number of values.
- However, we expect P to behave the same way for all X<0.
- Similarly, we expect P to perform the same way for all values of X>=0.
- We therefore partition the input domain of P into two sub-domains.

One test case:

X=-3

Equivalence class

X<0

X>=0

Equivalence class

Another test case:

X=15

All test inputs in the X<0 sub-domain are considered equivalent.

All test inputs in the X>=0 sub-domain are considered equivalent.

years

Less than 1 year, 5 days

1 – 5 years, 10 days

5 – 10 years, 15 days,

> = 10 years, 20 days

vacation

days

- Goofy Mortgage Company (GMC)
- Income: writes mortgages incomes between $1,000/month and $80,000/month
- Number of dwellings: Write a single mortgage for one through five houses
- Applicant: Make mortgages only for a person
- Dwelling types: Make mortgages on Condominisums, Townhouses, and Single

Income

Number of dwelling

Applicant

Dwelling types

Mortgage Qualification

Qualified or unqualified

- Income: is a continuous range of value
- One class of qualified values and two classes of unqualified values

Qualified (valid)

Three test inputs:

$500

$1,500

$90,000

$80,000

$1,000

Unqualified (invalid)

- Number of dwellings: is a discrete values within a range of permissible values
- One qualified class and two unqualified classes

Three test inputs:

2

8

0

Qualified

0

5

1

2

3

6

4

Unqualified

- applicant: a single qualified value
- One qualified and one unqualified classes

Corporation

Partnership

…

Person

two test inputs:

Person

Corporation

Qualified

Unqualified

- Dwelling types: a set of qualified values
- One qualified and one unqualified classes

Farm

Mobile Home

….

Single Family

Townhouse

Condo

Two test inputs:

Condo

Farm

qualified

unqualified

- Income: 3
- 1 qualified (valid), 2 unqualified (invalid)

- Number of dwelling: 3
- 1 qualified, 2 unqualified

- Applicant: 2
- 1 qualified, 1 unqualified

- Dwelling types: 2
- 1 qualified, 1 unqualified

All inputs are qualified

All inputs are unqualified

Which field is rejected?

Only one input is unqualified

- Testing by contract? Defense testing?
- By contract: test valid value
- Defensive testing: test invalid value

- Identify the equivalence classes of input or output.
- By contract: at least one class – valid class
- Defensive testing:
- Take at least 2 classes
- One class that satisfies the condition – valid class
- Second class that does not satisfy the condition – invalid class

- Design test cases based on the equivalence classes

- Testing by contract
- If an input condition specifies a range,
- One valid class

- If an input condition requires a specific value,
- One valid class

- If an input condition specifies a member of set,
- One valid class

- If an input condition is Boolean,
- One valid class

- Defensive testing
- If an input condition specifies a range,
- Three equivalence classes are defined
- One valid class, two invalid classes

- Three equivalence classes are defined
- If an input condition requires a specific value,
- Two equivalence classes
- One valid class, one invalid class

- Two equivalence classes
- If an input condition specifies a member of set,
- Two equivalence classes are defined
- One valid class, one invalid class

- Two equivalence classes are defined
- If an input condition is Boolean,
- Two equivalence classes are defined
- One valid class, one invalid class

- Two equivalence classes are defined

- In a computer store, the computer item can have a quantity between -500 to +500. What are the equivalence classes?

- In a computer store, the computer item type can be P2, P3, P4 and P5 (each type influences the price ) What are the equivalence classes?

- Bank account can be 500 to 1000, or 0 to 4999, or 2000 (integer). What are the equivalence classes?

- Suppose that program P takes one character X and one string Y as inputs.
- For character X: P performs task T1 for all lower case characters and T2 for upper case characters.
- For string Y: P performs task T3 for the null string and T4 for all other strings.
- Let us consider testing by contract

- We can select only 2 test inputs to cover all four equivalence classes. These are:
- X: lower case, Y: null string
- X: upper case, Y: not a null string

- Or we can select 4 test inputs to cover all the combinations
- How many should we have?
- Will answer it later

- Payroll office uses the following rules to calculate the monthly net income (testing by contract)
- Income
- If the gross income is less than $3000, federal tax bracket is 15%
- If the gross income is grater than or equal to $3000, and less than $5000, federal tax bracket is 20%
- Otherwise, federal tax bracket is 25%

- Status (F or T):
- The full-time employees pay 8% to the retirement account.
- The temporary employees pay 0% to the retirement account.

- Income

- Payroll office uses the following rules to calculate the monthly salary
- Income: should be greater than or equal to $500, and less than or equal to $20,000
- If gross income is less than $3000, federal tax is 15%
- If gross income is between $3000 and $5000, federal tax is 20%
- Otherwise, federal tax is 25%

- Status: should be F or T. Display error message if user gives wrong status
- The full-time employees pay 8% to the retirement account.
- The temporary employees pay 0% to the retirement account.

- Income: should be greater than or equal to $500, and less than or equal to $20,000

- An equivalence class is considered covered when at least one test has been selected from it.
- In the previous example, the two equivalence classes are non-overlapping. In other words the two sub-domains are disjoint.

- Suppose that program P takes three integers X, Y and Z. It is known that:
- X<Y T1, otherwise T2
- Z>Y T3, otherwise T4

X<Y, Z<=Y

X=4, Y=7, Z=1

X>=Y, Z<=Y

X=4, Y=2, Z=1

X<Y

X<Y, Z>Y

X=1, Y=7, Z=9

X>=Y

Z>Y

Z<=Y

X>=Y, Z>Y

X=4, Y=2, Z=9

- In this example, we could select 4 test cases as:
- X=4, Y=7, Z=1satisfies X<Y
- X=4, Y=2, Z=9satisfies X>=Y
- X=1, Y=7, Z=9satisfies Z>Y
- X=1, Y=7, Z=2satisfies Z<=Y

- Thus, we have one test case from each equivalence class combination.

- Can we reduce it to 2 test inputs and satisfy all four equivalence classes?
- X=4, Y=7, Z=1satisfies X<Y and Z<=Y
- X=4, Y=2, Z=9satisfies X>=Y and Z>Y

- Assumes the ‘single fault’ or “independence of input variables.”
- e.g. If there are 2 input variables, these input variables are independent of each other.

- Partition the test cases of each input variable separately into different equivalent classes.
- Choose the test case from each of the equivalence classes for each input variable independently of the other input variable

Assume the equivalence partitioning of input X is: 1 to 10; 11 to 20, 21 to 30

and the equivalence partitioning of input Y is: 1 to 5; 6 to 10; 11 to 15; and 16 to 20

We have covered everyone

of the 3 equivalence classes

for input X.

X

30

For ( x, y )

we have:

(24, 2)

(15, 8 )

( 4, 13)

(23, 17)

20

10

1

Y

1

5

10

15

20

General rule for # of test cases?

What do you think?

# of partitions of the largest set?

We have covered each of the 4 equivalence classes for input Y.

- This is the same as the weak normal equivalence testing except for
“multiple fault assumption”

or

“dependence among the inputs”

- All the combinations of equivalence classes of the variables must be included.

Assume the equivalence partitioning of input X is: 1 to 10; 11 to 20, 21 to 30

and the equivalence partitioning of input Y is: 1 to 5; 6 to 10; 11 to15; and 16 to 20

X

30

We have covered everyone

of the 3 x 4 Cartesian product of equivalence

classes

20

10

1

Y

1

5

10

15

20

General rule for # of test cases?

What do you think?

- Up to now we have only considered partitioning the valid input space.
- “Weak robust” is similar to “weak normal” equivalence test except that the invalid input variables are now considered.

A note about considering invalid input is that there may not

be any definition “specified” for the various, different invalid

inputs - - - making definition of the output for these invalid

inputs a bit difficult at times. (but fertile ground for testing)

Assume the equivalence partitioning of input X is 1 to 10; 11 to 20, 21 to 30

and the equivalence partitioning of input Y is 1 to 5; 6 to 10; 11 to 15; and 16 to 20

X

30

We have covered everyone of the 5 equivalence classes for input X.

20

10

1

Y

1

5

10

15

20

We have covered each of the 6 equivalence classes for input Y.

Assume the equivalence partitioning of input X is 1 to 10; 11 to 20, 21 to 30

and the equivalence partitioning of input Y is 1 to 5; 6 to 10; 11 to 15; and 16 to 20

X

30

We have covered everyone of the 5 equivalence classes for input X.

20

10

1

Y

Every time, only one invalid

1

5

10

15

20

We have covered each of the 6 equivalence classes for input Y.

- Does not assume “single fault” - - - assumes dependency of input variables
- “Strong robust” is similar to “strong normal” equivalence test except that the invalid input variables are now considered.

Assume the equivalence partitioning of input X is: 1 to 10; 11 to 20, 21 to 30

and the equivalence partitioning of input Y is: 1 to 5; 6 to 10; 11 to 15; and 16 to 20

X

30

We have covered everyone

of the 5 x 6 Cartesian product of equivalence

classes (including invalid

inputs)

20

10

1

Y

1

5

10

15

20

Divide the outputs into equivalence classes

Find out what input values would cause those outputs

Ages

Function Hire

NO

PART

FULL

years

- Less than 1 year, 0-5 days
- 1 – 5 years, 6-10 days
- 5 – 10 years, 11-15 days,
- >= 10 years, 16-20 days
- Find out the equivalence classes based on the output

vacation

days

- The airport parking office based on the type of parking lot, and hours to determine the rate
- Testing by contract

- In the previous examples we derived equivalence classes based on the conditions satisfied by the input or output data.
- Then we selected just enough tests to cover each partition.
- Think of the advantages and disadvantages of this approach!

- Errors occur at the boundaries of the input domain rather than in the center
- Rather than selecting any element of an equivalence class, testing at the edge of the class
- Suited to systems in which the input data takes on values within ranges

ages

Hire

- If (applicantage >=0 && applicationage <16)
- HireStatus = “NO”

- If (applicantage >=16 && applicationage <18)
- HireStatus = “PART”

- If (applicantage >=18 && applicationage <75)
- HireStatus = “FULL”

- If (applicantage >=75 && applicationage <=99)
- HireStatus = “NO”

Hire status

18

99

16

0

75

boundary

- Identify the equivalence classes
- Identify the boundaries of each equivalence class
- Create test cases
- One point on the boundary
- One point just below the boundary
- One point just above the boundary

- {-1,0,1} {15,16,17} {17,18,19} {74,75,76}
{98,99,100}

18

99

16

0

75

boundary

14

5

2

1

y2

10

11

6

9

8

13

12

y1

3

4

7

x1

x2

- Now suppose that a program takes two integers X and Y and that x1<=X<=x2 and y1<=Y<=y2.
In this case the four sides of the rectangle represent the boundary.

The heuristic for test selection in this case is:

Select one test at each corner (1, 2, 3, 4).

Select one test just outside of each of the four sides of the boundary (5, 6, 7, 8)

Select one test just inside of each of the four sides of the boundary (10, 11, 12, 13).

Select one test case inside of the bounded region (9).

Select one test case outside of the bounded region (14).

How many equivalence classes do we get?

- GMC
- Valid income $1000 to $80,000
- Valid number of dwelling: 1 to 5
- Applicant: Make mortgages only for a person
- Dwelling types: Make mortgages on Condominisums, Townhouses, and Single

Order charge

<$100, no discount

>=$100 and <$500, 10% discount

>=$500, 15% discount

Final charge

Just as we applied BVA to input data, we can apply it to output data.

Doing so gives us equivalence classes for the output domain.

We then try to find test inputs that will cover each boundary.

Order charge

10% discount

Discount

discount <= $100

Marriage

History

Insurance discount

Discount

Suited to complex business rules

Each rule becomes a test case

- OrderCharge: This component is used to calculate the charge of the order based on the customer type and the special discount
- Based on the customer type
No discount for “regular” customers

8% discount for “silver” customers

15% discount for “gold” customers

- If we have special discount:
- 10% discount applies to all customers

- Suppose you want to buy some funds, if all the following conditions are valid
- Symbol
- Quantity
- Sufficient money in your account
Then you can buy the funds

Build the decision table, and the test cases.

- The airport parking office based on the type of parking lot, and time (hours) to determine the rate
- Testing by contract

- Based on the use cases
- Use case: a scenario that describes the use of a system by an actor to accomplish a specific goal
- The set of use cases makes up the functional requirements of a system

- VCU registration system
- A student wants to register for a course

- Create at least one test case for the main success scenario
- at least one test case for each exception

- Withdraw money from ATM machine

- Normal flow:
- The user will indicate that she wants to order the items that have already been selected.
- The system will present the billing and shipping information that the user previously stored.
- The user will confirm that the existing billing and shipping information should be used for this order.
- The system will present the amount that the order will cost, including applicable taxes and shipping charges.
- The user will confirm that the order information is accurate.
- The system will provide the user with a tracking ID for the order.

- Alternative flow:
- 3A: The user enters billing and shipping information for the order. The user desires to use shipping and billing information that differs from the information stored in her account. This alternate flow also applies if the user does not maintain billing and / or shipping information in their account, or if the user does not have an account.
- The user will indicate that this order should use alternate billing or shipping information.
- The user will enter billing and shipping information for this order.
- The system will validate the billing and shipping information.
- The use case continues

- Alternative flow:
- 5A: The user will discover an error in the billing or shipping information associated with their account, and will change it.
- The user will indicate that the billing and shipping information is incorrect.
- The user will edit the billing and shipping information associated with their account.
- The system will validate the billing and shipping information.
- The use case returns to step 2 and continues.

- 5B: The user will discover an error in the billing or shipping information that is uniquely being used for this order, and will change it.
- The user will indicate that the billing and shipping information is incorrect.
- The user will edit the billing and shipping information for this order.
- The use case returns to step 3A step 3.

- Based on the internal paths, structures, and implementation of software under test (SUT)
- Require detailed programming skills

- General process:
- Analyze the SUT’s implementation
- Paths through the SUT are identified
- Inputs are chosen to cause the SUT to execute selected paths. Expected results are determined
- Run tests
- Actual outputs are compared with expected outputs

- Disadvantages:
- The number of execution paths may be large
- The tester must have the programming skills to understand and evaluate the SUT. Unfortunately, many testers today do not have this background.

- Advantages:
- Every path through the software under test has been identified and tested.

- Partition Testing
- reduces the number of test cases required to test a class in much the same way as equivalence partitioning for conventional software
- state-based partitioning
- categorize and test operations based on their ability to change the state of a class

- attribute-based partitioning
- categorize and test operations based on the attributes that they use

- category-based partitioning
- categorize and test operations based on the generic function each performs

- Based on the state transition diagram
- State: a condition in which a system is waiting for one or more events
- Transition: represents a change from one state to another caused by event
- Event: causes the system to change state
- Actions: an operation initiated because of a state change

Basic idea:

The state of an object is modified by operations

Methods can be modeled as state transitions

Test cases are sequences of method calls that traverse the state machine model

15.4/5

Slot: represents a slot of a computer model.

.... slots can be bound or unbound. Bound slots are assigned a compatible component, unbound slots are empty. Class slot offers the following services:

Incorporate: slots can be installed on a model as required or optional....

Bind: slots can be bound to a compatible component....

Unbind: bound slots can be unbound by removing the bound component.

IsBound: returns the current binding, if bound; otherwise returns the special value empty.

From the informal specification we can identify three states:

Not_present

Unbound

Bound

and four transitions

incorporate: from Not_presentto Unbound

bind: from Unbound to Bound

unbind: ...to Unbound

isBound: does not change state

TC-1: incorporate, bind

TC-2: incorporate, unBind, bind,

TC-3: incorporate, isBind, bind, isBound, unBind

- Airline reservation

Paid

Made

StartPayTimer

Pay

money

Ticketed

give

Info

cancel

cancel

cancel

expires

giveticket

Used

Cancelled by customer

Refund

Cancelled Nonpay

- Create a set of test cases such that
- All states are visited at least once
- All events are triggered at least once
- All transitions are exercised at least once under test

- All states are visited at least once

Paid

Made

StartPayTimer

Pay

money

Ticketed

give

Info

cancel

cancel

cancel

expires

giveticket

Used

Cancelled by customer

Refund

Cancelled Nonpay

- All events are triggered at least once

Paid

Made

StartPayTimer

Pay

money

Ticketed

give

Info

cancel

cancel

cancel

expires

giveticket

Used

Cancelled by customer

Refund

Cancelled Nonpay

- All transition are triggered at least once

Paid

Made

StartPayTimer

Pay

money

Ticketed

give

Info

cancel

cancel

cancel

expires

giveticket

Used

Cancelled by customer

Refund

Cancelled Nonpay

Class Account{

balance;

creditLimit;

……

Open()

Setup()

Deposit()

Withdraw()

Balance()

Summarize()

Creditlimit()

Close()

}

- Divide the operations into state operations and nonstate operations
- Design tests exercises operations that change state and those do not change state separately.
- Example
- Account class
- open(), setup(), deposit(), withdraw(), balance(), summarize(), creditlimit(), close(),
- P1: open-setup-deposit-withdraw-close
- P2: open-setup-deposit-deposit-withdraw-close
- P3: open-setup-deposit-withdraw-withdraw-close
- P4: open-setup-deposit-balance-withdraw-close
- P5: open-setup-deposit-summarize-withdraw-close
- P6: open-setup-deposit-creditlimit-balance-withdraw-close

- Account class

Classify methods (execution paths) as

inspectors: use, but do not modify, instance variables

modifiers: modify, but not use instance variables

inspector/modifiers: use and modify instance variables

others: do not use or modify instance variables

Example – class slot:

Slot()modifier

Incorporate ()modifier

bind()modifier

unbind()modifier

isbound()inspector

- Categorizes class operations based on the attributes that they use
- Design tests
- Operations that use attributes
- Operations that modify attributes
- Operations that do not use or modify attributes

- Example
- Account class
- open(), setup(), deposit(), withdraw(), balance(), summarize(), creditlimit(), close(),
- Attribute balance
- Inspector: balance(), summarize()
- Modifier: deposit(), withdraw()
- Others: open(), setup(), creditlimit(), close()

- Account class

- Categorize class operations based on the generic function that each perform
- Example
- Account class
- open(), setup(), deposit(), withdraw(), balance(),summarize(),creditlimit(),close(),

- Initialization: open, setup
- Computational: deposit, withdraw
- Queries: balance, summaries, creditlimit
- Termination:close

- Account class

- Test cases and class hierarchy
Class Base{

Inherited()

Redefined()

}

Class Derived : public Base

{

Redefined() ?? Test it or not?

Inherited() ??Test it or not?

}

- Identify the execution paths through a module of program code and then creates and executes test cases to cover those paths

... our goal is to ensure that all

statements and conditions have

been executed at least once ...

120

1

2

3

4

5

6

7

8

- Flow chart

121

- Process blocks: sequence of program statements
- Decision point: at which the control flow can change
- Junction point: control flows join together

- Level 1: 100% statement coverage
if (a>0) {x = x+1;}

if (b==3) {y = 0;}

a=1,b=3

- Level 2: 100% decision coverage
if (a>0) {x = x+1;}

if (b==3) {y = 0;}

Test each decision: true and false

if (a>0 && c==1) {x = x+1;}

if (b==3|| d<0) {y = 0;}

- Level 3: 100% condition coverage
if (x && y) {x = x+1;}

x= true, y = false

x = false, y = true

- Level 4: 100% multiple condition coverage
if (a>0 && c==1) {x = x+1;}

if (b==3|| d<0) {y = 0;}

a>0, c=1, b=3, d<0 both decisions true

a<=0, c=1, b=3, d>=0 both decisions false

a>0, c ≠ 1, b≠3, d<0

a<=0, c ≠ 1, b ≠ 3, d>=0

- Level 5: 100% path coverage
if (a>0) {x = x+1;}

if (b==3) {y = 0;}

a=1, b=3

a=1, b=2

a =-1, b =3

logic errors and incorrect assumptions

are inversely proportional to a path's

execution probability

we often

believe

that a path is not

likely to be executed; in fact, reality is

often counter intuitive

typographical errors are random; it's

likely that untested paths will contain

some

128

- Design the test case to
- Guarantee that all independent paths within a module have been exercised at least once
- Exercise all logical decision on their true and false sides
- Execute all loops at their boundaries and within their operational bounds
- Execute internal data structures at least once to ensure their validity

129

- First, we compute the cyclomatic complexity
- Cyclomatic complexity defines the number of independent paths in the basis set of a program and provides us with an upper bound for the number of test that must be conducted to ensure that all statements have been executed at least once

130

Cyclomatic complexity:

1. number of simple decisions

(predicate nodes) + 1

2. Number of enclosed areas

3. E-N +2 (flow graph)

In this case, V(G) = 4

131

get number x and y 1

if x> y 2

Display “x > y” 3

else

Display “x < = y” 4

End 5

132

- if (a or b)
then x

else y

- if (a and b)
then X

else y

133

Procedure average:

This procedure computes the average of 100 or fewer numbers that lie between bounding values; it also computes the sum and the total number valid

Interface returns average, total.input, total.valid

Interface accepts value, minimum, maximum

Type value [1:100] is scalar array

Type average, total.input, total.valid,minimum, maximum, sum is scalar

Type i is integer

134

i=1;

total.input = total.valid =0;

sum =0;

do while value[i]<>-999 and total.input <100

increment total.input by 1;

if value[i] >=minimum and value[i] <=maximum

then increment total.valid by 1;

sum = sum + value[i];

else skip

endif

increment i by 1;

enddo

if total.valid >0

then average = sum/total.valid

else average = -999;

endif

end average

135

i=1;

total.input = total.valid =0;

sum =0;

do while value[i]<>-999 and total.input <100

increment total.input by 1;

if value[i] >=minimum and value[i] <=maximum

then increment total.valid by 1;

sum = sum + value[i];

else skip

endif

increment i by 1;

enddo

if total.valid >0

then average = sum/total.valid

else average = -999;

endif

end average

1

3

2

4

6

5

7

8

9

10

11

12

13

14

136

i=1;

total.input = total.valid =0;

sum =0;

do while value[i]<>-999 and total.input <100

increment total.input by 1;

if value[i] >=minimum and value[i] <=maximum

then increment total.valid by 1;

sum = sum + value[i];

else skip

endif

increment i by 1;

enddo

if total.valid >0

then average = sum/total.valid

else average = -999;

endif

end average

1

2

3

4

6

5

7

8

9

10

11

12

13

137

- Determine the cyclomatic complexity of the flow graph
- 5 predicate nodes +1
- 17 edges -13 nodes +2
- 6 regions

138

1

2

3

4

5

6

7

8

Next, we derive the

independent paths:

Since V(G) = 4,

T

there are four paths

F

T

F

Path 1: 1,2,3,6,7,8

Path 2: 1,2,3,5,7,8

Path 3: 1,2,4,7,8

Path 4: 1,2,4,7,2,4,7,8

T

Finally, we derive test

F

cases to exercise these

paths.

139

- Determine the independent paths
- Path 1: 1,2,10,11,13
- Path 2: 1,2,10,12,13
- Path 3: 1,2,3,10,11,13
- Path 4: 1,2,3,4,5,6,8,9,2…
- Path 5: 1,2,3,4,5,6,7,8,9,2….
- Path 6: 1,2,3,4,5,8,9,2….

- Prepare test cases that will force execution of each path in the basis set

140

Procedure cashback:

This procedure computes the cashback for the Costco members based on their memberships and the credit card used for the purchase.

Interface returns cashback

Interface accepts members, given_number

Type member [1:100] is array. member[i] has four attributes: membership, card,expenditure, cashback

Type i is integer

141

sum =0; i =0;

do while i <=given_number

if membership ==‘Gold’ and card == “amex”

then member can get 2% cash back

calculate sum

endif

if membership ==‘Executive’

if Card = =“amex”

then member can get 4% cash back

calculate sum

else

member can get 2% cash back

calculate sum

endif

endif

increment i by 1;

Enddo

142

1

sum =0; i =0;

do while i <=given_number

if membership ==‘Gold’ and card == “amex”

then member can get 2% cash back

calculate sum

endif

if membership ==‘Executive’

if Card = =“amex”

then member can get 4% cash back

calculate sum

else

member can get 2% cash back

calculate sum

endif

endif

increment i by 1;

Enddo

2

3

4

5

6

7

8

9

10

143

Simple

loop

Nested

Loops

Concatenated

Loops

Unstructured

Loops

144

Minimum conditions—Simple Loops

1. skip the loop entirely

2. only one pass through the loop

3. two passes through the loop

4. m passes through the loop m < n

5. (n-1), and n passes through

the loop

where n is the maximum number

of allowable passes

145

Nested Loops

1. Start at the innermost loop. Set all outer loops to their

minimum iteration parameter values.

2. Test the min, min+1, typical, max-1 and max for the

innermost loop, while holding the outer loops at their

minimum values.

3. Move out one loop and set it up as in step 2, holding all

other loops at typical values. Continue this step until

the outermost loop has been tested.

Concatenated Loops

If the loops are independent of one another

then treat each as a simple loop

else* treat as nested loops

endif*

for example, the final loop counter value of loop 1 is

used to initialize loop 2.

146