C/C++ [C++] Unit testing of a class HELP

AI Thread Summary
The discussion revolves around a C++ code snippet for an inventory management system using a class called `InventoryTag`. The code initializes an inventory quantity and includes methods to retrieve the quantity and add inventory, but only if the number of items exceeds 10. The user is attempting to implement unit tests for the `addInventory` method but is encountering issues. Initial tests show that adding 25 items updates the inventory correctly, but adding 5 does not affect the quantity as expected, leading to a failed unit test. The user is confused about the expected behavior and the specific requirements for writing unit tests, indicating they are using zyBooks for their coursework. Suggestions from other participants include using assertions to validate the expected outcomes of the tests, but the user reports that removing assertions allows the code to run without errors, although it does not meet the testing criteria. The conversation highlights the challenges of understanding unit testing in the context of the provided code.
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.
 
Technology 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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
35
Views
4K
Replies
22
Views
3K
Replies
23
Views
2K
Replies
1
Views
1K
Replies
1
Views
8K
Replies
8
Views
2K
Replies
5
Views
3K
Back
Top