diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 new file mode 100644 index 0000000..1379a5b --- /dev/null +++ b/test/Jamfile.v2 @@ -0,0 +1,14 @@ +#~ Copyright Rene Rivera 2008 +#~ Distributed under the Boost Software License, Version 1.0. +#~ (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +import testing ; + +test-suite array : + [ run array0.cpp ] + [ run array1.cpp ] + [ run array2.cpp ] + [ run array3.cpp ] + [ run array4.cpp ] + [ run array5.cpp ] + ; diff --git a/test/array0.cpp b/test/array0.cpp new file mode 100644 index 0000000..065256b --- /dev/null +++ b/test/array0.cpp @@ -0,0 +1,107 @@ +/* tests for using class array<> specialization for size 0 + * (C) Copyright Alisdair Meredith 2006. + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ + +#include +#include +#include + +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::out_of_range & ) { + } + + try { + BadValue( const_test_case.at( 0 ) ); + } catch ( const std::out_of_range & ) { + } +} + +} + +int main() +{ + RunTests< bool >(); + RunTests< void * >(); + RunTests< long double >(); + RunTests< std::string >(); + return failed_tests; +} + diff --git a/test/array1.cpp b/test/array1.cpp new file mode 100644 index 0000000..740968f --- /dev/null +++ b/test/array1.cpp @@ -0,0 +1,58 @@ +/* simple example for using class array<> + * + * (C) Copyright Nicolai M. Josuttis 2001. + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + * + * Changelog: + * 20 Jan 2001 - Removed boolalpha use since stock GCC doesn't support it + * (David Abrahams) + */ + +#include +#include + +int main() +{ + // define special type name + typedef boost::array Array; + + // create and initialize an array + Array a = { { 42 } }; + + // access elements + for (unsigned i=1; i + * (C) Copyright Nicolai M. Josuttis 2001. + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ + +#include +#include +#include +#include "print.hpp" +using namespace std; +using namespace boost; + +int main() +{ + // create and initialize array + array a = { { 1, 2, 3, 4, 5 } }; + + print_elements(a); + + // modify elements directly + for (unsigned i=0; i()); // operation + print_elements(a); + + return 0; // makes Visual-C++ compiler happy +} + diff --git a/test/array3.cpp b/test/array3.cpp new file mode 100644 index 0000000..a4eac5d --- /dev/null +++ b/test/array3.cpp @@ -0,0 +1,55 @@ +/* example for using class array<> + * (C) Copyright Nicolai M. Josuttis 2001. + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ + +#include +#include +#include + +template +void print_elements (const T& x); + +int main() +{ + // create array of four seasons + boost::array seasons = { + { "spring", "summer", "autumn", "winter" } + }; + + // copy and change order + boost::array seasons_orig = seasons; + for (unsigned i=seasons.size()-1; i>0; --i) { + std::swap(seasons.at(i),seasons.at((i+1)%seasons.size())); + } + + std::cout << "one way: "; + print_elements(seasons); + + // try swap() + std::cout << "other way: "; + std::swap(seasons,seasons_orig); + print_elements(seasons); + + // try reverse iterators + std::cout << "reverse: "; + for (boost::array::reverse_iterator pos + =seasons.rbegin(); pos +void print_elements (const T& x) +{ + for (unsigned i=0; i + * (C) Copyright Nicolai M. Josuttis 2001. + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ + +#include +#include +#include +#include +#include + +int main() +{ + // array of arrays of seasons + boost::array,2> seasons_i18n = { + { { { "spring", "summer", "autumn", "winter", } }, + { { "Fruehling", "Sommer", "Herbst", "Winter" } } + } + }; + + // for any array of seasons print seasons + for (unsigned i=0; i seasons = seasons_i18n[i]; + for (unsigned j=0; j + * (C) Copyright Nicolai M. Josuttis 2001. + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ + +#include +#include + +template +void test_static_size (const T& cont) +{ + int tmp[T::static_size]; + for (unsigned i=0; i Array; + + // create and initialize an array + const Array a = { { 42.42 } }; + + // use some common STL container operations + std::cout << "static_size: " << a.size() << std::endl; + std::cout << "size: " << a.size() << std::endl; + // Can't use std::boolalpha because it isn't portable + std::cout << "empty: " << (a.empty()? "true" : "false") << std::endl; + std::cout << "max_size: " << a.max_size() << std::endl; + std::cout << "front: " << a.front() << std::endl; + std::cout << "back: " << a.back() << std::endl; + std::cout << "[0]: " << a[0] << std::endl; + std::cout << "elems: "; + + // iterate through all elements + for (Array::const_iterator pos=a.begin(); pos DArray; + typedef boost::array IArray; + IArray ia = { { 1, 2, 3, 4, 5, 6 } } ; // extra braces silence GCC warning + DArray da; + da = ia; + da.assign(42); + + return 0; // makes Visual-C++ compiler happy +} + diff --git a/test/print.hpp b/test/print.hpp new file mode 100644 index 0000000..6d68e8e --- /dev/null +++ b/test/print.hpp @@ -0,0 +1,27 @@ +/* The following code example is taken from the book + * "The C++ Standard Library - A Tutorial and Reference" + * by Nicolai M. Josuttis, Addison-Wesley, 1999 + * + * (C) Copyright Nicolai M. Josuttis 1999. + * Distributed under the Boost Software License, Version 1.0. (See + * accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + */ +#include + +/* print_elements() + * - prints optional C-string optcstr followed by + * - all elements of the collection coll + * - separated by spaces + */ +template +inline void print_elements (const T& coll, const char* optcstr="") +{ + typename T::const_iterator pos; + + std::cout << optcstr; + for (pos=coll.begin(); pos!=coll.end(); ++pos) { + std::cout << *pos << ' '; + } + std::cout << std::endl; +}