1

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?

cipher
  • 2,414
  • 4
  • 30
  • 54
  • 1
    Please see this post https://stackoverflow.com/questions/4540925/unit-tests-in-c?rq=1 specifically look at the second answer (by @AndyT). You basically just set up a bunch of test cases that you already know the answer to. The unit tests will let you know if any of your test cases fail. – Cory Kramer Apr 08 '14 at 13:01
  • @Cyber : Thanks. Can you please respond to my EDIT? Am I moving towards the right direction? – cipher Apr 08 '14 at 13:32
  • 1
    It is a unit test indeed. But using a ready made framework like CppUnit, boost::unit_test or gtest is highly recommended as they provide lots of handy tools and also can handle much more severe situations easily (like crashes in tests). – berkus Apr 08 '14 at 13:38

1 Answers1

1

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.

Robert French
  • 686
  • 4
  • 12
  • Now the question: how ? – cipher Apr 08 '14 at 13:11
  • and that's where the question of frameworks comes into it. http://stackoverflow.com/questions/87794/c-unit-testing-framework – Robert French Apr 08 '14 at 13:12
  • Please see the edited version! Am i doing the correct 'thing' ? – cipher Apr 08 '14 at 13:28
  • 1
    Well technically you're testing units of your code so yes, you can say that they are unit tests, but I agree with @berkus that using a framework makes life much easier and consistent. You would also want to test without inheriting (unless complex is only a base class and will always be inherited from like your testing class is doing). The whole point in unit testing is to ensure that the code does what its expected to do when being executed the way its intended while developing. The TDD methodology (Test Driven Development) uses a pattern called Red Green Refactor. worth looking at :-) – Robert French Apr 08 '14 at 18:36
  • 1
    Ah, yes! I have now not inherited the class I need to test! Because, declaring the class-that-tested-things `friend` to the class-that-need-be-tested did all things right. However, as you guys mentioned, i will now try some frameworks. I just wanted to conceptually and example-tically understand unit tests! – cipher Apr 09 '14 at 00:39