StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StandardAirlineITTestSuite.cpp
Go to the documentation of this file.
1 
5 // //////////////////////////////////////////////////////////////////////
6 // Import section
7 // //////////////////////////////////////////////////////////////////////
8 // STL
9 #include <sstream>
10 #include <fstream>
11 #include <string>
12 // Boost MPL
13 #include <boost/mpl/push_back.hpp>
14 #include <boost/mpl/vector.hpp>
15 #include <boost/mpl/at.hpp>
16 #include <boost/mpl/assert.hpp>
17 #include <boost/type_traits/is_same.hpp>
18 // Boost Unit Test Framework (UTF)
19 #define BOOST_TEST_DYN_LINK
20 #define BOOST_TEST_MAIN
21 #define BOOST_TEST_MODULE StdAirTest
22 #if BOOST_VERSION >= 103900
23 #include <boost/test/unit_test.hpp>
24 #else // BOOST_VERSION >= 103900
25 #include <boost/test/test_tools.hpp>
26 #include <boost/test/results_reporter.hpp>
27 #include <boost/test/unit_test_suite.hpp>
28 #include <boost/test/output_test_stream.hpp>
29 #include <boost/test/unit_test_log.hpp>
30 #include <boost/test/framework.hpp>
31 #include <boost/test/detail/unit_test_parameters.hpp>
32 #endif // BOOST_VERSION >= 103900
33 // Boost Serialisation
34 #include <boost/archive/text_oarchive.hpp>
35 #include <boost/archive/text_iarchive.hpp>
36 // StdAir
42 #include <stdair/bom/BomRoot.hpp>
46 // StdAir Test Suite
49 
50 namespace boost_utf = boost::unit_test;
51 
52 #if BOOST_VERSION >= 103900
53 
54 // (Boost) Unit Test XML Report
55 std::ofstream utfReportStream ("StandardAirlineITTestSuite_utfresults.xml");
56 
60 struct UnitTestConfig {
62  UnitTestConfig() {
63  boost_utf::unit_test_log.set_stream (utfReportStream);
64  boost_utf::unit_test_log.set_format (boost_utf::XML);
65  boost_utf::unit_test_log.set_threshold_level (boost_utf::log_test_units);
66  // boost_utf::unit_test_log.set_threshold_level (boost_utf::log_successful_tests);
67  }
68 
70  ~UnitTestConfig() {
71  }
72 };
73 
74 
75 // /////////////// Main: Unit Test Suite //////////////
76 
77 // Set the UTF configuration (re-direct the output to a specific file)
78 BOOST_GLOBAL_FIXTURE (UnitTestConfig);
79 
80 // Start the test suite
81 BOOST_AUTO_TEST_SUITE (master_test_suite)
82 
83 
87 BOOST_AUTO_TEST_CASE (float_comparison_test) {
88  float a = 0.2f;
89  a = 5*a;
90  const float b = 1.0f;
91 
92  // Test the Boost way
93  BOOST_CHECK_MESSAGE (a == b, "The two floats (" << a << " and " << b
94  << ") should be equal, but are not");
95  BOOST_CHECK_CLOSE (a, b, 0.0001);
96 
97  // Test the Google way
98  const FloatingPoint<float> lhs (a), rhs (b);
99  BOOST_CHECK_MESSAGE (lhs.AlmostEquals (rhs),
100  "The two floats (" << a << " and " << b
101  << ") should be equal, but are not");
102 }
103 
108 BOOST_AUTO_TEST_CASE (mpl_structure_test) {
109  const stdair::ClassCode_T lBookingClassCodeA ("A");
110  const stdair_test::BookingClass lA (lBookingClassCodeA);
111  const stdair_test::Cabin lCabin (lA);
112 
113  BOOST_CHECK_EQUAL (lCabin.toString(), lBookingClassCodeA);
114  BOOST_CHECK_MESSAGE (lCabin.toString() == lBookingClassCodeA,
115  "The cabin key, '" << lCabin.toString()
116  << "' is not equal to '" << lBookingClassCodeA << "'");
117 
118  // MPL
119  typedef boost::mpl::vector<stdair_test::BookingClass> MPL_BookingClass;
120  typedef boost::mpl::push_back<MPL_BookingClass,
121  stdair_test::Cabin>::type types;
122 
123  if (boost::is_same<stdair_test::BookingClass,
124  stdair_test::Cabin::child>::value == false) {
125  BOOST_REQUIRE ("The two types mut be equal, but are not");
126  }
127 
128  if (boost::is_same<boost::mpl::at_c<types, 1>::type,
129  stdair_test::Cabin>::value == false) {
130  BOOST_REQUIRE ("The type must be stdair_test::Cabin, but is not");
131  }
132 }
133 
137 BOOST_AUTO_TEST_CASE (stdair_service_initialisation_test) {
138  // Output log File
139  const std::string lLogFilename ("StandardAirlineITTestSuite_init.log");
140 
141  // Set the log parameters
142  std::ofstream logOutputFile;
143 
144  // Open and clean the log outputfile
145  logOutputFile.open (lLogFilename.c_str());
146  logOutputFile.clear();
147 
148  // Initialise the stdair BOM
149  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
150  stdair::STDAIR_Service stdairService (lLogParams);
151 
152  // Retrieve (a reference on) the top of the BOM tree
153  stdair::BomRoot& lBomRoot = stdairService.getBomRoot();
154 
155  // Retrieve the BomRoot key, and compare it to the expected one
156  const std::string& lBomRootKeyStr = lBomRoot.describeKey();
157  const std::string lBomRootString (" -- ROOT -- ");
158 
159  // DEBUG
160  STDAIR_LOG_DEBUG ("The BOM root key is '" << lBomRootKeyStr
161  << "'. It should be equal to '" << lBomRootString << "'");
162 
163  BOOST_CHECK_EQUAL (lBomRootKeyStr, lBomRootString);
164  BOOST_CHECK_MESSAGE (lBomRootKeyStr == lBomRootString,
165  "The BOM root key, '" << lBomRootKeyStr
166  << "', should be equal to '" << lBomRootString
167  << "', but is not.");
168 
169  // Build a sample BOM tree
170  stdairService.buildSampleBom();
171 
172  // DEBUG: Display the whole BOM tree
173  const std::string& lCSVDump = stdairService.csvDisplay();
174  STDAIR_LOG_DEBUG (lCSVDump);
175 
176  // Close the Log outputFile
177  logOutputFile.close();
178 }
179 
183 BOOST_AUTO_TEST_CASE (bom_structure_instantiation_test) {
184  // Step 0.0: initialisation
185  // Create the root of the Bom tree (i.e., a BomRoot object)
186  stdair::BomRoot& lBomRoot =
188 
189  // Step 0.1: Inventory level
190  // Create an Inventory (BA)
191  const stdair::AirlineCode_T lBAAirlineCode ("BA");
192  const stdair::InventoryKey lBAKey (lBAAirlineCode);
193  myprovider::Inventory& lBAInv =
195  stdair::FacBomManager::addToList (lBomRoot, lBAInv);
196 
197  BOOST_CHECK_EQUAL (lBAInv.describeKey(), lBAAirlineCode);
198  BOOST_CHECK_MESSAGE (lBAInv.describeKey() == lBAAirlineCode,
199  "The inventory key, '" << lBAInv.describeKey()
200  << "', should be equal to '" << lBAAirlineCode
201  << "', but is not");
202 
203  // Create an Inventory for AF
204  const stdair::AirlineCode_T lAFAirlineCode ("AF");
205  const stdair::InventoryKey lAFKey (lAFAirlineCode);
206  myprovider::Inventory& lAFInv =
208  stdair::FacBomManager::addToList (lBomRoot, lAFInv);
209 
210  BOOST_CHECK_EQUAL (lAFInv.describeKey(), lAFAirlineCode);
211  BOOST_CHECK_MESSAGE (lAFInv.describeKey() == lAFAirlineCode,
212  "The inventory key, '" << lAFInv.describeKey()
213  << "', should be equal to '" << lAFAirlineCode
214  << "', but is not");
215 
216  // Browse the inventories
217  const myprovider::InventoryList_T& lInventoryList =
218  stdair::BomManager::getList<myprovider::Inventory> (lBomRoot);
219  const std::string lInventoryKeyArray[2] = {lBAAirlineCode, lAFAirlineCode};
220  short idx = 0;
221  for (myprovider::InventoryList_T::const_iterator itInv =
222  lInventoryList.begin(); itInv != lInventoryList.end();
223  ++itInv, ++idx) {
224  const myprovider::Inventory* lInv_ptr = *itInv;
225  BOOST_REQUIRE (lInv_ptr != NULL);
226 
227  BOOST_CHECK_EQUAL (lInventoryKeyArray[idx], lInv_ptr->describeKey());
228  BOOST_CHECK_MESSAGE (lInventoryKeyArray[idx] == lInv_ptr->describeKey(),
229  "They inventory key, '" << lInventoryKeyArray[idx]
230  << "', does not match that of the Inventory object: '"
231  << lInv_ptr->describeKey() << "'");
232  }
233 }
234 
238 BOOST_AUTO_TEST_CASE (bom_structure_serialisation_test) {
239 
240  // Backup (thanks to Boost.Serialisation) file
241  const std::string lBackupFilename = "StandardAirlineITTestSuite_serial.txt";
242 
243  // Output log File
244  const std::string lLogFilename ("StandardAirlineITTestSuite_serial.log");
245 
246  // Set the log parameters
247  std::ofstream logOutputFile;
248 
249  // Open and clean the log outputfile
250  logOutputFile.open (lLogFilename.c_str());
251  logOutputFile.clear();
252 
253  // Initialise the stdair BOM
254  const stdair::BasLogParams lLogParams (stdair::LOG::DEBUG, logOutputFile);
255  stdair::STDAIR_Service stdairService (lLogParams);
256 
257  // Build a sample BOM tree
258  stdairService.buildSampleBom();
259 
260  // DEBUG: Display the whole BOM tree
261  const std::string& lCSVDump = stdairService.csvDisplay();
262  STDAIR_LOG_DEBUG (lCSVDump);
263 
264  // Retrieve (a reference on) the top of the BOM tree
265  stdair::BomRoot& lBomRoot = stdairService.getBomRoot();
266 
267  // Retrieve the BomRoot key, and compare it to the expected one
268  const std::string lBAInvKeyStr ("BA");
269  stdair::Inventory* lBAInv_ptr = lBomRoot.getInventory (lBAInvKeyStr);
270 
271  // DEBUG
272  STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
273  << lBAInvKeyStr << "' key.");
274 
275  BOOST_REQUIRE_MESSAGE (lBAInv_ptr != NULL,
276  "An Inventory object should exist with the key, '"
277  << lBAInvKeyStr << "'.");
278 
279  // create and open a character archive for output
280  std::ofstream ofs (lBackupFilename.c_str());
281 
282  // save data to archive
283  {
284  boost::archive::text_oarchive oa (ofs);
285  // write class instance to archive
286  oa << lBomRoot;
287  // archive and stream closed when destructors are called
288  }
289 
290  // ... some time later restore the class instance to its orginal state
291  stdair::BomRoot& lRestoredBomRoot =
293  {
294  // create and open an archive for input
295  std::ifstream ifs (lBackupFilename.c_str());
296  boost::archive::text_iarchive ia(ifs);
297  // read class state from archive
298  ia >> lRestoredBomRoot;
299  // archive and stream closed when destructors are called
300  }
301 
302  // DEBUG: Display the whole BOM tree
303  std::ostringstream oRestoredCSVDumpStr;
304  stdair::BomDisplay::csvDisplay (oRestoredCSVDumpStr, lRestoredBomRoot);
305  STDAIR_LOG_DEBUG (oRestoredCSVDumpStr.str());
306 
307  // Retrieve the BomRoot key, and compare it to the expected one
308  const std::string& lBomRootKeyStr = lRestoredBomRoot.describeKey();
309  const std::string lBomRootString (" -- ROOT -- ");
310 
311  // DEBUG
312  STDAIR_LOG_DEBUG ("The BOM root key is '" << lBomRootKeyStr
313  << "'. It should be equal to '" << lBomRootString << "'");
314 
315  BOOST_CHECK_EQUAL (lBomRootKeyStr, lBomRootString);
316  BOOST_CHECK_MESSAGE (lBomRootKeyStr == lBomRootString,
317  "The BOM root key, '" << lBomRootKeyStr
318  << "', should be equal to '" << lBomRootString
319  << "', but is not.");
320 
321  // Retrieve the Inventory
322  stdair::Inventory* lRestoredBAInv_ptr =
323  lRestoredBomRoot.getInventory (lBAInvKeyStr);
324 
325  // DEBUG
326  STDAIR_LOG_DEBUG ("There should be an Inventory object corresponding to the '"
327  << lBAInvKeyStr << "' key.");
328 
329  BOOST_CHECK_MESSAGE (lRestoredBAInv_ptr != NULL,
330  "An Inventory object should exist with the key, '"
331  << lBAInvKeyStr << "'.");
332 
333  // Close the Log outputFile
334  logOutputFile.close();
335 }
336 
337 // End the test suite
338 BOOST_AUTO_TEST_SUITE_END()
339 
340 #else // BOOST_VERSION >= 103900
341 boost_utf::test_suite* init_unit_test_suite (int, char* []) {
342  boost_utf::test_suite* test = BOOST_TEST_SUITE ("Unit test example 1");
343  return test;
344 }
345 #endif // BOOST_VERSION >= 103900
346