counter_test.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2004-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. #include <cctype>
  7. #include <boost/iostreams/copy.hpp>
  8. #include <boost/iostreams/device/file.hpp>
  9. #include <boost/iostreams/device/null.hpp>
  10. #include <boost/iostreams/filter/counter.hpp>
  11. #include <boost/iostreams/filtering_stream.hpp>
  12. #include <boost/test/test_tools.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include "detail/constants.hpp"
  15. #include "detail/filters.hpp"
  16. #include "detail/temp_file.hpp"
  17. #include "detail/verification.hpp"
  18. using namespace std;
  19. using namespace boost;
  20. using namespace boost::iostreams;
  21. using namespace boost::iostreams::test;
  22. using boost::unit_test::test_suite;
  23. void read_counter()
  24. {
  25. test_file src;
  26. filtering_istream in;
  27. in.push(counter());
  28. in.push(padding_filter('a'), 0);
  29. in.push(counter());
  30. in.push(file_source(src.name(), in_mode));
  31. counter* first_counter = BOOST_IOSTREAMS_COMPONENT(in, 0, counter);
  32. counter* second_counter = BOOST_IOSTREAMS_COMPONENT(in, 2, counter);
  33. int first_count = 0;
  34. int second_count = 0;
  35. int line_count = 0;
  36. int reps = data_reps < 50 ? data_reps : 25; // Keep test short.
  37. for (int w = 0; w < reps; ++w) {
  38. int len = data_length();
  39. for (int z = 0; z < len; ++z) {
  40. in.get();
  41. ++first_count;
  42. ++second_count;
  43. BOOST_CHECK(first_counter->characters() == first_count);
  44. BOOST_CHECK(second_counter->characters() == second_count);
  45. in.get();
  46. ++first_count;
  47. BOOST_CHECK(first_counter->characters() == first_count);
  48. BOOST_CHECK(second_counter->characters() == second_count);
  49. }
  50. ++line_count;
  51. BOOST_CHECK(first_counter->lines() == line_count);
  52. BOOST_CHECK(first_counter->lines() == line_count);
  53. }
  54. }
  55. void write_counter()
  56. {
  57. filtering_ostream out;
  58. out.push(counter());
  59. out.push(padding_filter('a'), 0);
  60. out.push(counter());
  61. out.push(null_sink());
  62. counter* first_counter = BOOST_IOSTREAMS_COMPONENT(out, 0, counter);
  63. counter* second_counter = BOOST_IOSTREAMS_COMPONENT(out, 2, counter);
  64. int first_count = 0;
  65. int second_count = 0;
  66. int line_count = 0;
  67. int reps = data_reps < 50 ? data_reps : 25; // Keep test short.
  68. const char* data = narrow_data();
  69. for (int w = 0; w < reps; ++w) {
  70. int len = data_length();
  71. for (int z = 0; z < len; ++z) {
  72. out.put(data[z]);
  73. out.flush();
  74. ++first_count;
  75. second_count += 2;
  76. BOOST_CHECK(first_counter->characters() == first_count);
  77. BOOST_CHECK(second_counter->characters() == second_count);
  78. }
  79. ++line_count;
  80. BOOST_CHECK(first_counter->lines() == line_count);
  81. BOOST_CHECK(first_counter->lines() == line_count);
  82. }
  83. }
  84. test_suite* init_unit_test_suite(int, char* [])
  85. {
  86. test_suite* test = BOOST_TEST_SUITE("counter test");
  87. test->add(BOOST_TEST_CASE(&read_counter));
  88. test->add(BOOST_TEST_CASE(&write_counter));
  89. return test;
  90. }