runtime-configuration_3.run-fail.cpp 1.7 KB

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