C/C++ What is the proper way to write a unit test for addInventory() in C++?

AI Thread Summary
The discussion revolves around writing a unit test for the `addInventory()` method in a C++ class. The main objective is to ensure that the method correctly updates the inventory quantity based on the input parameter. The user has successfully implemented the `addInventory()` function but is struggling with the unit test that verifies its functionality. The expected output for a failed test case is specified, where if the quantity does not match the expected value after adding inventory, a specific failure message should be printed. The user is advised to implement a check after calling `addInventory()` to compare the updated quantity with the expected value. If the check fails, the required error message should be displayed. Additionally, there is a suggestion regarding the use of a unit testing framework to simplify the testing process, allowing the user to focus on determining the necessary tests rather than the mechanics of test creation. The user acknowledges the need for conditional statements to implement the required checks effectively.
Teh
Messages
47
Reaction score
0
I got one correct for my code...what i am missing...any tips or guide?

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:

Beginning tests.
UNIT TEST FAILED: addInventory()
Tests complete.
Note: UNIT TEST FAILED is preceded by 3 spaces.


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  */
   addInventory()
 
   redSweater.addInventory(sweaterShipment);
 
  
   cout << "Tests complete." << endl;

   return 0;
}

Inventory is 0, shipment is 25. Testing that quantityRemaining was updated to 25.
Your value: 25
Testing with sweaterShipment of 25. addInventory updates quantityRemaining.
Your output: Beginning tests.
Tests complete.
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.
 
Technology news on Phys.org
Teh said:
I got one correct for my code...what i am missing...any tips or guide?

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:

Beginning tests.
UNIT TEST FAILED: addInventory()
Tests complete.
Note: UNIT TEST FAILED is preceded by 3 spaces.


You need to write code to add the amount of inventory specified, which you have done. Now you just need to check if you get the expected quantity after adding the inventory. If the check fails print the message they've asked for. I'm just not sure how the values they've provided as a sample would actually fail.

On another note if your being asked to do unit testing, why are they not having you use a unit testing framework to make the process of creating the tests easier so you can focus on the important part of unit testing which is determining which tests you require.
 
thanks for the help I needed to use if and else statement thanks!
 
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 have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
3K
Replies
75
Views
6K
Replies
3
Views
2K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
4
Views
4K
Back
Top