windows_pipe_test.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2004-2007 Jonathan Turkanis
  3. // (C) Copyright 2012 Boris Schaeling
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  6. // See http://www.boost.org/libs/iostreams for documentation.
  7. #include <windows.h>
  8. #include <boost/iostreams/device/file_descriptor.hpp>
  9. #include <boost/iostreams/filtering_stream.hpp>
  10. #include <boost/iostreams/get.hpp>
  11. #include <boost/test/test_tools.hpp>
  12. #include <boost/test/unit_test.hpp>
  13. using namespace boost::iostreams;
  14. using boost::unit_test::test_suite;
  15. void read_from_file_descriptor_source_test()
  16. {
  17. HANDLE handles[2];
  18. ::CreatePipe(&handles[0], &handles[1], NULL, 0);
  19. char buffer[2] = { 'a', 'b' };
  20. DWORD written;
  21. ::WriteFile(handles[1], buffer, 2, &written, NULL);
  22. ::CloseHandle(handles[1]);
  23. file_descriptor_source is(handles[0], close_handle);
  24. BOOST_CHECK_EQUAL('a', get(is));
  25. BOOST_CHECK_EQUAL('b', get(is));
  26. BOOST_CHECK_EQUAL(-1, get(is));
  27. BOOST_CHECK_EQUAL(-1, get(is));
  28. }
  29. void read_from_filtering_istream_test()
  30. {
  31. HANDLE handles[2];
  32. ::CreatePipe(&handles[0], &handles[1], NULL, 0);
  33. char buffer[2] = { 'a', 'b' };
  34. DWORD written;
  35. ::WriteFile(handles[1], buffer, 2, &written, NULL);
  36. ::CloseHandle(handles[1]);
  37. file_descriptor_source source(handles[0], close_handle);
  38. filtering_istream is;
  39. is.push(source);
  40. BOOST_CHECK_EQUAL('a', get(is));
  41. BOOST_CHECK_EQUAL('b', get(is));
  42. BOOST_CHECK_EQUAL(-1, get(is));
  43. BOOST_CHECK_EQUAL(-1, get(is));
  44. }
  45. test_suite* init_unit_test_suite(int, char* [])
  46. {
  47. test_suite* test = BOOST_TEST_SUITE("Windows pipe test");
  48. test->add(BOOST_TEST_CASE(&read_from_file_descriptor_source_test));
  49. test->add(BOOST_TEST_CASE(&read_from_filtering_istream_test));
  50. return test;
  51. }