File: fast_streambuf_iterator_test.cpp

package info (click to toggle)
seqan3 3.0.2%2Bds-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,052 kB
  • sloc: cpp: 144,641; makefile: 1,288; ansic: 294; sh: 228; xml: 217; javascript: 50; python: 27; php: 25
file content (213 lines) | stat: -rw-r--r-- 7,020 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// -----------------------------------------------------------------------------------------------------
// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
// -----------------------------------------------------------------------------------------------------

#include <gtest/gtest.h>

#include <seqan3/io/stream/iterator.hpp>
#include <seqan3/std/iterator>

// -----------------------------------------------------------------------------
// fast_istreambuf_iterator
// -----------------------------------------------------------------------------

TEST(fast_istreambuf_iterator, concept)
{
    EXPECT_TRUE(std::input_iterator<seqan3::detail::fast_istreambuf_iterator<char>>);
    EXPECT_FALSE(std::forward_iterator<seqan3::detail::fast_istreambuf_iterator<char>>);
    EXPECT_FALSE(std::bidirectional_iterator<seqan3::detail::fast_istreambuf_iterator<char>>);
    EXPECT_FALSE(std::random_access_iterator<seqan3::detail::fast_istreambuf_iterator<char>>);
    EXPECT_FALSE((std::output_iterator<seqan3::detail::fast_istreambuf_iterator<char>, char>));
}

TEST(fast_istreambuf_iterator, construction)
{
    using type = seqan3::detail::fast_istreambuf_iterator<char>;
    EXPECT_TRUE(std::is_nothrow_default_constructible_v<type>);
    EXPECT_TRUE(std::is_nothrow_copy_constructible_v<type>);
    EXPECT_TRUE(std::is_nothrow_move_constructible_v<type>);
    EXPECT_TRUE(std::is_nothrow_copy_assignable_v<type>);
    EXPECT_TRUE(std::is_nothrow_move_assignable_v<type>);
    EXPECT_TRUE(std::is_nothrow_destructible_v<type>);
    EXPECT_TRUE((std::is_constructible_v<type, std::basic_streambuf<char> &>));
}

TEST(fast_istreambuf_iterator, basic)
{
    std::istringstream str{"test"};
    seqan3::detail::fast_istreambuf_iterator<char> it{*str.rdbuf()};

    EXPECT_EQ(*it, 't');
    ++it;
    EXPECT_EQ(*it, 'e');
    it++;
    EXPECT_EQ(*it, 's');
}

TEST(fast_istreambuf_iterator, comparison)
{
    std::istringstream str{"test\n"};
    seqan3::detail::fast_istreambuf_iterator<char> it{*str.rdbuf()};

    EXPECT_FALSE(it == std::default_sentinel);
    EXPECT_FALSE(std::default_sentinel == it);
    EXPECT_TRUE(it != std::default_sentinel);
    EXPECT_TRUE(std::default_sentinel != it);
}

// -----------------------------------------------------------------------------
// fast_ostreambuf_iterator
// -----------------------------------------------------------------------------

TEST(fast_ostreambuf_iterator, concept)
{
    using type = seqan3::detail::fast_ostreambuf_iterator<char>;

    EXPECT_TRUE((std::output_iterator<type, char>));

    EXPECT_FALSE(std::input_iterator<type>);
    EXPECT_FALSE(std::forward_iterator<type>);
    EXPECT_FALSE(std::bidirectional_iterator<type>);
    EXPECT_FALSE(std::random_access_iterator<type>);
}

TEST(fast_ostreambuf_iterator, construction)
{
    using type = seqan3::detail::fast_ostreambuf_iterator<char>;

    EXPECT_TRUE(std::is_nothrow_default_constructible_v<type>);
    EXPECT_TRUE(std::is_nothrow_copy_constructible_v<type>);
    EXPECT_TRUE(std::is_nothrow_move_constructible_v<type>);
    EXPECT_TRUE(std::is_nothrow_copy_assignable_v<type>);
    EXPECT_TRUE(std::is_nothrow_move_assignable_v<type>);
    EXPECT_TRUE(std::is_nothrow_destructible_v<type>);
    EXPECT_TRUE((std::is_constructible_v<type, std::basic_streambuf<char> &>));
}

TEST(fast_ostreambuf_iterator, assignment)
{
    std::ostringstream ostr{};
    seqan3::detail::fast_ostreambuf_iterator<char> it{*ostr.rdbuf()};

    it = 't';
    *it = 'e';

    it++; // no op
    ++it; // no op

    *it++ = 's';
    *++it = 't';

    EXPECT_EQ(ostr.str(), std::string_view{"test"});
}

TEST(fast_ostreambuf_iterator, write_range_simple_case)
{
    // the simple case without using the return value of write_value
    std::ostringstream ostr{};
    seqan3::detail::fast_ostreambuf_iterator<char> it{*ostr.rdbuf()};

    std::string rng{"test\ntestest"};

    it.write_range(rng);

    EXPECT_EQ(ostr.str(), std::string_view{"test\ntestest"});
}

TEST(fast_ostreambuf_iterator, write_range_ensure_overflow)
{
    // ensure that buffer overflows at least once
    std::ostringstream ostr;
    char buf[40];
    ostr.rdbuf()->pubsetbuf(buf, sizeof buf);

    seqan3::detail::fast_ostreambuf_iterator<char> it{*ostr.rdbuf()};

    std::string rng{"veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylargerange"};

    it.write_range(rng);

    EXPECT_EQ(ostr.str().substr(0, 78),
              std::string_view{"veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylargerange"});
}

TEST(fast_ostreambuf_iterator, write_range_return_value)
{
    // use the return value of write_range to keep track of the chunk you have already written
    std::ostringstream ostr{};
    seqan3::detail::fast_ostreambuf_iterator<char> it{*ostr.rdbuf()};

    std::string rng{"test\ntestest"};
    using str_it_t = std::string::iterator;

    auto rng_it = it.write_range(std::ranges::subrange<str_it_t, str_it_t>(rng.begin(), rng.begin() + 5));

    EXPECT_EQ(rng_it, (rng.begin() + 5));
    EXPECT_EQ(ostr.str(), std::string_view{"test\n"});

    rng_it = it.write_range(std::ranges::subrange<str_it_t, str_it_t>(rng.begin() + 5, rng.end()));

    EXPECT_EQ(rng_it, rng.end());
    EXPECT_EQ(ostr.str(), std::string_view{"test\ntestest"});
}

TEST(fast_ostreambuf_iterator, write_range_unsafe_range)
{
    // write_range on a non-forwarding range returns void
    std::ostringstream ostr{};
    seqan3::detail::fast_ostreambuf_iterator<char> it{*ostr.rdbuf()};

    EXPECT_TRUE((std::same_as<void, decltype(it.write_range(std::string{"foo"}))>));

    it.write_range(std::string{"foo"});

    EXPECT_EQ(ostr.str(), std::string_view{"foo"});
}

TEST(fast_ostreambuf_iterator, write_number)
{
    {
        std::ostringstream ostr;
        char buf[400];
        ostr.rdbuf()->pubsetbuf(buf, sizeof buf);

        seqan3::detail::fast_ostreambuf_iterator<char> it{*ostr.rdbuf()};

        uint64_t num{54389234};

        it.write_number(num);

        EXPECT_EQ(ostr.str().substr(0, 8), std::string_view{"54389234"});
    }

    {
        std::ostringstream ostr;
        char buf[100];
        ostr.rdbuf()->pubsetbuf(buf, sizeof buf);

        seqan3::detail::fast_ostreambuf_iterator<char> it{*ostr.rdbuf()};

        uint64_t num{54389234};

        it.write_number(num);

        EXPECT_EQ(ostr.str().substr(0, 8), std::string_view{"54389234"});
    }
}

TEST(fast_ostreambuf_iterator, write_end_of_line)
{
    std::ostringstream ostr{};
    seqan3::detail::fast_ostreambuf_iterator<char> it{*ostr.rdbuf()};

    ostr << "test";
    it.write_end_of_line(false);

    ostr << "testest";
    it.write_end_of_line(true);

    EXPECT_EQ(ostr.str(), std::string_view{"test\ntestest\r\n"});
}