Software Testing
Software Testing
SCHOOL OF ENGINEERING
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY
LAB FILE
Course Outcomes:
CSH405B.1: Define software testing and infer mastery of the testing
techniques.
CSH405B.2: Identify the fundamental characteristics of good test case
design and apply
contemporary testing principles and techniques.
Bloom's Taxonomy: BT1, BT2, BT3
string decimalToBinary(int n) {
if (n < 0) {
return "Error: Negative number";
}
if (n == 0) {
return "0";
}
int main() {
int
number;
cout << "Enter a positive integer: ";
cin >> number;
return 0;
}
Output
#include <iostream>
#include <iomanip>
using namespace std;
double factorialSum(int n) {
if (n <= 0) {
cout << "Error: Please enter positive integer" << endl;
return -1;
}
int main() {
int terms;
cout << "Enter number of terms: ";
cin >> terms;
double result = factorialSum(terms);
if (result != -1) {
cout << fixed << setprecision(4);
cout << "Sum of series: " << result << endl;
}
return 0;
}
Output
Program to accept a positive integer. Then add up all the digits and
print out the sum. For example given 12345, the sum is 1+2+3+4+5.
#include <iostream>
using namespace std;
int digitSum(int n) {
if (n <= 0) {
cout << "Error: Please enter positive integer" << endl;
return -1;
}
int sum = 0;
int temp = n;
while (temp > 0) {
sum += temp % 10;
temp /= 10;
}
return sum;
}
int main() {
int number;
cout << "Enter a positive integer: ";
cin >> number;
return 0;
}
Output
int inverseNumber(int n) {
if (n < 10000 || n > 99999) {
cout << "Error: Please enter a 5-digit positive integer" << endl;
return -1;
}
int inverse = 0;
int temp = n;
while (temp > 0) {
inverse = inverse * 10 + (temp % 10);
temp /= 10;
}
return inverse;
}
int main() {
int number;
cout << "Enter a 5-digit positive integer: ";
cin >> number;
return 0;
}
Output
e. Program To generate pattern
1
121
12321
1234321
123454321
12345654321
#include <iostream>
using namespace std;
void generatePattern(int n) {
if (n <= 0) {
cout << "Error: Please enter positive integer" << endl;
return;
}
int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;
generatePattern(rows);
return 0;
}
Output
Lab 2
Course Outcomes:
CSH405B.1: Define software testing and infer mastery of the testing
techniques.
CSH405B.2: Identify the fundamental characteristics of good test case
design and apply
contemporary testing principles and techniques.
Bloom's Taxonomy: BT1, BT2,BT3
Rifle salespersons in the Arizona Territory sold rifle locks, stocks, and
barrels made by a gunsmith in Missouri. Locks cost $55.00, stocks cost
$40.00, and barrels cost $35.00. Salespersons had to sell at least one
complete rifle per month, and production limits are such that the most
one salesperson could sell in a month is 70 locks, 80 stocks, and 90
barrels. The sales person used to send the details of the sold items to the
gunsmith. The gunsmith then computed the sales person’s commission
as follows:
10% on sales up to and including $1000
15% on the next $800
And 20% on any sales in excess of $1800.
The company produces a monthly sales report that gives the total
number of locks, stocks, and barrels sold, the salesperson’s total dollar
sales, and finally his/her commission.
Write the C++ program based on the given case study and perform:
Boundary Value Testing.
Robustness Testing.
And Worst Case Testing.
Format of Test Case Report:
Test Input Input Input Expected Actual Test
Case Variable Variable Variable Output Output Result
Id 1 2 3 (Pass/Fail)
Code :
#include <bits/stdc++.h>
using namespace std;
struct TestCase {
string id;
int locks;
int stocks;
int barrels;
double expectedSales;
double expectedCommission;
double actualSales;
double actualCommission;
string result;
};
class SalesCommissionCalculator {
private:
int locks;
int stocks;
int barrels;
double totalSales;
double commission;
bool validateInput() {
return (locks >= 1 && locks <= 70) &&
(stocks >= 1 && stocks <= 80) &&
(barrels >= 1 && barrels <= 90);
}
void calculateTotalSales() {
totalSales = (locks * LOCK_PRICE) +
(stocks * STOCK_PRICE) +
(barrels * BARREL_PRICE);
}
void calculateCommission() {
if (totalSales <= 1000) {
commission = totalSales * 0.10;
} else if (totalSales <= 1800) {
commission = 1000 * 0.10 + (totalSales - 1000) * 0.15;
} else {
commission = 1000 * 0.10 + 800 * 0.15 + (totalSales - 1800) *
0.20;
}
}
public:
SalesCommissionCalculator(int l = 0, int s = 0, int b = 0) : locks(l),
stocks(s), barrels(b) {
totalSales = 0.0;
commission = 0.0;
}
calculateTotalSales();
calculateCommission();
return true;
}
vector<TestCase> bvtCases = {
{"BVT-1", 1, 1, 1, 130.00, 13.00, 0, 0, ""},
{"BVT-2", 1, 1, 90, 3285.00, 517.00, 0, 0, ""},
{"BVT-3", 1, 80, 1, 3295.00, 519.00, 0, 0, ""},
{"BVT-4", 70, 1, 1, 3925.00, 645.00, 0, 0, ""},
{"BVT-5", 70, 80, 90, 10850.00, 1970.00, 0, 0, ""},
{"BVT-6", 35, 40, 45, 5425.00, 965.00, 0, 0, ""}
};
vector<TestCase> robustCases = {
{"RT-1", 0, 40, 45, 0, 0, 0, 0, "ERROR"},
{"RT-2", 71, 40, 45, 0, 0, 0, 0, "ERROR"},
{"RT-3", 35, 0, 45, 0, 0, 0, 0, "ERROR"},
{"RT-4", 35, 81, 45, 0, 0, 0, 0, "ERROR"},
{"RT-5", 35, 40, 0, 0, 0, 0, 0, "ERROR"},
{"RT-6", 35, 40, 91, 0, 0, 0, 0, "ERROR"},
{"RT-7", -1, 40, 45, 0, 0, 0, 0, "ERROR"},
{"RT-8", 35, -5, 45, 0, 0, 0, 0, "ERROR"}
};
vector<TestCase> wcCases = {
{"WC-1", 10, 10, 10, 1300.00, 145.00, 0, 0, ""},
{"WC-2", 9, 9, 9, 1170.00, 125.50, 0, 0, ""},
{"WC-3", 18, 18, 18, 2340.00, 368.00, 0, 0, ""},
{"WC-4", 17, 17, 17, 2210.00, 343.00, 0, 0, ""},
{"WC-5", 19, 19, 19, 2470.00, 394.00, 0, 0, ""},
{"WC-6", 8, 8, 8, 1040.00, 106.00, 0, 0, ""},
{"WC-7", 20, 20, 20, 2600.00, 420.00, 0, 0, ""}
};
if ([Link] > 0) {
cout << fixed << setprecision(2)
<< setw(12) << [Link]
<< setw(15) << [Link]
<< setw(12) << [Link]
<< setw(15) << [Link];
} else {
cout << setw(12) << "ERROR"
<< setw(15) << "ERROR"
<< setw(12) << "ERROR"
<< setw(15) << "ERROR";
}
void manualInput() {
int locks, stocks, barrels;
SalesCommissionCalculator calculator;
int main() {
vector<TestCase> allTestCases;
int choice;
cout << "1. Run All Tests\n2. Manual Input\nChoose option (1 or 2):
";
cin >> choice;
if (choice == 1) {
runBoundaryValueTests(allTestCases);
runRobustnessTests(allTestCases);
runWorstCaseTests(allTestCases);
printTestReport(allTestCases);
} else {
manualInput();
}
return 0;
}
Output
Lab 3
Course Outcomes:
CSH405B.1: Define software testing and infer mastery of the testing
techniques.
CSH405B.2: Identify the fundamental characteristics of good test case
design and apply
contemporary testing principles and techniques.
CSH405B.3: Make use of Test Driven Development (TDD) to help
construct software
without code smells by choosing manual and automated testing
approaches.
Bloom's Taxonomy: BT1, BT2,BT3
Rifle salespersons in the Arizona Territory sold rifle locks, stocks, and
barrels made by a gunsmith in Missouri. Locks cost $55.00, stocks cost
$40.00, and barrels cost $35.00. Salespersons had to sell at least one
complete rifle per month, and production limits are such that the most
one salesperson could sell in a month is 70 locks, 80 stocks, and 90
barrels. The sales person used to send the details of the sold items to the
gunsmith. The gunsmith then computed the sales person’s commission
as follows:
10% on sales up to and including $1000
15% on the next $800
And 20% on any sales in excess of $1800.
The company produces a monthly sales report that gives the total
number of locks, stocks, and barrels sold, the salesperson’s total dollar
sales, and finally his/her commission.
Write the C++ program based on the given case study and perform:
Weak Normal Equivalence Class Testing.
Weak Robust Equivalence Class Testing.
Strong Normal Equivalence Case Testing.
And Strong Robust Equivalence Class Testing.
struct TestCase {
string id;
int locks;
int stocks;
int barrels;
double expectedSales;
double expectedCommission;
double actualSales;
double actualCommission;
string result;
};
class SalesCommissionCalculator {
private:
int locks;
int stocks;
int barrels;
double totalSales;
double commission;
bool validateInput() {
return (locks >= 1 && locks <= 70) &&
(stocks >= 1 && stocks <= 80) &&
(barrels >= 1 && barrels <= 90);
}
void calculateTotalSales() {
totalSales = (locks * LOCK_PRICE) +
(stocks * STOCK_PRICE) +
(barrels * BARREL_PRICE);
}
void calculateCommission() {
if (totalSales <= 1000) {
commission = totalSales * 0.10;
} else if (totalSales <= 1800) {
commission = 1000 * 0.10 + (totalSales - 1000) * 0.15;
} else {
commission = 1000 * 0.10 + 800 * 0.15 + (totalSales - 1800) * 0.20;
}
}
public:
SalesCommissionCalculator(int l = 0, int s = 0, int b = 0) : locks(l),
stocks(s), barrels(b) {
totalSales = 0.0;
commission = 0.0;
}
if (!validateInput()) {
return false;
}
calculateTotalSales();
calculateCommission();
return true;
}
int main() {
vector<TestCase> allTestCases;
cout << "RIFLE SALES COMMISSION CALCULATOR - EQUIVALENCE CLASS
TESTING" << endl;
cout <<
"=============================================================
" << endl;
runWeakNormalTests(allTestCases);
runWeakRobustTests(allTestCases);
runStrongNormalTests(allTestCases);
runStrongRobustTests(allTestCases);
return 0;
}
Output:
CSW442-PR SOFTWARE TESTING WORKSHOP
LAB ASSIGNMENT-4
Course Outcomes:
CSH405B.1: Define software testing and infer mastery of the testing techniques.
CSH405B.2: Identify the fundamental characteristics of good test case design and apply
contemporary testing principles and techniques.
CSH405B.3: Make use of Test Driven Development (TDD) to help construct software
without code smells by choosing manual and automated testing approaches.
Question 1: Perform Basis Path Testing for the following given source codes, in
following terms:
#include <stdio.h>
#include <conio.h>
int main()
{
int i,j,k,l,count=0;
for(i=1;i<=4;i++)
{
for(j=1;j<=4;j++)
{
for(k=1;k<=4;k++)
{
for(l=1;l<=4;l++)
{
if(i!=j&&j!=k&&i!=k&&i!=l&&j!=l&&k!=l)
{
printf("\n%d %d %d %d",i,j,k,l);
count++;
}
}
}
}
}
printf("\ncount = %d",count);
CSW442-PR SOFTWARE TESTING WORKSHOP
getch();
return 0;
}
Cyclomatic Complexity
Method 1 (Edges & Nodes): V(G) = 14 - 12 + 2 = 4
Method 2 (Predicate Nodes): 5 + 1 = 6
Method 3 (Independent Paths): 6
Independent Paths
All loops entered, if-condition true → valid permutation printed.
All loops entered, if-condition false → no print.
i-loop exits directly (no iterations).
j-loop exits directly.
k-loop exits directly.
l-loop exits directly.
Test Cases
Test Input Input Input Input Expected Actual Test
Case Id Variable Variable Variable Variable Output Output Result
1 2 3 4
TC1 1 2 3 4 Prints 1 2 Same Pass
34
TC2 1 1 2 3 No output Same Pass
(fails
condition)
TC3 i>4 - - - Loop Same Pass
exits, only
count=0
printed
TC4 1 j>4 - - No Same Pass
iteration
of j-loop
TC5 1 2 k>4 - No Same Pass
iteration
of k-loop
TC6 1 2 3 l>4 No Same Pass
iteration
of l-loop
CSW442-PR SOFTWARE TESTING WORKSHOP
Program to calculate overtime pay of 10 employees. Overtime is paid at the rate of Rs.
12.00 per hour for every hour worked above 40 hours. Assume that employees do not
work for fractional part of an hour.
employee=1;
while (employee<=10)
{
printf("\nEnter the [Link] Hrs done by employee %d:",employee);
scanf ("%d",&hour);
if(hour>40)
{
overtime = hour-40;
overtime_pay =12*overtime;
printf ("\nYour overtime pay is:%d\n\n", overtime_pay);
}
else if (hour<=40)
{
printf("\nYou won't get overtime pay.\n\n");
}
employee++;
}
getch();
return 0;
}
Cyclomatic Complexity
Predicate nodes: while loop + if + else = 2
V(G) = P + 1 = 2 + 1 = 3
Independent paths = 3
Independent Paths
Employee ≤ 10, hours > 40 → overtime calculated.
Employee ≤ 10, hours ≤ 40 → no overtime.
Employee > 10 → exit loop and terminate.
Test Cases
Test Case Employee Hours Expected Actual Result
Id Worked Output Output
TC1 1 45 Overtime Same Pass
pay = 60
TC2 2 40 No overtime Same Pass
TC3 11 - Exit loop Same Pass
CSW442-PR SOFTWARE TESTING WORKSHOP
When interest compounds q times per year at an annual rate of r % for n years, the
principle p compounds to an amount a as per the following formula
Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.
#include <stdio.h>
#include <conio.h>
int main()
{
int p,r,n,q,a,i,j,product;
float amt=1;
for(i=0;i<10;i++)
{
amt=1;
printf("enter the values of p,r,n and q: ");
scanf("%d%d%d%d",&p,&r,&n,&q);
product=n*q;
for(j=1;j<=product;j++)/*for calculating power value.
function also can be use but this is loop
chapter so we try to make use of loop*/
{
amt = amt * ( 1 +(float) r / q );/* r/q gives integer.(float) changes
to float value.*/
}
amt= p *amt;
printf("%f",amt);
getch();
return 0;
}
Cyclomatic Complexity
Predicate nodes: outer for + inner for = 2
V(G) = P + 1 = 2 + 1 = 3
Independent paths = 3
Independent Paths
Loop executes with valid inputs, compound interest calculated.
Outer loop condition fails immediately (i ≥ 10).
Inner loop condition fails immediately (product=0).
CSW442-PR SOFTWARE TESTING WORKSHOP
Test Cases
Test Case p r n q Expected Result
Id Output
TC1 1000 10 2 1 Amount > Pass
1000
TC2 2000 5 0 1 Amount = Pass
2000
TC3 - - - - No Pass
execution
when i≥10
Design a structure to store a length in yards, feet and inches(for eg7 yards,2 feet,3
inches). Write a function to find the difference between two measurements as
represented by these structures.
#include<stdio.h>
#include<conio.h>
struct len
{
int yards;
int feet;
int inches;
}s;
void convert(int y,int f,int in)
{int ff,inn;
ff=y*3;
inn=f*12;
printf("%d %d",ff,inn);
}
void main()
{ clrscr();
Cyclomatic Complexity
Only sequential steps, no decisions.
Predicate nodes = 0 → V(G) = 0 + 1 = 1
Independent Paths
Only 1 independent path: sequential execution.
Test Cases
Test Case Yards Feet Inches Expected Result
Id Output
TC1 7 2 3 Feet=21, Pass
Inches=24
TC2 0 5 10 Feet=0, Pass
Inches=60
CSW442-PR SOFTWARE TESTING WORKSHOP
LAB ASSIGNMENT-5
Course Outcomes:
CO1: Define software testing and infer mastery of the testing techniques.
CO2: Identify the fundamental characteristics of good test case design and apply
contemporary testing principles and techniques.
CO3: Make use of Test Driven Development (TDD) to help construct software without
code smells by choosing manual and automated testing approaches.
Actions (or x
Effects) x
x
Unstructured Approach
#include <iostream>
using namespace std;
int main() {
double salary, expenses, tax = 0, surcharge = 0;
return 0;
}
#include <iostream>
using namespace std;
int main() {
double salary, expenses;
cout << "Enter salary: ";
cin >> salary;
cout << "Enter expenses: ";
cin >> expenses;
double totalTax = calculateTax(salary, expenses);
cout << "Tax to be paid: " << totalTax << endl;
return 0;
}
CSW442-PR SOFTWARE TESTING WORKSHOP
Causes:
Effects:
Condition/Cause R1 R2 R3 R4
Salary ≤ 70000 X
Expenses ≤ 30000 X
Expenses ≤ 40000 X X X
Tax (10%) X
Tax (20%) X X X
Surcharge (5%) X
Surcharge (9%) X
CSW442-PR SOFTWARE
TESTING WORKSHOP