Unit Tests in C++

Introduction
Configuring GTest
Writing the Program
Testing the Program
Conclusion

Introduction

We will be exploring C++ exception handling using GTest.


Configuring GTest

The two prerequisites for Gtest are:
1. CMake
2. Python 2.7

Now in the terminal:

wget http://googletest.googlecode.com/files/gtest-1.7.0.zip
cd gtest-1.7.0
mkdir mybuild
cd mybuild
cmake -G"Unix Makefiles" ..
make

Writing the Program

#include "string"
#include "iostream"
#include "gtest/gtest.h"

enum {SUCC,ERR,IO};

using namespace std;

struct SUCCESS{
    int a;
};
struct ERROR:SUCCESS{
    int a;
};
struct IOERROR:ERROR {
    int a;
};

/**
    * SUCCESS
    * |
    * ERROR
    * | 
    * IOERR
*/

void f(int i){
    if(i==ERR)
        throw ERROR();
    else if(i==IO)
        throw IOERROR();
    else
        throw SUCCESS();
};


int inheritance_hierarchy(int i){
    try{
        f(i);
    }catch(SUCCESS& x){
        return SUCC;
    }catch(ERROR& x){
        return ERR;
    }catch(IOERROR& x){
        return IO;
    }   
    return -1; // Finished G succesfully // should never come here //
};

int bottom_up(int i){
    try{
        f(i);
    }catch(IOERROR& x){
        return IO;
    }catch(ERROR& x){
        return ERR;
    }catch(SUCCESS& x){
        return SUCC;    
    }   
    return -1; // Finished G succesfully // should never come here //
}


TEST(INHERITANCE, hierarchy_SUCC_SUCC) {
    EXPECT_EQ(inheritance_hierarchy(SUCC),SUCC);
}

TEST(INHERITANCE, hierarchy_ERR_SUCC) {
    ASSERT_EQ(inheritance_hierarchy(ERR),SUCC);
}

TEST(INHERITANCE, hierarchy_IO_SUCC) {
    ASSERT_EQ(inheritance_hierarchy(IO),SUCC);
}

TEST(BOTTOM_UP, SUCC_SUCC) {
    EXPECT_EQ(bottom_up(SUCC),SUCC);
}

TEST(BOTTOM_UP, ERR_ERR) {
    EXPECT_EQ(bottom_up(ERR),ERR);
}

TEST(BOTTOM_UP, IO_IO) {
    EXPECT_EQ(bottom_up(IO),IO);
}

int main(int argc, char **argv){
    ::testing::InitGoogleTest(&argc, argv);
    RUN_ALL_TESTS();

    cout << "finished main" << endl;
    return 0;
};

Testing the Program

To build the above program.

g++ exceptions.c++ -fprofile-arcs -ftest-coverage -pedantic -std=c++11 -Wall -o TestExceptions -lgtest -lgtest_main -lpthread

[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from INHERITANCE
[ RUN      ] INHERITANCE.hierarchy_SUCC_SUCC
[       OK ] INHERITANCE.hierarchy_SUCC_SUCC (0 ms)
[ RUN      ] INHERITANCE.hierarchy_ERR_SUCC
[       OK ] INHERITANCE.hierarchy_ERR_SUCC (0 ms)
[ RUN      ] INHERITANCE.hierarchy_IO_SUCC
[       OK ] INHERITANCE.hierarchy_IO_SUCC (0 ms)
[----------] 3 tests from INHERITANCE (0 ms total)

[----------] 3 tests from BOTTOM_UP
[ RUN      ] BOTTOM_UP.SUCC_SUCC
[       OK ] BOTTOM_UP.SUCC_SUCC (0 ms)
[ RUN      ] BOTTOM_UP.ERR_ERR
[       OK ] BOTTOM_UP.ERR_ERR (0 ms)
[ RUN      ] BOTTOM_UP.IO_IO
[       OK ] BOTTOM_UP.IO_IO (0 ms)
[----------] 3 tests from BOTTOM_UP (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (1 ms total)
[  PASSED  ] 6 tests.

Conclusion

We can see that Exception handling depends on the order of the catch blocks when they inherit from a root object. Using GTest put together a set of tests to assess this idea.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s