void LazyObjectTest::testDiscardingNotifications() { BOOST_TEST_MESSAGE( "Testing that lazy object discards notifications after the first..."); boost::shared_ptr<SimpleQuote> q(new SimpleQuote(0.0)); boost::shared_ptr<Instrument> s(new Stock(Handle<Quote>(q))); Flag f; f.registerWith(s); s->NPV(); q->setValue(1.0); if (!f.isUp()) BOOST_FAIL("Observer was not notified of change"); f.lower(); q->setValue(2.0); if (f.isUp()) BOOST_FAIL("Observer was notified of second change"); f.lower(); s->NPV(); q->setValue(3.0); if (!f.isUp()) BOOST_FAIL("Observer was not notified of change after recalculation"); }
void InstrumentTest::testObservable() { BOOST_TEST_MESSAGE("Testing observability of instruments..."); boost::shared_ptr<SimpleQuote> me1(new SimpleQuote(0.0)); RelinkableHandle<Quote> h(me1); boost::shared_ptr<Instrument> s(new Stock(h)); Flag f; f.registerWith(s); s->NPV(); me1->setValue(3.14); if (!f.isUp()) BOOST_FAIL("Observer was not notified of instrument change"); s->NPV(); f.lower(); boost::shared_ptr<SimpleQuote> me2(new SimpleQuote(0.0)); h.linkTo(me2); if (!f.isUp()) BOOST_FAIL("Observer was not notified of instrument change"); f.lower(); s->freeze(); s->NPV(); me2->setValue(2.71); if (f.isUp()) BOOST_FAIL("Observer was notified of frozen instrument change"); s->NPV(); s->unfreeze(); if (!f.isUp()) BOOST_FAIL("Observer was not notified of instrument change"); }
void QuoteTest::testObservableHandle() { BOOST_MESSAGE("Testing observability of quote handles..."); boost::shared_ptr<SimpleQuote> me1(new SimpleQuote(0.0)); RelinkableHandle<Quote> h(me1); Flag f; f.registerWith(h); me1->setValue(3.14); if (!f.isUp()) BOOST_FAIL("Observer was not notified of quote change"); f.lower(); boost::shared_ptr<SimpleQuote> me2(new SimpleQuote(0.0)); h.linkTo(me2); if (!f.isUp()) BOOST_FAIL("Observer was not notified of quote change"); }
void TermStructureTest::testZSpreadedObs() { BOOST_TEST_MESSAGE("Testing observability of zero-spreaded term structure..."); CommonVars vars; ext::shared_ptr<SimpleQuote> me(new SimpleQuote(0.01)); Handle<Quote> mh(me); RelinkableHandle<YieldTermStructure> h(vars.dummyTermStructure); ext::shared_ptr<YieldTermStructure> spreaded( new ZeroSpreadedTermStructure(h,mh)); Flag flag; flag.registerWith(spreaded); h.linkTo(vars.termStructure); if (!flag.isUp()) BOOST_ERROR("Observer was not notified of term structure change"); flag.lower(); me->setValue(0.005); if (!flag.isUp()) BOOST_ERROR("Observer was not notified of spread change"); }
void LazyObjectTest::testForwardingNotifications() { BOOST_TEST_MESSAGE( "Testing that lazy object forwards all notifications when told..."); boost::shared_ptr<SimpleQuote> q(new SimpleQuote(0.0)); boost::shared_ptr<Instrument> s(new Stock(Handle<Quote>(q))); s->alwaysForwardNotifications(); Flag f; f.registerWith(s); s->NPV(); q->setValue(1.0); if (!f.isUp()) BOOST_FAIL("Observer was not notified of change"); f.lower(); q->setValue(2.0); if (!f.isUp()) BOOST_FAIL("Observer was not notified of second change"); }
void *ImageProcessThread(void *threadargs) { long tid = (long)((struct Thread_data *)threadargs)->thread_id; printf("ImageProcess thread #%ld!\n", tid); CoordList localLimbs, localPixelFiducials, localScreenFiducials; IndexList localIds; uint8_t localMin, localMax; std::vector<float> localMapping; cv::Point2f localPixelCenter, localScreenCenter, localError; timespec waittime; waittime.tv_sec = frameRate.tv_sec/10; waittime.tv_nsec = frameRate.tv_nsec/10; while(1) { if (stop_message[tid] == 1) { printf("ImageProcess thread #%ld exiting\n", tid); started[tid] = false; pthread_exit( NULL ); } if (cameraReady) { while(1) { if(procReady.check()) { procReady.lower(); break; } else { nanosleep(&waittime, NULL); } } //printf("ImageProcessThread: trying to lock\n"); if (pthread_mutex_trylock(&mutexImage) == 0) { //printf("ImageProcessThread: got lock\n"); if(!frame.empty()) { aspect.LoadFrame(frame); pthread_mutex_unlock(&mutexImage); runResult = aspect.Run(); switch(GeneralizeError(runResult)) { case NO_ERROR: aspect.GetScreenFiducials(localScreenFiducials); aspect.GetScreenCenter(localScreenCenter); aspect.GetMapping(localMapping); case MAPPING_ERROR: aspect.GetFiducialIDs(localIds); case ID_ERROR: aspect.GetPixelFiducials(localPixelFiducials); case FIDUCIAL_ERROR: aspect.GetPixelCenter(localPixelCenter); aspect.GetPixelError(localError); case CENTER_ERROR: aspect.GetPixelCrossings(localLimbs); if (REPORT_FOCUS) aspect.ReportFocus(); case LIMB_ERROR: case RANGE_ERROR: aspect.GetPixelMinMax(localMin, localMax); break; default: std::cout << "Nothing worked\n"; } pthread_mutex_lock(&mutexProcess); switch(GeneralizeError(runResult)) { case NO_ERROR: screenFiducials = localScreenFiducials; screenCenter = localScreenCenter; mapping = localMapping; case MAPPING_ERROR: ids = localIds; case ID_ERROR: pixelFiducials = localPixelFiducials; case FIDUCIAL_ERROR: pixelCenter = localPixelCenter; error = localError; case CENTER_ERROR: limbs = localLimbs; case LIMB_ERROR: case RANGE_ERROR: frameMin = localMin; frameMax = localMax; break; default: break; } pthread_mutex_unlock(&mutexProcess); } else { //std::cout << "Frame empty!" << std::endl; } /* std::cout << ids.size() << " fiducials found:"; for(uint8_t i = 0; i < ids.size() && i < 20; i++) std::cout << pixelFiducials[i]; std::cout << std::endl; for(uint8_t i = 0; i < ids.size() && i < 20; i++) std::cout << ids[i]; std::cout << std::endl; for(uint8_t i = 0; i < ids.size() && i < 20; i++) std::cout << screenFiducials[i]; std::cout << std::endl; std::cout << "Sun center (pixels): " << pixelCenter << ", Sun center (screen): " << screenCenter << std::endl; */ } } } }
void QuoteTest::testForwardValueQuoteAndImpliedStdevQuote() { BOOST_MESSAGE( "Testing forward-value and implied-standard-deviation quotes..."); Real forwardRate = .05; DayCounter dc = ActualActual(); Calendar calendar = TARGET(); boost::shared_ptr<SimpleQuote> forwardQuote(new SimpleQuote(forwardRate)); Handle<Quote> forwardHandle(forwardQuote); Date evaluationDate = Settings::instance().evaluationDate(); boost::shared_ptr<YieldTermStructure>yc (new FlatForward( evaluationDate, forwardHandle, dc)); Handle<YieldTermStructure> ycHandle(yc); Period euriborTenor(1,Years); boost::shared_ptr<Index> euribor(new Euribor(euriborTenor, ycHandle)); Date fixingDate = calendar.advance(evaluationDate, euriborTenor); boost::shared_ptr<ForwardValueQuote> forwardValueQuote( new ForwardValueQuote(euribor, fixingDate)); Rate forwardValue = forwardValueQuote->value(); Rate expectedForwardValue = euribor->fixing(fixingDate, true); // we test if the forward value given by the quote is consistent // with the one directly given by the index if (std::fabs(forwardValue-expectedForwardValue) > 1.0e-15) BOOST_FAIL("Foward Value Quote quote yields " << forwardValue << "\n" << "expected result is " << expectedForwardValue); // then we test the observer/observable chain Flag f; f.registerWith(forwardValueQuote); forwardQuote->setValue(0.04); if (!f.isUp()) BOOST_FAIL("Observer was not notified of quote change"); // and we retest if the values are still matching forwardValue = forwardValueQuote->value(); expectedForwardValue = euribor->fixing(fixingDate, true); if (std::fabs(forwardValue-expectedForwardValue) > 1.0e-15) BOOST_FAIL("Foward Value Quote quote yields " << forwardValue << "\n" << "expected result is " << expectedForwardValue); // we test the ImpliedStdevQuote class f.unregisterWith(forwardValueQuote); f.lower(); Real price = 0.02; Rate strike = 0.04; Volatility guess = .15; Real accuracy = 1.0e-6; Option::Type optionType = Option::Call; boost::shared_ptr<SimpleQuote> priceQuote(new SimpleQuote(price)); Handle<Quote> priceHandle(priceQuote); boost::shared_ptr<ImpliedStdDevQuote> impliedStdevQuote(new ImpliedStdDevQuote(optionType, forwardHandle, priceHandle, strike, guess, accuracy)); Real impliedStdev = impliedStdevQuote->value(); Real expectedImpliedStdev = blackFormulaImpliedStdDev(optionType, strike, forwardQuote->value(), price, 1.0, 0.0, guess, 1.0e-6); if (std::fabs(impliedStdev-expectedImpliedStdev) > 1.0e-15) BOOST_FAIL("\nimpliedStdevQuote yields :" << impliedStdev << "\nexpected result is :" << expectedImpliedStdev); // then we test the observer/observable chain boost::shared_ptr<Quote> quote = impliedStdevQuote; f.registerWith(quote); forwardQuote->setValue(0.05); if (!f.isUp()) BOOST_FAIL("Observer was not notified of quote change"); quote->value(); f.lower(); quote->value(); priceQuote->setValue(0.11); if (!f.isUp()) BOOST_FAIL("Observer was not notified of quote change"); }