StdAir Logo  0.45.0
C++ Standard Airline IT Object Library
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_REQUIRE ("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_REQUIRE ("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 BOM tree
00153   stdair::BomRoot& lBomRoot = stdairService.getBomRoot();
00154 
00155   // Retrieve the BomRoot key, and compare it to the expected one
00156   const std::string& lBomRootKeyStr = lBomRoot.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 the 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   // DEBUG: Display the whole BOM tree
00261   const std::string& lCSVDump = stdairService.csvDisplay();
00262   STDAIR_LOG_DEBUG (lCSVDump);
00263 
00264   // Retrieve (a reference on) the top of the BOM tree
00265   stdair::BomRoot& lBomRoot = stdairService.getBomRoot();
00266 
00267   // Retrieve the BomRoot key, and compare it to the expected one
00268   const std::string lBAInvKeyStr ("BA");
00269   stdair::Inventory* lBAInv_ptr = lBomRoot.getInventory (lBAInvKeyStr);
00270 
00271   // DEBUG
00272   STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
00273                     << lBAInvKeyStr << "' key.");
00274 
00275   BOOST_REQUIRE_MESSAGE (lBAInv_ptr != NULL,
00276                          "An Inventory object should exist with the key, '"
00277                          << lBAInvKeyStr << "'.");
00278 
00279   // create and open a character archive for output
00280   std::ofstream ofs (lBackupFilename.c_str());
00281 
00282   // save data to archive
00283   {
00284     boost::archive::text_oarchive oa (ofs);
00285     // write class instance to archive
00286     oa << lBomRoot;
00287     // archive and stream closed when destructors are called
00288   }
00289 
00290   // ... some time later restore the class instance to its orginal state
00291   stdair::BomRoot& lRestoredBomRoot =
00292     stdair::FacBom<stdair::BomRoot>::instance().create();
00293   {
00294     // create and open an archive for input
00295     std::ifstream ifs (lBackupFilename.c_str());
00296     boost::archive::text_iarchive ia(ifs);
00297     // read class state from archive
00298     ia >> lRestoredBomRoot;
00299     // archive and stream closed when destructors are called
00300   }
00301   
00302   // DEBUG: Display the whole BOM tree
00303   std::ostringstream oRestoredCSVDumpStr;
00304   stdair::BomDisplay::csvDisplay (oRestoredCSVDumpStr, lRestoredBomRoot);
00305   STDAIR_LOG_DEBUG (oRestoredCSVDumpStr.str());
00306 
00307   // Retrieve the BomRoot key, and compare it to the expected one
00308   const std::string& lBomRootKeyStr = lRestoredBomRoot.describeKey();
00309   const std::string lBomRootString (" -- ROOT -- ");
00310 
00311   // DEBUG
00312   STDAIR_LOG_DEBUG ("The BOM root key is '" << lBomRootKeyStr
00313                     << "'. It should be equal to '" << lBomRootString << "'");
00314   
00315   BOOST_CHECK_EQUAL (lBomRootKeyStr, lBomRootString);
00316   BOOST_CHECK_MESSAGE (lBomRootKeyStr == lBomRootString,
00317                        "The BOM root key, '" << lBomRootKeyStr
00318                        << "', should be equal to '" << lBomRootString
00319                        << "', but is not.");
00320 
00321   // Retrieve the Inventory
00322   stdair::Inventory* lRestoredBAInv_ptr =
00323     lRestoredBomRoot.getInventory (lBAInvKeyStr);
00324 
00325   // DEBUG
00326   STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
00327                     << lBAInvKeyStr << "' key.");
00328 
00329   BOOST_CHECK_MESSAGE (lRestoredBAInv_ptr != NULL,
00330                        "An Inventory object should exist with the key, '"
00331                        << lBAInvKeyStr << "'.");
00332 
00333   // Close the Log outputFile
00334   logOutputFile.close();
00335 }
00336 
00337 // End the test suite
00338 BOOST_AUTO_TEST_SUITE_END()
00339 
00340 #else // BOOST_VERSION >= 103900
00341 boost_utf::test_suite* init_unit_test_suite (int, char* []) {
00342   boost_utf::test_suite* test = BOOST_TEST_SUITE ("Unit test example 1");
00343   return test;
00344 }
00345 #endif // BOOST_VERSION >= 103900
00346