#include #include #include #include #include // a toy source implementation that reads only one byte every time read is called class read_one_source_t { public: typedef char char_type; typedef boost::iostreams::source_tag category; read_one_source_t() : pos_m(0) { // generate data that we can inspect later std::copy(boost::counting_iterator(0), boost::counting_iterator(100), data_m); } std::streamsize read(char* s, std::streamsize n) { // bounds checking if (pos_m >= 100 || n <= 0) return -1; // write a single char *s = data_m[pos_m++]; // always return 1 return 1; } private: std::size_t pos_m; char data_m[100]; }; void nonblocking_read_test() { read_one_source_t src; boost::iostreams::non_blocking_adapter nb(src); char read_data[100]; std::streamsize amt = boost::iostreams::read(nb, read_data, 100); BOOST_CHECK_EQUAL(amt, 100); for (int i = 0; i < 100; ++i) { BOOST_CHECK_EQUAL(std::char_traits::to_int_type(read_data[i]), i); } } boost::unit_test::test_suite* init_unit_test_suite(int, char* []) { boost::unit_test::test_suite* test = BOOST_TEST_SUITE("non-blocking read test"); test->add(BOOST_TEST_CASE(&nonblocking_read_test)); return test; }