0% found this document useful (0 votes)
5 views31 pages

Software Testing

Uploaded by

Saurav Tiwary
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)
5 views31 pages

Software Testing

Uploaded by

Saurav Tiwary
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

Lab 1

1. Program to find binary value of a number.


#include <iostream>
#include <string>
using namespace std;

string decimalToBinary(int n) {
if (n < 0) {
return "Error: Negative number";
}
if (n == 0) {
return "0";
}

string binary = "";


while (n > 0) {
binary = to_string(n % 2) + binary;
n /= 2;
}
return binary;
}

int main() {
int number;
cout << "Enter a positive integer: ";
cin >> number;

string binary = decimalToBinary(number);


cout << "Binary equivalent: " << binary << endl;

return 0;
}
Output

2. Program to find sum 1/1! + 1/2! + 1/3! + ………….

#include <iostream>
#include <iomanip>
using namespace std;

double factorialSum(int n) {
if (n <= 0) {
cout << "Error: Please enter positive integer" << endl;
return -1;
}

double sum = 0.0;


double fact = 1.0;

for (int i = 1; i <= n; i++) {


fact *= i;
sum += 1.0 / fact;
}

return sum;
}

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

3. 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;

int sum = digitSum(number);


if (sum != -1) {
cout << "Sum of digits: " << sum << endl;
}

return 0;
}
Output

4. Program to accept a 5 digit positive integer. Print out the inverse of


that number. For example given 12345, print out 54321.
#include <iostream>
using namespace std;

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;

int inverse = inverseNumber(number);


if (inverse != -1) {
cout << "Inverse number: " << inverse << endl;
}

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;
}

for (int i = 1; i <= n; i++) {


// Print leading spaces
for (int j = 1; j <= n - i; j++) {
cout << " ";
}

// Print increasing numbers


for (int j = 1; j <= i; j++) {
cout << j;
}

// Print decreasing numbers


for (int j = i - 1; j >= 1; j--) {
cout << j;
}

cout << endl;


}
}

int main() {
int rows;
cout << "Enter number of rows: ";
cin >> rows;

generatePattern(rows);

return 0;
}
Output

Lab 2
1. 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:
1. 10% on sales up to and including $1000
2. 15% on the next $800
3. 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:
1. Boundary Value Testing.
2. Robustness Testing.
3. 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;

const double LOCK_PRICE = 55.00;


const double STOCK_PRICE = 40.00;
const double BARREL_PRICE = 35.00;

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;
}

bool processSales(int l, int s, int b) {


locks = l;
stocks = s;
barrels = b;

if (!validateInput()) {
return false;
}

calculateTotalSales();
calculateCommission();
return true;
}

double getTotalSales() const { return totalSales; }


double getCommission() const { return commission; }
};

void runBoundaryValueTests(vector<TestCase>& testCases) {


cout << "\n=== BOUNDARY VALUE TESTING ===" << endl;

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, ""}
};

for (auto& test : bvtCases) {


SalesCommissionCalculator calc;
if (calc.processSales(test.locks, test.stocks, test.barrels)) {
test.actualSales = calc.getTotalSales();
test.actualCommission = calc.getCommission();
test.result = (abs(test.actualSales - test.expectedSales) < 0.01
&&
abs(test.actualCommission - test.expectedCommission)
< 0.01)
? "PASS" : "FAIL";
} else {
test.result = "FAIL";
}
testCases.push_back(test);
}
}
void runRobustnessTests(vector<TestCase>& testCases) {
cout << "\n=== ROBUSTNESS TESTING ===" << endl;

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"}
};

for (auto& test : robustCases) {


SalesCommissionCalculator calc;
if (calc.processSales(test.locks, test.stocks, test.barrels)) {
test.result = "FAIL";
} else {
test.result = "PASS";
}
testCases.push_back(test);
}
}

void runWorstCaseTests(vector<TestCase>& testCases) {


cout << "\n=== WORST CASE TESTING ===" << endl;

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, ""}
};

for (auto& test : wcCases) {


SalesCommissionCalculator calc;
if (calc.processSales(test.locks, test.stocks, test.barrels)) {
test.actualSales = calc.getTotalSales();
test.actualCommission = calc.getCommission();
test.result = (abs(test.actualSales - test.expectedSales) < 0.01
&&
abs(test.actualCommission - test.expectedCommission)
< 0.01)
? "PASS" : "FAIL";
} else {
test.result = "FAIL";
}
testCases.push_back(test);
}
}

void printTestReport(const vector<TestCase>& testCases) {


cout << "\n\n=== COMPREHENSIVE TEST REPORT ===" << endl;
cout <<
"========================================================
==========================================" << endl;
cout << left << setw(8) << "TC ID" << setw(8) << "Locks" << setw(8)
<< "Stocks"
<< setw(10) << "Barrels" << setw(12) << "Exp Sales" << setw(15)
<< "Exp Commission"
<< setw(12) << "Act Sales" << setw(15) << "Act Commission" <<
setw(8) << "Result" << endl;
cout <<
"========================================================
==========================================" << endl;

for (const auto& test : testCases) {


cout << left << setw(8) << test.id
<< setw(8) << test.locks
<< setw(8) << test.stocks
<< setw(10) << test.barrels;

if (test.expectedSales > 0) {
cout << fixed << setprecision(2)
<< setw(12) << test.expectedSales
<< setw(15) << test.expectedCommission
<< setw(12) << test.actualSales
<< setw(15) << test.actualCommission;
} else {
cout << setw(12) << "ERROR"
<< setw(15) << "ERROR"
<< setw(12) << "ERROR"
<< setw(15) << "ERROR";
}

cout << setw(8) << test.result << endl;


}
cout <<
"========================================================
==========================================" << endl;
}

void manualInput() {
int locks, stocks, barrels;

cout << "\n=== MANUAL INPUT ===" << endl;


cout << "Enter number of locks sold (1-70): ";
cin >> locks;

cout << "Enter number of stocks sold (1-80): ";


cin >> stocks;

cout << "Enter number of barrels sold (1-90): ";


cin >> barrels;

SalesCommissionCalculator calculator;

if (calculator.processSales(locks, stocks, barrels)) {


cout << fixed << setprecision(2);
cout << "\n=== SALES REPORT ===" << endl;
cout << "Locks sold: " << locks << endl;
cout << "Stocks sold: " << stocks << endl;
cout << "Barrels sold: " << barrels << endl;
cout << "Total sales: $" << calculator.getTotalSales() << endl;
cout << "Commission: $" << calculator.getCommission() << endl;
} else {
cout << "Error: Invalid input! Constraints:\n";
cout << "Locks: 1-70, Stocks: 1-80, Barrels: 1-90\n";
}
}

int main() {
vector<TestCase> allTestCases;

cout << "RIFLE SALES COMMISSION CALCULATOR" << endl;


cout << "=================================" << endl;
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
1. 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:
1. 10% on sales up to and including $1000
2. 15% on the next $800
3. 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:
1. Weak Normal Equivalence Class Testing.
2. Weak Robust Equivalence Class Testing.
3. Strong Normal Equivalence Case Testing.
4. And Strong Robust Equivalence Class 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;

const double LOCK_PRICE = 55.00;


const double STOCK_PRICE = 40.00;
const double BARREL_PRICE = 35.00;

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;
}

bool processSales(int l, int s, int b) {


locks = l;
stocks = s;
barrels = b;

if (!validateInput()) {
return false;
}

calculateTotalSales();
calculateCommission();
return true;
}

double getTotalSales() const { return totalSales; }


double getCommission() const { return commission; }
};
void runWeakNormalTests(vector<TestCase>& testCases) {
vector<TestCase> wnCases = {
{"WN-1", 1, 1, 1, 130.00, 13.00, 0, 0, ""},
{"WN-2", 35, 40, 45, 5425.00, 965.00, 0, 0, ""},
{"WN-3", 10, 10, 10, 1300.00, 145.00, 0, 0, ""},
{"WN-4", 18, 18, 18, 2340.00, 368.00, 0, 0, ""}
};

for (auto& test : wnCases) {


SalesCommissionCalculator calc;
if (calc.processSales(test.locks, test.stocks, test.barrels)) {
test.actualSales = calc.getTotalSales();
test.actualCommission = calc.getCommission();
test.result = (abs(test.actualSales - test.expectedSales) < 0.01 &&
abs(test.actualCommission - test.expectedCommission) <
0.01)
? "PASS" : "FAIL";
} else {
test.result = "FAIL";
}
testCases.push_back(test);
}
}
void runWeakRobustTests(vector<TestCase>& testCases) {
vector<TestCase> wrCases = {
{"WR-1", 1, 1, 1, 130.00, 13.00, 0, 0, ""},
{"WR-2", 0, 40, 45, 0, 0, 0, 0, "ERROR"},
{"WR-3", 35, 0, 45, 0, 0, 0, 0, "ERROR"},
{"WR-4", 35, 40, 0, 0, 0, 0, 0, "ERROR"},
{"WR-5", 71, 40, 45, 0, 0, 0, 0, "ERROR"}
};

for (auto& test : wrCases) {


SalesCommissionCalculator calc;
if (calc.processSales(test.locks, test.stocks, test.barrels)) {
if (test.expectedSales > 0) {
test.actualSales = calc.getTotalSales();
test.actualCommission = calc.getCommission();
test.result = (abs(test.actualSales - test.expectedSales) < 0.01 &&
abs(test.actualCommission - test.expectedCommission) <
0.01)
? "PASS" : "FAIL";
} else {
test.result = "FAIL";
}
} else {
if (test.expectedSales == 0) {
test.result = "PASS";
} else {
test.result = "FAIL";
}
}
testCases.push_back(test);
}
}

void runStrongNormalTests(vector<TestCase>& testCases) {


vector<TestCase> snCases = {
{"SN-1", 1, 1, 1, 130.00, 13.00, 0, 0, ""},
{"SN-2", 1, 1, 90, 3285.00, 517.00, 0, 0, ""},
{"SN-3", 1, 80, 1, 3295.00, 519.00, 0, 0, ""},
{"SN-4", 70, 1, 1, 3925.00, 645.00, 0, 0, ""},
{"SN-5", 70, 80, 90, 10850.00, 1970.00, 0, 0, ""},
{"SN-6", 10, 10, 10, 1300.00, 145.00, 0, 0, ""},
{"SN-7", 18, 18, 18, 2340.00, 368.00, 0, 0, ""}
};

for (auto& test : snCases) {


SalesCommissionCalculator calc;
if (calc.processSales(test.locks, test.stocks, test.barrels)) {
test.actualSales = calc.getTotalSales();
test.actualCommission = calc.getCommission();
test.result = (abs(test.actualSales - test.expectedSales) < 0.01 &&
abs(test.actualCommission - test.expectedCommission) <
0.01)
? "PASS" : "FAIL";
} else {
test.result = "FAIL";
}
testCases.push_back(test);
}
}

void runStrongRobustTests(vector<TestCase>& testCases) {


vector<TestCase> srCases = {
{"SR-1", 1, 1, 1, 130.00, 13.00, 0, 0, ""},
{"SR-2", 0, 40, 45, 0, 0, 0, 0, "ERROR"},
{"SR-3", 35, 0, 45, 0, 0, 0, 0, "ERROR"},
{"SR-4", 35, 40, 0, 0, 0, 0, 0, "ERROR"},
{"SR-5", 71, 40, 45, 0, 0, 0, 0, "ERROR"},
{"SR-6", 35, 81, 45, 0, 0, 0, 0, "ERROR"},
{"SR-7", 35, 40, 91, 0, 0, 0, 0, "ERROR"},
{"SR-8", -1, 40, 45, 0, 0, 0, 0, "ERROR"},
{"SR-9", 35, -5, 45, 0, 0, 0, 0, "ERROR"}
};

for (auto& test : srCases) {


SalesCommissionCalculator calc;
if (calc.processSales(test.locks, test.stocks, test.barrels)) {
if (test.expectedSales > 0) {
test.actualSales = calc.getTotalSales();
test.actualCommission = calc.getCommission();
test.result = (abs(test.actualSales - test.expectedSales) < 0.01 &&
abs(test.actualCommission - test.expectedCommission) <
0.01)
? "PASS" : "FAIL";
} else {
test.result = "FAIL";
}
} else {
if (test.expectedSales == 0) {
test.result = "PASS";
} else {
test.result = "FAIL";
}
}
testCases.push_back(test);
}
}

void printTestReport(const vector<TestCase>& testCases, const string&


testType) {
cout << "\n\n=== " << testType << " TEST REPORT ===" << endl;
cout <<
"=============================================================
=====================================" << endl;
cout << left << setw(8) << "TC ID" << setw(8) << "Locks" << setw(8) <<
"Stocks"
<< setw(10) << "Barrels" << setw(12) << "Exp Sales" << setw(15) <<
"Exp Commission"
<< setw(12) << "Act Sales" << setw(15) << "Act Commission" <<
setw(8) << "Result" << endl;
cout <<
"=============================================================
=====================================" << endl;

for (const auto& test : testCases) {


if (test.id.find(testType.substr(0,2)) != string::npos) {
cout << left << setw(8) << test.id
<< setw(8) << test.locks
<< setw(8) << test.stocks
<< setw(10) << test.barrels;

if (test.expectedSales > 0) {
cout << fixed << setprecision(2)
<< setw(12) << test.expectedSales
<< setw(15) << test.expectedCommission
<< setw(12) << test.actualSales
<< setw(15) << test.actualCommission;
} else {
cout << setw(12) << "ERROR"
<< setw(15) << "ERROR"
<< setw(12) << "ERROR"
<< setw(15) << "ERROR";
}

cout << setw(8) << test.result << endl;


}
}
cout <<
"=============================================================
=====================================" << endl;
}

int main() {
vector<TestCase> allTestCases;

cout << "RIFLE SALES COMMISSION CALCULATOR - EQUIVALENCE CLASS


TESTING" << endl;
cout <<
"=============================================================
" << endl;

runWeakNormalTests(allTestCases);
runWeakRobustTests(allTestCases);
runStrongNormalTests(allTestCases);
runStrongRobustTests(allTestCases);

printTestReport(allTestCases, "WEAK NORMAL");


printTestReport(allTestCases, "WEAK ROBUST");
printTestReport(allTestCases, "STRONG NORMAL");
printTestReport(allTestCases, "STRONG ROBUST");

return 0;
}

Output:
Lab 4
1.

You might also like