I have a parent class test with child classes that are sub-tests. When I call test->run() I want to run all my sub tests. The code below does not work b.c. the child class is not recognized in the parent class.
using namespace std;
class test
{
protected:
short verbosity_;
public:
void setVerbosity(short v)
{
if((v==0 || v==1 || v==2))
{
verbosity_ = v;
}
else
{
cout << " Verbosity Level Invalid " << endl;
}
}
void run()
{
natives natives_1; // this does not work
}
};
class natives : public test
{
public:
natives()
{
this->run();
}
void run()
{
testInts<short>();
testInts<int>();
testInts<long>();
testInts<unsigned short>();
testInts<unsigned int>();
testInts<unsigned long>();
}
protected:
template<class T> void testFloats()
{
}
template<class T> void testInts()
{
short passState, bitDepth;
T failMax, pow2 = 1, minValue = 0, maxValue = 0, bitCount = 0, failValue = 0;
const char* a = typeid(T).name();
bool signedType = ((*a == 't') || (*a == 'j') || (*a == 'm'));
while(pow2 > 0)
{
pow2 *= 2;
bitCount++;
}
maxValue = pow2-1;
failValue = pow2;
int native1 = bitCount;
int native2 = sizeof(T)*8;
int native3 = numeric_limits<T>::digits;
if( !signedType )
{
native1++;
native3++;
}
if(verbosity_>=1) cout << endl << "**********\n" << reportType(a) << "\n**********" << endl << endl;
if ((native1 == native2) && (native1 == native3))
{
if(verbosity_>=1)cout << "Correlation:\t\tPass: " << native1 << endl ;
if(verbosity_>=2)
{
cout << "--Algorithm:\t\t" << native1 << endl;
cout << "--Sizeof:\t\t" << native2 << endl;
cout << "--Reported:\t\t" << native3 << endl;
cout << "----Max Value:\t\t" << maxValue << endl;
cout << "----Max+1\t\t" << failValue << endl;
}
else
{
}
}
else
{
cout << "Correlation:\t\tFail" << endl ;
}
}
string reportType(const char* c1)
{
string s1;
switch(*c1)
{
case 't':
s1 = "Unsigned short";
break;
case 'j':
s1 = "Unsigned int";
break;
case 'm':
s1 = "Unsigned long";
break;
case 's':
s1 = "Short";
break;
case 'i':
s1 = "Int";
break;
case 'l':
s1 = "Long";
break;
default:
s1 = "Switch failed";
}
return s1;
}
};
Probably a better way to do it would be to make a pure virtual function called
runin thetestclass, and then the tests of the subclass will implement it and those will be run when you callrunon an instance of the subclass.For instance:
Then you can do
And they will still be run when you do it through a
Test*(orTest&):You might also be able to use CRTP here but I think it’s unnecessary.