<HTML> <HEAD> <TITLE>The parameterized free function based test case</TITLE> <LINK rel="stylesheet" type="text/css" href="../../../../style/btl.css" media="screen"> <LINK rel="stylesheet" type="text/css" href="../../../../style/btl-print.css" media="print"> <META http-equiv="Content-Language" content="en-us"> <META http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </HEAD> <BODY> <DIV class="header"> <A href="../../../../index.html">Boost.Test</A> > <A href="../../../index.html">Components</A> > <A href="../../index.html">The Unit Test Framework</A> > <A href="../index.html">Components</A> > <A href="index.html">The Test Case</A> > <SPAN class="current_article">The parameterized function test case</SPAN> </DIV> <DIV class="body"> <IMG src='../../../../btl1.gif' width='252' height='43' alt="Boost Test logo"> <H1 class="subtitle">The parameterized free function based test case</H1> <H5>Definition</H5> <P class="first-line-indented"> defined in <A href="../../../../../../../boost/test/unit_test_suite.hpp">unit_test_suite.hpp</A> <H5>Synopsis</H5> <PRE class="code"><SPAN class="reserv-word">template</SPAN> <<SPAN class="reserv-word">typename</SPAN> ParamIterator, <SPAN class="reserv-word">typename</SPAN> ParameterType> <SPAN class="reserv-word">class</SPAN> <SPAN class="new-term">parametrized_function_test_case</SPAN> : <SPAN class="reserv-word">public</SPAN> test_case { ... <SPAN class="comment">// Implementation</SPAN> };</PRE> <H5>Description</H5> <P class="first-line-indented">Instances of the class parametrized_function_test_case are created by the framework for the supplied user's free function with the following specification: void (*fct)( ParameterType ). <H5>Construction</H5> <P class="first-line-indented">To create an instance of the class parametrized_function_test_case use the following macro:</P> <P><SPAN class="new-term">BOOST_PARAM_TEST_CASE</SPAN>( free_function_address, first_parameter, last_parameter ).</P> <P class="first-line-indented">The first macro parameter is an address of free function - body of the test case. The first_parameter and last_parameter are begin and end iterators for the list of parameters accordingly. BOOST_PARAM_TEST_CASE creates a new instance of the class parametrized_function_test_case and returns a pointer to the abstract class test_case. In most cases you will use it as an argument to the method <A href="../test_suite/index.html">test_suite</A>::add(...). Be aware that the parametrized_function_test_case does not store list of parameters internally. The user should make sure that parameters list will not be destroyed until the test case is run. That's why it not recommended to create a parameters list as local variable in <A href="../../index.html">init_unit_test_suite</A>. A simple way to handle a parameters list lifetime is to place it into a user defined test suite class. <H5>Example</H5> <PRE class="code"><SPAN class="cpp-type">void</SPAN> test_file_reader( <SPAN class="cpp-type">std::string</SPAN> <SPAN class="reserv-word">const</SPAN>& file_name ) { ... } <SPAN class="reserv-word">struct</SPAN> reader_testing : <SPAN class="reserv-word">public</SPAN> boost::unit_test_framework::test_suite { reader_testing() { files_to_test.push_back( <SPAN class="literal">"file 1"</SPAN> ); ... files_to_test.push_back( <SPAN class="literal">"file N"</SPAN> ); add( BOOST_PARAM_TEST_CASE( &test_file_reader, files_to_test.begin(), files_to_test.end() ); } <SPAN class="cpp-type">std::list</SPAN><<SPAN class="cpp-type">std::string</SPAN>> files_to_test; }; ... ts->add( <SPAN class="reserv-word">new</SPAN> reader_testing );</PRE> </DIV> <DIV class="footer"> <DIV class="footer-body"> <P> © <A name="Copyright">Copyright</A> <A href='mailto:rogeeff@fusemail.com'>Gennadiy Rozental</A> 2001-2004. <BR> Use, modification, and distribution are subject to the Boost Software License, Version 1.0. (See accompanying file <A href="../../../../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</A> or copy at <A href="http://www.boost.org/LICENSE_1_0.txt">www.boost.org/LICENSE_1_0.txt</A>)</P> <P>Revised: <!-- #BeginDate format:Sw1 -->6 January, 2004<!-- #EndDate --> </P> </DIV> </DIV> </BODY> </HTML>