forked from boostorg/array
Compare commits
2 Commits
develop
...
svn-branch
Author | SHA1 | Date | |
---|---|---|---|
fab09b5f94 | |||
522292bd56 |
107
array0.cpp
Normal file
107
array0.cpp
Normal file
@ -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 <string>
|
||||
#include <iostream>
|
||||
#include <boost/array.hpp>
|
||||
|
||||
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;
|
||||
}
|
||||
|
58
array1.cpp
Normal file
58
array1.cpp
Normal file
@ -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 <iostream>
|
||||
#include <boost/array.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
// define special type name
|
||||
typedef boost::array<float,6> Array;
|
||||
|
||||
// create and initialize an array
|
||||
Array a = { { 42 } };
|
||||
|
||||
// access elements
|
||||
for (unsigned i=1; i<a.size(); ++i) {
|
||||
a[i] = a[i-1]+1;
|
||||
}
|
||||
|
||||
// use some common STL container operations
|
||||
std::cout << "size: " << a.size() << std::endl;
|
||||
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 << "elems: ";
|
||||
|
||||
// iterate through all elements
|
||||
for (Array::const_iterator pos=a.begin(); pos<a.end(); ++pos) {
|
||||
std::cout << *pos << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
// check copy constructor and assignment operator
|
||||
Array b(a);
|
||||
Array c;
|
||||
c = a;
|
||||
if (a==b && a==c) {
|
||||
std::cout << "copy construction and copy assignment are OK"
|
||||
<< std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << "copy construction and copy assignment FAILED"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
return 0; // makes Visual-C++ compiler happy
|
||||
}
|
||||
|
40
array2.cpp
Normal file
40
array2.cpp
Normal file
@ -0,0 +1,40 @@
|
||||
/* 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 <algorithm>
|
||||
#include <functional>
|
||||
#include <boost/array.hpp>
|
||||
#include "print.hpp"
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
// create and initialize array
|
||||
array<int,10> a = { { 1, 2, 3, 4, 5 } };
|
||||
|
||||
print_elements(a);
|
||||
|
||||
// modify elements directly
|
||||
for (unsigned i=0; i<a.size(); ++i) {
|
||||
++a[i];
|
||||
}
|
||||
print_elements(a);
|
||||
|
||||
// change order using an STL algorithm
|
||||
reverse(a.begin(),a.end());
|
||||
print_elements(a);
|
||||
|
||||
// negate elements using STL framework
|
||||
transform(a.begin(),a.end(), // source
|
||||
a.begin(), // destination
|
||||
negate<int>()); // operation
|
||||
print_elements(a);
|
||||
|
||||
return 0; // makes Visual-C++ compiler happy
|
||||
}
|
||||
|
55
array3.cpp
Normal file
55
array3.cpp
Normal file
@ -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 <string>
|
||||
#include <iostream>
|
||||
#include <boost/array.hpp>
|
||||
|
||||
template <class T>
|
||||
void print_elements (const T& x);
|
||||
|
||||
int main()
|
||||
{
|
||||
// create array of four seasons
|
||||
boost::array<std::string,4> seasons = {
|
||||
{ "spring", "summer", "autumn", "winter" }
|
||||
};
|
||||
|
||||
// copy and change order
|
||||
boost::array<std::string,4> 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<std::string,4>::reverse_iterator pos
|
||||
=seasons.rbegin(); pos<seasons.rend(); ++pos) {
|
||||
std::cout << " " << *pos;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
return 0; // makes Visual-C++ compiler happy
|
||||
}
|
||||
|
||||
template <class T>
|
||||
void print_elements (const T& x)
|
||||
{
|
||||
for (unsigned i=0; i<x.size(); ++i) {
|
||||
std::cout << " " << x[i];
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
43
array4.cpp
Normal file
43
array4.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
/* 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 <algorithm>
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <boost/array.hpp>
|
||||
|
||||
int main()
|
||||
{
|
||||
// array of arrays of seasons
|
||||
boost::array<boost::array<std::string,4>,2> seasons_i18n = {
|
||||
{ { { "spring", "summer", "autumn", "winter", } },
|
||||
{ { "Fruehling", "Sommer", "Herbst", "Winter" } }
|
||||
}
|
||||
};
|
||||
|
||||
// for any array of seasons print seasons
|
||||
for (unsigned i=0; i<seasons_i18n.size(); ++i) {
|
||||
boost::array<std::string,4> seasons = seasons_i18n[i];
|
||||
for (unsigned j=0; j<seasons.size(); ++j) {
|
||||
std::cout << seasons[j] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
// print first element of first array
|
||||
std::cout << "first element of first array: "
|
||||
<< seasons_i18n[0][0] << std::endl;
|
||||
|
||||
// print last element of last array
|
||||
std::cout << "last element of last array: "
|
||||
<< seasons_i18n[seasons_i18n.size()-1][seasons_i18n[0].size()-1]
|
||||
<< std::endl;
|
||||
|
||||
return 0; // makes Visual-C++ compiler happy
|
||||
}
|
||||
|
72
array5.cpp
Normal file
72
array5.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
/* 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)
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <boost/array.hpp>
|
||||
|
||||
template <typename T>
|
||||
void test_static_size (const T& cont)
|
||||
{
|
||||
int tmp[T::static_size];
|
||||
for (unsigned i=0; i<T::static_size; ++i) {
|
||||
tmp[i] = int(cont[i]);
|
||||
}
|
||||
for (unsigned j=0; j<T::static_size; ++j) {
|
||||
std::cout << tmp[j] << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// define special type name
|
||||
typedef boost::array<float,6> 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<a.end(); ++pos) {
|
||||
std::cout << *pos << ' ';
|
||||
}
|
||||
std::cout << std::endl;
|
||||
test_static_size(a);
|
||||
|
||||
// check copy constructor and assignment operator
|
||||
Array b(a);
|
||||
Array c;
|
||||
c = a;
|
||||
if (a==b && a==c) {
|
||||
std::cout << "copy construction and copy assignment are OK"
|
||||
<< std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << "copy construction and copy assignment are BROKEN"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
typedef boost::array<double,6> DArray;
|
||||
typedef boost::array<int,6> 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
|
||||
}
|
||||
|
27
print.hpp
Normal file
27
print.hpp
Normal file
@ -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 <iostream>
|
||||
|
||||
/* print_elements()
|
||||
* - prints optional C-string optcstr followed by
|
||||
* - all elements of the collection coll
|
||||
* - separated by spaces
|
||||
*/
|
||||
template <class T>
|
||||
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;
|
||||
}
|
Reference in New Issue
Block a user