简体   繁体   中英

Unittest++ does not recognize tests

I'm currently performing unit tests (which I've just learned this week) in order to refactor an existing program before extending its functionality.

However, UnitTest++ suddenly gave an error that said "Project contains 0 tests" after I'd done about 5-6 tests on a template class ( full code here ) and now it keeps giving me this error even though I've reverted the test code to what it was before the error showed up. Right now it fails whenever the object is instantiated.

The class is for managing arrays of data in an optimization algorithm, so the functions are just to initialize, read, write, and update arrays and in itself should not have been a problem (or in need of much testing).

I can't find a thread that discusses a similar issue. If someone can point me towards the cause, that would be great.

Specifications:

I use CodeLite 8.2.0 as the IDE on Windows 8.1 with MinGW (TDM-GCC-32) as the compiler and UnitTest++1.3 as the unit test framework.

The code before UnitTest++ breaks (in case it might be useful) is as follows:

#include "C:\UnitTest++-1.3\src\UnitTest++.h"
#include "candidate.h"
#include <stdexcept>
using namespace std;
int main(int argc, char **argv)
{
   return UnitTest::RunAllTests();
}
//Test constructor and initialization of memories
TEST(init_num){
int numvar=4;
int fit_size=2;
int err=0;
Candidate<double> can;
try{
    can.init_can(numvar, fit_size);
}
catch(out_of_range){
    err=1;
    }
catch(invalid_argument){
    err=2;
    }
CHECK(err==0);
//CHECK(can.num==numvar);
}
TEST(init_fitrange){
int numvar=2;
int fit_size=-1;
bool err=0;
Candidate<double> can;
try{
    can.init_can(numvar, fit_size);
    }
catch(invalid_argument){
    err=1;
    }
CHECK(err==1);
}
//Skip memory initialization check. If numvar and num_fit are valid the memory will be initialized properly.
TEST(velocity_init){
int numvar=2;
int fit_size=2;
double vel[numvar];
double total=0;
Candidate<double> can;
can.init_can(numvar, fit_size);
can.init_velocity();
for(int i=0;i<numvar;++i){
    vel[i]=1;
    }
can.update_vel(vel);
for(int i=0;i<numvar;++i){
    total+=can.velocity[i];
    }
CHECK(total==numvar);
//same mechanism as update_global, update_best and read functions
}

The error came up when I appended this test to the list:

TEST(write_contfit){
int numvar=2;
int fit_size=2;
Candidate<double> can;
can.init_can(numvar, fit_size);
double fit[fit_size];
int tt=fit_size;
double total=0;
for(int i=0;i<fit_size;++i){
   fit[i]=1;
}
can.write_contfit(fit,tt);
for(int i=0;i<fit_size;i++){
total+=can.contfit[i];
}
CHECK(total==fit_size);
}

After basically tearing the class apart and building it back up again under the unit test framework, I found where in the code had made the framework gone awry. This has to do with operations on array pointers without allocated memory. The most problematic function is the destructor which frees up any memory allocation using delete[] . A quick dirty fix is to comment out all delete[] lines. I don't think it's a good way of doing it, but at least it allows me to test the rest of the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM