[C++] Unit testing of a class HELP

  • Context: C/C++ 
  • Thread starter Thread starter EvanET
  • Start date Start date
  • Tags Tags
    Class Testing Unit
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
4 replies · 3K views
EvanET
Messages
10
Reaction score
0
I'm given the code:
Code:
#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

   /* Your solution goes here  */

   cout << "Tests complete." << endl;

   return 0;
}

I've tried everything to my knowledge and used my textbook, still stuck.
 
Physics news on Phys.org
EvanET said:
I'm given the code:
Code:
#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

   /* Your solution goes here  */

   cout << "Tests complete." << endl;

   return 0;
}

I've tried everything to my knowledge and used my textbook, still stuck.

Hi EvanET! Welcome to MHB! ;)

How about something like:
Code:
assert(0 == sweaterInventoryBefore);
redSweater.addInventory(sweaterShipment);
assert(sweaterShipment == redSweater.getQuantityRemaining());
 
I like Serena said:
Hi EvanET! Welcome to MHB! ;)

How about something like:
Code:
assert(0 == sweaterInventoryBefore);
redSweater.addInventory(sweaterShipment);
assert(sweaterShipment == redSweater.getQuantityRemaining());

if i use the code it doesn't work, i took out the assert, the code ran but here are my results:

[PASS]Inventory is 0, shipment is 25. Testing that quantityRemaining was updated to 25.
Your value: 25
[PASS]Testing with sweaterShipment of 25. addInventory updates quantityRemaining.
Your output: Beginning tests.
Tests complete.
[PASS]Inventory is 25, shipment is 5. Testing that quantityRemaining remains 25.
Your value: 25
✖Testing sweaterShipment of 5. addInventory does not update quantityRemaining.
Expected output: Beginning tests.
UNIT TEST FAILED: addInventory()
Tests complete.
Your output: Beginning tests.
Tests complete.
Tests aborted.
 
EvanET said:
if i use the code it doesn't work, i took out the assert, the code ran but here are my results:

[PASS]Inventory is 0, shipment is 25. Testing that quantityRemaining was updated to 25.
Your value: 25
[PASS]Testing with sweaterShipment of 25. addInventory updates quantityRemaining.
Your output: Beginning tests.
Tests complete.
[PASS]Inventory is 25, shipment is 5. Testing that quantityRemaining remains 25.
Your value: 25
✖Testing sweaterShipment of 5. addInventory does not update quantityRemaining.
Expected output: Beginning tests.
UNIT TEST FAILED: addInventory()
Tests complete.
Your output: Beginning tests.
Tests complete.
Tests aborted.

It appears there's some program running around this program that expects something specific, but for me that is unknown.
Which instruction did you receive about writing unit tests?
 
I like Serena said:
It appears there's some program running around this program that expects something specific, but for me that is unknown.
Which instruction did you receive about writing unit tests?
I am using zybooks