#include #include #include // Unbelievably ugly hack to let me test private methods. #define private public #define protected public #include "IDigit.hxx" #include "IDigitProxy.hxx" #include "IDigitManager.hxx" #undef private #undef protected #include "ICOMETEvent.hxx" #include "IOADatabase.hxx" #include "IDataHit.hxx" #include "IGeometryId.hxx" #include "COMETGeomId.hxx" #include "ICTHGeomId.hxx" namespace tut { struct baseTDigit { baseTDigit() { // Run before each test. } ~baseTDigit() { // Run after each test. } }; // Declare the test typedef test_group::object testTDigit; test_group groupTDigit("IDigit"); // Test the default constructor and destructor. template<> template<> void testTDigit::test<1> () { COMET::IOADatabase::Get().Digits(); } // Test the IDigitProxy type setters and getters work template <> template <> void testTDigit::test<2> () { COMET::IDigitProxy proxy; // Check that the uninitialized digit is invalid ensure("Uninitialized digit is invalid", !proxy.IsValid()); // Check that we can set the full range of proxy types. for (int val = 0; val<32; ++val) { proxy.SetProxyType(val); int i = proxy.GetProxyType(); int j = proxy.GetProxySalt(); int k = proxy.GetProxyOffset(); ensure_equals("ProxyType set", i, val); ensure_equals("ProxySalt not set", j, 0); ensure_equals("ProxyOffset not set", k, 0); } } // Test the IDigitProxy salt setters and getters work template <> template <> void testTDigit::test<3> () { COMET::IDigitProxy proxy; // Check that we can set the full range of proxy types. for (int val = 0; val<3000; ++val) { proxy.SetProxySalt(val); int i = proxy.GetProxyType(); int j = proxy.GetProxySalt(); int k = proxy.GetProxyOffset(); ensure_equals("ProxyType not set", i, 0); ensure_equals("ProxySalt set", j, val%1024); ensure_equals("ProxyOffset not set", k, 0); } } // Test the IDigitProxy offset setters and getters work template <> template <> void testTDigit::test<4> () { COMET::IDigitProxy proxy; // Check that we can set the full range of proxy types. for (int val = 0; val<131071; val += 321) { proxy.SetProxyOffset(val); int i = proxy.GetProxyType(); int j = proxy.GetProxySalt(); int k = proxy.GetProxyOffset(); ensure_equals("ProxyType not set", i, 0); ensure_equals("ProxySalt not set", j, 0); ensure_equals("ProxyOffset set", k, val); } } class IFactoryForTest: public COMET::IDigitFactory { public: IFactoryForTest() : COMET::IDigitFactory("test") {}; ~IFactoryForTest() {} COMET::IDigitContainer* MakeDigits() { COMET::IDigitContainer* digits = new COMET::IDigitContainer(); for (int i = 0; i<100; ++i) { COMET::IDigit* digit = new COMET::IDigit(COMET::IChannelId(0xF0000000 + i)); digits->push_back(digit); } return digits; } }; // Test that the "test" IDigitFactory can be added to IDigitManager. template <> template <> void testTDigit::test<5> () { ensure("Test IDigitFactory is not available", !COMET::IOADatabase::Get().Digits().FactoryAvailable("test")); COMET::IOADatabase::Get().Digits().RegisterFactory(new IFactoryForTest()); ensure("Test IDigitFactory is available", COMET::IOADatabase::Get().Digits().FactoryAvailable("test")); } // Test that the "test" IDigitFactory digits are created in the right place. template <> template <> void testTDigit::test<6> () { ensure("Test IDigitFactory is available", COMET::IOADatabase::Get().Digits().FactoryAvailable("test")); COMET::ICOMETEvent event; COMET::IHandle digits = COMET::IOADatabase::Get().Digits().CacheDigits("test"); ensure("Digit container was created",digits); ensure_greaterthan("Digit container has digits", digits->size(), (unsigned) 1); COMET::IHandle lookup = event.Get("~/digits/test"); ensure("Digit container in correct place inside event", lookup); ensure_equals("Found digit container same as return", COMET::GetPointer(lookup), COMET::GetPointer(digits)); COMET::IHandle parent = digits->Get(".."); ensure("Digit container is in temporary store", parent->IsTemporary(digits)); ensure_greaterthan("Signature is valid", digits->GetSignature(), (unsigned)0); } // Test the IDigitProxy. template <> template <> void testTDigit::test<7> () { COMET::ICOMETEvent event; COMET::IHandle digits = event.GetDigits("test"); ensure("Digit container was created",digits); std::vector proxies; for (unsigned int i = 0; isize(); ++i) { proxies.push_back(COMET::IDigitProxy(*digits, i)); } std::vector::iterator proxy = proxies.begin(); COMET::IDigitContainer::iterator digit = digits->begin(); unsigned int offset = 0; while (proxy != proxies.end()) { ensure("Digits end not reached", digit != digits->end()); ensure_equals("Proxy type is correct", proxy->GetProxyType(), COMET::IDigitProxy::kTest); ensure_equals("Proxy offset is correct", proxy->GetProxyOffset(), offset); ensure("Proxy salt matches", proxy->CheckSalt(digits->GetSignature(),*digit)); ensure("Proxy salt does not match wrong signature", !proxy->CheckSalt(digits->GetSignature()+1,*digit)); ensure_equals("Proxy points to right address.", *(*proxy), *digit); ++offset, ++proxy; ++digit; } } // Test the digit access routines for IDigitProxy. template <> template <> void testTDigit::test<8> () { COMET::ICOMETEvent event; COMET::IHandle digits = event.GetDigits("test"); ensure("Digit container was created",digits); unsigned int chan = 0; for (COMET::IDigitContainer::iterator it = digits->begin(); it != digits->end(); ++it) { COMET::IDigit* digit = *it; ensure_equals("Digit valid", digit->GetChannelId().AsUInt()&0xFFFF, chan++); } } // Test the digit proxies in THits. template <> template <> void testTDigit::test<9> () { COMET::ICOMETEvent event; COMET::IHandle digits = event.GetDigits("test"); COMET::IHandle hits(new COMET::IHitSelection("test")); // Run a fake calibration to make a hits selection int barCount = 0; for (COMET::IDigitContainer::iterator it = digits->begin(); it != digits->end(); ++it) { // Create the hit and save a proxy for this digit. std::auto_ptr hit(new COMET::IWritableDataHit); hit->SetDigit(COMET::IDigitProxy(*digits,it)); if (barCount > 63) barCount = 0; hit->SetGeomID(COMET::GeomId::CTH::Counter(0,barCount++,0,0)); // Save the new hit in the hit selection. hits->push_back(COMET::IHandle(new COMET::IDataHit(*hit))); } COMET::IHandle hitDir = event.Get("hits"); hitDir->AddDatum(hits); COMET::IHandle select = event.GetHitSelection("test"); for (COMET::IHitSelection::iterator it = select->begin(); it != select->end(); ++it) { COMET::IHandle dataHit(*it); COMET::IDigit* d1 = *dataHit->GetDigit(); COMET::IDigit* d2 = dataHit->GetDigit().As(); COMET::IDigitProxy dp1 = dataHit->GetDigit(); COMET::IDigit* d3 = *dp1; const COMET::IDigitProxy& dp2 = dataHit->GetDigit(); ensure("valid digit",d1); ensure_equals("Digits are the same",d1,d2); ensure_equals("Digit proxy returns same value",d1,d3); ensure("Digit proxies are the same", dp1 == dp2); COMET::IChannelId c1 = d1->GetChannelId(); COMET::IChannelId c3 = dataHit->GetChannelID(); ensure_equals("Channels are the same",c1.AsUInt(),c3.AsUInt()); } } // Test the IDigitProxy objects created with the two different // constructors are the same. template <> template <> void testTDigit::test<10> () { COMET::ICOMETEvent event; COMET::IHandle digits = event.GetDigits("test"); ensure("Digit container was created",digits); std::vector offsetProxies; std::vector iteratorProxies; int digitOffset = 0; for (COMET::IDigitContainer::iterator d = digits->begin(); d != digits->end(); ++d) { iteratorProxies.push_back(COMET::IDigitProxy(*digits, d)); offsetProxies.push_back(COMET::IDigitProxy(*digits, digitOffset++)); } ensure_equals("Same number of offset and iterator proxies", offsetProxies.size(), iteratorProxies.size()); for (std::vector::size_type i=0; i template <> void testTDigit::test<11> () { COMET::IDigitProxy proxy; proxy.SetProxyType(proxy.ConvertName("test")); proxy.SetProxyOffset(0x1FFFE); ensure("Proxy with offset 0x1FFFE is valid", proxy.IsValid()); proxy.SetProxyOffset(0x1FFFF); ensure("Proxy with offset 0x1FFFF is invalid", !proxy.IsValid()); } };