• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  (C) Copyright Gennadiy Rozental 2001-2015.
2 //  Distributed under the Boost Software License, Version 1.0.
3 //  (See accompanying file LICENSE_1_0.txt or copy at
4 //  http://www.boost.org/LICENSE_1_0.txt)
5 
6 //  See http://www.boost.org/libs/test for the library home page.
7 //
8 //  File        : $RCSfile$
9 //
10 //  Version     : $Revision$
11 //
12 //  Description : BOOST_TEST_FOREACH compile only test
13 // *****************************************************************************
14 
15 // STL
16 #include <iostream>
17 #include <list>
18 #include <vector>
19 #include <string>
20 
21 #include <boost/test/utils/foreach.hpp>
22 
23 #ifdef BOOST_MSVC
24 #pragma warning(disable:4702) // Unreachable code
25 #endif
26 
27 template<class T>
baz(std::list<T> const & list_of_T)28 void baz( std::list<T>const& list_of_T )
29 {
30     // an example of using BOOST_TEST_FOREACH with dependent types
31     BOOST_TEST_FOREACH( T const&, t, list_of_T )
32     {
33         std::cout << t << std::endl;
34     }
35 }
36 
main()37 int main()
38 {
39     std::list<int> int_list;
40     int_list.push_back( 1 );
41     int_list.push_back( 2 );
42     int_list.push_back( 3 );
43 
44     // use BOOST_TEST_FOREACH with a STL container, and a reference as the loop variable.
45     BOOST_TEST_FOREACH( int&, i, int_list )
46     {
47         ++i;
48         std::cout << i << std::endl;
49     }
50 
51     std::cout << std::endl;
52 
53     // use BOOST_TEST_FOREACH with a std::vector
54     std::vector<int> int_vec;
55     int_vec.push_back( 1 );
56     int_vec.push_back( 2 );
57     int_vec.push_back( 3 );
58     int_vec.push_back( 3 );
59     int_vec.push_back( 3 );
60 
61     BOOST_TEST_FOREACH( int const&, i, int_vec )
62     {
63         std::cout << i << std::endl;
64         if( i == 3 )
65             break;
66     }
67 
68     std::cout << std::endl;
69 
70     // use BOOST_TEST_FOREACH with dependent types
71     baz( int_list );
72     std::cout << std::endl;
73 
74     // iterate over characters in a std::string
75     std::string str( "hello" );
76 
77     BOOST_TEST_FOREACH( char&, ch, str )
78     {
79         std::cout << ch;
80         // mutate the string
81         ++ch;
82     }
83     std::cout << std::endl;
84     std::cout << std::endl;
85 
86     BOOST_TEST_FOREACH( char, ch, str )
87     {
88         // break work as you would expect
89         std::cout << ch;
90         break;
91     }
92     std::cout << std::endl;
93     std::cout << std::endl;
94 
95     BOOST_TEST_FOREACH( char, ch, str )
96     {
97         // continue work as you would expect
98         if( ch == 'm' )
99             continue;
100 
101         std::cout << ch;
102     }
103     std::cout << std::endl;
104     std::cout << std::endl;
105 
106     // use BOOST_TEST_FOREACH with const reference.
107     std::vector<int> const& int_vec_const_ref = int_vec;
108 
109     BOOST_TEST_FOREACH( int const&, i, int_vec_const_ref )
110     {
111         std::cout << (i+1) << std::endl;
112     }
113     std::cout << std::endl;
114 
115     return 0;
116 }
117