cipher
cipher

Reputation: 2484

Unit tests On a Basic Level

There are LOTS of questions about unit tests in SO. But What I could not find was a basic implementation example of some sort!

Suppose, I have an C++ code that does nothing but some complex number operations. Technically, the class would be:

class complex{
protected:
    float r,i;
public:
    complex(float rr=0, float ii=0):r(rr),i(ii){}

    complex operator+(complex a){
        return complex(r+a.r, i+a.i);
    }

    complex operator-(complex a){
        return complex(r-a.r, i-a.i);
    }

    complex operator*(complex a){
        return complex(r*a.r-i*a.i, r*a.i+i*a.r);
    }
};

Now what would be it's unit test? How would you write a unit test for the aforementioned class? Do I always need some kind of unit-testing-frame-work to start writing unit test? In short, HOW do i get started? If possible, please answer without suggesting to use any framework!

EDIT:

Thanks to comments and answers. What i now did was created a separate file that contained only my class say class_complex.cpp with some edits:

class test_complex;
class complex{.....
.....
friend class test_complex;
}

And then created another file named unittest_class_complex.cpp which contained the code

#include <iostream>

#include "class_complex.cpp"

/* Test Cases */
class test_complex:public complex{
public:
    void pass(){
        std::cout<<"Pass\n";
    }
    void fail(){
        std::cout<<"Fail\n";    
    }

    void test_default_values(){
        std::cout<<"Default Values Test: ";
        complex c1;
        if(c1.r==0 && c1.i==0){
            pass();
        } else {
            fail();
        }
    }

    void test_value_constructor(){
        std::cout<<"Non-Default Values Test: ";
        complex c1(10,2);
        if(c1.r==10 && c1.i==2){
            pass();
        } else {
            fail();
        }
    }

    void test_addition(){
        std::cout<<"Addition Test: ";
        complex c1(1,1), c2(2,2), c3;
        c3 = c1 + c2;
        if(c3.r==3 &&c3.i==3){
            pass();
        } else {
            fail();
        }
    }

};
int main(){
    test_complex c;
    c.test_default_values();
    c.test_value_constructor();
    c.test_addition();
    return 0;
}

And then build-ed the file and then ran it! Now: Am I going to the correct direction? Can this be termed as a kind of unit test?

Upvotes: 1

Views: 455

Answers (1)

Robert French
Robert French

Reputation: 724

You could have a few for that class..

instantiate and validate the defaults for r and i (assert) 

instantiate with non-default values and validate r and i are set properly (you need getters)

perform addition and validate the expected result (you can even do this with edge cases) 

perform subtraction and validate the expected result (again with edge cases) 

perform the multiplication and validate the expected result (again with edge cases) 

A unit test should test a single unit of code ... you have a constructor and three operator overloads that's a MINIMUM of four tests right there, but you should always check defaults / set values as well and running through a few edge cases never hurts if you think there may be something problematic in your code.

Upvotes: 1

Related Questions