0% found this document useful (0 votes)
164 views

Software Testing and Quality Assurance Assignment: Exercise 1

The document provides specifications and code for three different programming exercises: 1) A bisection algorithm to find π/2, with test cases to cover different code paths. 2) Code to order and find the greatest element in an integer array, with a control flow graph and test cases for coverage criteria. 3) A binary search routine with data flow graph and path questions.

Uploaded by

Muluneh Meri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
164 views

Software Testing and Quality Assurance Assignment: Exercise 1

The document provides specifications and code for three different programming exercises: 1) A bisection algorithm to find π/2, with test cases to cover different code paths. 2) Code to order and find the greatest element in an integer array, with a control flow graph and test cases for coverage criteria. 3) A binary search routine with data flow graph and path questions.

Uploaded by

Muluneh Meri
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Software testing and quality assurance assignment

Exercise 1
Assume following specification for some piece of code which could be part of a bisection
algorithm to find π/2:
 Input parameters are the float values a and b.
 Swap a and b unless a <= b.
 Set a and b to 1 and 3 unless cos(a) >= 0 or cos(b) <= 0.
 Set x to the arithmetic mean of a and b.
 Set a to x if cos(x) > 0 and b to x otherwise.
 Print a and b.
This is the code:
1. if (a > b) {
2. float tmp(b); b = a; a = tmp;
3. }
4. if (cos(a) < 0 || cos(b) > 0) {
5. a = 1; b = 3;
6. }
7. x = (a + b) / 2;
8. if (cos(x) > 0) {
9. a = x;
10. } else {
11. b = x;
12. }

 Write test cases that are able to cover the following situations:
(A) swap code at line 2
(B) line 2 is not executed
(C) line 5 is executed
(D) line 5 is not executed
(E) line 9 is executed
(F) Line 11 is executed
Exercise 2
Consider the following requirements and code:
The program shall take as input an array of three integer numbers.
The program shall output the greatest number among the elements of the array.
The program shall order the elements of the array in decreasing order.
1. public int[] order(int v[]) {
2. int tmp;
3. if (v[0]<v[1]) {
4. tmp = v[0];
5. v[1] = v[1];
6. v[1] = tmp;
7. }
8. if (v[1]<v[2]) {
9. tmp = v[0];
10. v[1] = v[2];
11. v[2] = tmp;
12. }
13. return v;
14. }

 Create the control flow graph


 Write the test cases in order to find the errors, using the following coverage criteria:
statement coverage, branch coverage, Path coverage
Exercise 3
For the code fragment shown below answer the following questions.
1. int binsearch(int X, int V[], int n){
2. int low, high, mid;
3. low = 0;
4. high = n - 1;
5. while (low <= high) {
6. mid = (low + high)/2;
7. if (X < V[mid])
8. high = mid - 1;
9. else if (X > V[mid])
10. low = mid + 1;
11. else
12. return mid;
13. }
14. return -1;
15. }

Figure: 1 Binary search routine code fragment version 1

A. Draw a data flow graph for the binsearch() function given above
B. Assuming that the input arrayV[] has at least one element in it, find an infeasible path in
the data flow graph for the binsearch() function.
C. Find a set of complete paths satisfying the all-defs selection criterion with respect to
variable mid.
D. Find a set of complete paths satisfying the all-defs selection criterion with respect to
variable high.
1. int modifiedbinsearch(int X, int V[], int n){
2. int low, high, mid;
3. low = 0;
4. high = n - 1;
5. while (low <= high) {
6. mid = (low + high)/2;
7. if (X < V[mid]) {
8. high = mid - 1;
9. mid = mid - 1;
10. }
11. else if (X > V[mid])
12. low = mid + 1;
13. else
14. return mid;
15. }
16. return -1;
17. }

Figure 2: Binary search routine code fragment version 2

E. Identify a data flow anomaly in the code given in Figure 2.

You might also like