array_test.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <boost/iostreams/detail/fstream.hpp>
  7. #include <boost/iostreams/device/array.hpp>
  8. #include <boost/iostreams/stream.hpp>
  9. #include <boost/test/test_tools.hpp>
  10. #include <boost/test/unit_test.hpp>
  11. #include "detail/sequence.hpp"
  12. #include "detail/temp_file.hpp"
  13. #include "detail/verification.hpp"
  14. using boost::unit_test::test_suite;
  15. void array_test()
  16. {
  17. using namespace std;
  18. using namespace boost::iostreams;
  19. using namespace boost::iostreams::test;
  20. test_file test;
  21. //--------------stream<array_source>-------------------------------//
  22. {
  23. test_sequence<> seq;
  24. stream<array_source> first(&seq[0], &seq[0] + seq.size());
  25. ifstream second(test.name().c_str(), BOOST_IOS::in | BOOST_IOS::binary);
  26. BOOST_CHECK_MESSAGE(
  27. compare_streams_in_chars(first, second),
  28. "failed reading from stream<array_source> in chars"
  29. );
  30. }
  31. {
  32. test_sequence<> seq;
  33. stream<array_source> first(&seq[0], &seq[0] + seq.size());
  34. ifstream second(test.name().c_str(), BOOST_IOS::in | BOOST_IOS::binary);
  35. BOOST_CHECK_MESSAGE(
  36. compare_streams_in_chunks(first, second),
  37. "failed reading from stream<array_source> in chunks"
  38. );
  39. }
  40. //--------------stream<array_sink>---------------------------------//
  41. {
  42. vector<char> first(data_reps * data_length(), '?');
  43. stream<array_sink> out(&first[0], &first[0] + first.size());
  44. write_data_in_chars(out);
  45. ifstream second(test.name().c_str(), BOOST_IOS::in | BOOST_IOS::binary);
  46. BOOST_CHECK_MESSAGE(
  47. compare_container_and_stream(first, second),
  48. "failed writing to stream<array_sink> in chars"
  49. );
  50. }
  51. {
  52. vector<char> first(data_reps * data_length(), '?');
  53. stream<array_sink> out(&first[0], &first[0] + first.size());
  54. write_data_in_chunks(out);
  55. ifstream second(test.name().c_str(), BOOST_IOS::in | BOOST_IOS::binary);
  56. BOOST_CHECK_MESSAGE(
  57. compare_container_and_stream(first, second),
  58. "failed writing to stream<array_sink> in chunks"
  59. );
  60. }
  61. //--------------random access---------------------------------------------//
  62. {
  63. vector<char> first(data_reps * data_length(), '?');
  64. stream<boost::iostreams::array> io(&first[0], &first[0] + first.size());
  65. BOOST_CHECK_MESSAGE(
  66. test_seekable_in_chars(io),
  67. "failed seeking within stream<array>, in chars"
  68. );
  69. }
  70. {
  71. vector<char> first(data_reps * data_length(), '?');
  72. stream<boost::iostreams::array> io(&first[0], &first[0] + first.size());
  73. BOOST_CHECK_MESSAGE(
  74. test_seekable_in_chars(io),
  75. "failed seeking within stream<array>, in chunks"
  76. );
  77. }
  78. }
  79. test_suite* init_unit_test_suite(int, char* [])
  80. {
  81. test_suite* test = BOOST_TEST_SUITE("array test");
  82. test->add(BOOST_TEST_CASE(&array_test));
  83. return test;
  84. }