samedi 27 mars 2021

How to write unit tests for a completed cpp program [closed]

I had a task to make a Matrix class in cpp and have the following functions:

1, Read the matrix from a file and store it, (Read)

2, Print the matrix in a chess-style (imagine a chessboard and every brown tile is a matrix element and every white tile is a 0 (Print)

3, Have a function to get the value of the matrix on a certain position, (att)

4, Add two matrices together (summ)

5, Multiply to matrices together (multiply with a sidefunction:smallmultiply)

So I've got this program up and running but I have to make unit tests for it but I have no idea how should I do and I couldn't understand by tutorials.

Basically what I would like to autotest for example:

1, Is the file can be opened,

2, summ,multiply and att functions and their errors when the input is wrong

Could someone help me how to write autotests like this in cpp?

main.ccp:

#include <iostream>
#include "matrix.h"

using namespace std;

int main(int argc, char** argv)
{
    std::string filename2 = "test.txt";
    std::string filename3 = "test2.txt";
    std::string filename4 = "test3.txt";

    Matrix m;
    m.Read(filename2);
    m.print();
    m.att(1,2);
    Matrix l;
    l.Read(filename3);
    l.print();
    m.summ(l);
    Matrix k;
    k.Read(filename4);
    k.print();
    m.multiply(k);
    return 0;
}

matrix.h :

#ifndef MATRIX_H_INCLUDED
#define MATRIX_H_INCLUDED
#include<vector>
#include <iostream>
#include <fstream>

class Matrix {
public:
    void reSize(int k) { _v.resize(k,5); }
    void Size(int x,int y){row=x;column=y;}

    void print(){
        std::cout<<"The given matrix:"<<std::endl;
        int rownum=0;
        for(int i=0; i<row*column;i+=column)
                {
                    for(int j=0;j<column;j++)
                            {
                            if(rownum%2){std::cout<<"0 ";}
                            std::cout<<_v[i+j]<<" ";
                            if(rownum%2){}else{std::cout<<"0 ";}
                            }
                    rownum=rownum+1;
                    std::cout<<std::endl;
                }
        std::cout<<std::endl;
        }

    void Read(const std::string &filename) {
        std::ifstream f(filename.c_str());
        if(f.fail()){
        std::cout << "Wrong filename!\n";
        exit(1);
    }
        int a,b;
        f>>a;
        f>>b;
        Size(a,b);
        reSize(a*b);

       for( int i=0;i<row*column;i++) {
            f>>_v[i];
            }
    }

    void att(int a, int b)
        {
        std::cout<<"The value on (" <<a<<","<<b<<") is : "<<_v[a*column+b-1]<<std::endl;
        std::cout<<std::endl;
        }

     void summ(Matrix k)
        {
            if(column!=k.column && row!=k.row)
            {
                std::cout<<"The number of rows and colunms in the two matrices are not equal"<<std::endl;
                std::cout<<"So you can not sum them"<<std::endl;
                exit(1);
            }

            std::cout<<"The sum of the two matrices:"<<std::endl;
            int rownum=0;
            for(int i=0; i<row*column;i+=column)
                {
                    for(int j=0;j<column;j++)
                            {
                            if(rownum%2){std::cout<<"0 ";}
                            std::cout<<_v[i+j]+k._v[i+j]<<" ";
                            if(rownum%2){}else{std::cout<<"0 ";}
                            }
                    rownum=rownum+1;
                    std::cout<<std::endl;
                }
        std::cout<<std::endl;
        }

        void multiply(Matrix k)
        {
            if(column!=k.row)
            {
                std::cout<<"The number of the first matrix's columns does not equal with the number of the second matrix's rows "<<std::endl;
                std::cout<<"This two matrices can not be multiplied"<<std::endl;
                exit(1);
            }
            std::cout<<"The multiplication of the two matrices:"<<std::endl;
            int rownum=0;
            for(int i=0; i<row*row;i+=row)
                {
                    for(int j=0;j<row;j++)
                            {
                            if(rownum%2){std::cout<<"0 ";}
                            smallmultiply(_v,k._v,rownum,j,row,column);
                            std::cout<<" ";
                            if(rownum%2){}else{std::cout<<"0 ";}
                            }
                    rownum=rownum+1;
                    std::cout<<std::endl;
                }
        std::cout<<std::endl;
        }

        void smallmultiply(std::vector<int> a,std::vector<int> b, int x, int y, int row2,int column2)
        {
            int localsum=0;
            for (int k=0;k<column2;k++)
            {
                localsum = localsum + a[x*column2+k]*b[k*row2+y];
            }
            std::cout<<localsum;
        }

    private:
    std::vector<int> _v;
    int row;
    int column;
};
#endif // MATRIX_H_INCLUDED

Aucun commentaire:

Enregistrer un commentaire