SlideShare a Scribd company logo
1 of 9
Exercise 1
    1. void f() {
    2.       float x;
    3.       float y;
    4.       x = read();
    5.       y = read();
    6.       if (x > 0)
    7.             x += 10;
    8.       y = y / x;
    9.       write(x);
    10.            write(y);
    11.      }

       Create the control flow graph of this program

       Write the test cases using the following coverage criteria

            Node coverage (statement coverage)

            Edge coverage (branch coverage)

       Do they identify the fault ?

Note well:

       Create one node for each atomic statement
       Compound statements must be divided into atomic statements
       Create an edge from node N1 to node N2 if during the execution the corresponding statements
        can be executed one immediately after the other




1
Exercise 2
The following Java function returns the highest absolute value in an array of maximum 5 integers (and
minimum 1), -1 if bad input (too large array, empty array, etc..).


    1. public int max_absolute(int[] numbers){
    2.             if(numbers.length > 5)
    3.                   return -1;
    4.             int max_value = 0;
    5.             for(int i = 0; i<numbers.length; i++){
    6.                   if (numbers[i] < 0 )
    7.                         max_value = Math.max(max_value,Math.abs(numbers[i]));
    8.                   else max_value = Math.max(max_value, numbers[i]);
    9.             }
    10.                  return max_value;
    11.      }


       Draw the control graph;
       The function is tested with the following test cases (separately): for each of them, define
        statement, branch and loop ratio coverage.
int[]   all_equals = {0,0,0,0,0};    // T1 - (0)
int[]   all_positive = {1,2,3,4,5};   // T2- (5)
int[]   all_negative = {-1,-2,-3,-4,-5}; // T3- (5)
int[]   out_of_size = {1,2,3,4,5,6};    // T4- (-1)
int[]   mixed = {-10,10,3,5,-6}; // T5 - (10)
int[]   empty = {};    // T6 - (-1)


best until now: T5 + T4 (provide 100% node coverage, 100% branch coverage, but
only 1/3 loop)
we need two more test cases to achieve 100% coverage on loop coverage: T6, T7 {1}

LOOP coverage requires T5+T6+T7
T5 {{-10,10,3,5,-6};                      Enters in            loop 5 times  coverage 1/3
T6 {}                                     Enters in            loop 0 times  coverage 1/3
                                          T4 IS NOT            SAME AS T6 for loop coverage
T7 {1}                                    Enters in            loop 1 time
Finally we achieve 100% coverage (Statement, branch            and loop) with T4, T5, T6, T7
T6 will fail




       Which test case will fail?
       Which combination of tests can assure 100% coverage of branches, statements and loops ?




2
Exercise 3
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




3
Exercise 4
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: Node
          (statement) coverage, Edge (branch) coverage, Path coverage




4
Exercise 5
Consider the following code:


#include <iostream.h>

int main(void) {
int nDependents, Exemption;
float Income, TaxSubTotal, TaxTotal;

cout << "Welcome to the Elbonian tax calculator. Enter your
yearly income: ";
cin >> Income;

// first if - check income

if (Income < 0) {
cout << "You cannot have a negative income.n";
return 0;
}

cout << "Enter the number of dependents you have, including
yourself: ";
cin >> nDependents;

// second if - check dependents

if (nDependents <= 0) {
cout <<"You must have at least one dependent.n";
return 0;
}

// third if (else-if) - compute tax subtotal

if (Income < 10000)
TaxSubTotal = .02 * Income;
else if (Income < 50000)
TaxSubTotal = 200 + .03 * (Income - 10000);
else
TaxSubTotal = 1400 + .04 * (Income - 50000);

Exemption= nDependents * 50;
TaxTotal=TaxSubTotal - Exemption;




5
// last if - check negative tax

if (TaxTotal<0) //In case of negative tax
TaxTotal=0;

cout   <<   "$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$ n";
cout   <<   "Elbonian Tax Collection Agency n";
cout   <<   "Tax Bill n";
cout   <<   " Citizen's Income: " << Income <<'n';
cout   <<   " Tax Subtotal: " << TaxSubTotal << 'n';
cout   <<   "Number of Dependents: " << nDependents << 'n';
cout   <<   " Tax Exepmtion: " << Exemption << 'n';
cout   <<   " Final Tax Bill: " << TaxTotal << 'n';
cout   <<   "$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$ n";

}



Build a test-suite for complete path coverage.




6
Exercise 6
Consider the following code:
    1. float foo (int a, int b, int c, int d, float e) {
    2.             float e;
    3.             if (a == 0) {
    4.                   return 0;
    5.             }
    6.             int x = 0;
    7.             if ((a==b) || ((c == d) && bug(a) )) {
    8.                   x=1;
    9.             }
    10.                  e = 1/x;
    11.                  return e;
    12.        }

Function bug(a) should return a value of true when passed a value of a=1.
Build:
    •    a test suite for 100% statement coverage
    •    a test suite for 100% branch coverage
    •    a test suite for 100% condition coverage




7
Exercise 7

A function converts a sequence of chars in an integer number. The sequence can start with a ‘-‘ (negative
number). If the sequence is shorter than 6 chars, it is filled with blanks (to the left side).
The integer number must be in the range minint = -32768 to maxint = 32767.
The function signals an error if the sequence of chars is not allowed.



    1. public class ConvertInt {
    2.      public int convert(char[] str) throws Exception{
    3.      if (str.length > 6)
    4.            throw new Exception();
    5.      int number=0;int digit; int i=0;
    6.      if (str[0]=='-')
    7.            i=1;
    8.      for(; i<str.length; i++){
    9.            digit = str[i] - '0';
    10.           number = number * 10 + digit;
    11.           }
    12.     if (str[0]=='-')
    13.                      number = -number;
    14.     if (number > 32767 || number < -32768)


number > 32767                        T        T              F      F
number < -32768                       T        F              T      F
                                      T6       T3             T8     T2
                                      Not                     “-
                                      feasible                60000”



    15.                                 throw new Exception();
    16.          return number;
    17.          }
    18.      }


Write tests in order to achieve:
    • node coverage
    • edge coverage
    • condition coverage for all if statements
    • multiple condition coverage of if statement at line 14
    • path coverage
Compare the tests cases written here with the ones written with the black box approach. Are they the same or
not?




8
Exercise 8
A queue of events in a simulation system receives events. Each event has a time tag.
It is possible to extract events from the queue, the extraction must return the event with lower time tag.
The queue discards events with negative or null time tag.
The queue must accept at least 100.000 events.
Events with the same time tag must be merged (i.e. the second received is discarded)
Define test cases to achieve
     • Node coverage
     • Edge coverage

    1. import java.util.Iterator;
    2. import java.util.LinkedList;
    3.
    4. public class EventsQueue {
    5.       private LinkedList queue;
    6.
    7.     public EventsQueue(){
    8.       queue = new LinkedList();
    9.     }
    10.          public void insert(int event){
    11.            int index = 0;
    12.            int size = queue.size();
    13.            while (index < size &&
    14.                       ((Integer)queue.get(index)).intValue() < event){
    15.                  index++;
    16.            }
    17.            queue.add(index, new Integer(event));
    18.          }
    19.          public int pop(){
    20.            Object o = queue.getFirst();
    21.            if (o != null)
    22.            return ((Integer) o).intValue();
    23.            else return -1;
    24.          }
    25.          public void print(){
    26.            Iterator i = queue.iterator();
    27.            int event;
    28.            while (i.hasNext()){
    29.                  event = ((Integer) i.next()).intValue();
    30.                       System.out.println(event + " ");
    31.            }
    32.          }}




9

More Related Content

What's hot (20)

C aptitude scribd
C aptitude scribdC aptitude scribd
C aptitude scribd
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
Computer Programming- Lecture 7
Computer Programming- Lecture 7Computer Programming- Lecture 7
Computer Programming- Lecture 7
 
06.Loops
06.Loops06.Loops
06.Loops
 
Arrays
ArraysArrays
Arrays
 
Templates presentation
Templates presentationTemplates presentation
Templates presentation
 
Ch9c
Ch9cCh9c
Ch9c
 
C interview question answer 2
C interview question answer 2C interview question answer 2
C interview question answer 2
 
C sharp chap4
C sharp chap4C sharp chap4
C sharp chap4
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Computer Programming- Lecture 10
Computer Programming- Lecture 10Computer Programming- Lecture 10
Computer Programming- Lecture 10
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Computer Programming- Lecture 5
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5
 
Scilab for real dummies j.heikell - part3
Scilab for real dummies j.heikell - part3Scilab for real dummies j.heikell - part3
Scilab for real dummies j.heikell - part3
 
Computer Programming- Lecture 3
Computer Programming- Lecture 3Computer Programming- Lecture 3
Computer Programming- Lecture 3
 
Ch4
Ch4Ch4
Ch4
 
Java small steps - 2019
Java   small steps - 2019Java   small steps - 2019
Java small steps - 2019
 
Array within a class
Array within a classArray within a class
Array within a class
 
Lec3
Lec3Lec3
Lec3
 
The Ring programming language version 1.5.2 book - Part 175 of 181
The Ring programming language version 1.5.2 book - Part 175 of 181The Ring programming language version 1.5.2 book - Part 175 of 181
The Ring programming language version 1.5.2 book - Part 175 of 181
 

Similar to White box-sol

White Box Testing (Introduction to)
White Box Testing (Introduction to)White Box Testing (Introduction to)
White Box Testing (Introduction to)Henry Muccini
 
How to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITHow to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITEgor Bogatov
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithmK Hari Shankar
 
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfShashikantSathe3
 
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfSHASHIKANT346021
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptxMrhaider4
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++NUST Stuff
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance PuzzlersDoug Hawkins
 
C++ control structure
C++ control structureC++ control structure
C++ control structurebluejayjunior
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docxjosies1
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework HelpC++ Homework Help
 
C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorNeeraj Kaushik
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2docSrikanth
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 

Similar to White box-sol (20)

Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
Lecture 1 mte 407
Lecture 1 mte 407Lecture 1 mte 407
Lecture 1 mte 407
 
White Box Testing (Introduction to)
White Box Testing (Introduction to)White Box Testing (Introduction to)
White Box Testing (Introduction to)
 
How to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITHow to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJIT
 
Advance data structure & algorithm
Advance data structure & algorithmAdvance data structure & algorithm
Advance data structure & algorithm
 
C sharp chap6
C sharp chap6C sharp chap6
C sharp chap6
 
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdf
 
LECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdfLECTURE 3 LOOPS, ARRAYS.pdf
LECTURE 3 LOOPS, ARRAYS.pdf
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Java Performance Puzzlers
Java Performance PuzzlersJava Performance Puzzlers
Java Performance Puzzlers
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
C Arrays.ppt
C Arrays.pptC Arrays.ppt
C Arrays.ppt
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
C-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression CalculatorC-Sharp Arithmatic Expression Calculator
C-Sharp Arithmatic Expression Calculator
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 

Recently uploaded

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitolTechU
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 

Recently uploaded (20)

EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Capitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptxCapitol Tech U Doctoral Presentation - April 2024.pptx
Capitol Tech U Doctoral Presentation - April 2024.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 

White box-sol

  • 1. Exercise 1 1. void f() { 2. float x; 3. float y; 4. x = read(); 5. y = read(); 6. if (x > 0) 7. x += 10; 8. y = y / x; 9. write(x); 10. write(y); 11. }  Create the control flow graph of this program  Write the test cases using the following coverage criteria  Node coverage (statement coverage)  Edge coverage (branch coverage)  Do they identify the fault ? Note well:  Create one node for each atomic statement  Compound statements must be divided into atomic statements  Create an edge from node N1 to node N2 if during the execution the corresponding statements can be executed one immediately after the other 1
  • 2. Exercise 2 The following Java function returns the highest absolute value in an array of maximum 5 integers (and minimum 1), -1 if bad input (too large array, empty array, etc..). 1. public int max_absolute(int[] numbers){ 2. if(numbers.length > 5) 3. return -1; 4. int max_value = 0; 5. for(int i = 0; i<numbers.length; i++){ 6. if (numbers[i] < 0 ) 7. max_value = Math.max(max_value,Math.abs(numbers[i])); 8. else max_value = Math.max(max_value, numbers[i]); 9. } 10. return max_value; 11. }  Draw the control graph;  The function is tested with the following test cases (separately): for each of them, define statement, branch and loop ratio coverage. int[] all_equals = {0,0,0,0,0}; // T1 - (0) int[] all_positive = {1,2,3,4,5}; // T2- (5) int[] all_negative = {-1,-2,-3,-4,-5}; // T3- (5) int[] out_of_size = {1,2,3,4,5,6}; // T4- (-1) int[] mixed = {-10,10,3,5,-6}; // T5 - (10) int[] empty = {}; // T6 - (-1) best until now: T5 + T4 (provide 100% node coverage, 100% branch coverage, but only 1/3 loop) we need two more test cases to achieve 100% coverage on loop coverage: T6, T7 {1} LOOP coverage requires T5+T6+T7 T5 {{-10,10,3,5,-6}; Enters in loop 5 times  coverage 1/3 T6 {} Enters in loop 0 times  coverage 1/3 T4 IS NOT SAME AS T6 for loop coverage T7 {1} Enters in loop 1 time Finally we achieve 100% coverage (Statement, branch and loop) with T4, T5, T6, T7 T6 will fail  Which test case will fail?  Which combination of tests can assure 100% coverage of branches, statements and loops ? 2
  • 3. Exercise 3 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 3
  • 4. Exercise 4 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: Node (statement) coverage, Edge (branch) coverage, Path coverage 4
  • 5. Exercise 5 Consider the following code: #include <iostream.h> int main(void) { int nDependents, Exemption; float Income, TaxSubTotal, TaxTotal; cout << "Welcome to the Elbonian tax calculator. Enter your yearly income: "; cin >> Income; // first if - check income if (Income < 0) { cout << "You cannot have a negative income.n"; return 0; } cout << "Enter the number of dependents you have, including yourself: "; cin >> nDependents; // second if - check dependents if (nDependents <= 0) { cout <<"You must have at least one dependent.n"; return 0; } // third if (else-if) - compute tax subtotal if (Income < 10000) TaxSubTotal = .02 * Income; else if (Income < 50000) TaxSubTotal = 200 + .03 * (Income - 10000); else TaxSubTotal = 1400 + .04 * (Income - 50000); Exemption= nDependents * 50; TaxTotal=TaxSubTotal - Exemption; 5
  • 6. // last if - check negative tax if (TaxTotal<0) //In case of negative tax TaxTotal=0; cout << "$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$ n"; cout << "Elbonian Tax Collection Agency n"; cout << "Tax Bill n"; cout << " Citizen's Income: " << Income <<'n'; cout << " Tax Subtotal: " << TaxSubTotal << 'n'; cout << "Number of Dependents: " << nDependents << 'n'; cout << " Tax Exepmtion: " << Exemption << 'n'; cout << " Final Tax Bill: " << TaxTotal << 'n'; cout << "$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$S$ n"; } Build a test-suite for complete path coverage. 6
  • 7. Exercise 6 Consider the following code: 1. float foo (int a, int b, int c, int d, float e) { 2. float e; 3. if (a == 0) { 4. return 0; 5. } 6. int x = 0; 7. if ((a==b) || ((c == d) && bug(a) )) { 8. x=1; 9. } 10. e = 1/x; 11. return e; 12. } Function bug(a) should return a value of true when passed a value of a=1. Build: • a test suite for 100% statement coverage • a test suite for 100% branch coverage • a test suite for 100% condition coverage 7
  • 8. Exercise 7 A function converts a sequence of chars in an integer number. The sequence can start with a ‘-‘ (negative number). If the sequence is shorter than 6 chars, it is filled with blanks (to the left side). The integer number must be in the range minint = -32768 to maxint = 32767. The function signals an error if the sequence of chars is not allowed. 1. public class ConvertInt { 2. public int convert(char[] str) throws Exception{ 3. if (str.length > 6) 4. throw new Exception(); 5. int number=0;int digit; int i=0; 6. if (str[0]=='-') 7. i=1; 8. for(; i<str.length; i++){ 9. digit = str[i] - '0'; 10. number = number * 10 + digit; 11. } 12. if (str[0]=='-') 13. number = -number; 14. if (number > 32767 || number < -32768) number > 32767 T T F F number < -32768 T F T F T6 T3 T8 T2 Not “- feasible 60000” 15. throw new Exception(); 16. return number; 17. } 18. } Write tests in order to achieve: • node coverage • edge coverage • condition coverage for all if statements • multiple condition coverage of if statement at line 14 • path coverage Compare the tests cases written here with the ones written with the black box approach. Are they the same or not? 8
  • 9. Exercise 8 A queue of events in a simulation system receives events. Each event has a time tag. It is possible to extract events from the queue, the extraction must return the event with lower time tag. The queue discards events with negative or null time tag. The queue must accept at least 100.000 events. Events with the same time tag must be merged (i.e. the second received is discarded) Define test cases to achieve • Node coverage • Edge coverage 1. import java.util.Iterator; 2. import java.util.LinkedList; 3. 4. public class EventsQueue { 5. private LinkedList queue; 6. 7. public EventsQueue(){ 8. queue = new LinkedList(); 9. } 10. public void insert(int event){ 11. int index = 0; 12. int size = queue.size(); 13. while (index < size && 14. ((Integer)queue.get(index)).intValue() < event){ 15. index++; 16. } 17. queue.add(index, new Integer(event)); 18. } 19. public int pop(){ 20. Object o = queue.getFirst(); 21. if (o != null) 22. return ((Integer) o).intValue(); 23. else return -1; 24. } 25. public void print(){ 26. Iterator i = queue.iterator(); 27. int event; 28. while (i.hasNext()){ 29. event = ((Integer) i.next()).intValue(); 30. System.out.println(event + " "); 31. } 32. }} 9