]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // (C) Copyright Gennadiy Rozental 2003-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 : tests function template test case | |
13 | // *************************************************************************** | |
14 | ||
15 | // Boost.Test | |
16 | #define BOOST_TEST_MAIN | |
17 | #include <boost/test/unit_test.hpp> | |
18 | #include <boost/test/unit_test_log.hpp> | |
19 | #include <boost/test/results_collector.hpp> | |
20 | #include <boost/test/utils/nullstream.hpp> | |
21 | typedef boost::onullstream onullstream_type; | |
22 | ||
23 | // BOOST | |
24 | #include <boost/mpl/range_c.hpp> | |
25 | #include <boost/mpl/list_c.hpp> | |
92f5a8d4 | 26 | #include <boost/mpl/vector.hpp> |
7c673cae FG |
27 | #include <boost/scoped_ptr.hpp> |
28 | ||
92f5a8d4 TL |
29 | #include <boost/config.hpp> |
30 | ||
7c673cae FG |
31 | namespace ut = boost::unit_test; |
32 | namespace mpl = boost::mpl; | |
33 | ||
34 | // STL | |
35 | #include <iostream> | |
36 | ||
11fdf7f2 TL |
37 | struct logger_guard { |
38 | logger_guard(std::ostream& s_out) { | |
39 | ut::unit_test_log.set_stream( s_out ); | |
40 | } | |
41 | ~logger_guard() { | |
42 | ut::unit_test_log.set_stream( std::cout ); | |
43 | } | |
44 | }; | |
45 | ||
7c673cae FG |
46 | //____________________________________________________________________________// |
47 | ||
48 | BOOST_TEST_CASE_TEMPLATE_FUNCTION( test0, Number ) | |
49 | { | |
50 | BOOST_TEST( 2 == (int)Number::value ); | |
51 | } | |
52 | ||
53 | //____________________________________________________________________________// | |
54 | ||
55 | BOOST_TEST_CASE_TEMPLATE_FUNCTION( test1, Number ) | |
56 | { | |
57 | BOOST_TEST( 6 == (int)Number::value ); | |
58 | BOOST_TEST_REQUIRE( 2 <= (int)Number::value ); | |
59 | BOOST_TEST( 3 == (int)Number::value ); | |
60 | } | |
61 | ||
62 | //____________________________________________________________________________// | |
63 | ||
64 | BOOST_TEST_CASE_TEMPLATE_FUNCTION( test2, Number ) | |
65 | { | |
66 | throw Number(); | |
67 | } | |
68 | ||
69 | //____________________________________________________________________________// | |
70 | ||
71 | BOOST_AUTO_TEST_CASE( test0_only_2 ) | |
72 | { | |
11fdf7f2 TL |
73 | // if an exception is thrown in the test, this object is destructed when we reach the logger |
74 | // for logging the exception. This happens for instance if the test->add throws: | |
75 | // - test case aborted, null_output destructed but still refered from the logger | |
76 | // - exception caught by the framework, and exception content logged | |
77 | // - reference to a non-existing log stream | |
7c673cae | 78 | onullstream_type null_output; |
11fdf7f2 | 79 | logger_guard G( null_output ); |
7c673cae | 80 | |
11fdf7f2 | 81 | typedef boost::mpl::list_c<int,2> only_2; |
7c673cae FG |
82 | ut::test_suite* test = BOOST_TEST_SUITE( "" ); |
83 | ||
84 | test->add( BOOST_TEST_CASE_TEMPLATE( test0, only_2 ) ); | |
85 | ||
86 | test->p_default_status.value = ut::test_unit::RS_ENABLED; | |
87 | ut::framework::finalize_setup_phase( test->p_id ); | |
88 | ut::framework::run( test ); | |
89 | ut::test_results const& tr = ut::results_collector.results( test->p_id ); | |
90 | ||
91 | ut::unit_test_log.set_stream( std::cout ); | |
92 | BOOST_TEST( tr.p_assertions_failed == 0U ); | |
93 | BOOST_TEST( !tr.p_aborted ); | |
94 | } | |
95 | ||
96 | //____________________________________________________________________________// | |
97 | ||
98 | BOOST_AUTO_TEST_CASE( test0_one_to_ten ) | |
99 | { | |
100 | onullstream_type null_output; | |
11fdf7f2 TL |
101 | logger_guard G( null_output ); |
102 | ||
7c673cae FG |
103 | ut::test_suite* test = BOOST_TEST_SUITE( "" ); |
104 | ||
105 | typedef boost::mpl::range_c<int,0,10> one_to_ten; | |
7c673cae FG |
106 | |
107 | test->add( BOOST_TEST_CASE_TEMPLATE( test0, one_to_ten ) ); | |
108 | ||
109 | test->p_default_status.value = ut::test_unit::RS_ENABLED; | |
110 | ut::framework::finalize_setup_phase( test->p_id ); | |
111 | ut::framework::run( test ); | |
112 | ut::test_results const& tr = ut::results_collector.results( test->p_id ); | |
113 | ||
114 | ut::unit_test_log.set_stream( std::cout ); | |
115 | BOOST_TEST( tr.p_assertions_failed == 9U ); | |
116 | BOOST_TEST( !tr.p_aborted ); | |
117 | ||
118 | } | |
119 | ||
120 | //____________________________________________________________________________// | |
121 | ||
122 | BOOST_AUTO_TEST_CASE( test1_one_to_five ) | |
123 | { | |
124 | onullstream_type null_output; | |
11fdf7f2 TL |
125 | logger_guard G( null_output ); |
126 | ||
7c673cae FG |
127 | ut::test_suite* test = BOOST_TEST_SUITE( "" ); |
128 | ||
129 | typedef boost::mpl::range_c<int,1,5> one_to_five; | |
7c673cae FG |
130 | test->add( BOOST_TEST_CASE_TEMPLATE( test1, one_to_five ) ); |
131 | ||
132 | test->p_default_status.value = ut::test_unit::RS_ENABLED; | |
133 | ut::framework::finalize_setup_phase( test->p_id ); | |
134 | ut::framework::run( test ); | |
135 | ut::test_results const& tr = ut::results_collector.results( test->p_id ); | |
136 | ||
137 | ut::unit_test_log.set_stream( std::cout ); | |
138 | BOOST_TEST( tr.p_assertions_failed == 7U ); | |
139 | BOOST_TEST( !tr.p_aborted ); | |
140 | } | |
141 | ||
142 | //____________________________________________________________________________// | |
143 | ||
144 | BOOST_AUTO_TEST_CASE( test2_one_to_three ) | |
145 | { | |
146 | onullstream_type null_output; | |
11fdf7f2 | 147 | logger_guard G( null_output ); |
7c673cae FG |
148 | ut::test_suite* test = BOOST_TEST_SUITE( "" ); |
149 | ||
150 | typedef boost::mpl::range_c<int,1,3> one_to_three; | |
7c673cae FG |
151 | test->add( BOOST_TEST_CASE_TEMPLATE( test2, one_to_three ) ); |
152 | ||
153 | test->p_default_status.value = ut::test_unit::RS_ENABLED; | |
154 | ut::framework::finalize_setup_phase( test->p_id ); | |
155 | ut::framework::run( test ); | |
156 | ut::test_results const& tr = ut::results_collector.results( test->p_id ); | |
157 | ||
158 | ut::unit_test_log.set_stream( std::cout ); | |
159 | BOOST_TEST( tr.p_assertions_failed == 2U ); | |
160 | BOOST_TEST( !tr.p_aborted ); | |
161 | BOOST_TEST( !tr.passed() ); | |
162 | } | |
163 | ||
164 | //____________________________________________________________________________// | |
165 | ||
92f5a8d4 TL |
166 | // checks if volatile, const, ... are properly handled |
167 | typedef boost::mpl::vector<int,int const, int volatile,int const volatile> test_types_ints_variations; | |
168 | BOOST_AUTO_TEST_CASE_TEMPLATE( tctempl2, T, test_types_ints_variations ) | |
169 | { | |
170 | BOOST_TEST( sizeof(T) == sizeof(int) ); | |
171 | } | |
172 | ||
173 | // checks if references are properly handled | |
174 | typedef boost::mpl::vector< | |
175 | int | |
176 | , int& | |
177 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES | |
178 | , int&& | |
179 | #endif | |
180 | , int const | |
181 | , int const& | |
182 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES | |
183 | , int const&& | |
184 | #endif | |
185 | > test_types_ints_ref_variations; | |
186 | BOOST_AUTO_TEST_CASE_TEMPLATE( tctempl3, T, test_types_ints_ref_variations ) | |
187 | { | |
188 | BOOST_TEST( (sizeof(T) == sizeof(int&) || sizeof(T) == sizeof(int)) ); | |
189 | } | |
190 | ||
191 | // checks if pointers are properly handled | |
192 | typedef boost::mpl::vector<int,int*,int const*, int const volatile*, int const*const, int*&> test_types_ints_pointer_variations; | |
193 | BOOST_AUTO_TEST_CASE_TEMPLATE( tctempl4, T, test_types_ints_pointer_variations ) | |
194 | { | |
195 | BOOST_TEST( (sizeof(T) == sizeof(int*) || sizeof(T) == sizeof(int))); | |
196 | } | |
197 | ||
198 | ||
7c673cae | 199 | // EOF |