From fc037cf6dca99cdf9cef6c8bd24e51ba34af97cc Mon Sep 17 00:00:00 2001 From: Dave Hickin Date: Thu, 20 Aug 2015 16:16:58 +0100 Subject: [PATCH] Add unit tests for NTAttribute --- test/nt/Makefile | 4 + test/nt/ntattributeTest.cpp | 176 ++++++++++++++++++++++++++++++++++++ 2 files changed, 180 insertions(+) create mode 100644 test/nt/ntattributeTest.cpp diff --git a/test/nt/Makefile b/test/nt/Makefile index aab1e22..2369833 100644 --- a/test/nt/Makefile +++ b/test/nt/Makefile @@ -52,6 +52,10 @@ TESTPROD_HOST += ntaggregateTest ntaggregateTest_SRCS = ntaggregateTest.cpp TESTS += ntaggregateTest +TESTPROD_HOST += ntattributeTest +ntattributeTest_SRCS = ntattributeTest.cpp +TESTS += ntattributeTest + TESTPROD_HOST += ntutilsTest ntutilsTest_SRCS = ntutilsTest.cpp TESTS += ntutilsTest diff --git a/test/nt/ntattributeTest.cpp b/test/nt/ntattributeTest.cpp new file mode 100644 index 0000000..5e46bdb --- /dev/null +++ b/test/nt/ntattributeTest.cpp @@ -0,0 +1,176 @@ +/** + * Copyright - See the COPYRIGHT that is included with this distribution. + * EPICS pvDataCPP is distributed subject to a Software License Agreement found + * in file LICENSE that is included with this distribution. + * + */ +#include +#include + +#include + + +using namespace epics::nt; +using namespace epics::pvData; +using std::tr1::dynamic_pointer_cast; + +static FieldCreatePtr fieldCreate = getFieldCreate(); +static StandardFieldPtr standardField = getStandardField(); +static NTFieldPtr ntField = NTField::get(); + +void test_builder() +{ + testDiag("test_builder"); + + NTAttributeBuilderPtr builder = NTAttribute::createBuilder(); + testOk(builder.get() != 0, "Got builder"); + + StructureConstPtr structure = builder-> + addTags()-> + addDescriptor()-> + addAlarm()-> + addTimeStamp()-> + add("extra",fieldCreate->createScalar(pvString)) -> + createStructure(); + testOk1(structure.get() != 0); + if (!structure) + return; + + testOk1(NTAttribute::is_a(structure)); + testOk1(structure->getID() == NTAttribute::URI); + testOk1(structure->getNumberFields() == 7); + testOk1(structure->getField("name").get() != 0); + testOk1(structure->getField("value").get() != 0); + testOk1(structure->getField("tags").get() != 0); + testOk1(structure->getField("descriptor").get() != 0); + testOk1(structure->getField("alarm").get() != 0); + testOk1(structure->getField("timeStamp").get() != 0); + + ScalarConstPtr nameField = structure->getField("name"); + testOk(nameField.get() != 0 && nameField->getScalarType() == pvString, + "name is string"); + + UnionConstPtr valueField = structure->getField("value"); + testOk(valueField.get() != 0, "value is enum"); + + ScalarArrayConstPtr tagsField = structure->getField("tags"); + testOk(tagsField.get() != 0 && tagsField->getElementType() == pvString, + "tags is string[]"); + + std::cout << *structure << std::endl; + +} + +void test_ntattribute() +{ + testDiag("test_ntattribute"); + + NTAttributeBuilderPtr builder = NTAttribute::createBuilder(); + testOk(builder.get() != 0, "Got builder"); + + NTAttributePtr ntAttribute = builder-> + addTags()-> + addDescriptor()-> + addAlarm()-> + addTimeStamp()-> + create(); + testOk1(ntAttribute.get() != 0); + + testOk1(ntAttribute->getPVStructure().get() != 0); + testOk1(ntAttribute->getName().get() != 0); + testOk1(ntAttribute->getValue().get() != 0); + testOk1(ntAttribute->getTags().get() != 0); + testOk1(ntAttribute->getDescriptor().get() != 0); + testOk1(ntAttribute->getAlarm().get() != 0); + testOk1(ntAttribute->getTimeStamp().get() != 0); + + // + // timeStamp ops + // + PVTimeStamp pvTimeStamp; + if (ntAttribute->attachTimeStamp(pvTimeStamp)) + { + testPass("timeStamp attach"); + + // example how to set current time + TimeStamp ts; + ts.getCurrent(); + pvTimeStamp.set(ts); + + // example how to get EPICS time + TimeStamp ts2; + pvTimeStamp.get(ts2); + testOk1(ts2.getEpicsSecondsPastEpoch() != 0); + } + else + testFail("timeStamp attach fail"); + + // + // alarm ops + // + PVAlarm pvAlarm; + if (ntAttribute->attachAlarm(pvAlarm)) + { + testPass("alarm attach"); + + // example how to set an alarm + Alarm alarm; + alarm.setStatus(deviceStatus); + alarm.setSeverity(minorAlarm); + alarm.setMessage("simulation alarm"); + pvAlarm.set(alarm); + } + else + testFail("alarm attach fail"); + + // + // set descriptor + // + ntAttribute->getDescriptor()->put("This is a test NTAttribute"); + + // dump ntAttribute + std::cout << *ntAttribute->getPVStructure() << std::endl; + +} + +void test_wrap() +{ + testDiag("test_wrap"); + + NTAttributePtr nullPtr = NTAttribute::wrap(PVStructurePtr()); + testOk(nullPtr.get() == 0, "nullptr wrap"); + + nullPtr = NTAttribute::wrap( + getPVDataCreate()->createPVStructure( + NTField::get()->createTimeStamp() + ) + ); + testOk(nullPtr.get() == 0, "wrong type wrap"); + + + NTAttributeBuilderPtr builder = NTAttribute::createBuilder(); + testOk(builder.get() != 0, "Got builder"); + + PVStructurePtr pvStructure = builder-> + createPVStructure(); + testOk1(pvStructure.get() != 0); + if (!pvStructure) + return; + + testOk1(NTAttribute::isCompatible(pvStructure)==true); + NTAttributePtr ptr = NTAttribute::wrap(pvStructure); + testOk(ptr.get() != 0, "wrap OK"); + + ptr = NTAttribute::wrapUnsafe(pvStructure); + testOk(ptr.get() != 0, "wrapUnsafe OK"); +} + +MAIN(testNTAttribute) { + testPlan(33); + test_builder(); + test_ntattribute(); + test_wrap(); + return testDone(); +} + +