Skip to content

Commit a16e949

Browse files
committed
Tests: ispanstream added assign and swap
1 parent 222f11f commit a16e949

File tree

10 files changed

+427
-172
lines changed

10 files changed

+427
-172
lines changed

libcxx/include/spanstream

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,37 @@ protected:
167167
if ((__which & ios_base::in) && (__which & ios_base::out) && (ios_base::cur == __way))
168168
return __error;
169169

170-
off_type __baseoff = [this, __way, __which] {
171-
switch (__way) {
172-
case ios_base::beg:
173-
return off_type(0);
174-
175-
case ios_base::cur:
176-
if (__which & ios_base::out)
177-
return off_type(this->pptr() - this->pbase());
178-
return off_type(this->gptr() - this->eback());
179-
180-
case ios_base::end:
181-
if ((__which & ios_base::out) && !(__which & ios_base::in))
182-
return off_type(this->pptr() - this->pbase());
183-
return off_type(__buf_.size());
184-
}
185-
}();
170+
// Calculate __baseoff
171+
172+
off_type __baseoff;
173+
174+
switch (__way) {
175+
case ios_base::beg:
176+
__baseoff = off_type(0);
177+
break;
178+
179+
case ios_base::cur:
180+
if (__which & ios_base::out)
181+
__baseoff = off_type(this->pptr() - this->pbase());
182+
else
183+
__baseoff = off_type(this->gptr() - this->eback());
184+
break;
185+
186+
case ios_base::end:
187+
if ((__which & ios_base::out) && !(__which & ios_base::in))
188+
__baseoff = off_type(this->pptr() - this->pbase());
189+
else
190+
__baseoff = off_type(__buf_.size());
191+
break;
192+
193+
default:
194+
return __error;
195+
};
196+
197+
// Calculate __newoff
186198

187199
off_type __newoff;
200+
188201
if (__builtin_add_overflow(__baseoff, __off, &__newoff) || (__newoff < off_type(0)) ||
189202
(std::cmp_greater(__newoff, __buf_.size())))
190203
return __error;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// <spanstream>
12+
13+
// template<class charT, class traits = char_traits<charT>>
14+
// class basic_ispanstream
15+
// : public basic_streambuf<charT, traits> {
16+
17+
// // [spanbuf.cons], constructors
18+
//
19+
// basic_ispanstream& operator=(basic_ispanstream&& rhs);
20+
21+
#include <cassert>
22+
#include <concepts>
23+
#include <span>
24+
#include <spanstream>
25+
26+
#include "constexpr_char_traits.h"
27+
#include "test_convertible.h"
28+
#include "test_macros.h"
29+
30+
template <typename CharT, typename TraitsT = std::char_traits<CharT>>
31+
void test() {
32+
using SpStream = std::basic_ispanstream<CharT, TraitsT>;
33+
34+
CharT arr[4];
35+
std::span<CharT> sp{arr};
36+
37+
// Mode: default
38+
{
39+
SpStream rhsSpSt{sp};
40+
SpStream spSt = std::move(rhsSpSt);
41+
assert(spSt.span().data() == arr);
42+
assert(!spSt.span().empty());
43+
assert(spSt.span().size() == 4);
44+
}
45+
// Mode: `ios_base::in`
46+
{
47+
SpStream rhsSpSt{sp, std::ios_base::in};
48+
SpStream spSt = std::move(rhsSpSt);
49+
assert(spSt.span().data() == arr);
50+
assert(!spSt.span().empty());
51+
assert(spSt.span().size() == 4);
52+
}
53+
// Mode `ios_base::out`
54+
{
55+
SpStream rhsSpSt{sp, std::ios_base::out};
56+
SpStream spSt = std::move(rhsSpSt);
57+
assert(spSt.span().data() == arr);
58+
assert(spSt.span().empty());
59+
assert(spSt.span().size() == 0);
60+
}
61+
// Mode: multiple
62+
{
63+
SpStream rhsSpSt{sp, std::ios_base::in | std::ios_base::out | std::ios_base::binary};
64+
SpStream spSt = std::move(rhsSpSt);
65+
assert(spSt.span().data() == arr);
66+
assert(spSt.span().empty());
67+
assert(spSt.span().size() == 0);
68+
}
69+
}
70+
71+
int main(int, char**) {
72+
test<char>();
73+
test<char, constexpr_char_traits<char>>();
74+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
75+
test<wchar_t>();
76+
test<wchar_t, constexpr_char_traits<wchar_t>>();
77+
#endif
78+
79+
return 0;
80+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// <spanstream>
12+
13+
// template<class charT, class traits = char_traits<charT>>
14+
// class basic_ispanstream
15+
// : public basic_streambuf<charT, traits> {
16+
17+
// // [ispanstream.swap], swap
18+
// void swap(basic_ispanstream& rhs);
19+
20+
#include <cassert>
21+
#include <concepts>
22+
#include <span>
23+
#include <spanstream>
24+
25+
#include "constexpr_char_traits.h"
26+
#include "test_convertible.h"
27+
#include "test_macros.h"
28+
29+
template <typename CharT, typename TraitsT = std::char_traits<CharT>>
30+
void test() {
31+
using SpStream = std::basic_ispanstream<CharT, TraitsT>;
32+
33+
CharT arr[4];
34+
std::span<CharT> sp{arr};
35+
36+
// Mode: default
37+
{
38+
SpStream rhsSpSt{sp};
39+
SpStream spSt(std::span<CharT>{});
40+
spSt.swap(rhsSpSt);
41+
assert(spSt.span().data() == arr);
42+
assert(!spSt.span().empty());
43+
assert(spSt.span().size() == 4);
44+
}
45+
// Mode: `ios_base::in`
46+
{
47+
SpStream rhsSpSt{sp, std::ios_base::in};
48+
SpStream spSt(std::span<CharT>{});
49+
spSt.swap(rhsSpSt);
50+
assert(spSt.span().data() == arr);
51+
assert(!spSt.span().empty());
52+
assert(spSt.span().size() == 4);
53+
}
54+
// Mode `ios_base::out`
55+
{
56+
SpStream rhsSpSt{sp, std::ios_base::out};
57+
SpStream spSt(std::span<CharT>{});
58+
spSt.swap(rhsSpSt);
59+
assert(spSt.span().data() == arr);
60+
assert(spSt.span().empty());
61+
assert(spSt.span().size() == 0);
62+
}
63+
// Mode: multiple
64+
{
65+
SpStream rhsSpSt{sp, std::ios_base::in | std::ios_base::out | std::ios_base::binary};
66+
SpStream spSt(std::span<CharT>{});
67+
spSt.swap(rhsSpSt);
68+
assert(spSt.span().data() == arr);
69+
assert(spSt.span().empty());
70+
assert(spSt.span().size() == 0);
71+
}
72+
}
73+
74+
int main(int, char**) {
75+
test<char>();
76+
test<char, constexpr_char_traits<char>>();
77+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
78+
test<wchar_t>();
79+
test<wchar_t, constexpr_char_traits<wchar_t>>();
80+
#endif
81+
82+
return 0;
83+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
11+
// <spanstream>
12+
13+
// template<class charT, class traits>
14+
// void swap(basic_ispanstream<charT, traits>& x, basic_ispanstream<charT, traits>& y);
15+
16+
#include <cassert>
17+
#include <concepts>
18+
#include <span>
19+
#include <spanstream>
20+
21+
#include "constexpr_char_traits.h"
22+
#include "test_convertible.h"
23+
#include "test_macros.h"
24+
25+
template <typename CharT, typename TraitsT = std::char_traits<CharT>>
26+
void test() {
27+
using SpStream = std::basic_ispanstream<CharT, TraitsT>;
28+
29+
CharT arr[4];
30+
std::span<CharT> sp{arr};
31+
32+
// Mode: default
33+
{
34+
SpStream rhsSpSt{sp};
35+
SpStream spSt(std::span<CharT>{});
36+
std::swap(spSt, rhsSpSt);
37+
assert(spSt.span().data() == arr);
38+
assert(!spSt.span().empty());
39+
assert(spSt.span().size() == 4);
40+
}
41+
// Mode: `ios_base::in`
42+
{
43+
SpStream rhsSpSt{sp, std::ios_base::in};
44+
SpStream spSt(std::span<CharT>{});
45+
std::swap(spSt, rhsSpSt);
46+
assert(spSt.span().data() == arr);
47+
assert(!spSt.span().empty());
48+
assert(spSt.span().size() == 4);
49+
}
50+
// Mode `ios_base::out`
51+
{
52+
SpStream rhsSpSt{sp, std::ios_base::out};
53+
SpStream spSt(std::span<CharT>{});
54+
std::swap(spSt, rhsSpSt);
55+
assert(spSt.span().data() == arr);
56+
assert(spSt.span().empty());
57+
assert(spSt.span().size() == 0);
58+
}
59+
// Mode: multiple
60+
{
61+
SpStream rhsSpSt{sp, std::ios_base::in | std::ios_base::out | std::ios_base::binary};
62+
SpStream spSt(std::span<CharT>{});
63+
std::swap(spSt, rhsSpSt);
64+
assert(spSt.span().data() == arr);
65+
assert(spSt.span().empty());
66+
assert(spSt.span().size() == 0);
67+
}
68+
}
69+
70+
int main(int, char**) {
71+
test<char>();
72+
test<char, constexpr_char_traits<char>>();
73+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
74+
test<wchar_t>();
75+
test<wchar_t, constexpr_char_traits<wchar_t>>();
76+
#endif
77+
78+
return 0;
79+
}

libcxx/test/std/input.output/span.streams/ispanstream/ispanstream.cons/move.pass.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,35 @@ void test() {
3636

3737
// Mode: default
3838
{
39-
SpStream rhsSpStream{sp};
40-
SpStream sps(std::move(rhsSpStream));
41-
assert(sps.span().data() == arr);
42-
assert(!sps.span().empty());
43-
assert(sps.span().size() == 4);
39+
SpStream rhsSpSt{sp};
40+
SpStream spSt(std::move(rhsSpSt));
41+
assert(spSt.span().data() == arr);
42+
assert(!spSt.span().empty());
43+
assert(spSt.span().size() == 4);
4444
}
4545
// Mode: `ios_base::in`
4646
{
47-
SpStream rhsSpStream{sp, std::ios_base::in};
48-
SpStream sps(std::move(rhsSpStream));
49-
assert(sps.span().data() == arr);
50-
assert(!sps.span().empty());
51-
assert(sps.span().size() == 4);
47+
SpStream rhsSpSt{sp, std::ios_base::in};
48+
SpStream spSt(std::move(rhsSpSt));
49+
assert(spSt.span().data() == arr);
50+
assert(!spSt.span().empty());
51+
assert(spSt.span().size() == 4);
5252
}
5353
// Mode `ios_base::out`
5454
{
55-
SpStream rhsSpStream{sp, std::ios_base::out};
56-
SpStream sps(std::move(rhsSpStream));
57-
assert(sps.span().data() == arr);
58-
assert(sps.span().empty());
59-
assert(sps.span().size() == 0);
55+
SpStream rhsSpSt{sp, std::ios_base::out};
56+
SpStream spSt(std::move(rhsSpSt));
57+
assert(spSt.span().data() == arr);
58+
assert(spSt.span().empty());
59+
assert(spSt.span().size() == 0);
6060
}
6161
// Mode: multiple
6262
{
63-
SpStream rhsSpStream{sp, std::ios_base::in | std::ios_base::out | std::ios_base::binary};
64-
SpStream sps(std::move(rhsSpStream));
65-
assert(sps.span().data() == arr);
66-
assert(sps.span().empty());
67-
assert(sps.span().size() == 0);
63+
SpStream rhsSpSt{sp, std::ios_base::in | std::ios_base::out | std::ios_base::binary};
64+
SpStream spSt(std::move(rhsSpSt));
65+
assert(spSt.span().data() == arr);
66+
assert(spSt.span().empty());
67+
assert(spSt.span().size() == 0);
6868
}
6969
}
7070

0 commit comments

Comments
 (0)