update testIntrospect with epicsUnitTest

This commit is contained in:
Michael Davidsaver
2013-05-24 18:25:09 -04:00
parent 9e865bc37d
commit 9039a10c9a
2 changed files with 123 additions and 98 deletions

View File

@@ -5,6 +5,7 @@ include $(TOP)/configure/CONFIG
PROD_HOST += testIntrospect
testIntrospect_SRCS += testIntrospect.cpp
testIntrospect_LIBS += pvData Com
TESTS += testIntrospect
PROD_HOST += testPVAppend
testPVAppend_SRCS += testPVAppend.cpp

View File

@@ -12,8 +12,8 @@
#include <string>
#include <cstdio>
#include <epicsAssert.h>
#include <epicsExit.h>
#include <epicsUnitTest.h>
#include <testMain.h>
#include <pv/requester.h>
#include <pv/executor.h>
@@ -23,130 +23,154 @@
using namespace epics::pvData;
static bool debug = false;
static FieldCreatePtr fieldCreate;
static PVDataCreatePtr pvDataCreate;
static StandardFieldPtr standardField;
static String builder("");
static void testScalarCommon(FILE * fd,ScalarType stype,
static void testScalarCommon(ScalarType stype,
bool isInteger,bool isNumeric,bool isPrimitive)
{
String builder;
ScalarConstPtr pscalar = fieldCreate->createScalar(stype);
Type type = pscalar->getType();
assert(type==scalar);
testOk1(type==scalar);
builder.clear();
TypeFunc::toString(&builder,type);
assert(builder.compare("scalar")==0);
testOk1(builder.compare("scalar")==0);
ScalarType scalarType = pscalar->getScalarType();
assert(scalarType==stype);
assert(ScalarTypeFunc::isInteger(scalarType)==isInteger);
assert(ScalarTypeFunc::isNumeric(scalarType)==isNumeric);
assert(ScalarTypeFunc::isPrimitive(scalarType)==isPrimitive);
builder.clear();
pscalar->toString(&builder);
if(debug) fprintf(fd,"%s\n",builder.c_str());
testOk1(scalarType==stype);
testOk1(ScalarTypeFunc::isInteger(scalarType)==isInteger);
testOk1(ScalarTypeFunc::isNumeric(scalarType)==isNumeric);
testOk1(ScalarTypeFunc::isPrimitive(scalarType)==isPrimitive);
}
static void testScalar(FILE * fd) {
if(debug) fprintf(fd,"\ntestScalar\n");
testScalarCommon(fd,pvBoolean,false,false,true);
testScalarCommon(fd,pvByte,true,true,true);
testScalarCommon(fd,pvShort,true,true,true);
testScalarCommon(fd,pvInt,true,true,true);
testScalarCommon(fd,pvLong,true,true,true);
testScalarCommon(fd,pvFloat,false,true,true);
testScalarCommon(fd,pvDouble,false,true,true);
testScalarCommon(fd,pvString,false,false,false);
fprintf(fd,"testScalar PASSED\n");
static void testScalar() {
testDiag("testScalar");
testScalarCommon(pvBoolean,false,false,true);
testScalarCommon(pvByte,true,true,true);
testScalarCommon(pvShort,true,true,true);
testScalarCommon(pvInt,true,true,true);
testScalarCommon(pvLong,true,true,true);
testScalarCommon(pvFloat,false,true,true);
testScalarCommon(pvDouble,false,true,true);
testScalarCommon(pvString,false,false,false);
}
static void testScalarArrayCommon(FILE * fd,ScalarType stype,
static void testScalarArrayCommon(ScalarType stype,
bool isInteger,bool isNumeric,bool isPrimitive)
{
String builder;
ScalarArrayConstPtr pscalar = fieldCreate->createScalarArray(stype);
Type type = pscalar->getType();
assert(type==scalarArray);
testOk1(type==scalarArray);
builder.clear();
TypeFunc::toString(&builder,type);
assert(builder.compare("scalarArray")==0);
testOk1(builder.compare("scalarArray")==0);
ScalarType scalarType = pscalar->getElementType();
assert(scalarType==stype);
assert(ScalarTypeFunc::isInteger(scalarType)==isInteger);
assert(ScalarTypeFunc::isNumeric(scalarType)==isNumeric);
assert(ScalarTypeFunc::isPrimitive(scalarType)==isPrimitive);
builder.clear();
pscalar->toString(&builder);
if(debug) fprintf(fd,"%s\n",builder.c_str());
testOk1(scalarType==stype);
testOk1(ScalarTypeFunc::isInteger(scalarType)==isInteger);
testOk1(ScalarTypeFunc::isNumeric(scalarType)==isNumeric);
testOk1(ScalarTypeFunc::isPrimitive(scalarType)==isPrimitive);
}
static void testScalarArray(FILE * fd) {
if(debug) fprintf(fd,"\ntestScalarArray\n");
testScalarArrayCommon(fd,pvBoolean,false,false,true);
testScalarArrayCommon(fd,pvByte,true,true,true);
testScalarArrayCommon(fd,pvShort,true,true,true);
testScalarArrayCommon(fd,pvInt,true,true,true);
testScalarArrayCommon(fd,pvLong,true,true,true);
testScalarArrayCommon(fd,pvFloat,false,true,true);
testScalarArrayCommon(fd,pvDouble,false,true,true);
testScalarArrayCommon(fd,pvString,false,false,false);
fprintf(fd,"testScalarArray PASSED\n");
static void testScalarArray() {
testDiag("testScalarArray");
testScalarArrayCommon(pvBoolean,false,false,true);
testScalarArrayCommon(pvByte,true,true,true);
testScalarArrayCommon(pvShort,true,true,true);
testScalarArrayCommon(pvInt,true,true,true);
testScalarArrayCommon(pvLong,true,true,true);
testScalarArrayCommon(pvFloat,false,true,true);
testScalarArrayCommon(pvDouble,false,true,true);
testScalarArrayCommon(pvString,false,false,false);
}
static void testSimpleStructure(FILE * fd) {
if(debug) fprintf(fd,"\ntestSimpleStructure\n");
String properties("alarm,timeStamp,display,control,valueAlarm");
StructureConstPtr ptop = standardField->scalar(pvDouble,properties);
builder.clear();
ptop->toString(&builder);
if(debug) fprintf(fd,"%s\n",builder.c_str());
fprintf(fd,"testSimpleStructure PASSED\n");
}
static StructureConstPtr createPowerSupply() {
size_t nfields = 3;
String properties("alarm");
StringArray names;
names.reserve(nfields);
FieldConstPtrArray powerSupply;
powerSupply.reserve(nfields);
names.push_back("voltage");
powerSupply.push_back(standardField->scalar(pvDouble,properties));
names.push_back("power");
powerSupply.push_back(standardField->scalar(pvDouble,properties));
names.push_back("current");
powerSupply.push_back(standardField->scalar(pvDouble,properties));
return fieldCreate->createStructure(names,powerSupply);
}
static void testStructureArray(FILE * fd) {
if(debug) fprintf(fd,"\ntestStructureArray\n");
String properties("alarm,timeStamp");
StructureConstPtr powerSupply = createPowerSupply();
StructureConstPtr top = standardField->structureArray(
powerSupply,properties);
builder.clear();
top->toString(&builder);
if(debug) fprintf(fd,"%s\n",builder.c_str());
fprintf(fd,"testStructureArray PASSED\n");
}
int main(int argc,char *argv[])
static void testStructure()
{
char *fileName = 0;
if(argc>1) fileName = argv[1];
FILE * fd = stdout;
if(fileName!=0 && fileName[0]!=0) {
fd = fopen(fileName,"w+");
}
testDiag("testStructure");
StringArray names1(2);
names1[0] = "innerA";
names1[1] = "innerB";
FieldConstPtrArray fields1(2);
fields1[0] = fieldCreate->createScalar(pvDouble);
fields1[1] = fieldCreate->createScalarArray(pvString);
StructureConstPtr struct1 = fieldCreate->createStructure(names1, fields1);
testOk1(struct1->getNumberFields()==2);
testOk1(struct1->getField("innerA")==fields1[0]);
testOk1(struct1->getField("innerB")==fields1[1]);
testOk1(struct1->getFieldIndex("innerA")==0);
testOk1(struct1->getFieldIndex("innerB")==1);
testOk1(struct1->getField(0)==fields1[0]);
testOk1(struct1->getField(1)==fields1[1]);
testOk1(struct1->getFieldName(0)==names1[0]);
testOk1(struct1->getFieldName(1)==names1[1]);
testOk1(struct1->getID() == Structure::DEFAULT_ID);
testOk1(fields1 == struct1->getFields()); // vector equality
StructureArrayConstPtr struct1arr = fieldCreate->createStructureArray(struct1);
testOk1(struct1arr->getStructure()==struct1);
testOk1(struct1arr->getID()=="structure[]");
}
#define testExcept(EXCEPT, CMD) try{ CMD; testFail( "No exception from: " #CMD); } \
catch(EXCEPT& e) {testPass("Got expected exception from: " #CMD);} \
catch(std::exception& e) {testFail("Got wrong exception %s(%s) from: " #CMD, typeid(e).name(),e.what());} \
catch(...) {testFail("Got unknown execption from: " #CMD);}
static void testError()
{
testDiag("testError");
ScalarType invalidtype = (ScalarType)9999;
testExcept(std::invalid_argument, ScalarTypeFunc::getScalarType("invalidtype"));
testExcept(std::invalid_argument, ScalarTypeFunc::elementSize(invalidtype));
testExcept(std::invalid_argument, ScalarTypeFunc::name(invalidtype));
testOk1(!ScalarTypeFunc::isInteger(invalidtype));
testOk1(!ScalarTypeFunc::isUInteger(invalidtype));
testOk1(!ScalarTypeFunc::isNumeric(invalidtype));
testOk1(!ScalarTypeFunc::isPrimitive(invalidtype));
testExcept(std::invalid_argument, fieldCreate->createScalar(invalidtype));
testExcept(std::invalid_argument, fieldCreate->createScalarArray(invalidtype));
StringArray names;
FieldConstPtrArray fields(1);
// fails because names.size()!=fields.size()
testExcept(std::invalid_argument, fieldCreate->createStructure(names,fields));
names.resize(1);;
// fails because names[0].size()==0
testExcept(std::invalid_argument, fieldCreate->createStructure(names,fields));
names[0] = "hello";
// fails because fields[0].get()==NULL
testExcept(std::invalid_argument, fieldCreate->createStructure(names,fields));
fields[0] = std::tr1::static_pointer_cast<const Field>(fieldCreate->createScalar(pvDouble));
testOk1(fieldCreate->createStructure(names,fields).get()!=NULL);
}
MAIN(testIntrospect)
{
testPlan(122);
fieldCreate = getFieldCreate();
pvDataCreate = getPVDataCreate();
standardField = getStandardField();
testScalar(fd);
testScalarArray(fd);
testSimpleStructure(fd);
testStructureArray(fd);
return(0);
testScalar();
testScalarArray();
testStructure();
testError();
return testDone();
}