1 // Copyright (c) 2019 Raffi Enficiaud 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 //[example_code 9 #define BOOST_TEST_ALTERNATIVE_INIT_API 10 #include <boost/test/included/unit_test.hpp> 11 #include <functional> 12 #include <sstream> 13 14 using namespace boost::unit_test; 15 test_function(int i)16void test_function(int i) { 17 BOOST_TEST(i >= 1); 18 } 19 20 // helper read_integer(const std::string & str)21int read_integer(const std::string &str) { 22 std::istringstream buff( str ); 23 int number = 0; 24 buff >> number; 25 if(buff.fail()) { 26 // it is also possible to raise a boost.test specific exception. 27 throw framework::setup_error("Argument '" + str + "' not integer"); 28 } 29 return number; 30 } 31 init_unit_test()32bool init_unit_test() 33 { 34 int argc = boost::unit_test::framework::master_test_suite().argc; 35 char** argv = boost::unit_test::framework::master_test_suite().argv; 36 37 if( argc <= 1) { 38 return false; // returning false to indicate an error 39 } 40 41 if( std::string(argv[1]) == "--create-parametrized" ) { 42 if(argc < 3) { 43 // the logging availability depends on the logger type 44 BOOST_TEST_MESSAGE("Not enough parameters"); 45 return false; 46 } 47 48 int number_tests = read_integer(argv[2]); 49 int test_start = 0; 50 if(argc > 3) { 51 test_start = read_integer(argv[3]); 52 } 53 54 for(int i = test_start; i < number_tests; i++) { 55 std::ostringstream ostr; 56 ostr << "name " << i; 57 // create test-cases, avoiding duplicate names 58 framework::master_test_suite(). 59 add( BOOST_TEST_CASE_NAME( std::bind(&test_function, i), ostr.str().c_str() ) ); 60 } 61 } 62 return true; 63 } 64 //] 65