Write a unit test for addInventory(). Call redSweater.addInventory() with parame
ID: 3668696 • Letter: W
Question
Write a unit test for addInventory(). Call redSweater.addInventory() with parameter sweaterShipment. Print the shown error if the subsequent quantity is incorrect. Sample output for failed unit test given initial quantity is 10 and sweaterShipment is 50:
Note: UNIT TEST FAILED is preceded by 3 spaces.
#include <iostream>
using namespace std;
class InventoryTag {
public:
InventoryTag();
int getQuantityRemaining() const;
void addInventory(int numItems);
private:
int quantityRemaining;
};
InventoryTag::InventoryTag() {
quantityRemaining = 0;
}
int InventoryTag::getQuantityRemaining() const {
return quantityRemaining;
}
void InventoryTag::addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
}
int main() {
InventoryTag redSweater;
int sweaterShipment = 0;
int sweaterInventoryBefore = 0;
sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = 25;
cout << "Beginning tests." << endl;
// FIXME add unit test for addInventory
#%#%#@%@# ANSWER HERE @#%#@%@
cout << "Tests complete." << endl;
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
class InventoryTag {
public:
InventoryTag();
int getQuantityRemaining() const;
void addInventory(int numItems);
private:
int quantityRemaining;
};
InventoryTag::InventoryTag() {
quantityRemaining = 0;
}
int InventoryTag::getQuantityRemaining() const {
return quantityRemaining;
}
void InventoryTag::addInventory(int numItems) {
if (numItems > 10) {
quantityRemaining = quantityRemaining + numItems;
}
}
int main() {
InventoryTag redSweater;
int sweaterShipment = 0;
int sweaterInventoryBefore = 0;
sweaterInventoryBefore = redSweater.getQuantityRemaining();
sweaterShipment = 25;
cout << "Beginning tests." << endl;
testAddInventory(redSweater,sweaterShipment);
cout << "Tests complete." << endl;
return 0;
}
void testAddInventory(InventoryTag redSweater,int sweaterShipment)
{
redsweater.addInventory(sweaterShipment);
int quantity=redsweater.getQuantityRemaining();
if(quantity != 25)
cout<<" UnNIT TEST FAILED:addInventory()";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.