Boost logo

Boost Users :

Subject: [Boost-users] [IOStreams] Is such a behaviour considered as bug
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2011-08-11 09:22:41


Hello *,

I really enjoyed working with Iostreams, while testing some of my
components. Using IOStreams with array_device seemed to be a very powerfull
approach to test some of my stream based algorithms. But currently I face
some issues which seem to be really weired to me. I produced some code
snippet to better explain it. I used Boost.Test library to verify my
assumptions.

    using namespace boost;
    namespace io=boost::iostreams;

    typedef unsinged char byte;
    typedef array<byte, 2> arr_buffer; //boost::array
    typedef io::basic_array<byte> arr_dev;

    arr_buffer buffer_ = {{}};
    arr_dev dev_(buffer_.begin(), buffer_.end());
    io::stream<arr_dev> ios_(dev_);

    arr_dev::pair_type range = dev_.input_sequence();
    BOOST_MESSAGE("size: " << distance(range.first, range.second)); //
output: size is 2

    BOOST_CHECK_EQUAL(0u, ios_.tellg()); //stream is at position 0 as
expected
    ios_.seekg(1);
    BOOST_CHECK_EQUAL(1u, ios_.tellg()); // stream is at position 1 as
expected
    ios_.seekg(2);
    BOOST_MESSAGE("current pos: " << ios_.tellg()); // !!! stream is at
position 2
    ios_.seekg(3);
    BOOST_MESSAGE("current pos: " << ios_.tellg()); // stream is at position
-1
    BOOST_MESSAGE("eof: " << ios_.eof()); // !!! but eof is false

If the defined stream is only 2 bytes long why is 2 a valid stream position?
Why is eof always false? Do you think this behavior is buggy?

I changed the same behavior to read bytes from stream and created an array
of length 3, but passed the range of 2 bytes to the device ctor. This works
as expected:

    arr_buffer buffer_ = {{'a', 'b', 'c'}};
    arr_dev dev_(buffer_.begin(), buffer_.begin()+2); //range of 2 bytes is
passed
    io::stream<arr_dev> ios_(dev_);

    arr_dev::pair_type range = dev_.input_sequence();
    BOOST_MESSAGE("size: " << distance(range.first, range.second));

    byte bt;

    BOOST_MESSAGE("current pos: " << ios_.tellg()); // prints 0
    BOOST_CHECK_EQUAL(0u, ios_.tellg());
    ios_.read(&bt, 1);
    BOOST_MESSAGE("retrieved: " << bt); // prints 'a'

    BOOST_MESSAGE("current pos: " << ios_.tellg()); //prints 1
    BOOST_CHECK_EQUAL(1u, ios_.tellg());
    ios_.read(&bt, 1);
    BOOST_MESSAGE("retrieved: " << bt); // prints 'b'

    BOOST_MESSAGE("current pos: " << ios_.tellg()); // !!! prints 2, why not
-1???
    ios_.read(&bt, 1);
    BOOST_MESSAGE("retrieved: " << bt); // still prints 'b' => nothing
retrieved
    BOOST_MESSAGE("current pos: " << ios_.tellg()); // !!! now it prints -1
    BOOST_MESSAGE("eof: " << ios_.eof()); // !!! now eof is true

Any ideas why it behaves as described?

With Kind Regards,
Ovanes



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net