0% found this document useful (0 votes)
82 views3 pages

Software Testing and QA Assignment Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views3 pages

Software Testing and QA Assignment Guide

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
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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.

Common questions

Powered by AI

Data flow analysis involves examining the paths along which data can flow in a program and checking for irregularities. In the modified binary search function from Source 2, one such anomaly can be identified. After line 8, where 'high' is being set to 'mid - 1', 'mid' is immediately reassigned on line 9 to 'mid - 1'. This leads to a potential redundancy or oversight in variable assignment, as reassigning 'mid' without its use between 8 and 9 creates a data flow anomaly .

Creating a control flow graph allows testers to visualize the execution paths and interactions within the code. For the given array-ordering program, a control flow graph can help identify logical errors, such as improper swapping of values (line 5) and ensuring all paths leading from conditional branches are tested for correct ordering. It highlights errors in control logic and aids in achieving comprehensive test coverage by showing potential pathways in code execution .

To fully evaluate, consider cases where: (1) 'cos(x) > 0', so 'a' is set to 'x', by setting initial 'a=0.5, b=2' triggering line 9, as cos(1.25) > 0. (2) 'cos(x) <= 0', so 'b' is set to 'x', by setting 'a=1, b=3' ensuring cos(2) < 0, hence triggering line 11 .

The code's initial swaps ensure ordering before calculations, necessary for logical continuity in bisection. The subsequent conditional adjustments influence precision in reaching π/2. Inefficiency might arise from resetting 'a' and 'b' at line 5, potentially disrupting convergence by broadening the interval gratuitously, showcased in independent test cases. Thus, reevaluating conditional expressions' impact on iterative narrowing can optimize .

In the modified binary search code, an anomaly occurs because of the reassignment of the 'mid' variable after adjusting 'high'. Specifically, 'mid' is adjusted to 'mid - 1' directly after 'high' is updated on line 8. This introduces inefficiency, as 'mid' is recalculated without being utilized, potentially leading to data flow anomalies or incorrect path execution. The modification reduces efficiency by introducing redundancy without improving logical correctness .

The algorithm accomplishes this through a series of swaps. It compares pairs of numbers and swaps them such that the greatest values move to the higher index positions in the array in each step. After ordering, the largest number resides at the beginning due to the greatest comparisons made between the pairs, ensuring the algorithm outputs the greatest element accurately .

The swapping logic in lines 3-7 of the ordering function fails to adhere strictly to the specification, evidenced by the inefficient operation 'v[1] = v[1];'. This exposes a potential typo, as the logical swap was intended between two different elements. Correctness and efficiency are hindered since the program doesn’t ensure proper succession in array position adjustment, reflected in test case design .

Test cases to cover the swapping scenario and non-swapping scenario include: (A) Input values (a=3, b=1) will cause swapping, hence line 2 is executed. (B) Input values (a=1, b=3) will not cause swapping, hence line 2 is not executed. The swap is based on the condition 'if (a > b)' .

In the 'binsearch()' function, an infeasible path arises from the conditions set on 'mid'. Given that the function terminates once 'X' matches 'V[mid]' or the search bounds (low, high) are inappropriate, multiple condition-based paths will never occur (e.g., paths leading to 'high = mid - 1' and 'low = mid + 1' sequentially within a single execution). Infeasible paths are those logically impossible to hit due to the branching and control conditions applied .

Statement coverage will reveal if every line, such as improper assignment on line 5 where 'v[1] = v[1];', executes. Branch coverage checks both true and false branches of conditions, identifying issues like incorrect logic that fails to swap array elements properly. Specifically, addressing v[0]<v[1] and v[1]<v[2] ensures logical correctness and branch execution at lines 4, 8, and logical testing for all paths .

You might also like