I need to write a test.cpp file for this class header. I need to test the constructors and the setters and getters. I've attached the header file with the functions that I need to test. Can anyone help me figure out how to test these in a main function in a test.cpp file?
#include <iostream>
#include <string>
usingnamespace std;
class Answer {
public:
/**
* Requires: Nothing
* Modifies: weight, text
* Effects: Default constructor
* Initializes weight to 0, text to an empty string
*/
Answer();
/**
* Requires: Nothing
* Modifies: weight, text
* Effects: Non-Default constructor
* Initializes weight to inWieght, text to inText
*/
Answer(int inWeight, string inText);
/**
* Requires: Nothing
* Modifies: Nothing
* Effects: Returns the text of this answer
*/
string getText();
/**
* Requires: Nothing
* Modifies: text
* Effects: Set the answer text to the inText
*/
void setText(string inText);
/**
* Requires: Nothing
* Modifies: Nothing
* Effects: Returns the text of this answer
*/
int getWeight();
/**
* Requires: Nothing
* Modifies: text
* Effects: Set the answer text to the inText
*/
void setWeight(int inWeight);
/**
* Requires: ins to be in the good state
* Modifies: ins, weight, text
* Effects: Initializes private data member from the input stream.
* Effects: Sets the values of this answer
* weight and text by reading values from the ins.
*/
void read(istream &ins);
/**
* Requires: outs to be in the good state
* Modifies: outs
* Effects: Writes the answer text to the output stream outs.
* Does not write the weight.
*/
void write(ostream &outs);
private:
// The text for this answer
string text;
// the weight factor for this answer
int weight;
};
/**
* Overloading >> and << for reading and writing Answer to and from streams.
*/
istream& operator >> (istream& ins, Answer& answer);
ostream& operator << (ostream& outs, Answer Answer);
#endif
#include "Answer.h"
void test(bool condition, const std::string& prompt)
{
std::cout << prompt << ": ";
if (condition)
std::cout << "true" << std::endl;
else
std::cout << "false" << std::endl;
}
int main()
{
// Test default constructor:
{
Answer answer;
test(answer.getText() == "", "Default ctor should init text to empty string");
test(answer.getWeight() == 0, "Default ctor should init weight to 0");
}
// Test 2-arg constructor:
{
Answer answer(42, "text");
test(answer.getText() == "text", "2-arg ctor should set text to the 2nd arg");
test(answer.getWeight() == 42, "2-arg ctor should set weight to 1st arg");
}
// ...
// test setWeight() and getWeight(), setText(), getText()
{
Answer answer;
answer.setWeight(42);
answer.setText("doge");
test(answer.getWeight() == 42, "SetWeight should correctly set the answer's weight");
test(answer.getText() == "doge", "SetText should correctly set the answer's text");
}
// ... also test read/write/>>/<< but I don't know how your file is supposed to look like so that's on you
}