14 #define BOOST_TEST_DYN_LINK
15 #define BOOST_TEST_MAIN
16 #define BOOST_TEST_MODULE DemandGenerationTest
17 #include <boost/test/unit_test.hpp>
19 #include <stdair/stdair_basic_types.hpp>
20 #include <stdair/basic/BasConst_General.hpp>
21 #include <stdair/basic/BasLogParams.hpp>
22 #include <stdair/basic/BasDBParams.hpp>
23 #include <stdair/basic/BasFileMgr.hpp>
24 #include <stdair/basic/ProgressStatusSet.hpp>
25 #include <stdair/bom/EventStruct.hpp>
26 #include <stdair/bom/BookingRequestStruct.hpp>
27 #include <stdair/service/Logger.hpp>
32 #include <trademgen/config/trademgen-paths.hpp>
34 namespace boost_utf = boost::unit_test;
37 std::ofstream utfReportStream (
"DemandGenerationTestSuite_utfresults.xml");
42 struct UnitTestConfig {
45 boost_utf::unit_test_log.set_stream (utfReportStream);
46 boost_utf::unit_test_log.set_format (boost_utf::XML);
47 boost_utf::unit_test_log.set_threshold_level (boost_utf::log_test_units);
57 typedef std::pair<stdair::Count_T, stdair::Count_T> NbOfEventsPair_T;
58 typedef std::map<
const stdair::DemandStreamKeyStr_T,
59 NbOfEventsPair_T> NbOfEventsByDemandStreamMap_T;
65 void testDemandGenerationHelper (
const unsigned short iTestFlag,
66 const stdair::Filename_T& iDemandInputFilename,
67 const stdair::DemandGenerationMethod& iDemandGenerationMethod,
68 const bool isBuiltin) {
71 const stdair::RandomSeed_T lRandomSeed = stdair::DEFAULT_RANDOM_SEED;
74 std::ostringstream oStr;
75 oStr <<
"DemandGenerationTestSuite_" << iTestFlag <<
".log";
76 const stdair::Filename_T lLogFilename (oStr.str());
79 std::ofstream logOutputFile;
81 logOutputFile.open (lLogFilename.c_str());
82 logOutputFile.clear();
85 const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
97 NbOfEventsByDemandStreamMap_T lNbOfEventsMap;
100 stdair::Count_T lRefExpectedNbOfEvents (0);
101 stdair::Count_T lRefActualNbOfEvents (0);
104 if (isBuiltin ==
true) {
107 trademgenService.buildSampleBom();
109 lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
110 value_type (
"SIN-BKK 2010-Feb-08 Y",
111 NbOfEventsPair_T (4, 60)));
112 lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
113 value_type (
"BKK-HKG 2010-Feb-08 Y",
114 NbOfEventsPair_T (4, 60)));
115 lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
116 value_type (
"SIN-HKG 2010-Feb-08 Y",
117 NbOfEventsPair_T (4, 60)));
120 lRefExpectedNbOfEvents = 180;
121 lRefActualNbOfEvents = 186;
127 trademgenService.parseAndLoad (lDemandFilePath);
129 lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
130 value_type (
"SIN-HND 2010-Feb-08 Y",
131 NbOfEventsPair_T (1, 10)));
132 lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
133 value_type (
"SIN-HND 2010-Feb-09 Y",
134 NbOfEventsPair_T (1, 10)));
135 lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
136 value_type (
"SIN-BKK 2010-Feb-08 Y",
137 NbOfEventsPair_T (1, 10)));
138 lNbOfEventsMap.insert (NbOfEventsByDemandStreamMap_T::
139 value_type (
"SIN-BKK 2010-Feb-09 Y",
140 NbOfEventsPair_T (1, 10)));
143 lRefExpectedNbOfEvents = 40;
144 lRefActualNbOfEvents = 40;
149 const stdair::Count_T& lExpectedNbOfEventsToBeGenerated =
150 trademgenService.getExpectedTotalNumberOfRequestsToBeGenerated();
152 BOOST_CHECK_EQUAL (lRefExpectedNbOfEvents,
153 std::floor (lExpectedNbOfEventsToBeGenerated));
155 BOOST_CHECK_MESSAGE (lRefExpectedNbOfEvents ==
156 std::floor (lExpectedNbOfEventsToBeGenerated),
157 "Expected total number of requests to be generated: "
158 << lExpectedNbOfEventsToBeGenerated
160 << std::floor (lExpectedNbOfEventsToBeGenerated)
161 <<
"). Reference value: " << lRefExpectedNbOfEvents);
168 const stdair::Count_T& lActualNbOfEventsToBeGenerated =
169 trademgenService.generateFirstRequests(iDemandGenerationMethod);
172 STDAIR_LOG_DEBUG (
"Expected number of events: "
173 << lExpectedNbOfEventsToBeGenerated <<
", actual: "
174 << lActualNbOfEventsToBeGenerated);
177 BOOST_CHECK_EQUAL (lRefActualNbOfEvents, lActualNbOfEventsToBeGenerated);
179 BOOST_CHECK_MESSAGE (lRefActualNbOfEvents == lActualNbOfEventsToBeGenerated,
180 "Actual total number of requests to be generated: "
181 << lExpectedNbOfEventsToBeGenerated
183 << std::floor (lExpectedNbOfEventsToBeGenerated)
184 <<
"). Reference value: " << lRefActualNbOfEvents);
187 const bool isQueueDone = trademgenService.isQueueDone();
188 BOOST_REQUIRE_MESSAGE (isQueueDone ==
false,
189 "The event queue should not be empty.");
198 stdair::Count_T idx = 1;
199 while (trademgenService.isQueueDone() ==
false) {
202 stdair::EventStruct lEventStruct;
203 stdair::ProgressStatusSet lPPS = trademgenService.popEvent (lEventStruct);
206 STDAIR_LOG_DEBUG (
"Poped event: '" << lEventStruct.describe() <<
"'.");
209 const stdair::BookingRequestStruct& lPoppedRequest =
210 lEventStruct.getBookingRequest();
213 STDAIR_LOG_DEBUG (
"Poped booking request: '"
214 << lPoppedRequest.describe() <<
"'.");
217 const stdair::DemandGeneratorKey_T& lDemandStreamKey =
218 lPoppedRequest.getDemandGeneratorKey();
221 const NbOfEventsByDemandStreamMap_T::iterator itNbOfEventsMap =
222 lNbOfEventsMap.find (lDemandStreamKey);
223 BOOST_REQUIRE_MESSAGE (itNbOfEventsMap != lNbOfEventsMap.end(),
224 "The demand stream key '" << lDemandStreamKey
225 <<
"' is not expected in that test");
236 const NbOfEventsPair_T& lNbOfEventsPair = itNbOfEventsMap->second;
237 stdair::Count_T lCurrentNbOfEvents = lNbOfEventsPair.first;
238 const stdair::Count_T& lExpectedTotalNbOfEvents = lNbOfEventsPair.second;
241 const bool stillHavingRequestsToBeGenerated = trademgenService.
242 stillHavingRequestsToBeGenerated (lDemandStreamKey, lPPS,
243 iDemandGenerationMethod);
251 if (lCurrentNbOfEvents == 1) {
257 const stdair::ProgressStatus& lDemandStreamProgressStatus =
258 lPPS.getSpecificGeneratorStatus();
259 const stdair::Count_T& lNbOfRequests =
260 lDemandStreamProgressStatus.getExpectedNb();
262 BOOST_CHECK_EQUAL (lNbOfRequests, lExpectedTotalNbOfEvents);
263 BOOST_CHECK_MESSAGE (lNbOfRequests == lExpectedTotalNbOfEvents,
264 "[" << lDemandStreamKey
265 <<
"] Total number of requests to be generated: "
266 << lNbOfRequests <<
"). Expected value: "
267 << lExpectedTotalNbOfEvents);
271 STDAIR_LOG_DEBUG (
"=> [" << lDemandStreamKey <<
"][" << lCurrentNbOfEvents
272 <<
"/" << lExpectedTotalNbOfEvents
273 <<
"] is now processed. "
274 <<
"Still generate events for that demand stream? "
275 << stillHavingRequestsToBeGenerated);
279 if (stillHavingRequestsToBeGenerated ==
true) {
280 const stdair::BookingRequestPtr_T lNextRequest_ptr =
281 trademgenService.generateNextRequest (lDemandStreamKey,
282 iDemandGenerationMethod);
283 assert (lNextRequest_ptr != NULL);
290 const stdair::Duration_T lDuration =
291 lNextRequest_ptr->getRequestDateTime()
292 - lPoppedRequest.getRequestDateTime();
293 BOOST_REQUIRE_GT (lDuration.total_milliseconds(), 0);
294 BOOST_REQUIRE_MESSAGE (lDuration.total_milliseconds() > 0,
295 "[" << lDemandStreamKey
296 <<
"] The date-time of the generated event ("
297 << lNextRequest_ptr->getRequestDateTime()
298 <<
") is lower than the date-time "
299 <<
"of the current event ("
300 << lPoppedRequest.getRequestDateTime() <<
")");
303 STDAIR_LOG_DEBUG (
"[" << lDemandStreamKey <<
"][" << lCurrentNbOfEvents
304 <<
"/" << lExpectedTotalNbOfEvents
305 <<
"] Added request: '" << lNextRequest_ptr->describe()
306 <<
"'. Is queue done? "
307 << trademgenService.isQueueDone());
310 ++lCurrentNbOfEvents;
311 itNbOfEventsMap->second = NbOfEventsPair_T (lCurrentNbOfEvents,
312 lExpectedTotalNbOfEvents);
321 if (iDemandGenerationMethod == stdair::DemandGenerationMethod::STA_ORD) {
323 BOOST_CHECK_EQUAL (idx, lRefActualNbOfEvents);
324 BOOST_CHECK_MESSAGE (idx == lRefActualNbOfEvents,
325 "The total actual number of events is "
326 << lRefActualNbOfEvents <<
", but " << idx
327 <<
" events have been generated");
332 trademgenService.reset();
335 STDAIR_LOG_DEBUG (
"End of the simulation");
338 logOutputFile.close();
346 BOOST_GLOBAL_FIXTURE (UnitTestConfig);
349 BOOST_AUTO_TEST_SUITE (master_test_suite)
354 BOOST_AUTO_TEST_CASE (trademgen_simple_simulation_test) {
357 const stdair::Filename_T lInputFilename (STDAIR_SAMPLE_DIR
"/demand01.csv");
360 const stdair::DemandGenerationMethod lDemandGenerationMethod (stdair::DemandGenerationMethod::STA_ORD);
363 const bool isBuiltin =
false;
364 BOOST_CHECK_NO_THROW (testDemandGenerationHelper(0,
366 lDemandGenerationMethod,
374 BOOST_AUTO_TEST_CASE (trademgen_missing_input_file_test) {
377 const stdair::Filename_T lInputFilename (STDAIR_SAMPLE_DIR
"/missingFile.csv");
380 const stdair::DemandGenerationMethod lDemandGenerationMethod (stdair::DemandGenerationMethod::STA_ORD);
383 const bool isBuiltin =
false;
384 BOOST_CHECK_THROW (testDemandGenerationHelper(1,
386 lDemandGenerationMethod,
395 BOOST_AUTO_TEST_CASE (trademgen_default_bom_simulation_test) {
398 const stdair::DemandGenerationMethod lDemandGenerationMethod (stdair::DemandGenerationMethod::STA_ORD);
401 const bool isBuiltin =
true;
402 BOOST_CHECK_NO_THROW (testDemandGenerationHelper(2,
404 lDemandGenerationMethod,
412 BOOST_AUTO_TEST_CASE (trademgen_poisson_process_test) {
415 const stdair::DemandGenerationMethod lDemandGenerationMethod (stdair::DemandGenerationMethod::POI_PRO);
418 const bool isBuiltin =
true;
419 BOOST_CHECK_NO_THROW (testDemandGenerationHelper(3,
421 lDemandGenerationMethod,
427 BOOST_AUTO_TEST_SUITE_END()