Testing class constructors, setters, and gutters

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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  #include <iostream>
#include <string>

using namespace 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#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
}
Topic archived. No new replies allowed.