Test code

Can someone please tell me which will be the test code of this program?

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class statistic{
    double * store;
    int noOfData;
public:
    statistic(double* s,int sz);
    statistic(const statistic &);
    ~statistic();
    double average() const;
    int size() const;
    const double* data() const;
};

statistic::statistic (double * s , int sz):noOfData(sz){
    store=new double[noOfData];
    for (int i=0;i<noOfData;i++) store[i]=s[i];
}


An object of the class represents a number (= noOfData) measurements (real numbers) stored in the table store.
The average method calculates the average of the measurements.
The method size returns the number of measurements.
This method returns a pointer data (pointer) in the table store.
Due to the dynamic memory allocation (see the implementation of copyright), the class needs to make copies (copy constructor) and Destroyer (destructor).

Requested the implementation of the class and appropriate test code.

Can someone tell me how to do the "Test Code"
To test your code, make a program which uses all of it's features, with normal and with extreme cases as well. (for example when noOfData is 0). Print the results, and compare them with the expected results. If all tests match, then you have a correctly working program*.

*given you choose your test cases carefully to test every bit of code you've written.
1
2
3
4
5
6
7
8
9
10
11
statistic *statisticTester = NULL;
 
  cout << "Creating a default statistic..." << endl;
  try {
    statisticTester = new statistic;
  } catch (bad_alloc xa) {
    cout << "Unable to allocate a statistic." << endl;
    return EXIT_FAILURE;
  }
 
  cout << "This statistic has " << statisticTester ->getnoOfData() << " data." << endl


is that correct?
Your code seems correct, but I wouldn't test if the dynamic allocation was successful, because it's not your code's fault if it fails.
Test statistics' member functions, constructors, destructors etc, so the code you've written. And compare them with the expected result.
I do not understand what to do .. Can you give me an example? How can I test the destractor?
Well, I would suggest you to count all methods functions of the class firstly, then if the compiler passes the code, simply calculate number of cases with computing the power.
For example, if there is 1 method with 2 functions then raise it to 2nd power.
Also, have a look over this link:
http://en.wikipedia.org/wiki/Logic_gate


I wonder if C++ Boost library has some standard C++ unit testing libraries just like JUnit has it for Java. What I mean is every developer uses the same library to code their unit testing code instead of everyone invent their own although I got to admit the objective is still achieved with each own invention :)
yes, it does. Check out boost.test.

EDIT: http://www.boost.org/doc/libs/1_44_0/libs/test/doc/html/index.html
Last edited on
Topic archived. No new replies allowed.