Examples for Boost Libraries |
This example demonstrates a test case for boost::array an, STL compliant container wrapper for arrays of constant size. STL provides class std::vector as a replacement for ordinary arrays. However, std::vector<> provides the semantics of dynamic arrays. Thus, it manages data to be able to change the number of elements. This results in some overhead only in the case of arrays that need static size. Class array fulfills most but, not all of the requirements of "reversible containers". An array is not a reversible STL container because of the following reasons:
boost::array <int,4>a={1,2,3}Note : If there are fewer elements in the initializer list, then each remaining element gets default-initialized (thus, it has a defined value).
The MMP file for Array is as shown below:
TARGET OpenCBoostArrayEx.exe TARGETTYPE exe UID 0 0xEA9D51D0 USERINCLUDE ..\inc SOURCEPATH ..\data START RESOURCE OpenCBoostArrayEx_reg.rss #ifdef WINSCW TARGETPATH \private\10003a3f\apps #else TARGETPATH \private\10003a3f\import\apps #endif END //RESOURCE SOURCEPATH ..\src SOURCE OpenCBoostArrayEx.cpp SYSTEMINCLUDE \epoc32\include SYSTEMINCLUDE \epoc32\include\stdapis SYSTEMINCLUDE \epoc32\include\stdapis\sys SYSTEMINCLUDE \epoc32\include\stdapis\stlport SYSTEMINCLUDE \epoc32\include\stdapis\stlport\stl SYSTEMINCLUDE \epoc32\include\stdapis\boost STATICLIBRARY libcrt0.lib STATICLIBRARY EEXE.LIB LIBRARY libstdcpp.lib LIBRARY libc.lib LIBRARY libpthread.lib LIBRARY euser.lib OPTION CW -wchar_t on MACRO _WCHAR_T_DECLARED
The code snippet below is an example usage for the boost array:
#include <string> #include <iostream> #include <boost/array.hpp> // This is a GCCE toolchain workaround needed when compiling with GCCE // and using main() entry point #ifdef __GCCE__ #include <staticlibinit_gcce.h> #endif namespace { unsigned int failed_tests = 0; void fail_test( const char * reason ) { ++failed_tests; std::cerr << "Test failure " << failed_tests << ": " << reason << std::endl; } template<class T> void BadValue( const T & ) { fail_test( "Unexpected value" ); } template<class T> void RunTests() { typedef boost::array< T, 0 > test_type; // Test value and aggegrate initialization test_type test_case = {}; const boost::array< T, 0 > const_test_case = test_type(); test_case.assign( T() ); // front/back and operator[] must compile, but calling them is undefined // Likewise, all tests below should evaluate to false, avoiding undefined behaviour if( !test_case.empty() ) { BadValue( test_case.front() ); } if( !const_test_case.empty() ) { BadValue( const_test_case.back() ); } if( test_case.size() > 0 ) { BadValue( test_case[ 0 ] ); } if( const_test_case.max_size() > 0 ) { BadValue( const_test_case[ 0 ] ); } // Assert requirements of TR1 6.2.2.4 if( test_case.begin() != test_case.end() ) { fail_test( "Not an empty range" ); } if( const_test_case.begin() != const_test_case.end() ) { fail_test( "Not an empty range" ); } if( test_case.begin() == const_test_case.begin() ) { fail_test( "iterators for different containers are not distinct" ); } if( test_case.data() == const_test_case.data() ) { // Value of data is unspecified in TR1, so no requirement this test pass or fail // However, it must compile! } // Check can safely use all iterator types with std algorithms std::for_each( test_case.begin(), test_case.end(), BadValue< T > ); std::for_each( test_case.rbegin(), test_case.rend(), BadValue< T > ); std::for_each( const_test_case.begin(), const_test_case.end(), BadValue< T > ); std::for_each( const_test_case.rbegin(), const_test_case.rend(), BadValue< T > ); // Check swap is well formed std::swap( test_case, test_case ); // Check assigment operator and overloads are well formed test_case = const_test_case; // Confirm at() throws the std lib defined exception try { BadValue( test_case.at( 0 ) ); } catch ( const std::range_error & ) { } try { BadValue( const_test_case.at( 0 ) ); } catch ( const std::range_error & ) { } } } int main() { RunTests< bool >(); RunTests< void * >(); RunTests< long double >(); RunTests< std::string >(); if(failed_tests != 0) printf("Test Failed"); else printf("Test Passed. No failed cases for Bool, void, double and string"); getchar(); return 0; }