runtime-configuration_4.run-fail.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_MODULE runtime_configuration4
  8. #include <boost/test/included/unit_test.hpp>
  9. #include <boost/test/data/test_case.hpp>
  10. #include <iostream>
  11. #include <functional>
  12. #include <sstream>
  13. #include <fstream>
  14. // this dataset loads a file that contains a list of strings
  15. // this list is used to create a dataset test case.
  16. class file_dataset
  17. {
  18. private:
  19. std::string m_filename;
  20. std::size_t m_line_start;
  21. std::size_t m_line_end;
  22. public:
  23. enum { arity = 2 };
  24. public:
  25. file_dataset(std::size_t line_start = 0, std::size_t line_end = std::size_t(-1))
  26. : m_line_start(line_start)
  27. , m_line_end(line_end)
  28. {
  29. int argc = boost::unit_test::framework::master_test_suite().argc;
  30. char** argv = boost::unit_test::framework::master_test_suite().argv;
  31. if(argc != 3)
  32. throw std::logic_error("Incorrect number of arguments");
  33. if(std::string(argv[1]) != "--test-file")
  34. throw std::logic_error("First argument != '--test-file'");
  35. if(!(m_line_start < std::size_t(-1)))
  36. throw std::logic_error("Incorrect line start/end");
  37. m_filename = argv[2];
  38. std::ifstream file(m_filename);
  39. if(!file.is_open())
  40. throw std::logic_error("Cannot open the file '" + m_filename + "'");
  41. std::size_t nb_lines = std::count_if(
  42. std::istreambuf_iterator<char>(file),
  43. std::istreambuf_iterator<char>(),
  44. [](char c){ return c == '\n';});
  45. m_line_end = (std::min)(nb_lines, m_line_end);
  46. if(!(m_line_start <= m_line_end))
  47. throw std::logic_error("Incorrect line start/end");
  48. }
  49. struct iterator {
  50. iterator(std::string const& filename, std::size_t line_start)
  51. : file(filename, std::ios::binary) {
  52. if(!file.is_open())
  53. throw std::runtime_error("Cannot open the file");
  54. for(std::size_t i = 0; i < line_start; i++) {
  55. getline(file, m_current_line);
  56. }
  57. }
  58. auto operator*() const -> std::tuple<float, float> {
  59. float a, b;
  60. std::istringstream istr(m_current_line);
  61. istr >> a >> b;
  62. return std::tuple<float, float>(a, b);
  63. }
  64. void operator++() {
  65. getline(file, m_current_line);
  66. }
  67. private:
  68. std::ifstream file;
  69. std::string m_current_line;
  70. };
  71. // size of the DS
  72. boost::unit_test::data::size_t size() const {
  73. return m_line_end - m_line_start;
  74. }
  75. // iterator over the lines of the file
  76. iterator begin() const {
  77. return iterator(m_filename, m_line_start);
  78. }
  79. };
  80. namespace boost { namespace unit_test { namespace data {
  81. namespace monomorphic {
  82. template <>
  83. struct is_dataset<file_dataset> : boost::mpl::true_ {};
  84. }
  85. }}}
  86. BOOST_DATA_TEST_CASE(command_line_test_file,
  87. boost::unit_test::data::make_delayed<file_dataset>( 3, 10 ),
  88. input, expected) {
  89. BOOST_TEST(input <= expected);
  90. }
  91. //]