*/
#include <sstream>
#include <fstream>
#include <map>
#include <cmath>
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE DemandGenerationTest
#include <boost/test/unit_test.hpp>
#include <stdair/stdair_basic_types.hpp>
#include <stdair/basic/BasConst_General.hpp>
#include <stdair/basic/BasLogParams.hpp>
#include <stdair/basic/BasDBParams.hpp>
#include <stdair/basic/BasFileMgr.hpp>
#include <stdair/basic/ProgressStatusSet.hpp>
#include <stdair/bom/EventStruct.hpp>
#include <stdair/bom/EventQueue.hpp>
#include <stdair/bom/BookingRequestStruct.hpp>
#include <stdair/service/Logger.hpp>
#include <trademgen/TRADEMGEN_Service.hpp>
#include <trademgen/bom/DemandStreamKey.hpp>
#include <trademgen/config/trademgen-paths.hpp>
namespace boost_utf = boost::unit_test;
std::ofstream utfReportStream ("DemandGenerationTestSuite_utfresults.xml");
struct UnitTestConfig {
UnitTestConfig() {
boost_utf::unit_test_log.set_stream (utfReportStream);
boost_utf::unit_test_log.set_format (boost_utf::XML);
boost_utf::unit_test_log.set_threshold_level (boost_utf::log_test_units);
}
~UnitTestConfig() {
}
};
typedef std::pair<stdair::Count_T, stdair::Count_T> NbOfEventsPair_T;
typedef std::map<const stdair::DemandStreamKeyStr_T,
NbOfEventsPair_T> NbOfEventsByDemandStreamMap_T;
BOOST_GLOBAL_FIXTURE (UnitTestConfig);
BOOST_AUTO_TEST_SUITE (master_test_suite)
BOOST_AUTO_TEST_CASE (trademgen_simple_simulation_test) {
const stdair::RandomSeed_T lRandomSeed = stdair::DEFAULT_RANDOM_SEED;
const stdair::Filename_T lInputFilename (STDAIR_SAMPLE_DIR "/demand01.csv");
const bool doesExistAndIsReadable =
stdair::BasFileMgr::doesExistAndIsReadable (lInputFilename);
BOOST_CHECK_MESSAGE (doesExistAndIsReadable == true,
"The '" << lInputFilename
<< "' input file can not be open and read");
const stdair::Filename_T lLogFilename ("DemandGenerationTestSuite.log");
std::ofstream logOutputFile;
logOutputFile.open (lLogFilename.c_str());
logOutputFile.clear();
const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
TRADEMGEN::TRADEMGEN_Service trademgenService (lLogParams, lRandomSeed);
BOOST_CHECK_NO_THROW (trademgenService.parseAndLoad (lInputFilename));
NbOfEventsByDemandStreamMap_T lNbOfEventsMap;
lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
value_type ("SIN-HND 2010-Feb-08 Y",
NbOfEventsPair_T (1, 10)));
lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
value_type ("SIN-HND 2010-Feb-09 Y",
NbOfEventsPair_T (1, 10)));
lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
value_type ("SIN-BKK 2010-Feb-08 Y",
NbOfEventsPair_T (1, 10)));
lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
value_type ("SIN-BKK 2010-Feb-09 Y",
NbOfEventsPair_T (1, 10)));
stdair::Count_T lRefExpectedNbOfEvents (40);
const stdair::Count_T& lExpectedNbOfEventsToBeGenerated =
trademgenService.getExpectedTotalNumberOfRequestsToBeGenerated();
BOOST_CHECK_EQUAL (lRefExpectedNbOfEvents,
std::floor (lExpectedNbOfEventsToBeGenerated));
BOOST_CHECK_MESSAGE (lRefExpectedNbOfEvents ==
std::floor (lExpectedNbOfEventsToBeGenerated),
"Expected total number of requests to be generated: "
<< lExpectedNbOfEventsToBeGenerated
<< " (=> "
<< std::floor (lExpectedNbOfEventsToBeGenerated)
<< "). Reference value: " << lRefExpectedNbOfEvents);
stdair::DemandGenerationMethod lDemandGenerationMethod (stdair::DemandGenerationMethod::STA_ORD);
const stdair::Count_T& lActualNbOfEventsToBeGenerated =
trademgenService.generateFirstRequests(lDemandGenerationMethod);
STDAIR_LOG_DEBUG ("Expected number of events: "
<< lExpectedNbOfEventsToBeGenerated << ", actual: "
<< lActualNbOfEventsToBeGenerated);
const stdair::Count_T lRefActualNbOfEvents (40);
BOOST_CHECK_EQUAL (lRefActualNbOfEvents, lActualNbOfEventsToBeGenerated);
BOOST_CHECK_MESSAGE (lRefActualNbOfEvents == lActualNbOfEventsToBeGenerated,
"Actual total number of requests to be generated: "
<< lExpectedNbOfEventsToBeGenerated
<< " (=> "
<< std::floor (lExpectedNbOfEventsToBeGenerated)
<< "). Reference value: " << lRefActualNbOfEvents);
const bool isQueueDone = trademgenService.isQueueDone();
BOOST_REQUIRE_MESSAGE (isQueueDone == false,
"The event queue should not be empty. You may check "
<< "the input file: '" << lInputFilename << "'");
stdair::Count_T idx = 1;
while (trademgenService.isQueueDone() == false) {
stdair::EventStruct lEventStruct;
stdair::ProgressStatusSet lPPS = trademgenService.popEvent (lEventStruct);
STDAIR_LOG_DEBUG ("Poped event: '" << lEventStruct.describe() << "'.");
const stdair::BookingRequestStruct& lPoppedRequest =
lEventStruct.getBookingRequest();
STDAIR_LOG_DEBUG ("Poped booking request: '"
<< lPoppedRequest.describe() << "'.");
const stdair::DemandGeneratorKey_T& lDemandStreamKey =
lPoppedRequest.getDemandGeneratorKey();
const NbOfEventsByDemandStreamMap_T::iterator itNbOfEventsMap =
lNbOfEventsMap.find (lDemandStreamKey);
BOOST_REQUIRE_MESSAGE (itNbOfEventsMap != lNbOfEventsMap.end(),
"The demand stream key '" << lDemandStreamKey
<< "' is not expected in that test");
const NbOfEventsPair_T& lNbOfEventsPair = itNbOfEventsMap->second;
stdair::Count_T lCurrentNbOfEvents = lNbOfEventsPair.first;
const stdair::Count_T& lExpectedTotalNbOfEvents = lNbOfEventsPair.second;
const bool stillHavingRequestsToBeGenerated = trademgenService.
stillHavingRequestsToBeGenerated (lDemandStreamKey, lPPS,
lDemandGenerationMethod);
if (lCurrentNbOfEvents == 1) {
const stdair::ProgressStatus& lDemandStreamProgressStatus =
lPPS.getSpecificGeneratorStatus();
const stdair::Count_T& lNbOfRequests =
lDemandStreamProgressStatus.getExpectedNb();
BOOST_CHECK_EQUAL (lNbOfRequests, lExpectedTotalNbOfEvents);
BOOST_CHECK_MESSAGE (lNbOfRequests == lExpectedTotalNbOfEvents,
"[" << lDemandStreamKey
<< "] Total number of requests to be generated: "
<< lNbOfRequests << "). Expected value: "
<< lExpectedTotalNbOfEvents);
}
STDAIR_LOG_DEBUG ("=> [" << lDemandStreamKey << "][" << lCurrentNbOfEvents
<< "/" << lExpectedTotalNbOfEvents
<< "] is now processed. "
<< "Still generate events for that demand stream? "
<< stillHavingRequestsToBeGenerated);
if (stillHavingRequestsToBeGenerated == true) {
const stdair::BookingRequestPtr_T lNextRequest_ptr =
trademgenService.generateNextRequest (lDemandStreamKey,
lDemandGenerationMethod);
assert (lNextRequest_ptr != NULL);
const stdair::Duration_T lDuration =
lNextRequest_ptr->getRequestDateTime()
- lPoppedRequest.getRequestDateTime();
BOOST_REQUIRE_GT (lDuration.total_milliseconds(), 0);
BOOST_REQUIRE_MESSAGE (lDuration.total_milliseconds() > 0,
"[" << lDemandStreamKey
<< "] The date-time of the generated event ("
<< lNextRequest_ptr->getRequestDateTime()
<< ") is lower than the date-time "
<< "of the current event ("
<< lPoppedRequest.getRequestDateTime() << ")");
STDAIR_LOG_DEBUG ("[" << lDemandStreamKey << "][" << lCurrentNbOfEvents
<< "/" << lExpectedTotalNbOfEvents
<< "] Added request: '" << lNextRequest_ptr->describe()
<< "'. Is queue done? "
<< trademgenService.isQueueDone());
++lCurrentNbOfEvents;
itNbOfEventsMap->second = NbOfEventsPair_T (lCurrentNbOfEvents,
lExpectedTotalNbOfEvents);
}
++idx;
}
--idx;
BOOST_CHECK_EQUAL (idx, lRefActualNbOfEvents);
BOOST_CHECK_MESSAGE (idx == lRefActualNbOfEvents,
"The total actual number of events is "
<< lRefActualNbOfEvents << ", but " << idx
<< " events have been generated");
trademgenService.reset();
STDAIR_LOG_DEBUG ("End of the simulation");
logOutputFile.close();
}
BOOST_AUTO_TEST_SUITE_END()