Alessandro Cerro
Alessandro Cerro

Reputation: 179

QTest, and void Functions

I'm approaching to Unit testing for the first time in QtCreator, and I wonder how I can Test functions that does not have a return type. Like:

rgb_process.h:

class RGB_process : Process
{
public:
    RGB_process(cv::Mat& src, cv::Mat& dst, int exp = 0, double c = 1, int r = 0, int g = 0, int b = 0);

    virtual void doProcess() override;

private:
    int exposure_Val;
    double contrast_Val;
    int red_Val;
    int green_Val;
    int blue_Val;
};

rgb_process.cpp:

#include "rgb_process.h"

RGB_process::RGB_process(cv::Mat& src, cv::Mat& dst, int exp, double c, int r, int g, int b)
    : Process (src, dst), exposure_Val(exp), contrast_Val(c), red_Val(r), green_Val(g), blue_Val(b){
}

void RGB_process::doProcess(){

    for(int i = 0; i < src.rows; i++)
        for(int j = 0; j < src.cols; j++)
            for(int k = 0; k < 3; k++){
                if(k == 0)  //_R
                    dst.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>((src.at<cv::Vec3b>(i,j)[k] + exposure_Val + red_Val )*(259 * (contrast_Val + 255) / (255 * (259 - contrast_Val))));
                if(k == 1)  //_G
                    dst.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>((src.at<cv::Vec3b>(i,j)[k] + exposure_Val + green_Val )*(259 * (contrast_Val + 255) / (255 * (259 - contrast_Val))));
                if(k == 2)  //_B
                    dst.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>((src.at<cv::Vec3b>(i,j)[k] + exposure_Val + blue_Val )*(259 * (contrast_Val + 255) / (255 * (259 - contrast_Val))));
            }
}

I can't find any kind of examples on the net, similar to my needs.

Upvotes: 2

Views: 510

Answers (1)

Silvano Cerza
Silvano Cerza

Reputation: 970

When unit testing a function you shouldn't just verify that its return value is what you expect, you must also verify that the objects you work with inside your functions are manipulated in a predictable way, if possible. In case you're testing a const method it might be enough to just verify the return value.

Here for example you're working on the dst matrix, so you can verify that it's manipulated as you expect since you're applying a pretty clear process on it.

cv::Mat mySrc;
cv::Mat myDst;

// Here insert some data in mySrc matrix

RGB_process(mySrc, myDst);

// Here verify myDst matrix is still untouched

RGB_process.doProcess();

// Here verify myDst matrix contains processed data from mySrc matrix

You can do this because your RGB_process constructor gets a cv::Mat& and doesn't copy the whole matrix.

Upvotes: 2

Related Questions