$treeview $search $mathjax
StdAir Logo  1.00.2
$projectbrief
$projectbrief
$searchbox

test/stdair/StandardAirlineITTestSuite.cpp

Go to the documentation of this file.
00001 
00005 // //////////////////////////////////////////////////////////////////////
00006 // Import section
00007 // //////////////////////////////////////////////////////////////////////
00008 // STL
00009 #include <sstream>
00010 #include <fstream>
00011 #include <string>
00012 // Boost MPL
00013 #include <boost/mpl/push_back.hpp>
00014 #include <boost/mpl/vector.hpp>
00015 #include <boost/mpl/at.hpp>
00016 #include <boost/mpl/assert.hpp>
00017 #include <boost/type_traits/is_same.hpp>
00018 // Boost Unit Test Framework (UTF)
00019 #define BOOST_TEST_DYN_LINK
00020 #define BOOST_TEST_MAIN
00021 #define BOOST_TEST_MODULE StdAirTest
00022 #if BOOST_VERSION >= 103900
00023 #include <boost/test/unit_test.hpp>
00024 #else  // BOOST_VERSION >= 103900
00025 #include <boost/test/test_tools.hpp>
00026 #include <boost/test/results_reporter.hpp>
00027 #include <boost/test/unit_test_suite.hpp>
00028 #include <boost/test/output_test_stream.hpp>
00029 #include <boost/test/unit_test_log.hpp>
00030 #include <boost/test/framework.hpp>
00031 #include <boost/test/detail/unit_test_parameters.hpp>
00032 #endif // BOOST_VERSION >= 103900
00033 // Boost Serialisation
00034 #include <boost/archive/text_oarchive.hpp>
00035 #include <boost/archive/text_iarchive.hpp>
00036 // StdAir
00037 #include <stdair/stdair_inventory_types.hpp>
00038 #include <stdair/service/Logger.hpp>
00039 #include <stdair/STDAIR_Service.hpp>
00040 #include <stdair/basic/float_utils.hpp>
00041 #include <stdair/bom/BomDisplay.hpp>
00042 #include <stdair/bom/BomRoot.hpp>
00043 #include <stdair/bom/BomManager.hpp>
00044 #include <stdair/factory/FacBom.hpp>
00045 #include <stdair/factory/FacBomManager.hpp>
00046 // StdAir Test Suite
00047 #include <test/stdair/StdairTestLib.hpp>
00048 #include <test/stdair/MPInventory.hpp>
00049 
00050 namespace boost_utf = boost::unit_test;
00051 
00052 #if BOOST_VERSION >= 103900
00053 
00054 // (Boost) Unit Test XML Report
00055 std::ofstream utfReportStream ("StandardAirlineITTestSuite_utfresults.xml");
00056 
00060 struct UnitTestConfig {
00062   UnitTestConfig() {
00063     boost_utf::unit_test_log.set_stream (utfReportStream);
00064     boost_utf::unit_test_log.set_format (boost_utf::XML);
00065     boost_utf::unit_test_log.set_threshold_level (boost_utf::log_test_units);
00066     // boost_utf::unit_test_log.set_threshold_level (boost_utf::log_successful_tests);
00067   }
00068 
00070   ~UnitTestConfig() {
00071   }
00072 };
00073 
00074 
00075 // /////////////// Main: Unit Test Suite //////////////
00076 
00077 // Set the UTF configuration (re-direct the output to a specific file)
00078 BOOST_GLOBAL_FIXTURE (UnitTestConfig);
00079 
00080 // Start the test suite
00081 BOOST_AUTO_TEST_SUITE (master_test_suite)
00082 
00083 
00087 BOOST_AUTO_TEST_CASE (float_comparison_test) {
00088   float a = 0.2f;
00089   a = 5*a;
00090   const float b = 1.0f;
00091 
00092   // Test the Boost way
00093   BOOST_CHECK_MESSAGE (a == b, "The two floats (" << a << " and " << b
00094                        << ") should be equal, but are not");
00095   BOOST_CHECK_CLOSE (a, b, 0.0001);
00096 
00097   // Test the Google way
00098   const FloatingPoint<float> lhs (a), rhs (b);
00099   BOOST_CHECK_MESSAGE (lhs.AlmostEquals (rhs),
00100                        "The two floats (" << a << " and " << b
00101                        << ") should be equal, but are not");
00102 }
00103 
00108 BOOST_AUTO_TEST_CASE (mpl_structure_test) {
00109   const stdair::ClassCode_T lBookingClassCodeA ("A");
00110   const stdair_test::BookingClass lA (lBookingClassCodeA);
00111   const stdair_test::Cabin lCabin (lA);
00112 
00113   BOOST_CHECK_EQUAL (lCabin.toString(), lBookingClassCodeA);
00114   BOOST_CHECK_MESSAGE (lCabin.toString() == lBookingClassCodeA,
00115                        "The cabin key, '" << lCabin.toString()
00116                        << "' is not equal to '" << lBookingClassCodeA << "'");
00117 
00118   // MPL
00119   typedef boost::mpl::vector<stdair_test::BookingClass> MPL_BookingClass;
00120   typedef boost::mpl::push_back<MPL_BookingClass,
00121                                 stdair_test::Cabin>::type types;
00122   
00123   if (boost::is_same<stdair_test::BookingClass,
00124                      stdair_test::Cabin::child>::value == false) {
00125     BOOST_ERROR ("The two types mut be equal, but are not");
00126   }
00127   
00128   if (boost::is_same<boost::mpl::at_c<types, 1>::type,
00129                      stdair_test::Cabin>::value == false) {
00130     BOOST_ERROR ("The type must be stdair_test::Cabin, but is not");
00131   }
00132 }
00133 
00137 BOOST_AUTO_TEST_CASE (stdair_service_initialisation_test) {
00138   // Output log File
00139   const std::string lLogFilename ("StandardAirlineITTestSuite_init.log");
00140     
00141   // Set the log parameters
00142   std::ofstream logOutputFile;
00143   
00144   // Open and clean the log outputfile
00145   logOutputFile.open (lLogFilename.c_str());
00146   logOutputFile.clear();
00147   
00148   // Initialise the stdair BOM
00149   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00150   stdair::STDAIR_Service stdairService (lLogParams);
00151 
00152   // Retrieve (a reference on) the top of the persistent BOM tree
00153   stdair::BomRoot& lPersistentBomRoot = stdairService.getPersistentBomRoot();
00154 
00155   // Retrieve the BomRoot key, and compare it to the expected one
00156   const std::string& lBomRootKeyStr = lPersistentBomRoot.describeKey();
00157   const std::string lBomRootString (" -- ROOT -- ");
00158 
00159   // DEBUG
00160   STDAIR_LOG_DEBUG ("The BOM root key is '" << lBomRootKeyStr
00161                     << "'. It should be equal to '" << lBomRootString << "'");
00162   
00163   BOOST_CHECK_EQUAL (lBomRootKeyStr, lBomRootString);
00164   BOOST_CHECK_MESSAGE (lBomRootKeyStr == lBomRootString,
00165                        "The BOM root key, '" << lBomRootKeyStr
00166                        << "', should be equal to '" << lBomRootString
00167                        << "', but is not.");
00168 
00169   // Build a sample BOM tree
00170   stdairService.buildSampleBom();
00171 
00172   // DEBUG: Display the whole BOM tree
00173   const std::string& lCSVDump = stdairService.csvDisplay ();
00174   STDAIR_LOG_DEBUG (lCSVDump);
00175 
00176   // Close the Log outputFile
00177   logOutputFile.close();
00178 }
00179 
00183 BOOST_AUTO_TEST_CASE (bom_structure_instantiation_test) {
00184   // Step 0.0: initialisation
00185   // Create the root of a Bom tree (i.e., a BomRoot object)
00186   stdair::BomRoot& lBomRoot =
00187     stdair::FacBom<stdair::BomRoot>::instance().create();
00188         
00189   // Step 0.1: Inventory level
00190   // Create an Inventory (BA)
00191   const stdair::AirlineCode_T lBAAirlineCode ("BA");
00192   const stdair::InventoryKey lBAKey (lBAAirlineCode);
00193   myprovider::Inventory& lBAInv =
00194     stdair::FacBom<myprovider::Inventory>::instance().create (lBAKey);
00195   stdair::FacBomManager::addToList (lBomRoot, lBAInv);
00196 
00197   BOOST_CHECK_EQUAL (lBAInv.describeKey(), lBAAirlineCode);
00198   BOOST_CHECK_MESSAGE (lBAInv.describeKey() == lBAAirlineCode,
00199                        "The inventory key, '" << lBAInv.describeKey()
00200                        << "', should be equal to '" << lBAAirlineCode
00201                        << "', but is not");
00202 
00203   // Create an Inventory for AF
00204   const stdair::AirlineCode_T lAFAirlineCode ("AF");
00205   const stdair::InventoryKey lAFKey (lAFAirlineCode);
00206   myprovider::Inventory& lAFInv =
00207     stdair::FacBom<myprovider::Inventory>::instance().create (lAFKey);
00208   stdair::FacBomManager::addToList (lBomRoot, lAFInv);
00209 
00210   BOOST_CHECK_EQUAL (lAFInv.describeKey(), lAFAirlineCode);
00211   BOOST_CHECK_MESSAGE (lAFInv.describeKey() == lAFAirlineCode,
00212                        "The inventory key, '" << lAFInv.describeKey()
00213                        << "', should be equal to '" << lAFAirlineCode
00214                        << "', but is not");
00215   
00216   // Browse the inventories
00217   const myprovider::InventoryList_T& lInventoryList =
00218       stdair::BomManager::getList<myprovider::Inventory> (lBomRoot);
00219   const std::string lInventoryKeyArray[2] = {lBAAirlineCode, lAFAirlineCode};
00220   short idx = 0;
00221   for (myprovider::InventoryList_T::const_iterator itInv =
00222          lInventoryList.begin(); itInv != lInventoryList.end();
00223        ++itInv, ++idx) {
00224     const myprovider::Inventory* lInv_ptr = *itInv;
00225     BOOST_REQUIRE (lInv_ptr != NULL);
00226     
00227     BOOST_CHECK_EQUAL (lInventoryKeyArray[idx], lInv_ptr->describeKey());
00228     BOOST_CHECK_MESSAGE (lInventoryKeyArray[idx] == lInv_ptr->describeKey(),
00229                          "They inventory key, '" << lInventoryKeyArray[idx]
00230                          << "', does not match that of the Inventory object: '"
00231                          << lInv_ptr->describeKey() << "'");
00232   }
00233 }
00234 
00238 BOOST_AUTO_TEST_CASE (bom_structure_serialisation_test) {
00239 
00240   // Backup (thanks to Boost.Serialisation) file
00241   const std::string lBackupFilename = "StandardAirlineITTestSuite_serial.txt";
00242 
00243   // Output log File
00244   const std::string lLogFilename ("StandardAirlineITTestSuite_serial.log");
00245     
00246   // Set the log parameters
00247   std::ofstream logOutputFile;
00248   
00249   // Open and clean the log outputfile
00250   logOutputFile.open (lLogFilename.c_str());
00251   logOutputFile.clear();
00252   
00253   // Initialise the stdair BOM
00254   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00255   stdair::STDAIR_Service stdairService (lLogParams);
00256 
00257   // Build a sample BOM tree
00258   stdairService.buildSampleBom();
00259 
00260   // Retrieve (a reference on) the top of the persistent BOM tree
00261   stdair::BomRoot& lPersistentBomRoot = stdairService.getPersistentBomRoot();
00262 
00263   // DEBUG: Display the whole BOM tree
00264   const std::string& lCSVDump = stdairService.csvDisplay ();
00265   STDAIR_LOG_DEBUG (lCSVDump);
00266 
00267   // Clone the persistent BOM
00268   stdairService.clonePersistentBom ();
00269 
00270   // Retrieve the BomRoot key, and compare it to the expected one
00271   const std::string lBAInvKeyStr ("BA");
00272   stdair::Inventory* lBAInv_ptr = 
00273     lPersistentBomRoot.getInventory (lBAInvKeyStr);
00274 
00275   // DEBUG
00276   STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
00277                     << lBAInvKeyStr << "' key.");
00278 
00279   BOOST_REQUIRE_MESSAGE (lBAInv_ptr != NULL,
00280                          "An Inventory object should exist with the key, '"
00281                          << lBAInvKeyStr << "'.");
00282 
00283   // create and open a character archive for output
00284   std::ofstream ofs (lBackupFilename.c_str());
00285 
00286   // save data to archive
00287   {
00288     boost::archive::text_oarchive oa (ofs);
00289     // write class instance to archive
00290     oa << lPersistentBomRoot;
00291     // archive and stream closed when destructors are called
00292   }
00293 
00294   // ... some time later restore the class instance to its orginal state
00295   stdair::BomRoot& lRestoredBomRoot =
00296     stdair::FacBom<stdair::BomRoot>::instance().create();
00297   {
00298     // create and open an archive for input
00299     std::ifstream ifs (lBackupFilename.c_str());
00300     boost::archive::text_iarchive ia(ifs);
00301     // read class state from archive
00302     ia >> lRestoredBomRoot;
00303     // archive and stream closed when destructors are called
00304   }
00305   
00306   // DEBUG: Display the whole restored BOM tree
00307   const std::string& lRestoredCSVDump = 
00308     stdairService.csvDisplay(lRestoredBomRoot);
00309   STDAIR_LOG_DEBUG (lRestoredCSVDump);
00310 
00311   // Retrieve the BomRoot key, and compare it to the expected one
00312   const std::string& lBomRootKeyStr = lRestoredBomRoot.describeKey();
00313   const std::string lBomRootString (" -- ROOT -- ");
00314 
00315   // DEBUG
00316   STDAIR_LOG_DEBUG ("The BOM root key is '" << lBomRootKeyStr
00317                     << "'. It should be equal to '" << lBomRootString << "'");
00318   
00319   BOOST_CHECK_EQUAL (lBomRootKeyStr, lBomRootString);
00320   BOOST_CHECK_MESSAGE (lBomRootKeyStr == lBomRootString,
00321                        "The BOM root key, '" << lBomRootKeyStr
00322                        << "', should be equal to '" << lBomRootString
00323                        << "', but is not.");
00324 
00325   // Retrieve the Inventory
00326   stdair::Inventory* lRestoredBAInv_ptr =
00327     lRestoredBomRoot.getInventory (lBAInvKeyStr);
00328 
00329   // DEBUG
00330   STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
00331                     << lBAInvKeyStr << "' key in the restored BOM root.");
00332 
00333   BOOST_CHECK_MESSAGE (lRestoredBAInv_ptr != NULL,
00334                        "An Inventory object should exist with the key, '"
00335                        << lBAInvKeyStr << "' in the restored BOM root.");
00336 
00337   // Close the Log outputFile
00338   logOutputFile.close();
00339 }
00340 
00344 BOOST_AUTO_TEST_CASE (bom_structure_clone_test) {  
00345 
00346   // Output log File
00347   const std::string lLogFilename ("StandardAirlineITTestSuite_clone.log");
00348 
00349   // Set the log parameters
00350   std::ofstream logOutputFile;
00351   
00352   // Open and clean the log outputfile
00353   logOutputFile.open (lLogFilename.c_str());
00354   logOutputFile.clear();
00355   
00356   // Initialise the stdair BOM
00357   const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
00358   stdair::STDAIR_Service stdairService (lLogParams);
00359 
00360   // Build a sample BOM tree
00361   stdairService.buildSampleBom();
00362 
00363   // Retrieve (a constant reference on) the top of the persistent BOM tree
00364   const stdair::BomRoot& lPersistentBomRoot =
00365     stdairService.getPersistentBomRoot();  
00366 
00367   // DEBUG: Display the whole persistent BOM tree
00368   const std::string& lCSVDump = stdairService.csvDisplay (); 
00369   STDAIR_LOG_DEBUG ("Display the persistent BOM tree.");
00370   STDAIR_LOG_DEBUG (lCSVDump); 
00371 
00372   // Clone the persistent BOM
00373   stdairService.clonePersistentBom (); 
00374 
00375   // Retrieve (a reference on) the top of the clone BOM tree
00376   stdair::BomRoot& lCloneBomRoot = stdairService.getBomRoot();   
00377 
00378   // DEBUG: Display the clone BOM tree after the clone process.
00379   const std::string& lAfterCloneCSVDump = 
00380     stdairService.csvDisplay(lCloneBomRoot); 
00381   STDAIR_LOG_DEBUG ("Display the clone BOM tree after the clone process.");
00382   STDAIR_LOG_DEBUG (lAfterCloneCSVDump);
00383 
00384   // Retrieve the clone BomRoot key, and compare it to the persistent BomRoot 
00385   // key.
00386   const std::string& lCloneBomRootKeyStr = lCloneBomRoot.describeKey();
00387   const std::string& lPersistentBomRootKeyStr = 
00388     lPersistentBomRoot.describeKey();
00389 
00390   // DEBUG
00391   STDAIR_LOG_DEBUG ("The clone BOM root key is '" << lCloneBomRootKeyStr
00392                     << "'. It should be equal to '" 
00393                     << lPersistentBomRootKeyStr << "'");
00394   
00395   BOOST_CHECK_EQUAL (lCloneBomRootKeyStr, lPersistentBomRootKeyStr);
00396   BOOST_CHECK_MESSAGE (lCloneBomRootKeyStr == lPersistentBomRootKeyStr,
00397                        "The clone BOM root key, '" << lCloneBomRootKeyStr
00398                        << "', should be equal to '" << lPersistentBomRootKeyStr
00399                        << "', but is not.");
00400 
00401   // Retrieve the BA inventory in the clone BOM root
00402   const std::string lBAInvKeyStr ("BA");
00403   stdair::Inventory* lCloneBAInv_ptr =
00404     lCloneBomRoot.getInventory (lBAInvKeyStr); 
00405 
00406   // DEBUG
00407   STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
00408                     << lBAInvKeyStr << "' key in the clone BOM root.");
00409 
00410   BOOST_CHECK_MESSAGE (lCloneBAInv_ptr != NULL,
00411                        "An Inventory object should exist with the key, '"
00412                        << lBAInvKeyStr << "' in the clone BOM root.");
00413 
00414   // Close the Log outputFile
00415   logOutputFile.close();
00416 }
00417 
00418 // End the test suite
00419 BOOST_AUTO_TEST_SUITE_END()
00420 
00421 #else // BOOST_VERSION >= 103900
00422 boost_utf::test_suite* init_unit_test_suite (int, char* []) {
00423   boost_utf::test_suite* test = BOOST_TEST_SUITE ("Unit test example 1");
00424   return test;
00425 }
00426 #endif // BOOST_VERSION >= 103900
00427