Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions include/boost/iostreams/detail/adapter/range_adapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,26 @@ template<>
struct range_adapter_impl<std::forward_iterator_tag> {
template<typename Iter, typename Ch>
static std::streamsize read
(Iter& cur, Iter& last, Ch* s,std::streamsize n)
(Iter& cur, Iter& last, Ch* s, std::streamsize n)
{
std::streamsize rem = n; // No. of chars remaining.
while (cur != last && rem-- > 0) *s++ = *cur++;
return n - rem != 0 ? n - rem : -1;
std::streamsize count = 0;
while (cur != last && count < n) {
*s++ = *cur++;
++count;
}
return count != 0 ? count : -1;
}

template<typename Iter, typename Ch>
static std::streamsize write
(Iter& cur, Iter& last, const Ch* s, std::streamsize n)
{
while (cur != last && n-- > 0) *cur++ = *s++;
if (cur == last && n > 0)
std::streamsize count = 0;
while (cur != last && count < n) {
*cur++ = *s++;
++count;
}
if (count < n)
boost::throw_exception(write_area_exhausted());
return n;
}
Expand Down
209 changes: 209 additions & 0 deletions test/forward_range_test.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
// (C) Copyright 2026
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)

// See http://www.boost.org/libs/iostreams for documentation.

#ifndef BOOST_IOSTREAMS_TEST_FORWARD_RANGE_HPP_INCLUDED
#define BOOST_IOSTREAMS_TEST_FORWARD_RANGE_HPP_INCLUDED

#include <cstddef>
#include <list>
#include <string>
#include <vector>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/test/test_tools.hpp>

namespace {

struct to_char {
typedef char result_type;
char operator()(int c) const { return static_cast<char>(c); }
};

struct proxy_char_ref {
char* p;
proxy_char_ref(char* p) : p(p) {}
operator char() const { return *p; }
proxy_char_ref& operator=(char c) { *p = c; return *this; }
proxy_char_ref& operator=(const proxy_char_ref& other) { *p = *other.p; return *this; }
};

struct proxy_char_iterator {
typedef std::forward_iterator_tag iterator_category;
typedef char value_type;
typedef std::ptrdiff_t difference_type;
typedef proxy_char_ref* pointer;
typedef proxy_char_ref reference;

char* ptr;

proxy_char_iterator() : ptr(0) {}
explicit proxy_char_iterator(char* p) : ptr(p) {}

reference operator*() const { return proxy_char_ref(ptr); }
proxy_char_iterator& operator++() { ++ptr; return *this; }
proxy_char_iterator operator++(int) {
proxy_char_iterator tmp = *this;
++ptr;
return tmp;
}
bool operator==(const proxy_char_iterator& o) const { return ptr == o.ptr; }
bool operator!=(const proxy_char_iterator& o) const { return ptr != o.ptr; }
};

} // anonymous namespace

void forward_range_test()
{
using namespace std;
using namespace boost;
using namespace boost::iostreams;

// 1. Reading from forward range exceeding stream buffer size (4096)
{
vector<int> input(5000, 0x41);
filtering_istream in(
make_iterator_range(
make_transform_iterator(input.begin(), to_char()),
make_transform_iterator(input.end(), to_char())
)
);
string output((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
BOOST_CHECK_EQUAL(output.size(), 5000);
BOOST_CHECK(output == string(5000, 'A'));
}

// 2. Reading from forward range matching stream buffer size exactly (4096)
{
vector<int> input(4096, 0x42);
filtering_istream in(
make_iterator_range(
make_transform_iterator(input.begin(), to_char()),
make_transform_iterator(input.end(), to_char())
)
);
string output((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
BOOST_CHECK_EQUAL(output.size(), 4096);
BOOST_CHECK(output == string(4096, 'B'));
}

// 3. Reading from forward range smaller than buffer size
{
vector<int> input(100, 0x43);
filtering_istream in(
make_iterator_range(
make_transform_iterator(input.begin(), to_char()),
make_transform_iterator(input.end(), to_char())
)
);
string output((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
BOOST_CHECK_EQUAL(output.size(), 100);
BOOST_CHECK(output == string(100, 'C'));
}

// 4. Reading from empty forward range
{
vector<int> input;
filtering_istream in(
make_iterator_range(
make_transform_iterator(input.begin(), to_char()),
make_transform_iterator(input.end(), to_char())
)
);
string output((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
BOOST_CHECK_EQUAL(output.size(), 0);
}

// 5. Direct multi-buffer reading with in.read() exceeding buffer size
{
vector<int> input(5000, 0x44);
filtering_istream in(
make_iterator_range(
make_transform_iterator(input.begin(), to_char()),
make_transform_iterator(input.end(), to_char())
)
);
vector<char> buf(5000, ' ');
in.read(&buf[0], 5000);
BOOST_CHECK_EQUAL(in.gcount(), 5000);
BOOST_CHECK(string(buf.begin(), buf.end()) == string(5000, 'D'));
}

// 6. Writing to forward range with excess capacity (unbuffered)
{
list<char> storage(10, ' ');
filtering_ostream out(make_iterator_range(storage), 0);
out.write("hello", 5);
BOOST_CHECK(out.good());
string s(storage.begin(), storage.end());
BOOST_CHECK_EQUAL(s, "hello ");
}

// 7. Writing to forward range with exact capacity (unbuffered)
{
list<char> storage(5, ' ');
filtering_ostream out(make_iterator_range(storage), 0);
out.write("hello", 5);
BOOST_CHECK(out.good());
string s(storage.begin(), storage.end());
BOOST_CHECK_EQUAL(s, "hello");
}

// 8. Writing to forward range with insufficient capacity (unbuffered)
{
list<char> storage(4, ' ');
filtering_ostream out(make_iterator_range(storage), 0);
out.exceptions(std::ios_base::badbit);
BOOST_CHECK_THROW(out.write("hello", 5), std::exception);
}

// 9. Writing to forward range exceeding stream buffer size (buffered)
{
list<char> storage(5000, ' ');
string payload(5000, 'Z');
filtering_ostream out(make_iterator_range(storage));
out.write(payload.data(), payload.size());
out.flush();
BOOST_CHECK(out.good());
string s(storage.begin(), storage.end());
BOOST_CHECK_EQUAL(s, payload);
}

// 10. Writing to forward range with insufficient capacity (buffered)
{
list<char> storage(100, ' ');
string payload(5000, 'Z');
filtering_ostream out(make_iterator_range(storage));
out.exceptions(std::ios_base::badbit);
BOOST_CHECK_THROW({
out.write(payload.data(), payload.size());
out.flush();
}, std::exception);
}

// 11. Reading and writing with custom forward iterator returning proxy reference
{
string src = "proxy iterator range adapter test";
vector<char> src_vec(src.begin(), src.end());
proxy_char_iterator rbegin(&src_vec[0]);
proxy_char_iterator rend(&src_vec[0] + src_vec.size());

filtering_istream in(make_iterator_range(rbegin, rend));
string out_str((istreambuf_iterator<char>(in)), istreambuf_iterator<char>());
BOOST_CHECK_EQUAL(out_str, src);

vector<char> dst_vec(src.size(), ' ');
proxy_char_iterator wbegin(&dst_vec[0]);
proxy_char_iterator wend(&dst_vec[0] + dst_vec.size());

filtering_ostream out(make_iterator_range(wbegin, wend), 0);
out.write(&src[0], src.size());
BOOST_CHECK(out.good());
BOOST_CHECK_EQUAL(string(dst_vec.begin(), dst_vec.end()), src);
}
}

#endif // #ifndef BOOST_IOSTREAMS_TEST_FORWARD_RANGE_HPP_INCLUDED
45 changes: 45 additions & 0 deletions test/read_input_seq_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define BOOST_IOSTREAMS_TEST_READ_INPUT_SEQUENCE_HPP_INCLUDED

#include <fstream>
#include <list>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/test/test_tools.hpp>
Expand Down Expand Up @@ -65,6 +66,50 @@ void read_input_sequence_test()
"in chars with large buffer"
);
}

{
list<char> fwd_seq(seq.begin(), seq.end());
filtering_stream<input> first(make_iterator_range(fwd_seq), 0);
ifstream second(file.name().c_str());
BOOST_CHECK_MESSAGE(
compare_streams_in_chars(first, second),
"failed reading from range_adapter based on forward_iterator "
"in chars with no buffer"
);
}

{
list<char> fwd_seq(seq.begin(), seq.end());
filtering_stream<input> first(make_iterator_range(fwd_seq), 0);
ifstream second(file.name().c_str());
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from range_adapter based on forward_iterator "
"in chars with no buffer"
);
}

{
list<char> fwd_seq(seq.begin(), seq.end());
filtering_stream<input> first(make_iterator_range(fwd_seq));
ifstream second(file.name().c_str());
BOOST_CHECK_MESSAGE(
compare_streams_in_chars(first, second),
"failed reading from range_adapter based on forward_iterator "
"in chars with large buffer"
);
}

{
list<char> fwd_seq(seq.begin(), seq.end());
filtering_stream<input> first(make_iterator_range(fwd_seq));
ifstream second(file.name().c_str());
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from range_adapter based on forward_iterator "
"in chars with large buffer"
);
}
}

#endif // #ifndef BOOST_IOSTREAMS_TEST_READ_INPUT_SEQUENCE_HPP_INCLUDED
2 changes: 2 additions & 0 deletions test/sequence_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// See http://www.boost.org/libs/iostreams for documentation.

#include <boost/test/unit_test.hpp>
#include "forward_range_test.hpp"
#include "read_input_seq_test.hpp"
#include "read_seekable_seq_test.hpp"
#include "write_output_seq_test.hpp"
Expand All @@ -20,5 +21,6 @@ test_suite* init_unit_test_suite(int, char* [])
test->add(BOOST_TEST_CASE(&read_seekable_sequence_test));
test->add(BOOST_TEST_CASE(&write_output_sequence_test));
test->add(BOOST_TEST_CASE(&write_seekable_sequence_test));
test->add(BOOST_TEST_CASE(&forward_range_test));
return test;
}
49 changes: 49 additions & 0 deletions test/write_output_seq_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define BOOST_IOSTREAMS_TEST_WRITE_OUTPUT_SEQUENCE_HPP_INCLUDED

#include <fstream>
#include <list>
#include <vector>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/range/iterator_range.hpp>
Expand Down Expand Up @@ -73,6 +74,54 @@ void write_output_sequence_test()
"in chunks with large buffer"
);
}

{
list<char> first(data_reps * data_length(), '?');
filtering_ostream out(make_iterator_range(first), 0);
write_data_in_chars(out);
ifstream second(test.name().c_str());
BOOST_CHECK_MESSAGE(
compare_container_and_stream(first, second),
"failed writing to filtering_ostream based on forward_iterator sequence "
"in chars with no buffer"
);
}

{
list<char> first(data_reps * data_length(), '?');
filtering_ostream out(make_iterator_range(first), 0);
write_data_in_chunks(out);
ifstream second(test.name().c_str());
BOOST_CHECK_MESSAGE(
compare_container_and_stream(first, second),
"failed writing to filtering_ostream based on forward_iterator sequence "
"in chunks with no buffer"
);
}

{
list<char> first(data_reps * data_length(), '?');
filtering_ostream out(make_iterator_range(first));
write_data_in_chars(out);
ifstream second(test.name().c_str());
BOOST_CHECK_MESSAGE(
compare_container_and_stream(first, second),
"failed writing to filtering_ostream based on forward_iterator sequence "
"in chars with large buffer"
);
}

{
list<char> first(data_reps * data_length(), '?');
filtering_ostream out(make_iterator_range(first));
write_data_in_chunks(out);
ifstream second(test.name().c_str());
BOOST_CHECK_MESSAGE(
compare_container_and_stream(first, second),
"failed writing to filtering_ostream based on forward_iterator sequence "
"in chunks with large buffer"
);
}
}

#endif // #ifndef BOOST_IOSTREAMS_TEST_WRITE_OUTPUT_SEQUENCE_HPP_INCLUDED
Loading