主要问题是如何实现 startTest() 以便它在所有子类中调用 runTest 。谢谢!
/*******************
COMPILER TEST
*******************/
class archeTest
{
protected:
short verbosity_;
public:
void setVerbosity(short v)
{
if( ((v == 1) || (v == 0) )
{
verbosity_ = v;
}
else
{
cout << " Verbosity Level Invalid " << endl;
}
}
virtual void runTest() = 0;
{
}
void startTest()
{
}
};
class testNatives : public archeTest
{
public:
void runTest()
{
testInts<short>();
testInts<int>();
testInts<long>();
testInts<unsigned short>();
testInts<unsigned int>();
testInts<unsigned long>();
}
void reportResults() const
{
}
protected:
template<class T> void testFloats()
template<class T> void testInts()
{
verbosity_ = 1;
T failMax;
short passState;
short bitDepth;
const char* a = typeid(T).name();
bool signedType = ((*a == 't') || (*a == 'j') || (*a == 'm'));
/* Bit Depth - Algorithm */
T pow2 = 1, minValue = 0, maxValue = 0, bitCount = 0, failValue = 0;
while(pow2 > 0)
{
pow2 *= 2;
maxValue = pow2-1;
bitCount++;
}
failValue = pow2;
int native1 = bitCount;
int native2 = sizeof(T)*8;
int native3 = numeric_limits<T>::digits;
if( !signedType )
{
native1++;
native3++;
}
if(verbosity_)
{
cout << endl << "**********\n" << reportType(a) << "\n**********" << endl << endl;
cout << "Bit Depth - Algorithm:\t" << native1 << endl;
cout << "Bit Depth - Sizeof:\t" << native2 << endl;
cout << "Bit Depth - File:\t" << native3 << endl;
}
if (native1 == native2 && native1 == native3)
{
cout << "Correlation:\t\tPass" << endl ;
}
else
{
cout << "Correlation:\t\tFail" << endl ;
}
cout << "Max Value:\t\t" << maxValue << endl;
cout << "Max+1 Value:\t\t" << failValue << 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;
}
};
int main()
{
testNatives A;
A.runTest();
}